2013-01-17 23 views
7

Sto cercando di autorizzare il cliente con autofirmato.Autorizzazione ssl client su node.js

In primo luogo, I`m creazione di certificati:

certificato CA

openssl genrsa -des3 -out ca.key 2048 
openssl req -new -x509 -days 365 -key ca.key -out ca.crt 

certificato Server

openssl genrsa -out server.key 1024 
openssl req -new -key server.key -out server.csr 
openssl x509 -req -in server.csr -out server.crt -CA ca.crt -CAkey ca.key -CAcreateserial -days 365 

sertificate client

openssl genrsa -out client.key 1024 
openssl req -new -key client.key -out client.csr 
openssl x509 -req -in client.csr -out client.crt -CA ca.crt -CAkey ca.key -CAcreateserial -days 365 

Converti cliente certi ficato di p12

openssl pkcs12 -export -in client.crt -inkey client.key -name "My cert" -out client.p12 

Apri e installare il certificato p12 aperta client.p12

mio node.js del server (utilizzando express.js)

var express = require('express') 
    , routes = require('./routes') 
    , user = require('./routes/user') 
    , http = require('http') 
    , path = require('path') 
    , https = require('https') 
    , fs = require('fs'); 

var app = express(); 

app.configure(function() { 
    app.set('port', process.env.PORT || 3000); 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'ejs'); 
    app.use(express.favicon()); 
    app.use(express.logger('dev')); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(app.router); 
    app.use(express.static(path.join(__dirname, 'public'))); 
}); 

app.configure('development', function() { 
    app.use(express.errorHandler()); 
}); 

app.get('/', function(req, res) { 
    console.log(req.client.authorized); 
    res.send(req.client.authorized) 
}); 

var options = { 
    key:fs.readFileSync('ssl/server.key'), 
    cert:fs.readFileSync('ssl/server.crt'), 
    ca:[fs.readFileSync('ssl/ca.crt')], 
    requestCert:true, 
    rejectUnauthorized:false, 
    passphrase: 'passphrase', 
    agent: false 
    }; 

    https.createServer(options,app).listen(app.get('port'), function() { 
     console.log("Express server listening on port " + app.get('port')); 
    }); 

Quando i server è in esecuzione, apro https://localhost:3000 in Chrome, ma l'autenticazione non passa: req.client.authorized è false

Il messaggio di Chrome è

The identity of this website has not been verified. 
• Server's certificate does not match the URL. 

Dov'è il mio errore?

risposta

2

Con il supporto HTTPS, utilizzare request.connection.verifyPeer() e request.connection.getPeerCertificate() per ottenere i dettagli di autenticazione del client.

http://nodejs.org/api/http.html#http_request_connection

+0

La richiesta di certificato client è già attiva nell'esempio di codice tramite 'requestCert: true'. Il server non può richiedere un certificato client in un gestore richieste; deve essere configurato nelle opzioni del server, poiché lo scambio di certificati avviene come parte dell'handshake SSL (vale a dire prima che la richiesta venga gestita). – ttreitlinger

3

URL del server viene confrontato con il nome della parte comune del certificato del server.

Quando si crea la richiesta di certificato del server, ricordare di inserire il nome host del server nella parte Nome comune. Se stai testando localmente (usando https://localhost come indirizzo) usa localhost come Common Name.

Problemi correlati