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