Blob


1 #include "config.h"
3 #include <sys/types.h>
4 #include <sys/uio.h>
6 #include <limits.h>
7 #include <unistd.h>
8 #include <stdio.h>
10 #include <openssl/err.h>
11 #include <openssl/bio.h>
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include <openssl/x509.h>
15 #include <openssl/x509_vfy.h>
16 #include <openssl/pem.h>
17 #include <openssl/ssl.h>
19 #include "log.h"
21 #ifndef HAVE_SSL_CTX_LOAD_VERIFY_MEM
23 #define X509_LOOKUP_add_mem(x,iov,type) \
24 X509_LOOKUP_ctrl((x),X509_L_MEM,(const char *)(iov),\
25 (long)(type),NULL)
27 #define X509_L_MEM 3
29 #define SSL_ECDH_CURVE "prime256v1"
31 X509_LOOKUP_METHOD *
32 X509_LOOKUP_mem(void);
34 static int
35 X509_STORE_load_mem(X509_STORE *ctx, void *buf, int len)
36 {
37 X509_LOOKUP *lookup;
38 struct iovec iov;
39 lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_mem());
40 if (lookup == NULL)
41 return (0);
42 iov.iov_base = buf;
43 iov.iov_len = len;
44 if (X509_LOOKUP_add_mem(lookup, &iov, X509_FILETYPE_PEM) != 1)
45 return (0);
46 return (1);
47 }
49 int
50 SSL_CTX_load_verify_mem(SSL_CTX *ctx, void *buf, int len)
51 {
52 return (X509_STORE_load_mem(SSL_CTX_get_cert_store(ctx), buf, len));
53 }
55 #endif /* HAVE_SSL_CTX_LOAD_VERIFY_MEM */
57 #ifndef HAVE_SSL_CTX_USE_CERTIFICATE_CHAIN_MEM
58 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
59 * All rights reserved.
60 *
61 * This package is an SSL implementation written
62 * by Eric Young (eay@cryptsoft.com).
63 * The implementation was written so as to conform with Netscapes SSL.
64 *
65 * This library is free for commercial and non-commercial use as long as
66 * the following conditions are aheared to. The following conditions
67 * apply to all code found in this distribution, be it the RC4, RSA,
68 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
69 * included with this distribution is covered by the same copyright terms
70 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
71 *
72 * Copyright remains Eric Young's, and as such any Copyright notices in
73 * the code are not to be removed.
74 * If this package is used in a product, Eric Young should be given attribution
75 * as the author of the parts of the library used.
76 * This can be in the form of a textual message at program startup or
77 * in documentation (online or textual) provided with the package.
78 *
79 * Redistribution and use in source and binary forms, with or without
80 * modification, are permitted provided that the following conditions
81 * are met:
82 * 1. Redistributions of source code must retain the copyright
83 * notice, this list of conditions and the following disclaimer.
84 * 2. Redistributions in binary form must reproduce the above copyright
85 * notice, this list of conditions and the following disclaimer in the
86 * documentation and/or other materials provided with the distribution.
87 * 3. All advertising materials mentioning features or use of this software
88 * must display the following acknowledgement:
89 * "This product includes cryptographic software written by
90 * Eric Young (eay@cryptsoft.com)"
91 * The word 'cryptographic' can be left out if the rouines from the library
92 * being used are not cryptographic related :-).
93 * 4. If you include any Windows specific code (or a derivative thereof) from
94 * the apps directory (application code) you must include an acknowledgement:
95 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
96 *
97 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
98 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
100 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
101 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
103 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
104 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
105 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
106 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
107 * SUCH DAMAGE.
109 * The licence and distribution terms for any publically available version or
110 * derivative of this code cannot be changed. i.e. this code cannot simply be
111 * copied and put under another distribution licence
112 * [including the GNU Public Licence.]
113 */
115 /*
116 * SSL operations needed when running in a privilege separated environment.
117 * Adapted from openssl's ssl_rsa.c by Pierre-Yves Ritschard .
118 */
120 /*
121 * Read a bio that contains our certificate in "PEM" format,
122 * possibly followed by a sequence of CA certificates that should be
123 * sent to the peer in the Certificate message.
124 */
125 static int
126 ssl_ctx_use_certificate_chain_bio(SSL_CTX *ctx, BIO *in)
128 int ret = 0;
129 X509 *x = NULL;
131 ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
133 x = PEM_read_bio_X509_AUX(in, NULL, SSL_CTX_get_default_passwd_cb(ctx),
134 SSL_CTX_get_default_passwd_cb_userdata(ctx));
135 if (x == NULL) {
136 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_PEM_LIB);
137 goto end;
140 ret = SSL_CTX_use_certificate(ctx, x);
142 if (ERR_peek_error() != 0)
143 ret = 0;
144 /* Key/certificate mismatch doesn't imply ret==0 ... */
145 if (ret) {
146 /*
147 * If we could set up our certificate, now proceed to
148 * the CA certificates.
149 */
150 X509 *ca;
151 STACK_OF(X509) *chain;
152 int r;
153 unsigned long err;
155 SSL_CTX_get_extra_chain_certs_only(ctx, &chain);
156 if (chain != NULL) {
157 sk_X509_pop_free(chain, X509_free);
158 SSL_CTX_clear_extra_chain_certs(ctx);
161 while ((ca = PEM_read_bio_X509(in, NULL,
162 SSL_CTX_get_default_passwd_cb(ctx),
163 SSL_CTX_get_default_passwd_cb_userdata(ctx))) != NULL) {
164 r = SSL_CTX_add_extra_chain_cert(ctx, ca);
165 if (!r) {
166 X509_free(ca);
167 ret = 0;
168 goto end;
170 /*
171 * Note that we must not free r if it was successfully
172 * added to the chain (while we must free the main
173 * certificate, since its reference count is increased
174 * by SSL_CTX_use_certificate).
175 */
178 /* When the while loop ends, it's usually just EOF. */
179 err = ERR_peek_last_error();
180 if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
181 ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
182 ERR_clear_error();
183 else
184 ret = 0; /* some real error */
187 end:
188 if (x != NULL)
189 X509_free(x);
190 return (ret);
193 int
194 SSL_CTX_use_certificate_chain_mem(SSL_CTX *ctx, void *buf, int len)
196 BIO *in;
197 int ret = 0;
199 in = BIO_new_mem_buf(buf, len);
200 if (in == NULL) {
201 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
202 goto end;
205 ret = ssl_ctx_use_certificate_chain_bio(ctx, in);
207 end:
208 BIO_free(in);
209 return (ret);
212 #endif /* HAVE_SSL_CTX_USE_CERTIFICATE_CHAIN_MEM */