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