Blame


1 f9ab77a8 2023-08-23 op #include "config.h"
2 f9ab77a8 2023-08-23 op
3 f9ab77a8 2023-08-23 op #include <sys/types.h>
4 f9ab77a8 2023-08-23 op #include <sys/uio.h>
5 f9ab77a8 2023-08-23 op
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>
9 f9ab77a8 2023-08-23 op
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>
18 f9ab77a8 2023-08-23 op
19 f9ab77a8 2023-08-23 op #include "log.h"
20 f9ab77a8 2023-08-23 op
21 f9ab77a8 2023-08-23 op #ifndef HAVE_SSL_CTX_LOAD_VERIFY_MEM
22 f9ab77a8 2023-08-23 op
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)
26 f9ab77a8 2023-08-23 op
27 f9ab77a8 2023-08-23 op #define X509_L_MEM 3
28 f9ab77a8 2023-08-23 op
29 f9ab77a8 2023-08-23 op #define SSL_ECDH_CURVE "prime256v1"
30 f9ab77a8 2023-08-23 op
31 f9ab77a8 2023-08-23 op X509_LOOKUP_METHOD *
32 f9ab77a8 2023-08-23 op X509_LOOKUP_mem(void);
33 f9ab77a8 2023-08-23 op
34 08e6d6ed 2024-01-07 op static int
35 f9ab77a8 2023-08-23 op X509_STORE_load_mem(X509_STORE *ctx, void *buf, int len)
36 f9ab77a8 2023-08-23 op {
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)
41 f9ab77a8 2023-08-23 op return (0);
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)
45 f9ab77a8 2023-08-23 op return (0);
46 f9ab77a8 2023-08-23 op return (1);
47 f9ab77a8 2023-08-23 op }
48 f9ab77a8 2023-08-23 op
49 f9ab77a8 2023-08-23 op int
50 f9ab77a8 2023-08-23 op SSL_CTX_load_verify_mem(SSL_CTX *ctx, void *buf, int len)
51 f9ab77a8 2023-08-23 op {
52 f9ab77a8 2023-08-23 op return (X509_STORE_load_mem(SSL_CTX_get_cert_store(ctx), buf, len));
53 f9ab77a8 2023-08-23 op }
54 f9ab77a8 2023-08-23 op
55 f9ab77a8 2023-08-23 op #endif /* HAVE_SSL_CTX_LOAD_VERIFY_MEM */
56 f9ab77a8 2023-08-23 op
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.
60 f9ab77a8 2023-08-23 op *
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.
64 f9ab77a8 2023-08-23 op *
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).
71 f9ab77a8 2023-08-23 op *
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.
78 f9ab77a8 2023-08-23 op *
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
81 f9ab77a8 2023-08-23 op * are met:
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)"
96 f9ab77a8 2023-08-23 op *
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.
108 f9ab77a8 2023-08-23 op *
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.]
113 f9ab77a8 2023-08-23 op */
114 f9ab77a8 2023-08-23 op
115 f9ab77a8 2023-08-23 op /*
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 .
118 f9ab77a8 2023-08-23 op */
119 f9ab77a8 2023-08-23 op
120 f9ab77a8 2023-08-23 op /*
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.
124 f9ab77a8 2023-08-23 op */
125 f9ab77a8 2023-08-23 op static int
126 f9ab77a8 2023-08-23 op ssl_ctx_use_certificate_chain_bio(SSL_CTX *ctx, BIO *in)
127 f9ab77a8 2023-08-23 op {
128 f9ab77a8 2023-08-23 op int ret = 0;
129 f9ab77a8 2023-08-23 op X509 *x = NULL;
130 f9ab77a8 2023-08-23 op
131 f9ab77a8 2023-08-23 op ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
132 f9ab77a8 2023-08-23 op
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);
137 f9ab77a8 2023-08-23 op goto end;
138 f9ab77a8 2023-08-23 op }
139 f9ab77a8 2023-08-23 op
140 f9ab77a8 2023-08-23 op ret = SSL_CTX_use_certificate(ctx, x);
141 f9ab77a8 2023-08-23 op
142 f9ab77a8 2023-08-23 op if (ERR_peek_error() != 0)
143 f9ab77a8 2023-08-23 op ret = 0;
144 f9ab77a8 2023-08-23 op /* Key/certificate mismatch doesn't imply ret==0 ... */
145 f9ab77a8 2023-08-23 op if (ret) {
146 f9ab77a8 2023-08-23 op /*
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.
149 f9ab77a8 2023-08-23 op */
150 f9ab77a8 2023-08-23 op X509 *ca;
151 f9ab77a8 2023-08-23 op STACK_OF(X509) *chain;
152 f9ab77a8 2023-08-23 op int r;
153 f9ab77a8 2023-08-23 op unsigned long err;
154 f9ab77a8 2023-08-23 op
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);
159 f9ab77a8 2023-08-23 op }
160 f9ab77a8 2023-08-23 op
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);
165 f9ab77a8 2023-08-23 op if (!r) {
166 f9ab77a8 2023-08-23 op X509_free(ca);
167 f9ab77a8 2023-08-23 op ret = 0;
168 f9ab77a8 2023-08-23 op goto end;
169 f9ab77a8 2023-08-23 op }
170 f9ab77a8 2023-08-23 op /*
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).
175 f9ab77a8 2023-08-23 op */
176 f9ab77a8 2023-08-23 op }
177 f9ab77a8 2023-08-23 op
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();
183 f9ab77a8 2023-08-23 op else
184 f9ab77a8 2023-08-23 op ret = 0; /* some real error */
185 f9ab77a8 2023-08-23 op }
186 f9ab77a8 2023-08-23 op
187 f9ab77a8 2023-08-23 op end:
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);
191 f9ab77a8 2023-08-23 op }
192 f9ab77a8 2023-08-23 op
193 f9ab77a8 2023-08-23 op int
194 f9ab77a8 2023-08-23 op SSL_CTX_use_certificate_chain_mem(SSL_CTX *ctx, void *buf, int len)
195 f9ab77a8 2023-08-23 op {
196 f9ab77a8 2023-08-23 op BIO *in;
197 f9ab77a8 2023-08-23 op int ret = 0;
198 f9ab77a8 2023-08-23 op
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);
202 f9ab77a8 2023-08-23 op goto end;
203 f9ab77a8 2023-08-23 op }
204 f9ab77a8 2023-08-23 op
205 f9ab77a8 2023-08-23 op ret = ssl_ctx_use_certificate_chain_bio(ctx, in);
206 f9ab77a8 2023-08-23 op
207 f9ab77a8 2023-08-23 op end:
208 f9ab77a8 2023-08-23 op BIO_free(in);
209 f9ab77a8 2023-08-23 op return (ret);
210 f9ab77a8 2023-08-23 op }
211 f9ab77a8 2023-08-23 op
212 f9ab77a8 2023-08-23 op #endif /* HAVE_SSL_CTX_USE_CERTIFICATE_CHAIN_MEM */