summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorreyk <>2014-09-28 14:45:48 +0000
committerreyk <>2014-09-28 14:45:48 +0000
commit86dd9a4f816c164cfa45e157991a16f15badb4a3 (patch)
tree2558f6bbdff94c43a1f9a53c84ba5f0ffa3ea8c0
parent091f5c3e6dfe57ebde616bd69bdc1866949e19a4 (diff)
downloadopenbsd-86dd9a4f816c164cfa45e157991a16f15badb4a3.tar.gz
openbsd-86dd9a4f816c164cfa45e157991a16f15badb4a3.tar.bz2
openbsd-86dd9a4f816c164cfa45e157991a16f15badb4a3.zip
Add a new API function SSL_CTX_use_certificate_chain() that allows to
read the PEM-encoded certificate chain from memory instead of a file. This idea is derived from an older implementation in relayd that was needed to use the function with a privep'ed process in a chroot. Now it is time to get it into LibreSSL to make the API more privsep- friendly and to make it available for other programs and the ressl library. ok jsing@ miod@
-rw-r--r--src/lib/libressl/ressl.c21
-rw-r--r--src/lib/libssl/src/doc/ssl/SSL_CTX_use_certificate.pod10
-rw-r--r--src/lib/libssl/src/ssl/ssl.h3
-rw-r--r--src/lib/libssl/src/ssl/ssl_rsa.c64
-rw-r--r--src/lib/libssl/ssl.h3
-rw-r--r--src/lib/libssl/ssl_rsa.c64
6 files changed, 111 insertions, 54 deletions
diff --git a/src/lib/libressl/ressl.c b/src/lib/libressl/ressl.c
index 1bf971419b..f01448b8f4 100644
--- a/src/lib/libressl/ressl.c
+++ b/src/lib/libressl/ressl.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ressl.c,v 1.13 2014/09/28 06:24:00 tedu Exp $ */ 1/* $OpenBSD: ressl.c,v 1.14 2014/09/28 14:45:48 reyk Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -110,22 +110,11 @@ ressl_configure_keypair(struct ressl *ctx)
110 BIO *bio = NULL; 110 BIO *bio = NULL;
111 111
112 if (ctx->config->cert_mem != NULL) { 112 if (ctx->config->cert_mem != NULL) {
113 if ((bio = BIO_new_mem_buf(ctx->config->cert_mem, 113 if (SSL_CTX_use_certificate_chain(ctx->ssl_ctx,
114 ctx->config->cert_len)) == NULL) { 114 ctx->config->cert_mem, ctx->config->cert_len) != 1) {
115 ressl_set_error(ctx, "failed to create buffer");
116 goto err;
117 }
118 if ((cert = PEM_read_bio_X509(bio, NULL, NULL, NULL)) == NULL) {
119 ressl_set_error(ctx, "failed to read certificate");
120 goto err;
121 }
122 if (SSL_CTX_use_certificate(ctx->ssl_ctx, cert) != 1) {
123 ressl_set_error(ctx, "failed to load certificate"); 115 ressl_set_error(ctx, "failed to load certificate");
124 goto err; 116 goto err;
125 } 117 }
126 BIO_free(bio);
127 bio = NULL;
128 X509_free(cert);
129 cert = NULL; 118 cert = NULL;
130 } 119 }
131 if (ctx->config->key_mem != NULL) { 120 if (ctx->config->key_mem != NULL) {
@@ -150,8 +139,8 @@ ressl_configure_keypair(struct ressl *ctx)
150 } 139 }
151 140
152 if (ctx->config->cert_file != NULL) { 141 if (ctx->config->cert_file != NULL) {
153 if (SSL_CTX_use_certificate_file(ctx->ssl_ctx, 142 if (SSL_CTX_use_certificate_chain_file(ctx->ssl_ctx,
154 ctx->config->cert_file, SSL_FILETYPE_PEM) != 1) { 143 ctx->config->cert_file) != 1) {
155 ressl_set_error(ctx, "failed to load certificate file"); 144 ressl_set_error(ctx, "failed to load certificate file");
156 goto err; 145 goto err;
157 } 146 }
diff --git a/src/lib/libssl/src/doc/ssl/SSL_CTX_use_certificate.pod b/src/lib/libssl/src/doc/ssl/SSL_CTX_use_certificate.pod
index 8e0d609d05..560e00937f 100644
--- a/src/lib/libssl/src/doc/ssl/SSL_CTX_use_certificate.pod
+++ b/src/lib/libssl/src/doc/ssl/SSL_CTX_use_certificate.pod
@@ -3,8 +3,9 @@
3=head1 NAME 3=head1 NAME
4 4
5SSL_CTX_use_certificate, SSL_CTX_use_certificate_ASN1, 5SSL_CTX_use_certificate, SSL_CTX_use_certificate_ASN1,
6SSL_CTX_use_certificate_file, SSL_use_certificate, SSL_use_certificate_ASN1, 6SSL_CTX_use_certificate_file, SSL_use_certificate,
7SSL_use_certificate_file, SSL_CTX_use_certificate_chain_file, 7SSL_use_certificate_ASN1, SSL_use_certificate_file,
8SSL_CTX_use_certificate_chain, SSL_CTX_use_certificate_chain_file,
8SSL_CTX_use_PrivateKey, SSL_CTX_use_PrivateKey_ASN1, 9SSL_CTX_use_PrivateKey, SSL_CTX_use_PrivateKey_ASN1,
9SSL_CTX_use_PrivateKey_file, SSL_CTX_use_RSAPrivateKey, 10SSL_CTX_use_PrivateKey_file, SSL_CTX_use_RSAPrivateKey,
10SSL_CTX_use_RSAPrivateKey_ASN1, SSL_CTX_use_RSAPrivateKey_file, 11SSL_CTX_use_RSAPrivateKey_ASN1, SSL_CTX_use_RSAPrivateKey_file,
@@ -24,6 +25,7 @@ data
24 int SSL_use_certificate_ASN1(SSL *ssl, unsigned char *d, int len); 25 int SSL_use_certificate_ASN1(SSL *ssl, unsigned char *d, int len);
25 int SSL_use_certificate_file(SSL *ssl, const char *file, int type); 26 int SSL_use_certificate_file(SSL *ssl, const char *file, int type);
26 27
28 int SSL_CTX_use_certificate_chain(SSL_CTX *ctx, void *buf, int len);
27 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file); 29 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file);
28 30
29 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey); 31 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey);
@@ -75,8 +77,8 @@ SSL_use_certificate_file() loads the certificate from B<file> into B<ssl>.
75See the NOTES section on why SSL_CTX_use_certificate_chain_file() 77See the NOTES section on why SSL_CTX_use_certificate_chain_file()
76should be preferred. 78should be preferred.
77 79
78SSL_CTX_use_certificate_chain_file() loads a certificate chain from 80The SSL_CTX_use_certificate_chain*() functions load a certificate chain
79B<file> into B<ctx>. The certificates must be in PEM format and must 81into B<ctx>. The certificates must be in PEM format and must
80be sorted starting with the subject's certificate (actual client or server 82be sorted starting with the subject's certificate (actual client or server
81certificate), followed by intermediate CA certificates if applicable, and 83certificate), followed by intermediate CA certificates if applicable, and
82ending at the highest level (root) CA. 84ending at the highest level (root) CA.
diff --git a/src/lib/libssl/src/ssl/ssl.h b/src/lib/libssl/src/ssl/ssl.h
index 3b948245f2..c7dd4259cc 100644
--- a/src/lib/libssl/src/ssl/ssl.h
+++ b/src/lib/libssl/src/ssl/ssl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl.h,v 1.65 2014/09/27 11:01:06 jsing Exp $ */ 1/* $OpenBSD: ssl.h,v 1.66 2014/09/28 14:45:48 reyk Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -1530,6 +1530,7 @@ int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type);
1530int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type); 1530int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type);
1531int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type); 1531int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type);
1532int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file); /* PEM type */ 1532int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file); /* PEM type */
1533int SSL_CTX_use_certificate_chain(SSL_CTX *ctx, void *buf, int len);
1533STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file); 1534STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file);
1534int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stackCAs, 1535int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stackCAs,
1535 const char *file); 1536 const char *file);
diff --git a/src/lib/libssl/src/ssl/ssl_rsa.c b/src/lib/libssl/src/ssl/ssl_rsa.c
index d4d14bad35..e8b72f016e 100644
--- a/src/lib/libssl/src/ssl/ssl_rsa.c
+++ b/src/lib/libssl/src/ssl/ssl_rsa.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl_rsa.c,v 1.16 2014/07/12 16:03:37 miod Exp $ */ 1/* $OpenBSD: ssl_rsa.c,v 1.17 2014/09/28 14:45:48 reyk Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -66,6 +66,8 @@
66 66
67static int ssl_set_cert(CERT *c, X509 *x509); 67static int ssl_set_cert(CERT *c, X509 *x509);
68static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey); 68static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
69static int ssl_ctx_use_certificate_chain_bio(SSL_CTX *, BIO *);
70
69int 71int
70SSL_use_certificate(SSL *ssl, X509 *x) 72SSL_use_certificate(SSL *ssl, X509 *x)
71{ 73{
@@ -637,30 +639,18 @@ SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const unsigned char *d,
637 639
638 640
639/* 641/*
640 * Read a file that contains our certificate in "PEM" format, 642 * Read a bio that contains our certificate in "PEM" format,
641 * possibly followed by a sequence of CA certificates that should be 643 * possibly followed by a sequence of CA certificates that should be
642 * sent to the peer in the Certificate message. 644 * sent to the peer in the Certificate message.
643 */ 645 */
644int 646static int
645SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file) 647ssl_ctx_use_certificate_chain_bio(SSL_CTX *ctx, BIO *in)
646{ 648{
647 BIO *in;
648 int ret = 0; 649 int ret = 0;
649 X509 *x = NULL; 650 X509 *x = NULL;
650 651
651 ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */ 652 ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
652 653
653 in = BIO_new(BIO_s_file_internal());
654 if (in == NULL) {
655 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
656 goto end;
657 }
658
659 if (BIO_read_filename(in, file) <= 0) {
660 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
661 goto end;
662 }
663
664 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, 654 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
665 ctx->default_passwd_callback_userdata); 655 ctx->default_passwd_callback_userdata);
666 if (x == NULL) { 656 if (x == NULL) {
@@ -716,6 +706,48 @@ SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
716end: 706end:
717 if (x != NULL) 707 if (x != NULL)
718 X509_free(x); 708 X509_free(x);
709 return (ret);
710}
711
712int
713SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
714{
715 BIO *in;
716 int ret = 0;
717
718 in = BIO_new(BIO_s_file_internal());
719 if (in == NULL) {
720 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
721 goto end;
722 }
723
724 if (BIO_read_filename(in, file) <= 0) {
725 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
726 goto end;
727 }
728
729 ret = ssl_ctx_use_certificate_chain_bio(ctx, in);
730
731end:
732 BIO_free(in);
733 return (ret);
734}
735
736int
737SSL_CTX_use_certificate_chain(SSL_CTX *ctx, void *buf, int len)
738{
739 BIO *in;
740 int ret = 0;
741
742 in = BIO_new_mem_buf(buf, len);
743 if (in == NULL) {
744 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
745 goto end;
746 }
747
748 ret = ssl_ctx_use_certificate_chain_bio(ctx, in);
749
750end:
719 BIO_free(in); 751 BIO_free(in);
720 return (ret); 752 return (ret);
721} 753}
diff --git a/src/lib/libssl/ssl.h b/src/lib/libssl/ssl.h
index 3b948245f2..c7dd4259cc 100644
--- a/src/lib/libssl/ssl.h
+++ b/src/lib/libssl/ssl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl.h,v 1.65 2014/09/27 11:01:06 jsing Exp $ */ 1/* $OpenBSD: ssl.h,v 1.66 2014/09/28 14:45:48 reyk Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -1530,6 +1530,7 @@ int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type);
1530int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type); 1530int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type);
1531int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type); 1531int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type);
1532int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file); /* PEM type */ 1532int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file); /* PEM type */
1533int SSL_CTX_use_certificate_chain(SSL_CTX *ctx, void *buf, int len);
1533STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file); 1534STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file);
1534int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stackCAs, 1535int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stackCAs,
1535 const char *file); 1536 const char *file);
diff --git a/src/lib/libssl/ssl_rsa.c b/src/lib/libssl/ssl_rsa.c
index d4d14bad35..e8b72f016e 100644
--- a/src/lib/libssl/ssl_rsa.c
+++ b/src/lib/libssl/ssl_rsa.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl_rsa.c,v 1.16 2014/07/12 16:03:37 miod Exp $ */ 1/* $OpenBSD: ssl_rsa.c,v 1.17 2014/09/28 14:45:48 reyk Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -66,6 +66,8 @@
66 66
67static int ssl_set_cert(CERT *c, X509 *x509); 67static int ssl_set_cert(CERT *c, X509 *x509);
68static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey); 68static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
69static int ssl_ctx_use_certificate_chain_bio(SSL_CTX *, BIO *);
70
69int 71int
70SSL_use_certificate(SSL *ssl, X509 *x) 72SSL_use_certificate(SSL *ssl, X509 *x)
71{ 73{
@@ -637,30 +639,18 @@ SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const unsigned char *d,
637 639
638 640
639/* 641/*
640 * Read a file that contains our certificate in "PEM" format, 642 * Read a bio that contains our certificate in "PEM" format,
641 * possibly followed by a sequence of CA certificates that should be 643 * possibly followed by a sequence of CA certificates that should be
642 * sent to the peer in the Certificate message. 644 * sent to the peer in the Certificate message.
643 */ 645 */
644int 646static int
645SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file) 647ssl_ctx_use_certificate_chain_bio(SSL_CTX *ctx, BIO *in)
646{ 648{
647 BIO *in;
648 int ret = 0; 649 int ret = 0;
649 X509 *x = NULL; 650 X509 *x = NULL;
650 651
651 ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */ 652 ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
652 653
653 in = BIO_new(BIO_s_file_internal());
654 if (in == NULL) {
655 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
656 goto end;
657 }
658
659 if (BIO_read_filename(in, file) <= 0) {
660 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
661 goto end;
662 }
663
664 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, 654 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
665 ctx->default_passwd_callback_userdata); 655 ctx->default_passwd_callback_userdata);
666 if (x == NULL) { 656 if (x == NULL) {
@@ -716,6 +706,48 @@ SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
716end: 706end:
717 if (x != NULL) 707 if (x != NULL)
718 X509_free(x); 708 X509_free(x);
709 return (ret);
710}
711
712int
713SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
714{
715 BIO *in;
716 int ret = 0;
717
718 in = BIO_new(BIO_s_file_internal());
719 if (in == NULL) {
720 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
721 goto end;
722 }
723
724 if (BIO_read_filename(in, file) <= 0) {
725 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
726 goto end;
727 }
728
729 ret = ssl_ctx_use_certificate_chain_bio(ctx, in);
730
731end:
732 BIO_free(in);
733 return (ret);
734}
735
736int
737SSL_CTX_use_certificate_chain(SSL_CTX *ctx, void *buf, int len)
738{
739 BIO *in;
740 int ret = 0;
741
742 in = BIO_new_mem_buf(buf, len);
743 if (in == NULL) {
744 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
745 goto end;
746 }
747
748 ret = ssl_ctx_use_certificate_chain_bio(ctx, in);
749
750end:
719 BIO_free(in); 751 BIO_free(in);
720 return (ret); 752 return (ret);
721} 753}