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