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