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