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