1 | /*
|
---|
2 | * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
|
---|
4 | *
|
---|
5 | * Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
6 | * this file except in compliance with the License. You can obtain a copy
|
---|
7 | * in the file LICENSE in the source distribution or at
|
---|
8 | * https://www.openssl.org/source/license.html
|
---|
9 | */
|
---|
10 |
|
---|
11 | #include <stdio.h>
|
---|
12 | #include <sys/types.h>
|
---|
13 |
|
---|
14 | #include "internal/nelem.h"
|
---|
15 | #include <openssl/bio.h>
|
---|
16 | #include "internal/o_dir.h" /* Must be after bio.h due to openssl-mangling.h */
|
---|
17 | #include <openssl/pem.h>
|
---|
18 | #include <openssl/store.h>
|
---|
19 | #include <openssl/x509v3.h>
|
---|
20 | #include <openssl/dh.h>
|
---|
21 | #include <openssl/bn.h>
|
---|
22 | #include <openssl/crypto.h>
|
---|
23 | #include "internal/refcount.h"
|
---|
24 | #include "ssl_local.h"
|
---|
25 | #include "ssl_cert_table.h"
|
---|
26 | #include "internal/thread_once.h"
|
---|
27 | #ifndef OPENSSL_NO_POSIX_IO
|
---|
28 | # include <sys/stat.h>
|
---|
29 | # ifdef _WIN32
|
---|
30 | # define stat _stat
|
---|
31 | # endif
|
---|
32 | # ifndef S_ISDIR
|
---|
33 | # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
|
---|
34 | # endif
|
---|
35 | #endif
|
---|
36 |
|
---|
37 |
|
---|
38 | static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
|
---|
39 | int op, int bits, int nid, void *other,
|
---|
40 | void *ex);
|
---|
41 |
|
---|
42 | static CRYPTO_ONCE ssl_x509_store_ctx_once = CRYPTO_ONCE_STATIC_INIT;
|
---|
43 | static volatile int ssl_x509_store_ctx_idx = -1;
|
---|
44 |
|
---|
45 | DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init)
|
---|
46 | {
|
---|
47 | ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0,
|
---|
48 | "SSL for verify callback",
|
---|
49 | NULL, NULL, NULL);
|
---|
50 | return ssl_x509_store_ctx_idx >= 0;
|
---|
51 | }
|
---|
52 |
|
---|
53 | int SSL_get_ex_data_X509_STORE_CTX_idx(void)
|
---|
54 | {
|
---|
55 |
|
---|
56 | if (!RUN_ONCE(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init))
|
---|
57 | return -1;
|
---|
58 | return ssl_x509_store_ctx_idx;
|
---|
59 | }
|
---|
60 |
|
---|
61 | CERT *ssl_cert_new(size_t ssl_pkey_num)
|
---|
62 | {
|
---|
63 | CERT *ret = NULL;
|
---|
64 |
|
---|
65 | /* Should never happen */
|
---|
66 | if (!ossl_assert(ssl_pkey_num >= SSL_PKEY_NUM))
|
---|
67 | return NULL;
|
---|
68 |
|
---|
69 | ret = OPENSSL_zalloc(sizeof(*ret));
|
---|
70 | if (ret == NULL)
|
---|
71 | return NULL;
|
---|
72 |
|
---|
73 | ret->ssl_pkey_num = ssl_pkey_num;
|
---|
74 | ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY));
|
---|
75 | if (ret->pkeys == NULL) {
|
---|
76 | OPENSSL_free(ret);
|
---|
77 | return NULL;
|
---|
78 | }
|
---|
79 |
|
---|
80 | ret->key = &(ret->pkeys[SSL_PKEY_RSA]);
|
---|
81 | ret->sec_cb = ssl_security_default_callback;
|
---|
82 | ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL;
|
---|
83 | ret->sec_ex = NULL;
|
---|
84 | if (!CRYPTO_NEW_REF(&ret->references, 1)) {
|
---|
85 | OPENSSL_free(ret->pkeys);
|
---|
86 | OPENSSL_free(ret);
|
---|
87 | return NULL;
|
---|
88 | }
|
---|
89 |
|
---|
90 | return ret;
|
---|
91 | }
|
---|
92 |
|
---|
93 | CERT *ssl_cert_dup(CERT *cert)
|
---|
94 | {
|
---|
95 | CERT *ret = OPENSSL_zalloc(sizeof(*ret));
|
---|
96 | size_t i;
|
---|
97 | #ifndef OPENSSL_NO_COMP_ALG
|
---|
98 | int j;
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | if (ret == NULL)
|
---|
102 | return NULL;
|
---|
103 |
|
---|
104 | ret->ssl_pkey_num = cert->ssl_pkey_num;
|
---|
105 | ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY));
|
---|
106 | if (ret->pkeys == NULL) {
|
---|
107 | OPENSSL_free(ret);
|
---|
108 | return NULL;
|
---|
109 | }
|
---|
110 |
|
---|
111 | ret->key = &ret->pkeys[cert->key - cert->pkeys];
|
---|
112 | if (!CRYPTO_NEW_REF(&ret->references, 1)) {
|
---|
113 | OPENSSL_free(ret->pkeys);
|
---|
114 | OPENSSL_free(ret);
|
---|
115 | return NULL;
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (cert->dh_tmp != NULL) {
|
---|
119 | ret->dh_tmp = cert->dh_tmp;
|
---|
120 | EVP_PKEY_up_ref(ret->dh_tmp);
|
---|
121 | }
|
---|
122 |
|
---|
123 | ret->dh_tmp_cb = cert->dh_tmp_cb;
|
---|
124 | ret->dh_tmp_auto = cert->dh_tmp_auto;
|
---|
125 |
|
---|
126 | for (i = 0; i < ret->ssl_pkey_num; i++) {
|
---|
127 | CERT_PKEY *cpk = cert->pkeys + i;
|
---|
128 | CERT_PKEY *rpk = ret->pkeys + i;
|
---|
129 |
|
---|
130 | if (cpk->x509 != NULL) {
|
---|
131 | rpk->x509 = cpk->x509;
|
---|
132 | X509_up_ref(rpk->x509);
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (cpk->privatekey != NULL) {
|
---|
136 | rpk->privatekey = cpk->privatekey;
|
---|
137 | EVP_PKEY_up_ref(cpk->privatekey);
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (cpk->chain) {
|
---|
141 | rpk->chain = X509_chain_up_ref(cpk->chain);
|
---|
142 | if (!rpk->chain) {
|
---|
143 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
---|
144 | goto err;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | if (cpk->serverinfo != NULL) {
|
---|
148 | /* Just copy everything. */
|
---|
149 | rpk->serverinfo = OPENSSL_memdup(cpk->serverinfo, cpk->serverinfo_length);
|
---|
150 | if (rpk->serverinfo == NULL)
|
---|
151 | goto err;
|
---|
152 | rpk->serverinfo_length = cpk->serverinfo_length;
|
---|
153 | }
|
---|
154 | #ifndef OPENSSL_NO_COMP_ALG
|
---|
155 | for (j = TLSEXT_comp_cert_none; j < TLSEXT_comp_cert_limit; j++) {
|
---|
156 | if (cpk->comp_cert[j] != NULL) {
|
---|
157 | if (!OSSL_COMP_CERT_up_ref(cpk->comp_cert[j]))
|
---|
158 | goto err;
|
---|
159 | rpk->comp_cert[j] = cpk->comp_cert[j];
|
---|
160 | }
|
---|
161 | }
|
---|
162 | #endif
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* Configured sigalgs copied across */
|
---|
166 | if (cert->conf_sigalgs) {
|
---|
167 | ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen
|
---|
168 | * sizeof(*cert->conf_sigalgs));
|
---|
169 | if (ret->conf_sigalgs == NULL)
|
---|
170 | goto err;
|
---|
171 | memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
|
---|
172 | cert->conf_sigalgslen * sizeof(*cert->conf_sigalgs));
|
---|
173 | ret->conf_sigalgslen = cert->conf_sigalgslen;
|
---|
174 | } else
|
---|
175 | ret->conf_sigalgs = NULL;
|
---|
176 |
|
---|
177 | if (cert->client_sigalgs) {
|
---|
178 | ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen
|
---|
179 | * sizeof(*cert->client_sigalgs));
|
---|
180 | if (ret->client_sigalgs == NULL)
|
---|
181 | goto err;
|
---|
182 | memcpy(ret->client_sigalgs, cert->client_sigalgs,
|
---|
183 | cert->client_sigalgslen * sizeof(*cert->client_sigalgs));
|
---|
184 | ret->client_sigalgslen = cert->client_sigalgslen;
|
---|
185 | } else
|
---|
186 | ret->client_sigalgs = NULL;
|
---|
187 | /* Copy any custom client certificate types */
|
---|
188 | if (cert->ctype) {
|
---|
189 | ret->ctype = OPENSSL_memdup(cert->ctype, cert->ctype_len);
|
---|
190 | if (ret->ctype == NULL)
|
---|
191 | goto err;
|
---|
192 | ret->ctype_len = cert->ctype_len;
|
---|
193 | }
|
---|
194 |
|
---|
195 | ret->cert_flags = cert->cert_flags;
|
---|
196 |
|
---|
197 | ret->cert_cb = cert->cert_cb;
|
---|
198 | ret->cert_cb_arg = cert->cert_cb_arg;
|
---|
199 |
|
---|
200 | if (cert->verify_store) {
|
---|
201 | X509_STORE_up_ref(cert->verify_store);
|
---|
202 | ret->verify_store = cert->verify_store;
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (cert->chain_store) {
|
---|
206 | X509_STORE_up_ref(cert->chain_store);
|
---|
207 | ret->chain_store = cert->chain_store;
|
---|
208 | }
|
---|
209 |
|
---|
210 | ret->sec_cb = cert->sec_cb;
|
---|
211 | ret->sec_level = cert->sec_level;
|
---|
212 | ret->sec_ex = cert->sec_ex;
|
---|
213 |
|
---|
214 | if (!custom_exts_copy(&ret->custext, &cert->custext))
|
---|
215 | goto err;
|
---|
216 | #ifndef OPENSSL_NO_PSK
|
---|
217 | if (cert->psk_identity_hint) {
|
---|
218 | ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint);
|
---|
219 | if (ret->psk_identity_hint == NULL)
|
---|
220 | goto err;
|
---|
221 | }
|
---|
222 | #endif
|
---|
223 | return ret;
|
---|
224 |
|
---|
225 | err:
|
---|
226 | ssl_cert_free(ret);
|
---|
227 |
|
---|
228 | return NULL;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /* Free up and clear all certificates and chains */
|
---|
232 |
|
---|
233 | void ssl_cert_clear_certs(CERT *c)
|
---|
234 | {
|
---|
235 | size_t i;
|
---|
236 | #ifndef OPENSSL_NO_COMP_ALG
|
---|
237 | int j;
|
---|
238 | #endif
|
---|
239 |
|
---|
240 | if (c == NULL)
|
---|
241 | return;
|
---|
242 | for (i = 0; i < c->ssl_pkey_num; i++) {
|
---|
243 | CERT_PKEY *cpk = c->pkeys + i;
|
---|
244 | X509_free(cpk->x509);
|
---|
245 | cpk->x509 = NULL;
|
---|
246 | EVP_PKEY_free(cpk->privatekey);
|
---|
247 | cpk->privatekey = NULL;
|
---|
248 | OSSL_STACK_OF_X509_free(cpk->chain);
|
---|
249 | cpk->chain = NULL;
|
---|
250 | OPENSSL_free(cpk->serverinfo);
|
---|
251 | cpk->serverinfo = NULL;
|
---|
252 | cpk->serverinfo_length = 0;
|
---|
253 | #ifndef OPENSSL_NO_COMP_ALG
|
---|
254 | for (j = 0; j < TLSEXT_comp_cert_limit; j++) {
|
---|
255 | OSSL_COMP_CERT_free(cpk->comp_cert[j]);
|
---|
256 | cpk->comp_cert[j] = NULL;
|
---|
257 | cpk->cert_comp_used = 0;
|
---|
258 | }
|
---|
259 | #endif
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | void ssl_cert_free(CERT *c)
|
---|
264 | {
|
---|
265 | int i;
|
---|
266 |
|
---|
267 | if (c == NULL)
|
---|
268 | return;
|
---|
269 | CRYPTO_DOWN_REF(&c->references, &i);
|
---|
270 | REF_PRINT_COUNT("CERT", c);
|
---|
271 | if (i > 0)
|
---|
272 | return;
|
---|
273 | REF_ASSERT_ISNT(i < 0);
|
---|
274 |
|
---|
275 | EVP_PKEY_free(c->dh_tmp);
|
---|
276 |
|
---|
277 | ssl_cert_clear_certs(c);
|
---|
278 | OPENSSL_free(c->conf_sigalgs);
|
---|
279 | OPENSSL_free(c->client_sigalgs);
|
---|
280 | OPENSSL_free(c->ctype);
|
---|
281 | X509_STORE_free(c->verify_store);
|
---|
282 | X509_STORE_free(c->chain_store);
|
---|
283 | custom_exts_free(&c->custext);
|
---|
284 | #ifndef OPENSSL_NO_PSK
|
---|
285 | OPENSSL_free(c->psk_identity_hint);
|
---|
286 | #endif
|
---|
287 | OPENSSL_free(c->pkeys);
|
---|
288 | CRYPTO_FREE_REF(&c->references);
|
---|
289 | OPENSSL_free(c);
|
---|
290 | }
|
---|
291 |
|
---|
292 | int ssl_cert_set0_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
|
---|
293 | {
|
---|
294 | int i, r;
|
---|
295 | CERT_PKEY *cpk = s != NULL ? s->cert->key : ctx->cert->key;
|
---|
296 |
|
---|
297 | if (!cpk)
|
---|
298 | return 0;
|
---|
299 | for (i = 0; i < sk_X509_num(chain); i++) {
|
---|
300 | X509 *x = sk_X509_value(chain, i);
|
---|
301 |
|
---|
302 | r = ssl_security_cert(s, ctx, x, 0, 0);
|
---|
303 | if (r != 1) {
|
---|
304 | ERR_raise(ERR_LIB_SSL, r);
|
---|
305 | return 0;
|
---|
306 | }
|
---|
307 | }
|
---|
308 | OSSL_STACK_OF_X509_free(cpk->chain);
|
---|
309 | cpk->chain = chain;
|
---|
310 | return 1;
|
---|
311 | }
|
---|
312 |
|
---|
313 | int ssl_cert_set1_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
|
---|
314 | {
|
---|
315 | STACK_OF(X509) *dchain;
|
---|
316 |
|
---|
317 | if (!chain)
|
---|
318 | return ssl_cert_set0_chain(s, ctx, NULL);
|
---|
319 | dchain = X509_chain_up_ref(chain);
|
---|
320 | if (!dchain)
|
---|
321 | return 0;
|
---|
322 | if (!ssl_cert_set0_chain(s, ctx, dchain)) {
|
---|
323 | OSSL_STACK_OF_X509_free(dchain);
|
---|
324 | return 0;
|
---|
325 | }
|
---|
326 | return 1;
|
---|
327 | }
|
---|
328 |
|
---|
329 | int ssl_cert_add0_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x)
|
---|
330 | {
|
---|
331 | int r;
|
---|
332 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
|
---|
333 |
|
---|
334 | if (!cpk)
|
---|
335 | return 0;
|
---|
336 | r = ssl_security_cert(s, ctx, x, 0, 0);
|
---|
337 | if (r != 1) {
|
---|
338 | ERR_raise(ERR_LIB_SSL, r);
|
---|
339 | return 0;
|
---|
340 | }
|
---|
341 | if (!cpk->chain)
|
---|
342 | cpk->chain = sk_X509_new_null();
|
---|
343 | if (!cpk->chain || !sk_X509_push(cpk->chain, x))
|
---|
344 | return 0;
|
---|
345 | return 1;
|
---|
346 | }
|
---|
347 |
|
---|
348 | int ssl_cert_add1_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x)
|
---|
349 | {
|
---|
350 | if (!ssl_cert_add0_chain_cert(s, ctx, x))
|
---|
351 | return 0;
|
---|
352 | X509_up_ref(x);
|
---|
353 | return 1;
|
---|
354 | }
|
---|
355 |
|
---|
356 | int ssl_cert_select_current(CERT *c, X509 *x)
|
---|
357 | {
|
---|
358 | size_t i;
|
---|
359 |
|
---|
360 | if (x == NULL)
|
---|
361 | return 0;
|
---|
362 | for (i = 0; i < c->ssl_pkey_num; i++) {
|
---|
363 | CERT_PKEY *cpk = c->pkeys + i;
|
---|
364 | if (cpk->x509 == x && cpk->privatekey) {
|
---|
365 | c->key = cpk;
|
---|
366 | return 1;
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | for (i = 0; i < c->ssl_pkey_num; i++) {
|
---|
371 | CERT_PKEY *cpk = c->pkeys + i;
|
---|
372 | if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
|
---|
373 | c->key = cpk;
|
---|
374 | return 1;
|
---|
375 | }
|
---|
376 | }
|
---|
377 | return 0;
|
---|
378 | }
|
---|
379 |
|
---|
380 | int ssl_cert_set_current(CERT *c, long op)
|
---|
381 | {
|
---|
382 | size_t i, idx;
|
---|
383 |
|
---|
384 | if (!c)
|
---|
385 | return 0;
|
---|
386 | if (op == SSL_CERT_SET_FIRST)
|
---|
387 | idx = 0;
|
---|
388 | else if (op == SSL_CERT_SET_NEXT) {
|
---|
389 | idx = (size_t)(c->key - c->pkeys + 1);
|
---|
390 | if (idx >= c->ssl_pkey_num)
|
---|
391 | return 0;
|
---|
392 | } else
|
---|
393 | return 0;
|
---|
394 | for (i = idx; i < c->ssl_pkey_num; i++) {
|
---|
395 | CERT_PKEY *cpk = c->pkeys + i;
|
---|
396 | if (cpk->x509 && cpk->privatekey) {
|
---|
397 | c->key = cpk;
|
---|
398 | return 1;
|
---|
399 | }
|
---|
400 | }
|
---|
401 | return 0;
|
---|
402 | }
|
---|
403 |
|
---|
404 | void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
|
---|
405 | {
|
---|
406 | c->cert_cb = cb;
|
---|
407 | c->cert_cb_arg = arg;
|
---|
408 | }
|
---|
409 |
|
---|
410 | /*
|
---|
411 | * Verify a certificate chain/raw public key
|
---|
412 | * Return codes:
|
---|
413 | * 1: Verify success
|
---|
414 | * 0: Verify failure or error
|
---|
415 | * -1: Retry required
|
---|
416 | */
|
---|
417 | static int ssl_verify_internal(SSL_CONNECTION *s, STACK_OF(X509) *sk, EVP_PKEY *rpk)
|
---|
418 | {
|
---|
419 | X509 *x;
|
---|
420 | int i = 0;
|
---|
421 | X509_STORE *verify_store;
|
---|
422 | X509_STORE_CTX *ctx = NULL;
|
---|
423 | X509_VERIFY_PARAM *param;
|
---|
424 | SSL_CTX *sctx;
|
---|
425 |
|
---|
426 | /* Something must be passed in */
|
---|
427 | if ((sk == NULL || sk_X509_num(sk) == 0) && rpk == NULL)
|
---|
428 | return 0;
|
---|
429 |
|
---|
430 | /* Only one can be set */
|
---|
431 | if (sk != NULL && rpk != NULL)
|
---|
432 | return 0;
|
---|
433 |
|
---|
434 | sctx = SSL_CONNECTION_GET_CTX(s);
|
---|
435 | if (s->cert->verify_store)
|
---|
436 | verify_store = s->cert->verify_store;
|
---|
437 | else
|
---|
438 | verify_store = sctx->cert_store;
|
---|
439 |
|
---|
440 | ctx = X509_STORE_CTX_new_ex(sctx->libctx, sctx->propq);
|
---|
441 | if (ctx == NULL) {
|
---|
442 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
---|
443 | return 0;
|
---|
444 | }
|
---|
445 |
|
---|
446 | if (sk != NULL) {
|
---|
447 | x = sk_X509_value(sk, 0);
|
---|
448 | if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) {
|
---|
449 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
---|
450 | goto end;
|
---|
451 | }
|
---|
452 | } else {
|
---|
453 | if (!X509_STORE_CTX_init_rpk(ctx, verify_store, rpk)) {
|
---|
454 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
---|
455 | goto end;
|
---|
456 | }
|
---|
457 | }
|
---|
458 | param = X509_STORE_CTX_get0_param(ctx);
|
---|
459 | /*
|
---|
460 | * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
|
---|
461 | * point, for now a single @SECLEVEL sets the same policy for TLS crypto
|
---|
462 | * and PKI authentication.
|
---|
463 | */
|
---|
464 | X509_VERIFY_PARAM_set_auth_level(param,
|
---|
465 | SSL_get_security_level(SSL_CONNECTION_GET_SSL(s)));
|
---|
466 |
|
---|
467 | /* Set suite B flags if needed */
|
---|
468 | X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));
|
---|
469 | if (!X509_STORE_CTX_set_ex_data(ctx,
|
---|
470 | SSL_get_ex_data_X509_STORE_CTX_idx(), s)) {
|
---|
471 | goto end;
|
---|
472 | }
|
---|
473 |
|
---|
474 | /* Verify via DANE if enabled */
|
---|
475 | if (DANETLS_ENABLED(&s->dane))
|
---|
476 | X509_STORE_CTX_set0_dane(ctx, &s->dane);
|
---|
477 |
|
---|
478 | /*
|
---|
479 | * We need to inherit the verify parameters. These can be determined by
|
---|
480 | * the context: if its a server it will verify SSL client certificates or
|
---|
481 | * vice versa.
|
---|
482 | */
|
---|
483 |
|
---|
484 | X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server");
|
---|
485 | /*
|
---|
486 | * Anything non-default in "s->param" should overwrite anything in the ctx.
|
---|
487 | */
|
---|
488 | X509_VERIFY_PARAM_set1(param, s->param);
|
---|
489 |
|
---|
490 | if (s->verify_callback)
|
---|
491 | X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);
|
---|
492 |
|
---|
493 | if (sctx->app_verify_callback != NULL) {
|
---|
494 | i = sctx->app_verify_callback(ctx, sctx->app_verify_arg);
|
---|
495 | } else {
|
---|
496 | i = X509_verify_cert(ctx);
|
---|
497 | /* We treat an error in the same way as a failure to verify */
|
---|
498 | if (i < 0)
|
---|
499 | i = 0;
|
---|
500 | }
|
---|
501 |
|
---|
502 | s->verify_result = X509_STORE_CTX_get_error(ctx);
|
---|
503 | OSSL_STACK_OF_X509_free(s->verified_chain);
|
---|
504 | s->verified_chain = NULL;
|
---|
505 |
|
---|
506 | if (sk != NULL && X509_STORE_CTX_get0_chain(ctx) != NULL) {
|
---|
507 | s->verified_chain = X509_STORE_CTX_get1_chain(ctx);
|
---|
508 | if (s->verified_chain == NULL) {
|
---|
509 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
---|
510 | i = 0;
|
---|
511 | }
|
---|
512 | }
|
---|
513 |
|
---|
514 | /* Move peername from the store context params to the SSL handle's */
|
---|
515 | X509_VERIFY_PARAM_move_peername(s->param, param);
|
---|
516 |
|
---|
517 | end:
|
---|
518 | X509_STORE_CTX_free(ctx);
|
---|
519 | return i;
|
---|
520 | }
|
---|
521 |
|
---|
522 | /*
|
---|
523 | * Verify a raw public key
|
---|
524 | * Return codes:
|
---|
525 | * 1: Verify success
|
---|
526 | * 0: Verify failure or error
|
---|
527 | * -1: Retry required
|
---|
528 | */
|
---|
529 | int ssl_verify_rpk(SSL_CONNECTION *s, EVP_PKEY *rpk)
|
---|
530 | {
|
---|
531 | return ssl_verify_internal(s, NULL, rpk);
|
---|
532 | }
|
---|
533 |
|
---|
534 | /*
|
---|
535 | * Verify a certificate chain
|
---|
536 | * Return codes:
|
---|
537 | * 1: Verify success
|
---|
538 | * 0: Verify failure or error
|
---|
539 | * -1: Retry required
|
---|
540 | */
|
---|
541 | int ssl_verify_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk)
|
---|
542 | {
|
---|
543 | return ssl_verify_internal(s, sk, NULL);
|
---|
544 | }
|
---|
545 |
|
---|
546 | static void set0_CA_list(STACK_OF(X509_NAME) **ca_list,
|
---|
547 | STACK_OF(X509_NAME) *name_list)
|
---|
548 | {
|
---|
549 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
|
---|
550 | *ca_list = name_list;
|
---|
551 | }
|
---|
552 |
|
---|
553 | STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk)
|
---|
554 | {
|
---|
555 | int i;
|
---|
556 | const int num = sk_X509_NAME_num(sk);
|
---|
557 | STACK_OF(X509_NAME) *ret;
|
---|
558 | X509_NAME *name;
|
---|
559 |
|
---|
560 | ret = sk_X509_NAME_new_reserve(NULL, num);
|
---|
561 | if (ret == NULL) {
|
---|
562 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
|
---|
563 | return NULL;
|
---|
564 | }
|
---|
565 | for (i = 0; i < num; i++) {
|
---|
566 | name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
|
---|
567 | if (name == NULL) {
|
---|
568 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
---|
569 | sk_X509_NAME_pop_free(ret, X509_NAME_free);
|
---|
570 | return NULL;
|
---|
571 | }
|
---|
572 | sk_X509_NAME_push(ret, name); /* Cannot fail after reserve call */
|
---|
573 | }
|
---|
574 | return ret;
|
---|
575 | }
|
---|
576 |
|
---|
577 | void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
|
---|
578 | {
|
---|
579 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
|
---|
580 |
|
---|
581 | if (sc == NULL)
|
---|
582 | return;
|
---|
583 |
|
---|
584 | set0_CA_list(&sc->ca_names, name_list);
|
---|
585 | }
|
---|
586 |
|
---|
587 | void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
|
---|
588 | {
|
---|
589 | set0_CA_list(&ctx->ca_names, name_list);
|
---|
590 | }
|
---|
591 |
|
---|
592 | const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx)
|
---|
593 | {
|
---|
594 | return ctx->ca_names;
|
---|
595 | }
|
---|
596 |
|
---|
597 | const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s)
|
---|
598 | {
|
---|
599 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
|
---|
600 |
|
---|
601 | if (sc == NULL)
|
---|
602 | return NULL;
|
---|
603 |
|
---|
604 | return sc->ca_names != NULL ? sc->ca_names : s->ctx->ca_names;
|
---|
605 | }
|
---|
606 |
|
---|
607 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
|
---|
608 | {
|
---|
609 | set0_CA_list(&ctx->client_ca_names, name_list);
|
---|
610 | }
|
---|
611 |
|
---|
612 | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
|
---|
613 | {
|
---|
614 | return ctx->client_ca_names;
|
---|
615 | }
|
---|
616 |
|
---|
617 | void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
|
---|
618 | {
|
---|
619 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
|
---|
620 |
|
---|
621 | if (sc == NULL)
|
---|
622 | return;
|
---|
623 |
|
---|
624 | set0_CA_list(&sc->client_ca_names, name_list);
|
---|
625 | }
|
---|
626 |
|
---|
627 | const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s)
|
---|
628 | {
|
---|
629 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
|
---|
630 |
|
---|
631 | if (sc == NULL)
|
---|
632 | return NULL;
|
---|
633 |
|
---|
634 | return sc->s3.tmp.peer_ca_names;
|
---|
635 | }
|
---|
636 |
|
---|
637 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
|
---|
638 | {
|
---|
639 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
|
---|
640 |
|
---|
641 | if (sc == NULL)
|
---|
642 | return NULL;
|
---|
643 |
|
---|
644 | if (!sc->server)
|
---|
645 | return sc->s3.tmp.peer_ca_names;
|
---|
646 | return sc->client_ca_names != NULL ? sc->client_ca_names
|
---|
647 | : s->ctx->client_ca_names;
|
---|
648 | }
|
---|
649 |
|
---|
650 | static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x)
|
---|
651 | {
|
---|
652 | X509_NAME *name;
|
---|
653 |
|
---|
654 | if (x == NULL)
|
---|
655 | return 0;
|
---|
656 | if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL))
|
---|
657 | return 0;
|
---|
658 |
|
---|
659 | if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
|
---|
660 | return 0;
|
---|
661 |
|
---|
662 | if (!sk_X509_NAME_push(*sk, name)) {
|
---|
663 | X509_NAME_free(name);
|
---|
664 | return 0;
|
---|
665 | }
|
---|
666 | return 1;
|
---|
667 | }
|
---|
668 |
|
---|
669 | int SSL_add1_to_CA_list(SSL *ssl, const X509 *x)
|
---|
670 | {
|
---|
671 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
|
---|
672 |
|
---|
673 | if (sc == NULL)
|
---|
674 | return 0;
|
---|
675 |
|
---|
676 | return add_ca_name(&sc->ca_names, x);
|
---|
677 | }
|
---|
678 |
|
---|
679 | int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x)
|
---|
680 | {
|
---|
681 | return add_ca_name(&ctx->ca_names, x);
|
---|
682 | }
|
---|
683 |
|
---|
684 | /*
|
---|
685 | * The following two are older names are to be replaced with
|
---|
686 | * SSL(_CTX)_add1_to_CA_list
|
---|
687 | */
|
---|
688 | int SSL_add_client_CA(SSL *ssl, X509 *x)
|
---|
689 | {
|
---|
690 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
|
---|
691 |
|
---|
692 | if (sc == NULL)
|
---|
693 | return 0;
|
---|
694 |
|
---|
695 | return add_ca_name(&sc->client_ca_names, x);
|
---|
696 | }
|
---|
697 |
|
---|
698 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
|
---|
699 | {
|
---|
700 | return add_ca_name(&ctx->client_ca_names, x);
|
---|
701 | }
|
---|
702 |
|
---|
703 | static int xname_cmp(const X509_NAME *a, const X509_NAME *b)
|
---|
704 | {
|
---|
705 | unsigned char *abuf = NULL, *bbuf = NULL;
|
---|
706 | int alen, blen, ret;
|
---|
707 |
|
---|
708 | /* X509_NAME_cmp() itself casts away constness in this way, so
|
---|
709 | * assume it's safe:
|
---|
710 | */
|
---|
711 | alen = i2d_X509_NAME((X509_NAME *)a, &abuf);
|
---|
712 | blen = i2d_X509_NAME((X509_NAME *)b, &bbuf);
|
---|
713 |
|
---|
714 | if (alen < 0 || blen < 0)
|
---|
715 | ret = -2;
|
---|
716 | else if (alen != blen)
|
---|
717 | ret = alen - blen;
|
---|
718 | else /* alen == blen */
|
---|
719 | ret = memcmp(abuf, bbuf, alen);
|
---|
720 |
|
---|
721 | OPENSSL_free(abuf);
|
---|
722 | OPENSSL_free(bbuf);
|
---|
723 |
|
---|
724 | return ret;
|
---|
725 | }
|
---|
726 |
|
---|
727 | static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
|
---|
728 | {
|
---|
729 | return xname_cmp(*a, *b);
|
---|
730 | }
|
---|
731 |
|
---|
732 | static unsigned long xname_hash(const X509_NAME *a)
|
---|
733 | {
|
---|
734 | /* This returns 0 also if SHA1 is not available */
|
---|
735 | return X509_NAME_hash_ex((X509_NAME *)a, NULL, NULL, NULL);
|
---|
736 | }
|
---|
737 |
|
---|
738 | STACK_OF(X509_NAME) *SSL_load_client_CA_file_ex(const char *file,
|
---|
739 | OSSL_LIB_CTX *libctx,
|
---|
740 | const char *propq)
|
---|
741 | {
|
---|
742 | BIO *in = BIO_new(BIO_s_file());
|
---|
743 | X509 *x = NULL;
|
---|
744 | X509_NAME *xn = NULL;
|
---|
745 | STACK_OF(X509_NAME) *ret = NULL;
|
---|
746 | LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
|
---|
747 | OSSL_LIB_CTX *prev_libctx = NULL;
|
---|
748 |
|
---|
749 | if (name_hash == NULL) {
|
---|
750 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
|
---|
751 | goto err;
|
---|
752 | }
|
---|
753 | if (in == NULL) {
|
---|
754 | ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB);
|
---|
755 | goto err;
|
---|
756 | }
|
---|
757 |
|
---|
758 | x = X509_new_ex(libctx, propq);
|
---|
759 | if (x == NULL) {
|
---|
760 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
---|
761 | goto err;
|
---|
762 | }
|
---|
763 | if (BIO_read_filename(in, file) <= 0)
|
---|
764 | goto err;
|
---|
765 |
|
---|
766 | /* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */
|
---|
767 | prev_libctx = OSSL_LIB_CTX_set0_default(libctx);
|
---|
768 | for (;;) {
|
---|
769 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
|
---|
770 | break;
|
---|
771 | if (ret == NULL) {
|
---|
772 | ret = sk_X509_NAME_new_null();
|
---|
773 | if (ret == NULL) {
|
---|
774 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
|
---|
775 | goto err;
|
---|
776 | }
|
---|
777 | }
|
---|
778 | if ((xn = X509_get_subject_name(x)) == NULL)
|
---|
779 | goto err;
|
---|
780 | /* check for duplicates */
|
---|
781 | xn = X509_NAME_dup(xn);
|
---|
782 | if (xn == NULL)
|
---|
783 | goto err;
|
---|
784 | if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
|
---|
785 | /* Duplicate. */
|
---|
786 | X509_NAME_free(xn);
|
---|
787 | xn = NULL;
|
---|
788 | } else {
|
---|
789 | lh_X509_NAME_insert(name_hash, xn);
|
---|
790 | if (!sk_X509_NAME_push(ret, xn))
|
---|
791 | goto err;
|
---|
792 | }
|
---|
793 | }
|
---|
794 | goto done;
|
---|
795 |
|
---|
796 | err:
|
---|
797 | X509_NAME_free(xn);
|
---|
798 | sk_X509_NAME_pop_free(ret, X509_NAME_free);
|
---|
799 | ret = NULL;
|
---|
800 | done:
|
---|
801 | /* restore the old libctx */
|
---|
802 | OSSL_LIB_CTX_set0_default(prev_libctx);
|
---|
803 | BIO_free(in);
|
---|
804 | X509_free(x);
|
---|
805 | lh_X509_NAME_free(name_hash);
|
---|
806 | if (ret != NULL)
|
---|
807 | ERR_clear_error();
|
---|
808 | return ret;
|
---|
809 | }
|
---|
810 |
|
---|
811 | STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
|
---|
812 | {
|
---|
813 | return SSL_load_client_CA_file_ex(file, NULL, NULL);
|
---|
814 | }
|
---|
815 |
|
---|
816 | int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
|
---|
817 | const char *file)
|
---|
818 | {
|
---|
819 | BIO *in;
|
---|
820 | X509 *x = NULL;
|
---|
821 | X509_NAME *xn = NULL;
|
---|
822 | int ret = 1;
|
---|
823 | int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
|
---|
824 |
|
---|
825 | oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
|
---|
826 |
|
---|
827 | in = BIO_new(BIO_s_file());
|
---|
828 |
|
---|
829 | if (in == NULL) {
|
---|
830 | ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB);
|
---|
831 | goto err;
|
---|
832 | }
|
---|
833 |
|
---|
834 | if (BIO_read_filename(in, file) <= 0)
|
---|
835 | goto err;
|
---|
836 |
|
---|
837 | for (;;) {
|
---|
838 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
|
---|
839 | break;
|
---|
840 | if ((xn = X509_get_subject_name(x)) == NULL)
|
---|
841 | goto err;
|
---|
842 | xn = X509_NAME_dup(xn);
|
---|
843 | if (xn == NULL)
|
---|
844 | goto err;
|
---|
845 | if (sk_X509_NAME_find(stack, xn) >= 0) {
|
---|
846 | /* Duplicate. */
|
---|
847 | X509_NAME_free(xn);
|
---|
848 | } else if (!sk_X509_NAME_push(stack, xn)) {
|
---|
849 | X509_NAME_free(xn);
|
---|
850 | goto err;
|
---|
851 | }
|
---|
852 | }
|
---|
853 |
|
---|
854 | ERR_clear_error();
|
---|
855 | goto done;
|
---|
856 |
|
---|
857 | err:
|
---|
858 | ret = 0;
|
---|
859 | done:
|
---|
860 | BIO_free(in);
|
---|
861 | X509_free(x);
|
---|
862 | (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
|
---|
863 | return ret;
|
---|
864 | }
|
---|
865 |
|
---|
866 | int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
|
---|
867 | const char *dir)
|
---|
868 | {
|
---|
869 | OPENSSL_DIR_CTX *d = NULL;
|
---|
870 | const char *filename;
|
---|
871 | int ret = 0;
|
---|
872 |
|
---|
873 | /* Note that a side effect is that the CAs will be sorted by name */
|
---|
874 |
|
---|
875 | while ((filename = OPENSSL_DIR_read(&d, dir))) {
|
---|
876 | char buf[1024];
|
---|
877 | int r;
|
---|
878 | #ifndef OPENSSL_NO_POSIX_IO
|
---|
879 | struct stat st;
|
---|
880 |
|
---|
881 | #else
|
---|
882 | /* Cannot use stat so just skip current and parent directories */
|
---|
883 | if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
|
---|
884 | continue;
|
---|
885 | #endif
|
---|
886 | if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
|
---|
887 | ERR_raise(ERR_LIB_SSL, SSL_R_PATH_TOO_LONG);
|
---|
888 | goto err;
|
---|
889 | }
|
---|
890 | #ifdef OPENSSL_SYS_VMS
|
---|
891 | r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
|
---|
892 | #else
|
---|
893 | r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
|
---|
894 | #endif
|
---|
895 | #ifndef OPENSSL_NO_POSIX_IO
|
---|
896 | /* Skip subdirectories */
|
---|
897 | if (!stat(buf, &st) && S_ISDIR(st.st_mode))
|
---|
898 | continue;
|
---|
899 | #endif
|
---|
900 | if (r <= 0 || r >= (int)sizeof(buf))
|
---|
901 | goto err;
|
---|
902 | if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
|
---|
903 | goto err;
|
---|
904 | }
|
---|
905 |
|
---|
906 | if (errno) {
|
---|
907 | ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
|
---|
908 | "calling OPENSSL_dir_read(%s)", dir);
|
---|
909 | ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
|
---|
910 | goto err;
|
---|
911 | }
|
---|
912 |
|
---|
913 | ret = 1;
|
---|
914 |
|
---|
915 | err:
|
---|
916 | if (d)
|
---|
917 | OPENSSL_DIR_end(&d);
|
---|
918 |
|
---|
919 | return ret;
|
---|
920 | }
|
---|
921 |
|
---|
922 | static int add_uris_recursive(STACK_OF(X509_NAME) *stack,
|
---|
923 | const char *uri, int depth)
|
---|
924 | {
|
---|
925 | int ok = 1;
|
---|
926 | OSSL_STORE_CTX *ctx = NULL;
|
---|
927 | X509 *x = NULL;
|
---|
928 | X509_NAME *xn = NULL;
|
---|
929 |
|
---|
930 | if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)
|
---|
931 | goto err;
|
---|
932 |
|
---|
933 | while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) {
|
---|
934 | OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
|
---|
935 | int infotype = info == 0 ? 0 : OSSL_STORE_INFO_get_type(info);
|
---|
936 |
|
---|
937 | if (info == NULL)
|
---|
938 | continue;
|
---|
939 |
|
---|
940 | if (infotype == OSSL_STORE_INFO_NAME) {
|
---|
941 | /*
|
---|
942 | * This is an entry in the "directory" represented by the current
|
---|
943 | * uri. if |depth| allows, dive into it.
|
---|
944 | */
|
---|
945 | if (depth > 0)
|
---|
946 | ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info),
|
---|
947 | depth - 1);
|
---|
948 | } else if (infotype == OSSL_STORE_INFO_CERT) {
|
---|
949 | if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL
|
---|
950 | || (xn = X509_get_subject_name(x)) == NULL
|
---|
951 | || (xn = X509_NAME_dup(xn)) == NULL)
|
---|
952 | goto err;
|
---|
953 | if (sk_X509_NAME_find(stack, xn) >= 0) {
|
---|
954 | /* Duplicate. */
|
---|
955 | X509_NAME_free(xn);
|
---|
956 | } else if (!sk_X509_NAME_push(stack, xn)) {
|
---|
957 | X509_NAME_free(xn);
|
---|
958 | goto err;
|
---|
959 | }
|
---|
960 | }
|
---|
961 |
|
---|
962 | OSSL_STORE_INFO_free(info);
|
---|
963 | }
|
---|
964 |
|
---|
965 | ERR_clear_error();
|
---|
966 | goto done;
|
---|
967 |
|
---|
968 | err:
|
---|
969 | ok = 0;
|
---|
970 | done:
|
---|
971 | OSSL_STORE_close(ctx);
|
---|
972 |
|
---|
973 | return ok;
|
---|
974 | }
|
---|
975 |
|
---|
976 | int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
|
---|
977 | const char *store)
|
---|
978 | {
|
---|
979 | int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b)
|
---|
980 | = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
|
---|
981 | int ret = add_uris_recursive(stack, store, 1);
|
---|
982 |
|
---|
983 | (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
|
---|
984 | return ret;
|
---|
985 | }
|
---|
986 |
|
---|
987 | /* Build a certificate chain for current certificate */
|
---|
988 | int ssl_build_cert_chain(SSL_CONNECTION *s, SSL_CTX *ctx, int flags)
|
---|
989 | {
|
---|
990 | CERT *c = s != NULL ? s->cert : ctx->cert;
|
---|
991 | CERT_PKEY *cpk = c->key;
|
---|
992 | X509_STORE *chain_store = NULL;
|
---|
993 | X509_STORE_CTX *xs_ctx = NULL;
|
---|
994 | STACK_OF(X509) *chain = NULL, *untrusted = NULL;
|
---|
995 | X509 *x;
|
---|
996 | SSL_CTX *real_ctx = (s == NULL) ? ctx : SSL_CONNECTION_GET_CTX(s);
|
---|
997 | int i, rv = 0;
|
---|
998 |
|
---|
999 | if (cpk->x509 == NULL) {
|
---|
1000 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET);
|
---|
1001 | goto err;
|
---|
1002 | }
|
---|
1003 | /* Rearranging and check the chain: add everything to a store */
|
---|
1004 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
|
---|
1005 | chain_store = X509_STORE_new();
|
---|
1006 | if (chain_store == NULL)
|
---|
1007 | goto err;
|
---|
1008 | for (i = 0; i < sk_X509_num(cpk->chain); i++) {
|
---|
1009 | x = sk_X509_value(cpk->chain, i);
|
---|
1010 | if (!X509_STORE_add_cert(chain_store, x))
|
---|
1011 | goto err;
|
---|
1012 | }
|
---|
1013 | /* Add EE cert too: it might be self signed */
|
---|
1014 | if (!X509_STORE_add_cert(chain_store, cpk->x509))
|
---|
1015 | goto err;
|
---|
1016 | } else {
|
---|
1017 | if (c->chain_store != NULL)
|
---|
1018 | chain_store = c->chain_store;
|
---|
1019 | else
|
---|
1020 | chain_store = real_ctx->cert_store;
|
---|
1021 |
|
---|
1022 | if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
|
---|
1023 | untrusted = cpk->chain;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | xs_ctx = X509_STORE_CTX_new_ex(real_ctx->libctx, real_ctx->propq);
|
---|
1027 | if (xs_ctx == NULL) {
|
---|
1028 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
---|
1029 | goto err;
|
---|
1030 | }
|
---|
1031 | if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
|
---|
1032 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
---|
1033 | goto err;
|
---|
1034 | }
|
---|
1035 | /* Set suite B flags if needed */
|
---|
1036 | X509_STORE_CTX_set_flags(xs_ctx,
|
---|
1037 | c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
|
---|
1038 |
|
---|
1039 | i = X509_verify_cert(xs_ctx);
|
---|
1040 | if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
|
---|
1041 | if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
|
---|
1042 | ERR_clear_error();
|
---|
1043 | i = 1;
|
---|
1044 | rv = 2;
|
---|
1045 | }
|
---|
1046 | if (i > 0)
|
---|
1047 | chain = X509_STORE_CTX_get1_chain(xs_ctx);
|
---|
1048 | if (i <= 0) {
|
---|
1049 | i = X509_STORE_CTX_get_error(xs_ctx);
|
---|
1050 | ERR_raise_data(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED,
|
---|
1051 | "Verify error:%s", X509_verify_cert_error_string(i));
|
---|
1052 |
|
---|
1053 | goto err;
|
---|
1054 | }
|
---|
1055 | /* Remove EE certificate from chain */
|
---|
1056 | x = sk_X509_shift(chain);
|
---|
1057 | X509_free(x);
|
---|
1058 | if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
|
---|
1059 | if (sk_X509_num(chain) > 0) {
|
---|
1060 | /* See if last cert is self signed */
|
---|
1061 | x = sk_X509_value(chain, sk_X509_num(chain) - 1);
|
---|
1062 | if (X509_get_extension_flags(x) & EXFLAG_SS) {
|
---|
1063 | x = sk_X509_pop(chain);
|
---|
1064 | X509_free(x);
|
---|
1065 | }
|
---|
1066 | }
|
---|
1067 | }
|
---|
1068 | /*
|
---|
1069 | * Check security level of all CA certificates: EE will have been checked
|
---|
1070 | * already.
|
---|
1071 | */
|
---|
1072 | for (i = 0; i < sk_X509_num(chain); i++) {
|
---|
1073 | x = sk_X509_value(chain, i);
|
---|
1074 | rv = ssl_security_cert(s, ctx, x, 0, 0);
|
---|
1075 | if (rv != 1) {
|
---|
1076 | ERR_raise(ERR_LIB_SSL, rv);
|
---|
1077 | OSSL_STACK_OF_X509_free(chain);
|
---|
1078 | rv = 0;
|
---|
1079 | goto err;
|
---|
1080 | }
|
---|
1081 | }
|
---|
1082 | OSSL_STACK_OF_X509_free(cpk->chain);
|
---|
1083 | cpk->chain = chain;
|
---|
1084 | if (rv == 0)
|
---|
1085 | rv = 1;
|
---|
1086 | err:
|
---|
1087 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
|
---|
1088 | X509_STORE_free(chain_store);
|
---|
1089 | X509_STORE_CTX_free(xs_ctx);
|
---|
1090 |
|
---|
1091 | return rv;
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
|
---|
1095 | {
|
---|
1096 | X509_STORE **pstore;
|
---|
1097 | if (chain)
|
---|
1098 | pstore = &c->chain_store;
|
---|
1099 | else
|
---|
1100 | pstore = &c->verify_store;
|
---|
1101 | X509_STORE_free(*pstore);
|
---|
1102 | *pstore = store;
|
---|
1103 | if (ref && store)
|
---|
1104 | X509_STORE_up_ref(store);
|
---|
1105 | return 1;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | int ssl_cert_get_cert_store(CERT *c, X509_STORE **pstore, int chain)
|
---|
1109 | {
|
---|
1110 | *pstore = (chain ? c->chain_store : c->verify_store);
|
---|
1111 | return 1;
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | int ssl_get_security_level_bits(const SSL *s, const SSL_CTX *ctx, int *levelp)
|
---|
1115 | {
|
---|
1116 | int level;
|
---|
1117 | /*
|
---|
1118 | * note that there's a corresponding minbits_table
|
---|
1119 | * in crypto/x509/x509_vfy.c that's used for checking the security level
|
---|
1120 | * of RSA and DSA keys
|
---|
1121 | */
|
---|
1122 | static const int minbits_table[5 + 1] = { 0, 80, 112, 128, 192, 256 };
|
---|
1123 |
|
---|
1124 | if (ctx != NULL)
|
---|
1125 | level = SSL_CTX_get_security_level(ctx);
|
---|
1126 | else
|
---|
1127 | level = SSL_get_security_level(s);
|
---|
1128 |
|
---|
1129 | if (level > 5)
|
---|
1130 | level = 5;
|
---|
1131 | else if (level < 0)
|
---|
1132 | level = 0;
|
---|
1133 |
|
---|
1134 | if (levelp != NULL)
|
---|
1135 | *levelp = level;
|
---|
1136 |
|
---|
1137 | return minbits_table[level];
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
|
---|
1141 | int op, int bits, int nid, void *other,
|
---|
1142 | void *ex)
|
---|
1143 | {
|
---|
1144 | int level, minbits, pfs_mask;
|
---|
1145 | const SSL_CONNECTION *sc;
|
---|
1146 |
|
---|
1147 | minbits = ssl_get_security_level_bits(s, ctx, &level);
|
---|
1148 |
|
---|
1149 | if (level == 0) {
|
---|
1150 | /*
|
---|
1151 | * No EDH keys weaker than 1024-bits even at level 0, otherwise,
|
---|
1152 | * anything goes.
|
---|
1153 | */
|
---|
1154 | if (op == SSL_SECOP_TMP_DH && bits < 80)
|
---|
1155 | return 0;
|
---|
1156 | return 1;
|
---|
1157 | }
|
---|
1158 | switch (op) {
|
---|
1159 | case SSL_SECOP_CIPHER_SUPPORTED:
|
---|
1160 | case SSL_SECOP_CIPHER_SHARED:
|
---|
1161 | case SSL_SECOP_CIPHER_CHECK:
|
---|
1162 | {
|
---|
1163 | const SSL_CIPHER *c = other;
|
---|
1164 | /* No ciphers below security level */
|
---|
1165 | if (bits < minbits)
|
---|
1166 | return 0;
|
---|
1167 | /* No unauthenticated ciphersuites */
|
---|
1168 | if (c->algorithm_auth & SSL_aNULL)
|
---|
1169 | return 0;
|
---|
1170 | /* No MD5 mac ciphersuites */
|
---|
1171 | if (c->algorithm_mac & SSL_MD5)
|
---|
1172 | return 0;
|
---|
1173 | /* SHA1 HMAC is 160 bits of security */
|
---|
1174 | if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
|
---|
1175 | return 0;
|
---|
1176 | /* Level 3: forward secure ciphersuites only */
|
---|
1177 | pfs_mask = SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK;
|
---|
1178 | if (level >= 3 && c->min_tls != TLS1_3_VERSION &&
|
---|
1179 | !(c->algorithm_mkey & pfs_mask))
|
---|
1180 | return 0;
|
---|
1181 | break;
|
---|
1182 | }
|
---|
1183 | case SSL_SECOP_VERSION:
|
---|
1184 | if ((sc = SSL_CONNECTION_FROM_CONST_SSL(s)) == NULL)
|
---|
1185 | return 0;
|
---|
1186 | if (!SSL_CONNECTION_IS_DTLS(sc)) {
|
---|
1187 | /* SSLv3, TLS v1.0 and TLS v1.1 only allowed at level 0 */
|
---|
1188 | if (nid <= TLS1_1_VERSION && level > 0)
|
---|
1189 | return 0;
|
---|
1190 | } else {
|
---|
1191 | /* DTLS v1.0 only allowed at level 0 */
|
---|
1192 | if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level > 0)
|
---|
1193 | return 0;
|
---|
1194 | }
|
---|
1195 | break;
|
---|
1196 |
|
---|
1197 | case SSL_SECOP_COMPRESSION:
|
---|
1198 | if (level >= 2)
|
---|
1199 | return 0;
|
---|
1200 | break;
|
---|
1201 | case SSL_SECOP_TICKET:
|
---|
1202 | if (level >= 3)
|
---|
1203 | return 0;
|
---|
1204 | break;
|
---|
1205 | default:
|
---|
1206 | if (bits < minbits)
|
---|
1207 | return 0;
|
---|
1208 | }
|
---|
1209 | return 1;
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | int ssl_security(const SSL_CONNECTION *s, int op, int bits, int nid, void *other)
|
---|
1213 | {
|
---|
1214 | return s->cert->sec_cb(SSL_CONNECTION_GET_SSL(s), NULL, op, bits, nid,
|
---|
1215 | other, s->cert->sec_ex);
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
|
---|
1219 | {
|
---|
1220 | return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
|
---|
1221 | ctx->cert->sec_ex);
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | int ssl_cert_lookup_by_nid(int nid, size_t *pidx, SSL_CTX *ctx)
|
---|
1225 | {
|
---|
1226 | size_t i;
|
---|
1227 |
|
---|
1228 | for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
|
---|
1229 | if (ssl_cert_info[i].nid == nid) {
|
---|
1230 | *pidx = i;
|
---|
1231 | return 1;
|
---|
1232 | }
|
---|
1233 | }
|
---|
1234 | for (i = 0; i < ctx->sigalg_list_len; i++) {
|
---|
1235 | if (ctx->ssl_cert_info[i].nid == nid) {
|
---|
1236 | *pidx = SSL_PKEY_NUM + i;
|
---|
1237 | return 1;
|
---|
1238 | }
|
---|
1239 | }
|
---|
1240 | return 0;
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx, SSL_CTX *ctx)
|
---|
1244 | {
|
---|
1245 | size_t i;
|
---|
1246 |
|
---|
1247 | /* check classic pk types */
|
---|
1248 | for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
|
---|
1249 | const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i];
|
---|
1250 |
|
---|
1251 | if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
|
---|
1252 | || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
|
---|
1253 | if (pidx != NULL)
|
---|
1254 | *pidx = i;
|
---|
1255 | return tmp_lu;
|
---|
1256 | }
|
---|
1257 | }
|
---|
1258 | /* check provider-loaded pk types */
|
---|
1259 | for (i = 0; ctx->sigalg_list_len; i++) {
|
---|
1260 | SSL_CERT_LOOKUP *tmp_lu = &(ctx->ssl_cert_info[i]);
|
---|
1261 |
|
---|
1262 | if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
|
---|
1263 | || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
|
---|
1264 | if (pidx != NULL)
|
---|
1265 | *pidx = SSL_PKEY_NUM + i;
|
---|
1266 | return &ctx->ssl_cert_info[i];
|
---|
1267 | }
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | return NULL;
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx, SSL_CTX *ctx)
|
---|
1274 | {
|
---|
1275 | if (idx >= (OSSL_NELEM(ssl_cert_info) + ctx->sigalg_list_len))
|
---|
1276 | return NULL;
|
---|
1277 | else if (idx >= (OSSL_NELEM(ssl_cert_info)))
|
---|
1278 | return &(ctx->ssl_cert_info[idx - SSL_PKEY_NUM]);
|
---|
1279 | return &ssl_cert_info[idx];
|
---|
1280 | }
|
---|