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