TLS: handle cert chains
This commit is contained in:
parent
43bc1d7ec8
commit
8cd07bb273
@ -185,6 +185,72 @@ Handle<Value> SecureContext::SetKey(const Arguments& args) {
|
||||
}
|
||||
|
||||
|
||||
// Read a file that contains our certificate in "PEM" format,
|
||||
// possibly followed by a sequence of CA certificates that should be
|
||||
// sent to the peer in the Certificate message.
|
||||
//
|
||||
// Taken from OpenSSL - editted for style.
|
||||
int SSL_CTX_use_certificate_chain(SSL_CTX *ctx, BIO *in) {
|
||||
int ret = 0;
|
||||
X509 *x = NULL;
|
||||
|
||||
x = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
|
||||
|
||||
if (x == NULL) {
|
||||
SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = SSL_CTX_use_certificate(ctx, x);
|
||||
|
||||
if (ERR_peek_error() != 0) {
|
||||
// Key/certificate mismatch doesn't imply ret==0 ...
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
// If we could set up our certificate, now proceed to
|
||||
// the CA certificates.
|
||||
X509 *ca;
|
||||
int r;
|
||||
unsigned long err;
|
||||
|
||||
if (ctx->extra_certs != NULL) {
|
||||
sk_X509_pop_free(ctx->extra_certs, X509_free);
|
||||
ctx->extra_certs = NULL;
|
||||
}
|
||||
|
||||
while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
|
||||
r = SSL_CTX_add_extra_chain_cert(ctx, ca);
|
||||
|
||||
if (!r) {
|
||||
X509_free(ca);
|
||||
ret = 0;
|
||||
goto end;
|
||||
}
|
||||
// Note that we must not free r if it was successfully
|
||||
// added to the chain (while we must free the main
|
||||
// certificate, since its reference count is increased
|
||||
// by SSL_CTX_use_certificate).
|
||||
}
|
||||
|
||||
// When the while loop ends, it's usually just EOF.
|
||||
err = ERR_peek_last_error();
|
||||
if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
|
||||
ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
|
||||
ERR_clear_error();
|
||||
} else {
|
||||
// some real error
|
||||
ret = 0;
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
if (x != NULL) X509_free(x);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> SecureContext::SetCert(const Arguments& args) {
|
||||
HandleScope scope;
|
||||
|
||||
@ -195,11 +261,23 @@ Handle<Value> SecureContext::SetCert(const Arguments& args) {
|
||||
String::New("Bad parameter")));
|
||||
}
|
||||
|
||||
X509* x509 = LoadX509(args[0]);
|
||||
if (!x509) return False();
|
||||
BIO* bio = LoadBIO(args[0]);
|
||||
if (!bio) return False();
|
||||
|
||||
SSL_CTX_use_certificate(sc->ctx_, x509);
|
||||
X509_free(x509);
|
||||
int rv = SSL_CTX_use_certificate_chain(sc->ctx_, bio);
|
||||
|
||||
BIO_free(bio);
|
||||
|
||||
if (!rv) {
|
||||
unsigned long err = ERR_get_error();
|
||||
if (!err) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("SSL_CTX_use_certificate_chain")));
|
||||
}
|
||||
char string[120];
|
||||
ERR_error_string(err, string);
|
||||
return ThrowException(Exception::Error(String::New(string)));
|
||||
}
|
||||
|
||||
return True();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user