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