]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * ! \file ssl/ssl_lib.c \brief Version independent SSL functions. | |
4f43d0e7 | 3 | */ |
58964a49 | 4 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
5 | * All rights reserved. |
6 | * | |
7 | * This package is an SSL implementation written | |
8 | * by Eric Young (eay@cryptsoft.com). | |
9 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 10 | * |
d02b48c6 RE |
11 | * This library is free for commercial and non-commercial use as long as |
12 | * the following conditions are aheared to. The following conditions | |
13 | * apply to all code found in this distribution, be it the RC4, RSA, | |
14 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
15 | * included with this distribution is covered by the same copyright terms | |
16 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 17 | * |
d02b48c6 RE |
18 | * Copyright remains Eric Young's, and as such any Copyright notices in |
19 | * the code are not to be removed. | |
20 | * If this package is used in a product, Eric Young should be given attribution | |
21 | * as the author of the parts of the library used. | |
22 | * This can be in the form of a textual message at program startup or | |
23 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 24 | * |
d02b48c6 RE |
25 | * Redistribution and use in source and binary forms, with or without |
26 | * modification, are permitted provided that the following conditions | |
27 | * are met: | |
28 | * 1. Redistributions of source code must retain the copyright | |
29 | * notice, this list of conditions and the following disclaimer. | |
30 | * 2. Redistributions in binary form must reproduce the above copyright | |
31 | * notice, this list of conditions and the following disclaimer in the | |
32 | * documentation and/or other materials provided with the distribution. | |
33 | * 3. All advertising materials mentioning features or use of this software | |
34 | * must display the following acknowledgement: | |
35 | * "This product includes cryptographic software written by | |
36 | * Eric Young (eay@cryptsoft.com)" | |
37 | * The word 'cryptographic' can be left out if the rouines from the library | |
38 | * being used are not cryptographic related :-). | |
0f113f3e | 39 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
40 | * the apps directory (application code) you must include an acknowledgement: |
41 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 42 | * |
d02b48c6 RE |
43 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
44 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
45 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
47 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
49 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
51 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
52 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
53 | * SUCH DAMAGE. | |
0f113f3e | 54 | * |
d02b48c6 RE |
55 | * The licence and distribution terms for any publically available version or |
56 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
57 | * copied and put under another distribution licence | |
58 | * [including the GNU Public Licence.] | |
59 | */ | |
bf21446a | 60 | /* ==================================================================== |
52b8dad8 | 61 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
bf21446a BM |
62 | * |
63 | * Redistribution and use in source and binary forms, with or without | |
64 | * modification, are permitted provided that the following conditions | |
65 | * are met: | |
66 | * | |
67 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 68 | * notice, this list of conditions and the following disclaimer. |
bf21446a BM |
69 | * |
70 | * 2. Redistributions in binary form must reproduce the above copyright | |
71 | * notice, this list of conditions and the following disclaimer in | |
72 | * the documentation and/or other materials provided with the | |
73 | * distribution. | |
74 | * | |
75 | * 3. All advertising materials mentioning features or use of this | |
76 | * software must display the following acknowledgment: | |
77 | * "This product includes software developed by the OpenSSL Project | |
78 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
79 | * | |
80 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
81 | * endorse or promote products derived from this software without | |
82 | * prior written permission. For written permission, please contact | |
83 | * openssl-core@openssl.org. | |
84 | * | |
85 | * 5. Products derived from this software may not be called "OpenSSL" | |
86 | * nor may "OpenSSL" appear in their names without prior written | |
87 | * permission of the OpenSSL Project. | |
88 | * | |
89 | * 6. Redistributions of any form whatsoever must retain the following | |
90 | * acknowledgment: | |
91 | * "This product includes software developed by the OpenSSL Project | |
92 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
93 | * | |
94 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
95 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
96 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
97 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
98 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
99 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
100 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
101 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
102 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
103 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
104 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
105 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
106 | * ==================================================================== | |
107 | * | |
108 | * This product includes cryptographic software written by Eric Young | |
109 | * (eay@cryptsoft.com). This product includes software written by Tim | |
110 | * Hudson (tjh@cryptsoft.com). | |
111 | * | |
112 | */ | |
ea262260 BM |
113 | /* ==================================================================== |
114 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
0f113f3e | 115 | * ECC cipher suite support in OpenSSL originally developed by |
ea262260 BM |
116 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
117 | */ | |
ddac1974 NL |
118 | /* ==================================================================== |
119 | * Copyright 2005 Nokia. All rights reserved. | |
120 | * | |
121 | * The portions of the attached software ("Contribution") is developed by | |
122 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source | |
123 | * license. | |
124 | * | |
125 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of | |
126 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites | |
127 | * support (see RFC 4279) to OpenSSL. | |
128 | * | |
129 | * No patent licenses or other rights except those expressly stated in | |
130 | * the OpenSSL open source license shall be deemed granted or received | |
131 | * expressly, by implication, estoppel, or otherwise. | |
132 | * | |
133 | * No assurances are provided by Nokia that the Contribution does not | |
134 | * infringe the patent or other intellectual property rights of any third | |
135 | * party or that the license provides you with all the necessary rights | |
136 | * to make use of the Contribution. | |
137 | * | |
138 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN | |
139 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA | |
140 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY | |
141 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR | |
142 | * OTHERWISE. | |
143 | */ | |
bbb8de09 | 144 | |
f3f1cf84 | 145 | #ifdef REF_DEBUG |
0f113f3e | 146 | # include <assert.h> |
027e257b | 147 | #endif |
d02b48c6 | 148 | #include <stdio.h> |
7b63c0fa | 149 | #include "ssl_locl.h" |
ec577822 BM |
150 | #include <openssl/objects.h> |
151 | #include <openssl/lhash.h> | |
bb7cd4e3 | 152 | #include <openssl/x509v3.h> |
6434abbf | 153 | #include <openssl/rand.h> |
67c8e7f4 | 154 | #include <openssl/ocsp.h> |
3eeaab4b | 155 | #ifndef OPENSSL_NO_DH |
0f113f3e | 156 | # include <openssl/dh.h> |
3eeaab4b | 157 | #endif |
bdfe932d | 158 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
159 | # include <openssl/engine.h> |
160 | #endif | |
07bbc92c | 161 | #include <openssl/async.h> |
ed29e82a RP |
162 | #ifndef OPENSSL_NO_CT |
163 | # include <openssl/ct.h> | |
164 | #endif | |
0f113f3e | 165 | |
df2ee0e2 | 166 | const char SSL_version_str[] = OPENSSL_VERSION_TEXT; |
0f113f3e MC |
167 | |
168 | SSL3_ENC_METHOD ssl3_undef_enc_method = { | |
169 | /* | |
170 | * evil casts, but these functions are only called if there's a library | |
171 | * bug | |
172 | */ | |
d102d9df MC |
173 | (int (*)(SSL *, SSL3_RECORD *, unsigned int, int))ssl_undefined_function, |
174 | (int (*)(SSL *, SSL3_RECORD *, unsigned char *, int))ssl_undefined_function, | |
0f113f3e MC |
175 | ssl_undefined_function, |
176 | (int (*)(SSL *, unsigned char *, unsigned char *, int)) | |
177 | ssl_undefined_function, | |
178 | (int (*)(SSL *, int))ssl_undefined_function, | |
179 | (int (*)(SSL *, const char *, int, unsigned char *)) | |
180 | ssl_undefined_function, | |
181 | 0, /* finish_mac_length */ | |
0f113f3e MC |
182 | NULL, /* client_finished_label */ |
183 | 0, /* client_finished_label_len */ | |
184 | NULL, /* server_finished_label */ | |
185 | 0, /* server_finished_label_len */ | |
186 | (int (*)(int))ssl_undefined_function, | |
187 | (int (*)(SSL *, unsigned char *, size_t, const char *, | |
188 | size_t, const unsigned char *, size_t, | |
189 | int use_context))ssl_undefined_function, | |
190 | }; | |
d02b48c6 | 191 | |
07bbc92c MC |
192 | struct ssl_async_args { |
193 | SSL *s; | |
194 | void *buf; | |
195 | int num; | |
ec447924 | 196 | enum { READFUNC, WRITEFUNC, OTHERFUNC} type; |
add2f5ca | 197 | union { |
ec447924 MC |
198 | int (*func_read)(SSL *, void *, int); |
199 | int (*func_write)(SSL *, const void *, int); | |
200 | int (*func_other)(SSL *); | |
add2f5ca | 201 | } f; |
07bbc92c MC |
202 | }; |
203 | ||
919ba009 VD |
204 | static const struct { |
205 | uint8_t mtype; | |
206 | uint8_t ord; | |
207 | int nid; | |
208 | } dane_mds[] = { | |
209 | { DANETLS_MATCHING_FULL, 0, NID_undef }, | |
210 | { DANETLS_MATCHING_2256, 1, NID_sha256 }, | |
211 | { DANETLS_MATCHING_2512, 2, NID_sha512 }, | |
212 | }; | |
213 | ||
214 | static int dane_ctx_enable(struct dane_ctx_st *dctx) | |
215 | { | |
216 | const EVP_MD **mdevp; | |
217 | uint8_t *mdord; | |
218 | uint8_t mdmax = DANETLS_MATCHING_LAST; | |
219 | int n = ((int) mdmax) + 1; /* int to handle PrivMatch(255) */ | |
220 | size_t i; | |
221 | ||
222 | mdevp = OPENSSL_zalloc(n * sizeof(*mdevp)); | |
223 | mdord = OPENSSL_zalloc(n * sizeof(*mdord)); | |
224 | ||
225 | if (mdord == NULL || mdevp == NULL) { | |
226 | OPENSSL_free(mdevp); | |
227 | SSLerr(SSL_F_DANE_CTX_ENABLE, ERR_R_MALLOC_FAILURE); | |
228 | return 0; | |
229 | } | |
230 | ||
231 | /* Install default entries */ | |
232 | for (i = 0; i < OSSL_NELEM(dane_mds); ++i) { | |
233 | const EVP_MD *md; | |
234 | ||
235 | if (dane_mds[i].nid == NID_undef || | |
236 | (md = EVP_get_digestbynid(dane_mds[i].nid)) == NULL) | |
237 | continue; | |
238 | mdevp[dane_mds[i].mtype] = md; | |
239 | mdord[dane_mds[i].mtype] = dane_mds[i].ord; | |
240 | } | |
241 | ||
242 | dctx->mdevp = mdevp; | |
243 | dctx->mdord = mdord; | |
244 | dctx->mdmax = mdmax; | |
245 | ||
246 | return 1; | |
247 | } | |
248 | ||
249 | static void dane_ctx_final(struct dane_ctx_st *dctx) | |
250 | { | |
251 | OPENSSL_free(dctx->mdevp); | |
252 | dctx->mdevp = NULL; | |
253 | ||
254 | OPENSSL_free(dctx->mdord); | |
255 | dctx->mdord = NULL; | |
256 | dctx->mdmax = 0; | |
257 | } | |
258 | ||
259 | static void tlsa_free(danetls_record *t) | |
260 | { | |
261 | if (t == NULL) | |
262 | return; | |
263 | OPENSSL_free(t->data); | |
264 | EVP_PKEY_free(t->spki); | |
265 | OPENSSL_free(t); | |
266 | } | |
267 | ||
268 | static void dane_final(struct dane_st *dane) | |
269 | { | |
270 | sk_danetls_record_pop_free(dane->trecs, tlsa_free); | |
271 | dane->trecs = NULL; | |
272 | ||
273 | sk_X509_pop_free(dane->certs, X509_free); | |
274 | dane->certs = NULL; | |
275 | ||
276 | X509_free(dane->mcert); | |
277 | dane->mcert = NULL; | |
278 | dane->mtlsa = NULL; | |
279 | dane->mdpth = -1; | |
280 | dane->pdpth = -1; | |
281 | } | |
282 | ||
283 | /* | |
284 | * dane_copy - Copy dane configuration, sans verification state. | |
285 | */ | |
286 | static int ssl_dane_dup(SSL *to, SSL *from) | |
287 | { | |
288 | int num; | |
289 | int i; | |
290 | ||
291 | if (!DANETLS_ENABLED(&from->dane)) | |
292 | return 1; | |
293 | ||
294 | dane_final(&to->dane); | |
295 | ||
296 | num = sk_danetls_record_num(from->dane.trecs); | |
297 | for (i = 0; i < num; ++i) { | |
298 | danetls_record *t = sk_danetls_record_value(from->dane.trecs, i); | |
299 | if (SSL_dane_tlsa_add(to, t->usage, t->selector, t->mtype, | |
300 | t->data, t->dlen) <= 0) | |
301 | return 0; | |
302 | } | |
303 | return 1; | |
304 | } | |
305 | ||
306 | static int dane_mtype_set( | |
307 | struct dane_ctx_st *dctx, | |
308 | const EVP_MD *md, | |
309 | uint8_t mtype, | |
310 | uint8_t ord) | |
311 | { | |
312 | int i; | |
313 | ||
314 | if (mtype == DANETLS_MATCHING_FULL && md != NULL) { | |
315 | SSLerr(SSL_F_DANE_MTYPE_SET, | |
316 | SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL); | |
317 | return 0; | |
318 | } | |
319 | ||
320 | if (mtype > dctx->mdmax) { | |
321 | const EVP_MD **mdevp; | |
322 | uint8_t *mdord; | |
323 | int n = ((int) mtype) + 1; | |
324 | ||
325 | mdevp = OPENSSL_realloc(dctx->mdevp, n * sizeof(*mdevp)); | |
326 | if (mdevp == NULL) { | |
327 | SSLerr(SSL_F_DANE_MTYPE_SET, ERR_R_MALLOC_FAILURE); | |
328 | return -1; | |
329 | } | |
330 | dctx->mdevp = mdevp; | |
331 | ||
332 | mdord = OPENSSL_realloc(dctx->mdord, n * sizeof(*mdord)); | |
333 | if (mdord == NULL) { | |
334 | SSLerr(SSL_F_DANE_MTYPE_SET, ERR_R_MALLOC_FAILURE); | |
335 | return -1; | |
336 | } | |
337 | dctx->mdord = mdord; | |
338 | ||
339 | /* Zero-fill any gaps */ | |
340 | for (i = dctx->mdmax+1; i < mtype; ++i) { | |
341 | mdevp[i] = NULL; | |
342 | mdord[i] = 0; | |
343 | } | |
344 | ||
345 | dctx->mdmax = mtype; | |
346 | } | |
347 | ||
348 | dctx->mdevp[mtype] = md; | |
349 | /* Coerce ordinal of disabled matching types to 0 */ | |
350 | dctx->mdord[mtype] = (md == NULL) ? 0 : ord; | |
351 | ||
352 | return 1; | |
353 | } | |
354 | ||
355 | static const EVP_MD *tlsa_md_get(struct dane_st *dane, uint8_t mtype) | |
356 | { | |
357 | if (mtype > dane->dctx->mdmax) | |
358 | return NULL; | |
359 | return dane->dctx->mdevp[mtype]; | |
360 | } | |
361 | ||
362 | static int dane_tlsa_add( | |
363 | struct dane_st *dane, | |
364 | uint8_t usage, | |
365 | uint8_t selector, | |
366 | uint8_t mtype, | |
367 | unsigned char *data, | |
368 | size_t dlen) | |
369 | { | |
370 | danetls_record *t; | |
371 | const EVP_MD *md = NULL; | |
372 | int ilen = (int)dlen; | |
373 | int i; | |
374 | ||
375 | if (dane->trecs == NULL) { | |
376 | SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_NOT_ENABLED); | |
377 | return -1; | |
378 | } | |
379 | ||
380 | if (ilen < 0 || dlen != (size_t)ilen) { | |
381 | SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_DATA_LENGTH); | |
382 | return 0; | |
383 | } | |
384 | ||
385 | if (usage > DANETLS_USAGE_LAST) { | |
386 | SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE); | |
387 | return 0; | |
388 | } | |
389 | ||
390 | if (selector > DANETLS_SELECTOR_LAST) { | |
391 | SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_SELECTOR); | |
392 | return 0; | |
393 | } | |
394 | ||
395 | if (mtype != DANETLS_MATCHING_FULL) { | |
396 | md = tlsa_md_get(dane, mtype); | |
397 | if (md == NULL) { | |
398 | SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE); | |
399 | return 0; | |
400 | } | |
401 | } | |
402 | ||
403 | if (md != NULL && dlen != (size_t)EVP_MD_size(md)) { | |
404 | SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH); | |
405 | return 0; | |
406 | } | |
407 | if (!data) { | |
408 | SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_NULL_DATA); | |
409 | return 0; | |
410 | } | |
411 | ||
412 | if ((t = OPENSSL_zalloc(sizeof(*t))) == NULL) { | |
413 | SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE); | |
414 | return -1; | |
415 | } | |
416 | ||
417 | t->usage = usage; | |
418 | t->selector = selector; | |
419 | t->mtype = mtype; | |
420 | t->data = OPENSSL_malloc(ilen); | |
421 | if (t->data == NULL) { | |
422 | tlsa_free(t); | |
423 | SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE); | |
424 | return -1; | |
425 | } | |
426 | memcpy(t->data, data, ilen); | |
427 | t->dlen = ilen; | |
428 | ||
429 | /* Validate and cache full certificate or public key */ | |
430 | if (mtype == DANETLS_MATCHING_FULL) { | |
431 | const unsigned char *p = data; | |
432 | X509 *cert = NULL; | |
433 | EVP_PKEY *pkey = NULL; | |
434 | ||
435 | switch (selector) { | |
436 | case DANETLS_SELECTOR_CERT: | |
437 | if (!d2i_X509(&cert, &p, dlen) || p < data || | |
438 | dlen != (size_t)(p - data)) { | |
439 | tlsa_free(t); | |
440 | SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE); | |
441 | return 0; | |
442 | } | |
443 | if (X509_get0_pubkey(cert) == NULL) { | |
444 | tlsa_free(t); | |
445 | SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE); | |
446 | return 0; | |
447 | } | |
448 | ||
449 | if ((DANETLS_USAGE_BIT(usage) & DANETLS_TA_MASK) == 0) { | |
450 | X509_free(cert); | |
451 | break; | |
452 | } | |
453 | ||
454 | /* | |
455 | * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA | |
456 | * records that contain full certificates of trust-anchors that are | |
457 | * not present in the wire chain. For usage PKIX-TA(0), we augment | |
458 | * the chain with untrusted Full(0) certificates from DNS, in case | |
459 | * they are missing from the chain. | |
460 | */ | |
461 | if ((dane->certs == NULL && | |
462 | (dane->certs = sk_X509_new_null()) == NULL) || | |
463 | !sk_X509_push(dane->certs, cert)) { | |
464 | SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE); | |
465 | X509_free(cert); | |
466 | tlsa_free(t); | |
467 | return -1; | |
468 | } | |
469 | break; | |
470 | ||
471 | case DANETLS_SELECTOR_SPKI: | |
472 | if (!d2i_PUBKEY(&pkey, &p, dlen) || p < data || | |
473 | dlen != (size_t)(p - data)) { | |
474 | tlsa_free(t); | |
475 | SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY); | |
476 | return 0; | |
477 | } | |
478 | ||
479 | /* | |
480 | * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA | |
481 | * records that contain full bare keys of trust-anchors that are | |
482 | * not present in the wire chain. | |
483 | */ | |
484 | if (usage == DANETLS_USAGE_DANE_TA) | |
485 | t->spki = pkey; | |
486 | else | |
487 | EVP_PKEY_free(pkey); | |
488 | break; | |
489 | } | |
490 | } | |
491 | ||
492 | /*- | |
493 | * Find the right insertion point for the new record. | |
494 | * | |
495 | * See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that | |
496 | * they can be processed first, as they require no chain building, and no | |
497 | * expiration or hostname checks. Because DANE-EE(3) is numerically | |
498 | * largest, this is accomplished via descending sort by "usage". | |
499 | * | |
500 | * We also sort in descending order by matching ordinal to simplify | |
501 | * the implementation of digest agility in the verification code. | |
502 | * | |
503 | * The choice of order for the selector is not significant, so we | |
504 | * use the same descending order for consistency. | |
505 | */ | |
506 | for (i = 0; i < sk_danetls_record_num(dane->trecs); ++i) { | |
507 | danetls_record *rec = sk_danetls_record_value(dane->trecs, i); | |
508 | if (rec->usage > usage) | |
509 | continue; | |
510 | if (rec->usage < usage) | |
511 | break; | |
512 | if (rec->selector > selector) | |
513 | continue; | |
514 | if (rec->selector < selector) | |
515 | break; | |
516 | if (dane->dctx->mdord[rec->mtype] > dane->dctx->mdord[mtype]) | |
517 | continue; | |
518 | break; | |
519 | } | |
520 | ||
521 | if (!sk_danetls_record_insert(dane->trecs, t, i)) { | |
522 | tlsa_free(t); | |
523 | SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE); | |
524 | return -1; | |
525 | } | |
526 | dane->umask |= DANETLS_USAGE_BIT(usage); | |
527 | ||
528 | return 1; | |
529 | } | |
530 | ||
d31fb0b5 RS |
531 | static void clear_ciphers(SSL *s) |
532 | { | |
533 | /* clear the current cipher */ | |
534 | ssl_clear_cipher_ctx(s); | |
535 | ssl_clear_hash_ctx(&s->read_hash); | |
536 | ssl_clear_hash_ctx(&s->write_hash); | |
537 | } | |
538 | ||
4f43d0e7 | 539 | int SSL_clear(SSL *s) |
0f113f3e | 540 | { |
0f113f3e MC |
541 | if (s->method == NULL) { |
542 | SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED); | |
543 | return (0); | |
544 | } | |
d02b48c6 | 545 | |
0f113f3e MC |
546 | if (ssl_clear_bad_session(s)) { |
547 | SSL_SESSION_free(s->session); | |
548 | s->session = NULL; | |
549 | } | |
d62bfb39 | 550 | |
0f113f3e MC |
551 | s->error = 0; |
552 | s->hit = 0; | |
553 | s->shutdown = 0; | |
d02b48c6 | 554 | |
0f113f3e MC |
555 | if (s->renegotiate) { |
556 | SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR); | |
557 | return 0; | |
558 | } | |
d02b48c6 | 559 | |
fe3a3291 | 560 | ossl_statem_clear(s); |
413c4f45 | 561 | |
0f113f3e MC |
562 | s->version = s->method->version; |
563 | s->client_version = s->version; | |
564 | s->rwstate = SSL_NOTHING; | |
d02b48c6 | 565 | |
25aaa98a RS |
566 | BUF_MEM_free(s->init_buf); |
567 | s->init_buf = NULL; | |
d31fb0b5 | 568 | clear_ciphers(s); |
0f113f3e | 569 | s->first_packet = 0; |
d02b48c6 | 570 | |
919ba009 VD |
571 | /* Reset DANE verification result state */ |
572 | s->dane.mdpth = -1; | |
573 | s->dane.pdpth = -1; | |
574 | X509_free(s->dane.mcert); | |
575 | s->dane.mcert = NULL; | |
576 | s->dane.mtlsa = NULL; | |
577 | ||
578 | /* Clear the verification result peername */ | |
579 | X509_VERIFY_PARAM_move_peername(s->param, NULL); | |
580 | ||
0f113f3e MC |
581 | /* |
582 | * Check to see if we were changed into a different method, if so, revert | |
583 | * back if we are not doing session-id reuse. | |
584 | */ | |
024f543c | 585 | if (!ossl_statem_get_in_handshake(s) && (s->session == NULL) |
0f113f3e MC |
586 | && (s->method != s->ctx->method)) { |
587 | s->method->ssl_free(s); | |
588 | s->method = s->ctx->method; | |
589 | if (!s->method->ssl_new(s)) | |
590 | return (0); | |
591 | } else | |
0f113f3e | 592 | s->method->ssl_clear(s); |
33d23b87 | 593 | |
af9752e5 | 594 | RECORD_LAYER_clear(&s->rlayer); |
33d23b87 | 595 | |
0f113f3e MC |
596 | return (1); |
597 | } | |
d02b48c6 | 598 | |
4f43d0e7 | 599 | /** Used to change an SSL_CTXs default SSL method type */ |
0f113f3e MC |
600 | int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth) |
601 | { | |
602 | STACK_OF(SSL_CIPHER) *sk; | |
603 | ||
604 | ctx->method = meth; | |
605 | ||
606 | sk = ssl_create_cipher_list(ctx->method, &(ctx->cipher_list), | |
607 | &(ctx->cipher_list_by_id), | |
608 | SSL_DEFAULT_CIPHER_LIST, ctx->cert); | |
609 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) { | |
610 | SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION, | |
611 | SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS); | |
612 | return (0); | |
613 | } | |
614 | return (1); | |
615 | } | |
d02b48c6 | 616 | |
4f43d0e7 | 617 | SSL *SSL_new(SSL_CTX *ctx) |
0f113f3e MC |
618 | { |
619 | SSL *s; | |
620 | ||
621 | if (ctx == NULL) { | |
622 | SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX); | |
623 | return (NULL); | |
624 | } | |
625 | if (ctx->method == NULL) { | |
626 | SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION); | |
627 | return (NULL); | |
628 | } | |
629 | ||
b51bce94 | 630 | s = OPENSSL_zalloc(sizeof(*s)); |
0f113f3e MC |
631 | if (s == NULL) |
632 | goto err; | |
0f113f3e | 633 | |
16203f7b AG |
634 | s->lock = CRYPTO_THREAD_lock_new(); |
635 | if (s->lock == NULL) { | |
636 | SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE); | |
637 | OPENSSL_free(s); | |
638 | return NULL; | |
639 | } | |
640 | ||
c036e210 | 641 | RECORD_LAYER_init(&s->rlayer, s); |
28d59af8 | 642 | |
0f113f3e | 643 | s->options = ctx->options; |
7946ab33 KR |
644 | s->min_proto_version = ctx->min_proto_version; |
645 | s->max_proto_version = ctx->max_proto_version; | |
0f113f3e MC |
646 | s->mode = ctx->mode; |
647 | s->max_cert_list = ctx->max_cert_list; | |
0e04674e | 648 | s->references = 1; |
0f113f3e | 649 | |
2c382349 KR |
650 | /* |
651 | * Earlier library versions used to copy the pointer to the CERT, not | |
652 | * its contents; only when setting new parameters for the per-SSL | |
653 | * copy, ssl_cert_new would be called (and the direct reference to | |
654 | * the per-SSL_CTX settings would be lost, but those still were | |
655 | * indirectly accessed for various purposes, and for that reason they | |
656 | * used to be known as s->ctx->default_cert). Now we don't look at the | |
657 | * SSL_CTX's CERT after having duplicated it once. | |
658 | */ | |
659 | s->cert = ssl_cert_dup(ctx->cert); | |
660 | if (s->cert == NULL) | |
661 | goto err; | |
0f113f3e | 662 | |
52e1d7b1 | 663 | RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead); |
0f113f3e MC |
664 | s->msg_callback = ctx->msg_callback; |
665 | s->msg_callback_arg = ctx->msg_callback_arg; | |
666 | s->verify_mode = ctx->verify_mode; | |
667 | s->not_resumable_session_cb = ctx->not_resumable_session_cb; | |
0f113f3e MC |
668 | s->sid_ctx_length = ctx->sid_ctx_length; |
669 | OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx); | |
670 | memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx)); | |
671 | s->verify_callback = ctx->default_verify_callback; | |
672 | s->generate_session_id = ctx->generate_session_id; | |
673 | ||
674 | s->param = X509_VERIFY_PARAM_new(); | |
a71edf3b | 675 | if (s->param == NULL) |
0f113f3e MC |
676 | goto err; |
677 | X509_VERIFY_PARAM_inherit(s->param, ctx->param); | |
0f113f3e MC |
678 | s->quiet_shutdown = ctx->quiet_shutdown; |
679 | s->max_send_fragment = ctx->max_send_fragment; | |
d102d9df MC |
680 | s->split_send_fragment = ctx->split_send_fragment; |
681 | s->max_pipelines = ctx->max_pipelines; | |
94777c9c MC |
682 | if (s->max_pipelines > 1) |
683 | RECORD_LAYER_set_read_ahead(&s->rlayer, 1); | |
dad78fb1 MC |
684 | if (ctx->default_read_buf_len > 0) |
685 | SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len); | |
bf21446a | 686 | |
16203f7b | 687 | SSL_CTX_up_ref(ctx); |
0f113f3e | 688 | s->ctx = ctx; |
0f113f3e MC |
689 | s->tlsext_debug_cb = 0; |
690 | s->tlsext_debug_arg = NULL; | |
691 | s->tlsext_ticket_expected = 0; | |
692 | s->tlsext_status_type = -1; | |
693 | s->tlsext_status_expected = 0; | |
694 | s->tlsext_ocsp_ids = NULL; | |
695 | s->tlsext_ocsp_exts = NULL; | |
696 | s->tlsext_ocsp_resp = NULL; | |
697 | s->tlsext_ocsp_resplen = -1; | |
16203f7b | 698 | SSL_CTX_up_ref(ctx); |
0f113f3e MC |
699 | s->initial_ctx = ctx; |
700 | # ifndef OPENSSL_NO_EC | |
701 | if (ctx->tlsext_ecpointformatlist) { | |
702 | s->tlsext_ecpointformatlist = | |
7644a9ae RS |
703 | OPENSSL_memdup(ctx->tlsext_ecpointformatlist, |
704 | ctx->tlsext_ecpointformatlist_length); | |
0f113f3e MC |
705 | if (!s->tlsext_ecpointformatlist) |
706 | goto err; | |
707 | s->tlsext_ecpointformatlist_length = | |
708 | ctx->tlsext_ecpointformatlist_length; | |
709 | } | |
710 | if (ctx->tlsext_ellipticcurvelist) { | |
711 | s->tlsext_ellipticcurvelist = | |
7644a9ae RS |
712 | OPENSSL_memdup(ctx->tlsext_ellipticcurvelist, |
713 | ctx->tlsext_ellipticcurvelist_length); | |
0f113f3e MC |
714 | if (!s->tlsext_ellipticcurvelist) |
715 | goto err; | |
716 | s->tlsext_ellipticcurvelist_length = | |
717 | ctx->tlsext_ellipticcurvelist_length; | |
718 | } | |
719 | # endif | |
bf48836c | 720 | # ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e | 721 | s->next_proto_negotiated = NULL; |
ee2ffc27 | 722 | # endif |
6f017a8f | 723 | |
0f113f3e MC |
724 | if (s->ctx->alpn_client_proto_list) { |
725 | s->alpn_client_proto_list = | |
726 | OPENSSL_malloc(s->ctx->alpn_client_proto_list_len); | |
727 | if (s->alpn_client_proto_list == NULL) | |
728 | goto err; | |
729 | memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list, | |
730 | s->ctx->alpn_client_proto_list_len); | |
731 | s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len; | |
732 | } | |
d02b48c6 | 733 | |
696178ed | 734 | s->verified_chain = NULL; |
0f113f3e | 735 | s->verify_result = X509_V_OK; |
d02b48c6 | 736 | |
a974e64a MC |
737 | s->default_passwd_callback = ctx->default_passwd_callback; |
738 | s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata; | |
739 | ||
0f113f3e | 740 | s->method = ctx->method; |
d02b48c6 | 741 | |
0f113f3e MC |
742 | if (!s->method->ssl_new(s)) |
743 | goto err; | |
d02b48c6 | 744 | |
0f113f3e | 745 | s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1; |
bf21446a | 746 | |
61986d32 | 747 | if (!SSL_clear(s)) |
69f68237 | 748 | goto err; |
58964a49 | 749 | |
0f113f3e | 750 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); |
58964a49 | 751 | |
ddac1974 | 752 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
753 | s->psk_client_callback = ctx->psk_client_callback; |
754 | s->psk_server_callback = ctx->psk_server_callback; | |
ddac1974 NL |
755 | #endif |
756 | ||
07bbc92c MC |
757 | s->job = NULL; |
758 | ||
ed29e82a RP |
759 | #ifndef OPENSSL_NO_CT |
760 | if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback, | |
761 | ctx->ct_validation_callback_arg)) | |
762 | goto err; | |
763 | #endif | |
764 | ||
16203f7b | 765 | return s; |
0f113f3e | 766 | err: |
62adbcee | 767 | SSL_free(s); |
0f113f3e | 768 | SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE); |
16203f7b | 769 | return NULL; |
0f113f3e | 770 | } |
d02b48c6 | 771 | |
a18a31e4 MC |
772 | void SSL_up_ref(SSL *s) |
773 | { | |
16203f7b AG |
774 | int i; |
775 | CRYPTO_atomic_add(&s->references, 1, &i, s->lock); | |
a18a31e4 MC |
776 | } |
777 | ||
0f113f3e MC |
778 | int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx, |
779 | unsigned int sid_ctx_len) | |
780 | { | |
781 | if (sid_ctx_len > sizeof ctx->sid_ctx) { | |
782 | SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT, | |
783 | SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); | |
784 | return 0; | |
785 | } | |
786 | ctx->sid_ctx_length = sid_ctx_len; | |
787 | memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len); | |
4eb77b26 BM |
788 | |
789 | return 1; | |
0f113f3e | 790 | } |
4eb77b26 | 791 | |
0f113f3e MC |
792 | int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx, |
793 | unsigned int sid_ctx_len) | |
794 | { | |
795 | if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { | |
796 | SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT, | |
797 | SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); | |
798 | return 0; | |
799 | } | |
800 | ssl->sid_ctx_length = sid_ctx_len; | |
801 | memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len); | |
b4cadc6e BL |
802 | |
803 | return 1; | |
0f113f3e | 804 | } |
b4cadc6e | 805 | |
dc644fe2 | 806 | int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) |
0f113f3e | 807 | { |
16203f7b | 808 | CRYPTO_THREAD_write_lock(ctx->lock); |
0f113f3e | 809 | ctx->generate_session_id = cb; |
16203f7b | 810 | CRYPTO_THREAD_unlock(ctx->lock); |
0f113f3e MC |
811 | return 1; |
812 | } | |
dc644fe2 GT |
813 | |
814 | int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) | |
0f113f3e | 815 | { |
16203f7b | 816 | CRYPTO_THREAD_write_lock(ssl->lock); |
0f113f3e | 817 | ssl->generate_session_id = cb; |
16203f7b | 818 | CRYPTO_THREAD_unlock(ssl->lock); |
0f113f3e MC |
819 | return 1; |
820 | } | |
dc644fe2 | 821 | |
f85c9904 | 822 | int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id, |
0f113f3e MC |
823 | unsigned int id_len) |
824 | { | |
825 | /* | |
826 | * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how | |
827 | * we can "construct" a session to give us the desired check - ie. to | |
828 | * find if there's a session in the hash table that would conflict with | |
829 | * any new session built out of this id/id_len and the ssl_version in use | |
830 | * by this SSL. | |
831 | */ | |
832 | SSL_SESSION r, *p; | |
833 | ||
834 | if (id_len > sizeof r.session_id) | |
835 | return 0; | |
836 | ||
837 | r.ssl_version = ssl->version; | |
838 | r.session_id_length = id_len; | |
839 | memcpy(r.session_id, id, id_len); | |
840 | ||
16203f7b | 841 | CRYPTO_THREAD_read_lock(ssl->ctx->lock); |
0f113f3e | 842 | p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r); |
16203f7b | 843 | CRYPTO_THREAD_unlock(ssl->ctx->lock); |
0f113f3e MC |
844 | return (p != NULL); |
845 | } | |
dc644fe2 | 846 | |
bb7cd4e3 | 847 | int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) |
0f113f3e MC |
848 | { |
849 | return X509_VERIFY_PARAM_set_purpose(s->param, purpose); | |
850 | } | |
bb7cd4e3 DSH |
851 | |
852 | int SSL_set_purpose(SSL *s, int purpose) | |
0f113f3e MC |
853 | { |
854 | return X509_VERIFY_PARAM_set_purpose(s->param, purpose); | |
855 | } | |
926a56bf | 856 | |
bb7cd4e3 | 857 | int SSL_CTX_set_trust(SSL_CTX *s, int trust) |
0f113f3e MC |
858 | { |
859 | return X509_VERIFY_PARAM_set_trust(s->param, trust); | |
860 | } | |
bb7cd4e3 DSH |
861 | |
862 | int SSL_set_trust(SSL *s, int trust) | |
0f113f3e MC |
863 | { |
864 | return X509_VERIFY_PARAM_set_trust(s->param, trust); | |
865 | } | |
bb7cd4e3 | 866 | |
919ba009 VD |
867 | int SSL_set1_host(SSL *s, const char *hostname) |
868 | { | |
869 | return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0); | |
870 | } | |
871 | ||
872 | int SSL_add1_host(SSL *s, const char *hostname) | |
873 | { | |
874 | return X509_VERIFY_PARAM_add1_host(s->param, hostname, 0); | |
875 | } | |
876 | ||
877 | void SSL_set_hostflags(SSL *s, unsigned int flags) | |
878 | { | |
879 | X509_VERIFY_PARAM_set_hostflags(s->param, flags); | |
880 | } | |
881 | ||
882 | const char *SSL_get0_peername(SSL *s) | |
883 | { | |
884 | return X509_VERIFY_PARAM_get0_peername(s->param); | |
885 | } | |
886 | ||
887 | int SSL_CTX_dane_enable(SSL_CTX *ctx) | |
888 | { | |
889 | return dane_ctx_enable(&ctx->dane); | |
890 | } | |
891 | ||
892 | int SSL_dane_enable(SSL *s, const char *basedomain) | |
893 | { | |
894 | struct dane_st *dane = &s->dane; | |
895 | ||
896 | if (s->ctx->dane.mdmax == 0) { | |
897 | SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_CONTEXT_NOT_DANE_ENABLED); | |
898 | return 0; | |
899 | } | |
900 | if (dane->trecs != NULL) { | |
901 | SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_DANE_ALREADY_ENABLED); | |
902 | return 0; | |
903 | } | |
904 | ||
8d887efa VD |
905 | /* |
906 | * Default SNI name. This rejects empty names, while set1_host below | |
907 | * accepts them and disables host name checks. To avoid side-effects with | |
908 | * invalid input, set the SNI name first. | |
909 | */ | |
910 | if (s->tlsext_hostname == NULL) { | |
911 | if (!SSL_set_tlsext_host_name(s, basedomain)) { | |
912 | SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN); | |
913 | return -1; | |
914 | } | |
915 | } | |
916 | ||
919ba009 VD |
917 | /* Primary RFC6125 reference identifier */ |
918 | if (!X509_VERIFY_PARAM_set1_host(s->param, basedomain, 0)) { | |
919 | SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN); | |
920 | return -1; | |
921 | } | |
922 | ||
919ba009 VD |
923 | dane->mdpth = -1; |
924 | dane->pdpth = -1; | |
925 | dane->dctx = &s->ctx->dane; | |
926 | dane->trecs = sk_danetls_record_new_null(); | |
927 | ||
928 | if (dane->trecs == NULL) { | |
929 | SSLerr(SSL_F_SSL_DANE_ENABLE, ERR_R_MALLOC_FAILURE); | |
930 | return -1; | |
931 | } | |
932 | return 1; | |
933 | } | |
934 | ||
935 | int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki) | |
936 | { | |
937 | struct dane_st *dane = &s->dane; | |
938 | ||
c0a445a9 | 939 | if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK) |
919ba009 VD |
940 | return -1; |
941 | if (dane->mtlsa) { | |
942 | if (mcert) | |
943 | *mcert = dane->mcert; | |
944 | if (mspki) | |
945 | *mspki = (dane->mcert == NULL) ? dane->mtlsa->spki : NULL; | |
946 | } | |
947 | return dane->mdpth; | |
948 | } | |
949 | ||
950 | int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector, | |
951 | uint8_t *mtype, unsigned const char **data, size_t *dlen) | |
952 | { | |
953 | struct dane_st *dane = &s->dane; | |
954 | ||
c0a445a9 | 955 | if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK) |
919ba009 VD |
956 | return -1; |
957 | if (dane->mtlsa) { | |
958 | if (usage) | |
959 | *usage = dane->mtlsa->usage; | |
960 | if (selector) | |
961 | *selector = dane->mtlsa->selector; | |
962 | if (mtype) | |
963 | *mtype = dane->mtlsa->mtype; | |
964 | if (data) | |
965 | *data = dane->mtlsa->data; | |
966 | if (dlen) | |
967 | *dlen = dane->mtlsa->dlen; | |
968 | } | |
969 | return dane->mdpth; | |
970 | } | |
971 | ||
972 | struct dane_st *SSL_get0_dane(SSL *s) | |
973 | { | |
974 | return &s->dane; | |
975 | } | |
976 | ||
977 | int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector, | |
978 | uint8_t mtype, unsigned char *data, size_t dlen) | |
979 | { | |
980 | return dane_tlsa_add(&s->dane, usage, selector, mtype, data, dlen); | |
981 | } | |
982 | ||
983 | int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, uint8_t mtype, uint8_t ord) | |
984 | { | |
985 | return dane_mtype_set(&ctx->dane, md, mtype, ord); | |
986 | } | |
987 | ||
ccf11751 | 988 | int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) |
0f113f3e MC |
989 | { |
990 | return X509_VERIFY_PARAM_set1(ctx->param, vpm); | |
991 | } | |
ccf11751 DSH |
992 | |
993 | int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) | |
0f113f3e MC |
994 | { |
995 | return X509_VERIFY_PARAM_set1(ssl->param, vpm); | |
996 | } | |
ccf11751 | 997 | |
7af31968 | 998 | X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) |
0f113f3e MC |
999 | { |
1000 | return ctx->param; | |
1001 | } | |
7af31968 DSH |
1002 | |
1003 | X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) | |
0f113f3e MC |
1004 | { |
1005 | return ssl->param; | |
1006 | } | |
7af31968 | 1007 | |
a5ee80b9 | 1008 | void SSL_certs_clear(SSL *s) |
0f113f3e MC |
1009 | { |
1010 | ssl_cert_clear_certs(s->cert); | |
1011 | } | |
a5ee80b9 | 1012 | |
4f43d0e7 | 1013 | void SSL_free(SSL *s) |
0f113f3e MC |
1014 | { |
1015 | int i; | |
58964a49 | 1016 | |
0f113f3e MC |
1017 | if (s == NULL) |
1018 | return; | |
e03ddfae | 1019 | |
16203f7b | 1020 | CRYPTO_atomic_add(&s->references, -1, &i, s->lock); |
f3f1cf84 | 1021 | REF_PRINT_COUNT("SSL", s); |
0f113f3e MC |
1022 | if (i > 0) |
1023 | return; | |
f3f1cf84 | 1024 | REF_ASSERT_ISNT(i < 0); |
d02b48c6 | 1025 | |
222561fe | 1026 | X509_VERIFY_PARAM_free(s->param); |
919ba009 | 1027 | dane_final(&s->dane); |
0f113f3e MC |
1028 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); |
1029 | ||
1030 | if (s->bbio != NULL) { | |
1031 | /* If the buffering BIO is in place, pop it off */ | |
1032 | if (s->bbio == s->wbio) { | |
1033 | s->wbio = BIO_pop(s->wbio); | |
1034 | } | |
1035 | BIO_free(s->bbio); | |
1036 | s->bbio = NULL; | |
1037 | } | |
ca3a82c3 RS |
1038 | BIO_free_all(s->rbio); |
1039 | if (s->wbio != s->rbio) | |
0f113f3e MC |
1040 | BIO_free_all(s->wbio); |
1041 | ||
25aaa98a | 1042 | BUF_MEM_free(s->init_buf); |
0f113f3e MC |
1043 | |
1044 | /* add extra stuff */ | |
25aaa98a RS |
1045 | sk_SSL_CIPHER_free(s->cipher_list); |
1046 | sk_SSL_CIPHER_free(s->cipher_list_by_id); | |
0f113f3e MC |
1047 | |
1048 | /* Make the next call work :-) */ | |
1049 | if (s->session != NULL) { | |
1050 | ssl_clear_bad_session(s); | |
1051 | SSL_SESSION_free(s->session); | |
1052 | } | |
1053 | ||
d31fb0b5 | 1054 | clear_ciphers(s); |
d02b48c6 | 1055 | |
e0e920b1 | 1056 | ssl_cert_free(s->cert); |
0f113f3e | 1057 | /* Free up if allocated */ |
d02b48c6 | 1058 | |
b548a1f1 | 1059 | OPENSSL_free(s->tlsext_hostname); |
e0e920b1 | 1060 | SSL_CTX_free(s->initial_ctx); |
e481f9b9 | 1061 | #ifndef OPENSSL_NO_EC |
b548a1f1 RS |
1062 | OPENSSL_free(s->tlsext_ecpointformatlist); |
1063 | OPENSSL_free(s->tlsext_ellipticcurvelist); | |
e481f9b9 | 1064 | #endif /* OPENSSL_NO_EC */ |
222561fe | 1065 | sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free); |
25aaa98a | 1066 | sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); |
ed29e82a RP |
1067 | #ifndef OPENSSL_NO_CT |
1068 | SCT_LIST_free(s->scts); | |
1069 | OPENSSL_free(s->tlsext_scts); | |
1070 | #endif | |
b548a1f1 RS |
1071 | OPENSSL_free(s->tlsext_ocsp_resp); |
1072 | OPENSSL_free(s->alpn_client_proto_list); | |
0f113f3e | 1073 | |
222561fe | 1074 | sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free); |
0f113f3e | 1075 | |
696178ed DSH |
1076 | sk_X509_pop_free(s->verified_chain, X509_free); |
1077 | ||
0f113f3e MC |
1078 | if (s->method != NULL) |
1079 | s->method->ssl_free(s); | |
1080 | ||
f161995e | 1081 | RECORD_LAYER_release(&s->rlayer); |
33d23b87 | 1082 | |
e0e920b1 | 1083 | SSL_CTX_free(s->ctx); |
7c3908dd | 1084 | |
ff75a257 MC |
1085 | ASYNC_WAIT_CTX_free(s->waitctx); |
1086 | ||
e481f9b9 | 1087 | #if !defined(OPENSSL_NO_NEXTPROTONEG) |
b548a1f1 | 1088 | OPENSSL_free(s->next_proto_negotiated); |
ee2ffc27 BL |
1089 | #endif |
1090 | ||
e783bae2 | 1091 | #ifndef OPENSSL_NO_SRTP |
25aaa98a | 1092 | sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles); |
0f113f3e MC |
1093 | #endif |
1094 | ||
16203f7b AG |
1095 | CRYPTO_THREAD_lock_free(s->lock); |
1096 | ||
0f113f3e MC |
1097 | OPENSSL_free(s); |
1098 | } | |
1099 | ||
3ffbe008 MC |
1100 | void SSL_set_rbio(SSL *s, BIO *rbio) |
1101 | { | |
ca3a82c3 | 1102 | if (s->rbio != rbio) |
3ffbe008 MC |
1103 | BIO_free_all(s->rbio); |
1104 | s->rbio = rbio; | |
1105 | } | |
1106 | ||
1107 | void SSL_set_wbio(SSL *s, BIO *wbio) | |
0f113f3e MC |
1108 | { |
1109 | /* | |
1110 | * If the output buffering BIO is still in place, remove it | |
1111 | */ | |
1112 | if (s->bbio != NULL) { | |
1113 | if (s->wbio == s->bbio) { | |
1114 | s->wbio = s->wbio->next_bio; | |
1115 | s->bbio->next_bio = NULL; | |
1116 | } | |
1117 | } | |
ca3a82c3 | 1118 | if (s->wbio != wbio && s->rbio != s->wbio) |
0f113f3e | 1119 | BIO_free_all(s->wbio); |
0f113f3e MC |
1120 | s->wbio = wbio; |
1121 | } | |
d02b48c6 | 1122 | |
3ffbe008 MC |
1123 | void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) |
1124 | { | |
1125 | SSL_set_wbio(s, wbio); | |
1126 | SSL_set_rbio(s, rbio); | |
1127 | } | |
1128 | ||
0821bcd4 | 1129 | BIO *SSL_get_rbio(const SSL *s) |
0f113f3e MC |
1130 | { |
1131 | return (s->rbio); | |
1132 | } | |
d02b48c6 | 1133 | |
0821bcd4 | 1134 | BIO *SSL_get_wbio(const SSL *s) |
0f113f3e MC |
1135 | { |
1136 | return (s->wbio); | |
1137 | } | |
d02b48c6 | 1138 | |
0821bcd4 | 1139 | int SSL_get_fd(const SSL *s) |
0f113f3e MC |
1140 | { |
1141 | return (SSL_get_rfd(s)); | |
1142 | } | |
24cbf3ef | 1143 | |
0821bcd4 | 1144 | int SSL_get_rfd(const SSL *s) |
0f113f3e MC |
1145 | { |
1146 | int ret = -1; | |
1147 | BIO *b, *r; | |
1148 | ||
1149 | b = SSL_get_rbio(s); | |
1150 | r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); | |
1151 | if (r != NULL) | |
1152 | BIO_get_fd(r, &ret); | |
1153 | return (ret); | |
1154 | } | |
d02b48c6 | 1155 | |
0821bcd4 | 1156 | int SSL_get_wfd(const SSL *s) |
0f113f3e MC |
1157 | { |
1158 | int ret = -1; | |
1159 | BIO *b, *r; | |
1160 | ||
1161 | b = SSL_get_wbio(s); | |
1162 | r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); | |
1163 | if (r != NULL) | |
1164 | BIO_get_fd(r, &ret); | |
1165 | return (ret); | |
1166 | } | |
24cbf3ef | 1167 | |
bc36ee62 | 1168 | #ifndef OPENSSL_NO_SOCK |
0f113f3e MC |
1169 | int SSL_set_fd(SSL *s, int fd) |
1170 | { | |
1171 | int ret = 0; | |
1172 | BIO *bio = NULL; | |
1173 | ||
1174 | bio = BIO_new(BIO_s_socket()); | |
1175 | ||
1176 | if (bio == NULL) { | |
1177 | SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB); | |
1178 | goto err; | |
1179 | } | |
1180 | BIO_set_fd(bio, fd, BIO_NOCLOSE); | |
1181 | SSL_set_bio(s, bio, bio); | |
1182 | ret = 1; | |
1183 | err: | |
1184 | return (ret); | |
1185 | } | |
d02b48c6 | 1186 | |
0f113f3e MC |
1187 | int SSL_set_wfd(SSL *s, int fd) |
1188 | { | |
1189 | int ret = 0; | |
1190 | BIO *bio = NULL; | |
1191 | ||
1192 | if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET) | |
1193 | || ((int)BIO_get_fd(s->rbio, NULL) != fd)) { | |
1194 | bio = BIO_new(BIO_s_socket()); | |
1195 | ||
1196 | if (bio == NULL) { | |
1197 | SSLerr(SSL_F_SSL_SET_WFD, ERR_R_BUF_LIB); | |
1198 | goto err; | |
1199 | } | |
1200 | BIO_set_fd(bio, fd, BIO_NOCLOSE); | |
1201 | SSL_set_bio(s, SSL_get_rbio(s), bio); | |
1202 | } else | |
1203 | SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s)); | |
1204 | ret = 1; | |
1205 | err: | |
1206 | return (ret); | |
1207 | } | |
1208 | ||
1209 | int SSL_set_rfd(SSL *s, int fd) | |
1210 | { | |
1211 | int ret = 0; | |
1212 | BIO *bio = NULL; | |
1213 | ||
1214 | if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET) | |
1215 | || ((int)BIO_get_fd(s->wbio, NULL) != fd)) { | |
1216 | bio = BIO_new(BIO_s_socket()); | |
1217 | ||
1218 | if (bio == NULL) { | |
1219 | SSLerr(SSL_F_SSL_SET_RFD, ERR_R_BUF_LIB); | |
1220 | goto err; | |
1221 | } | |
1222 | BIO_set_fd(bio, fd, BIO_NOCLOSE); | |
1223 | SSL_set_bio(s, bio, SSL_get_wbio(s)); | |
1224 | } else | |
1225 | SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s)); | |
1226 | ret = 1; | |
1227 | err: | |
1228 | return (ret); | |
1229 | } | |
1230 | #endif | |
ca03109c BM |
1231 | |
1232 | /* return length of latest Finished message we sent, copy to 'buf' */ | |
0821bcd4 | 1233 | size_t SSL_get_finished(const SSL *s, void *buf, size_t count) |
0f113f3e MC |
1234 | { |
1235 | size_t ret = 0; | |
1236 | ||
1237 | if (s->s3 != NULL) { | |
1238 | ret = s->s3->tmp.finish_md_len; | |
1239 | if (count > ret) | |
1240 | count = ret; | |
1241 | memcpy(buf, s->s3->tmp.finish_md, count); | |
1242 | } | |
1243 | return ret; | |
1244 | } | |
ca03109c BM |
1245 | |
1246 | /* return length of latest Finished message we expected, copy to 'buf' */ | |
0821bcd4 | 1247 | size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) |
0f113f3e MC |
1248 | { |
1249 | size_t ret = 0; | |
ca03109c | 1250 | |
0f113f3e MC |
1251 | if (s->s3 != NULL) { |
1252 | ret = s->s3->tmp.peer_finish_md_len; | |
1253 | if (count > ret) | |
1254 | count = ret; | |
1255 | memcpy(buf, s->s3->tmp.peer_finish_md, count); | |
1256 | } | |
1257 | return ret; | |
1258 | } | |
ca03109c | 1259 | |
0821bcd4 | 1260 | int SSL_get_verify_mode(const SSL *s) |
0f113f3e MC |
1261 | { |
1262 | return (s->verify_mode); | |
1263 | } | |
d02b48c6 | 1264 | |
0821bcd4 | 1265 | int SSL_get_verify_depth(const SSL *s) |
0f113f3e MC |
1266 | { |
1267 | return X509_VERIFY_PARAM_get_depth(s->param); | |
1268 | } | |
7f89714e | 1269 | |
0f113f3e MC |
1270 | int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) { |
1271 | return (s->verify_callback); | |
1272 | } | |
d02b48c6 | 1273 | |
0821bcd4 | 1274 | int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) |
0f113f3e MC |
1275 | { |
1276 | return (ctx->verify_mode); | |
1277 | } | |
d02b48c6 | 1278 | |
0821bcd4 | 1279 | int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) |
0f113f3e MC |
1280 | { |
1281 | return X509_VERIFY_PARAM_get_depth(ctx->param); | |
1282 | } | |
1283 | ||
1284 | int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) { | |
1285 | return (ctx->default_verify_callback); | |
1286 | } | |
1287 | ||
1288 | void SSL_set_verify(SSL *s, int mode, | |
1289 | int (*callback) (int ok, X509_STORE_CTX *ctx)) | |
1290 | { | |
1291 | s->verify_mode = mode; | |
1292 | if (callback != NULL) | |
1293 | s->verify_callback = callback; | |
1294 | } | |
1295 | ||
1296 | void SSL_set_verify_depth(SSL *s, int depth) | |
1297 | { | |
1298 | X509_VERIFY_PARAM_set_depth(s->param, depth); | |
1299 | } | |
1300 | ||
1301 | void SSL_set_read_ahead(SSL *s, int yes) | |
1302 | { | |
52e1d7b1 | 1303 | RECORD_LAYER_set_read_ahead(&s->rlayer, yes); |
0f113f3e | 1304 | } |
d02b48c6 | 1305 | |
0821bcd4 | 1306 | int SSL_get_read_ahead(const SSL *s) |
0f113f3e | 1307 | { |
52e1d7b1 | 1308 | return RECORD_LAYER_get_read_ahead(&s->rlayer); |
0f113f3e | 1309 | } |
d02b48c6 | 1310 | |
0821bcd4 | 1311 | int SSL_pending(const SSL *s) |
0f113f3e MC |
1312 | { |
1313 | /* | |
1314 | * SSL_pending cannot work properly if read-ahead is enabled | |
1315 | * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is | |
1316 | * impossible to fix since SSL_pending cannot report errors that may be | |
1317 | * observed while scanning the new data. (Note that SSL_pending() is | |
1318 | * often used as a boolean value, so we'd better not return -1.) | |
1319 | */ | |
1320 | return (s->method->ssl_pending(s)); | |
1321 | } | |
d02b48c6 | 1322 | |
49580f25 MC |
1323 | int SSL_has_pending(const SSL *s) |
1324 | { | |
1325 | /* | |
1326 | * Similar to SSL_pending() but returns a 1 to indicate that we have | |
1327 | * unprocessed data available or 0 otherwise (as opposed to the number of | |
1328 | * bytes available). Unlike SSL_pending() this will take into account | |
1329 | * read_ahead data. A 1 return simply indicates that we have unprocessed | |
1330 | * data. That data may not result in any application data, or we may fail | |
1331 | * to parse the records for some reason. | |
1332 | */ | |
1333 | if (SSL_pending(s)) | |
1334 | return 1; | |
1335 | ||
1336 | return RECORD_LAYER_read_pending(&s->rlayer); | |
1337 | } | |
1338 | ||
0821bcd4 | 1339 | X509 *SSL_get_peer_certificate(const SSL *s) |
0f113f3e MC |
1340 | { |
1341 | X509 *r; | |
d02b48c6 | 1342 | |
0f113f3e MC |
1343 | if ((s == NULL) || (s->session == NULL)) |
1344 | r = NULL; | |
1345 | else | |
1346 | r = s->session->peer; | |
d02b48c6 | 1347 | |
0f113f3e MC |
1348 | if (r == NULL) |
1349 | return (r); | |
d02b48c6 | 1350 | |
05f0fb9f | 1351 | X509_up_ref(r); |
0f113f3e MC |
1352 | |
1353 | return (r); | |
1354 | } | |
d02b48c6 | 1355 | |
0821bcd4 | 1356 | STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) |
0f113f3e MC |
1357 | { |
1358 | STACK_OF(X509) *r; | |
1359 | ||
c34b0f99 | 1360 | if ((s == NULL) || (s->session == NULL)) |
0f113f3e MC |
1361 | r = NULL; |
1362 | else | |
c34b0f99 | 1363 | r = s->session->peer_chain; |
0f113f3e MC |
1364 | |
1365 | /* | |
1366 | * If we are a client, cert_chain includes the peer's own certificate; if | |
1367 | * we are a server, it does not. | |
1368 | */ | |
1369 | ||
1370 | return (r); | |
1371 | } | |
1372 | ||
1373 | /* | |
1374 | * Now in theory, since the calling process own 't' it should be safe to | |
1375 | * modify. We need to be able to read f without being hassled | |
1376 | */ | |
17dd65e6 | 1377 | int SSL_copy_session_id(SSL *t, const SSL *f) |
0f113f3e | 1378 | { |
16203f7b | 1379 | int i; |
0f113f3e | 1380 | /* Do we need to to SSL locking? */ |
61986d32 | 1381 | if (!SSL_set_session(t, SSL_get_session(f))) { |
17dd65e6 | 1382 | return 0; |
69f68237 | 1383 | } |
0f113f3e MC |
1384 | |
1385 | /* | |
87d9cafa | 1386 | * what if we are setup for one protocol version but want to talk another |
0f113f3e MC |
1387 | */ |
1388 | if (t->method != f->method) { | |
919ba009 VD |
1389 | t->method->ssl_free(t); |
1390 | t->method = f->method; | |
1391 | if (t->method->ssl_new(t) == 0) | |
1392 | return 0; | |
0f113f3e MC |
1393 | } |
1394 | ||
16203f7b | 1395 | CRYPTO_atomic_add(&f->cert->references, 1, &i, f->cert->lock); |
24a0d393 KR |
1396 | ssl_cert_free(t->cert); |
1397 | t->cert = f->cert; | |
61986d32 | 1398 | if (!SSL_set_session_id_context(t, f->sid_ctx, f->sid_ctx_length)) { |
17dd65e6 | 1399 | return 0; |
69f68237 | 1400 | } |
17dd65e6 MC |
1401 | |
1402 | return 1; | |
0f113f3e | 1403 | } |
d02b48c6 | 1404 | |
58964a49 | 1405 | /* Fix this so it checks all the valid key/cert options */ |
0821bcd4 | 1406 | int SSL_CTX_check_private_key(const SSL_CTX *ctx) |
0f113f3e MC |
1407 | { |
1408 | if ((ctx == NULL) || | |
24a0d393 | 1409 | (ctx->cert->key->x509 == NULL)) { |
0f113f3e MC |
1410 | SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, |
1411 | SSL_R_NO_CERTIFICATE_ASSIGNED); | |
1412 | return (0); | |
1413 | } | |
1414 | if (ctx->cert->key->privatekey == NULL) { | |
1415 | SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, | |
1416 | SSL_R_NO_PRIVATE_KEY_ASSIGNED); | |
1417 | return (0); | |
1418 | } | |
1419 | return (X509_check_private_key | |
1420 | (ctx->cert->key->x509, ctx->cert->key->privatekey)); | |
1421 | } | |
d02b48c6 | 1422 | |
58964a49 | 1423 | /* Fix this function so that it takes an optional type parameter */ |
0821bcd4 | 1424 | int SSL_check_private_key(const SSL *ssl) |
0f113f3e MC |
1425 | { |
1426 | if (ssl == NULL) { | |
1427 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, ERR_R_PASSED_NULL_PARAMETER); | |
1428 | return (0); | |
1429 | } | |
0f113f3e MC |
1430 | if (ssl->cert->key->x509 == NULL) { |
1431 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED); | |
1432 | return (0); | |
1433 | } | |
1434 | if (ssl->cert->key->privatekey == NULL) { | |
1435 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED); | |
1436 | return (0); | |
1437 | } | |
1438 | return (X509_check_private_key(ssl->cert->key->x509, | |
1439 | ssl->cert->key->privatekey)); | |
1440 | } | |
d02b48c6 | 1441 | |
07bbc92c MC |
1442 | int SSL_waiting_for_async(SSL *s) |
1443 | { | |
82676094 MC |
1444 | if(s->job) |
1445 | return 1; | |
1446 | ||
07bbc92c MC |
1447 | return 0; |
1448 | } | |
1449 | ||
ff75a257 | 1450 | int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds) |
f4da39d2 | 1451 | { |
ff75a257 MC |
1452 | ASYNC_WAIT_CTX *ctx = s->waitctx; |
1453 | ||
1454 | if (ctx == NULL) | |
1455 | return 0; | |
1456 | return ASYNC_WAIT_CTX_get_all_fds(ctx, fds, numfds); | |
1457 | } | |
f4da39d2 | 1458 | |
ff75a257 MC |
1459 | int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, size_t *numaddfds, |
1460 | OSSL_ASYNC_FD *delfd, size_t *numdelfds) | |
1461 | { | |
1462 | ASYNC_WAIT_CTX *ctx = s->waitctx; | |
1463 | ||
1464 | if (ctx == NULL) | |
1465 | return 0; | |
1466 | return ASYNC_WAIT_CTX_get_changed_fds(ctx, addfd, numaddfds, delfd, | |
1467 | numdelfds); | |
f4da39d2 MC |
1468 | } |
1469 | ||
4f43d0e7 | 1470 | int SSL_accept(SSL *s) |
0f113f3e | 1471 | { |
c4c32155 | 1472 | if (s->handshake_func == NULL) { |
0f113f3e MC |
1473 | /* Not properly initialized yet */ |
1474 | SSL_set_accept_state(s); | |
07bbc92c | 1475 | } |
add2f5ca MC |
1476 | |
1477 | return SSL_do_handshake(s); | |
0f113f3e | 1478 | } |
d02b48c6 | 1479 | |
4f43d0e7 | 1480 | int SSL_connect(SSL *s) |
0f113f3e | 1481 | { |
c4c32155 | 1482 | if (s->handshake_func == NULL) { |
0f113f3e MC |
1483 | /* Not properly initialized yet */ |
1484 | SSL_set_connect_state(s); | |
add2f5ca | 1485 | } |
b31b04d9 | 1486 | |
add2f5ca | 1487 | return SSL_do_handshake(s); |
0f113f3e | 1488 | } |
d02b48c6 | 1489 | |
0821bcd4 | 1490 | long SSL_get_default_timeout(const SSL *s) |
0f113f3e MC |
1491 | { |
1492 | return (s->method->get_timeout()); | |
1493 | } | |
1494 | ||
7fecbf6f | 1495 | static int ssl_start_async_job(SSL *s, struct ssl_async_args *args, |
add2f5ca MC |
1496 | int (*func)(void *)) { |
1497 | int ret; | |
ff75a257 MC |
1498 | if (s->waitctx == NULL) { |
1499 | s->waitctx = ASYNC_WAIT_CTX_new(); | |
1500 | if (s->waitctx == NULL) | |
1501 | return -1; | |
1502 | } | |
1503 | switch(ASYNC_start_job(&s->job, s->waitctx, &ret, func, args, | |
add2f5ca MC |
1504 | sizeof(struct ssl_async_args))) { |
1505 | case ASYNC_ERR: | |
1506 | s->rwstate = SSL_NOTHING; | |
7fecbf6f | 1507 | SSLerr(SSL_F_SSL_START_ASYNC_JOB, SSL_R_FAILED_TO_INIT_ASYNC); |
add2f5ca MC |
1508 | return -1; |
1509 | case ASYNC_PAUSE: | |
1510 | s->rwstate = SSL_ASYNC_PAUSED; | |
1511 | return -1; | |
1512 | case ASYNC_FINISH: | |
1513 | s->job = NULL; | |
1514 | return ret; | |
1515 | default: | |
1516 | s->rwstate = SSL_NOTHING; | |
7fecbf6f | 1517 | SSLerr(SSL_F_SSL_START_ASYNC_JOB, ERR_R_INTERNAL_ERROR); |
add2f5ca MC |
1518 | /* Shouldn't happen */ |
1519 | return -1; | |
1520 | } | |
1521 | } | |
07bbc92c | 1522 | |
add2f5ca | 1523 | static int ssl_io_intern(void *vargs) |
07bbc92c MC |
1524 | { |
1525 | struct ssl_async_args *args; | |
1526 | SSL *s; | |
1527 | void *buf; | |
1528 | int num; | |
1529 | ||
1530 | args = (struct ssl_async_args *)vargs; | |
1531 | s = args->s; | |
1532 | buf = args->buf; | |
1533 | num = args->num; | |
ec447924 MC |
1534 | switch (args->type) { |
1535 | case READFUNC: | |
1536 | return args->f.func_read(s, buf, num); | |
1537 | case WRITEFUNC: | |
1538 | return args->f.func_write(s, buf, num); | |
1539 | case OTHERFUNC: | |
1540 | return args->f.func_other(s); | |
1541 | } | |
1542 | return -1; | |
07bbc92c MC |
1543 | } |
1544 | ||
0f113f3e MC |
1545 | int SSL_read(SSL *s, void *buf, int num) |
1546 | { | |
c4c32155 | 1547 | if (s->handshake_func == NULL) { |
0f113f3e MC |
1548 | SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED); |
1549 | return -1; | |
1550 | } | |
1551 | ||
1552 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { | |
1553 | s->rwstate = SSL_NOTHING; | |
1554 | return (0); | |
1555 | } | |
07bbc92c | 1556 | |
44a27ac2 | 1557 | if((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) { |
add2f5ca MC |
1558 | struct ssl_async_args args; |
1559 | ||
1560 | args.s = s; | |
1561 | args.buf = buf; | |
1562 | args.num = num; | |
ec447924 MC |
1563 | args.type = READFUNC; |
1564 | args.f.func_read = s->method->ssl_read; | |
add2f5ca | 1565 | |
7fecbf6f | 1566 | return ssl_start_async_job(s, &args, ssl_io_intern); |
07bbc92c MC |
1567 | } else { |
1568 | return s->method->ssl_read(s, buf, num); | |
1569 | } | |
0f113f3e MC |
1570 | } |
1571 | ||
1572 | int SSL_peek(SSL *s, void *buf, int num) | |
1573 | { | |
c4c32155 | 1574 | if (s->handshake_func == NULL) { |
0f113f3e MC |
1575 | SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED); |
1576 | return -1; | |
1577 | } | |
1578 | ||
1579 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { | |
1580 | return (0); | |
1581 | } | |
add2f5ca MC |
1582 | if((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) { |
1583 | struct ssl_async_args args; | |
0f113f3e | 1584 | |
add2f5ca MC |
1585 | args.s = s; |
1586 | args.buf = buf; | |
1587 | args.num = num; | |
ec447924 MC |
1588 | args.type = READFUNC; |
1589 | args.f.func_read = s->method->ssl_peek; | |
07bbc92c | 1590 | |
7fecbf6f | 1591 | return ssl_start_async_job(s, &args, ssl_io_intern); |
add2f5ca MC |
1592 | } else { |
1593 | return s->method->ssl_peek(s, buf, num); | |
1594 | } | |
07bbc92c MC |
1595 | } |
1596 | ||
0f113f3e MC |
1597 | int SSL_write(SSL *s, const void *buf, int num) |
1598 | { | |
c4c32155 | 1599 | if (s->handshake_func == NULL) { |
0f113f3e MC |
1600 | SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED); |
1601 | return -1; | |
1602 | } | |
1603 | ||
1604 | if (s->shutdown & SSL_SENT_SHUTDOWN) { | |
1605 | s->rwstate = SSL_NOTHING; | |
1606 | SSLerr(SSL_F_SSL_WRITE, SSL_R_PROTOCOL_IS_SHUTDOWN); | |
1607 | return (-1); | |
1608 | } | |
07bbc92c | 1609 | |
44a27ac2 | 1610 | if((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) { |
add2f5ca MC |
1611 | struct ssl_async_args args; |
1612 | ||
1613 | args.s = s; | |
1614 | args.buf = (void *)buf; | |
1615 | args.num = num; | |
ec447924 MC |
1616 | args.type = WRITEFUNC; |
1617 | args.f.func_write = s->method->ssl_write; | |
add2f5ca | 1618 | |
7fecbf6f | 1619 | return ssl_start_async_job(s, &args, ssl_io_intern); |
07bbc92c MC |
1620 | } else { |
1621 | return s->method->ssl_write(s, buf, num); | |
1622 | } | |
0f113f3e | 1623 | } |
d02b48c6 | 1624 | |
4f43d0e7 | 1625 | int SSL_shutdown(SSL *s) |
0f113f3e MC |
1626 | { |
1627 | /* | |
1628 | * Note that this function behaves differently from what one might | |
1629 | * expect. Return values are 0 for no success (yet), 1 for success; but | |
1630 | * calling it once is usually not enough, even if blocking I/O is used | |
1631 | * (see ssl3_shutdown). | |
1632 | */ | |
1633 | ||
c4c32155 | 1634 | if (s->handshake_func == NULL) { |
0f113f3e MC |
1635 | SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED); |
1636 | return -1; | |
1637 | } | |
1638 | ||
64f9f406 MC |
1639 | if (!SSL_in_init(s)) { |
1640 | if((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) { | |
1641 | struct ssl_async_args args; | |
ec447924 | 1642 | |
64f9f406 MC |
1643 | args.s = s; |
1644 | args.type = OTHERFUNC; | |
1645 | args.f.func_other = s->method->ssl_shutdown; | |
ec447924 | 1646 | |
64f9f406 MC |
1647 | return ssl_start_async_job(s, &args, ssl_io_intern); |
1648 | } else { | |
1649 | return s->method->ssl_shutdown(s); | |
1650 | } | |
ec447924 | 1651 | } else { |
64f9f406 MC |
1652 | SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_SHUTDOWN_WHILE_IN_INIT); |
1653 | return -1; | |
ec447924 | 1654 | } |
0f113f3e | 1655 | } |
d02b48c6 | 1656 | |
4f43d0e7 | 1657 | int SSL_renegotiate(SSL *s) |
0f113f3e MC |
1658 | { |
1659 | if (s->renegotiate == 0) | |
1660 | s->renegotiate = 1; | |
44959ee4 | 1661 | |
0f113f3e | 1662 | s->new_session = 1; |
44959ee4 | 1663 | |
0f113f3e MC |
1664 | return (s->method->ssl_renegotiate(s)); |
1665 | } | |
d02b48c6 | 1666 | |
44959ee4 | 1667 | int SSL_renegotiate_abbreviated(SSL *s) |
0f113f3e MC |
1668 | { |
1669 | if (s->renegotiate == 0) | |
1670 | s->renegotiate = 1; | |
c519e89f | 1671 | |
0f113f3e | 1672 | s->new_session = 0; |
c519e89f | 1673 | |
0f113f3e MC |
1674 | return (s->method->ssl_renegotiate(s)); |
1675 | } | |
44959ee4 | 1676 | |
6b0e9fac | 1677 | int SSL_renegotiate_pending(SSL *s) |
0f113f3e MC |
1678 | { |
1679 | /* | |
1680 | * becomes true when negotiation is requested; false again once a | |
1681 | * handshake has finished | |
1682 | */ | |
1683 | return (s->renegotiate != 0); | |
1684 | } | |
1685 | ||
1686 | long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) | |
1687 | { | |
1688 | long l; | |
1689 | ||
1690 | switch (cmd) { | |
1691 | case SSL_CTRL_GET_READ_AHEAD: | |
52e1d7b1 | 1692 | return (RECORD_LAYER_get_read_ahead(&s->rlayer)); |
0f113f3e | 1693 | case SSL_CTRL_SET_READ_AHEAD: |
52e1d7b1 MC |
1694 | l = RECORD_LAYER_get_read_ahead(&s->rlayer); |
1695 | RECORD_LAYER_set_read_ahead(&s->rlayer, larg); | |
0f113f3e MC |
1696 | return (l); |
1697 | ||
1698 | case SSL_CTRL_SET_MSG_CALLBACK_ARG: | |
1699 | s->msg_callback_arg = parg; | |
1700 | return 1; | |
1701 | ||
0f113f3e MC |
1702 | case SSL_CTRL_MODE: |
1703 | return (s->mode |= larg); | |
1704 | case SSL_CTRL_CLEAR_MODE: | |
1705 | return (s->mode &= ~larg); | |
1706 | case SSL_CTRL_GET_MAX_CERT_LIST: | |
1707 | return (s->max_cert_list); | |
1708 | case SSL_CTRL_SET_MAX_CERT_LIST: | |
1709 | l = s->max_cert_list; | |
1710 | s->max_cert_list = larg; | |
1711 | return (l); | |
1712 | case SSL_CTRL_SET_MAX_SEND_FRAGMENT: | |
1713 | if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) | |
1714 | return 0; | |
1715 | s->max_send_fragment = larg; | |
d102d9df MC |
1716 | if (s->max_send_fragment < s->split_send_fragment) |
1717 | s->split_send_fragment = s->max_send_fragment; | |
1718 | return 1; | |
1719 | case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT: | |
6b99e875 | 1720 | if ((unsigned int)larg > s->max_send_fragment || larg == 0) |
d102d9df MC |
1721 | return 0; |
1722 | s->split_send_fragment = larg; | |
0f113f3e | 1723 | return 1; |
d102d9df MC |
1724 | case SSL_CTRL_SET_MAX_PIPELINES: |
1725 | if (larg < 1 || larg > SSL_MAX_PIPELINES) | |
1726 | return 0; | |
1727 | s->max_pipelines = larg; | |
94777c9c MC |
1728 | if (larg > 1) |
1729 | RECORD_LAYER_set_read_ahead(&s->rlayer, 1); | |
07077415 | 1730 | return 1; |
0f113f3e MC |
1731 | case SSL_CTRL_GET_RI_SUPPORT: |
1732 | if (s->s3) | |
1733 | return s->s3->send_connection_binding; | |
1734 | else | |
1735 | return 0; | |
1736 | case SSL_CTRL_CERT_FLAGS: | |
1737 | return (s->cert->cert_flags |= larg); | |
1738 | case SSL_CTRL_CLEAR_CERT_FLAGS: | |
1739 | return (s->cert->cert_flags &= ~larg); | |
1740 | ||
1741 | case SSL_CTRL_GET_RAW_CIPHERLIST: | |
1742 | if (parg) { | |
76106e60 | 1743 | if (s->s3->tmp.ciphers_raw == NULL) |
0f113f3e | 1744 | return 0; |
76106e60 DSH |
1745 | *(unsigned char **)parg = s->s3->tmp.ciphers_raw; |
1746 | return (int)s->s3->tmp.ciphers_rawlen; | |
e9fa092e EK |
1747 | } else { |
1748 | return TLS_CIPHER_LEN; | |
1749 | } | |
c5364614 | 1750 | case SSL_CTRL_GET_EXTMS_SUPPORT: |
024f543c | 1751 | if (!s->session || SSL_in_init(s) || ossl_statem_get_in_handshake(s)) |
156a8722 DSH |
1752 | return -1; |
1753 | if (s->session->flags & SSL_SESS_FLAG_EXTMS) | |
c5364614 DSH |
1754 | return 1; |
1755 | else | |
1756 | return 0; | |
7946ab33 | 1757 | case SSL_CTRL_SET_MIN_PROTO_VERSION: |
4fa52141 VD |
1758 | return ssl_set_version_bound(s->ctx->method->version, (int)larg, |
1759 | &s->min_proto_version); | |
7946ab33 | 1760 | case SSL_CTRL_SET_MAX_PROTO_VERSION: |
4fa52141 VD |
1761 | return ssl_set_version_bound(s->ctx->method->version, (int)larg, |
1762 | &s->max_proto_version); | |
0f113f3e MC |
1763 | default: |
1764 | return (s->method->ssl_ctrl(s, cmd, larg, parg)); | |
1765 | } | |
1766 | } | |
1767 | ||
1768 | long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void)) | |
1769 | { | |
1770 | switch (cmd) { | |
1771 | case SSL_CTRL_SET_MSG_CALLBACK: | |
1772 | s->msg_callback = (void (*) | |
1773 | (int write_p, int version, int content_type, | |
1774 | const void *buf, size_t len, SSL *ssl, | |
1775 | void *arg))(fp); | |
1776 | return 1; | |
1777 | ||
1778 | default: | |
1779 | return (s->method->ssl_callback_ctrl(s, cmd, fp)); | |
1780 | } | |
1781 | } | |
d3442bc7 | 1782 | |
3c1d6bbc | 1783 | LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) |
0f113f3e MC |
1784 | { |
1785 | return ctx->sessions; | |
1786 | } | |
1787 | ||
1788 | long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) | |
1789 | { | |
1790 | long l; | |
1791 | /* For some cases with ctx == NULL perform syntax checks */ | |
1792 | if (ctx == NULL) { | |
1793 | switch (cmd) { | |
14536c8c | 1794 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1795 | case SSL_CTRL_SET_CURVES_LIST: |
1796 | return tls1_set_curves_list(NULL, NULL, parg); | |
1797 | #endif | |
1798 | case SSL_CTRL_SET_SIGALGS_LIST: | |
1799 | case SSL_CTRL_SET_CLIENT_SIGALGS_LIST: | |
1800 | return tls1_set_sigalgs_list(NULL, parg, 0); | |
1801 | default: | |
1802 | return 0; | |
1803 | } | |
1804 | } | |
1805 | ||
1806 | switch (cmd) { | |
1807 | case SSL_CTRL_GET_READ_AHEAD: | |
1808 | return (ctx->read_ahead); | |
1809 | case SSL_CTRL_SET_READ_AHEAD: | |
1810 | l = ctx->read_ahead; | |
1811 | ctx->read_ahead = larg; | |
1812 | return (l); | |
1813 | ||
1814 | case SSL_CTRL_SET_MSG_CALLBACK_ARG: | |
1815 | ctx->msg_callback_arg = parg; | |
1816 | return 1; | |
1817 | ||
1818 | case SSL_CTRL_GET_MAX_CERT_LIST: | |
1819 | return (ctx->max_cert_list); | |
1820 | case SSL_CTRL_SET_MAX_CERT_LIST: | |
1821 | l = ctx->max_cert_list; | |
1822 | ctx->max_cert_list = larg; | |
1823 | return (l); | |
1824 | ||
1825 | case SSL_CTRL_SET_SESS_CACHE_SIZE: | |
1826 | l = ctx->session_cache_size; | |
1827 | ctx->session_cache_size = larg; | |
1828 | return (l); | |
1829 | case SSL_CTRL_GET_SESS_CACHE_SIZE: | |
1830 | return (ctx->session_cache_size); | |
1831 | case SSL_CTRL_SET_SESS_CACHE_MODE: | |
1832 | l = ctx->session_cache_mode; | |
1833 | ctx->session_cache_mode = larg; | |
1834 | return (l); | |
1835 | case SSL_CTRL_GET_SESS_CACHE_MODE: | |
1836 | return (ctx->session_cache_mode); | |
1837 | ||
1838 | case SSL_CTRL_SESS_NUMBER: | |
1839 | return (lh_SSL_SESSION_num_items(ctx->sessions)); | |
1840 | case SSL_CTRL_SESS_CONNECT: | |
1841 | return (ctx->stats.sess_connect); | |
1842 | case SSL_CTRL_SESS_CONNECT_GOOD: | |
1843 | return (ctx->stats.sess_connect_good); | |
1844 | case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: | |
1845 | return (ctx->stats.sess_connect_renegotiate); | |
1846 | case SSL_CTRL_SESS_ACCEPT: | |
1847 | return (ctx->stats.sess_accept); | |
1848 | case SSL_CTRL_SESS_ACCEPT_GOOD: | |
1849 | return (ctx->stats.sess_accept_good); | |
1850 | case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: | |
1851 | return (ctx->stats.sess_accept_renegotiate); | |
1852 | case SSL_CTRL_SESS_HIT: | |
1853 | return (ctx->stats.sess_hit); | |
1854 | case SSL_CTRL_SESS_CB_HIT: | |
1855 | return (ctx->stats.sess_cb_hit); | |
1856 | case SSL_CTRL_SESS_MISSES: | |
1857 | return (ctx->stats.sess_miss); | |
1858 | case SSL_CTRL_SESS_TIMEOUTS: | |
1859 | return (ctx->stats.sess_timeout); | |
1860 | case SSL_CTRL_SESS_CACHE_FULL: | |
1861 | return (ctx->stats.sess_cache_full); | |
0f113f3e MC |
1862 | case SSL_CTRL_MODE: |
1863 | return (ctx->mode |= larg); | |
1864 | case SSL_CTRL_CLEAR_MODE: | |
1865 | return (ctx->mode &= ~larg); | |
1866 | case SSL_CTRL_SET_MAX_SEND_FRAGMENT: | |
1867 | if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) | |
1868 | return 0; | |
1869 | ctx->max_send_fragment = larg; | |
d102d9df MC |
1870 | if (ctx->max_send_fragment < ctx->split_send_fragment) |
1871 | ctx->split_send_fragment = ctx->split_send_fragment; | |
0f113f3e | 1872 | return 1; |
d102d9df | 1873 | case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT: |
6b99e875 | 1874 | if ((unsigned int)larg > ctx->max_send_fragment || larg == 0) |
d102d9df MC |
1875 | return 0; |
1876 | ctx->split_send_fragment = larg; | |
1877 | return 1; | |
1878 | case SSL_CTRL_SET_MAX_PIPELINES: | |
1879 | if (larg < 1 || larg > SSL_MAX_PIPELINES) | |
1880 | return 0; | |
1881 | ctx->max_pipelines = larg; | |
07077415 | 1882 | return 1; |
0f113f3e MC |
1883 | case SSL_CTRL_CERT_FLAGS: |
1884 | return (ctx->cert->cert_flags |= larg); | |
1885 | case SSL_CTRL_CLEAR_CERT_FLAGS: | |
1886 | return (ctx->cert->cert_flags &= ~larg); | |
7946ab33 | 1887 | case SSL_CTRL_SET_MIN_PROTO_VERSION: |
4fa52141 VD |
1888 | return ssl_set_version_bound(ctx->method->version, (int)larg, |
1889 | &ctx->min_proto_version); | |
7946ab33 | 1890 | case SSL_CTRL_SET_MAX_PROTO_VERSION: |
4fa52141 VD |
1891 | return ssl_set_version_bound(ctx->method->version, (int)larg, |
1892 | &ctx->max_proto_version); | |
0f113f3e MC |
1893 | default: |
1894 | return (ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg)); | |
1895 | } | |
1896 | } | |
1897 | ||
1898 | long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void)) | |
1899 | { | |
1900 | switch (cmd) { | |
1901 | case SSL_CTRL_SET_MSG_CALLBACK: | |
1902 | ctx->msg_callback = (void (*) | |
1903 | (int write_p, int version, int content_type, | |
1904 | const void *buf, size_t len, SSL *ssl, | |
1905 | void *arg))(fp); | |
1906 | return 1; | |
1907 | ||
1908 | default: | |
1909 | return (ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp)); | |
1910 | } | |
1911 | } | |
d3442bc7 | 1912 | |
ccd86b68 | 1913 | int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b) |
0f113f3e | 1914 | { |
90d9e49a DSH |
1915 | if (a->id > b->id) |
1916 | return 1; | |
1917 | if (a->id < b->id) | |
1918 | return -1; | |
1919 | return 0; | |
0f113f3e MC |
1920 | } |
1921 | ||
1922 | int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap, | |
1923 | const SSL_CIPHER *const *bp) | |
1924 | { | |
90d9e49a DSH |
1925 | if ((*ap)->id > (*bp)->id) |
1926 | return 1; | |
1927 | if ((*ap)->id < (*bp)->id) | |
1928 | return -1; | |
1929 | return 0; | |
0f113f3e | 1930 | } |
d02b48c6 | 1931 | |
4f43d0e7 | 1932 | /** return a STACK of the ciphers available for the SSL and in order of |
d02b48c6 | 1933 | * preference */ |
0821bcd4 | 1934 | STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) |
0f113f3e MC |
1935 | { |
1936 | if (s != NULL) { | |
1937 | if (s->cipher_list != NULL) { | |
1938 | return (s->cipher_list); | |
1939 | } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) { | |
1940 | return (s->ctx->cipher_list); | |
1941 | } | |
1942 | } | |
1943 | return (NULL); | |
1944 | } | |
1945 | ||
831eef2c NM |
1946 | STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s) |
1947 | { | |
1948 | if ((s == NULL) || (s->session == NULL) || !s->server) | |
1949 | return NULL; | |
1950 | return s->session->ciphers; | |
1951 | } | |
1952 | ||
8b8e5bed | 1953 | STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s) |
0f113f3e MC |
1954 | { |
1955 | STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers; | |
1956 | int i; | |
1957 | ciphers = SSL_get_ciphers(s); | |
1958 | if (!ciphers) | |
1959 | return NULL; | |
1960 | ssl_set_client_disabled(s); | |
1961 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { | |
1962 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i); | |
1963 | if (!ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED)) { | |
1964 | if (!sk) | |
1965 | sk = sk_SSL_CIPHER_new_null(); | |
1966 | if (!sk) | |
1967 | return NULL; | |
1968 | if (!sk_SSL_CIPHER_push(sk, c)) { | |
1969 | sk_SSL_CIPHER_free(sk); | |
1970 | return NULL; | |
1971 | } | |
1972 | } | |
1973 | } | |
1974 | return sk; | |
1975 | } | |
8b8e5bed | 1976 | |
4f43d0e7 | 1977 | /** return a STACK of the ciphers available for the SSL and in order of |
d02b48c6 | 1978 | * algorithm id */ |
f73e07cf | 1979 | STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) |
0f113f3e MC |
1980 | { |
1981 | if (s != NULL) { | |
1982 | if (s->cipher_list_by_id != NULL) { | |
1983 | return (s->cipher_list_by_id); | |
1984 | } else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) { | |
1985 | return (s->ctx->cipher_list_by_id); | |
1986 | } | |
1987 | } | |
1988 | return (NULL); | |
1989 | } | |
d02b48c6 | 1990 | |
4f43d0e7 | 1991 | /** The old interface to get the same thing as SSL_get_ciphers() */ |
0f113f3e MC |
1992 | const char *SSL_get_cipher_list(const SSL *s, int n) |
1993 | { | |
4a640fb6 | 1994 | const SSL_CIPHER *c; |
0f113f3e MC |
1995 | STACK_OF(SSL_CIPHER) *sk; |
1996 | ||
1997 | if (s == NULL) | |
1998 | return (NULL); | |
1999 | sk = SSL_get_ciphers(s); | |
2000 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n)) | |
2001 | return (NULL); | |
2002 | c = sk_SSL_CIPHER_value(sk, n); | |
2003 | if (c == NULL) | |
2004 | return (NULL); | |
2005 | return (c->name); | |
2006 | } | |
d02b48c6 | 2007 | |
25f923dd | 2008 | /** specify the ciphers to be used by default by the SSL_CTX */ |
018e57c7 | 2009 | int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) |
0f113f3e MC |
2010 | { |
2011 | STACK_OF(SSL_CIPHER) *sk; | |
2012 | ||
2013 | sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list, | |
2014 | &ctx->cipher_list_by_id, str, ctx->cert); | |
2015 | /* | |
2016 | * ssl_create_cipher_list may return an empty stack if it was unable to | |
2017 | * find a cipher matching the given rule string (for example if the rule | |
2018 | * string specifies a cipher which has been disabled). This is not an | |
2019 | * error as far as ssl_create_cipher_list is concerned, and hence | |
2020 | * ctx->cipher_list and ctx->cipher_list_by_id has been updated. | |
2021 | */ | |
2022 | if (sk == NULL) | |
2023 | return 0; | |
2024 | else if (sk_SSL_CIPHER_num(sk) == 0) { | |
2025 | SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); | |
2026 | return 0; | |
2027 | } | |
2028 | return 1; | |
2029 | } | |
d02b48c6 | 2030 | |
4f43d0e7 | 2031 | /** specify the ciphers to be used by the SSL */ |
0f113f3e MC |
2032 | int SSL_set_cipher_list(SSL *s, const char *str) |
2033 | { | |
2034 | STACK_OF(SSL_CIPHER) *sk; | |
2035 | ||
2036 | sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list, | |
2037 | &s->cipher_list_by_id, str, s->cert); | |
2038 | /* see comment in SSL_CTX_set_cipher_list */ | |
2039 | if (sk == NULL) | |
2040 | return 0; | |
2041 | else if (sk_SSL_CIPHER_num(sk) == 0) { | |
2042 | SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); | |
2043 | return 0; | |
2044 | } | |
2045 | return 1; | |
2046 | } | |
d02b48c6 | 2047 | |
0f113f3e MC |
2048 | char *SSL_get_shared_ciphers(const SSL *s, char *buf, int len) |
2049 | { | |
2050 | char *p; | |
2051 | STACK_OF(SSL_CIPHER) *sk; | |
4a640fb6 | 2052 | const SSL_CIPHER *c; |
0f113f3e MC |
2053 | int i; |
2054 | ||
2055 | if ((s->session == NULL) || (s->session->ciphers == NULL) || (len < 2)) | |
2056 | return (NULL); | |
2057 | ||
2058 | p = buf; | |
2059 | sk = s->session->ciphers; | |
2060 | ||
2061 | if (sk_SSL_CIPHER_num(sk) == 0) | |
2062 | return NULL; | |
2063 | ||
2064 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { | |
2065 | int n; | |
2066 | ||
2067 | c = sk_SSL_CIPHER_value(sk, i); | |
2068 | n = strlen(c->name); | |
2069 | if (n + 1 > len) { | |
2070 | if (p != buf) | |
2071 | --p; | |
2072 | *p = '\0'; | |
2073 | return buf; | |
2074 | } | |
a89c9a0d | 2075 | memcpy(p, c->name, n + 1); |
0f113f3e MC |
2076 | p += n; |
2077 | *(p++) = ':'; | |
2078 | len -= n + 1; | |
2079 | } | |
2080 | p[-1] = '\0'; | |
2081 | return (buf); | |
2082 | } | |
2083 | ||
52b8dad8 | 2084 | /** return a servername extension value if provided in Client Hello, or NULL. |
f1fd4544 | 2085 | * So far, only host_name types are defined (RFC 3546). |
ed3883d2 BM |
2086 | */ |
2087 | ||
f1fd4544 | 2088 | const char *SSL_get_servername(const SSL *s, const int type) |
0f113f3e MC |
2089 | { |
2090 | if (type != TLSEXT_NAMETYPE_host_name) | |
2091 | return NULL; | |
a13c20f6 | 2092 | |
0f113f3e MC |
2093 | return s->session && !s->tlsext_hostname ? |
2094 | s->session->tlsext_hostname : s->tlsext_hostname; | |
2095 | } | |
ed3883d2 | 2096 | |
f1fd4544 | 2097 | int SSL_get_servername_type(const SSL *s) |
0f113f3e MC |
2098 | { |
2099 | if (s->session | |
2100 | && (!s->tlsext_hostname ? s->session-> | |
2101 | tlsext_hostname : s->tlsext_hostname)) | |
2102 | return TLSEXT_NAMETYPE_host_name; | |
2103 | return -1; | |
2104 | } | |
ee2ffc27 | 2105 | |
0f113f3e MC |
2106 | /* |
2107 | * SSL_select_next_proto implements the standard protocol selection. It is | |
ee2ffc27 | 2108 | * expected that this function is called from the callback set by |
0f113f3e MC |
2109 | * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a |
2110 | * vector of 8-bit, length prefixed byte strings. The length byte itself is | |
2111 | * not included in the length. A byte string of length 0 is invalid. No byte | |
2112 | * string may be truncated. The current, but experimental algorithm for | |
2113 | * selecting the protocol is: 1) If the server doesn't support NPN then this | |
2114 | * is indicated to the callback. In this case, the client application has to | |
2115 | * abort the connection or have a default application level protocol. 2) If | |
2116 | * the server supports NPN, but advertises an empty list then the client | |
2117 | * selects the first protcol in its list, but indicates via the API that this | |
2118 | * fallback case was enacted. 3) Otherwise, the client finds the first | |
2119 | * protocol in the server's list that it supports and selects this protocol. | |
2120 | * This is because it's assumed that the server has better information about | |
2121 | * which protocol a client should use. 4) If the client doesn't support any | |
2122 | * of the server's advertised protocols, then this is treated the same as | |
2123 | * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was | |
2124 | * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached. | |
ee2ffc27 | 2125 | */ |
0f113f3e MC |
2126 | int SSL_select_next_proto(unsigned char **out, unsigned char *outlen, |
2127 | const unsigned char *server, | |
2128 | unsigned int server_len, | |
2129 | const unsigned char *client, | |
2130 | unsigned int client_len) | |
2131 | { | |
2132 | unsigned int i, j; | |
2133 | const unsigned char *result; | |
2134 | int status = OPENSSL_NPN_UNSUPPORTED; | |
2135 | ||
2136 | /* | |
2137 | * For each protocol in server preference order, see if we support it. | |
2138 | */ | |
2139 | for (i = 0; i < server_len;) { | |
2140 | for (j = 0; j < client_len;) { | |
2141 | if (server[i] == client[j] && | |
2142 | memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) { | |
2143 | /* We found a match */ | |
2144 | result = &server[i]; | |
2145 | status = OPENSSL_NPN_NEGOTIATED; | |
2146 | goto found; | |
2147 | } | |
2148 | j += client[j]; | |
2149 | j++; | |
2150 | } | |
2151 | i += server[i]; | |
2152 | i++; | |
2153 | } | |
2154 | ||
2155 | /* There's no overlap between our protocols and the server's list. */ | |
2156 | result = client; | |
2157 | status = OPENSSL_NPN_NO_OVERLAP; | |
2158 | ||
2159 | found: | |
2160 | *out = (unsigned char *)result + 1; | |
2161 | *outlen = result[0]; | |
2162 | return status; | |
2163 | } | |
ee2ffc27 | 2164 | |
e481f9b9 | 2165 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
2166 | /* |
2167 | * SSL_get0_next_proto_negotiated sets *data and *len to point to the | |
2168 | * client's requested protocol for this connection and returns 0. If the | |
2169 | * client didn't request any protocol, then *data is set to NULL. Note that | |
2170 | * the client can request any protocol it chooses. The value returned from | |
2171 | * this function need not be a member of the list of supported protocols | |
ee2ffc27 BL |
2172 | * provided by the callback. |
2173 | */ | |
0f113f3e MC |
2174 | void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, |
2175 | unsigned *len) | |
2176 | { | |
2177 | *data = s->next_proto_negotiated; | |
2178 | if (!*data) { | |
2179 | *len = 0; | |
2180 | } else { | |
2181 | *len = s->next_proto_negotiated_len; | |
2182 | } | |
2183 | } | |
2184 | ||
2185 | /* | |
2186 | * SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when | |
2187 | * a TLS server needs a list of supported protocols for Next Protocol | |
2188 | * Negotiation. The returned list must be in wire format. The list is | |
2189 | * returned by setting |out| to point to it and |outlen| to its length. This | |
2190 | * memory will not be modified, but one should assume that the SSL* keeps a | |
2191 | * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it | |
2192 | * wishes to advertise. Otherwise, no such extension will be included in the | |
2193 | * ServerHello. | |
2194 | */ | |
2195 | void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, | |
2196 | int (*cb) (SSL *ssl, | |
2197 | const unsigned char | |
2198 | **out, | |
2199 | unsigned int *outlen, | |
2200 | void *arg), void *arg) | |
2201 | { | |
2202 | ctx->next_protos_advertised_cb = cb; | |
2203 | ctx->next_protos_advertised_cb_arg = arg; | |
2204 | } | |
2205 | ||
2206 | /* | |
2207 | * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a | |
ee2ffc27 BL |
2208 | * client needs to select a protocol from the server's provided list. |out| |
2209 | * must be set to point to the selected protocol (which may be within |in|). | |
0f113f3e MC |
2210 | * The length of the protocol name must be written into |outlen|. The |
2211 | * server's advertised protocols are provided in |in| and |inlen|. The | |
2212 | * callback can assume that |in| is syntactically valid. The client must | |
2213 | * select a protocol. It is fatal to the connection if this callback returns | |
2214 | * a value other than SSL_TLSEXT_ERR_OK. | |
ee2ffc27 | 2215 | */ |
0f113f3e MC |
2216 | void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, |
2217 | int (*cb) (SSL *s, unsigned char **out, | |
2218 | unsigned char *outlen, | |
2219 | const unsigned char *in, | |
2220 | unsigned int inlen, | |
2221 | void *arg), void *arg) | |
2222 | { | |
2223 | ctx->next_proto_select_cb = cb; | |
2224 | ctx->next_proto_select_cb_arg = arg; | |
2225 | } | |
e481f9b9 | 2226 | #endif |
a398f821 | 2227 | |
0f113f3e MC |
2228 | /* |
2229 | * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|. | |
6f017a8f | 2230 | * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit |
0f113f3e MC |
2231 | * length-prefixed strings). Returns 0 on success. |
2232 | */ | |
2233 | int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos, | |
817cd0d5 | 2234 | unsigned int protos_len) |
0f113f3e | 2235 | { |
25aaa98a | 2236 | OPENSSL_free(ctx->alpn_client_proto_list); |
817cd0d5 | 2237 | ctx->alpn_client_proto_list = OPENSSL_memdup(protos, protos_len); |
72e9be3d RS |
2238 | if (ctx->alpn_client_proto_list == NULL) { |
2239 | SSLerr(SSL_F_SSL_CTX_SET_ALPN_PROTOS, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 2240 | return 1; |
72e9be3d | 2241 | } |
0f113f3e MC |
2242 | ctx->alpn_client_proto_list_len = protos_len; |
2243 | ||
2244 | return 0; | |
2245 | } | |
2246 | ||
2247 | /* | |
2248 | * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|. | |
6f017a8f | 2249 | * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit |
0f113f3e MC |
2250 | * length-prefixed strings). Returns 0 on success. |
2251 | */ | |
2252 | int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos, | |
817cd0d5 | 2253 | unsigned int protos_len) |
0f113f3e | 2254 | { |
25aaa98a | 2255 | OPENSSL_free(ssl->alpn_client_proto_list); |
817cd0d5 | 2256 | ssl->alpn_client_proto_list = OPENSSL_memdup(protos, protos_len); |
72e9be3d RS |
2257 | if (ssl->alpn_client_proto_list == NULL) { |
2258 | SSLerr(SSL_F_SSL_SET_ALPN_PROTOS, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 2259 | return 1; |
72e9be3d | 2260 | } |
0f113f3e MC |
2261 | ssl->alpn_client_proto_list_len = protos_len; |
2262 | ||
2263 | return 0; | |
2264 | } | |
2265 | ||
2266 | /* | |
2267 | * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is | |
2268 | * called during ClientHello processing in order to select an ALPN protocol | |
2269 | * from the client's list of offered protocols. | |
2270 | */ | |
2271 | void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, | |
2272 | int (*cb) (SSL *ssl, | |
2273 | const unsigned char **out, | |
2274 | unsigned char *outlen, | |
2275 | const unsigned char *in, | |
2276 | unsigned int inlen, | |
2277 | void *arg), void *arg) | |
2278 | { | |
2279 | ctx->alpn_select_cb = cb; | |
2280 | ctx->alpn_select_cb_arg = arg; | |
2281 | } | |
2282 | ||
2283 | /* | |
2284 | * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from | |
2285 | * |ssl|. On return it sets |*data| to point to |*len| bytes of protocol name | |
2286 | * (not including the leading length-prefix byte). If the server didn't | |
2287 | * respond with a negotiated protocol then |*len| will be zero. | |
2288 | */ | |
6f017a8f | 2289 | void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data, |
817cd0d5 | 2290 | unsigned int *len) |
0f113f3e MC |
2291 | { |
2292 | *data = NULL; | |
2293 | if (ssl->s3) | |
2294 | *data = ssl->s3->alpn_selected; | |
2295 | if (*data == NULL) | |
2296 | *len = 0; | |
2297 | else | |
2298 | *len = ssl->s3->alpn_selected_len; | |
2299 | } | |
2300 | ||
f1fd4544 | 2301 | |
74b4b494 | 2302 | int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen, |
0f113f3e MC |
2303 | const char *label, size_t llen, |
2304 | const unsigned char *p, size_t plen, | |
2305 | int use_context) | |
2306 | { | |
2307 | if (s->version < TLS1_VERSION) | |
2308 | return -1; | |
e0af0405 | 2309 | |
0f113f3e MC |
2310 | return s->method->ssl3_enc->export_keying_material(s, out, olen, label, |
2311 | llen, p, plen, | |
2312 | use_context); | |
2313 | } | |
e0af0405 | 2314 | |
3c1d6bbc | 2315 | static unsigned long ssl_session_hash(const SSL_SESSION *a) |
0f113f3e MC |
2316 | { |
2317 | unsigned long l; | |
2318 | ||
2319 | l = (unsigned long) | |
2320 | ((unsigned int)a->session_id[0]) | | |
2321 | ((unsigned int)a->session_id[1] << 8L) | | |
2322 | ((unsigned long)a->session_id[2] << 16L) | | |
2323 | ((unsigned long)a->session_id[3] << 24L); | |
2324 | return (l); | |
2325 | } | |
2326 | ||
2327 | /* | |
2328 | * NB: If this function (or indeed the hash function which uses a sort of | |
dc644fe2 | 2329 | * coarser function than this one) is changed, ensure |
0f113f3e MC |
2330 | * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on |
2331 | * being able to construct an SSL_SESSION that will collide with any existing | |
2332 | * session with a matching session ID. | |
2333 | */ | |
2334 | static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) | |
2335 | { | |
2336 | if (a->ssl_version != b->ssl_version) | |
2337 | return (1); | |
2338 | if (a->session_id_length != b->session_id_length) | |
2339 | return (1); | |
2340 | return (memcmp(a->session_id, b->session_id, a->session_id_length)); | |
2341 | } | |
2342 | ||
2343 | /* | |
2344 | * These wrapper functions should remain rather than redeclaring | |
d0fa136c | 2345 | * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each |
0f113f3e MC |
2346 | * variable. The reason is that the functions aren't static, they're exposed |
2347 | * via ssl.h. | |
2348 | */ | |
97b17195 | 2349 | |
4ebb342f | 2350 | SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) |
0f113f3e MC |
2351 | { |
2352 | SSL_CTX *ret = NULL; | |
2353 | ||
2354 | if (meth == NULL) { | |
2355 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED); | |
2356 | return (NULL); | |
2357 | } | |
2358 | ||
0fc32b07 MC |
2359 | if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL)) |
2360 | return NULL; | |
7fa792d1 | 2361 | |
0f113f3e | 2362 | if (FIPS_mode() && (meth->version < TLS1_VERSION)) { |
4fa52141 | 2363 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_AT_LEAST_TLS_1_0_NEEDED_IN_FIPS_MODE); |
0f113f3e MC |
2364 | return NULL; |
2365 | } | |
2366 | ||
2367 | if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) { | |
2368 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); | |
2369 | goto err; | |
2370 | } | |
b51bce94 | 2371 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
2372 | if (ret == NULL) |
2373 | goto err; | |
2374 | ||
0f113f3e | 2375 | ret->method = meth; |
7946ab33 KR |
2376 | ret->min_proto_version = 0; |
2377 | ret->max_proto_version = 0; | |
0f113f3e MC |
2378 | ret->session_cache_mode = SSL_SESS_CACHE_SERVER; |
2379 | ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; | |
64b25758 | 2380 | /* We take the system default. */ |
0f113f3e | 2381 | ret->session_timeout = meth->get_timeout(); |
0f113f3e | 2382 | ret->references = 1; |
16203f7b AG |
2383 | ret->lock = CRYPTO_THREAD_lock_new(); |
2384 | if (ret->lock == NULL) { | |
2385 | SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE); | |
2386 | OPENSSL_free(ret); | |
2387 | return NULL; | |
2388 | } | |
0f113f3e | 2389 | ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; |
0f113f3e | 2390 | ret->verify_mode = SSL_VERIFY_NONE; |
0f113f3e MC |
2391 | if ((ret->cert = ssl_cert_new()) == NULL) |
2392 | goto err; | |
2393 | ||
62d0577e | 2394 | ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp); |
0f113f3e MC |
2395 | if (ret->sessions == NULL) |
2396 | goto err; | |
2397 | ret->cert_store = X509_STORE_new(); | |
2398 | if (ret->cert_store == NULL) | |
2399 | goto err; | |
ed29e82a RP |
2400 | #ifndef OPENSSL_NO_CT |
2401 | ret->ctlog_store = CTLOG_STORE_new(); | |
2402 | if (ret->ctlog_store == NULL) | |
2403 | goto err; | |
2404 | #endif | |
61986d32 | 2405 | if (!ssl_create_cipher_list(ret->method, |
0f113f3e | 2406 | &ret->cipher_list, &ret->cipher_list_by_id, |
69f68237 MC |
2407 | SSL_DEFAULT_CIPHER_LIST, ret->cert) |
2408 | || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) { | |
0f113f3e MC |
2409 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS); |
2410 | goto err2; | |
2411 | } | |
2412 | ||
2413 | ret->param = X509_VERIFY_PARAM_new(); | |
a71edf3b | 2414 | if (ret->param == NULL) |
0f113f3e MC |
2415 | goto err; |
2416 | ||
2417 | if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) { | |
2418 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES); | |
2419 | goto err2; | |
2420 | } | |
2421 | if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) { | |
2422 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES); | |
2423 | goto err2; | |
2424 | } | |
2425 | ||
2426 | if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL) | |
2427 | goto err; | |
2428 | ||
2429 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data); | |
2430 | ||
0f113f3e MC |
2431 | /* No compression for DTLS */ |
2432 | if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS)) | |
2433 | ret->comp_methods = SSL_COMP_get_compression_methods(); | |
2434 | ||
2435 | ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; | |
d102d9df | 2436 | ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
566dda07 | 2437 | |
0f113f3e | 2438 | /* Setup RFC4507 ticket keys */ |
266483d2 | 2439 | if ((RAND_bytes(ret->tlsext_tick_key_name, 16) <= 0) |
0f113f3e MC |
2440 | || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0) |
2441 | || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0)) | |
2442 | ret->options |= SSL_OP_NO_TICKET; | |
6434abbf | 2443 | |
edc032b5 | 2444 | #ifndef OPENSSL_NO_SRP |
61986d32 | 2445 | if (!SSL_CTX_SRP_CTX_init(ret)) |
69f68237 | 2446 | goto err; |
edc032b5 | 2447 | #endif |
4db9677b | 2448 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
2449 | # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO |
2450 | # define eng_strx(x) #x | |
2451 | # define eng_str(x) eng_strx(x) | |
2452 | /* Use specific client engine automatically... ignore errors */ | |
2453 | { | |
2454 | ENGINE *eng; | |
2455 | eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); | |
2456 | if (!eng) { | |
2457 | ERR_clear_error(); | |
2458 | ENGINE_load_builtin_engines(); | |
2459 | eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); | |
2460 | } | |
2461 | if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng)) | |
2462 | ERR_clear_error(); | |
2463 | } | |
2464 | # endif | |
2465 | #endif | |
2466 | /* | |
2467 | * Default is to connect to non-RI servers. When RI is more widely | |
2468 | * deployed might change this. | |
2469 | */ | |
2470 | ret->options |= SSL_OP_LEGACY_SERVER_CONNECT; | |
dc5744cb EK |
2471 | /* |
2472 | * Disable compression by default to prevent CRIME. Applications can | |
2473 | * re-enable compression by configuring | |
2474 | * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION); | |
2475 | * or by using the SSL_CONF library. | |
2476 | */ | |
2477 | ret->options |= SSL_OP_NO_COMPRESSION; | |
0f113f3e | 2478 | |
16203f7b | 2479 | return ret; |
0f113f3e MC |
2480 | err: |
2481 | SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE); | |
2482 | err2: | |
e0e920b1 | 2483 | SSL_CTX_free(ret); |
16203f7b | 2484 | return NULL; |
0f113f3e | 2485 | } |
d02b48c6 | 2486 | |
a18a31e4 MC |
2487 | void SSL_CTX_up_ref(SSL_CTX *ctx) |
2488 | { | |
16203f7b AG |
2489 | int i; |
2490 | CRYPTO_atomic_add(&ctx->references, 1, &i, ctx->lock); | |
a18a31e4 MC |
2491 | } |
2492 | ||
4f43d0e7 | 2493 | void SSL_CTX_free(SSL_CTX *a) |
0f113f3e MC |
2494 | { |
2495 | int i; | |
d02b48c6 | 2496 | |
0f113f3e MC |
2497 | if (a == NULL) |
2498 | return; | |
d02b48c6 | 2499 | |
16203f7b | 2500 | CRYPTO_atomic_add(&a->references, -1, &i, a->lock); |
f3f1cf84 | 2501 | REF_PRINT_COUNT("SSL_CTX", a); |
0f113f3e MC |
2502 | if (i > 0) |
2503 | return; | |
f3f1cf84 | 2504 | REF_ASSERT_ISNT(i < 0); |
0f113f3e | 2505 | |
222561fe | 2506 | X509_VERIFY_PARAM_free(a->param); |
919ba009 | 2507 | dane_ctx_final(&a->dane); |
0f113f3e MC |
2508 | |
2509 | /* | |
2510 | * Free internal session cache. However: the remove_cb() may reference | |
2511 | * the ex_data of SSL_CTX, thus the ex_data store can only be removed | |
2512 | * after the sessions were flushed. | |
2513 | * As the ex_data handling routines might also touch the session cache, | |
2514 | * the most secure solution seems to be: empty (flush) the cache, then | |
2515 | * free ex_data, then finally free the cache. | |
2516 | * (See ticket [openssl.org #212].) | |
2517 | */ | |
2518 | if (a->sessions != NULL) | |
2519 | SSL_CTX_flush_sessions(a, 0); | |
2520 | ||
2521 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data); | |
25aaa98a | 2522 | lh_SSL_SESSION_free(a->sessions); |
222561fe | 2523 | X509_STORE_free(a->cert_store); |
ed29e82a RP |
2524 | #ifndef OPENSSL_NO_CT |
2525 | CTLOG_STORE_free(a->ctlog_store); | |
2526 | #endif | |
25aaa98a RS |
2527 | sk_SSL_CIPHER_free(a->cipher_list); |
2528 | sk_SSL_CIPHER_free(a->cipher_list_by_id); | |
e0e920b1 | 2529 | ssl_cert_free(a->cert); |
222561fe RS |
2530 | sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free); |
2531 | sk_X509_pop_free(a->extra_certs, X509_free); | |
0f113f3e | 2532 | a->comp_methods = NULL; |
e783bae2 | 2533 | #ifndef OPENSSL_NO_SRTP |
25aaa98a | 2534 | sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles); |
e783bae2 | 2535 | #endif |
edc032b5 | 2536 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 2537 | SSL_CTX_SRP_CTX_free(a); |
edc032b5 | 2538 | #endif |
bdfe932d | 2539 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 2540 | ENGINE_finish(a->client_cert_engine); |
ddac1974 | 2541 | #endif |
8671b898 | 2542 | |
e481f9b9 | 2543 | #ifndef OPENSSL_NO_EC |
25aaa98a RS |
2544 | OPENSSL_free(a->tlsext_ecpointformatlist); |
2545 | OPENSSL_free(a->tlsext_ellipticcurvelist); | |
8671b898 | 2546 | #endif |
e481f9b9 | 2547 | OPENSSL_free(a->alpn_client_proto_list); |
8671b898 | 2548 | |
16203f7b AG |
2549 | CRYPTO_THREAD_lock_free(a->lock); |
2550 | ||
0f113f3e MC |
2551 | OPENSSL_free(a); |
2552 | } | |
d02b48c6 | 2553 | |
3ae76679 | 2554 | void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) |
0f113f3e MC |
2555 | { |
2556 | ctx->default_passwd_callback = cb; | |
2557 | } | |
2558 | ||
2559 | void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) | |
2560 | { | |
2561 | ctx->default_passwd_callback_userdata = u; | |
2562 | } | |
2563 | ||
0c452abc CH |
2564 | pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) |
2565 | { | |
2566 | return ctx->default_passwd_callback; | |
2567 | } | |
2568 | ||
2569 | void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) | |
2570 | { | |
2571 | return ctx->default_passwd_callback_userdata; | |
2572 | } | |
2573 | ||
a974e64a MC |
2574 | void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb) |
2575 | { | |
2576 | s->default_passwd_callback = cb; | |
2577 | } | |
2578 | ||
2579 | void SSL_set_default_passwd_cb_userdata(SSL *s, void *u) | |
2580 | { | |
2581 | s->default_passwd_callback_userdata = u; | |
2582 | } | |
2583 | ||
0c452abc CH |
2584 | pem_password_cb *SSL_get_default_passwd_cb(SSL *s) |
2585 | { | |
2586 | return s->default_passwd_callback; | |
2587 | } | |
2588 | ||
2589 | void *SSL_get_default_passwd_cb_userdata(SSL *s) | |
2590 | { | |
2591 | return s->default_passwd_callback_userdata; | |
2592 | } | |
2593 | ||
0f113f3e MC |
2594 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, |
2595 | int (*cb) (X509_STORE_CTX *, void *), | |
2596 | void *arg) | |
2597 | { | |
2598 | ctx->app_verify_callback = cb; | |
2599 | ctx->app_verify_arg = arg; | |
2600 | } | |
2601 | ||
2602 | void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, | |
2603 | int (*cb) (int, X509_STORE_CTX *)) | |
2604 | { | |
2605 | ctx->verify_mode = mode; | |
2606 | ctx->default_verify_callback = cb; | |
2607 | } | |
2608 | ||
2609 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) | |
2610 | { | |
2611 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); | |
2612 | } | |
2613 | ||
2614 | void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), | |
2615 | void *arg) | |
2616 | { | |
2617 | ssl_cert_set_cert_cb(c->cert, cb, arg); | |
2618 | } | |
2619 | ||
2620 | void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg) | |
2621 | { | |
2622 | ssl_cert_set_cert_cb(s->cert, cb, arg); | |
2623 | } | |
18d71588 | 2624 | |
2cf28d61 | 2625 | void ssl_set_masks(SSL *s) |
0f113f3e | 2626 | { |
60f43e9e | 2627 | #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_GOST) |
0f113f3e | 2628 | CERT_PKEY *cpk; |
60f43e9e | 2629 | #endif |
6383d316 | 2630 | CERT *c = s->cert; |
f7d53487 | 2631 | uint32_t *pvalid = s->s3->tmp.valid_flags; |
bc71f910 | 2632 | int rsa_enc, rsa_sign, dh_tmp, dsa_sign; |
361a1191 | 2633 | unsigned long mask_k, mask_a; |
10bf4fc2 | 2634 | #ifndef OPENSSL_NO_EC |
361a1191 | 2635 | int have_ecc_cert, ecdsa_ok; |
0f113f3e | 2636 | X509 *x = NULL; |
14536c8c | 2637 | #endif |
0f113f3e MC |
2638 | if (c == NULL) |
2639 | return; | |
d02b48c6 | 2640 | |
bc36ee62 | 2641 | #ifndef OPENSSL_NO_DH |
0f113f3e | 2642 | dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL || c->dh_tmp_auto); |
d02b48c6 | 2643 | #else |
361a1191 | 2644 | dh_tmp = 0; |
d02b48c6 RE |
2645 | #endif |
2646 | ||
6383d316 | 2647 | rsa_enc = pvalid[SSL_PKEY_RSA_ENC] & CERT_PKEY_VALID; |
6383d316 | 2648 | rsa_sign = pvalid[SSL_PKEY_RSA_SIGN] & CERT_PKEY_SIGN; |
6383d316 | 2649 | dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_SIGN; |
14536c8c | 2650 | #ifndef OPENSSL_NO_EC |
6383d316 | 2651 | have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID; |
14536c8c | 2652 | #endif |
0f113f3e MC |
2653 | mask_k = 0; |
2654 | mask_a = 0; | |
0e1dba93 | 2655 | |
d02b48c6 | 2656 | #ifdef CIPHER_DEBUG |
b7557ccf AG |
2657 | fprintf(stderr, "dht=%d re=%d rs=%d ds=%d\n", |
2658 | dh_tmp, rsa_enc, rsa_sign, dsa_sign); | |
0f113f3e MC |
2659 | #endif |
2660 | ||
2a9b9654 | 2661 | #ifndef OPENSSL_NO_GOST |
e44380a9 DB |
2662 | cpk = &(c->pkeys[SSL_PKEY_GOST12_512]); |
2663 | if (cpk->x509 != NULL && cpk->privatekey != NULL) { | |
2664 | mask_k |= SSL_kGOST; | |
2665 | mask_a |= SSL_aGOST12; | |
2666 | } | |
2667 | cpk = &(c->pkeys[SSL_PKEY_GOST12_256]); | |
2668 | if (cpk->x509 != NULL && cpk->privatekey != NULL) { | |
2669 | mask_k |= SSL_kGOST; | |
2670 | mask_a |= SSL_aGOST12; | |
2671 | } | |
0f113f3e MC |
2672 | cpk = &(c->pkeys[SSL_PKEY_GOST01]); |
2673 | if (cpk->x509 != NULL && cpk->privatekey != NULL) { | |
2674 | mask_k |= SSL_kGOST; | |
2675 | mask_a |= SSL_aGOST01; | |
2676 | } | |
2a9b9654 | 2677 | #endif |
0f113f3e | 2678 | |
361a1191 | 2679 | if (rsa_enc) |
0f113f3e | 2680 | mask_k |= SSL_kRSA; |
d02b48c6 | 2681 | |
0f113f3e MC |
2682 | if (dh_tmp) |
2683 | mask_k |= SSL_kDHE; | |
d02b48c6 | 2684 | |
0f113f3e MC |
2685 | if (rsa_enc || rsa_sign) { |
2686 | mask_a |= SSL_aRSA; | |
0f113f3e | 2687 | } |
d02b48c6 | 2688 | |
0f113f3e MC |
2689 | if (dsa_sign) { |
2690 | mask_a |= SSL_aDSS; | |
0f113f3e | 2691 | } |
d02b48c6 | 2692 | |
0f113f3e | 2693 | mask_a |= SSL_aNULL; |
d02b48c6 | 2694 | |
0f113f3e MC |
2695 | /* |
2696 | * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites | |
2697 | * depending on the key usage extension. | |
2698 | */ | |
14536c8c | 2699 | #ifndef OPENSSL_NO_EC |
0f113f3e | 2700 | if (have_ecc_cert) { |
a8d8e06b | 2701 | uint32_t ex_kusage; |
0f113f3e MC |
2702 | cpk = &c->pkeys[SSL_PKEY_ECC]; |
2703 | x = cpk->x509; | |
a8d8e06b | 2704 | ex_kusage = X509_get_key_usage(x); |
a8d8e06b | 2705 | ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE; |
6383d316 | 2706 | if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN)) |
0f113f3e | 2707 | ecdsa_ok = 0; |
c7c46256 | 2708 | if (ecdsa_ok) |
0f113f3e | 2709 | mask_a |= SSL_aECDSA; |
0f113f3e | 2710 | } |
14536c8c | 2711 | #endif |
ea262260 | 2712 | |
10bf4fc2 | 2713 | #ifndef OPENSSL_NO_EC |
fe6ef247 | 2714 | mask_k |= SSL_kECDHE; |
ea262260 | 2715 | #endif |
ddac1974 NL |
2716 | |
2717 | #ifndef OPENSSL_NO_PSK | |
0f113f3e MC |
2718 | mask_k |= SSL_kPSK; |
2719 | mask_a |= SSL_aPSK; | |
526f94ad DSH |
2720 | if (mask_k & SSL_kRSA) |
2721 | mask_k |= SSL_kRSAPSK; | |
2722 | if (mask_k & SSL_kDHE) | |
2723 | mask_k |= SSL_kDHEPSK; | |
2724 | if (mask_k & SSL_kECDHE) | |
2725 | mask_k |= SSL_kECDHEPSK; | |
ddac1974 NL |
2726 | #endif |
2727 | ||
4d69f9e6 DSH |
2728 | s->s3->tmp.mask_k = mask_k; |
2729 | s->s3->tmp.mask_a = mask_a; | |
0f113f3e | 2730 | } |
d02b48c6 | 2731 | |
ef236ec3 DSH |
2732 | #ifndef OPENSSL_NO_EC |
2733 | ||
a2f9200f | 2734 | int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) |
0f113f3e | 2735 | { |
ce0c1f2b | 2736 | if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA) { |
0f113f3e | 2737 | /* key usage, if present, must allow signing */ |
ce0c1f2b | 2738 | if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) { |
0f113f3e MC |
2739 | SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, |
2740 | SSL_R_ECC_CERT_NOT_FOR_SIGNING); | |
2741 | return 0; | |
2742 | } | |
2743 | } | |
0f113f3e MC |
2744 | return 1; /* all checks are ok */ |
2745 | } | |
ea262260 | 2746 | |
ef236ec3 DSH |
2747 | #endif |
2748 | ||
2daceb03 | 2749 | static int ssl_get_server_cert_index(const SSL *s) |
0f113f3e MC |
2750 | { |
2751 | int idx; | |
2752 | idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); | |
2753 | if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) | |
2754 | idx = SSL_PKEY_RSA_SIGN; | |
e44380a9 DB |
2755 | if (idx == SSL_PKEY_GOST_EC) { |
2756 | if (s->cert->pkeys[SSL_PKEY_GOST12_512].x509) | |
2757 | idx = SSL_PKEY_GOST12_512; | |
2758 | else if (s->cert->pkeys[SSL_PKEY_GOST12_256].x509) | |
2759 | idx = SSL_PKEY_GOST12_256; | |
2760 | else if (s->cert->pkeys[SSL_PKEY_GOST01].x509) | |
2761 | idx = SSL_PKEY_GOST01; | |
2762 | else | |
2763 | idx = -1; | |
2764 | } | |
0f113f3e MC |
2765 | if (idx == -1) |
2766 | SSLerr(SSL_F_SSL_GET_SERVER_CERT_INDEX, ERR_R_INTERNAL_ERROR); | |
2767 | return idx; | |
2768 | } | |
a9e1c50b | 2769 | |
6383d316 | 2770 | CERT_PKEY *ssl_get_server_send_pkey(SSL *s) |
0f113f3e MC |
2771 | { |
2772 | CERT *c; | |
2773 | int i; | |
ea262260 | 2774 | |
0f113f3e MC |
2775 | c = s->cert; |
2776 | if (!s->s3 || !s->s3->tmp.new_cipher) | |
2777 | return NULL; | |
2cf28d61 | 2778 | ssl_set_masks(s); |
a9e1c50b | 2779 | |
0f113f3e | 2780 | i = ssl_get_server_cert_index(s); |
a9e1c50b | 2781 | |
0f113f3e MC |
2782 | /* This may or may not be an error. */ |
2783 | if (i < 0) | |
2784 | return NULL; | |
a9e1c50b | 2785 | |
0f113f3e MC |
2786 | /* May be NULL. */ |
2787 | return &c->pkeys[i]; | |
2788 | } | |
d02b48c6 | 2789 | |
0f113f3e MC |
2790 | EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, |
2791 | const EVP_MD **pmd) | |
2792 | { | |
2793 | unsigned long alg_a; | |
2794 | CERT *c; | |
2795 | int idx = -1; | |
d02b48c6 | 2796 | |
0f113f3e MC |
2797 | alg_a = cipher->algorithm_auth; |
2798 | c = s->cert; | |
d02b48c6 | 2799 | |
0f113f3e MC |
2800 | if ((alg_a & SSL_aDSS) && |
2801 | (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL)) | |
2802 | idx = SSL_PKEY_DSA_SIGN; | |
2803 | else if (alg_a & SSL_aRSA) { | |
2804 | if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) | |
2805 | idx = SSL_PKEY_RSA_SIGN; | |
2806 | else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) | |
2807 | idx = SSL_PKEY_RSA_ENC; | |
2808 | } else if ((alg_a & SSL_aECDSA) && | |
2809 | (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) | |
2810 | idx = SSL_PKEY_ECC; | |
2811 | if (idx == -1) { | |
2812 | SSLerr(SSL_F_SSL_GET_SIGN_PKEY, ERR_R_INTERNAL_ERROR); | |
2813 | return (NULL); | |
2814 | } | |
2815 | if (pmd) | |
d376e57d | 2816 | *pmd = s->s3->tmp.md[idx]; |
0f113f3e MC |
2817 | return c->pkeys[idx].privatekey; |
2818 | } | |
d02b48c6 | 2819 | |
a398f821 | 2820 | int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo, |
0f113f3e MC |
2821 | size_t *serverinfo_length) |
2822 | { | |
2823 | CERT *c = NULL; | |
2824 | int i = 0; | |
2825 | *serverinfo_length = 0; | |
2826 | ||
2827 | c = s->cert; | |
2828 | i = ssl_get_server_cert_index(s); | |
2829 | ||
2830 | if (i == -1) | |
2831 | return 0; | |
2832 | if (c->pkeys[i].serverinfo == NULL) | |
2833 | return 0; | |
2834 | ||
2835 | *serverinfo = c->pkeys[i].serverinfo; | |
2836 | *serverinfo_length = c->pkeys[i].serverinfo_length; | |
2837 | return 1; | |
2838 | } | |
0f113f3e MC |
2839 | |
2840 | void ssl_update_cache(SSL *s, int mode) | |
2841 | { | |
2842 | int i; | |
2843 | ||
2844 | /* | |
2845 | * If the session_id_length is 0, we are not supposed to cache it, and it | |
2846 | * would be rather hard to do anyway :-) | |
2847 | */ | |
2848 | if (s->session->session_id_length == 0) | |
2849 | return; | |
2850 | ||
2851 | i = s->session_ctx->session_cache_mode; | |
2852 | if ((i & mode) && (!s->hit) | |
2853 | && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) | |
2854 | || SSL_CTX_add_session(s->session_ctx, s->session)) | |
2855 | && (s->session_ctx->new_session_cb != NULL)) { | |
16203f7b | 2856 | SSL_SESSION_up_ref(s->session); |
0f113f3e MC |
2857 | if (!s->session_ctx->new_session_cb(s, s->session)) |
2858 | SSL_SESSION_free(s->session); | |
2859 | } | |
2860 | ||
2861 | /* auto flush every 255 connections */ | |
2862 | if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) { | |
2863 | if ((((mode & SSL_SESS_CACHE_CLIENT) | |
2864 | ? s->session_ctx->stats.sess_connect_good | |
2865 | : s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) { | |
2866 | SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL)); | |
2867 | } | |
2868 | } | |
2869 | } | |
d02b48c6 | 2870 | |
ba168244 | 2871 | const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx) |
0f113f3e MC |
2872 | { |
2873 | return ctx->method; | |
2874 | } | |
ba168244 | 2875 | |
4ebb342f | 2876 | const SSL_METHOD *SSL_get_ssl_method(SSL *s) |
0f113f3e MC |
2877 | { |
2878 | return (s->method); | |
2879 | } | |
d02b48c6 | 2880 | |
4ebb342f | 2881 | int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth) |
0f113f3e | 2882 | { |
0f113f3e MC |
2883 | int ret = 1; |
2884 | ||
2885 | if (s->method != meth) { | |
919ba009 VD |
2886 | const SSL_METHOD *sm = s->method; |
2887 | int (*hf)(SSL *) = s->handshake_func; | |
0f113f3e | 2888 | |
919ba009 | 2889 | if (sm->version == meth->version) |
0f113f3e MC |
2890 | s->method = meth; |
2891 | else { | |
919ba009 | 2892 | sm->ssl_free(s); |
0f113f3e MC |
2893 | s->method = meth; |
2894 | ret = s->method->ssl_new(s); | |
2895 | } | |
2896 | ||
919ba009 | 2897 | if (hf == sm->ssl_connect) |
0f113f3e | 2898 | s->handshake_func = meth->ssl_connect; |
919ba009 | 2899 | else if (hf == sm->ssl_accept) |
0f113f3e MC |
2900 | s->handshake_func = meth->ssl_accept; |
2901 | } | |
2902 | return (ret); | |
2903 | } | |
2904 | ||
2905 | int SSL_get_error(const SSL *s, int i) | |
2906 | { | |
2907 | int reason; | |
2908 | unsigned long l; | |
2909 | BIO *bio; | |
2910 | ||
2911 | if (i > 0) | |
2912 | return (SSL_ERROR_NONE); | |
2913 | ||
2914 | /* | |
2915 | * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc, | |
2916 | * where we do encode the error | |
2917 | */ | |
2918 | if ((l = ERR_peek_error()) != 0) { | |
2919 | if (ERR_GET_LIB(l) == ERR_LIB_SYS) | |
2920 | return (SSL_ERROR_SYSCALL); | |
2921 | else | |
2922 | return (SSL_ERROR_SSL); | |
2923 | } | |
2924 | ||
2925 | if ((i < 0) && SSL_want_read(s)) { | |
2926 | bio = SSL_get_rbio(s); | |
2927 | if (BIO_should_read(bio)) | |
2928 | return (SSL_ERROR_WANT_READ); | |
2929 | else if (BIO_should_write(bio)) | |
2930 | /* | |
2931 | * This one doesn't make too much sense ... We never try to write | |
2932 | * to the rbio, and an application program where rbio and wbio | |
2933 | * are separate couldn't even know what it should wait for. | |
2934 | * However if we ever set s->rwstate incorrectly (so that we have | |
2935 | * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and | |
2936 | * wbio *are* the same, this test works around that bug; so it | |
2937 | * might be safer to keep it. | |
2938 | */ | |
2939 | return (SSL_ERROR_WANT_WRITE); | |
2940 | else if (BIO_should_io_special(bio)) { | |
2941 | reason = BIO_get_retry_reason(bio); | |
2942 | if (reason == BIO_RR_CONNECT) | |
2943 | return (SSL_ERROR_WANT_CONNECT); | |
2944 | else if (reason == BIO_RR_ACCEPT) | |
2945 | return (SSL_ERROR_WANT_ACCEPT); | |
2946 | else | |
2947 | return (SSL_ERROR_SYSCALL); /* unknown */ | |
2948 | } | |
2949 | } | |
2950 | ||
2951 | if ((i < 0) && SSL_want_write(s)) { | |
2952 | bio = SSL_get_wbio(s); | |
2953 | if (BIO_should_write(bio)) | |
2954 | return (SSL_ERROR_WANT_WRITE); | |
2955 | else if (BIO_should_read(bio)) | |
2956 | /* | |
2957 | * See above (SSL_want_read(s) with BIO_should_write(bio)) | |
2958 | */ | |
2959 | return (SSL_ERROR_WANT_READ); | |
2960 | else if (BIO_should_io_special(bio)) { | |
2961 | reason = BIO_get_retry_reason(bio); | |
2962 | if (reason == BIO_RR_CONNECT) | |
2963 | return (SSL_ERROR_WANT_CONNECT); | |
2964 | else if (reason == BIO_RR_ACCEPT) | |
2965 | return (SSL_ERROR_WANT_ACCEPT); | |
2966 | else | |
2967 | return (SSL_ERROR_SYSCALL); | |
2968 | } | |
2969 | } | |
2970 | if ((i < 0) && SSL_want_x509_lookup(s)) { | |
2971 | return (SSL_ERROR_WANT_X509_LOOKUP); | |
2972 | } | |
07bbc92c MC |
2973 | if ((i < 0) && SSL_want_async(s)) { |
2974 | return SSL_ERROR_WANT_ASYNC; | |
2975 | } | |
0f113f3e MC |
2976 | |
2977 | if (i == 0) { | |
2978 | if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && | |
2979 | (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) | |
2980 | return (SSL_ERROR_ZERO_RETURN); | |
2981 | } | |
2982 | return (SSL_ERROR_SYSCALL); | |
2983 | } | |
d02b48c6 | 2984 | |
add2f5ca MC |
2985 | static int ssl_do_handshake_intern(void *vargs) |
2986 | { | |
2987 | struct ssl_async_args *args; | |
2988 | SSL *s; | |
2989 | ||
2990 | args = (struct ssl_async_args *)vargs; | |
2991 | s = args->s; | |
2992 | ||
2993 | return s->handshake_func(s); | |
2994 | } | |
2995 | ||
4f43d0e7 | 2996 | int SSL_do_handshake(SSL *s) |
0f113f3e MC |
2997 | { |
2998 | int ret = 1; | |
2999 | ||
3000 | if (s->handshake_func == NULL) { | |
3001 | SSLerr(SSL_F_SSL_DO_HANDSHAKE, SSL_R_CONNECTION_TYPE_NOT_SET); | |
add2f5ca | 3002 | return -1; |
0f113f3e MC |
3003 | } |
3004 | ||
3005 | s->method->ssl_renegotiate_check(s); | |
3006 | ||
3007 | if (SSL_in_init(s) || SSL_in_before(s)) { | |
add2f5ca MC |
3008 | if((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) { |
3009 | struct ssl_async_args args; | |
3010 | ||
3011 | args.s = s; | |
3012 | ||
7fecbf6f | 3013 | ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern); |
add2f5ca MC |
3014 | } else { |
3015 | ret = s->handshake_func(s); | |
3016 | } | |
0f113f3e | 3017 | } |
add2f5ca | 3018 | return ret; |
0f113f3e MC |
3019 | } |
3020 | ||
4f43d0e7 | 3021 | void SSL_set_accept_state(SSL *s) |
0f113f3e MC |
3022 | { |
3023 | s->server = 1; | |
3024 | s->shutdown = 0; | |
fe3a3291 | 3025 | ossl_statem_clear(s); |
0f113f3e | 3026 | s->handshake_func = s->method->ssl_accept; |
d31fb0b5 | 3027 | clear_ciphers(s); |
0f113f3e | 3028 | } |
d02b48c6 | 3029 | |
4f43d0e7 | 3030 | void SSL_set_connect_state(SSL *s) |
0f113f3e MC |
3031 | { |
3032 | s->server = 0; | |
3033 | s->shutdown = 0; | |
fe3a3291 | 3034 | ossl_statem_clear(s); |
0f113f3e | 3035 | s->handshake_func = s->method->ssl_connect; |
d31fb0b5 | 3036 | clear_ciphers(s); |
0f113f3e | 3037 | } |
d02b48c6 | 3038 | |
4f43d0e7 | 3039 | int ssl_undefined_function(SSL *s) |
0f113f3e MC |
3040 | { |
3041 | SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
3042 | return (0); | |
3043 | } | |
d02b48c6 | 3044 | |
41a15c4f | 3045 | int ssl_undefined_void_function(void) |
0f113f3e MC |
3046 | { |
3047 | SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION, | |
3048 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
3049 | return (0); | |
3050 | } | |
41a15c4f | 3051 | |
0821bcd4 | 3052 | int ssl_undefined_const_function(const SSL *s) |
0f113f3e | 3053 | { |
0f113f3e MC |
3054 | return (0); |
3055 | } | |
0821bcd4 | 3056 | |
2b8fa1d5 | 3057 | const SSL_METHOD *ssl_bad_method(int ver) |
0f113f3e MC |
3058 | { |
3059 | SSLerr(SSL_F_SSL_BAD_METHOD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
3060 | return (NULL); | |
3061 | } | |
d02b48c6 | 3062 | |
3eb2aff4 | 3063 | const char *ssl_protocol_to_string(int version) |
7d650072 KR |
3064 | { |
3065 | if (version == TLS1_2_VERSION) | |
3066 | return "TLSv1.2"; | |
3067 | else if (version == TLS1_1_VERSION) | |
3068 | return "TLSv1.1"; | |
3069 | else if (version == TLS1_VERSION) | |
3070 | return "TLSv1.0"; | |
3071 | else if (version == SSL3_VERSION) | |
3072 | return "SSLv3"; | |
3073 | else if (version == DTLS1_BAD_VER) | |
3074 | return "DTLSv0.9"; | |
3075 | else if (version == DTLS1_VERSION) | |
3076 | return "DTLSv1"; | |
3077 | else if (version == DTLS1_2_VERSION) | |
3078 | return "DTLSv1.2"; | |
0f113f3e MC |
3079 | else |
3080 | return ("unknown"); | |
3081 | } | |
d02b48c6 | 3082 | |
7d650072 KR |
3083 | const char *SSL_get_version(const SSL *s) |
3084 | { | |
3eb2aff4 | 3085 | return ssl_protocol_to_string(s->version); |
7d650072 KR |
3086 | } |
3087 | ||
4f43d0e7 | 3088 | SSL *SSL_dup(SSL *s) |
0f113f3e MC |
3089 | { |
3090 | STACK_OF(X509_NAME) *sk; | |
3091 | X509_NAME *xn; | |
3092 | SSL *ret; | |
3093 | int i; | |
3094 | ||
919ba009 VD |
3095 | /* If we're not quiescent, just up_ref! */ |
3096 | if (!SSL_in_init(s) || !SSL_in_before(s)) { | |
16203f7b | 3097 | CRYPTO_atomic_add(&s->references, 1, &i, s->lock); |
919ba009 VD |
3098 | return s; |
3099 | } | |
3100 | ||
3101 | /* | |
3102 | * Otherwise, copy configuration state, and session if set. | |
3103 | */ | |
0f113f3e MC |
3104 | if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL) |
3105 | return (NULL); | |
3106 | ||
0f113f3e | 3107 | if (s->session != NULL) { |
919ba009 VD |
3108 | /* |
3109 | * Arranges to share the same session via up_ref. This "copies" | |
3110 | * session-id, SSL_METHOD, sid_ctx, and 'cert' | |
3111 | */ | |
61986d32 | 3112 | if (!SSL_copy_session_id(ret, s)) |
17dd65e6 | 3113 | goto err; |
0f113f3e MC |
3114 | } else { |
3115 | /* | |
3116 | * No session has been established yet, so we have to expect that | |
3117 | * s->cert or ret->cert will be changed later -- they should not both | |
3118 | * point to the same object, and thus we can't use | |
3119 | * SSL_copy_session_id. | |
3120 | */ | |
919ba009 VD |
3121 | if (!SSL_set_ssl_method(ret, s->method)) |
3122 | goto err; | |
0f113f3e MC |
3123 | |
3124 | if (s->cert != NULL) { | |
e0e920b1 | 3125 | ssl_cert_free(ret->cert); |
0f113f3e MC |
3126 | ret->cert = ssl_cert_dup(s->cert); |
3127 | if (ret->cert == NULL) | |
3128 | goto err; | |
3129 | } | |
3130 | ||
61986d32 | 3131 | if (!SSL_set_session_id_context(ret, s->sid_ctx, s->sid_ctx_length)) |
69f68237 | 3132 | goto err; |
0f113f3e MC |
3133 | } |
3134 | ||
919ba009 VD |
3135 | ssl_dane_dup(ret, s); |
3136 | ret->version = s->version; | |
0f113f3e MC |
3137 | ret->options = s->options; |
3138 | ret->mode = s->mode; | |
3139 | SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s)); | |
3140 | SSL_set_read_ahead(ret, SSL_get_read_ahead(s)); | |
3141 | ret->msg_callback = s->msg_callback; | |
3142 | ret->msg_callback_arg = s->msg_callback_arg; | |
3143 | SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s)); | |
3144 | SSL_set_verify_depth(ret, SSL_get_verify_depth(s)); | |
3145 | ret->generate_session_id = s->generate_session_id; | |
3146 | ||
3147 | SSL_set_info_callback(ret, SSL_get_info_callback(s)); | |
3148 | ||
0f113f3e MC |
3149 | /* copy app data, a little dangerous perhaps */ |
3150 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data)) | |
3151 | goto err; | |
3152 | ||
3153 | /* setup rbio, and wbio */ | |
3154 | if (s->rbio != NULL) { | |
3155 | if (!BIO_dup_state(s->rbio, (char *)&ret->rbio)) | |
3156 | goto err; | |
3157 | } | |
3158 | if (s->wbio != NULL) { | |
3159 | if (s->wbio != s->rbio) { | |
3160 | if (!BIO_dup_state(s->wbio, (char *)&ret->wbio)) | |
3161 | goto err; | |
3162 | } else | |
3163 | ret->wbio = ret->rbio; | |
3164 | } | |
919ba009 | 3165 | |
0f113f3e | 3166 | ret->server = s->server; |
919ba009 VD |
3167 | if (s->handshake_func) { |
3168 | if (s->server) | |
3169 | SSL_set_accept_state(ret); | |
3170 | else | |
3171 | SSL_set_connect_state(ret); | |
3172 | } | |
0f113f3e | 3173 | ret->shutdown = s->shutdown; |
0f113f3e MC |
3174 | ret->hit = s->hit; |
3175 | ||
a974e64a MC |
3176 | ret->default_passwd_callback = s->default_passwd_callback; |
3177 | ret->default_passwd_callback_userdata = s->default_passwd_callback_userdata; | |
3178 | ||
0f113f3e MC |
3179 | X509_VERIFY_PARAM_inherit(ret->param, s->param); |
3180 | ||
3181 | /* dup the cipher_list and cipher_list_by_id stacks */ | |
3182 | if (s->cipher_list != NULL) { | |
3183 | if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) | |
3184 | goto err; | |
3185 | } | |
3186 | if (s->cipher_list_by_id != NULL) | |
3187 | if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id)) | |
3188 | == NULL) | |
3189 | goto err; | |
3190 | ||
3191 | /* Dup the client_CA list */ | |
3192 | if (s->client_CA != NULL) { | |
3193 | if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL) | |
3194 | goto err; | |
3195 | ret->client_CA = sk; | |
3196 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { | |
3197 | xn = sk_X509_NAME_value(sk, i); | |
3198 | if (sk_X509_NAME_set(sk, i, X509_NAME_dup(xn)) == NULL) { | |
3199 | X509_NAME_free(xn); | |
3200 | goto err; | |
3201 | } | |
3202 | } | |
3203 | } | |
66696478 | 3204 | return ret; |
0f113f3e | 3205 | |
0f113f3e | 3206 | err: |
66696478 RS |
3207 | SSL_free(ret); |
3208 | return NULL; | |
0f113f3e | 3209 | } |
d02b48c6 | 3210 | |
4f43d0e7 | 3211 | void ssl_clear_cipher_ctx(SSL *s) |
0f113f3e MC |
3212 | { |
3213 | if (s->enc_read_ctx != NULL) { | |
846ec07d | 3214 | EVP_CIPHER_CTX_free(s->enc_read_ctx); |
0f113f3e MC |
3215 | s->enc_read_ctx = NULL; |
3216 | } | |
3217 | if (s->enc_write_ctx != NULL) { | |
846ec07d | 3218 | EVP_CIPHER_CTX_free(s->enc_write_ctx); |
0f113f3e MC |
3219 | s->enc_write_ctx = NULL; |
3220 | } | |
09b6c2ef | 3221 | #ifndef OPENSSL_NO_COMP |
efa7dd64 RS |
3222 | COMP_CTX_free(s->expand); |
3223 | s->expand = NULL; | |
3224 | COMP_CTX_free(s->compress); | |
3225 | s->compress = NULL; | |
0f113f3e MC |
3226 | #endif |
3227 | } | |
d02b48c6 | 3228 | |
0821bcd4 | 3229 | X509 *SSL_get_certificate(const SSL *s) |
0f113f3e MC |
3230 | { |
3231 | if (s->cert != NULL) | |
3232 | return (s->cert->key->x509); | |
3233 | else | |
3234 | return (NULL); | |
3235 | } | |
d02b48c6 | 3236 | |
a25f9adc | 3237 | EVP_PKEY *SSL_get_privatekey(const SSL *s) |
0f113f3e MC |
3238 | { |
3239 | if (s->cert != NULL) | |
3240 | return (s->cert->key->privatekey); | |
3241 | else | |
3242 | return (NULL); | |
3243 | } | |
d02b48c6 | 3244 | |
a25f9adc | 3245 | X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) |
0f113f3e MC |
3246 | { |
3247 | if (ctx->cert != NULL) | |
3248 | return ctx->cert->key->x509; | |
3249 | else | |
3250 | return NULL; | |
3251 | } | |
a25f9adc DSH |
3252 | |
3253 | EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) | |
0f113f3e MC |
3254 | { |
3255 | if (ctx->cert != NULL) | |
3256 | return ctx->cert->key->privatekey; | |
3257 | else | |
3258 | return NULL; | |
3259 | } | |
a25f9adc | 3260 | |
babb3798 | 3261 | const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) |
0f113f3e MC |
3262 | { |
3263 | if ((s->session != NULL) && (s->session->cipher != NULL)) | |
3264 | return (s->session->cipher); | |
3265 | return (NULL); | |
3266 | } | |
3267 | ||
377dcdba | 3268 | const COMP_METHOD *SSL_get_current_compression(SSL *s) |
0f113f3e | 3269 | { |
9a555706 RS |
3270 | #ifndef OPENSSL_NO_COMP |
3271 | return s->compress ? COMP_CTX_get_method(s->compress) : NULL; | |
3272 | #else | |
3273 | return NULL; | |
3274 | #endif | |
0f113f3e | 3275 | } |
377dcdba RL |
3276 | |
3277 | const COMP_METHOD *SSL_get_current_expansion(SSL *s) | |
0f113f3e | 3278 | { |
9a555706 RS |
3279 | #ifndef OPENSSL_NO_COMP |
3280 | return s->expand ? COMP_CTX_get_method(s->expand) : NULL; | |
3281 | #else | |
3282 | return NULL; | |
0f113f3e | 3283 | #endif |
9a555706 | 3284 | } |
0f113f3e MC |
3285 | |
3286 | int ssl_init_wbio_buffer(SSL *s, int push) | |
3287 | { | |
3288 | BIO *bbio; | |
3289 | ||
3290 | if (s->bbio == NULL) { | |
3291 | bbio = BIO_new(BIO_f_buffer()); | |
3292 | if (bbio == NULL) | |
3293 | return (0); | |
3294 | s->bbio = bbio; | |
3295 | } else { | |
3296 | bbio = s->bbio; | |
3297 | if (s->bbio == s->wbio) | |
3298 | s->wbio = BIO_pop(s->wbio); | |
3299 | } | |
3300 | (void)BIO_reset(bbio); | |
3301 | /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ | |
3302 | if (!BIO_set_read_buffer_size(bbio, 1)) { | |
3303 | SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER, ERR_R_BUF_LIB); | |
3304 | return (0); | |
3305 | } | |
3306 | if (push) { | |
3307 | if (s->wbio != bbio) | |
3308 | s->wbio = BIO_push(bbio, s->wbio); | |
3309 | } else { | |
3310 | if (s->wbio == bbio) | |
3311 | s->wbio = BIO_pop(bbio); | |
3312 | } | |
3313 | return (1); | |
3314 | } | |
413c4f45 | 3315 | |
4f43d0e7 | 3316 | void ssl_free_wbio_buffer(SSL *s) |
0f113f3e | 3317 | { |
62adbcee | 3318 | /* callers ensure s is never null */ |
0f113f3e MC |
3319 | if (s->bbio == NULL) |
3320 | return; | |
3321 | ||
3322 | if (s->bbio == s->wbio) { | |
3323 | /* remove buffering */ | |
3324 | s->wbio = BIO_pop(s->wbio); | |
f3f1cf84 RS |
3325 | #ifdef REF_DEBUG |
3326 | /* | |
3327 | * not the usual REF_DEBUG, but this avoids | |
3328 | * adding one more preprocessor symbol | |
3329 | */ | |
0f113f3e MC |
3330 | assert(s->wbio != NULL); |
3331 | #endif | |
3332 | } | |
3333 | BIO_free(s->bbio); | |
3334 | s->bbio = NULL; | |
3335 | } | |
3336 | ||
3337 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) | |
3338 | { | |
3339 | ctx->quiet_shutdown = mode; | |
3340 | } | |
58964a49 | 3341 | |
0821bcd4 | 3342 | int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) |
0f113f3e MC |
3343 | { |
3344 | return (ctx->quiet_shutdown); | |
3345 | } | |
58964a49 | 3346 | |
0f113f3e MC |
3347 | void SSL_set_quiet_shutdown(SSL *s, int mode) |
3348 | { | |
3349 | s->quiet_shutdown = mode; | |
3350 | } | |
58964a49 | 3351 | |
0821bcd4 | 3352 | int SSL_get_quiet_shutdown(const SSL *s) |
0f113f3e MC |
3353 | { |
3354 | return (s->quiet_shutdown); | |
3355 | } | |
58964a49 | 3356 | |
0f113f3e MC |
3357 | void SSL_set_shutdown(SSL *s, int mode) |
3358 | { | |
3359 | s->shutdown = mode; | |
3360 | } | |
58964a49 | 3361 | |
0821bcd4 | 3362 | int SSL_get_shutdown(const SSL *s) |
0f113f3e MC |
3363 | { |
3364 | return (s->shutdown); | |
3365 | } | |
58964a49 | 3366 | |
0821bcd4 | 3367 | int SSL_version(const SSL *s) |
0f113f3e MC |
3368 | { |
3369 | return (s->version); | |
3370 | } | |
58964a49 | 3371 | |
0821bcd4 | 3372 | SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) |
0f113f3e MC |
3373 | { |
3374 | return (ssl->ctx); | |
3375 | } | |
3376 | ||
3377 | SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) | |
3378 | { | |
24a0d393 | 3379 | CERT *new_cert; |
0f113f3e MC |
3380 | if (ssl->ctx == ctx) |
3381 | return ssl->ctx; | |
0f113f3e MC |
3382 | if (ctx == NULL) |
3383 | ctx = ssl->initial_ctx; | |
24a0d393 KR |
3384 | new_cert = ssl_cert_dup(ctx->cert); |
3385 | if (new_cert == NULL) { | |
3386 | return NULL; | |
0f113f3e | 3387 | } |
24a0d393 KR |
3388 | ssl_cert_free(ssl->cert); |
3389 | ssl->cert = new_cert; | |
0f113f3e MC |
3390 | |
3391 | /* | |
3392 | * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH), | |
3393 | * so setter APIs must prevent invalid lengths from entering the system. | |
3394 | */ | |
3395 | OPENSSL_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)); | |
3396 | ||
3397 | /* | |
3398 | * If the session ID context matches that of the parent SSL_CTX, | |
3399 | * inherit it from the new SSL_CTX as well. If however the context does | |
3400 | * not match (i.e., it was set per-ssl with SSL_set_session_id_context), | |
3401 | * leave it unchanged. | |
3402 | */ | |
3403 | if ((ssl->ctx != NULL) && | |
3404 | (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) && | |
3405 | (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) { | |
3406 | ssl->sid_ctx_length = ctx->sid_ctx_length; | |
3407 | memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx)); | |
3408 | } | |
3409 | ||
16203f7b | 3410 | SSL_CTX_up_ref(ctx); |
e0e920b1 | 3411 | SSL_CTX_free(ssl->ctx); /* decrement reference count */ |
0f113f3e MC |
3412 | ssl->ctx = ctx; |
3413 | ||
16203f7b | 3414 | return ssl->ctx; |
0f113f3e | 3415 | } |
ed3883d2 | 3416 | |
4f43d0e7 | 3417 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) |
0f113f3e MC |
3418 | { |
3419 | return (X509_STORE_set_default_paths(ctx->cert_store)); | |
3420 | } | |
58964a49 | 3421 | |
d84a7b20 MC |
3422 | int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx) |
3423 | { | |
3424 | X509_LOOKUP *lookup; | |
3425 | ||
3426 | lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir()); | |
3427 | if (lookup == NULL) | |
3428 | return 0; | |
3429 | X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT); | |
3430 | ||
3431 | /* Clear any errors if the default directory does not exist */ | |
3432 | ERR_clear_error(); | |
3433 | ||
3434 | return 1; | |
3435 | } | |
3436 | ||
3437 | int SSL_CTX_set_default_verify_file(SSL_CTX *ctx) | |
3438 | { | |
3439 | X509_LOOKUP *lookup; | |
3440 | ||
3441 | lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file()); | |
3442 | if (lookup == NULL) | |
3443 | return 0; | |
3444 | ||
3445 | X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT); | |
3446 | ||
3447 | /* Clear any errors if the default file does not exist */ | |
3448 | ERR_clear_error(); | |
3449 | ||
3450 | return 1; | |
3451 | } | |
3452 | ||
303c0028 | 3453 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, |
0f113f3e MC |
3454 | const char *CApath) |
3455 | { | |
3456 | return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath)); | |
3457 | } | |
58964a49 | 3458 | |
45d87a1f | 3459 | void SSL_set_info_callback(SSL *ssl, |
0f113f3e MC |
3460 | void (*cb) (const SSL *ssl, int type, int val)) |
3461 | { | |
3462 | ssl->info_callback = cb; | |
3463 | } | |
3464 | ||
3465 | /* | |
3466 | * One compiler (Diab DCC) doesn't like argument names in returned function | |
3467 | * pointer. | |
3468 | */ | |
3469 | void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ , | |
3470 | int /* type */ , | |
3471 | int /* val */ ) { | |
3472 | return ssl->info_callback; | |
3473 | } | |
58964a49 | 3474 | |
0f113f3e MC |
3475 | void SSL_set_verify_result(SSL *ssl, long arg) |
3476 | { | |
3477 | ssl->verify_result = arg; | |
3478 | } | |
58964a49 | 3479 | |
0821bcd4 | 3480 | long SSL_get_verify_result(const SSL *ssl) |
0f113f3e MC |
3481 | { |
3482 | return (ssl->verify_result); | |
3483 | } | |
3484 | ||
d9f1c639 | 3485 | size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen) |
858618e7 | 3486 | { |
6b8f5d0d | 3487 | if (outlen == 0) |
858618e7 NM |
3488 | return sizeof(ssl->s3->client_random); |
3489 | if (outlen > sizeof(ssl->s3->client_random)) | |
3490 | outlen = sizeof(ssl->s3->client_random); | |
3491 | memcpy(out, ssl->s3->client_random, outlen); | |
d9f1c639 | 3492 | return outlen; |
858618e7 NM |
3493 | } |
3494 | ||
d9f1c639 | 3495 | size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen) |
858618e7 | 3496 | { |
6b8f5d0d | 3497 | if (outlen == 0) |
858618e7 NM |
3498 | return sizeof(ssl->s3->server_random); |
3499 | if (outlen > sizeof(ssl->s3->server_random)) | |
3500 | outlen = sizeof(ssl->s3->server_random); | |
3501 | memcpy(out, ssl->s3->server_random, outlen); | |
d9f1c639 | 3502 | return outlen; |
858618e7 NM |
3503 | } |
3504 | ||
d9f1c639 | 3505 | size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, |
6b8f5d0d | 3506 | unsigned char *out, size_t outlen) |
858618e7 | 3507 | { |
6b8f5d0d MC |
3508 | if (session->master_key_length < 0) { |
3509 | /* Should never happen */ | |
3510 | return 0; | |
3511 | } | |
d9f1c639 MC |
3512 | if (outlen == 0) |
3513 | return session->master_key_length; | |
6b8f5d0d | 3514 | if (outlen > (size_t)session->master_key_length) |
858618e7 NM |
3515 | outlen = session->master_key_length; |
3516 | memcpy(out, session->master_key, outlen); | |
d9f1c639 | 3517 | return outlen; |
858618e7 NM |
3518 | } |
3519 | ||
0f113f3e MC |
3520 | int SSL_set_ex_data(SSL *s, int idx, void *arg) |
3521 | { | |
3522 | return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); | |
3523 | } | |
3524 | ||
3525 | void *SSL_get_ex_data(const SSL *s, int idx) | |
3526 | { | |
3527 | return (CRYPTO_get_ex_data(&s->ex_data, idx)); | |
3528 | } | |
3529 | ||
0f113f3e MC |
3530 | int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) |
3531 | { | |
3532 | return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); | |
3533 | } | |
3534 | ||
3535 | void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) | |
3536 | { | |
3537 | return (CRYPTO_get_ex_data(&s->ex_data, idx)); | |
3538 | } | |
58964a49 | 3539 | |
4f43d0e7 | 3540 | int ssl_ok(SSL *s) |
0f113f3e MC |
3541 | { |
3542 | return (1); | |
3543 | } | |
dfeab068 | 3544 | |
0821bcd4 | 3545 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) |
0f113f3e MC |
3546 | { |
3547 | return (ctx->cert_store); | |
3548 | } | |
413c4f45 | 3549 | |
0f113f3e MC |
3550 | void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) |
3551 | { | |
222561fe | 3552 | X509_STORE_free(ctx->cert_store); |
0f113f3e MC |
3553 | ctx->cert_store = store; |
3554 | } | |
413c4f45 | 3555 | |
0821bcd4 | 3556 | int SSL_want(const SSL *s) |
0f113f3e MC |
3557 | { |
3558 | return (s->rwstate); | |
3559 | } | |
413c4f45 | 3560 | |
0f113f3e | 3561 | /** |
4f43d0e7 BL |
3562 | * \brief Set the callback for generating temporary DH keys. |
3563 | * \param ctx the SSL context. | |
3564 | * \param dh the callback | |
3565 | */ | |
3566 | ||
bc36ee62 | 3567 | #ifndef OPENSSL_NO_DH |
0f113f3e MC |
3568 | void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, |
3569 | DH *(*dh) (SSL *ssl, int is_export, | |
3570 | int keylength)) | |
3571 | { | |
3572 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh); | |
3573 | } | |
f8c3c05d | 3574 | |
0f113f3e MC |
3575 | void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh) (SSL *ssl, int is_export, |
3576 | int keylength)) | |
3577 | { | |
3578 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh); | |
3579 | } | |
79df9d62 | 3580 | #endif |
15d21c2d | 3581 | |
ddac1974 NL |
3582 | #ifndef OPENSSL_NO_PSK |
3583 | int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) | |
0f113f3e MC |
3584 | { |
3585 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { | |
3586 | SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, | |
3587 | SSL_R_DATA_LENGTH_TOO_LONG); | |
3588 | return 0; | |
3589 | } | |
df6da24b | 3590 | OPENSSL_free(ctx->cert->psk_identity_hint); |
0f113f3e | 3591 | if (identity_hint != NULL) { |
7644a9ae | 3592 | ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint); |
df6da24b | 3593 | if (ctx->cert->psk_identity_hint == NULL) |
0f113f3e MC |
3594 | return 0; |
3595 | } else | |
df6da24b | 3596 | ctx->cert->psk_identity_hint = NULL; |
0f113f3e MC |
3597 | return 1; |
3598 | } | |
ddac1974 NL |
3599 | |
3600 | int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) | |
0f113f3e MC |
3601 | { |
3602 | if (s == NULL) | |
3603 | return 0; | |
3604 | ||
0f113f3e MC |
3605 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { |
3606 | SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG); | |
3607 | return 0; | |
3608 | } | |
df6da24b | 3609 | OPENSSL_free(s->cert->psk_identity_hint); |
0f113f3e | 3610 | if (identity_hint != NULL) { |
7644a9ae | 3611 | s->cert->psk_identity_hint = OPENSSL_strdup(identity_hint); |
df6da24b | 3612 | if (s->cert->psk_identity_hint == NULL) |
0f113f3e MC |
3613 | return 0; |
3614 | } else | |
df6da24b | 3615 | s->cert->psk_identity_hint = NULL; |
0f113f3e MC |
3616 | return 1; |
3617 | } | |
ddac1974 NL |
3618 | |
3619 | const char *SSL_get_psk_identity_hint(const SSL *s) | |
0f113f3e MC |
3620 | { |
3621 | if (s == NULL || s->session == NULL) | |
3622 | return NULL; | |
3623 | return (s->session->psk_identity_hint); | |
3624 | } | |
ddac1974 NL |
3625 | |
3626 | const char *SSL_get_psk_identity(const SSL *s) | |
0f113f3e MC |
3627 | { |
3628 | if (s == NULL || s->session == NULL) | |
3629 | return NULL; | |
3630 | return (s->session->psk_identity); | |
3631 | } | |
7806f3dd | 3632 | |
52b8dad8 | 3633 | void SSL_set_psk_client_callback(SSL *s, |
0f113f3e MC |
3634 | unsigned int (*cb) (SSL *ssl, |
3635 | const char *hint, | |
3636 | char *identity, | |
3637 | unsigned int | |
3638 | max_identity_len, | |
3639 | unsigned char *psk, | |
3640 | unsigned int | |
3641 | max_psk_len)) | |
3642 | { | |
3643 | s->psk_client_callback = cb; | |
3644 | } | |
7806f3dd NL |
3645 | |
3646 | void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, | |
0f113f3e MC |
3647 | unsigned int (*cb) (SSL *ssl, |
3648 | const char *hint, | |
3649 | char *identity, | |
3650 | unsigned int | |
3651 | max_identity_len, | |
3652 | unsigned char *psk, | |
3653 | unsigned int | |
3654 | max_psk_len)) | |
3655 | { | |
3656 | ctx->psk_client_callback = cb; | |
3657 | } | |
7806f3dd | 3658 | |
52b8dad8 | 3659 | void SSL_set_psk_server_callback(SSL *s, |
0f113f3e MC |
3660 | unsigned int (*cb) (SSL *ssl, |
3661 | const char *identity, | |
3662 | unsigned char *psk, | |
3663 | unsigned int | |
3664 | max_psk_len)) | |
3665 | { | |
3666 | s->psk_server_callback = cb; | |
3667 | } | |
7806f3dd NL |
3668 | |
3669 | void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, | |
0f113f3e MC |
3670 | unsigned int (*cb) (SSL *ssl, |
3671 | const char *identity, | |
3672 | unsigned char *psk, | |
3673 | unsigned int | |
3674 | max_psk_len)) | |
3675 | { | |
3676 | ctx->psk_server_callback = cb; | |
3677 | } | |
3678 | #endif | |
3679 | ||
3680 | void SSL_CTX_set_msg_callback(SSL_CTX *ctx, | |
3681 | void (*cb) (int write_p, int version, | |
3682 | int content_type, const void *buf, | |
3683 | size_t len, SSL *ssl, void *arg)) | |
3684 | { | |
3685 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); | |
3686 | } | |
3687 | ||
3688 | void SSL_set_msg_callback(SSL *ssl, | |
3689 | void (*cb) (int write_p, int version, | |
3690 | int content_type, const void *buf, | |
3691 | size_t len, SSL *ssl, void *arg)) | |
3692 | { | |
3693 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); | |
3694 | } | |
a661b653 | 3695 | |
7c2d4fee | 3696 | void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx, |
0f113f3e MC |
3697 | int (*cb) (SSL *ssl, |
3698 | int | |
3699 | is_forward_secure)) | |
3700 | { | |
3701 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB, | |
3702 | (void (*)(void))cb); | |
3703 | } | |
3704 | ||
7c2d4fee | 3705 | void SSL_set_not_resumable_session_callback(SSL *ssl, |
0f113f3e MC |
3706 | int (*cb) (SSL *ssl, |
3707 | int is_forward_secure)) | |
3708 | { | |
3709 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB, | |
3710 | (void (*)(void))cb); | |
3711 | } | |
3712 | ||
3713 | /* | |
3714 | * Allocates new EVP_MD_CTX and sets pointer to it into given pointer | |
3715 | * vairable, freeing EVP_MD_CTX previously stored in that variable, if any. | |
3716 | * If EVP_MD pointer is passed, initializes ctx with this md Returns newly | |
3717 | * allocated ctx; | |
8671b898 | 3718 | */ |
b948e2c5 | 3719 | |
0f113f3e | 3720 | EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md) |
b948e2c5 | 3721 | { |
0f113f3e | 3722 | ssl_clear_hash_ctx(hash); |
bfb0641f | 3723 | *hash = EVP_MD_CTX_new(); |
5f3d93e4 | 3724 | if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) { |
bfb0641f | 3725 | EVP_MD_CTX_free(*hash); |
5f3d93e4 MC |
3726 | *hash = NULL; |
3727 | return NULL; | |
3728 | } | |
0f113f3e | 3729 | return *hash; |
b948e2c5 | 3730 | } |
0f113f3e MC |
3731 | |
3732 | void ssl_clear_hash_ctx(EVP_MD_CTX **hash) | |
b948e2c5 DSH |
3733 | { |
3734 | ||
0f113f3e | 3735 | if (*hash) |
bfb0641f | 3736 | EVP_MD_CTX_free(*hash); |
0f113f3e | 3737 | *hash = NULL; |
b948e2c5 | 3738 | } |
a661b653 | 3739 | |
48fbcbac DSH |
3740 | /* Retrieve handshake hashes */ |
3741 | int ssl_handshake_hash(SSL *s, unsigned char *out, int outlen) | |
3742 | { | |
6e59a892 | 3743 | EVP_MD_CTX *ctx = NULL; |
28ba2541 DSH |
3744 | EVP_MD_CTX *hdgst = s->s3->handshake_dgst; |
3745 | int ret = EVP_MD_CTX_size(hdgst); | |
28ba2541 DSH |
3746 | if (ret < 0 || ret > outlen) { |
3747 | ret = 0; | |
3748 | goto err; | |
48fbcbac | 3749 | } |
bfb0641f | 3750 | ctx = EVP_MD_CTX_new(); |
6e59a892 RL |
3751 | if (ctx == NULL) { |
3752 | ret = 0; | |
3753 | goto err; | |
3754 | } | |
3755 | if (!EVP_MD_CTX_copy_ex(ctx, hdgst) | |
3756 | || EVP_DigestFinal_ex(ctx, out, NULL) <= 0) | |
28ba2541 | 3757 | ret = 0; |
48fbcbac | 3758 | err: |
bfb0641f | 3759 | EVP_MD_CTX_free(ctx); |
48fbcbac DSH |
3760 | return ret; |
3761 | } | |
3762 | ||
b577fd0b | 3763 | int SSL_session_reused(SSL *s) |
0f113f3e MC |
3764 | { |
3765 | return s->hit; | |
3766 | } | |
08557cf2 | 3767 | |
87adf1fa | 3768 | int SSL_is_server(SSL *s) |
0f113f3e MC |
3769 | { |
3770 | return s->server; | |
3771 | } | |
87adf1fa | 3772 | |
47153c72 RS |
3773 | #if OPENSSL_API_COMPAT < 0x10100000L |
3774 | void SSL_set_debug(SSL *s, int debug) | |
3775 | { | |
3776 | /* Old function was do-nothing anyway... */ | |
3777 | (void)s; | |
3778 | (void)debug; | |
3779 | } | |
3780 | #endif | |
3781 | ||
3782 | ||
b362ccab | 3783 | void SSL_set_security_level(SSL *s, int level) |
0f113f3e MC |
3784 | { |
3785 | s->cert->sec_level = level; | |
3786 | } | |
b362ccab DSH |
3787 | |
3788 | int SSL_get_security_level(const SSL *s) | |
0f113f3e MC |
3789 | { |
3790 | return s->cert->sec_level; | |
3791 | } | |
b362ccab | 3792 | |
0f113f3e | 3793 | void SSL_set_security_callback(SSL *s, |
e4646a89 | 3794 | int (*cb) (const SSL *s, const SSL_CTX *ctx, int op, |
0f113f3e MC |
3795 | int bits, int nid, void *other, |
3796 | void *ex)) | |
3797 | { | |
3798 | s->cert->sec_cb = cb; | |
3799 | } | |
b362ccab | 3800 | |
e4646a89 | 3801 | int (*SSL_get_security_callback(const SSL *s)) (const SSL *s, const SSL_CTX *ctx, int op, |
0f113f3e MC |
3802 | int bits, int nid, |
3803 | void *other, void *ex) { | |
3804 | return s->cert->sec_cb; | |
3805 | } | |
b362ccab DSH |
3806 | |
3807 | void SSL_set0_security_ex_data(SSL *s, void *ex) | |
0f113f3e MC |
3808 | { |
3809 | s->cert->sec_ex = ex; | |
3810 | } | |
b362ccab DSH |
3811 | |
3812 | void *SSL_get0_security_ex_data(const SSL *s) | |
0f113f3e MC |
3813 | { |
3814 | return s->cert->sec_ex; | |
3815 | } | |
b362ccab DSH |
3816 | |
3817 | void SSL_CTX_set_security_level(SSL_CTX *ctx, int level) | |
0f113f3e MC |
3818 | { |
3819 | ctx->cert->sec_level = level; | |
3820 | } | |
b362ccab DSH |
3821 | |
3822 | int SSL_CTX_get_security_level(const SSL_CTX *ctx) | |
0f113f3e MC |
3823 | { |
3824 | return ctx->cert->sec_level; | |
3825 | } | |
b362ccab | 3826 | |
0f113f3e | 3827 | void SSL_CTX_set_security_callback(SSL_CTX *ctx, |
e4646a89 | 3828 | int (*cb) (const SSL *s, const SSL_CTX *ctx, int op, |
0f113f3e MC |
3829 | int bits, int nid, void *other, |
3830 | void *ex)) | |
3831 | { | |
3832 | ctx->cert->sec_cb = cb; | |
3833 | } | |
b362ccab | 3834 | |
e4646a89 KR |
3835 | int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (const SSL *s, |
3836 | const SSL_CTX *ctx, | |
0f113f3e MC |
3837 | int op, int bits, |
3838 | int nid, | |
3839 | void *other, | |
3840 | void *ex) { | |
3841 | return ctx->cert->sec_cb; | |
3842 | } | |
b362ccab DSH |
3843 | |
3844 | void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex) | |
0f113f3e MC |
3845 | { |
3846 | ctx->cert->sec_ex = ex; | |
3847 | } | |
b362ccab DSH |
3848 | |
3849 | void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx) | |
0f113f3e MC |
3850 | { |
3851 | return ctx->cert->sec_ex; | |
3852 | } | |
b362ccab | 3853 | |
8106cb8b VD |
3854 | |
3855 | /* | |
3856 | * Get/Set/Clear options in SSL_CTX or SSL, formerly macros, now functions that | |
3857 | * can return unsigned long, instead of the generic long return value from the | |
3858 | * control interface. | |
3859 | */ | |
3860 | unsigned long SSL_CTX_get_options(const SSL_CTX *ctx) | |
3861 | { | |
3862 | return ctx->options; | |
3863 | } | |
3864 | unsigned long SSL_get_options(const SSL* s) | |
3865 | { | |
3866 | return s->options; | |
3867 | } | |
3868 | unsigned long SSL_CTX_set_options(SSL_CTX *ctx, unsigned long op) | |
3869 | { | |
3870 | return ctx->options |= op; | |
3871 | } | |
3872 | unsigned long SSL_set_options(SSL *s, unsigned long op) | |
3873 | { | |
3874 | return s->options |= op; | |
3875 | } | |
3876 | unsigned long SSL_CTX_clear_options(SSL_CTX *ctx, unsigned long op) | |
3877 | { | |
3878 | return ctx->options &= ~op; | |
3879 | } | |
3880 | unsigned long SSL_clear_options(SSL *s, unsigned long op) | |
3881 | { | |
3882 | return s->options &= ~op; | |
3883 | } | |
3884 | ||
696178ed DSH |
3885 | STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s) |
3886 | { | |
3887 | return s->verified_chain; | |
3888 | } | |
3889 | ||
0f113f3e | 3890 | IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id); |
ed29e82a RP |
3891 | |
3892 | #ifndef OPENSSL_NO_CT | |
3893 | ||
3894 | /* | |
3895 | * Moves SCTs from the |src| stack to the |dst| stack. | |
3896 | * The source of each SCT will be set to |origin|. | |
3897 | * If |dst| points to a NULL pointer, a new stack will be created and owned by | |
3898 | * the caller. | |
3899 | * Returns the number of SCTs moved, or a negative integer if an error occurs. | |
3900 | */ | |
3901 | static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src, sct_source_t origin) | |
3902 | { | |
3903 | int scts_moved = 0; | |
3904 | SCT *sct = NULL; | |
3905 | ||
3906 | if (*dst == NULL) { | |
3907 | *dst = sk_SCT_new_null(); | |
3908 | if (*dst == NULL) { | |
3909 | SSLerr(SSL_F_CT_MOVE_SCTS, ERR_R_MALLOC_FAILURE); | |
3910 | goto err; | |
3911 | } | |
3912 | } | |
3913 | ||
3914 | while ((sct = sk_SCT_pop(src)) != NULL) { | |
3915 | if (SCT_set_source(sct, origin) != 1) | |
3916 | goto err; | |
3917 | ||
3918 | if (sk_SCT_push(*dst, sct) <= 0) | |
3919 | goto err; | |
3920 | scts_moved += 1; | |
3921 | } | |
3922 | ||
3923 | return scts_moved; | |
3924 | err: | |
3925 | if (sct != NULL) | |
3926 | sk_SCT_push(src, sct); /* Put the SCT back */ | |
3927 | return scts_moved; | |
3928 | } | |
3929 | ||
3930 | /* | |
3931 | * Look for data collected during ServerHello and parse if found. | |
3932 | * Return 1 on success, 0 on failure. | |
3933 | */ | |
3934 | static int ct_extract_tls_extension_scts(SSL *s) | |
3935 | { | |
3936 | int scts_extracted = 0; | |
3937 | ||
3938 | if (s->tlsext_scts != NULL) { | |
3939 | const unsigned char *p = s->tlsext_scts; | |
3940 | STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->tlsext_scts_len); | |
3941 | ||
3942 | scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION); | |
3943 | ||
3944 | SCT_LIST_free(scts); | |
3945 | } | |
3946 | ||
3947 | return scts_extracted; | |
3948 | } | |
3949 | ||
3950 | /* | |
3951 | * Checks for an OCSP response and then attempts to extract any SCTs found if it | |
3952 | * contains an SCT X509 extension. They will be stored in |s->scts|. | |
3953 | * Returns: | |
3954 | * - The number of SCTs extracted, assuming an OCSP response exists. | |
3955 | * - 0 if no OCSP response exists or it contains no SCTs. | |
3956 | * - A negative integer if an error occurs. | |
3957 | */ | |
3958 | static int ct_extract_ocsp_response_scts(SSL *s) | |
3959 | { | |
3960 | int scts_extracted = 0; | |
3961 | const unsigned char *p; | |
3962 | OCSP_BASICRESP *br = NULL; | |
3963 | OCSP_RESPONSE *rsp = NULL; | |
3964 | STACK_OF(SCT) *scts = NULL; | |
3965 | int i; | |
3966 | ||
3967 | if (s->tlsext_ocsp_resp == NULL || s->tlsext_ocsp_resplen == 0) | |
3968 | goto err; | |
3969 | ||
3970 | p = s->tlsext_ocsp_resp; | |
3971 | rsp = d2i_OCSP_RESPONSE(NULL, &p, s->tlsext_ocsp_resplen); | |
3972 | if (rsp == NULL) | |
3973 | goto err; | |
3974 | ||
3975 | br = OCSP_response_get1_basic(rsp); | |
3976 | if (br == NULL) | |
3977 | goto err; | |
3978 | ||
3979 | for (i = 0; i < OCSP_resp_count(br); ++i) { | |
3980 | OCSP_SINGLERESP *single = OCSP_resp_get0(br, i); | |
3981 | ||
3982 | if (single == NULL) | |
3983 | continue; | |
3984 | ||
3985 | scts = OCSP_SINGLERESP_get1_ext_d2i(single, NID_ct_cert_scts, NULL, NULL); | |
3986 | scts_extracted = ct_move_scts(&s->scts, scts, | |
3987 | SCT_SOURCE_OCSP_STAPLED_RESPONSE); | |
3988 | if (scts_extracted < 0) | |
3989 | goto err; | |
3990 | } | |
3991 | err: | |
3992 | SCT_LIST_free(scts); | |
3993 | OCSP_BASICRESP_free(br); | |
3994 | OCSP_RESPONSE_free(rsp); | |
3995 | return scts_extracted; | |
3996 | } | |
3997 | ||
3998 | /* | |
3999 | * Attempts to extract SCTs from the peer certificate. | |
4000 | * Return the number of SCTs extracted, or a negative integer if an error | |
4001 | * occurs. | |
4002 | */ | |
4003 | static int ct_extract_x509v3_extension_scts(SSL *s) | |
4004 | { | |
4005 | int scts_extracted = 0; | |
3f3c7d26 | 4006 | X509 *cert = s->session != NULL ? s->session->peer : NULL; |
ed29e82a RP |
4007 | |
4008 | if (cert != NULL) { | |
4009 | STACK_OF(SCT) *scts = | |
4010 | X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL); | |
4011 | ||
4012 | scts_extracted = | |
4013 | ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION); | |
4014 | ||
4015 | SCT_LIST_free(scts); | |
4016 | } | |
4017 | ||
4018 | return scts_extracted; | |
4019 | } | |
4020 | ||
4021 | /* | |
4022 | * Attempts to find all received SCTs by checking TLS extensions, the OCSP | |
4023 | * response (if it exists) and X509v3 extensions in the certificate. | |
4024 | * Returns NULL if an error occurs. | |
4025 | */ | |
4026 | const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s) | |
4027 | { | |
4028 | if (!s->scts_parsed) { | |
4029 | if (ct_extract_tls_extension_scts(s) < 0 || | |
4030 | ct_extract_ocsp_response_scts(s) < 0 || | |
4031 | ct_extract_x509v3_extension_scts(s) < 0) | |
4032 | goto err; | |
4033 | ||
4034 | s->scts_parsed = 1; | |
4035 | } | |
4036 | return s->scts; | |
4037 | err: | |
4038 | return NULL; | |
4039 | } | |
4040 | ||
4041 | int SSL_set_ct_validation_callback(SSL *s, ct_validation_cb callback, void *arg) | |
4042 | { | |
4043 | int ret = 0; | |
4044 | ||
4045 | /* | |
4046 | * Since code exists that uses the custom extension handler for CT, look | |
4047 | * for this and throw an error if they have already registered to use CT. | |
4048 | */ | |
4049 | if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx, | |
4050 | TLSEXT_TYPE_signed_certificate_timestamp)) { | |
4051 | SSLerr(SSL_F_SSL_SET_CT_VALIDATION_CALLBACK, | |
4052 | SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED); | |
4053 | goto err; | |
4054 | } | |
4055 | ||
4056 | s->ct_validation_callback = callback; | |
4057 | s->ct_validation_callback_arg = arg; | |
4058 | ||
4059 | if (callback != NULL) { | |
4060 | /* If we are validating CT, then we MUST accept SCTs served via OCSP */ | |
4061 | if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp)) | |
4062 | goto err; | |
4063 | } | |
4064 | ||
4065 | ret = 1; | |
4066 | err: | |
4067 | return ret; | |
4068 | } | |
4069 | ||
4070 | int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx, ct_validation_cb callback, | |
4071 | void *arg) | |
4072 | { | |
4073 | int ret = 0; | |
4074 | ||
4075 | /* | |
4076 | * Since code exists that uses the custom extension handler for CT, look for | |
4077 | * this and throw an error if they have already registered to use CT. | |
4078 | */ | |
4079 | if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx, | |
4080 | TLSEXT_TYPE_signed_certificate_timestamp)) { | |
4081 | SSLerr(SSL_F_SSL_CTX_SET_CT_VALIDATION_CALLBACK, | |
4082 | SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED); | |
4083 | goto err; | |
4084 | } | |
4085 | ||
4086 | ctx->ct_validation_callback = callback; | |
4087 | ctx->ct_validation_callback_arg = arg; | |
4088 | ret = 1; | |
4089 | err: | |
4090 | return ret; | |
4091 | } | |
4092 | ||
4093 | ct_validation_cb SSL_get_ct_validation_callback(const SSL *s) | |
4094 | { | |
4095 | return s->ct_validation_callback; | |
4096 | } | |
4097 | ||
4098 | ct_validation_cb SSL_CTX_get_ct_validation_callback(const SSL_CTX *ctx) | |
4099 | { | |
4100 | return ctx->ct_validation_callback; | |
4101 | } | |
4102 | ||
4d482ee2 | 4103 | int ssl_validate_ct(SSL *s) |
ed29e82a RP |
4104 | { |
4105 | int ret = 0; | |
3f3c7d26 | 4106 | X509 *cert = s->session != NULL ? s->session->peer : NULL; |
ed29e82a RP |
4107 | X509 *issuer = NULL; |
4108 | CT_POLICY_EVAL_CTX *ctx = NULL; | |
4109 | const STACK_OF(SCT) *scts; | |
4110 | ||
4111 | /* If no callback is set, attempt no validation - just return success */ | |
4112 | if (s->ct_validation_callback == NULL) | |
4113 | return 1; | |
4114 | ||
4115 | if (cert == NULL) { | |
4116 | SSLerr(SSL_F_SSL_VALIDATE_CT, SSL_R_NO_CERTIFICATE_ASSIGNED); | |
4117 | goto end; | |
4118 | } | |
4119 | ||
4120 | if (s->verified_chain != NULL && sk_X509_num(s->verified_chain) > 1) | |
4121 | issuer = sk_X509_value(s->verified_chain, 1); | |
4122 | ||
4123 | ctx = CT_POLICY_EVAL_CTX_new(); | |
4124 | if (ctx == NULL) { | |
4125 | SSLerr(SSL_F_SSL_VALIDATE_CT, ERR_R_MALLOC_FAILURE); | |
4126 | goto end; | |
4127 | } | |
4128 | ||
4129 | CT_POLICY_EVAL_CTX_set0_cert(ctx, cert); | |
4130 | CT_POLICY_EVAL_CTX_set0_issuer(ctx, issuer); | |
4131 | CT_POLICY_EVAL_CTX_set0_log_store(ctx, s->ctx->ctlog_store); | |
4132 | ||
4133 | scts = SSL_get0_peer_scts(s); | |
4134 | ||
4135 | if (SCT_LIST_validate(scts, ctx) != 1) { | |
4136 | SSLerr(SSL_F_SSL_VALIDATE_CT, SSL_R_SCT_VERIFICATION_FAILED); | |
4137 | goto end; | |
4138 | } | |
4139 | ||
4140 | ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg); | |
4141 | if (ret < 0) | |
4142 | ret = 0; /* This function returns 0 on failure */ | |
4143 | ||
4144 | end: | |
4145 | CT_POLICY_EVAL_CTX_free(ctx); | |
4146 | return ret; | |
4147 | } | |
4148 | ||
4149 | int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx) | |
4150 | { | |
328f36c5 | 4151 | return CTLOG_STORE_load_default_file(ctx->ctlog_store); |
ed29e82a RP |
4152 | } |
4153 | ||
4154 | int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path) | |
4155 | { | |
4156 | return CTLOG_STORE_load_file(ctx->ctlog_store, path); | |
4157 | } | |
4158 | ||
4159 | #endif |