]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
fecb3aae | 2 | * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. |
aa8f3d76 | 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
c80149d9 | 4 | * Copyright 2005 Nokia. All rights reserved. |
bf21446a | 5 | * |
2c18d164 | 6 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
7 | * this file except in compliance with the License. You can obtain a copy |
8 | * in the file LICENSE in the source distribution or at | |
9 | * https://www.openssl.org/source/license.html | |
bf21446a | 10 | */ |
846e33c7 | 11 | |
d02b48c6 | 12 | #include <stdio.h> |
706457b7 | 13 | #include "ssl_local.h" |
d5f9166b | 14 | #include "internal/e_os.h" |
ec577822 | 15 | #include <openssl/objects.h> |
bb7cd4e3 | 16 | #include <openssl/x509v3.h> |
6434abbf | 17 | #include <openssl/rand.h> |
67c8e7f4 | 18 | #include <openssl/ocsp.h> |
3c27208f RS |
19 | #include <openssl/dh.h> |
20 | #include <openssl/engine.h> | |
07bbc92c | 21 | #include <openssl/async.h> |
3c27208f | 22 | #include <openssl/ct.h> |
77359d22 | 23 | #include <openssl/trace.h> |
4566dae7 | 24 | #include <openssl/core_names.h> |
67dc995e | 25 | #include "internal/cryptlib.h" |
cd420b0b | 26 | #include "internal/refcount.h" |
50ec7505 | 27 | #include "internal/ktls.h" |
0f113f3e | 28 | |
38b051a1 TM |
29 | static int ssl_undefined_function_1(SSL_CONNECTION *sc, SSL3_RECORD *r, size_t s, |
30 | int t, SSL_MAC_BUF *mac, size_t macsize) | |
fce78bd4 | 31 | { |
38b051a1 | 32 | return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc)); |
fce78bd4 BE |
33 | } |
34 | ||
38b051a1 TM |
35 | static int ssl_undefined_function_2(SSL_CONNECTION *sc, SSL3_RECORD *r, |
36 | unsigned char *s, int t) | |
fce78bd4 | 37 | { |
38b051a1 | 38 | return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc)); |
fce78bd4 BE |
39 | } |
40 | ||
38b051a1 | 41 | static int ssl_undefined_function_3(SSL_CONNECTION *sc, unsigned char *r, |
fce78bd4 BE |
42 | unsigned char *s, size_t t, size_t *u) |
43 | { | |
38b051a1 | 44 | return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc)); |
fce78bd4 BE |
45 | } |
46 | ||
38b051a1 | 47 | static int ssl_undefined_function_4(SSL_CONNECTION *sc, int r) |
fce78bd4 | 48 | { |
38b051a1 | 49 | return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc)); |
fce78bd4 BE |
50 | } |
51 | ||
38b051a1 TM |
52 | static size_t ssl_undefined_function_5(SSL_CONNECTION *sc, const char *r, |
53 | size_t s, unsigned char *t) | |
fce78bd4 | 54 | { |
38b051a1 | 55 | return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc)); |
fce78bd4 BE |
56 | } |
57 | ||
58 | static int ssl_undefined_function_6(int r) | |
59 | { | |
fce78bd4 BE |
60 | return ssl_undefined_function(NULL); |
61 | } | |
62 | ||
38b051a1 TM |
63 | static int ssl_undefined_function_7(SSL_CONNECTION *sc, unsigned char *r, |
64 | size_t s, const char *t, size_t u, | |
fce78bd4 BE |
65 | const unsigned char *v, size_t w, int x) |
66 | { | |
38b051a1 TM |
67 | return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc)); |
68 | } | |
69 | ||
70 | static int ssl_undefined_function_8(SSL_CONNECTION *sc) | |
71 | { | |
72 | return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc)); | |
fce78bd4 BE |
73 | } |
74 | ||
0f113f3e | 75 | SSL3_ENC_METHOD ssl3_undef_enc_method = { |
fce78bd4 BE |
76 | ssl_undefined_function_1, |
77 | ssl_undefined_function_2, | |
38b051a1 | 78 | ssl_undefined_function_8, |
fce78bd4 BE |
79 | ssl_undefined_function_3, |
80 | ssl_undefined_function_4, | |
81 | ssl_undefined_function_5, | |
0f113f3e MC |
82 | NULL, /* client_finished_label */ |
83 | 0, /* client_finished_label_len */ | |
84 | NULL, /* server_finished_label */ | |
85 | 0, /* server_finished_label_len */ | |
fce78bd4 BE |
86 | ssl_undefined_function_6, |
87 | ssl_undefined_function_7, | |
0f113f3e | 88 | }; |
d02b48c6 | 89 | |
07bbc92c MC |
90 | struct ssl_async_args { |
91 | SSL *s; | |
92 | void *buf; | |
348240c6 | 93 | size_t num; |
a230b26e | 94 | enum { READFUNC, WRITEFUNC, OTHERFUNC } type; |
add2f5ca | 95 | union { |
eda75751 | 96 | int (*func_read) (SSL *, void *, size_t, size_t *); |
7ee8627f | 97 | int (*func_write) (SSL *, const void *, size_t, size_t *); |
a230b26e | 98 | int (*func_other) (SSL *); |
add2f5ca | 99 | } f; |
07bbc92c MC |
100 | }; |
101 | ||
919ba009 VD |
102 | static const struct { |
103 | uint8_t mtype; | |
104 | uint8_t ord; | |
a230b26e | 105 | int nid; |
919ba009 | 106 | } dane_mds[] = { |
a230b26e EK |
107 | { |
108 | DANETLS_MATCHING_FULL, 0, NID_undef | |
109 | }, | |
110 | { | |
111 | DANETLS_MATCHING_2256, 1, NID_sha256 | |
112 | }, | |
113 | { | |
114 | DANETLS_MATCHING_2512, 2, NID_sha512 | |
115 | }, | |
919ba009 VD |
116 | }; |
117 | ||
118 | static int dane_ctx_enable(struct dane_ctx_st *dctx) | |
119 | { | |
120 | const EVP_MD **mdevp; | |
121 | uint8_t *mdord; | |
122 | uint8_t mdmax = DANETLS_MATCHING_LAST; | |
a230b26e | 123 | int n = ((int)mdmax) + 1; /* int to handle PrivMatch(255) */ |
919ba009 VD |
124 | size_t i; |
125 | ||
5ae4ceb9 VD |
126 | if (dctx->mdevp != NULL) |
127 | return 1; | |
128 | ||
919ba009 VD |
129 | mdevp = OPENSSL_zalloc(n * sizeof(*mdevp)); |
130 | mdord = OPENSSL_zalloc(n * sizeof(*mdord)); | |
131 | ||
132 | if (mdord == NULL || mdevp == NULL) { | |
b3bd3d5a | 133 | OPENSSL_free(mdord); |
919ba009 | 134 | OPENSSL_free(mdevp); |
6849b73c | 135 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
919ba009 VD |
136 | return 0; |
137 | } | |
138 | ||
139 | /* Install default entries */ | |
140 | for (i = 0; i < OSSL_NELEM(dane_mds); ++i) { | |
141 | const EVP_MD *md; | |
142 | ||
143 | if (dane_mds[i].nid == NID_undef || | |
144 | (md = EVP_get_digestbynid(dane_mds[i].nid)) == NULL) | |
145 | continue; | |
146 | mdevp[dane_mds[i].mtype] = md; | |
147 | mdord[dane_mds[i].mtype] = dane_mds[i].ord; | |
148 | } | |
149 | ||
150 | dctx->mdevp = mdevp; | |
151 | dctx->mdord = mdord; | |
152 | dctx->mdmax = mdmax; | |
153 | ||
154 | return 1; | |
155 | } | |
156 | ||
157 | static void dane_ctx_final(struct dane_ctx_st *dctx) | |
158 | { | |
159 | OPENSSL_free(dctx->mdevp); | |
160 | dctx->mdevp = NULL; | |
161 | ||
162 | OPENSSL_free(dctx->mdord); | |
163 | dctx->mdord = NULL; | |
164 | dctx->mdmax = 0; | |
165 | } | |
166 | ||
167 | static void tlsa_free(danetls_record *t) | |
168 | { | |
169 | if (t == NULL) | |
170 | return; | |
171 | OPENSSL_free(t->data); | |
172 | EVP_PKEY_free(t->spki); | |
173 | OPENSSL_free(t); | |
174 | } | |
175 | ||
b9aec69a | 176 | static void dane_final(SSL_DANE *dane) |
919ba009 VD |
177 | { |
178 | sk_danetls_record_pop_free(dane->trecs, tlsa_free); | |
179 | dane->trecs = NULL; | |
180 | ||
79b2a2f2 | 181 | OSSL_STACK_OF_X509_free(dane->certs); |
919ba009 VD |
182 | dane->certs = NULL; |
183 | ||
184 | X509_free(dane->mcert); | |
185 | dane->mcert = NULL; | |
186 | dane->mtlsa = NULL; | |
187 | dane->mdpth = -1; | |
188 | dane->pdpth = -1; | |
189 | } | |
190 | ||
191 | /* | |
192 | * dane_copy - Copy dane configuration, sans verification state. | |
193 | */ | |
38b051a1 | 194 | static int ssl_dane_dup(SSL_CONNECTION *to, SSL_CONNECTION *from) |
919ba009 VD |
195 | { |
196 | int num; | |
197 | int i; | |
198 | ||
199 | if (!DANETLS_ENABLED(&from->dane)) | |
200 | return 1; | |
201 | ||
e431363f | 202 | num = sk_danetls_record_num(from->dane.trecs); |
919ba009 | 203 | dane_final(&to->dane); |
5ae4ceb9 | 204 | to->dane.flags = from->dane.flags; |
38b051a1 | 205 | to->dane.dctx = &SSL_CONNECTION_GET_CTX(to)->dane; |
7a908204 | 206 | to->dane.trecs = sk_danetls_record_new_reserve(NULL, num); |
9f6b22b8 VD |
207 | |
208 | if (to->dane.trecs == NULL) { | |
6849b73c | 209 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
9f6b22b8 VD |
210 | return 0; |
211 | } | |
919ba009 | 212 | |
919ba009 VD |
213 | for (i = 0; i < num; ++i) { |
214 | danetls_record *t = sk_danetls_record_value(from->dane.trecs, i); | |
9f6b22b8 | 215 | |
38b051a1 TM |
216 | if (SSL_dane_tlsa_add(SSL_CONNECTION_GET_SSL(to), t->usage, |
217 | t->selector, t->mtype, t->data, t->dlen) <= 0) | |
919ba009 VD |
218 | return 0; |
219 | } | |
220 | return 1; | |
221 | } | |
222 | ||
a230b26e EK |
223 | static int dane_mtype_set(struct dane_ctx_st *dctx, |
224 | const EVP_MD *md, uint8_t mtype, uint8_t ord) | |
919ba009 VD |
225 | { |
226 | int i; | |
227 | ||
228 | if (mtype == DANETLS_MATCHING_FULL && md != NULL) { | |
6849b73c | 229 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL); |
919ba009 VD |
230 | return 0; |
231 | } | |
232 | ||
233 | if (mtype > dctx->mdmax) { | |
234 | const EVP_MD **mdevp; | |
235 | uint8_t *mdord; | |
a230b26e | 236 | int n = ((int)mtype) + 1; |
919ba009 VD |
237 | |
238 | mdevp = OPENSSL_realloc(dctx->mdevp, n * sizeof(*mdevp)); | |
239 | if (mdevp == NULL) { | |
6849b73c | 240 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
919ba009 VD |
241 | return -1; |
242 | } | |
243 | dctx->mdevp = mdevp; | |
244 | ||
245 | mdord = OPENSSL_realloc(dctx->mdord, n * sizeof(*mdord)); | |
246 | if (mdord == NULL) { | |
6849b73c | 247 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
919ba009 VD |
248 | return -1; |
249 | } | |
250 | dctx->mdord = mdord; | |
251 | ||
252 | /* Zero-fill any gaps */ | |
a230b26e | 253 | for (i = dctx->mdmax + 1; i < mtype; ++i) { |
919ba009 VD |
254 | mdevp[i] = NULL; |
255 | mdord[i] = 0; | |
256 | } | |
257 | ||
258 | dctx->mdmax = mtype; | |
259 | } | |
260 | ||
261 | dctx->mdevp[mtype] = md; | |
262 | /* Coerce ordinal of disabled matching types to 0 */ | |
263 | dctx->mdord[mtype] = (md == NULL) ? 0 : ord; | |
264 | ||
265 | return 1; | |
266 | } | |
267 | ||
b9aec69a | 268 | static const EVP_MD *tlsa_md_get(SSL_DANE *dane, uint8_t mtype) |
919ba009 VD |
269 | { |
270 | if (mtype > dane->dctx->mdmax) | |
271 | return NULL; | |
272 | return dane->dctx->mdevp[mtype]; | |
273 | } | |
274 | ||
a230b26e EK |
275 | static int dane_tlsa_add(SSL_DANE *dane, |
276 | uint8_t usage, | |
277 | uint8_t selector, | |
6d4313f0 | 278 | uint8_t mtype, const unsigned char *data, size_t dlen) |
919ba009 VD |
279 | { |
280 | danetls_record *t; | |
281 | const EVP_MD *md = NULL; | |
282 | int ilen = (int)dlen; | |
283 | int i; | |
9f6b22b8 | 284 | int num; |
919ba009 VD |
285 | |
286 | if (dane->trecs == NULL) { | |
6849b73c | 287 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_NOT_ENABLED); |
919ba009 VD |
288 | return -1; |
289 | } | |
290 | ||
291 | if (ilen < 0 || dlen != (size_t)ilen) { | |
6849b73c | 292 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DATA_LENGTH); |
919ba009 VD |
293 | return 0; |
294 | } | |
295 | ||
296 | if (usage > DANETLS_USAGE_LAST) { | |
6849b73c | 297 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE); |
919ba009 VD |
298 | return 0; |
299 | } | |
300 | ||
301 | if (selector > DANETLS_SELECTOR_LAST) { | |
6849b73c | 302 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_SELECTOR); |
919ba009 VD |
303 | return 0; |
304 | } | |
305 | ||
306 | if (mtype != DANETLS_MATCHING_FULL) { | |
307 | md = tlsa_md_get(dane, mtype); | |
308 | if (md == NULL) { | |
6849b73c | 309 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE); |
919ba009 VD |
310 | return 0; |
311 | } | |
312 | } | |
313 | ||
ed576acd | 314 | if (md != NULL && dlen != (size_t)EVP_MD_get_size(md)) { |
6849b73c | 315 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH); |
919ba009 VD |
316 | return 0; |
317 | } | |
318 | if (!data) { | |
6849b73c | 319 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_NULL_DATA); |
919ba009 VD |
320 | return 0; |
321 | } | |
322 | ||
323 | if ((t = OPENSSL_zalloc(sizeof(*t))) == NULL) { | |
6849b73c | 324 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
919ba009 VD |
325 | return -1; |
326 | } | |
327 | ||
328 | t->usage = usage; | |
329 | t->selector = selector; | |
330 | t->mtype = mtype; | |
348240c6 | 331 | t->data = OPENSSL_malloc(dlen); |
919ba009 VD |
332 | if (t->data == NULL) { |
333 | tlsa_free(t); | |
6849b73c | 334 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
919ba009 VD |
335 | return -1; |
336 | } | |
348240c6 MC |
337 | memcpy(t->data, data, dlen); |
338 | t->dlen = dlen; | |
919ba009 VD |
339 | |
340 | /* Validate and cache full certificate or public key */ | |
341 | if (mtype == DANETLS_MATCHING_FULL) { | |
342 | const unsigned char *p = data; | |
343 | X509 *cert = NULL; | |
344 | EVP_PKEY *pkey = NULL; | |
345 | ||
346 | switch (selector) { | |
347 | case DANETLS_SELECTOR_CERT: | |
348240c6 | 348 | if (!d2i_X509(&cert, &p, ilen) || p < data || |
919ba009 VD |
349 | dlen != (size_t)(p - data)) { |
350 | tlsa_free(t); | |
6849b73c | 351 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE); |
919ba009 VD |
352 | return 0; |
353 | } | |
354 | if (X509_get0_pubkey(cert) == NULL) { | |
355 | tlsa_free(t); | |
6849b73c | 356 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE); |
919ba009 VD |
357 | return 0; |
358 | } | |
359 | ||
360 | if ((DANETLS_USAGE_BIT(usage) & DANETLS_TA_MASK) == 0) { | |
361 | X509_free(cert); | |
362 | break; | |
363 | } | |
364 | ||
365 | /* | |
366 | * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA | |
367 | * records that contain full certificates of trust-anchors that are | |
368 | * not present in the wire chain. For usage PKIX-TA(0), we augment | |
369 | * the chain with untrusted Full(0) certificates from DNS, in case | |
370 | * they are missing from the chain. | |
371 | */ | |
372 | if ((dane->certs == NULL && | |
373 | (dane->certs = sk_X509_new_null()) == NULL) || | |
374 | !sk_X509_push(dane->certs, cert)) { | |
6849b73c | 375 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
919ba009 VD |
376 | X509_free(cert); |
377 | tlsa_free(t); | |
378 | return -1; | |
379 | } | |
380 | break; | |
381 | ||
382 | case DANETLS_SELECTOR_SPKI: | |
348240c6 | 383 | if (!d2i_PUBKEY(&pkey, &p, ilen) || p < data || |
919ba009 VD |
384 | dlen != (size_t)(p - data)) { |
385 | tlsa_free(t); | |
6849b73c | 386 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY); |
919ba009 VD |
387 | return 0; |
388 | } | |
389 | ||
390 | /* | |
391 | * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA | |
392 | * records that contain full bare keys of trust-anchors that are | |
393 | * not present in the wire chain. | |
394 | */ | |
395 | if (usage == DANETLS_USAGE_DANE_TA) | |
396 | t->spki = pkey; | |
397 | else | |
398 | EVP_PKEY_free(pkey); | |
399 | break; | |
400 | } | |
401 | } | |
402 | ||
403 | /*- | |
404 | * Find the right insertion point for the new record. | |
405 | * | |
406 | * See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that | |
407 | * they can be processed first, as they require no chain building, and no | |
408 | * expiration or hostname checks. Because DANE-EE(3) is numerically | |
409 | * largest, this is accomplished via descending sort by "usage". | |
410 | * | |
411 | * We also sort in descending order by matching ordinal to simplify | |
412 | * the implementation of digest agility in the verification code. | |
413 | * | |
414 | * The choice of order for the selector is not significant, so we | |
415 | * use the same descending order for consistency. | |
416 | */ | |
9f6b22b8 VD |
417 | num = sk_danetls_record_num(dane->trecs); |
418 | for (i = 0; i < num; ++i) { | |
919ba009 | 419 | danetls_record *rec = sk_danetls_record_value(dane->trecs, i); |
9f6b22b8 | 420 | |
919ba009 VD |
421 | if (rec->usage > usage) |
422 | continue; | |
423 | if (rec->usage < usage) | |
424 | break; | |
425 | if (rec->selector > selector) | |
426 | continue; | |
427 | if (rec->selector < selector) | |
428 | break; | |
429 | if (dane->dctx->mdord[rec->mtype] > dane->dctx->mdord[mtype]) | |
430 | continue; | |
431 | break; | |
432 | } | |
433 | ||
434 | if (!sk_danetls_record_insert(dane->trecs, t, i)) { | |
435 | tlsa_free(t); | |
6849b73c | 436 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
919ba009 VD |
437 | return -1; |
438 | } | |
439 | dane->umask |= DANETLS_USAGE_BIT(usage); | |
440 | ||
441 | return 1; | |
442 | } | |
443 | ||
c8feba72 BK |
444 | /* |
445 | * Return 0 if there is only one version configured and it was disabled | |
446 | * at configure time. Return 1 otherwise. | |
447 | */ | |
448 | static int ssl_check_allowed_versions(int min_version, int max_version) | |
449 | { | |
450 | int minisdtls = 0, maxisdtls = 0; | |
451 | ||
452 | /* Figure out if we're doing DTLS versions or TLS versions */ | |
453 | if (min_version == DTLS1_BAD_VER | |
454 | || min_version >> 8 == DTLS1_VERSION_MAJOR) | |
455 | minisdtls = 1; | |
456 | if (max_version == DTLS1_BAD_VER | |
457 | || max_version >> 8 == DTLS1_VERSION_MAJOR) | |
458 | maxisdtls = 1; | |
459 | /* A wildcard version of 0 could be DTLS or TLS. */ | |
460 | if ((minisdtls && !maxisdtls && max_version != 0) | |
461 | || (maxisdtls && !minisdtls && min_version != 0)) { | |
462 | /* Mixing DTLS and TLS versions will lead to sadness; deny it. */ | |
463 | return 0; | |
464 | } | |
465 | ||
466 | if (minisdtls || maxisdtls) { | |
467 | /* Do DTLS version checks. */ | |
468 | if (min_version == 0) | |
469 | /* Ignore DTLS1_BAD_VER */ | |
470 | min_version = DTLS1_VERSION; | |
471 | if (max_version == 0) | |
472 | max_version = DTLS1_2_VERSION; | |
473 | #ifdef OPENSSL_NO_DTLS1_2 | |
474 | if (max_version == DTLS1_2_VERSION) | |
475 | max_version = DTLS1_VERSION; | |
476 | #endif | |
477 | #ifdef OPENSSL_NO_DTLS1 | |
478 | if (min_version == DTLS1_VERSION) | |
479 | min_version = DTLS1_2_VERSION; | |
480 | #endif | |
79b4444d DMSP |
481 | /* Done massaging versions; do the check. */ |
482 | if (0 | |
c8feba72 BK |
483 | #ifdef OPENSSL_NO_DTLS1 |
484 | || (DTLS_VERSION_GE(min_version, DTLS1_VERSION) | |
485 | && DTLS_VERSION_GE(DTLS1_VERSION, max_version)) | |
486 | #endif | |
487 | #ifdef OPENSSL_NO_DTLS1_2 | |
488 | || (DTLS_VERSION_GE(min_version, DTLS1_2_VERSION) | |
489 | && DTLS_VERSION_GE(DTLS1_2_VERSION, max_version)) | |
490 | #endif | |
491 | ) | |
492 | return 0; | |
493 | } else { | |
494 | /* Regular TLS version checks. */ | |
79b4444d DMSP |
495 | if (min_version == 0) |
496 | min_version = SSL3_VERSION; | |
497 | if (max_version == 0) | |
498 | max_version = TLS1_3_VERSION; | |
c8feba72 | 499 | #ifdef OPENSSL_NO_TLS1_3 |
79b4444d DMSP |
500 | if (max_version == TLS1_3_VERSION) |
501 | max_version = TLS1_2_VERSION; | |
c8feba72 BK |
502 | #endif |
503 | #ifdef OPENSSL_NO_TLS1_2 | |
79b4444d DMSP |
504 | if (max_version == TLS1_2_VERSION) |
505 | max_version = TLS1_1_VERSION; | |
c8feba72 BK |
506 | #endif |
507 | #ifdef OPENSSL_NO_TLS1_1 | |
79b4444d DMSP |
508 | if (max_version == TLS1_1_VERSION) |
509 | max_version = TLS1_VERSION; | |
c8feba72 BK |
510 | #endif |
511 | #ifdef OPENSSL_NO_TLS1 | |
79b4444d DMSP |
512 | if (max_version == TLS1_VERSION) |
513 | max_version = SSL3_VERSION; | |
c8feba72 BK |
514 | #endif |
515 | #ifdef OPENSSL_NO_SSL3 | |
79b4444d DMSP |
516 | if (min_version == SSL3_VERSION) |
517 | min_version = TLS1_VERSION; | |
c8feba72 BK |
518 | #endif |
519 | #ifdef OPENSSL_NO_TLS1 | |
79b4444d DMSP |
520 | if (min_version == TLS1_VERSION) |
521 | min_version = TLS1_1_VERSION; | |
c8feba72 BK |
522 | #endif |
523 | #ifdef OPENSSL_NO_TLS1_1 | |
79b4444d DMSP |
524 | if (min_version == TLS1_1_VERSION) |
525 | min_version = TLS1_2_VERSION; | |
c8feba72 BK |
526 | #endif |
527 | #ifdef OPENSSL_NO_TLS1_2 | |
79b4444d DMSP |
528 | if (min_version == TLS1_2_VERSION) |
529 | min_version = TLS1_3_VERSION; | |
c8feba72 | 530 | #endif |
79b4444d DMSP |
531 | /* Done massaging versions; do the check. */ |
532 | if (0 | |
c8feba72 BK |
533 | #ifdef OPENSSL_NO_SSL3 |
534 | || (min_version <= SSL3_VERSION && SSL3_VERSION <= max_version) | |
535 | #endif | |
536 | #ifdef OPENSSL_NO_TLS1 | |
537 | || (min_version <= TLS1_VERSION && TLS1_VERSION <= max_version) | |
538 | #endif | |
539 | #ifdef OPENSSL_NO_TLS1_1 | |
540 | || (min_version <= TLS1_1_VERSION && TLS1_1_VERSION <= max_version) | |
541 | #endif | |
542 | #ifdef OPENSSL_NO_TLS1_2 | |
543 | || (min_version <= TLS1_2_VERSION && TLS1_2_VERSION <= max_version) | |
544 | #endif | |
545 | #ifdef OPENSSL_NO_TLS1_3 | |
546 | || (min_version <= TLS1_3_VERSION && TLS1_3_VERSION <= max_version) | |
547 | #endif | |
548 | ) | |
549 | return 0; | |
550 | } | |
551 | return 1; | |
552 | } | |
553 | ||
08073700 RB |
554 | #if defined(__TANDEM) && defined(OPENSSL_VPROC) |
555 | /* | |
556 | * Define a VPROC function for HP NonStop build ssl library. | |
557 | * This is used by platform version identification tools. | |
558 | * Do not inline this procedure or make it static. | |
559 | */ | |
560 | # define OPENSSL_VPROC_STRING_(x) x##_SSL | |
561 | # define OPENSSL_VPROC_STRING(x) OPENSSL_VPROC_STRING_(x) | |
562 | # define OPENSSL_VPROC_FUNC OPENSSL_VPROC_STRING(OPENSSL_VPROC) | |
563 | void OPENSSL_VPROC_FUNC(void) {} | |
564 | #endif | |
565 | ||
566 | ||
38b051a1 | 567 | static void clear_ciphers(SSL_CONNECTION *s) |
d31fb0b5 RS |
568 | { |
569 | /* clear the current cipher */ | |
570 | ssl_clear_cipher_ctx(s); | |
571 | ssl_clear_hash_ctx(&s->read_hash); | |
572 | ssl_clear_hash_ctx(&s->write_hash); | |
573 | } | |
574 | ||
4f43d0e7 | 575 | int SSL_clear(SSL *s) |
0f113f3e | 576 | { |
0f113f3e | 577 | if (s->method == NULL) { |
6849b73c | 578 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_METHOD_SPECIFIED); |
a89325e4 | 579 | return 0; |
0f113f3e | 580 | } |
d02b48c6 | 581 | |
38b051a1 TM |
582 | return s->method->ssl_reset(s); |
583 | } | |
584 | ||
585 | int ossl_ssl_connection_reset(SSL *s) | |
586 | { | |
587 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); | |
588 | ||
589 | if (sc == NULL) | |
590 | return 0; | |
591 | ||
592 | if (ssl_clear_bad_session(sc)) { | |
593 | SSL_SESSION_free(sc->session); | |
594 | sc->session = NULL; | |
0f113f3e | 595 | } |
38b051a1 TM |
596 | SSL_SESSION_free(sc->psksession); |
597 | sc->psksession = NULL; | |
598 | OPENSSL_free(sc->psksession_id); | |
599 | sc->psksession_id = NULL; | |
600 | sc->psksession_id_len = 0; | |
601 | sc->hello_retry_request = 0; | |
602 | sc->sent_tickets = 0; | |
d62bfb39 | 603 | |
38b051a1 TM |
604 | sc->error = 0; |
605 | sc->hit = 0; | |
606 | sc->shutdown = 0; | |
d02b48c6 | 607 | |
38b051a1 | 608 | if (sc->renegotiate) { |
6849b73c | 609 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
0f113f3e MC |
610 | return 0; |
611 | } | |
d02b48c6 | 612 | |
38b051a1 | 613 | ossl_statem_clear(sc); |
413c4f45 | 614 | |
38b051a1 TM |
615 | /* TODO(QUIC): Version handling not yet clear */ |
616 | sc->version = s->method->version; | |
617 | sc->client_version = sc->version; | |
618 | sc->rwstate = SSL_NOTHING; | |
d02b48c6 | 619 | |
38b051a1 TM |
620 | BUF_MEM_free(sc->init_buf); |
621 | sc->init_buf = NULL; | |
622 | clear_ciphers(sc); | |
623 | sc->first_packet = 0; | |
d02b48c6 | 624 | |
38b051a1 | 625 | sc->key_update = SSL_KEY_UPDATE_NONE; |
44c04a2e | 626 | |
38b051a1 TM |
627 | EVP_MD_CTX_free(sc->pha_dgst); |
628 | sc->pha_dgst = NULL; | |
88834998 | 629 | |
919ba009 | 630 | /* Reset DANE verification result state */ |
38b051a1 TM |
631 | sc->dane.mdpth = -1; |
632 | sc->dane.pdpth = -1; | |
633 | X509_free(sc->dane.mcert); | |
634 | sc->dane.mcert = NULL; | |
635 | sc->dane.mtlsa = NULL; | |
919ba009 VD |
636 | |
637 | /* Clear the verification result peername */ | |
38b051a1 | 638 | X509_VERIFY_PARAM_move_peername(sc->param, NULL); |
919ba009 | 639 | |
29948ac8 | 640 | /* Clear any shared connection state */ |
38b051a1 TM |
641 | OPENSSL_free(sc->shared_sigalgs); |
642 | sc->shared_sigalgs = NULL; | |
643 | sc->shared_sigalgslen = 0; | |
29948ac8 | 644 | |
0f113f3e MC |
645 | /* |
646 | * Check to see if we were changed into a different method, if so, revert | |
24252537 | 647 | * back. |
0f113f3e | 648 | */ |
38b051a1 TM |
649 | if (s->method != SSL_CONNECTION_GET_CTX(sc)->method) { |
650 | s->method->ssl_deinit(s); | |
651 | s->method = SSL_CONNECTION_GET_CTX(sc)->method; | |
652 | if (!s->method->ssl_init(s)) | |
a89325e4 | 653 | return 0; |
b77f3ed1 MC |
654 | } else { |
655 | if (!s->method->ssl_clear(s)) | |
656 | return 0; | |
657 | } | |
33d23b87 | 658 | |
38b051a1 | 659 | RECORD_LAYER_clear(&sc->rlayer); |
cffafb5f MC |
660 | BIO_free(sc->rlayer.rrlnext); |
661 | sc->rlayer.rrlnext = NULL; | |
33d23b87 | 662 | |
79eebb08 | 663 | if (!ssl_set_new_record_layer(sc, |
222cf410 | 664 | SSL_CONNECTION_IS_DTLS(sc) ? DTLS_ANY_VERSION : TLS_ANY_VERSION, |
79eebb08 MC |
665 | OSSL_RECORD_DIRECTION_READ, |
666 | OSSL_RECORD_PROTECTION_LEVEL_NONE, | |
667 | NULL, 0, NULL, 0, NULL, 0, NULL, 0, | |
668 | NID_undef, NULL, NULL)) { | |
7c293999 | 669 | /* SSLfatal already called */ |
aedbb71b MC |
670 | return 0; |
671 | } | |
2b71b042 MC |
672 | if (!ssl_set_new_record_layer(sc, |
673 | SSL_CONNECTION_IS_DTLS(sc) ? DTLS_ANY_VERSION : TLS_ANY_VERSION, | |
674 | OSSL_RECORD_DIRECTION_WRITE, | |
675 | OSSL_RECORD_PROTECTION_LEVEL_NONE, | |
676 | NULL, 0, NULL, 0, NULL, 0, NULL, 0, | |
677 | NID_undef, NULL, NULL)) { | |
678 | /* SSLfatal already called */ | |
679 | return 0; | |
680 | } | |
aedbb71b | 681 | |
a89325e4 | 682 | return 1; |
0f113f3e | 683 | } |
d02b48c6 | 684 | |
dd0164e7 | 685 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4f43d0e7 | 686 | /** Used to change an SSL_CTXs default SSL method type */ |
0f113f3e MC |
687 | int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth) |
688 | { | |
689 | STACK_OF(SSL_CIPHER) *sk; | |
690 | ||
691 | ctx->method = meth; | |
692 | ||
5d120511 | 693 | if (!SSL_CTX_set_ciphersuites(ctx, OSSL_default_ciphersuites())) { |
6849b73c | 694 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS); |
2340ed27 BK |
695 | return 0; |
696 | } | |
a68eee67 | 697 | sk = ssl_create_cipher_list(ctx, |
f865b081 MC |
698 | ctx->tls13_ciphersuites, |
699 | &(ctx->cipher_list), | |
0f113f3e | 700 | &(ctx->cipher_list_by_id), |
5d120511 | 701 | OSSL_default_cipher_list(), ctx->cert); |
0f113f3e | 702 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) { |
6849b73c | 703 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS); |
26a7d938 | 704 | return 0; |
0f113f3e | 705 | } |
208fb891 | 706 | return 1; |
0f113f3e | 707 | } |
dd0164e7 | 708 | #endif |
d02b48c6 | 709 | |
4f43d0e7 | 710 | SSL *SSL_new(SSL_CTX *ctx) |
0f113f3e | 711 | { |
0f113f3e | 712 | if (ctx == NULL) { |
6849b73c | 713 | ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_CTX); |
26a7d938 | 714 | return NULL; |
0f113f3e MC |
715 | } |
716 | if (ctx->method == NULL) { | |
6849b73c | 717 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION); |
26a7d938 | 718 | return NULL; |
0f113f3e | 719 | } |
38b051a1 TM |
720 | return ctx->method->ssl_new(ctx); |
721 | } | |
722 | ||
723 | int ossl_ssl_init(SSL *ssl, SSL_CTX *ctx, int type) | |
724 | { | |
725 | ssl->type = type; | |
726 | ||
727 | ssl->references = 1; | |
728 | ssl->lock = CRYPTO_THREAD_lock_new(); | |
729 | if (ssl->lock == NULL) | |
730 | return 0; | |
731 | ||
732 | SSL_CTX_up_ref(ctx); | |
733 | ssl->ctx = ctx; | |
734 | ||
735 | ssl->method = ctx->method; | |
736 | ||
737 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, ssl, &ssl->ex_data)) | |
738 | return 0; | |
739 | ||
740 | return 1; | |
741 | } | |
742 | ||
743 | SSL *ossl_ssl_connection_new(SSL_CTX *ctx) | |
744 | { | |
745 | SSL_CONNECTION *s; | |
746 | SSL *ssl; | |
0f113f3e | 747 | |
b51bce94 | 748 | s = OPENSSL_zalloc(sizeof(*s)); |
0f113f3e | 749 | if (s == NULL) |
38b051a1 | 750 | return NULL; |
0f113f3e | 751 | |
38b051a1 TM |
752 | ssl = &s->ssl; |
753 | if (!ossl_ssl_init(ssl, ctx, SSL_TYPE_SSL_CONNECTION)) { | |
e6b10c34 BE |
754 | OPENSSL_free(s); |
755 | s = NULL; | |
ae3947de | 756 | goto err; |
e6b10c34 | 757 | } |
ae3947de | 758 | |
38b051a1 TM |
759 | #ifndef OPENSSL_NO_QUIC |
760 | /* set the parent (user visible) ssl to self */ | |
761 | s->user_ssl = ssl; | |
762 | #endif | |
763 | ||
c036e210 | 764 | RECORD_LAYER_init(&s->rlayer, s); |
28d59af8 | 765 | |
0f113f3e | 766 | s->options = ctx->options; |
5ae4ceb9 | 767 | s->dane.flags = ctx->dane.flags; |
7946ab33 KR |
768 | s->min_proto_version = ctx->min_proto_version; |
769 | s->max_proto_version = ctx->max_proto_version; | |
0f113f3e MC |
770 | s->mode = ctx->mode; |
771 | s->max_cert_list = ctx->max_cert_list; | |
3fc8d856 | 772 | s->max_early_data = ctx->max_early_data; |
4e8548e8 | 773 | s->recv_max_early_data = ctx->recv_max_early_data; |
9d0a8bb7 | 774 | s->num_tickets = ctx->num_tickets; |
e97be718 | 775 | s->pha_enabled = ctx->pha_enabled; |
0f113f3e | 776 | |
f865b081 MC |
777 | /* Shallow copy of the ciphersuites stack */ |
778 | s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites); | |
779 | if (s->tls13_ciphersuites == NULL) | |
780 | goto err; | |
781 | ||
2c382349 KR |
782 | /* |
783 | * Earlier library versions used to copy the pointer to the CERT, not | |
784 | * its contents; only when setting new parameters for the per-SSL | |
785 | * copy, ssl_cert_new would be called (and the direct reference to | |
786 | * the per-SSL_CTX settings would be lost, but those still were | |
787 | * indirectly accessed for various purposes, and for that reason they | |
788 | * used to be known as s->ctx->default_cert). Now we don't look at the | |
789 | * SSL_CTX's CERT after having duplicated it once. | |
790 | */ | |
791 | s->cert = ssl_cert_dup(ctx->cert); | |
792 | if (s->cert == NULL) | |
793 | goto err; | |
0f113f3e | 794 | |
52e1d7b1 | 795 | RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead); |
0f113f3e MC |
796 | s->msg_callback = ctx->msg_callback; |
797 | s->msg_callback_arg = ctx->msg_callback_arg; | |
798 | s->verify_mode = ctx->verify_mode; | |
799 | s->not_resumable_session_cb = ctx->not_resumable_session_cb; | |
eb7d6c2a MC |
800 | s->rlayer.record_padding_cb = ctx->record_padding_cb; |
801 | s->rlayer.record_padding_arg = ctx->record_padding_arg; | |
802 | s->rlayer.block_padding = ctx->block_padding; | |
0f113f3e | 803 | s->sid_ctx_length = ctx->sid_ctx_length; |
cbe29648 | 804 | if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx))) |
380a522f | 805 | goto err; |
0f113f3e MC |
806 | memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx)); |
807 | s->verify_callback = ctx->default_verify_callback; | |
808 | s->generate_session_id = ctx->generate_session_id; | |
809 | ||
810 | s->param = X509_VERIFY_PARAM_new(); | |
a71edf3b | 811 | if (s->param == NULL) |
0f113f3e MC |
812 | goto err; |
813 | X509_VERIFY_PARAM_inherit(s->param, ctx->param); | |
0f113f3e | 814 | s->quiet_shutdown = ctx->quiet_shutdown; |
cf72c757 F |
815 | |
816 | s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode; | |
0f113f3e | 817 | s->max_send_fragment = ctx->max_send_fragment; |
d102d9df MC |
818 | s->split_send_fragment = ctx->split_send_fragment; |
819 | s->max_pipelines = ctx->max_pipelines; | |
cffafb5f | 820 | s->rlayer.default_read_buf_len = ctx->default_read_buf_len; |
bf21446a | 821 | |
aff8c126 RS |
822 | s->ext.debug_cb = 0; |
823 | s->ext.debug_arg = NULL; | |
824 | s->ext.ticket_expected = 0; | |
825 | s->ext.status_type = ctx->ext.status_type; | |
826 | s->ext.status_expected = 0; | |
827 | s->ext.ocsp.ids = NULL; | |
828 | s->ext.ocsp.exts = NULL; | |
829 | s->ext.ocsp.resp = NULL; | |
830 | s->ext.ocsp.resp_len = 0; | |
16203f7b | 831 | SSL_CTX_up_ref(ctx); |
222da979 | 832 | s->session_ctx = ctx; |
aff8c126 RS |
833 | if (ctx->ext.ecpointformats) { |
834 | s->ext.ecpointformats = | |
835 | OPENSSL_memdup(ctx->ext.ecpointformats, | |
836 | ctx->ext.ecpointformats_len); | |
39a14059 MC |
837 | if (!s->ext.ecpointformats) { |
838 | s->ext.ecpointformats_len = 0; | |
0f113f3e | 839 | goto err; |
39a14059 | 840 | } |
aff8c126 RS |
841 | s->ext.ecpointformats_len = |
842 | ctx->ext.ecpointformats_len; | |
843 | } | |
844 | if (ctx->ext.supportedgroups) { | |
845 | s->ext.supportedgroups = | |
846 | OPENSSL_memdup(ctx->ext.supportedgroups, | |
9e84a42d | 847 | ctx->ext.supportedgroups_len |
b92d7b62 | 848 | * sizeof(*ctx->ext.supportedgroups)); |
39a14059 MC |
849 | if (!s->ext.supportedgroups) { |
850 | s->ext.supportedgroups_len = 0; | |
0f113f3e | 851 | goto err; |
39a14059 | 852 | } |
aff8c126 | 853 | s->ext.supportedgroups_len = ctx->ext.supportedgroups_len; |
0f113f3e | 854 | } |
dbc6268f | 855 | |
a230b26e | 856 | #ifndef OPENSSL_NO_NEXTPROTONEG |
aff8c126 | 857 | s->ext.npn = NULL; |
a230b26e | 858 | #endif |
6f017a8f | 859 | |
38b051a1 TM |
860 | if (ctx->ext.alpn != NULL) { |
861 | s->ext.alpn = OPENSSL_malloc(ctx->ext.alpn_len); | |
39a14059 MC |
862 | if (s->ext.alpn == NULL) { |
863 | s->ext.alpn_len = 0; | |
0f113f3e | 864 | goto err; |
39a14059 | 865 | } |
38b051a1 TM |
866 | memcpy(s->ext.alpn, ctx->ext.alpn, ctx->ext.alpn_len); |
867 | s->ext.alpn_len = ctx->ext.alpn_len; | |
0f113f3e | 868 | } |
d02b48c6 | 869 | |
696178ed | 870 | s->verified_chain = NULL; |
0f113f3e | 871 | s->verify_result = X509_V_OK; |
d02b48c6 | 872 | |
a974e64a MC |
873 | s->default_passwd_callback = ctx->default_passwd_callback; |
874 | s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata; | |
875 | ||
44c04a2e MC |
876 | s->key_update = SSL_KEY_UPDATE_NONE; |
877 | ||
c9598459 MC |
878 | s->allow_early_data_cb = ctx->allow_early_data_cb; |
879 | s->allow_early_data_cb_data = ctx->allow_early_data_cb_data; | |
880 | ||
38b051a1 | 881 | if (!ssl->method->ssl_init(ssl)) |
0f113f3e | 882 | goto err; |
d02b48c6 | 883 | |
0f113f3e | 884 | s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1; |
bf21446a | 885 | |
38b051a1 | 886 | if (!SSL_clear(ssl)) |
25a807bc | 887 | goto err; |
58964a49 | 888 | |
ddac1974 | 889 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
890 | s->psk_client_callback = ctx->psk_client_callback; |
891 | s->psk_server_callback = ctx->psk_server_callback; | |
ddac1974 | 892 | #endif |
f46184bd MC |
893 | s->psk_find_session_cb = ctx->psk_find_session_cb; |
894 | s->psk_use_session_cb = ctx->psk_use_session_cb; | |
ddac1974 | 895 | |
9f5a87fd PY |
896 | s->async_cb = ctx->async_cb; |
897 | s->async_cb_arg = ctx->async_cb_arg; | |
898 | ||
07bbc92c MC |
899 | s->job = NULL; |
900 | ||
ed29e82a | 901 | #ifndef OPENSSL_NO_CT |
38b051a1 | 902 | if (!SSL_set_ct_validation_callback(ssl, ctx->ct_validation_callback, |
a230b26e | 903 | ctx->ct_validation_callback_arg)) |
ed29e82a RP |
904 | goto err; |
905 | #endif | |
906 | ||
38b051a1 | 907 | return ssl; |
0f113f3e | 908 | err: |
38b051a1 | 909 | SSL_free(ssl); |
6849b73c | 910 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
16203f7b | 911 | return NULL; |
0f113f3e | 912 | } |
d02b48c6 | 913 | |
e417070c RS |
914 | int SSL_is_dtls(const SSL *s) |
915 | { | |
38b051a1 TM |
916 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
917 | ||
918 | if (sc == NULL) | |
919 | return 0; | |
920 | ||
921 | return SSL_CONNECTION_IS_DTLS(sc) ? 1 : 0; | |
e417070c RS |
922 | } |
923 | ||
c5ebfcab | 924 | int SSL_up_ref(SSL *s) |
a18a31e4 | 925 | { |
16203f7b | 926 | int i; |
c5ebfcab | 927 | |
2f545ae4 | 928 | if (CRYPTO_UP_REF(&s->references, &i, s->lock) <= 0) |
c5ebfcab F |
929 | return 0; |
930 | ||
931 | REF_PRINT_COUNT("SSL", s); | |
932 | REF_ASSERT_ISNT(i < 2); | |
933 | return ((i > 1) ? 1 : 0); | |
a18a31e4 MC |
934 | } |
935 | ||
0f113f3e MC |
936 | int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx, |
937 | unsigned int sid_ctx_len) | |
938 | { | |
fe9edc9d | 939 | if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { |
6849b73c | 940 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
0f113f3e MC |
941 | return 0; |
942 | } | |
943 | ctx->sid_ctx_length = sid_ctx_len; | |
944 | memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len); | |
4eb77b26 BM |
945 | |
946 | return 1; | |
0f113f3e | 947 | } |
4eb77b26 | 948 | |
0f113f3e MC |
949 | int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx, |
950 | unsigned int sid_ctx_len) | |
951 | { | |
38b051a1 TM |
952 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
953 | ||
954 | if (sc == NULL) | |
955 | return 0; | |
956 | ||
0f113f3e | 957 | if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { |
6849b73c | 958 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
0f113f3e MC |
959 | return 0; |
960 | } | |
38b051a1 TM |
961 | sc->sid_ctx_length = sid_ctx_len; |
962 | memcpy(sc->sid_ctx, sid_ctx, sid_ctx_len); | |
b4cadc6e BL |
963 | |
964 | return 1; | |
0f113f3e | 965 | } |
b4cadc6e | 966 | |
dc644fe2 | 967 | int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) |
0f113f3e | 968 | { |
cd3f8c1b RS |
969 | if (!CRYPTO_THREAD_write_lock(ctx->lock)) |
970 | return 0; | |
0f113f3e | 971 | ctx->generate_session_id = cb; |
16203f7b | 972 | CRYPTO_THREAD_unlock(ctx->lock); |
0f113f3e MC |
973 | return 1; |
974 | } | |
dc644fe2 GT |
975 | |
976 | int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) | |
0f113f3e | 977 | { |
38b051a1 TM |
978 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
979 | ||
980 | if (sc == NULL || !CRYPTO_THREAD_write_lock(ssl->lock)) | |
cd3f8c1b | 981 | return 0; |
38b051a1 | 982 | sc->generate_session_id = cb; |
16203f7b | 983 | CRYPTO_THREAD_unlock(ssl->lock); |
0f113f3e MC |
984 | return 1; |
985 | } | |
dc644fe2 | 986 | |
f85c9904 | 987 | int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id, |
0f113f3e MC |
988 | unsigned int id_len) |
989 | { | |
990 | /* | |
991 | * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how | |
69687aa8 | 992 | * we can "construct" a session to give us the desired check - i.e. to |
0f113f3e MC |
993 | * find if there's a session in the hash table that would conflict with |
994 | * any new session built out of this id/id_len and the ssl_version in use | |
995 | * by this SSL. | |
996 | */ | |
997 | SSL_SESSION r, *p; | |
38b051a1 | 998 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl); |
0f113f3e | 999 | |
38b051a1 | 1000 | if (sc == NULL || id_len > sizeof(r.session_id)) |
0f113f3e MC |
1001 | return 0; |
1002 | ||
38b051a1 | 1003 | r.ssl_version = sc->version; |
0f113f3e MC |
1004 | r.session_id_length = id_len; |
1005 | memcpy(r.session_id, id, id_len); | |
1006 | ||
38b051a1 | 1007 | if (!CRYPTO_THREAD_read_lock(sc->session_ctx->lock)) |
cd3f8c1b | 1008 | return 0; |
38b051a1 TM |
1009 | p = lh_SSL_SESSION_retrieve(sc->session_ctx->sessions, &r); |
1010 | CRYPTO_THREAD_unlock(sc->session_ctx->lock); | |
0f113f3e MC |
1011 | return (p != NULL); |
1012 | } | |
dc644fe2 | 1013 | |
bb7cd4e3 | 1014 | int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) |
0f113f3e MC |
1015 | { |
1016 | return X509_VERIFY_PARAM_set_purpose(s->param, purpose); | |
1017 | } | |
bb7cd4e3 DSH |
1018 | |
1019 | int SSL_set_purpose(SSL *s, int purpose) | |
0f113f3e | 1020 | { |
38b051a1 TM |
1021 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1022 | ||
1023 | if (sc == NULL) | |
1024 | return 0; | |
1025 | ||
1026 | return X509_VERIFY_PARAM_set_purpose(sc->param, purpose); | |
0f113f3e | 1027 | } |
926a56bf | 1028 | |
bb7cd4e3 | 1029 | int SSL_CTX_set_trust(SSL_CTX *s, int trust) |
0f113f3e MC |
1030 | { |
1031 | return X509_VERIFY_PARAM_set_trust(s->param, trust); | |
1032 | } | |
bb7cd4e3 DSH |
1033 | |
1034 | int SSL_set_trust(SSL *s, int trust) | |
0f113f3e | 1035 | { |
38b051a1 TM |
1036 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1037 | ||
1038 | if (sc == NULL) | |
1039 | return 0; | |
1040 | ||
1041 | return X509_VERIFY_PARAM_set_trust(sc->param, trust); | |
0f113f3e | 1042 | } |
bb7cd4e3 | 1043 | |
919ba009 VD |
1044 | int SSL_set1_host(SSL *s, const char *hostname) |
1045 | { | |
38b051a1 TM |
1046 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1047 | ||
1048 | if (sc == NULL) | |
1049 | return 0; | |
1050 | ||
c832840e DW |
1051 | /* If a hostname is provided and parses as an IP address, |
1052 | * treat it as such. */ | |
38b051a1 TM |
1053 | if (hostname != NULL |
1054 | && X509_VERIFY_PARAM_set1_ip_asc(sc->param, hostname) == 1) | |
c832840e DW |
1055 | return 1; |
1056 | ||
38b051a1 | 1057 | return X509_VERIFY_PARAM_set1_host(sc->param, hostname, 0); |
919ba009 VD |
1058 | } |
1059 | ||
1060 | int SSL_add1_host(SSL *s, const char *hostname) | |
1061 | { | |
38b051a1 TM |
1062 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1063 | ||
1064 | if (sc == NULL) | |
1065 | return 0; | |
1066 | ||
c832840e DW |
1067 | /* If a hostname is provided and parses as an IP address, |
1068 | * treat it as such. */ | |
892a9e4c DW |
1069 | if (hostname) |
1070 | { | |
1071 | ASN1_OCTET_STRING *ip; | |
1072 | char *old_ip; | |
1073 | ||
1074 | ip = a2i_IPADDRESS(hostname); | |
1075 | if (ip) { | |
1076 | /* We didn't want it; only to check if it *is* an IP address */ | |
1077 | ASN1_OCTET_STRING_free(ip); | |
1078 | ||
38b051a1 | 1079 | old_ip = X509_VERIFY_PARAM_get1_ip_asc(sc->param); |
892a9e4c DW |
1080 | if (old_ip) |
1081 | { | |
f2bfc53b | 1082 | OPENSSL_free(old_ip); |
892a9e4c DW |
1083 | /* There can be only one IP address */ |
1084 | return 0; | |
1085 | } | |
1086 | ||
38b051a1 | 1087 | return X509_VERIFY_PARAM_set1_ip_asc(sc->param, hostname); |
892a9e4c DW |
1088 | } |
1089 | } | |
c832840e | 1090 | |
38b051a1 | 1091 | return X509_VERIFY_PARAM_add1_host(sc->param, hostname, 0); |
919ba009 VD |
1092 | } |
1093 | ||
1094 | void SSL_set_hostflags(SSL *s, unsigned int flags) | |
1095 | { | |
38b051a1 TM |
1096 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1097 | ||
1098 | if (sc == NULL) | |
1099 | return; | |
1100 | ||
1101 | X509_VERIFY_PARAM_set_hostflags(sc->param, flags); | |
919ba009 VD |
1102 | } |
1103 | ||
4588cb44 | 1104 | const char *SSL_get0_peername(SSL *s) |
919ba009 | 1105 | { |
38b051a1 TM |
1106 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1107 | ||
1108 | if (sc == NULL) | |
1109 | return NULL; | |
1110 | ||
1111 | return X509_VERIFY_PARAM_get0_peername(sc->param); | |
919ba009 VD |
1112 | } |
1113 | ||
1114 | int SSL_CTX_dane_enable(SSL_CTX *ctx) | |
1115 | { | |
1116 | return dane_ctx_enable(&ctx->dane); | |
1117 | } | |
1118 | ||
5ae4ceb9 VD |
1119 | unsigned long SSL_CTX_dane_set_flags(SSL_CTX *ctx, unsigned long flags) |
1120 | { | |
1121 | unsigned long orig = ctx->dane.flags; | |
1122 | ||
1123 | ctx->dane.flags |= flags; | |
1124 | return orig; | |
1125 | } | |
1126 | ||
1127 | unsigned long SSL_CTX_dane_clear_flags(SSL_CTX *ctx, unsigned long flags) | |
1128 | { | |
1129 | unsigned long orig = ctx->dane.flags; | |
1130 | ||
1131 | ctx->dane.flags &= ~flags; | |
1132 | return orig; | |
1133 | } | |
1134 | ||
919ba009 VD |
1135 | int SSL_dane_enable(SSL *s, const char *basedomain) |
1136 | { | |
38b051a1 TM |
1137 | SSL_DANE *dane; |
1138 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); | |
919ba009 | 1139 | |
38b051a1 TM |
1140 | if (sc == NULL) |
1141 | return 0; | |
1142 | ||
1143 | dane = &sc->dane; | |
919ba009 | 1144 | if (s->ctx->dane.mdmax == 0) { |
6849b73c | 1145 | ERR_raise(ERR_LIB_SSL, SSL_R_CONTEXT_NOT_DANE_ENABLED); |
919ba009 VD |
1146 | return 0; |
1147 | } | |
1148 | if (dane->trecs != NULL) { | |
6849b73c | 1149 | ERR_raise(ERR_LIB_SSL, SSL_R_DANE_ALREADY_ENABLED); |
919ba009 VD |
1150 | return 0; |
1151 | } | |
1152 | ||
8d887efa VD |
1153 | /* |
1154 | * Default SNI name. This rejects empty names, while set1_host below | |
1155 | * accepts them and disables host name checks. To avoid side-effects with | |
1156 | * invalid input, set the SNI name first. | |
1157 | */ | |
38b051a1 | 1158 | if (sc->ext.hostname == NULL) { |
dccd20d1 | 1159 | if (!SSL_set_tlsext_host_name(s, basedomain)) { |
6849b73c | 1160 | ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN); |
dccd20d1 | 1161 | return -1; |
8d887efa VD |
1162 | } |
1163 | } | |
1164 | ||
919ba009 | 1165 | /* Primary RFC6125 reference identifier */ |
38b051a1 | 1166 | if (!X509_VERIFY_PARAM_set1_host(sc->param, basedomain, 0)) { |
6849b73c | 1167 | ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN); |
919ba009 VD |
1168 | return -1; |
1169 | } | |
1170 | ||
919ba009 VD |
1171 | dane->mdpth = -1; |
1172 | dane->pdpth = -1; | |
1173 | dane->dctx = &s->ctx->dane; | |
1174 | dane->trecs = sk_danetls_record_new_null(); | |
1175 | ||
1176 | if (dane->trecs == NULL) { | |
6849b73c | 1177 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
919ba009 VD |
1178 | return -1; |
1179 | } | |
1180 | return 1; | |
1181 | } | |
1182 | ||
5ae4ceb9 VD |
1183 | unsigned long SSL_dane_set_flags(SSL *ssl, unsigned long flags) |
1184 | { | |
38b051a1 TM |
1185 | unsigned long orig; |
1186 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); | |
1187 | ||
1188 | if (sc == NULL) | |
1189 | return 0; | |
1190 | ||
1191 | orig = sc->dane.flags; | |
5ae4ceb9 | 1192 | |
38b051a1 | 1193 | sc->dane.flags |= flags; |
5ae4ceb9 VD |
1194 | return orig; |
1195 | } | |
1196 | ||
1197 | unsigned long SSL_dane_clear_flags(SSL *ssl, unsigned long flags) | |
1198 | { | |
38b051a1 TM |
1199 | unsigned long orig; |
1200 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); | |
5ae4ceb9 | 1201 | |
38b051a1 TM |
1202 | if (sc == NULL) |
1203 | return 0; | |
1204 | ||
1205 | orig = sc->dane.flags; | |
1206 | ||
1207 | sc->dane.flags &= ~flags; | |
5ae4ceb9 VD |
1208 | return orig; |
1209 | } | |
1210 | ||
919ba009 VD |
1211 | int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki) |
1212 | { | |
38b051a1 TM |
1213 | SSL_DANE *dane; |
1214 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); | |
919ba009 | 1215 | |
38b051a1 TM |
1216 | if (sc == NULL) |
1217 | return -1; | |
1218 | ||
1219 | dane = &sc->dane; | |
1220 | ||
1221 | if (!DANETLS_ENABLED(dane) || sc->verify_result != X509_V_OK) | |
919ba009 VD |
1222 | return -1; |
1223 | if (dane->mtlsa) { | |
1224 | if (mcert) | |
1225 | *mcert = dane->mcert; | |
1226 | if (mspki) | |
1227 | *mspki = (dane->mcert == NULL) ? dane->mtlsa->spki : NULL; | |
1228 | } | |
1229 | return dane->mdpth; | |
1230 | } | |
1231 | ||
1232 | int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector, | |
6d4313f0 | 1233 | uint8_t *mtype, const unsigned char **data, size_t *dlen) |
919ba009 | 1234 | { |
38b051a1 TM |
1235 | SSL_DANE *dane; |
1236 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); | |
919ba009 | 1237 | |
38b051a1 TM |
1238 | if (sc == NULL) |
1239 | return -1; | |
1240 | ||
1241 | dane = &sc->dane; | |
1242 | ||
1243 | if (!DANETLS_ENABLED(dane) || sc->verify_result != X509_V_OK) | |
919ba009 VD |
1244 | return -1; |
1245 | if (dane->mtlsa) { | |
1246 | if (usage) | |
1247 | *usage = dane->mtlsa->usage; | |
1248 | if (selector) | |
1249 | *selector = dane->mtlsa->selector; | |
1250 | if (mtype) | |
1251 | *mtype = dane->mtlsa->mtype; | |
1252 | if (data) | |
1253 | *data = dane->mtlsa->data; | |
1254 | if (dlen) | |
1255 | *dlen = dane->mtlsa->dlen; | |
1256 | } | |
1257 | return dane->mdpth; | |
1258 | } | |
1259 | ||
b9aec69a | 1260 | SSL_DANE *SSL_get0_dane(SSL *s) |
919ba009 | 1261 | { |
38b051a1 TM |
1262 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1263 | ||
1264 | if (sc == NULL) | |
1265 | return NULL; | |
1266 | ||
1267 | return &sc->dane; | |
919ba009 VD |
1268 | } |
1269 | ||
1270 | int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector, | |
6d4313f0 | 1271 | uint8_t mtype, const unsigned char *data, size_t dlen) |
919ba009 | 1272 | { |
38b051a1 TM |
1273 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1274 | ||
1275 | if (sc == NULL) | |
1276 | return 0; | |
1277 | ||
1278 | return dane_tlsa_add(&sc->dane, usage, selector, mtype, data, dlen); | |
919ba009 VD |
1279 | } |
1280 | ||
a230b26e EK |
1281 | int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, uint8_t mtype, |
1282 | uint8_t ord) | |
919ba009 VD |
1283 | { |
1284 | return dane_mtype_set(&ctx->dane, md, mtype, ord); | |
1285 | } | |
1286 | ||
ccf11751 | 1287 | int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) |
0f113f3e MC |
1288 | { |
1289 | return X509_VERIFY_PARAM_set1(ctx->param, vpm); | |
1290 | } | |
ccf11751 DSH |
1291 | |
1292 | int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) | |
0f113f3e | 1293 | { |
38b051a1 TM |
1294 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
1295 | ||
1296 | if (sc == NULL) | |
1297 | return 0; | |
1298 | ||
1299 | return X509_VERIFY_PARAM_set1(sc->param, vpm); | |
0f113f3e | 1300 | } |
ccf11751 | 1301 | |
7af31968 | 1302 | X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) |
0f113f3e MC |
1303 | { |
1304 | return ctx->param; | |
1305 | } | |
7af31968 DSH |
1306 | |
1307 | X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) | |
0f113f3e | 1308 | { |
38b051a1 TM |
1309 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
1310 | ||
1311 | if (sc == NULL) | |
1312 | return NULL; | |
1313 | ||
1314 | return sc->param; | |
0f113f3e | 1315 | } |
7af31968 | 1316 | |
a5ee80b9 | 1317 | void SSL_certs_clear(SSL *s) |
0f113f3e | 1318 | { |
38b051a1 TM |
1319 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1320 | ||
1321 | if (sc == NULL) | |
1322 | return; | |
1323 | ||
1324 | ssl_cert_clear_certs(sc->cert); | |
0f113f3e | 1325 | } |
a5ee80b9 | 1326 | |
4f43d0e7 | 1327 | void SSL_free(SSL *s) |
0f113f3e MC |
1328 | { |
1329 | int i; | |
58964a49 | 1330 | |
e6e9170d RS |
1331 | if (s == NULL) |
1332 | return; | |
2f545ae4 | 1333 | CRYPTO_DOWN_REF(&s->references, &i, s->lock); |
f3f1cf84 | 1334 | REF_PRINT_COUNT("SSL", s); |
0f113f3e MC |
1335 | if (i > 0) |
1336 | return; | |
f3f1cf84 | 1337 | REF_ASSERT_ISNT(i < 0); |
d02b48c6 | 1338 | |
38b051a1 TM |
1339 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); |
1340 | ||
1341 | if (s->method != NULL) | |
1342 | s->method->ssl_free(s); | |
1343 | ||
1344 | SSL_CTX_free(s->ctx); | |
1345 | CRYPTO_THREAD_lock_free(s->lock); | |
1346 | ||
1347 | OPENSSL_free(s); | |
1348 | } | |
1349 | ||
1350 | void ossl_ssl_connection_free(SSL *ssl) | |
1351 | { | |
1352 | SSL_CONNECTION *s; | |
1353 | ||
1354 | s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); | |
1355 | if (s == NULL) | |
1356 | return; | |
1357 | ||
222561fe | 1358 | X509_VERIFY_PARAM_free(s->param); |
919ba009 | 1359 | dane_final(&s->dane); |
0f113f3e | 1360 | |
b77f3ed1 | 1361 | /* Ignore return value */ |
2e7dc7cd MC |
1362 | ssl_free_wbio_buffer(s); |
1363 | ||
65e2d672 | 1364 | BIO_free_all(s->wbio); |
50ec7505 | 1365 | s->wbio = NULL; |
325cfa85 | 1366 | BIO_free_all(s->rbio); |
50ec7505 | 1367 | s->rbio = NULL; |
0f113f3e | 1368 | |
b5cf81f7 MC |
1369 | RECORD_LAYER_clear(&s->rlayer); |
1370 | ||
25aaa98a | 1371 | BUF_MEM_free(s->init_buf); |
0f113f3e MC |
1372 | |
1373 | /* add extra stuff */ | |
25aaa98a RS |
1374 | sk_SSL_CIPHER_free(s->cipher_list); |
1375 | sk_SSL_CIPHER_free(s->cipher_list_by_id); | |
f865b081 | 1376 | sk_SSL_CIPHER_free(s->tls13_ciphersuites); |
eee2a6a7 | 1377 | sk_SSL_CIPHER_free(s->peer_ciphers); |
0f113f3e MC |
1378 | |
1379 | /* Make the next call work :-) */ | |
1380 | if (s->session != NULL) { | |
1381 | ssl_clear_bad_session(s); | |
1382 | SSL_SESSION_free(s->session); | |
1383 | } | |
9368f865 | 1384 | SSL_SESSION_free(s->psksession); |
add8d0e9 | 1385 | OPENSSL_free(s->psksession_id); |
0f113f3e | 1386 | |
d31fb0b5 | 1387 | clear_ciphers(s); |
d02b48c6 | 1388 | |
e0e920b1 | 1389 | ssl_cert_free(s->cert); |
29948ac8 | 1390 | OPENSSL_free(s->shared_sigalgs); |
0f113f3e | 1391 | /* Free up if allocated */ |
d02b48c6 | 1392 | |
aff8c126 | 1393 | OPENSSL_free(s->ext.hostname); |
222da979 | 1394 | SSL_CTX_free(s->session_ctx); |
aff8c126 | 1395 | OPENSSL_free(s->ext.ecpointformats); |
cd0fb43c | 1396 | OPENSSL_free(s->ext.peer_ecpointformats); |
aff8c126 | 1397 | OPENSSL_free(s->ext.supportedgroups); |
45436e61 | 1398 | OPENSSL_free(s->ext.peer_supportedgroups); |
aff8c126 | 1399 | sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free); |
3e41ac35 | 1400 | #ifndef OPENSSL_NO_OCSP |
aff8c126 | 1401 | sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free); |
3e41ac35 | 1402 | #endif |
ed29e82a RP |
1403 | #ifndef OPENSSL_NO_CT |
1404 | SCT_LIST_free(s->scts); | |
aff8c126 | 1405 | OPENSSL_free(s->ext.scts); |
ed29e82a | 1406 | #endif |
aff8c126 RS |
1407 | OPENSSL_free(s->ext.ocsp.resp); |
1408 | OPENSSL_free(s->ext.alpn); | |
cfef5027 | 1409 | OPENSSL_free(s->ext.tls13_cookie); |
94941cad MK |
1410 | if (s->clienthello != NULL) |
1411 | OPENSSL_free(s->clienthello->pre_proc_exts); | |
6b1bb98f | 1412 | OPENSSL_free(s->clienthello); |
9d75dce3 TS |
1413 | OPENSSL_free(s->pha_context); |
1414 | EVP_MD_CTX_free(s->pha_dgst); | |
0f113f3e | 1415 | |
fa7c2637 | 1416 | sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free); |
98732979 | 1417 | sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free); |
0f113f3e | 1418 | |
79b2a2f2 | 1419 | OSSL_STACK_OF_X509_free(s->verified_chain); |
696178ed | 1420 | |
38b051a1 TM |
1421 | if (ssl->method != NULL) |
1422 | ssl->method->ssl_deinit(ssl); | |
7c3908dd | 1423 | |
ff75a257 MC |
1424 | ASYNC_WAIT_CTX_free(s->waitctx); |
1425 | ||
e481f9b9 | 1426 | #if !defined(OPENSSL_NO_NEXTPROTONEG) |
aff8c126 | 1427 | OPENSSL_free(s->ext.npn); |
ee2ffc27 BL |
1428 | #endif |
1429 | ||
e783bae2 | 1430 | #ifndef OPENSSL_NO_SRTP |
25aaa98a | 1431 | sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles); |
0f113f3e | 1432 | #endif |
0f113f3e MC |
1433 | } |
1434 | ||
65e2d672 | 1435 | void SSL_set0_rbio(SSL *s, BIO *rbio) |
3ffbe008 | 1436 | { |
38b051a1 TM |
1437 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1438 | ||
1439 | if (sc == NULL) | |
1440 | return; | |
1441 | ||
1442 | BIO_free_all(sc->rbio); | |
1443 | sc->rbio = rbio; | |
cffafb5f | 1444 | sc->rlayer.rrlmethod->set1_bio(sc->rlayer.rrl, sc->rbio); |
3ffbe008 MC |
1445 | } |
1446 | ||
65e2d672 | 1447 | void SSL_set0_wbio(SSL *s, BIO *wbio) |
0f113f3e | 1448 | { |
38b051a1 TM |
1449 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1450 | ||
1451 | if (sc == NULL) | |
1452 | return; | |
1453 | ||
0f113f3e MC |
1454 | /* |
1455 | * If the output buffering BIO is still in place, remove it | |
1456 | */ | |
38b051a1 TM |
1457 | if (sc->bbio != NULL) |
1458 | sc->wbio = BIO_pop(sc->wbio); | |
2e7dc7cd | 1459 | |
38b051a1 TM |
1460 | BIO_free_all(sc->wbio); |
1461 | sc->wbio = wbio; | |
2e7dc7cd MC |
1462 | |
1463 | /* Re-attach |bbio| to the new |wbio|. */ | |
38b051a1 TM |
1464 | if (sc->bbio != NULL) |
1465 | sc->wbio = BIO_push(sc->bbio, sc->wbio); | |
b5cf81f7 MC |
1466 | |
1467 | sc->rlayer.wrlmethod->set1_bio(sc->rlayer.wrl, sc->wbio); | |
0f113f3e | 1468 | } |
d02b48c6 | 1469 | |
3ffbe008 MC |
1470 | void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) |
1471 | { | |
65e2d672 MC |
1472 | /* |
1473 | * For historical reasons, this function has many different cases in | |
1474 | * ownership handling. | |
1475 | */ | |
1476 | ||
1477 | /* If nothing has changed, do nothing */ | |
1478 | if (rbio == SSL_get_rbio(s) && wbio == SSL_get_wbio(s)) | |
1479 | return; | |
1480 | ||
1481 | /* | |
1482 | * If the two arguments are equal then one fewer reference is granted by the | |
1483 | * caller than we want to take | |
1484 | */ | |
1485 | if (rbio != NULL && rbio == wbio) | |
1486 | BIO_up_ref(rbio); | |
1487 | ||
1488 | /* | |
1489 | * If only the wbio is changed only adopt one reference. | |
1490 | */ | |
1491 | if (rbio == SSL_get_rbio(s)) { | |
1492 | SSL_set0_wbio(s, wbio); | |
1493 | return; | |
1494 | } | |
1495 | /* | |
1496 | * There is an asymmetry here for historical reasons. If only the rbio is | |
1497 | * changed AND the rbio and wbio were originally different, then we only | |
1498 | * adopt one reference. | |
1499 | */ | |
1500 | if (wbio == SSL_get_wbio(s) && SSL_get_rbio(s) != SSL_get_wbio(s)) { | |
1501 | SSL_set0_rbio(s, rbio); | |
1502 | return; | |
1503 | } | |
1504 | ||
1505 | /* Otherwise, adopt both references. */ | |
1506 | SSL_set0_rbio(s, rbio); | |
1507 | SSL_set0_wbio(s, wbio); | |
3ffbe008 MC |
1508 | } |
1509 | ||
0821bcd4 | 1510 | BIO *SSL_get_rbio(const SSL *s) |
0f113f3e | 1511 | { |
38b051a1 TM |
1512 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
1513 | ||
1514 | if (sc == NULL) | |
1515 | return NULL; | |
1516 | ||
1517 | return sc->rbio; | |
0f113f3e | 1518 | } |
d02b48c6 | 1519 | |
0821bcd4 | 1520 | BIO *SSL_get_wbio(const SSL *s) |
0f113f3e | 1521 | { |
38b051a1 TM |
1522 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
1523 | ||
1524 | if (sc == NULL) | |
1525 | return NULL; | |
1526 | ||
1527 | if (sc->bbio != NULL) { | |
2e7dc7cd MC |
1528 | /* |
1529 | * If |bbio| is active, the true caller-configured BIO is its | |
1530 | * |next_bio|. | |
1531 | */ | |
38b051a1 | 1532 | return BIO_next(sc->bbio); |
2e7dc7cd | 1533 | } |
38b051a1 | 1534 | return sc->wbio; |
0f113f3e | 1535 | } |
d02b48c6 | 1536 | |
0821bcd4 | 1537 | int SSL_get_fd(const SSL *s) |
0f113f3e | 1538 | { |
2e7dc7cd | 1539 | return SSL_get_rfd(s); |
0f113f3e | 1540 | } |
24cbf3ef | 1541 | |
0821bcd4 | 1542 | int SSL_get_rfd(const SSL *s) |
0f113f3e MC |
1543 | { |
1544 | int ret = -1; | |
1545 | BIO *b, *r; | |
1546 | ||
1547 | b = SSL_get_rbio(s); | |
1548 | r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); | |
1549 | if (r != NULL) | |
1550 | BIO_get_fd(r, &ret); | |
26a7d938 | 1551 | return ret; |
0f113f3e | 1552 | } |
d02b48c6 | 1553 | |
0821bcd4 | 1554 | int SSL_get_wfd(const SSL *s) |
0f113f3e MC |
1555 | { |
1556 | int ret = -1; | |
1557 | BIO *b, *r; | |
1558 | ||
1559 | b = SSL_get_wbio(s); | |
1560 | r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); | |
1561 | if (r != NULL) | |
1562 | BIO_get_fd(r, &ret); | |
26a7d938 | 1563 | return ret; |
0f113f3e | 1564 | } |
24cbf3ef | 1565 | |
bc36ee62 | 1566 | #ifndef OPENSSL_NO_SOCK |
0f113f3e MC |
1567 | int SSL_set_fd(SSL *s, int fd) |
1568 | { | |
1569 | int ret = 0; | |
1570 | BIO *bio = NULL; | |
1571 | ||
1572 | bio = BIO_new(BIO_s_socket()); | |
1573 | ||
1574 | if (bio == NULL) { | |
6849b73c | 1575 | ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB); |
0f113f3e MC |
1576 | goto err; |
1577 | } | |
1578 | BIO_set_fd(bio, fd, BIO_NOCLOSE); | |
1579 | SSL_set_bio(s, bio, bio); | |
50ec7505 BP |
1580 | #ifndef OPENSSL_NO_KTLS |
1581 | /* | |
1582 | * The new socket is created successfully regardless of ktls_enable. | |
1583 | * ktls_enable doesn't change any functionality of the socket, except | |
1584 | * changing the setsockopt to enable the processing of ktls_start. | |
1585 | * Thus, it is not a problem to call it for non-TLS sockets. | |
1586 | */ | |
1587 | ktls_enable(fd); | |
1588 | #endif /* OPENSSL_NO_KTLS */ | |
0f113f3e MC |
1589 | ret = 1; |
1590 | err: | |
26a7d938 | 1591 | return ret; |
0f113f3e | 1592 | } |
d02b48c6 | 1593 | |
0f113f3e MC |
1594 | int SSL_set_wfd(SSL *s, int fd) |
1595 | { | |
2e7dc7cd | 1596 | BIO *rbio = SSL_get_rbio(s); |
0f113f3e | 1597 | |
2e7dc7cd MC |
1598 | if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET |
1599 | || (int)BIO_get_fd(rbio, NULL) != fd) { | |
1600 | BIO *bio = BIO_new(BIO_s_socket()); | |
0f113f3e MC |
1601 | |
1602 | if (bio == NULL) { | |
6849b73c | 1603 | ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB); |
2e7dc7cd | 1604 | return 0; |
0f113f3e MC |
1605 | } |
1606 | BIO_set_fd(bio, fd, BIO_NOCLOSE); | |
65e2d672 | 1607 | SSL_set0_wbio(s, bio); |
50ec7505 BP |
1608 | #ifndef OPENSSL_NO_KTLS |
1609 | /* | |
1610 | * The new socket is created successfully regardless of ktls_enable. | |
1611 | * ktls_enable doesn't change any functionality of the socket, except | |
1612 | * changing the setsockopt to enable the processing of ktls_start. | |
1613 | * Thus, it is not a problem to call it for non-TLS sockets. | |
1614 | */ | |
1615 | ktls_enable(fd); | |
1616 | #endif /* OPENSSL_NO_KTLS */ | |
2e7dc7cd | 1617 | } else { |
65e2d672 MC |
1618 | BIO_up_ref(rbio); |
1619 | SSL_set0_wbio(s, rbio); | |
2e7dc7cd MC |
1620 | } |
1621 | return 1; | |
0f113f3e MC |
1622 | } |
1623 | ||
1624 | int SSL_set_rfd(SSL *s, int fd) | |
1625 | { | |
2e7dc7cd | 1626 | BIO *wbio = SSL_get_wbio(s); |
0f113f3e | 1627 | |
2e7dc7cd MC |
1628 | if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET |
1629 | || ((int)BIO_get_fd(wbio, NULL) != fd)) { | |
1630 | BIO *bio = BIO_new(BIO_s_socket()); | |
0f113f3e MC |
1631 | |
1632 | if (bio == NULL) { | |
6849b73c | 1633 | ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB); |
2e7dc7cd | 1634 | return 0; |
0f113f3e MC |
1635 | } |
1636 | BIO_set_fd(bio, fd, BIO_NOCLOSE); | |
65e2d672 | 1637 | SSL_set0_rbio(s, bio); |
2e7dc7cd | 1638 | } else { |
65e2d672 MC |
1639 | BIO_up_ref(wbio); |
1640 | SSL_set0_rbio(s, wbio); | |
2e7dc7cd MC |
1641 | } |
1642 | ||
1643 | return 1; | |
0f113f3e MC |
1644 | } |
1645 | #endif | |
ca03109c BM |
1646 | |
1647 | /* return length of latest Finished message we sent, copy to 'buf' */ | |
0821bcd4 | 1648 | size_t SSL_get_finished(const SSL *s, void *buf, size_t count) |
0f113f3e MC |
1649 | { |
1650 | size_t ret = 0; | |
38b051a1 TM |
1651 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
1652 | ||
1653 | if (sc == NULL) | |
1654 | return 0; | |
0f113f3e | 1655 | |
38b051a1 | 1656 | ret = sc->s3.tmp.finish_md_len; |
555cbb32 TS |
1657 | if (count > ret) |
1658 | count = ret; | |
38b051a1 | 1659 | memcpy(buf, sc->s3.tmp.finish_md, count); |
0f113f3e MC |
1660 | return ret; |
1661 | } | |
ca03109c BM |
1662 | |
1663 | /* return length of latest Finished message we expected, copy to 'buf' */ | |
0821bcd4 | 1664 | size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) |
0f113f3e MC |
1665 | { |
1666 | size_t ret = 0; | |
38b051a1 | 1667 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
ca03109c | 1668 | |
38b051a1 TM |
1669 | if (sc == NULL) |
1670 | return 0; | |
1671 | ||
1672 | ret = sc->s3.tmp.peer_finish_md_len; | |
555cbb32 TS |
1673 | if (count > ret) |
1674 | count = ret; | |
38b051a1 | 1675 | memcpy(buf, sc->s3.tmp.peer_finish_md, count); |
0f113f3e MC |
1676 | return ret; |
1677 | } | |
ca03109c | 1678 | |
0821bcd4 | 1679 | int SSL_get_verify_mode(const SSL *s) |
0f113f3e | 1680 | { |
38b051a1 TM |
1681 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
1682 | ||
1683 | if (sc == NULL) | |
1684 | return 0; | |
1685 | ||
1686 | return sc->verify_mode; | |
0f113f3e | 1687 | } |
d02b48c6 | 1688 | |
0821bcd4 | 1689 | int SSL_get_verify_depth(const SSL *s) |
0f113f3e | 1690 | { |
38b051a1 TM |
1691 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
1692 | ||
1693 | if (sc == NULL) | |
1694 | return 0; | |
1695 | ||
1696 | return X509_VERIFY_PARAM_get_depth(sc->param); | |
0f113f3e | 1697 | } |
7f89714e | 1698 | |
0f113f3e | 1699 | int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) { |
38b051a1 TM |
1700 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
1701 | ||
1702 | if (sc == NULL) | |
1703 | return NULL; | |
1704 | ||
1705 | return sc->verify_callback; | |
0f113f3e | 1706 | } |
d02b48c6 | 1707 | |
0821bcd4 | 1708 | int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) |
0f113f3e | 1709 | { |
26a7d938 | 1710 | return ctx->verify_mode; |
0f113f3e | 1711 | } |
d02b48c6 | 1712 | |
0821bcd4 | 1713 | int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) |
0f113f3e MC |
1714 | { |
1715 | return X509_VERIFY_PARAM_get_depth(ctx->param); | |
1716 | } | |
1717 | ||
1718 | int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) { | |
26a7d938 | 1719 | return ctx->default_verify_callback; |
0f113f3e MC |
1720 | } |
1721 | ||
1722 | void SSL_set_verify(SSL *s, int mode, | |
1723 | int (*callback) (int ok, X509_STORE_CTX *ctx)) | |
1724 | { | |
38b051a1 TM |
1725 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1726 | ||
1727 | if (sc == NULL) | |
1728 | return; | |
1729 | ||
1730 | sc->verify_mode = mode; | |
0f113f3e | 1731 | if (callback != NULL) |
38b051a1 | 1732 | sc->verify_callback = callback; |
0f113f3e MC |
1733 | } |
1734 | ||
1735 | void SSL_set_verify_depth(SSL *s, int depth) | |
1736 | { | |
38b051a1 TM |
1737 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1738 | ||
1739 | if (sc == NULL) | |
1740 | return; | |
1741 | ||
1742 | X509_VERIFY_PARAM_set_depth(sc->param, depth); | |
0f113f3e MC |
1743 | } |
1744 | ||
1745 | void SSL_set_read_ahead(SSL *s, int yes) | |
1746 | { | |
38b051a1 | 1747 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4566dae7 | 1748 | OSSL_PARAM options[2], *opts = options; |
38b051a1 TM |
1749 | |
1750 | if (sc == NULL) | |
1751 | return; | |
1752 | ||
1753 | RECORD_LAYER_set_read_ahead(&sc->rlayer, yes); | |
4566dae7 MC |
1754 | |
1755 | *opts++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD, | |
1756 | &sc->rlayer.read_ahead); | |
1757 | *opts = OSSL_PARAM_construct_end(); | |
1758 | ||
1759 | /* Ignore return value */ | |
1760 | sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options); | |
0f113f3e | 1761 | } |
d02b48c6 | 1762 | |
0821bcd4 | 1763 | int SSL_get_read_ahead(const SSL *s) |
0f113f3e | 1764 | { |
38b051a1 TM |
1765 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
1766 | ||
1767 | if (sc == NULL) | |
1768 | return 0; | |
1769 | ||
1770 | return RECORD_LAYER_get_read_ahead(&sc->rlayer); | |
0f113f3e | 1771 | } |
d02b48c6 | 1772 | |
0821bcd4 | 1773 | int SSL_pending(const SSL *s) |
0f113f3e | 1774 | { |
8b0e934a MC |
1775 | size_t pending = s->method->ssl_pending(s); |
1776 | ||
0f113f3e MC |
1777 | /* |
1778 | * SSL_pending cannot work properly if read-ahead is enabled | |
1779 | * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is | |
1780 | * impossible to fix since SSL_pending cannot report errors that may be | |
1781 | * observed while scanning the new data. (Note that SSL_pending() is | |
1782 | * often used as a boolean value, so we'd better not return -1.) | |
8b0e934a MC |
1783 | * |
1784 | * SSL_pending also cannot work properly if the value >INT_MAX. In that case | |
1785 | * we just return INT_MAX. | |
0f113f3e | 1786 | */ |
348240c6 | 1787 | return pending < INT_MAX ? (int)pending : INT_MAX; |
0f113f3e | 1788 | } |
d02b48c6 | 1789 | |
49580f25 MC |
1790 | int SSL_has_pending(const SSL *s) |
1791 | { | |
1792 | /* | |
1793 | * Similar to SSL_pending() but returns a 1 to indicate that we have | |
6d6b295a MC |
1794 | * processed or unprocessed data available or 0 otherwise (as opposed to the |
1795 | * number of bytes available). Unlike SSL_pending() this will take into | |
1796 | * account read_ahead data. A 1 return simply indicates that we have data. | |
1797 | * That data may not result in any application data, or we may fail to parse | |
1798 | * the records for some reason. | |
49580f25 | 1799 | */ |
38b051a1 TM |
1800 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
1801 | ||
6d6b295a MC |
1802 | /* Check buffered app data if any first */ |
1803 | if (SSL_CONNECTION_IS_DTLS(sc)) { | |
eddb067e | 1804 | TLS_RECORD *rdata; |
6d6b295a MC |
1805 | pitem *item, *iter; |
1806 | ||
1807 | iter = pqueue_iterator(sc->rlayer.d->buffered_app_data.q); | |
1808 | while ((item = pqueue_next(&iter)) != NULL) { | |
1809 | rdata = item->data; | |
eddb067e | 1810 | if (rdata->length > 0) |
6d6b295a MC |
1811 | return 1; |
1812 | } | |
1813 | } | |
38b051a1 TM |
1814 | |
1815 | if (RECORD_LAYER_processed_read_pending(&sc->rlayer)) | |
49580f25 MC |
1816 | return 1; |
1817 | ||
38b051a1 | 1818 | return RECORD_LAYER_read_pending(&sc->rlayer); |
49580f25 MC |
1819 | } |
1820 | ||
8c2bfd25 | 1821 | X509 *SSL_get1_peer_certificate(const SSL *s) |
0f113f3e | 1822 | { |
8c2bfd25 | 1823 | X509 *r = SSL_get0_peer_certificate(s); |
d02b48c6 | 1824 | |
8c2bfd25 TS |
1825 | if (r != NULL) |
1826 | X509_up_ref(r); | |
0f113f3e | 1827 | |
26a7d938 | 1828 | return r; |
0f113f3e | 1829 | } |
d02b48c6 | 1830 | |
8c2bfd25 TS |
1831 | X509 *SSL_get0_peer_certificate(const SSL *s) |
1832 | { | |
38b051a1 TM |
1833 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
1834 | ||
1835 | if (sc == NULL) | |
1836 | return NULL; | |
1837 | ||
1838 | if (sc->session == NULL) | |
8c2bfd25 TS |
1839 | return NULL; |
1840 | else | |
38b051a1 | 1841 | return sc->session->peer; |
8c2bfd25 TS |
1842 | } |
1843 | ||
0821bcd4 | 1844 | STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) |
0f113f3e MC |
1845 | { |
1846 | STACK_OF(X509) *r; | |
38b051a1 | 1847 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
0f113f3e | 1848 | |
38b051a1 TM |
1849 | if (sc == NULL) |
1850 | return NULL; | |
1851 | ||
1852 | if (sc->session == NULL) | |
0f113f3e MC |
1853 | r = NULL; |
1854 | else | |
38b051a1 | 1855 | r = sc->session->peer_chain; |
0f113f3e MC |
1856 | |
1857 | /* | |
1858 | * If we are a client, cert_chain includes the peer's own certificate; if | |
1859 | * we are a server, it does not. | |
1860 | */ | |
1861 | ||
26a7d938 | 1862 | return r; |
0f113f3e MC |
1863 | } |
1864 | ||
1865 | /* | |
1866 | * Now in theory, since the calling process own 't' it should be safe to | |
1867 | * modify. We need to be able to read f without being hassled | |
1868 | */ | |
17dd65e6 | 1869 | int SSL_copy_session_id(SSL *t, const SSL *f) |
0f113f3e | 1870 | { |
16203f7b | 1871 | int i; |
38b051a1 TM |
1872 | /* TODO(QUIC): Do we want to support this for QUIC connections? */ |
1873 | SSL_CONNECTION *tsc = SSL_CONNECTION_FROM_SSL_ONLY(t); | |
1874 | const SSL_CONNECTION *fsc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(f); | |
1875 | ||
1876 | if (tsc == NULL || fsc == NULL) | |
1877 | return 0; | |
1878 | ||
3e6a0d57 | 1879 | /* Do we need to do SSL locking? */ |
61986d32 | 1880 | if (!SSL_set_session(t, SSL_get_session(f))) { |
17dd65e6 | 1881 | return 0; |
69f68237 | 1882 | } |
0f113f3e MC |
1883 | |
1884 | /* | |
87d9cafa | 1885 | * what if we are setup for one protocol version but want to talk another |
0f113f3e MC |
1886 | */ |
1887 | if (t->method != f->method) { | |
38b051a1 | 1888 | t->method->ssl_deinit(t); |
919ba009 | 1889 | t->method = f->method; |
38b051a1 | 1890 | if (t->method->ssl_init(t) == 0) |
919ba009 | 1891 | return 0; |
0f113f3e MC |
1892 | } |
1893 | ||
38b051a1 TM |
1894 | CRYPTO_UP_REF(&fsc->cert->references, &i, fsc->cert->lock); |
1895 | ssl_cert_free(tsc->cert); | |
1896 | tsc->cert = fsc->cert; | |
1897 | if (!SSL_set_session_id_context(t, fsc->sid_ctx, (int)fsc->sid_ctx_length)) { | |
17dd65e6 | 1898 | return 0; |
69f68237 | 1899 | } |
17dd65e6 MC |
1900 | |
1901 | return 1; | |
0f113f3e | 1902 | } |
d02b48c6 | 1903 | |
58964a49 | 1904 | /* Fix this so it checks all the valid key/cert options */ |
0821bcd4 | 1905 | int SSL_CTX_check_private_key(const SSL_CTX *ctx) |
0f113f3e | 1906 | { |
a230b26e | 1907 | if ((ctx == NULL) || (ctx->cert->key->x509 == NULL)) { |
6849b73c | 1908 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); |
26a7d938 | 1909 | return 0; |
0f113f3e MC |
1910 | } |
1911 | if (ctx->cert->key->privatekey == NULL) { | |
6849b73c | 1912 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
26a7d938 | 1913 | return 0; |
0f113f3e | 1914 | } |
26a7d938 K |
1915 | return X509_check_private_key |
1916 | (ctx->cert->key->x509, ctx->cert->key->privatekey); | |
0f113f3e | 1917 | } |
d02b48c6 | 1918 | |
58964a49 | 1919 | /* Fix this function so that it takes an optional type parameter */ |
0821bcd4 | 1920 | int SSL_check_private_key(const SSL *ssl) |
0f113f3e | 1921 | { |
38b051a1 TM |
1922 | const SSL_CONNECTION *sc; |
1923 | ||
1924 | if ((sc = SSL_CONNECTION_FROM_CONST_SSL(ssl)) == NULL) { | |
6849b73c | 1925 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); |
26a7d938 | 1926 | return 0; |
0f113f3e | 1927 | } |
38b051a1 | 1928 | if (sc->cert->key->x509 == NULL) { |
6849b73c | 1929 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); |
26a7d938 | 1930 | return 0; |
0f113f3e | 1931 | } |
38b051a1 | 1932 | if (sc->cert->key->privatekey == NULL) { |
6849b73c | 1933 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
26a7d938 | 1934 | return 0; |
0f113f3e | 1935 | } |
38b051a1 TM |
1936 | return X509_check_private_key(sc->cert->key->x509, |
1937 | sc->cert->key->privatekey); | |
0f113f3e | 1938 | } |
d02b48c6 | 1939 | |
07bbc92c MC |
1940 | int SSL_waiting_for_async(SSL *s) |
1941 | { | |
38b051a1 TM |
1942 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1943 | ||
1944 | if (sc == NULL) | |
1945 | return 0; | |
1946 | ||
1947 | if (sc->job) | |
82676094 MC |
1948 | return 1; |
1949 | ||
07bbc92c MC |
1950 | return 0; |
1951 | } | |
1952 | ||
ff75a257 | 1953 | int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds) |
f4da39d2 | 1954 | { |
38b051a1 TM |
1955 | ASYNC_WAIT_CTX *ctx; |
1956 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); | |
ff75a257 | 1957 | |
38b051a1 TM |
1958 | if (sc == NULL) |
1959 | return 0; | |
1960 | ||
1961 | if ((ctx = sc->waitctx) == NULL) | |
ff75a257 MC |
1962 | return 0; |
1963 | return ASYNC_WAIT_CTX_get_all_fds(ctx, fds, numfds); | |
1964 | } | |
f4da39d2 | 1965 | |
ff75a257 MC |
1966 | int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, size_t *numaddfds, |
1967 | OSSL_ASYNC_FD *delfd, size_t *numdelfds) | |
1968 | { | |
38b051a1 TM |
1969 | ASYNC_WAIT_CTX *ctx; |
1970 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); | |
ff75a257 | 1971 | |
38b051a1 TM |
1972 | if (sc == NULL) |
1973 | return 0; | |
1974 | ||
1975 | if ((ctx = sc->waitctx) == NULL) | |
ff75a257 MC |
1976 | return 0; |
1977 | return ASYNC_WAIT_CTX_get_changed_fds(ctx, addfd, numaddfds, delfd, | |
1978 | numdelfds); | |
f4da39d2 MC |
1979 | } |
1980 | ||
9f5a87fd PY |
1981 | int SSL_CTX_set_async_callback(SSL_CTX *ctx, SSL_async_callback_fn callback) |
1982 | { | |
1983 | ctx->async_cb = callback; | |
1984 | return 1; | |
1985 | } | |
1986 | ||
1987 | int SSL_CTX_set_async_callback_arg(SSL_CTX *ctx, void *arg) | |
1988 | { | |
1989 | ctx->async_cb_arg = arg; | |
1990 | return 1; | |
1991 | } | |
1992 | ||
1993 | int SSL_set_async_callback(SSL *s, SSL_async_callback_fn callback) | |
1994 | { | |
38b051a1 TM |
1995 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1996 | ||
1997 | if (sc == NULL) | |
1998 | return 0; | |
1999 | ||
2000 | sc->async_cb = callback; | |
9f5a87fd PY |
2001 | return 1; |
2002 | } | |
2003 | ||
2004 | int SSL_set_async_callback_arg(SSL *s, void *arg) | |
2005 | { | |
38b051a1 TM |
2006 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2007 | ||
2008 | if (sc == NULL) | |
2009 | return 0; | |
2010 | ||
2011 | sc->async_cb_arg = arg; | |
9f5a87fd PY |
2012 | return 1; |
2013 | } | |
2014 | ||
2015 | int SSL_get_async_status(SSL *s, int *status) | |
2016 | { | |
38b051a1 TM |
2017 | ASYNC_WAIT_CTX *ctx; |
2018 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); | |
9f5a87fd | 2019 | |
38b051a1 TM |
2020 | if (sc == NULL) |
2021 | return 0; | |
2022 | ||
2023 | if ((ctx = sc->waitctx) == NULL) | |
9f5a87fd PY |
2024 | return 0; |
2025 | *status = ASYNC_WAIT_CTX_get_status(ctx); | |
2026 | return 1; | |
2027 | } | |
2028 | ||
4f43d0e7 | 2029 | int SSL_accept(SSL *s) |
0f113f3e | 2030 | { |
38b051a1 TM |
2031 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2032 | ||
2033 | if (sc == NULL) | |
2034 | return 0; | |
2035 | ||
2036 | if (sc->handshake_func == NULL) { | |
0f113f3e MC |
2037 | /* Not properly initialized yet */ |
2038 | SSL_set_accept_state(s); | |
07bbc92c | 2039 | } |
add2f5ca MC |
2040 | |
2041 | return SSL_do_handshake(s); | |
0f113f3e | 2042 | } |
d02b48c6 | 2043 | |
4f43d0e7 | 2044 | int SSL_connect(SSL *s) |
0f113f3e | 2045 | { |
38b051a1 TM |
2046 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2047 | ||
2048 | if (sc == NULL) | |
2049 | return 0; | |
2050 | ||
2051 | if (sc->handshake_func == NULL) { | |
0f113f3e MC |
2052 | /* Not properly initialized yet */ |
2053 | SSL_set_connect_state(s); | |
add2f5ca | 2054 | } |
b31b04d9 | 2055 | |
add2f5ca | 2056 | return SSL_do_handshake(s); |
0f113f3e | 2057 | } |
d02b48c6 | 2058 | |
0821bcd4 | 2059 | long SSL_get_default_timeout(const SSL *s) |
0f113f3e | 2060 | { |
f0131dc0 | 2061 | return (long int)ossl_time2seconds(s->method->get_timeout()); |
0f113f3e MC |
2062 | } |
2063 | ||
9f5a87fd PY |
2064 | static int ssl_async_wait_ctx_cb(void *arg) |
2065 | { | |
2066 | SSL *s = (SSL *)arg; | |
38b051a1 | 2067 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
9f5a87fd | 2068 | |
38b051a1 TM |
2069 | if (sc == NULL) |
2070 | return 0; | |
2071 | ||
2072 | return sc->async_cb(s, sc->async_cb_arg); | |
9f5a87fd PY |
2073 | } |
2074 | ||
7fecbf6f | 2075 | static int ssl_start_async_job(SSL *s, struct ssl_async_args *args, |
a230b26e EK |
2076 | int (*func) (void *)) |
2077 | { | |
add2f5ca | 2078 | int ret; |
38b051a1 TM |
2079 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2080 | ||
2081 | if (sc == NULL) | |
2082 | return 0; | |
2083 | ||
2084 | if (sc->waitctx == NULL) { | |
2085 | sc->waitctx = ASYNC_WAIT_CTX_new(); | |
2086 | if (sc->waitctx == NULL) | |
ff75a257 | 2087 | return -1; |
38b051a1 | 2088 | if (sc->async_cb != NULL |
9f5a87fd | 2089 | && !ASYNC_WAIT_CTX_set_callback |
38b051a1 | 2090 | (sc->waitctx, ssl_async_wait_ctx_cb, s)) |
9f5a87fd | 2091 | return -1; |
ff75a257 | 2092 | } |
07f620e3 | 2093 | |
38b051a1 TM |
2094 | sc->rwstate = SSL_NOTHING; |
2095 | switch (ASYNC_start_job(&sc->job, sc->waitctx, &ret, func, args, | |
a230b26e | 2096 | sizeof(struct ssl_async_args))) { |
add2f5ca | 2097 | case ASYNC_ERR: |
38b051a1 | 2098 | sc->rwstate = SSL_NOTHING; |
6849b73c | 2099 | ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_INIT_ASYNC); |
add2f5ca MC |
2100 | return -1; |
2101 | case ASYNC_PAUSE: | |
38b051a1 | 2102 | sc->rwstate = SSL_ASYNC_PAUSED; |
add2f5ca | 2103 | return -1; |
fc7f190c | 2104 | case ASYNC_NO_JOBS: |
38b051a1 | 2105 | sc->rwstate = SSL_ASYNC_NO_JOBS; |
fc7f190c | 2106 | return -1; |
add2f5ca | 2107 | case ASYNC_FINISH: |
38b051a1 | 2108 | sc->job = NULL; |
add2f5ca MC |
2109 | return ret; |
2110 | default: | |
38b051a1 | 2111 | sc->rwstate = SSL_NOTHING; |
6849b73c | 2112 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
add2f5ca MC |
2113 | /* Shouldn't happen */ |
2114 | return -1; | |
2115 | } | |
2116 | } | |
07bbc92c | 2117 | |
add2f5ca | 2118 | static int ssl_io_intern(void *vargs) |
07bbc92c MC |
2119 | { |
2120 | struct ssl_async_args *args; | |
2121 | SSL *s; | |
2122 | void *buf; | |
348240c6 | 2123 | size_t num; |
38b051a1 | 2124 | SSL_CONNECTION *sc; |
07bbc92c MC |
2125 | |
2126 | args = (struct ssl_async_args *)vargs; | |
2127 | s = args->s; | |
2128 | buf = args->buf; | |
2129 | num = args->num; | |
38b051a1 TM |
2130 | if ((sc = SSL_CONNECTION_FROM_SSL(s)) == NULL) |
2131 | return -1; | |
2132 | ||
ec447924 MC |
2133 | switch (args->type) { |
2134 | case READFUNC: | |
38b051a1 | 2135 | return args->f.func_read(s, buf, num, &sc->asyncrw); |
ec447924 | 2136 | case WRITEFUNC: |
38b051a1 | 2137 | return args->f.func_write(s, buf, num, &sc->asyncrw); |
ec447924 MC |
2138 | case OTHERFUNC: |
2139 | return args->f.func_other(s); | |
2140 | } | |
2141 | return -1; | |
07bbc92c MC |
2142 | } |
2143 | ||
4ee7d3f9 | 2144 | int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes) |
0f113f3e | 2145 | { |
38b051a1 TM |
2146 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2147 | ||
2148 | if (sc == NULL) | |
2149 | return -1; | |
2150 | ||
2151 | if (sc->handshake_func == NULL) { | |
6849b73c | 2152 | ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED); |
0f113f3e MC |
2153 | return -1; |
2154 | } | |
2155 | ||
38b051a1 TM |
2156 | if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) { |
2157 | sc->rwstate = SSL_NOTHING; | |
4ee7d3f9 | 2158 | return 0; |
0f113f3e | 2159 | } |
07bbc92c | 2160 | |
38b051a1 TM |
2161 | if (sc->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY |
2162 | || sc->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY) { | |
6849b73c | 2163 | ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0a5ece5b MC |
2164 | return 0; |
2165 | } | |
564547e4 MC |
2166 | /* |
2167 | * If we are a client and haven't received the ServerHello etc then we | |
2168 | * better do that | |
2169 | */ | |
38b051a1 | 2170 | ossl_statem_check_finish_init(sc, 0); |
0a5ece5b | 2171 | |
38b051a1 | 2172 | if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) { |
add2f5ca | 2173 | struct ssl_async_args args; |
eda75751 | 2174 | int ret; |
add2f5ca MC |
2175 | |
2176 | args.s = s; | |
2177 | args.buf = buf; | |
2178 | args.num = num; | |
ec447924 MC |
2179 | args.type = READFUNC; |
2180 | args.f.func_read = s->method->ssl_read; | |
add2f5ca | 2181 | |
eda75751 | 2182 | ret = ssl_start_async_job(s, &args, ssl_io_intern); |
38b051a1 | 2183 | *readbytes = sc->asyncrw; |
eda75751 | 2184 | return ret; |
07bbc92c | 2185 | } else { |
54105ddd | 2186 | return s->method->ssl_read(s, buf, num, readbytes); |
07bbc92c | 2187 | } |
0f113f3e MC |
2188 | } |
2189 | ||
4ee7d3f9 | 2190 | int SSL_read(SSL *s, void *buf, int num) |
eda75751 MC |
2191 | { |
2192 | int ret; | |
54105ddd | 2193 | size_t readbytes; |
eda75751 MC |
2194 | |
2195 | if (num < 0) { | |
6849b73c | 2196 | ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH); |
eda75751 MC |
2197 | return -1; |
2198 | } | |
2199 | ||
4ee7d3f9 | 2200 | ret = ssl_read_internal(s, buf, (size_t)num, &readbytes); |
eda75751 MC |
2201 | |
2202 | /* | |
2203 | * The cast is safe here because ret should be <= INT_MAX because num is | |
2204 | * <= INT_MAX | |
2205 | */ | |
2206 | if (ret > 0) | |
54105ddd | 2207 | ret = (int)readbytes; |
eda75751 MC |
2208 | |
2209 | return ret; | |
2210 | } | |
2211 | ||
4ee7d3f9 KR |
2212 | int SSL_read_ex(SSL *s, void *buf, size_t num, size_t *readbytes) |
2213 | { | |
2214 | int ret = ssl_read_internal(s, buf, num, readbytes); | |
2215 | ||
2216 | if (ret < 0) | |
2217 | ret = 0; | |
2218 | return ret; | |
2219 | } | |
2220 | ||
f533fbd4 | 2221 | int SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes) |
d781d247 MC |
2222 | { |
2223 | int ret; | |
38b051a1 TM |
2224 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
2225 | ||
2226 | /* TODO(QUIC): This will need special handling for QUIC */ | |
2227 | if (sc == NULL) | |
2228 | return 0; | |
d781d247 | 2229 | |
38b051a1 | 2230 | if (!sc->server) { |
6849b73c | 2231 | ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
f533fbd4 | 2232 | return SSL_READ_EARLY_DATA_ERROR; |
d781d247 MC |
2233 | } |
2234 | ||
38b051a1 | 2235 | switch (sc->early_data_state) { |
d781d247 MC |
2236 | case SSL_EARLY_DATA_NONE: |
2237 | if (!SSL_in_before(s)) { | |
6849b73c | 2238 | ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
f533fbd4 | 2239 | return SSL_READ_EARLY_DATA_ERROR; |
d781d247 MC |
2240 | } |
2241 | /* fall through */ | |
2242 | ||
2243 | case SSL_EARLY_DATA_ACCEPT_RETRY: | |
38b051a1 | 2244 | sc->early_data_state = SSL_EARLY_DATA_ACCEPTING; |
d781d247 MC |
2245 | ret = SSL_accept(s); |
2246 | if (ret <= 0) { | |
2247 | /* NBIO or error */ | |
38b051a1 | 2248 | sc->early_data_state = SSL_EARLY_DATA_ACCEPT_RETRY; |
f533fbd4 | 2249 | return SSL_READ_EARLY_DATA_ERROR; |
d781d247 MC |
2250 | } |
2251 | /* fall through */ | |
2252 | ||
2253 | case SSL_EARLY_DATA_READ_RETRY: | |
38b051a1 TM |
2254 | if (sc->ext.early_data == SSL_EARLY_DATA_ACCEPTED) { |
2255 | sc->early_data_state = SSL_EARLY_DATA_READING; | |
d781d247 MC |
2256 | ret = SSL_read_ex(s, buf, num, readbytes); |
2257 | /* | |
ef6c191b MC |
2258 | * State machine will update early_data_state to |
2259 | * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData | |
2260 | * message | |
d781d247 | 2261 | */ |
38b051a1 | 2262 | if (ret > 0 || (ret <= 0 && sc->early_data_state |
d781d247 | 2263 | != SSL_EARLY_DATA_FINISHED_READING)) { |
38b051a1 | 2264 | sc->early_data_state = SSL_EARLY_DATA_READ_RETRY; |
f533fbd4 MC |
2265 | return ret > 0 ? SSL_READ_EARLY_DATA_SUCCESS |
2266 | : SSL_READ_EARLY_DATA_ERROR; | |
d781d247 MC |
2267 | } |
2268 | } else { | |
38b051a1 | 2269 | sc->early_data_state = SSL_EARLY_DATA_FINISHED_READING; |
d781d247 MC |
2270 | } |
2271 | *readbytes = 0; | |
f533fbd4 | 2272 | return SSL_READ_EARLY_DATA_FINISH; |
d781d247 MC |
2273 | |
2274 | default: | |
6849b73c | 2275 | ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
f533fbd4 | 2276 | return SSL_READ_EARLY_DATA_ERROR; |
d781d247 MC |
2277 | } |
2278 | } | |
2279 | ||
f5b519c4 | 2280 | int SSL_get_early_data_status(const SSL *s) |
1ea4d09a | 2281 | { |
38b051a1 TM |
2282 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s); |
2283 | ||
2284 | /* TODO(QUIC): This will need special handling for QUIC */ | |
2285 | if (sc == NULL) | |
2286 | return 0; | |
2287 | ||
2288 | return sc->ext.early_data; | |
1ea4d09a MC |
2289 | } |
2290 | ||
4ee7d3f9 | 2291 | static int ssl_peek_internal(SSL *s, void *buf, size_t num, size_t *readbytes) |
0f113f3e | 2292 | { |
38b051a1 TM |
2293 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2294 | ||
2295 | if (sc == NULL) | |
2296 | return 0; | |
2297 | ||
2298 | if (sc->handshake_func == NULL) { | |
6849b73c | 2299 | ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED); |
0f113f3e MC |
2300 | return -1; |
2301 | } | |
2302 | ||
38b051a1 | 2303 | if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) { |
4ee7d3f9 | 2304 | return 0; |
0f113f3e | 2305 | } |
38b051a1 | 2306 | if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) { |
add2f5ca | 2307 | struct ssl_async_args args; |
eda75751 | 2308 | int ret; |
0f113f3e | 2309 | |
add2f5ca MC |
2310 | args.s = s; |
2311 | args.buf = buf; | |
2312 | args.num = num; | |
ec447924 MC |
2313 | args.type = READFUNC; |
2314 | args.f.func_read = s->method->ssl_peek; | |
07bbc92c | 2315 | |
eda75751 | 2316 | ret = ssl_start_async_job(s, &args, ssl_io_intern); |
38b051a1 | 2317 | *readbytes = sc->asyncrw; |
eda75751 | 2318 | return ret; |
add2f5ca | 2319 | } else { |
54105ddd | 2320 | return s->method->ssl_peek(s, buf, num, readbytes); |
add2f5ca | 2321 | } |
07bbc92c MC |
2322 | } |
2323 | ||
4ee7d3f9 | 2324 | int SSL_peek(SSL *s, void *buf, int num) |
7ee8627f MC |
2325 | { |
2326 | int ret; | |
4ee7d3f9 | 2327 | size_t readbytes; |
7ee8627f MC |
2328 | |
2329 | if (num < 0) { | |
6849b73c | 2330 | ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH); |
7ee8627f MC |
2331 | return -1; |
2332 | } | |
2333 | ||
4ee7d3f9 | 2334 | ret = ssl_peek_internal(s, buf, (size_t)num, &readbytes); |
7ee8627f MC |
2335 | |
2336 | /* | |
2337 | * The cast is safe here because ret should be <= INT_MAX because num is | |
2338 | * <= INT_MAX | |
2339 | */ | |
2340 | if (ret > 0) | |
4ee7d3f9 | 2341 | ret = (int)readbytes; |
7ee8627f MC |
2342 | |
2343 | return ret; | |
2344 | } | |
2345 | ||
4ee7d3f9 KR |
2346 | |
2347 | int SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *readbytes) | |
2348 | { | |
2349 | int ret = ssl_peek_internal(s, buf, num, readbytes); | |
2350 | ||
2351 | if (ret < 0) | |
2352 | ret = 0; | |
2353 | return ret; | |
2354 | } | |
2355 | ||
2356 | int ssl_write_internal(SSL *s, const void *buf, size_t num, size_t *written) | |
0f113f3e | 2357 | { |
38b051a1 TM |
2358 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2359 | ||
2360 | if (sc == NULL) | |
2361 | return 0; | |
2362 | ||
2363 | if (sc->handshake_func == NULL) { | |
6849b73c | 2364 | ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED); |
0f113f3e MC |
2365 | return -1; |
2366 | } | |
2367 | ||
38b051a1 TM |
2368 | if (sc->shutdown & SSL_SENT_SHUTDOWN) { |
2369 | sc->rwstate = SSL_NOTHING; | |
6849b73c | 2370 | ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
4ee7d3f9 | 2371 | return -1; |
0f113f3e | 2372 | } |
07bbc92c | 2373 | |
38b051a1 TM |
2374 | if (sc->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY |
2375 | || sc->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY | |
2376 | || sc->early_data_state == SSL_EARLY_DATA_READ_RETRY) { | |
6849b73c | 2377 | ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
49e7fe12 | 2378 | return 0; |
0a5ece5b | 2379 | } |
564547e4 | 2380 | /* If we are a client and haven't sent the Finished we better do that */ |
38b051a1 | 2381 | ossl_statem_check_finish_init(sc, 1); |
49e7fe12 | 2382 | |
38b051a1 | 2383 | if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) { |
7ee8627f | 2384 | int ret; |
add2f5ca MC |
2385 | struct ssl_async_args args; |
2386 | ||
2387 | args.s = s; | |
2388 | args.buf = (void *)buf; | |
2389 | args.num = num; | |
ec447924 MC |
2390 | args.type = WRITEFUNC; |
2391 | args.f.func_write = s->method->ssl_write; | |
add2f5ca | 2392 | |
7ee8627f | 2393 | ret = ssl_start_async_job(s, &args, ssl_io_intern); |
38b051a1 | 2394 | *written = sc->asyncrw; |
7ee8627f | 2395 | return ret; |
07bbc92c | 2396 | } else { |
7ee8627f | 2397 | return s->method->ssl_write(s, buf, num, written); |
07bbc92c | 2398 | } |
0f113f3e | 2399 | } |
d02b48c6 | 2400 | |
7c3a7561 BP |
2401 | ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags) |
2402 | { | |
2403 | ossl_ssize_t ret; | |
38b051a1 | 2404 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
7c3a7561 | 2405 | |
38b051a1 TM |
2406 | if (sc == NULL) |
2407 | return 0; | |
2408 | ||
2409 | if (sc->handshake_func == NULL) { | |
6849b73c | 2410 | ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED); |
7c3a7561 BP |
2411 | return -1; |
2412 | } | |
2413 | ||
38b051a1 TM |
2414 | if (sc->shutdown & SSL_SENT_SHUTDOWN) { |
2415 | sc->rwstate = SSL_NOTHING; | |
6849b73c | 2416 | ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
7c3a7561 BP |
2417 | return -1; |
2418 | } | |
2419 | ||
38b051a1 | 2420 | if (!BIO_get_ktls_send(sc->wbio)) { |
6849b73c | 2421 | ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED); |
7c3a7561 BP |
2422 | return -1; |
2423 | } | |
2424 | ||
2425 | /* If we have an alert to send, lets send it */ | |
38b051a1 | 2426 | if (sc->s3.alert_dispatch) { |
7c3a7561 BP |
2427 | ret = (ossl_ssize_t)s->method->ssl_dispatch_alert(s); |
2428 | if (ret <= 0) { | |
2429 | /* SSLfatal() already called if appropriate */ | |
2430 | return ret; | |
2431 | } | |
2432 | /* if it went, fall through and send more stuff */ | |
2433 | } | |
2434 | ||
38b051a1 TM |
2435 | sc->rwstate = SSL_WRITING; |
2436 | if (BIO_flush(sc->wbio) <= 0) { | |
2437 | if (!BIO_should_retry(sc->wbio)) { | |
2438 | sc->rwstate = SSL_NOTHING; | |
7c3a7561 BP |
2439 | } else { |
2440 | #ifdef EAGAIN | |
2441 | set_sys_error(EAGAIN); | |
2442 | #endif | |
2443 | } | |
2444 | return -1; | |
2445 | } | |
2446 | ||
712c0942 | 2447 | #ifdef OPENSSL_NO_KTLS |
fa7a8074 RL |
2448 | ERR_raise_data(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR, |
2449 | "can't call ktls_sendfile(), ktls disabled"); | |
712c0942 | 2450 | return -1; |
7c3a7561 | 2451 | #else |
712c0942 | 2452 | ret = ktls_sendfile(SSL_get_wfd(s), fd, offset, size, flags); |
7c3a7561 BP |
2453 | if (ret < 0) { |
2454 | #if defined(EAGAIN) && defined(EINTR) && defined(EBUSY) | |
2455 | if ((get_last_sys_error() == EAGAIN) || | |
2456 | (get_last_sys_error() == EINTR) || | |
2457 | (get_last_sys_error() == EBUSY)) | |
38b051a1 | 2458 | BIO_set_retry_write(sc->wbio); |
7c3a7561 BP |
2459 | else |
2460 | #endif | |
6849b73c | 2461 | ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED); |
7c3a7561 BP |
2462 | return ret; |
2463 | } | |
38b051a1 | 2464 | sc->rwstate = SSL_NOTHING; |
7c3a7561 | 2465 | return ret; |
712c0942 | 2466 | #endif |
7c3a7561 BP |
2467 | } |
2468 | ||
4ee7d3f9 KR |
2469 | int SSL_write(SSL *s, const void *buf, int num) |
2470 | { | |
2471 | int ret; | |
2472 | size_t written; | |
2473 | ||
2474 | if (num < 0) { | |
6849b73c | 2475 | ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH); |
4ee7d3f9 KR |
2476 | return -1; |
2477 | } | |
2478 | ||
2479 | ret = ssl_write_internal(s, buf, (size_t)num, &written); | |
2480 | ||
2481 | /* | |
2482 | * The cast is safe here because ret should be <= INT_MAX because num is | |
2483 | * <= INT_MAX | |
2484 | */ | |
2485 | if (ret > 0) | |
2486 | ret = (int)written; | |
2487 | ||
2488 | return ret; | |
2489 | } | |
2490 | ||
2491 | int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written) | |
2492 | { | |
2493 | int ret = ssl_write_internal(s, buf, num, written); | |
2494 | ||
2495 | if (ret < 0) | |
2496 | ret = 0; | |
2497 | return ret; | |
2498 | } | |
2499 | ||
0665b4ed | 2500 | int SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written) |
49e7fe12 | 2501 | { |
a0cb628b | 2502 | int ret, early_data_state; |
2a8db717 | 2503 | size_t writtmp; |
f7414b08 | 2504 | uint32_t partialwrite; |
38b051a1 TM |
2505 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
2506 | ||
2507 | /* TODO(QUIC): This will need special handling for QUIC */ | |
2508 | if (sc == NULL) | |
2509 | return 0; | |
49e7fe12 | 2510 | |
38b051a1 | 2511 | switch (sc->early_data_state) { |
49e7fe12 | 2512 | case SSL_EARLY_DATA_NONE: |
38b051a1 | 2513 | if (sc->server |
09f28874 | 2514 | || !SSL_in_before(s) |
38b051a1 TM |
2515 | || ((sc->session == NULL || sc->session->ext.max_early_data == 0) |
2516 | && (sc->psk_use_session_cb == NULL))) { | |
6849b73c | 2517 | ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
49e7fe12 MC |
2518 | return 0; |
2519 | } | |
2520 | /* fall through */ | |
2521 | ||
2522 | case SSL_EARLY_DATA_CONNECT_RETRY: | |
38b051a1 | 2523 | sc->early_data_state = SSL_EARLY_DATA_CONNECTING; |
49e7fe12 MC |
2524 | ret = SSL_connect(s); |
2525 | if (ret <= 0) { | |
2526 | /* NBIO or error */ | |
38b051a1 | 2527 | sc->early_data_state = SSL_EARLY_DATA_CONNECT_RETRY; |
49e7fe12 MC |
2528 | return 0; |
2529 | } | |
2530 | /* fall through */ | |
2531 | ||
2532 | case SSL_EARLY_DATA_WRITE_RETRY: | |
38b051a1 | 2533 | sc->early_data_state = SSL_EARLY_DATA_WRITING; |
f7414b08 MC |
2534 | /* |
2535 | * We disable partial write for early data because we don't keep track | |
2536 | * of how many bytes we've written between the SSL_write_ex() call and | |
2537 | * the flush if the flush needs to be retried) | |
2538 | */ | |
38b051a1 TM |
2539 | partialwrite = sc->mode & SSL_MODE_ENABLE_PARTIAL_WRITE; |
2540 | sc->mode &= ~SSL_MODE_ENABLE_PARTIAL_WRITE; | |
2a8db717 | 2541 | ret = SSL_write_ex(s, buf, num, &writtmp); |
38b051a1 | 2542 | sc->mode |= partialwrite; |
2a8db717 | 2543 | if (!ret) { |
38b051a1 | 2544 | sc->early_data_state = SSL_EARLY_DATA_WRITE_RETRY; |
2a8db717 MC |
2545 | return ret; |
2546 | } | |
38b051a1 | 2547 | sc->early_data_state = SSL_EARLY_DATA_WRITE_FLUSH; |
2a8db717 MC |
2548 | /* fall through */ |
2549 | ||
2550 | case SSL_EARLY_DATA_WRITE_FLUSH: | |
2551 | /* The buffering BIO is still in place so we need to flush it */ | |
38b051a1 | 2552 | if (statem_flush(sc) != 1) |
2a8db717 | 2553 | return 0; |
2a8db717 | 2554 | *written = num; |
38b051a1 | 2555 | sc->early_data_state = SSL_EARLY_DATA_WRITE_RETRY; |
2a8db717 | 2556 | return 1; |
49e7fe12 | 2557 | |
116d0da5 | 2558 | case SSL_EARLY_DATA_FINISHED_READING: |
a0cb628b | 2559 | case SSL_EARLY_DATA_READ_RETRY: |
38b051a1 | 2560 | early_data_state = sc->early_data_state; |
09f28874 | 2561 | /* We are a server writing to an unauthenticated client */ |
38b051a1 | 2562 | sc->early_data_state = SSL_EARLY_DATA_UNAUTH_WRITING; |
09f28874 | 2563 | ret = SSL_write_ex(s, buf, num, written); |
5fe37157 MC |
2564 | /* The buffering BIO is still in place */ |
2565 | if (ret) | |
38b051a1 TM |
2566 | (void)BIO_flush(sc->wbio); |
2567 | sc->early_data_state = early_data_state; | |
09f28874 MC |
2568 | return ret; |
2569 | ||
49e7fe12 | 2570 | default: |
6849b73c | 2571 | ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
49e7fe12 MC |
2572 | return 0; |
2573 | } | |
2574 | } | |
2575 | ||
4f43d0e7 | 2576 | int SSL_shutdown(SSL *s) |
0f113f3e MC |
2577 | { |
2578 | /* | |
2579 | * Note that this function behaves differently from what one might | |
2580 | * expect. Return values are 0 for no success (yet), 1 for success; but | |
2581 | * calling it once is usually not enough, even if blocking I/O is used | |
2582 | * (see ssl3_shutdown). | |
2583 | */ | |
38b051a1 | 2584 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
0f113f3e | 2585 | |
38b051a1 TM |
2586 | if (sc == NULL) |
2587 | return -1; | |
2588 | ||
2589 | if (sc->handshake_func == NULL) { | |
6849b73c | 2590 | ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED); |
0f113f3e MC |
2591 | return -1; |
2592 | } | |
2593 | ||
64f9f406 | 2594 | if (!SSL_in_init(s)) { |
38b051a1 | 2595 | if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) { |
64f9f406 | 2596 | struct ssl_async_args args; |
ec447924 | 2597 | |
09134f18 | 2598 | memset(&args, 0, sizeof(args)); |
64f9f406 MC |
2599 | args.s = s; |
2600 | args.type = OTHERFUNC; | |
2601 | args.f.func_other = s->method->ssl_shutdown; | |
ec447924 | 2602 | |
64f9f406 MC |
2603 | return ssl_start_async_job(s, &args, ssl_io_intern); |
2604 | } else { | |
2605 | return s->method->ssl_shutdown(s); | |
2606 | } | |
ec447924 | 2607 | } else { |
6849b73c | 2608 | ERR_raise(ERR_LIB_SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT); |
64f9f406 | 2609 | return -1; |
ec447924 | 2610 | } |
0f113f3e | 2611 | } |
d02b48c6 | 2612 | |
4fbfe86a | 2613 | int SSL_key_update(SSL *s, int updatetype) |
44c04a2e | 2614 | { |
38b051a1 TM |
2615 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2616 | ||
2617 | if (sc == NULL) | |
2618 | return 0; | |
2619 | ||
2620 | if (!SSL_CONNECTION_IS_TLS13(sc)) { | |
6849b73c | 2621 | ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION); |
44c04a2e MC |
2622 | return 0; |
2623 | } | |
2624 | ||
2625 | if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED | |
2626 | && updatetype != SSL_KEY_UPDATE_REQUESTED) { | |
6849b73c | 2627 | ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_KEY_UPDATE_TYPE); |
44c04a2e MC |
2628 | return 0; |
2629 | } | |
2630 | ||
2631 | if (!SSL_is_init_finished(s)) { | |
6849b73c | 2632 | ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT); |
44c04a2e MC |
2633 | return 0; |
2634 | } | |
2635 | ||
38b051a1 | 2636 | if (RECORD_LAYER_write_pending(&sc->rlayer)) { |
3bec4851 MC |
2637 | ERR_raise(ERR_LIB_SSL, SSL_R_BAD_WRITE_RETRY); |
2638 | return 0; | |
2639 | } | |
2640 | ||
38b051a1 TM |
2641 | ossl_statem_set_in_init(sc, 1); |
2642 | sc->key_update = updatetype; | |
44c04a2e MC |
2643 | return 1; |
2644 | } | |
2645 | ||
3499327b | 2646 | int SSL_get_key_update_type(const SSL *s) |
53d1d07d | 2647 | { |
38b051a1 TM |
2648 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
2649 | ||
2650 | if (sc == NULL) | |
2651 | return 0; | |
2652 | ||
2653 | return sc->key_update; | |
53d1d07d MC |
2654 | } |
2655 | ||
55373bfd RS |
2656 | /* |
2657 | * Can we accept a renegotiation request? If yes, set the flag and | |
2658 | * return 1 if yes. If not, raise error and return 0. | |
2659 | */ | |
38b051a1 | 2660 | static int can_renegotiate(const SSL_CONNECTION *sc) |
0f113f3e | 2661 | { |
38b051a1 | 2662 | if (SSL_CONNECTION_IS_TLS13(sc)) { |
6849b73c | 2663 | ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION); |
2c0980d2 | 2664 | return 0; |
44c04a2e | 2665 | } |
cda6b998 | 2666 | |
38b051a1 | 2667 | if ((sc->options & SSL_OP_NO_RENEGOTIATION) != 0) { |
6849b73c | 2668 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_RENEGOTIATION); |
db0f35dd TS |
2669 | return 0; |
2670 | } | |
44959ee4 | 2671 | |
55373bfd RS |
2672 | return 1; |
2673 | } | |
2674 | ||
2675 | int SSL_renegotiate(SSL *s) | |
2676 | { | |
38b051a1 TM |
2677 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
2678 | ||
2679 | if (sc == NULL) | |
55373bfd RS |
2680 | return 0; |
2681 | ||
38b051a1 TM |
2682 | if (!can_renegotiate(sc)) |
2683 | return 0; | |
2684 | ||
2685 | sc->renegotiate = 1; | |
2686 | sc->new_session = 1; | |
26a7d938 | 2687 | return s->method->ssl_renegotiate(s); |
0f113f3e | 2688 | } |
d02b48c6 | 2689 | |
44959ee4 | 2690 | int SSL_renegotiate_abbreviated(SSL *s) |
0f113f3e | 2691 | { |
38b051a1 TM |
2692 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
2693 | ||
2694 | if (sc == NULL) | |
2695 | return 0; | |
2696 | ||
2697 | if (!can_renegotiate(sc)) | |
2c0980d2 | 2698 | return 0; |
c519e89f | 2699 | |
38b051a1 TM |
2700 | sc->renegotiate = 1; |
2701 | sc->new_session = 0; | |
26a7d938 | 2702 | return s->method->ssl_renegotiate(s); |
0f113f3e | 2703 | } |
44959ee4 | 2704 | |
3499327b | 2705 | int SSL_renegotiate_pending(const SSL *s) |
0f113f3e | 2706 | { |
38b051a1 TM |
2707 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
2708 | ||
2709 | if (sc == NULL) | |
2710 | return 0; | |
2711 | ||
0f113f3e MC |
2712 | /* |
2713 | * becomes true when negotiation is requested; false again once a | |
2714 | * handshake has finished | |
2715 | */ | |
38b051a1 | 2716 | return (sc->renegotiate != 0); |
0f113f3e MC |
2717 | } |
2718 | ||
3bfacb5f BK |
2719 | int SSL_new_session_ticket(SSL *s) |
2720 | { | |
38b051a1 TM |
2721 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2722 | ||
2723 | if (sc == NULL) | |
2724 | return 0; | |
2725 | ||
7c73fefe | 2726 | /* If we are in init because we're sending tickets, okay to send more. */ |
38b051a1 TM |
2727 | if ((SSL_in_init(s) && sc->ext.extra_tickets_expected == 0) |
2728 | || SSL_IS_FIRST_HANDSHAKE(sc) || !sc->server | |
2729 | || !SSL_CONNECTION_IS_TLS13(sc)) | |
35774d55 | 2730 | return 0; |
38b051a1 TM |
2731 | sc->ext.extra_tickets_expected++; |
2732 | if (!RECORD_LAYER_write_pending(&sc->rlayer) && !SSL_in_init(s)) | |
2733 | ossl_statem_set_in_init(sc, 1); | |
35774d55 | 2734 | return 1; |
3bfacb5f BK |
2735 | } |
2736 | ||
0f113f3e MC |
2737 | long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) |
2738 | { | |
2739 | long l; | |
38b051a1 TM |
2740 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2741 | ||
2742 | /* TODO(QUIC): Special handling for some ctrls will be needed */ | |
2743 | if (sc == NULL) | |
2744 | return 0; | |
0f113f3e MC |
2745 | |
2746 | switch (cmd) { | |
2747 | case SSL_CTRL_GET_READ_AHEAD: | |
38b051a1 | 2748 | return RECORD_LAYER_get_read_ahead(&sc->rlayer); |
0f113f3e | 2749 | case SSL_CTRL_SET_READ_AHEAD: |
38b051a1 TM |
2750 | l = RECORD_LAYER_get_read_ahead(&sc->rlayer); |
2751 | RECORD_LAYER_set_read_ahead(&sc->rlayer, larg); | |
26a7d938 | 2752 | return l; |
0f113f3e MC |
2753 | |
2754 | case SSL_CTRL_SET_MSG_CALLBACK_ARG: | |
38b051a1 | 2755 | sc->msg_callback_arg = parg; |
0f113f3e MC |
2756 | return 1; |
2757 | ||
0f113f3e | 2758 | case SSL_CTRL_MODE: |
4566dae7 MC |
2759 | { |
2760 | OSSL_PARAM options[2], *opts = options; | |
2761 | ||
2762 | sc->mode |= larg; | |
2763 | ||
2764 | *opts++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE, | |
2765 | &sc->mode); | |
2766 | *opts = OSSL_PARAM_construct_end(); | |
2767 | ||
2768 | /* Ignore return value */ | |
2769 | sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options); | |
2770 | ||
2771 | return sc->mode; | |
2772 | } | |
0f113f3e | 2773 | case SSL_CTRL_CLEAR_MODE: |
38b051a1 | 2774 | return (sc->mode &= ~larg); |
0f113f3e | 2775 | case SSL_CTRL_GET_MAX_CERT_LIST: |
38b051a1 | 2776 | return (long)sc->max_cert_list; |
0f113f3e | 2777 | case SSL_CTRL_SET_MAX_CERT_LIST: |
348240c6 MC |
2778 | if (larg < 0) |
2779 | return 0; | |
38b051a1 TM |
2780 | l = (long)sc->max_cert_list; |
2781 | sc->max_cert_list = (size_t)larg; | |
348240c6 | 2782 | return l; |
0f113f3e MC |
2783 | case SSL_CTRL_SET_MAX_SEND_FRAGMENT: |
2784 | if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) | |
2785 | return 0; | |
50ec7505 | 2786 | #ifndef OPENSSL_NO_KTLS |
38b051a1 | 2787 | if (sc->wbio != NULL && BIO_get_ktls_send(sc->wbio)) |
50ec7505 BP |
2788 | return 0; |
2789 | #endif /* OPENSSL_NO_KTLS */ | |
38b051a1 TM |
2790 | sc->max_send_fragment = larg; |
2791 | if (sc->max_send_fragment < sc->split_send_fragment) | |
2792 | sc->split_send_fragment = sc->max_send_fragment; | |
d102d9df MC |
2793 | return 1; |
2794 | case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT: | |
38b051a1 | 2795 | if ((size_t)larg > sc->max_send_fragment || larg == 0) |
d102d9df | 2796 | return 0; |
38b051a1 | 2797 | sc->split_send_fragment = larg; |
0f113f3e | 2798 | return 1; |
d102d9df MC |
2799 | case SSL_CTRL_SET_MAX_PIPELINES: |
2800 | if (larg < 1 || larg > SSL_MAX_PIPELINES) | |
2801 | return 0; | |
38b051a1 | 2802 | sc->max_pipelines = larg; |
cffafb5f MC |
2803 | if (sc->rlayer.rrlmethod->set_max_pipelines != NULL) |
2804 | sc->rlayer.rrlmethod->set_max_pipelines(sc->rlayer.rrl, (size_t)larg); | |
07077415 | 2805 | return 1; |
0f113f3e | 2806 | case SSL_CTRL_GET_RI_SUPPORT: |
38b051a1 | 2807 | return sc->s3.send_connection_binding; |
dfb39f73 | 2808 | case SSL_CTRL_SET_RETRY_VERIFY: |
38b051a1 | 2809 | sc->rwstate = SSL_RETRY_VERIFY; |
dfb39f73 | 2810 | return 1; |
0f113f3e | 2811 | case SSL_CTRL_CERT_FLAGS: |
38b051a1 | 2812 | return (sc->cert->cert_flags |= larg); |
0f113f3e | 2813 | case SSL_CTRL_CLEAR_CERT_FLAGS: |
38b051a1 | 2814 | return (sc->cert->cert_flags &= ~larg); |
0f113f3e MC |
2815 | |
2816 | case SSL_CTRL_GET_RAW_CIPHERLIST: | |
2817 | if (parg) { | |
38b051a1 | 2818 | if (sc->s3.tmp.ciphers_raw == NULL) |
0f113f3e | 2819 | return 0; |
38b051a1 TM |
2820 | *(unsigned char **)parg = sc->s3.tmp.ciphers_raw; |
2821 | return (int)sc->s3.tmp.ciphers_rawlen; | |
e9fa092e EK |
2822 | } else { |
2823 | return TLS_CIPHER_LEN; | |
2824 | } | |
c5364614 | 2825 | case SSL_CTRL_GET_EXTMS_SUPPORT: |
38b051a1 | 2826 | if (!sc->session || SSL_in_init(s) || ossl_statem_get_in_handshake(sc)) |
a230b26e | 2827 | return -1; |
38b051a1 | 2828 | if (sc->session->flags & SSL_SESS_FLAG_EXTMS) |
c5364614 DSH |
2829 | return 1; |
2830 | else | |
2831 | return 0; | |
7946ab33 | 2832 | case SSL_CTRL_SET_MIN_PROTO_VERSION: |
38b051a1 | 2833 | return ssl_check_allowed_versions(larg, sc->max_proto_version) |
c8feba72 | 2834 | && ssl_set_version_bound(s->ctx->method->version, (int)larg, |
38b051a1 | 2835 | &sc->min_proto_version); |
3edabd3c | 2836 | case SSL_CTRL_GET_MIN_PROTO_VERSION: |
38b051a1 | 2837 | return sc->min_proto_version; |
7946ab33 | 2838 | case SSL_CTRL_SET_MAX_PROTO_VERSION: |
38b051a1 | 2839 | return ssl_check_allowed_versions(sc->min_proto_version, larg) |
c8feba72 | 2840 | && ssl_set_version_bound(s->ctx->method->version, (int)larg, |
38b051a1 | 2841 | &sc->max_proto_version); |
3edabd3c | 2842 | case SSL_CTRL_GET_MAX_PROTO_VERSION: |
38b051a1 | 2843 | return sc->max_proto_version; |
0f113f3e | 2844 | default: |
26a7d938 | 2845 | return s->method->ssl_ctrl(s, cmd, larg, parg); |
0f113f3e MC |
2846 | } |
2847 | } | |
2848 | ||
2849 | long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void)) | |
2850 | { | |
38b051a1 TM |
2851 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2852 | ||
2853 | if (sc == NULL) | |
2854 | return 0; | |
2855 | ||
0f113f3e MC |
2856 | switch (cmd) { |
2857 | case SSL_CTRL_SET_MSG_CALLBACK: | |
38b051a1 TM |
2858 | sc->msg_callback = (void (*) |
2859 | (int write_p, int version, int content_type, | |
2860 | const void *buf, size_t len, SSL *ssl, | |
2861 | void *arg))(fp); | |
0f113f3e MC |
2862 | return 1; |
2863 | ||
2864 | default: | |
26a7d938 | 2865 | return s->method->ssl_callback_ctrl(s, cmd, fp); |
0f113f3e MC |
2866 | } |
2867 | } | |
d3442bc7 | 2868 | |
3c1d6bbc | 2869 | LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) |
0f113f3e MC |
2870 | { |
2871 | return ctx->sessions; | |
2872 | } | |
2873 | ||
acce0557 P |
2874 | static int ssl_tsan_load(SSL_CTX *ctx, TSAN_QUALIFIER int *stat) |
2875 | { | |
2876 | int res = 0; | |
2877 | ||
2878 | if (ssl_tsan_lock(ctx)) { | |
2879 | res = tsan_load(stat); | |
2880 | ssl_tsan_unlock(ctx); | |
2881 | } | |
2882 | return res; | |
2883 | } | |
2884 | ||
0f113f3e MC |
2885 | long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) |
2886 | { | |
2887 | long l; | |
2888 | /* For some cases with ctx == NULL perform syntax checks */ | |
2889 | if (ctx == NULL) { | |
2890 | switch (cmd) { | |
de4d764e | 2891 | case SSL_CTRL_SET_GROUPS_LIST: |
260009d8 | 2892 | return tls1_set_groups_list(ctx, NULL, NULL, parg); |
0f113f3e MC |
2893 | case SSL_CTRL_SET_SIGALGS_LIST: |
2894 | case SSL_CTRL_SET_CLIENT_SIGALGS_LIST: | |
2895 | return tls1_set_sigalgs_list(NULL, parg, 0); | |
2896 | default: | |
2897 | return 0; | |
2898 | } | |
2899 | } | |
2900 | ||
2901 | switch (cmd) { | |
2902 | case SSL_CTRL_GET_READ_AHEAD: | |
26a7d938 | 2903 | return ctx->read_ahead; |
0f113f3e MC |
2904 | case SSL_CTRL_SET_READ_AHEAD: |
2905 | l = ctx->read_ahead; | |
2906 | ctx->read_ahead = larg; | |
26a7d938 | 2907 | return l; |
0f113f3e MC |
2908 | |
2909 | case SSL_CTRL_SET_MSG_CALLBACK_ARG: | |
2910 | ctx->msg_callback_arg = parg; | |
2911 | return 1; | |
2912 | ||
2913 | case SSL_CTRL_GET_MAX_CERT_LIST: | |
26a7d938 | 2914 | return (long)ctx->max_cert_list; |
0f113f3e | 2915 | case SSL_CTRL_SET_MAX_CERT_LIST: |
348240c6 MC |
2916 | if (larg < 0) |
2917 | return 0; | |
2918 | l = (long)ctx->max_cert_list; | |
2919 | ctx->max_cert_list = (size_t)larg; | |
2920 | return l; | |
0f113f3e MC |
2921 | |
2922 | case SSL_CTRL_SET_SESS_CACHE_SIZE: | |
348240c6 MC |
2923 | if (larg < 0) |
2924 | return 0; | |
2925 | l = (long)ctx->session_cache_size; | |
2926 | ctx->session_cache_size = (size_t)larg; | |
2927 | return l; | |
0f113f3e | 2928 | case SSL_CTRL_GET_SESS_CACHE_SIZE: |
26a7d938 | 2929 | return (long)ctx->session_cache_size; |
0f113f3e MC |
2930 | case SSL_CTRL_SET_SESS_CACHE_MODE: |
2931 | l = ctx->session_cache_mode; | |
2932 | ctx->session_cache_mode = larg; | |
26a7d938 | 2933 | return l; |
0f113f3e | 2934 | case SSL_CTRL_GET_SESS_CACHE_MODE: |
26a7d938 | 2935 | return ctx->session_cache_mode; |
0f113f3e MC |
2936 | |
2937 | case SSL_CTRL_SESS_NUMBER: | |
26a7d938 | 2938 | return lh_SSL_SESSION_num_items(ctx->sessions); |
0f113f3e | 2939 | case SSL_CTRL_SESS_CONNECT: |
acce0557 | 2940 | return ssl_tsan_load(ctx, &ctx->stats.sess_connect); |
0f113f3e | 2941 | case SSL_CTRL_SESS_CONNECT_GOOD: |
acce0557 | 2942 | return ssl_tsan_load(ctx, &ctx->stats.sess_connect_good); |
0f113f3e | 2943 | case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: |
acce0557 | 2944 | return ssl_tsan_load(ctx, &ctx->stats.sess_connect_renegotiate); |
0f113f3e | 2945 | case SSL_CTRL_SESS_ACCEPT: |
acce0557 | 2946 | return ssl_tsan_load(ctx, &ctx->stats.sess_accept); |
0f113f3e | 2947 | case SSL_CTRL_SESS_ACCEPT_GOOD: |
acce0557 | 2948 | return ssl_tsan_load(ctx, &ctx->stats.sess_accept_good); |
0f113f3e | 2949 | case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: |
acce0557 | 2950 | return ssl_tsan_load(ctx, &ctx->stats.sess_accept_renegotiate); |
0f113f3e | 2951 | case SSL_CTRL_SESS_HIT: |
acce0557 | 2952 | return ssl_tsan_load(ctx, &ctx->stats.sess_hit); |
0f113f3e | 2953 | case SSL_CTRL_SESS_CB_HIT: |
acce0557 | 2954 | return ssl_tsan_load(ctx, &ctx->stats.sess_cb_hit); |
0f113f3e | 2955 | case SSL_CTRL_SESS_MISSES: |
acce0557 | 2956 | return ssl_tsan_load(ctx, &ctx->stats.sess_miss); |
0f113f3e | 2957 | case SSL_CTRL_SESS_TIMEOUTS: |
acce0557 | 2958 | return ssl_tsan_load(ctx, &ctx->stats.sess_timeout); |
0f113f3e | 2959 | case SSL_CTRL_SESS_CACHE_FULL: |
acce0557 | 2960 | return ssl_tsan_load(ctx, &ctx->stats.sess_cache_full); |
0f113f3e MC |
2961 | case SSL_CTRL_MODE: |
2962 | return (ctx->mode |= larg); | |
2963 | case SSL_CTRL_CLEAR_MODE: | |
2964 | return (ctx->mode &= ~larg); | |
2965 | case SSL_CTRL_SET_MAX_SEND_FRAGMENT: | |
2966 | if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) | |
2967 | return 0; | |
2968 | ctx->max_send_fragment = larg; | |
d102d9df | 2969 | if (ctx->max_send_fragment < ctx->split_send_fragment) |
bfb155c1 | 2970 | ctx->split_send_fragment = ctx->max_send_fragment; |
0f113f3e | 2971 | return 1; |
d102d9df | 2972 | case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT: |
7ee8627f | 2973 | if ((size_t)larg > ctx->max_send_fragment || larg == 0) |
d102d9df MC |
2974 | return 0; |
2975 | ctx->split_send_fragment = larg; | |
2976 | return 1; | |
2977 | case SSL_CTRL_SET_MAX_PIPELINES: | |
2978 | if (larg < 1 || larg > SSL_MAX_PIPELINES) | |
2979 | return 0; | |
2980 | ctx->max_pipelines = larg; | |
07077415 | 2981 | return 1; |
0f113f3e MC |
2982 | case SSL_CTRL_CERT_FLAGS: |
2983 | return (ctx->cert->cert_flags |= larg); | |
2984 | case SSL_CTRL_CLEAR_CERT_FLAGS: | |
2985 | return (ctx->cert->cert_flags &= ~larg); | |
7946ab33 | 2986 | case SSL_CTRL_SET_MIN_PROTO_VERSION: |
c8feba72 BK |
2987 | return ssl_check_allowed_versions(larg, ctx->max_proto_version) |
2988 | && ssl_set_version_bound(ctx->method->version, (int)larg, | |
2989 | &ctx->min_proto_version); | |
3edabd3c CH |
2990 | case SSL_CTRL_GET_MIN_PROTO_VERSION: |
2991 | return ctx->min_proto_version; | |
7946ab33 | 2992 | case SSL_CTRL_SET_MAX_PROTO_VERSION: |
c8feba72 BK |
2993 | return ssl_check_allowed_versions(ctx->min_proto_version, larg) |
2994 | && ssl_set_version_bound(ctx->method->version, (int)larg, | |
2995 | &ctx->max_proto_version); | |
3edabd3c CH |
2996 | case SSL_CTRL_GET_MAX_PROTO_VERSION: |
2997 | return ctx->max_proto_version; | |
0f113f3e | 2998 | default: |
26a7d938 | 2999 | return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg); |
0f113f3e MC |
3000 | } |
3001 | } | |
3002 | ||
3003 | long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void)) | |
3004 | { | |
3005 | switch (cmd) { | |
3006 | case SSL_CTRL_SET_MSG_CALLBACK: | |
3007 | ctx->msg_callback = (void (*) | |
3008 | (int write_p, int version, int content_type, | |
3009 | const void *buf, size_t len, SSL *ssl, | |
3010 | void *arg))(fp); | |
3011 | return 1; | |
3012 | ||
3013 | default: | |
26a7d938 | 3014 | return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp); |
0f113f3e MC |
3015 | } |
3016 | } | |
d3442bc7 | 3017 | |
ccd86b68 | 3018 | int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b) |
0f113f3e | 3019 | { |
90d9e49a DSH |
3020 | if (a->id > b->id) |
3021 | return 1; | |
3022 | if (a->id < b->id) | |
3023 | return -1; | |
3024 | return 0; | |
0f113f3e MC |
3025 | } |
3026 | ||
3027 | int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap, | |
3028 | const SSL_CIPHER *const *bp) | |
3029 | { | |
90d9e49a DSH |
3030 | if ((*ap)->id > (*bp)->id) |
3031 | return 1; | |
3032 | if ((*ap)->id < (*bp)->id) | |
3033 | return -1; | |
3034 | return 0; | |
0f113f3e | 3035 | } |
d02b48c6 | 3036 | |
38b051a1 TM |
3037 | /* |
3038 | * return a STACK of the ciphers available for the SSL and in order of | |
3039 | * preference | |
3040 | */ | |
0821bcd4 | 3041 | STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) |
0f113f3e | 3042 | { |
38b051a1 TM |
3043 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
3044 | ||
3045 | if (sc != NULL) { | |
3046 | if (sc->cipher_list != NULL) { | |
3047 | return sc->cipher_list; | |
0f113f3e | 3048 | } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) { |
26a7d938 | 3049 | return s->ctx->cipher_list; |
0f113f3e MC |
3050 | } |
3051 | } | |
26a7d938 | 3052 | return NULL; |
0f113f3e MC |
3053 | } |
3054 | ||
831eef2c NM |
3055 | STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s) |
3056 | { | |
38b051a1 TM |
3057 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
3058 | ||
3059 | if (sc == NULL || !sc->server) | |
831eef2c | 3060 | return NULL; |
38b051a1 | 3061 | return sc->peer_ciphers; |
831eef2c NM |
3062 | } |
3063 | ||
8b8e5bed | 3064 | STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s) |
0f113f3e MC |
3065 | { |
3066 | STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers; | |
3067 | int i; | |
38b051a1 TM |
3068 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
3069 | ||
3070 | if (sc == NULL) | |
3071 | return NULL; | |
1d0c08b4 | 3072 | |
0f113f3e MC |
3073 | ciphers = SSL_get_ciphers(s); |
3074 | if (!ciphers) | |
3075 | return NULL; | |
38b051a1 | 3076 | if (!ssl_set_client_disabled(sc)) |
1d0c08b4 | 3077 | return NULL; |
0f113f3e MC |
3078 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
3079 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i); | |
38b051a1 | 3080 | if (!ssl_cipher_disabled(sc, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) { |
0f113f3e MC |
3081 | if (!sk) |
3082 | sk = sk_SSL_CIPHER_new_null(); | |
3083 | if (!sk) | |
3084 | return NULL; | |
3085 | if (!sk_SSL_CIPHER_push(sk, c)) { | |
3086 | sk_SSL_CIPHER_free(sk); | |
3087 | return NULL; | |
3088 | } | |
3089 | } | |
3090 | } | |
3091 | return sk; | |
3092 | } | |
8b8e5bed | 3093 | |
4f43d0e7 | 3094 | /** return a STACK of the ciphers available for the SSL and in order of |
d02b48c6 | 3095 | * algorithm id */ |
38b051a1 | 3096 | STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL_CONNECTION *s) |
0f113f3e MC |
3097 | { |
3098 | if (s != NULL) { | |
38b051a1 | 3099 | if (s->cipher_list_by_id != NULL) |
26a7d938 | 3100 | return s->cipher_list_by_id; |
38b051a1 TM |
3101 | else if (s->ssl.ctx != NULL |
3102 | && s->ssl.ctx->cipher_list_by_id != NULL) | |
3103 | return s->ssl.ctx->cipher_list_by_id; | |
0f113f3e | 3104 | } |
26a7d938 | 3105 | return NULL; |
0f113f3e | 3106 | } |
d02b48c6 | 3107 | |
4f43d0e7 | 3108 | /** The old interface to get the same thing as SSL_get_ciphers() */ |
0f113f3e MC |
3109 | const char *SSL_get_cipher_list(const SSL *s, int n) |
3110 | { | |
4a640fb6 | 3111 | const SSL_CIPHER *c; |
0f113f3e MC |
3112 | STACK_OF(SSL_CIPHER) *sk; |
3113 | ||
3114 | if (s == NULL) | |
26a7d938 | 3115 | return NULL; |
0f113f3e MC |
3116 | sk = SSL_get_ciphers(s); |
3117 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n)) | |
26a7d938 | 3118 | return NULL; |
0f113f3e MC |
3119 | c = sk_SSL_CIPHER_value(sk, n); |
3120 | if (c == NULL) | |
26a7d938 K |
3121 | return NULL; |
3122 | return c->name; | |
0f113f3e | 3123 | } |
d02b48c6 | 3124 | |
9d5ac953 KY |
3125 | /** return a STACK of the ciphers available for the SSL_CTX and in order of |
3126 | * preference */ | |
3127 | STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) | |
3128 | { | |
3129 | if (ctx != NULL) | |
3130 | return ctx->cipher_list; | |
3131 | return NULL; | |
3132 | } | |
3133 | ||
3c83c5ba SR |
3134 | /* |
3135 | * Distinguish between ciphers controlled by set_ciphersuite() and | |
3136 | * set_cipher_list() when counting. | |
3137 | */ | |
3138 | static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER) *sk) | |
3139 | { | |
3140 | int i, num = 0; | |
3141 | const SSL_CIPHER *c; | |
3142 | ||
3143 | if (sk == NULL) | |
3144 | return 0; | |
3145 | for (i = 0; i < sk_SSL_CIPHER_num(sk); ++i) { | |
3146 | c = sk_SSL_CIPHER_value(sk, i); | |
3147 | if (c->min_tls >= TLS1_3_VERSION) | |
3148 | continue; | |
3149 | num++; | |
3150 | } | |
3151 | return num; | |
3152 | } | |
3153 | ||
25f923dd | 3154 | /** specify the ciphers to be used by default by the SSL_CTX */ |
018e57c7 | 3155 | int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) |
0f113f3e MC |
3156 | { |
3157 | STACK_OF(SSL_CIPHER) *sk; | |
3158 | ||
a68eee67 | 3159 | sk = ssl_create_cipher_list(ctx, ctx->tls13_ciphersuites, |
f865b081 MC |
3160 | &ctx->cipher_list, &ctx->cipher_list_by_id, str, |
3161 | ctx->cert); | |
0f113f3e MC |
3162 | /* |
3163 | * ssl_create_cipher_list may return an empty stack if it was unable to | |
3164 | * find a cipher matching the given rule string (for example if the rule | |
3165 | * string specifies a cipher which has been disabled). This is not an | |
3166 | * error as far as ssl_create_cipher_list is concerned, and hence | |
3167 | * ctx->cipher_list and ctx->cipher_list_by_id has been updated. | |
3168 | */ | |
3169 | if (sk == NULL) | |
3170 | return 0; | |
3c83c5ba | 3171 | else if (cipher_list_tls12_num(sk) == 0) { |
6849b73c | 3172 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH); |
0f113f3e MC |
3173 | return 0; |
3174 | } | |
3175 | return 1; | |
3176 | } | |
d02b48c6 | 3177 | |
4f43d0e7 | 3178 | /** specify the ciphers to be used by the SSL */ |
0f113f3e MC |
3179 | int SSL_set_cipher_list(SSL *s, const char *str) |
3180 | { | |
3181 | STACK_OF(SSL_CIPHER) *sk; | |
38b051a1 | 3182 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
0f113f3e | 3183 | |
38b051a1 TM |
3184 | if (sc == NULL) |
3185 | return 0; | |
3186 | ||
3187 | sk = ssl_create_cipher_list(s->ctx, sc->tls13_ciphersuites, | |
3188 | &sc->cipher_list, &sc->cipher_list_by_id, str, | |
3189 | sc->cert); | |
0f113f3e MC |
3190 | /* see comment in SSL_CTX_set_cipher_list */ |
3191 | if (sk == NULL) | |
3192 | return 0; | |
3c83c5ba | 3193 | else if (cipher_list_tls12_num(sk) == 0) { |
6849b73c | 3194 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH); |
0f113f3e MC |
3195 | return 0; |
3196 | } | |
3197 | return 1; | |
3198 | } | |
d02b48c6 | 3199 | |
a216df59 | 3200 | char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size) |
0f113f3e MC |
3201 | { |
3202 | char *p; | |
a216df59 | 3203 | STACK_OF(SSL_CIPHER) *clntsk, *srvrsk; |
4a640fb6 | 3204 | const SSL_CIPHER *c; |
0f113f3e | 3205 | int i; |
38b051a1 TM |
3206 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
3207 | ||
3208 | if (sc == NULL) | |
3209 | return NULL; | |
0f113f3e | 3210 | |
38b051a1 TM |
3211 | if (!sc->server |
3212 | || sc->peer_ciphers == NULL | |
a216df59 | 3213 | || size < 2) |
26a7d938 | 3214 | return NULL; |
0f113f3e MC |
3215 | |
3216 | p = buf; | |
38b051a1 | 3217 | clntsk = sc->peer_ciphers; |
a216df59 MC |
3218 | srvrsk = SSL_get_ciphers(s); |
3219 | if (clntsk == NULL || srvrsk == NULL) | |
3220 | return NULL; | |
0f113f3e | 3221 | |
a216df59 | 3222 | if (sk_SSL_CIPHER_num(clntsk) == 0 || sk_SSL_CIPHER_num(srvrsk) == 0) |
0f113f3e MC |
3223 | return NULL; |
3224 | ||
a216df59 | 3225 | for (i = 0; i < sk_SSL_CIPHER_num(clntsk); i++) { |
0f113f3e MC |
3226 | int n; |
3227 | ||
a216df59 MC |
3228 | c = sk_SSL_CIPHER_value(clntsk, i); |
3229 | if (sk_SSL_CIPHER_find(srvrsk, c) < 0) | |
3230 | continue; | |
3231 | ||
0f113f3e | 3232 | n = strlen(c->name); |
a216df59 | 3233 | if (n + 1 > size) { |
0f113f3e MC |
3234 | if (p != buf) |
3235 | --p; | |
3236 | *p = '\0'; | |
3237 | return buf; | |
3238 | } | |
4cacc9d5 | 3239 | strcpy(p, c->name); |
0f113f3e MC |
3240 | p += n; |
3241 | *(p++) = ':'; | |
a216df59 | 3242 | size -= n + 1; |
0f113f3e MC |
3243 | } |
3244 | p[-1] = '\0'; | |
26a7d938 | 3245 | return buf; |
0f113f3e MC |
3246 | } |
3247 | ||
7955c1f1 MC |
3248 | /** |
3249 | * Return the requested servername (SNI) value. Note that the behaviour varies | |
3250 | * depending on: | |
3251 | * - whether this is called by the client or the server, | |
3252 | * - if we are before or during/after the handshake, | |
3253 | * - if a resumption or normal handshake is being attempted/has occurred | |
3254 | * - whether we have negotiated TLSv1.2 (or below) or TLSv1.3 | |
38b051a1 | 3255 | * |
7955c1f1 | 3256 | * Note that only the host_name type is defined (RFC 3546). |
ed3883d2 | 3257 | */ |
f1fd4544 | 3258 | const char *SSL_get_servername(const SSL *s, const int type) |
0f113f3e | 3259 | { |
38b051a1 TM |
3260 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
3261 | int server; | |
3262 | ||
3263 | if (sc == NULL) | |
3264 | return NULL; | |
3265 | ||
7955c1f1 MC |
3266 | /* |
3267 | * If we don't know if we are the client or the server yet then we assume | |
3268 | * client. | |
3269 | */ | |
38b051a1 TM |
3270 | server = sc->handshake_func == NULL ? 0 : sc->server; |
3271 | ||
0f113f3e MC |
3272 | if (type != TLSEXT_NAMETYPE_host_name) |
3273 | return NULL; | |
a13c20f6 | 3274 | |
7955c1f1 MC |
3275 | if (server) { |
3276 | /** | |
3277 | * Server side | |
3278 | * In TLSv1.3 on the server SNI is not associated with the session | |
3279 | * but in TLSv1.2 or below it is. | |
3280 | * | |
3281 | * Before the handshake: | |
3282 | * - return NULL | |
3283 | * | |
3284 | * During/after the handshake (TLSv1.2 or below resumption occurred): | |
3285 | * - If a servername was accepted by the server in the original | |
3286 | * handshake then it will return that servername, or NULL otherwise. | |
3287 | * | |
3288 | * During/after the handshake (TLSv1.2 or below resumption did not occur): | |
3289 | * - The function will return the servername requested by the client in | |
3290 | * this handshake or NULL if none was requested. | |
3291 | */ | |
38b051a1 TM |
3292 | if (sc->hit && !SSL_CONNECTION_IS_TLS13(sc)) |
3293 | return sc->session->ext.hostname; | |
7955c1f1 MC |
3294 | } else { |
3295 | /** | |
3296 | * Client side | |
3297 | * | |
3298 | * Before the handshake: | |
3299 | * - If a servername has been set via a call to | |
3300 | * SSL_set_tlsext_host_name() then it will return that servername | |
3301 | * - If one has not been set, but a TLSv1.2 resumption is being | |
3302 | * attempted and the session from the original handshake had a | |
3303 | * servername accepted by the server then it will return that | |
3304 | * servername | |
3305 | * - Otherwise it returns NULL | |
3306 | * | |
3307 | * During/after the handshake (TLSv1.2 or below resumption occurred): | |
9f7505ab | 3308 | * - If the session from the original handshake had a servername accepted |
7955c1f1 MC |
3309 | * by the server then it will return that servername. |
3310 | * - Otherwise it returns the servername set via | |
3311 | * SSL_set_tlsext_host_name() (or NULL if it was not called). | |
3312 | * | |
3313 | * During/after the handshake (TLSv1.2 or below resumption did not occur): | |
3314 | * - It will return the servername set via SSL_set_tlsext_host_name() | |
3315 | * (or NULL if it was not called). | |
3316 | */ | |
3317 | if (SSL_in_before(s)) { | |
38b051a1 TM |
3318 | if (sc->ext.hostname == NULL |
3319 | && sc->session != NULL | |
3320 | && sc->session->ssl_version != TLS1_3_VERSION) | |
3321 | return sc->session->ext.hostname; | |
7955c1f1 | 3322 | } else { |
38b051a1 TM |
3323 | if (!SSL_CONNECTION_IS_TLS13(sc) && sc->hit |
3324 | && sc->session->ext.hostname != NULL) | |
3325 | return sc->session->ext.hostname; | |
7955c1f1 MC |
3326 | } |
3327 | } | |
3328 | ||
38b051a1 | 3329 | return sc->ext.hostname; |
0f113f3e | 3330 | } |
ed3883d2 | 3331 | |
f1fd4544 | 3332 | int SSL_get_servername_type(const SSL *s) |
0f113f3e | 3333 | { |
7955c1f1 | 3334 | if (SSL_get_servername(s, TLSEXT_NAMETYPE_host_name) != NULL) |
0f113f3e MC |
3335 | return TLSEXT_NAMETYPE_host_name; |
3336 | return -1; | |
3337 | } | |
ee2ffc27 | 3338 | |
0f113f3e MC |
3339 | /* |
3340 | * SSL_select_next_proto implements the standard protocol selection. It is | |
ee2ffc27 | 3341 | * expected that this function is called from the callback set by |
0f113f3e MC |
3342 | * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a |
3343 | * vector of 8-bit, length prefixed byte strings. The length byte itself is | |
3344 | * not included in the length. A byte string of length 0 is invalid. No byte | |
3345 | * string may be truncated. The current, but experimental algorithm for | |
3346 | * selecting the protocol is: 1) If the server doesn't support NPN then this | |
3347 | * is indicated to the callback. In this case, the client application has to | |
3348 | * abort the connection or have a default application level protocol. 2) If | |
3349 | * the server supports NPN, but advertises an empty list then the client | |
f430ba31 | 3350 | * selects the first protocol in its list, but indicates via the API that this |
0f113f3e MC |
3351 | * fallback case was enacted. 3) Otherwise, the client finds the first |
3352 | * protocol in the server's list that it supports and selects this protocol. | |
3353 | * This is because it's assumed that the server has better information about | |
3354 | * which protocol a client should use. 4) If the client doesn't support any | |
3355 | * of the server's advertised protocols, then this is treated the same as | |
3356 | * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was | |
3357 | * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached. | |
ee2ffc27 | 3358 | */ |
0f113f3e MC |
3359 | int SSL_select_next_proto(unsigned char **out, unsigned char *outlen, |
3360 | const unsigned char *server, | |
3361 | unsigned int server_len, | |
a230b26e | 3362 | const unsigned char *client, unsigned int client_len) |
0f113f3e MC |
3363 | { |
3364 | unsigned int i, j; | |
3365 | const unsigned char *result; | |
3366 | int status = OPENSSL_NPN_UNSUPPORTED; | |
3367 | ||
3368 | /* | |
3369 | * For each protocol in server preference order, see if we support it. | |
3370 | */ | |
3371 | for (i = 0; i < server_len;) { | |
3372 | for (j = 0; j < client_len;) { | |
3373 | if (server[i] == client[j] && | |
3374 | memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) { | |
3375 | /* We found a match */ | |
3376 | result = &server[i]; | |
3377 | status = OPENSSL_NPN_NEGOTIATED; | |
3378 | goto found; | |
3379 | } | |
3380 | j += client[j]; | |
3381 | j++; | |
3382 | } | |
3383 | i += server[i]; | |
3384 | i++; | |
3385 | } | |
3386 | ||
3387 | /* There's no overlap between our protocols and the server's list. */ | |
3388 | result = client; | |
3389 | status = OPENSSL_NPN_NO_OVERLAP; | |
3390 | ||
3391 | found: | |
3392 | *out = (unsigned char *)result + 1; | |
3393 | *outlen = result[0]; | |
3394 | return status; | |
3395 | } | |
ee2ffc27 | 3396 | |
e481f9b9 | 3397 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
3398 | /* |
3399 | * SSL_get0_next_proto_negotiated sets *data and *len to point to the | |
3400 | * client's requested protocol for this connection and returns 0. If the | |
3401 | * client didn't request any protocol, then *data is set to NULL. Note that | |
3402 | * the client can request any protocol it chooses. The value returned from | |
3403 | * this function need not be a member of the list of supported protocols | |
ee2ffc27 BL |
3404 | * provided by the callback. |
3405 | */ | |
0f113f3e MC |
3406 | void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, |
3407 | unsigned *len) | |
3408 | { | |
38b051a1 TM |
3409 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
3410 | ||
3411 | if (sc == NULL) { | |
3412 | /* We have no other way to indicate error */ | |
3413 | *data = NULL; | |
3414 | *len = 0; | |
3415 | return; | |
3416 | } | |
3417 | ||
3418 | *data = sc->ext.npn; | |
12a765a5 | 3419 | if (*data == NULL) { |
0f113f3e MC |
3420 | *len = 0; |
3421 | } else { | |
38b051a1 | 3422 | *len = (unsigned int)sc->ext.npn_len; |
0f113f3e MC |
3423 | } |
3424 | } | |
3425 | ||
3426 | /* | |
aff8c126 | 3427 | * SSL_CTX_set_npn_advertised_cb sets a callback that is called when |
0f113f3e MC |
3428 | * a TLS server needs a list of supported protocols for Next Protocol |
3429 | * Negotiation. The returned list must be in wire format. The list is | |
3430 | * returned by setting |out| to point to it and |outlen| to its length. This | |
3431 | * memory will not be modified, but one should assume that the SSL* keeps a | |
3432 | * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it | |
3433 | * wishes to advertise. Otherwise, no such extension will be included in the | |
3434 | * ServerHello. | |
3435 | */ | |
aff8c126 | 3436 | void SSL_CTX_set_npn_advertised_cb(SSL_CTX *ctx, |
8cbfcc70 | 3437 | SSL_CTX_npn_advertised_cb_func cb, |
aff8c126 | 3438 | void *arg) |
0f113f3e | 3439 | { |
aff8c126 RS |
3440 | ctx->ext.npn_advertised_cb = cb; |
3441 | ctx->ext.npn_advertised_cb_arg = arg; | |
0f113f3e MC |
3442 | } |
3443 | ||
3444 | /* | |
3445 | * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a | |
ee2ffc27 BL |
3446 | * client needs to select a protocol from the server's provided list. |out| |
3447 | * must be set to point to the selected protocol (which may be within |in|). | |
0f113f3e MC |
3448 | * The length of the protocol name must be written into |outlen|. The |
3449 | * server's advertised protocols are provided in |in| and |inlen|. The | |
3450 | * callback can assume that |in| is syntactically valid. The client must | |
3451 | * select a protocol. It is fatal to the connection if this callback returns | |
3452 | * a value other than SSL_TLSEXT_ERR_OK. | |
ee2ffc27 | 3453 | */ |
aff8c126 | 3454 | void SSL_CTX_set_npn_select_cb(SSL_CTX *ctx, |
8cbfcc70 | 3455 | SSL_CTX_npn_select_cb_func cb, |
aff8c126 | 3456 | void *arg) |
0f113f3e | 3457 | { |
aff8c126 RS |
3458 | ctx->ext.npn_select_cb = cb; |
3459 | ctx->ext.npn_select_cb_arg = arg; | |
0f113f3e | 3460 | } |
e481f9b9 | 3461 | #endif |
a398f821 | 3462 | |
feba11cf TS |
3463 | static int alpn_value_ok(const unsigned char *protos, unsigned int protos_len) |
3464 | { | |
3465 | unsigned int idx; | |
3466 | ||
3467 | if (protos_len < 2 || protos == NULL) | |
3468 | return 0; | |
3469 | ||
3470 | for (idx = 0; idx < protos_len; idx += protos[idx] + 1) { | |
3471 | if (protos[idx] == 0) | |
3472 | return 0; | |
3473 | } | |
3474 | return idx == protos_len; | |
3475 | } | |
0f113f3e MC |
3476 | /* |
3477 | * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|. | |
6f017a8f | 3478 | * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit |
0f113f3e MC |
3479 | * length-prefixed strings). Returns 0 on success. |
3480 | */ | |
3481 | int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos, | |
817cd0d5 | 3482 | unsigned int protos_len) |
0f113f3e | 3483 | { |
feba11cf TS |
3484 | unsigned char *alpn; |
3485 | ||
3486 | if (protos_len == 0 || protos == NULL) { | |
3487 | OPENSSL_free(ctx->ext.alpn); | |
3488 | ctx->ext.alpn = NULL; | |
39a14059 | 3489 | ctx->ext.alpn_len = 0; |
feba11cf TS |
3490 | return 0; |
3491 | } | |
3492 | /* Not valid per RFC */ | |
3493 | if (!alpn_value_ok(protos, protos_len)) | |
3494 | return 1; | |
3495 | ||
3496 | alpn = OPENSSL_memdup(protos, protos_len); | |
3497 | if (alpn == NULL) { | |
6849b73c | 3498 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
0f113f3e | 3499 | return 1; |
72e9be3d | 3500 | } |
feba11cf TS |
3501 | OPENSSL_free(ctx->ext.alpn); |
3502 | ctx->ext.alpn = alpn; | |
aff8c126 | 3503 | ctx->ext.alpn_len = protos_len; |
0f113f3e MC |
3504 | |
3505 | return 0; | |
3506 | } | |
3507 | ||
3508 | /* | |
3509 | * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|. | |
6f017a8f | 3510 | * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit |
0f113f3e MC |
3511 | * length-prefixed strings). Returns 0 on success. |
3512 | */ | |
3513 | int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos, | |
817cd0d5 | 3514 | unsigned int protos_len) |
0f113f3e | 3515 | { |
feba11cf | 3516 | unsigned char *alpn; |
38b051a1 TM |
3517 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
3518 | ||
3519 | if (sc == NULL) | |
3520 | return 1; | |
feba11cf TS |
3521 | |
3522 | if (protos_len == 0 || protos == NULL) { | |
38b051a1 TM |
3523 | OPENSSL_free(sc->ext.alpn); |
3524 | sc->ext.alpn = NULL; | |
3525 | sc->ext.alpn_len = 0; | |
feba11cf TS |
3526 | return 0; |
3527 | } | |
3528 | /* Not valid per RFC */ | |
3529 | if (!alpn_value_ok(protos, protos_len)) | |
3530 | return 1; | |
3531 | ||
3532 | alpn = OPENSSL_memdup(protos, protos_len); | |
3533 | if (alpn == NULL) { | |
6849b73c | 3534 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
0f113f3e | 3535 | return 1; |
72e9be3d | 3536 | } |
38b051a1 TM |
3537 | OPENSSL_free(sc->ext.alpn); |
3538 | sc->ext.alpn = alpn; | |
3539 | sc->ext.alpn_len = protos_len; | |
0f113f3e MC |
3540 | |
3541 | return 0; | |
3542 | } | |
3543 | ||
3544 | /* | |
3545 | * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is | |
3546 | * called during ClientHello processing in order to select an ALPN protocol | |
3547 | * from the client's list of offered protocols. | |
3548 | */ | |
3549 | void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, | |
8cbfcc70 RS |
3550 | SSL_CTX_alpn_select_cb_func cb, |
3551 | void *arg) | |
0f113f3e | 3552 | { |
aff8c126 RS |
3553 | ctx->ext.alpn_select_cb = cb; |
3554 | ctx->ext.alpn_select_cb_arg = arg; | |
0f113f3e MC |
3555 | } |
3556 | ||
3557 | /* | |
69687aa8 F |
3558 | * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|. |
3559 | * On return it sets |*data| to point to |*len| bytes of protocol name | |
0f113f3e MC |
3560 | * (not including the leading length-prefix byte). If the server didn't |
3561 | * respond with a negotiated protocol then |*len| will be zero. | |
3562 | */ | |
6f017a8f | 3563 | void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data, |
817cd0d5 | 3564 | unsigned int *len) |
0f113f3e | 3565 | { |
38b051a1 TM |
3566 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl); |
3567 | ||
3568 | if (sc == NULL) { | |
3569 | /* We have no other way to indicate error */ | |
3570 | *data = NULL; | |
3571 | *len = 0; | |
3572 | return; | |
3573 | } | |
3574 | ||
3575 | *data = sc->s3.alpn_selected; | |
0f113f3e MC |
3576 | if (*data == NULL) |
3577 | *len = 0; | |
3578 | else | |
38b051a1 | 3579 | *len = (unsigned int)sc->s3.alpn_selected_len; |
0f113f3e MC |
3580 | } |
3581 | ||
74b4b494 | 3582 | int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen, |
0f113f3e | 3583 | const char *label, size_t llen, |
23cec1f4 | 3584 | const unsigned char *context, size_t contextlen, |
0f113f3e MC |
3585 | int use_context) |
3586 | { | |
38b051a1 TM |
3587 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
3588 | ||
3589 | if (sc == NULL) | |
3590 | return -1; | |
3591 | ||
3592 | if (sc->session == NULL | |
3593 | || (sc->version < TLS1_VERSION && sc->version != DTLS1_BAD_VER)) | |
0f113f3e | 3594 | return -1; |
e0af0405 | 3595 | |
38b051a1 | 3596 | return s->method->ssl3_enc->export_keying_material(sc, out, olen, label, |
23cec1f4 MC |
3597 | llen, context, |
3598 | contextlen, use_context); | |
0f113f3e | 3599 | } |
e0af0405 | 3600 | |
b38ede80 TT |
3601 | int SSL_export_keying_material_early(SSL *s, unsigned char *out, size_t olen, |
3602 | const char *label, size_t llen, | |
3603 | const unsigned char *context, | |
3604 | size_t contextlen) | |
3605 | { | |
38b051a1 TM |
3606 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
3607 | ||
3608 | if (sc == NULL) | |
3609 | return -1; | |
3610 | ||
3611 | if (sc->version != TLS1_3_VERSION) | |
b38ede80 TT |
3612 | return 0; |
3613 | ||
38b051a1 | 3614 | return tls13_export_keying_material_early(sc, out, olen, label, llen, |
b38ede80 TT |
3615 | context, contextlen); |
3616 | } | |
3617 | ||
3c1d6bbc | 3618 | static unsigned long ssl_session_hash(const SSL_SESSION *a) |
0f113f3e | 3619 | { |
bd5d27c1 | 3620 | const unsigned char *session_id = a->session_id; |
0f113f3e | 3621 | unsigned long l; |
bd5d27c1 DB |
3622 | unsigned char tmp_storage[4]; |
3623 | ||
3624 | if (a->session_id_length < sizeof(tmp_storage)) { | |
3625 | memset(tmp_storage, 0, sizeof(tmp_storage)); | |
3626 | memcpy(tmp_storage, a->session_id, a->session_id_length); | |
3627 | session_id = tmp_storage; | |
3628 | } | |
0f113f3e MC |
3629 | |
3630 | l = (unsigned long) | |
bd5d27c1 DB |
3631 | ((unsigned long)session_id[0]) | |
3632 | ((unsigned long)session_id[1] << 8L) | | |
3633 | ((unsigned long)session_id[2] << 16L) | | |
3634 | ((unsigned long)session_id[3] << 24L); | |
26a7d938 | 3635 | return l; |
0f113f3e MC |
3636 | } |
3637 | ||
3638 | /* | |
3639 | * NB: If this function (or indeed the hash function which uses a sort of | |
dc644fe2 | 3640 | * coarser function than this one) is changed, ensure |
0f113f3e MC |
3641 | * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on |
3642 | * being able to construct an SSL_SESSION that will collide with any existing | |
3643 | * session with a matching session ID. | |
3644 | */ | |
3645 | static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) | |
3646 | { | |
3647 | if (a->ssl_version != b->ssl_version) | |
208fb891 | 3648 | return 1; |
0f113f3e | 3649 | if (a->session_id_length != b->session_id_length) |
208fb891 | 3650 | return 1; |
26a7d938 | 3651 | return memcmp(a->session_id, b->session_id, a->session_id_length); |
0f113f3e MC |
3652 | } |
3653 | ||
3654 | /* | |
3655 | * These wrapper functions should remain rather than redeclaring | |
d0fa136c | 3656 | * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each |
0f113f3e MC |
3657 | * variable. The reason is that the functions aren't static, they're exposed |
3658 | * via ssl.h. | |
3659 | */ | |
97b17195 | 3660 | |
b4250010 | 3661 | SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq, |
d8652be0 | 3662 | const SSL_METHOD *meth) |
0f113f3e MC |
3663 | { |
3664 | SSL_CTX *ret = NULL; | |
3665 | ||
3666 | if (meth == NULL) { | |
6849b73c | 3667 | ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_METHOD_PASSED); |
26a7d938 | 3668 | return NULL; |
0f113f3e MC |
3669 | } |
3670 | ||
0fc32b07 MC |
3671 | if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL)) |
3672 | return NULL; | |
7fa792d1 | 3673 | |
0f113f3e | 3674 | if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) { |
6849b73c | 3675 | ERR_raise(ERR_LIB_SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); |
0f113f3e MC |
3676 | goto err; |
3677 | } | |
b51bce94 | 3678 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
3679 | if (ret == NULL) |
3680 | goto err; | |
3681 | ||
045a8930 F |
3682 | /* Init the reference counting before any call to SSL_CTX_free */ |
3683 | ret->references = 1; | |
3684 | ret->lock = CRYPTO_THREAD_lock_new(); | |
3685 | if (ret->lock == NULL) { | |
3686 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); | |
3687 | OPENSSL_free(ret); | |
3688 | return NULL; | |
3689 | } | |
3690 | ||
acce0557 P |
3691 | #ifdef TSAN_REQUIRES_LOCKING |
3692 | ret->tsan_lock = CRYPTO_THREAD_lock_new(); | |
3693 | if (ret->tsan_lock == NULL) { | |
3694 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); | |
3695 | goto err; | |
3696 | } | |
3697 | #endif | |
3698 | ||
ba18627e MC |
3699 | ret->libctx = libctx; |
3700 | if (propq != NULL) { | |
3701 | ret->propq = OPENSSL_strdup(propq); | |
3702 | if (ret->propq == NULL) | |
3703 | goto err; | |
3704 | } | |
3705 | ||
0f113f3e | 3706 | ret->method = meth; |
7946ab33 KR |
3707 | ret->min_proto_version = 0; |
3708 | ret->max_proto_version = 0; | |
693cf80c | 3709 | ret->mode = SSL_MODE_AUTO_RETRY; |
0f113f3e MC |
3710 | ret->session_cache_mode = SSL_SESS_CACHE_SERVER; |
3711 | ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; | |
64b25758 | 3712 | /* We take the system default. */ |
0f113f3e | 3713 | ret->session_timeout = meth->get_timeout(); |
0f113f3e | 3714 | ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; |
0f113f3e | 3715 | ret->verify_mode = SSL_VERIFY_NONE; |
0f113f3e MC |
3716 | if ((ret->cert = ssl_cert_new()) == NULL) |
3717 | goto err; | |
3718 | ||
62d0577e | 3719 | ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp); |
0f113f3e MC |
3720 | if (ret->sessions == NULL) |
3721 | goto err; | |
3722 | ret->cert_store = X509_STORE_new(); | |
3723 | if (ret->cert_store == NULL) | |
3724 | goto err; | |
ed29e82a | 3725 | #ifndef OPENSSL_NO_CT |
d8652be0 | 3726 | ret->ctlog_store = CTLOG_STORE_new_ex(libctx, propq); |
ed29e82a RP |
3727 | if (ret->ctlog_store == NULL) |
3728 | goto err; | |
3729 | #endif | |
f865b081 | 3730 | |
c8f6c28a MC |
3731 | /* initialize cipher/digest methods table */ |
3732 | if (!ssl_load_ciphers(ret)) | |
075b1f2f | 3733 | goto err2; |
263ff2c9 MC |
3734 | /* initialise sig algs */ |
3735 | if (!ssl_setup_sig_algs(ret)) | |
3736 | goto err2; | |
3737 | ||
c8f6c28a | 3738 | |
9d2d857f MC |
3739 | if (!ssl_load_groups(ret)) |
3740 | goto err2; | |
3741 | ||
5d120511 | 3742 | if (!SSL_CTX_set_ciphersuites(ret, OSSL_default_ciphersuites())) |
f865b081 MC |
3743 | goto err; |
3744 | ||
a68eee67 | 3745 | if (!ssl_create_cipher_list(ret, |
f865b081 | 3746 | ret->tls13_ciphersuites, |
a230b26e | 3747 | &ret->cipher_list, &ret->cipher_list_by_id, |
5d120511 | 3748 | OSSL_default_cipher_list(), ret->cert) |
a230b26e | 3749 | || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) { |
6849b73c | 3750 | ERR_raise(ERR_LIB_SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS); |
0f113f3e MC |
3751 | goto err2; |
3752 | } | |
3753 | ||
3754 | ret->param = X509_VERIFY_PARAM_new(); | |
a71edf3b | 3755 | if (ret->param == NULL) |
0f113f3e MC |
3756 | goto err; |
3757 | ||
c8f6c28a MC |
3758 | /* |
3759 | * If these aren't available from the provider we'll get NULL returns. | |
3760 | * That's fine but will cause errors later if SSLv3 is negotiated | |
3761 | */ | |
3762 | ret->md5 = ssl_evp_md_fetch(libctx, NID_md5, propq); | |
3763 | ret->sha1 = ssl_evp_md_fetch(libctx, NID_sha1, propq); | |
0f113f3e | 3764 | |
fa7c2637 | 3765 | if ((ret->ca_names = sk_X509_NAME_new_null()) == NULL) |
0f113f3e MC |
3766 | goto err; |
3767 | ||
98732979 MC |
3768 | if ((ret->client_ca_names = sk_X509_NAME_new_null()) == NULL) |
3769 | goto err; | |
3770 | ||
25a807bc F |
3771 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data)) |
3772 | goto err; | |
0f113f3e | 3773 | |
4bfb96f2 TS |
3774 | if ((ret->ext.secure = OPENSSL_secure_zalloc(sizeof(*ret->ext.secure))) == NULL) |
3775 | goto err; | |
3776 | ||
0f113f3e MC |
3777 | /* No compression for DTLS */ |
3778 | if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS)) | |
3779 | ret->comp_methods = SSL_COMP_get_compression_methods(); | |
3780 | ||
3781 | ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; | |
d102d9df | 3782 | ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
566dda07 | 3783 | |
4e2e1ec9 | 3784 | /* Setup RFC5077 ticket keys */ |
8f21260b | 3785 | if ((RAND_bytes_ex(libctx, ret->ext.tick_key_name, |
0f8815aa | 3786 | sizeof(ret->ext.tick_key_name), 0) <= 0) |
8f21260b | 3787 | || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_hmac_key, |
0f8815aa | 3788 | sizeof(ret->ext.secure->tick_hmac_key), 0) <= 0) |
8f21260b | 3789 | || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_aes_key, |
0f8815aa | 3790 | sizeof(ret->ext.secure->tick_aes_key), 0) <= 0)) |
0f113f3e | 3791 | ret->options |= SSL_OP_NO_TICKET; |
6434abbf | 3792 | |
8f21260b | 3793 | if (RAND_priv_bytes_ex(libctx, ret->ext.cookie_hmac_key, |
0f8815aa | 3794 | sizeof(ret->ext.cookie_hmac_key), 0) <= 0) |
43054d3d MC |
3795 | goto err; |
3796 | ||
edc032b5 | 3797 | #ifndef OPENSSL_NO_SRP |
76cb077f | 3798 | if (!ssl_ctx_srp_ctx_init_intern(ret)) |
69f68237 | 3799 | goto err; |
edc032b5 | 3800 | #endif |
4db9677b | 3801 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
3802 | # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO |
3803 | # define eng_strx(x) #x | |
3804 | # define eng_str(x) eng_strx(x) | |
3805 | /* Use specific client engine automatically... ignore errors */ | |
3806 | { | |
3807 | ENGINE *eng; | |
3808 | eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); | |
3809 | if (!eng) { | |
3810 | ERR_clear_error(); | |
3811 | ENGINE_load_builtin_engines(); | |
3812 | eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); | |
3813 | } | |
3814 | if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng)) | |
3815 | ERR_clear_error(); | |
3816 | } | |
3817 | # endif | |
3818 | #endif | |
dc5744cb EK |
3819 | /* |
3820 | * Disable compression by default to prevent CRIME. Applications can | |
3821 | * re-enable compression by configuring | |
3822 | * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION); | |
a5816a5a MC |
3823 | * or by using the SSL_CONF library. Similarly we also enable TLSv1.3 |
3824 | * middlebox compatibility by default. This may be disabled by default in | |
3825 | * a later OpenSSL version. | |
dc5744cb | 3826 | */ |
a5816a5a | 3827 | ret->options |= SSL_OP_NO_COMPRESSION | SSL_OP_ENABLE_MIDDLEBOX_COMPAT; |
0f113f3e | 3828 | |
aff8c126 | 3829 | ret->ext.status_type = TLSEXT_STATUSTYPE_nothing; |
ba261f71 | 3830 | |
bfa9a9af | 3831 | /* |
c39e4048 BK |
3832 | * We cannot usefully set a default max_early_data here (which gets |
3833 | * propagated in SSL_new(), for the following reason: setting the | |
3834 | * SSL field causes tls_construct_stoc_early_data() to tell the | |
3835 | * client that early data will be accepted when constructing a TLS 1.3 | |
3836 | * session ticket, and the client will accordingly send us early data | |
3837 | * when using that ticket (if the client has early data to send). | |
3838 | * However, in order for the early data to actually be consumed by | |
3839 | * the application, the application must also have calls to | |
3840 | * SSL_read_early_data(); otherwise we'll just skip past the early data | |
3841 | * and ignore it. So, since the application must add calls to | |
3842 | * SSL_read_early_data(), we also require them to add | |
3843 | * calls to SSL_CTX_set_max_early_data() in order to use early data, | |
3844 | * eliminating the bandwidth-wasting early data in the case described | |
3845 | * above. | |
bfa9a9af | 3846 | */ |
c39e4048 | 3847 | ret->max_early_data = 0; |
bfa9a9af | 3848 | |
4e8548e8 MC |
3849 | /* |
3850 | * Default recv_max_early_data is a fully loaded single record. Could be | |
3851 | * split across multiple records in practice. We set this differently to | |
3852 | * max_early_data so that, in the default case, we do not advertise any | |
3853 | * support for early_data, but if a client were to send us some (e.g. | |
3854 | * because of an old, stale ticket) then we will tolerate it and skip over | |
3855 | * it. | |
3856 | */ | |
3857 | ret->recv_max_early_data = SSL3_RT_MAX_PLAIN_LENGTH; | |
3858 | ||
36ff232c MC |
3859 | /* By default we send two session tickets automatically in TLSv1.3 */ |
3860 | ret->num_tickets = 2; | |
9d0a8bb7 | 3861 | |
8a5ed9dc TM |
3862 | ssl_ctx_system_config(ret); |
3863 | ||
16203f7b | 3864 | return ret; |
0f113f3e | 3865 | err: |
6849b73c | 3866 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
0f113f3e | 3867 | err2: |
e0e920b1 | 3868 | SSL_CTX_free(ret); |
16203f7b | 3869 | return NULL; |
0f113f3e | 3870 | } |
d02b48c6 | 3871 | |
ba18627e MC |
3872 | SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) |
3873 | { | |
d8652be0 | 3874 | return SSL_CTX_new_ex(NULL, NULL, meth); |
ba18627e MC |
3875 | } |
3876 | ||
c5ebfcab | 3877 | int SSL_CTX_up_ref(SSL_CTX *ctx) |
a18a31e4 | 3878 | { |
16203f7b | 3879 | int i; |
c5ebfcab | 3880 | |
2f545ae4 | 3881 | if (CRYPTO_UP_REF(&ctx->references, &i, ctx->lock) <= 0) |
c5ebfcab F |
3882 | return 0; |
3883 | ||
3884 | REF_PRINT_COUNT("SSL_CTX", ctx); | |
3885 | REF_ASSERT_ISNT(i < 2); | |
3886 | return ((i > 1) ? 1 : 0); | |
a18a31e4 MC |
3887 | } |
3888 | ||
4f43d0e7 | 3889 | void SSL_CTX_free(SSL_CTX *a) |
0f113f3e MC |
3890 | { |
3891 | int i; | |
9d2d857f | 3892 | size_t j; |
d02b48c6 | 3893 | |
0f113f3e MC |
3894 | if (a == NULL) |
3895 | return; | |
d02b48c6 | 3896 | |
2f545ae4 | 3897 | CRYPTO_DOWN_REF(&a->references, &i, a->lock); |
f3f1cf84 | 3898 | REF_PRINT_COUNT("SSL_CTX", a); |
0f113f3e MC |
3899 | if (i > 0) |
3900 | return; | |
f3f1cf84 | 3901 | REF_ASSERT_ISNT(i < 0); |
0f113f3e | 3902 | |
222561fe | 3903 | X509_VERIFY_PARAM_free(a->param); |
919ba009 | 3904 | dane_ctx_final(&a->dane); |
0f113f3e MC |
3905 | |
3906 | /* | |
3907 | * Free internal session cache. However: the remove_cb() may reference | |
3908 | * the ex_data of SSL_CTX, thus the ex_data store can only be removed | |
3909 | * after the sessions were flushed. | |
3910 | * As the ex_data handling routines might also touch the session cache, | |
3911 | * the most secure solution seems to be: empty (flush) the cache, then | |
3912 | * free ex_data, then finally free the cache. | |
3913 | * (See ticket [openssl.org #212].) | |
3914 | */ | |
3915 | if (a->sessions != NULL) | |
3916 | SSL_CTX_flush_sessions(a, 0); | |
3917 | ||
3918 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data); | |
25aaa98a | 3919 | lh_SSL_SESSION_free(a->sessions); |
222561fe | 3920 | X509_STORE_free(a->cert_store); |
ed29e82a RP |
3921 | #ifndef OPENSSL_NO_CT |
3922 | CTLOG_STORE_free(a->ctlog_store); | |
3923 | #endif | |
25aaa98a RS |
3924 | sk_SSL_CIPHER_free(a->cipher_list); |
3925 | sk_SSL_CIPHER_free(a->cipher_list_by_id); | |
f865b081 | 3926 | sk_SSL_CIPHER_free(a->tls13_ciphersuites); |
e0e920b1 | 3927 | ssl_cert_free(a->cert); |
fa7c2637 | 3928 | sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free); |
98732979 | 3929 | sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free); |
79b2a2f2 | 3930 | OSSL_STACK_OF_X509_free(a->extra_certs); |
0f113f3e | 3931 | a->comp_methods = NULL; |
e783bae2 | 3932 | #ifndef OPENSSL_NO_SRTP |
25aaa98a | 3933 | sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles); |
e783bae2 | 3934 | #endif |
edc032b5 | 3935 | #ifndef OPENSSL_NO_SRP |
76cb077f | 3936 | ssl_ctx_srp_ctx_free_intern(a); |
edc032b5 | 3937 | #endif |
bdfe932d | 3938 | #ifndef OPENSSL_NO_ENGINE |
301fcb28 | 3939 | tls_engine_finish(a->client_cert_engine); |
ddac1974 | 3940 | #endif |
8671b898 | 3941 | |
aff8c126 | 3942 | OPENSSL_free(a->ext.ecpointformats); |
187753e0 | 3943 | OPENSSL_free(a->ext.supportedgroups); |
ddf8f1ce | 3944 | OPENSSL_free(a->ext.supported_groups_default); |
aff8c126 | 3945 | OPENSSL_free(a->ext.alpn); |
4bfb96f2 | 3946 | OPENSSL_secure_free(a->ext.secure); |
8671b898 | 3947 | |
c8f6c28a MC |
3948 | ssl_evp_md_free(a->md5); |
3949 | ssl_evp_md_free(a->sha1); | |
3950 | ||
9d2d857f MC |
3951 | for (j = 0; j < SSL_ENC_NUM_IDX; j++) |
3952 | ssl_evp_cipher_free(a->ssl_cipher_methods[j]); | |
3953 | for (j = 0; j < SSL_MD_NUM_IDX; j++) | |
3954 | ssl_evp_md_free(a->ssl_digest_methods[j]); | |
3955 | for (j = 0; j < a->group_list_len; j++) { | |
3956 | OPENSSL_free(a->group_list[j].tlsname); | |
3957 | OPENSSL_free(a->group_list[j].realname); | |
3958 | OPENSSL_free(a->group_list[j].algorithm); | |
3959 | } | |
3960 | OPENSSL_free(a->group_list); | |
c8f6c28a | 3961 | |
263ff2c9 MC |
3962 | OPENSSL_free(a->sigalg_lookup_cache); |
3963 | ||
16203f7b | 3964 | CRYPTO_THREAD_lock_free(a->lock); |
acce0557 P |
3965 | #ifdef TSAN_REQUIRES_LOCKING |
3966 | CRYPTO_THREAD_lock_free(a->tsan_lock); | |
3967 | #endif | |
16203f7b | 3968 | |
ba18627e MC |
3969 | OPENSSL_free(a->propq); |
3970 | ||
0f113f3e MC |
3971 | OPENSSL_free(a); |
3972 | } | |
d02b48c6 | 3973 | |
3ae76679 | 3974 | void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) |
0f113f3e MC |
3975 | { |
3976 | ctx->default_passwd_callback = cb; | |
3977 | } | |
3978 | ||
3979 | void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) | |
3980 | { | |
3981 | ctx->default_passwd_callback_userdata = u; | |
3982 | } | |
3983 | ||
0c452abc CH |
3984 | pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) |
3985 | { | |
3986 | return ctx->default_passwd_callback; | |
3987 | } | |
3988 | ||
3989 | void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) | |
3990 | { | |
3991 | return ctx->default_passwd_callback_userdata; | |
3992 | } | |
3993 | ||
a974e64a MC |
3994 | void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb) |
3995 | { | |
38b051a1 TM |
3996 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
3997 | ||
3998 | if (sc == NULL) | |
3999 | return; | |
4000 | ||
4001 | sc->default_passwd_callback = cb; | |
a974e64a MC |
4002 | } |
4003 | ||
4004 | void SSL_set_default_passwd_cb_userdata(SSL *s, void *u) | |
4005 | { | |
38b051a1 TM |
4006 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4007 | ||
4008 | if (sc == NULL) | |
4009 | return; | |
4010 | ||
4011 | sc->default_passwd_callback_userdata = u; | |
a974e64a MC |
4012 | } |
4013 | ||
0c452abc CH |
4014 | pem_password_cb *SSL_get_default_passwd_cb(SSL *s) |
4015 | { | |
38b051a1 TM |
4016 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4017 | ||
4018 | if (sc == NULL) | |
4019 | return NULL; | |
4020 | ||
4021 | return sc->default_passwd_callback; | |
0c452abc CH |
4022 | } |
4023 | ||
4024 | void *SSL_get_default_passwd_cb_userdata(SSL *s) | |
4025 | { | |
38b051a1 TM |
4026 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4027 | ||
4028 | if (sc == NULL) | |
4029 | return NULL; | |
4030 | ||
4031 | return sc->default_passwd_callback_userdata; | |
0c452abc CH |
4032 | } |
4033 | ||
0f113f3e MC |
4034 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, |
4035 | int (*cb) (X509_STORE_CTX *, void *), | |
4036 | void *arg) | |
4037 | { | |
4038 | ctx->app_verify_callback = cb; | |
4039 | ctx->app_verify_arg = arg; | |
4040 | } | |
4041 | ||
4042 | void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, | |
4043 | int (*cb) (int, X509_STORE_CTX *)) | |
4044 | { | |
4045 | ctx->verify_mode = mode; | |
4046 | ctx->default_verify_callback = cb; | |
4047 | } | |
4048 | ||
4049 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) | |
4050 | { | |
4051 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); | |
4052 | } | |
4053 | ||
a230b26e | 4054 | void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), void *arg) |
0f113f3e MC |
4055 | { |
4056 | ssl_cert_set_cert_cb(c->cert, cb, arg); | |
4057 | } | |
4058 | ||
4059 | void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg) | |
4060 | { | |
38b051a1 TM |
4061 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4062 | ||
4063 | if (sc == NULL) | |
4064 | return; | |
4065 | ||
4066 | ssl_cert_set_cert_cb(sc->cert, cb, arg); | |
0f113f3e | 4067 | } |
18d71588 | 4068 | |
38b051a1 | 4069 | void ssl_set_masks(SSL_CONNECTION *s) |
0f113f3e | 4070 | { |
6383d316 | 4071 | CERT *c = s->cert; |
555cbb32 | 4072 | uint32_t *pvalid = s->s3.tmp.valid_flags; |
bc71f910 | 4073 | int rsa_enc, rsa_sign, dh_tmp, dsa_sign; |
361a1191 | 4074 | unsigned long mask_k, mask_a; |
361a1191 | 4075 | int have_ecc_cert, ecdsa_ok; |
462f4f4b | 4076 | |
0f113f3e MC |
4077 | if (c == NULL) |
4078 | return; | |
d02b48c6 | 4079 | |
13c45372 | 4080 | dh_tmp = (c->dh_tmp != NULL |
13c45372 | 4081 | || c->dh_tmp_cb != NULL |
13c45372 | 4082 | || c->dh_tmp_auto); |
d02b48c6 | 4083 | |
d0ff28f8 | 4084 | rsa_enc = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID; |
38e8f3cd DSH |
4085 | rsa_sign = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID; |
4086 | dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_VALID; | |
6383d316 | 4087 | have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID; |
0f113f3e MC |
4088 | mask_k = 0; |
4089 | mask_a = 0; | |
0e1dba93 | 4090 | |
77359d22 RL |
4091 | OSSL_TRACE4(TLS_CIPHER, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n", |
4092 | dh_tmp, rsa_enc, rsa_sign, dsa_sign); | |
0f113f3e | 4093 | |
2a9b9654 | 4094 | #ifndef OPENSSL_NO_GOST |
4020c0b3 | 4095 | if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) { |
5a5530a2 | 4096 | mask_k |= SSL_kGOST | SSL_kGOST18; |
e44380a9 DB |
4097 | mask_a |= SSL_aGOST12; |
4098 | } | |
4020c0b3 | 4099 | if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) { |
5a5530a2 | 4100 | mask_k |= SSL_kGOST | SSL_kGOST18; |
e44380a9 DB |
4101 | mask_a |= SSL_aGOST12; |
4102 | } | |
4020c0b3 | 4103 | if (ssl_has_cert(s, SSL_PKEY_GOST01)) { |
0f113f3e MC |
4104 | mask_k |= SSL_kGOST; |
4105 | mask_a |= SSL_aGOST01; | |
4106 | } | |
2a9b9654 | 4107 | #endif |
0f113f3e | 4108 | |
361a1191 | 4109 | if (rsa_enc) |
0f113f3e | 4110 | mask_k |= SSL_kRSA; |
d02b48c6 | 4111 | |
0f113f3e MC |
4112 | if (dh_tmp) |
4113 | mask_k |= SSL_kDHE; | |
d02b48c6 | 4114 | |
6aaa29fb DSH |
4115 | /* |
4116 | * If we only have an RSA-PSS certificate allow RSA authentication | |
4117 | * if TLS 1.2 and peer supports it. | |
4118 | */ | |
4119 | ||
4120 | if (rsa_enc || rsa_sign || (ssl_has_cert(s, SSL_PKEY_RSA_PSS_SIGN) | |
4121 | && pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_EXPLICIT_SIGN | |
38b051a1 | 4122 | && TLS1_get_version(&s->ssl) == TLS1_2_VERSION)) |
0f113f3e | 4123 | mask_a |= SSL_aRSA; |
d02b48c6 | 4124 | |
0f113f3e MC |
4125 | if (dsa_sign) { |
4126 | mask_a |= SSL_aDSS; | |
0f113f3e | 4127 | } |
d02b48c6 | 4128 | |
0f113f3e | 4129 | mask_a |= SSL_aNULL; |
d02b48c6 | 4130 | |
0f113f3e MC |
4131 | /* |
4132 | * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites | |
4133 | * depending on the key usage extension. | |
4134 | */ | |
0f113f3e | 4135 | if (have_ecc_cert) { |
a8d8e06b | 4136 | uint32_t ex_kusage; |
4020c0b3 | 4137 | ex_kusage = X509_get_key_usage(c->pkeys[SSL_PKEY_ECC].x509); |
a8d8e06b | 4138 | ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE; |
6383d316 | 4139 | if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN)) |
0f113f3e | 4140 | ecdsa_ok = 0; |
c7c46256 | 4141 | if (ecdsa_ok) |
0f113f3e | 4142 | mask_a |= SSL_aECDSA; |
0f113f3e | 4143 | } |
b2021556 DSH |
4144 | /* Allow Ed25519 for TLS 1.2 if peer supports it */ |
4145 | if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED25519) | |
4146 | && pvalid[SSL_PKEY_ED25519] & CERT_PKEY_EXPLICIT_SIGN | |
38b051a1 | 4147 | && TLS1_get_version(&s->ssl) == TLS1_2_VERSION) |
b2021556 | 4148 | mask_a |= SSL_aECDSA; |
0e1d6ecf MC |
4149 | |
4150 | /* Allow Ed448 for TLS 1.2 if peer supports it */ | |
4151 | if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED448) | |
4152 | && pvalid[SSL_PKEY_ED448] & CERT_PKEY_EXPLICIT_SIGN | |
38b051a1 | 4153 | && TLS1_get_version(&s->ssl) == TLS1_2_VERSION) |
0e1d6ecf | 4154 | mask_a |= SSL_aECDSA; |
ea262260 | 4155 | |
fe6ef247 | 4156 | mask_k |= SSL_kECDHE; |
ddac1974 NL |
4157 | |
4158 | #ifndef OPENSSL_NO_PSK | |
0f113f3e MC |
4159 | mask_k |= SSL_kPSK; |
4160 | mask_a |= SSL_aPSK; | |
526f94ad DSH |
4161 | if (mask_k & SSL_kRSA) |
4162 | mask_k |= SSL_kRSAPSK; | |
4163 | if (mask_k & SSL_kDHE) | |
4164 | mask_k |= SSL_kDHEPSK; | |
4165 | if (mask_k & SSL_kECDHE) | |
4166 | mask_k |= SSL_kECDHEPSK; | |
ddac1974 NL |
4167 | #endif |
4168 | ||
555cbb32 TS |
4169 | s->s3.tmp.mask_k = mask_k; |
4170 | s->s3.tmp.mask_a = mask_a; | |
0f113f3e | 4171 | } |
d02b48c6 | 4172 | |
38b051a1 | 4173 | int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL_CONNECTION *s) |
0f113f3e | 4174 | { |
555cbb32 | 4175 | if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) { |
0f113f3e | 4176 | /* key usage, if present, must allow signing */ |
ce0c1f2b | 4177 | if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) { |
6849b73c | 4178 | ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
0f113f3e MC |
4179 | return 0; |
4180 | } | |
4181 | } | |
0f113f3e MC |
4182 | return 1; /* all checks are ok */ |
4183 | } | |
ea262260 | 4184 | |
38b051a1 TM |
4185 | int ssl_get_server_cert_serverinfo(SSL_CONNECTION *s, |
4186 | const unsigned char **serverinfo, | |
0f113f3e MC |
4187 | size_t *serverinfo_length) |
4188 | { | |
555cbb32 | 4189 | CERT_PKEY *cpk = s->s3.tmp.cert; |
0f113f3e MC |
4190 | *serverinfo_length = 0; |
4191 | ||
a497cf25 | 4192 | if (cpk == NULL || cpk->serverinfo == NULL) |
0f113f3e MC |
4193 | return 0; |
4194 | ||
a497cf25 DSH |
4195 | *serverinfo = cpk->serverinfo; |
4196 | *serverinfo_length = cpk->serverinfo_length; | |
0f113f3e MC |
4197 | return 1; |
4198 | } | |
0f113f3e | 4199 | |
38b051a1 | 4200 | void ssl_update_cache(SSL_CONNECTION *s, int mode) |
0f113f3e MC |
4201 | { |
4202 | int i; | |
4203 | ||
4204 | /* | |
4205 | * If the session_id_length is 0, we are not supposed to cache it, and it | |
4206 | * would be rather hard to do anyway :-) | |
4207 | */ | |
4208 | if (s->session->session_id_length == 0) | |
4209 | return; | |
4210 | ||
d316cdcf BK |
4211 | /* |
4212 | * If sid_ctx_length is 0 there is no specific application context | |
4213 | * associated with this session, so when we try to resume it and | |
c4fa1f7f BK |
4214 | * SSL_VERIFY_PEER is requested to verify the client identity, we have no |
4215 | * indication that this is actually a session for the proper application | |
4216 | * context, and the *handshake* will fail, not just the resumption attempt. | |
4217 | * Do not cache (on the server) these sessions that are not resumable | |
4218 | * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set). | |
d316cdcf | 4219 | */ |
c4fa1f7f | 4220 | if (s->server && s->session->sid_ctx_length == 0 |
d316cdcf BK |
4221 | && (s->verify_mode & SSL_VERIFY_PEER) != 0) |
4222 | return; | |
4223 | ||
0f113f3e | 4224 | i = s->session_ctx->session_cache_mode; |
5d61491c | 4225 | if ((i & mode) != 0 |
38b051a1 | 4226 | && (!s->hit || SSL_CONNECTION_IS_TLS13(s))) { |
ee94ec2e MC |
4227 | /* |
4228 | * Add the session to the internal cache. In server side TLSv1.3 we | |
6cc0b3c2 MC |
4229 | * normally don't do this because by default it's a full stateless ticket |
4230 | * with only a dummy session id so there is no reason to cache it, | |
4231 | * unless: | |
ee94ec2e MC |
4232 | * - we are doing early_data, in which case we cache so that we can |
4233 | * detect replays | |
4234 | * - the application has set a remove_session_cb so needs to know about | |
4235 | * session timeout events | |
6cc0b3c2 | 4236 | * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket |
ee94ec2e MC |
4237 | */ |
4238 | if ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0 | |
38b051a1 | 4239 | && (!SSL_CONNECTION_IS_TLS13(s) |
ee94ec2e | 4240 | || !s->server |
5d263fb7 MC |
4241 | || (s->max_early_data > 0 |
4242 | && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0) | |
6cc0b3c2 MC |
4243 | || s->session_ctx->remove_session_cb != NULL |
4244 | || (s->options & SSL_OP_NO_TICKET) != 0)) | |
ee94ec2e MC |
4245 | SSL_CTX_add_session(s->session_ctx, s->session); |
4246 | ||
4247 | /* | |
4248 | * Add the session to the external cache. We do this even in server side | |
4249 | * TLSv1.3 without early data because some applications just want to | |
4250 | * know about the creation of a session and aren't doing a full cache. | |
4251 | */ | |
4252 | if (s->session_ctx->new_session_cb != NULL) { | |
4253 | SSL_SESSION_up_ref(s->session); | |
38b051a1 TM |
4254 | if (!s->session_ctx->new_session_cb(SSL_CONNECTION_GET_SSL(s), |
4255 | s->session)) | |
ee94ec2e MC |
4256 | SSL_SESSION_free(s->session); |
4257 | } | |
0f113f3e MC |
4258 | } |
4259 | ||
4260 | /* auto flush every 255 connections */ | |
4261 | if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) { | |
9ef9088c | 4262 | TSAN_QUALIFIER int *stat; |
acce0557 | 4263 | |
1fcb4e4d BK |
4264 | if (mode & SSL_SESS_CACHE_CLIENT) |
4265 | stat = &s->session_ctx->stats.sess_connect_good; | |
4266 | else | |
4267 | stat = &s->session_ctx->stats.sess_accept_good; | |
acce0557 | 4268 | if ((ssl_tsan_load(s->session_ctx, stat) & 0xff) == 0xff) |
0f113f3e | 4269 | SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL)); |
0f113f3e MC |
4270 | } |
4271 | } | |
d02b48c6 | 4272 | |
3499327b | 4273 | const SSL_METHOD *SSL_CTX_get_ssl_method(const SSL_CTX *ctx) |
0f113f3e MC |
4274 | { |
4275 | return ctx->method; | |
4276 | } | |
ba168244 | 4277 | |
3499327b | 4278 | const SSL_METHOD *SSL_get_ssl_method(const SSL *s) |
0f113f3e | 4279 | { |
26a7d938 | 4280 | return s->method; |
0f113f3e | 4281 | } |
d02b48c6 | 4282 | |
4ebb342f | 4283 | int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth) |
0f113f3e | 4284 | { |
0f113f3e | 4285 | int ret = 1; |
38b051a1 TM |
4286 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4287 | ||
4288 | /* TODO(QUIC): Do we want this for QUIC? */ | |
4289 | if (sc == NULL | |
4290 | || (s->type != SSL_TYPE_SSL_CONNECTION && s->method != meth)) | |
4291 | return 0; | |
0f113f3e MC |
4292 | |
4293 | if (s->method != meth) { | |
919ba009 | 4294 | const SSL_METHOD *sm = s->method; |
38b051a1 | 4295 | int (*hf) (SSL *) = sc->handshake_func; |
0f113f3e | 4296 | |
919ba009 | 4297 | if (sm->version == meth->version) |
0f113f3e MC |
4298 | s->method = meth; |
4299 | else { | |
38b051a1 | 4300 | sm->ssl_deinit(s); |
0f113f3e | 4301 | s->method = meth; |
38b051a1 | 4302 | ret = s->method->ssl_init(s); |
0f113f3e MC |
4303 | } |
4304 | ||
919ba009 | 4305 | if (hf == sm->ssl_connect) |
38b051a1 | 4306 | sc->handshake_func = meth->ssl_connect; |
919ba009 | 4307 | else if (hf == sm->ssl_accept) |
38b051a1 | 4308 | sc->handshake_func = meth->ssl_accept; |
0f113f3e | 4309 | } |
26a7d938 | 4310 | return ret; |
0f113f3e MC |
4311 | } |
4312 | ||
4313 | int SSL_get_error(const SSL *s, int i) | |
4314 | { | |
4315 | int reason; | |
4316 | unsigned long l; | |
4317 | BIO *bio; | |
38b051a1 | 4318 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
0f113f3e MC |
4319 | |
4320 | if (i > 0) | |
26a7d938 | 4321 | return SSL_ERROR_NONE; |
0f113f3e | 4322 | |
38b051a1 TM |
4323 | /* TODO(QUIC): This will need more handling for QUIC_CONNECTIONs */ |
4324 | if (sc == NULL) | |
4325 | return SSL_ERROR_SSL; | |
4326 | ||
0f113f3e MC |
4327 | /* |
4328 | * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc, | |
4329 | * where we do encode the error | |
4330 | */ | |
4331 | if ((l = ERR_peek_error()) != 0) { | |
4332 | if (ERR_GET_LIB(l) == ERR_LIB_SYS) | |
26a7d938 | 4333 | return SSL_ERROR_SYSCALL; |
0f113f3e | 4334 | else |
26a7d938 | 4335 | return SSL_ERROR_SSL; |
0f113f3e MC |
4336 | } |
4337 | ||
8051ab2b MC |
4338 | if (SSL_want_read(s)) { |
4339 | bio = SSL_get_rbio(s); | |
4340 | if (BIO_should_read(bio)) | |
26a7d938 | 4341 | return SSL_ERROR_WANT_READ; |
8051ab2b MC |
4342 | else if (BIO_should_write(bio)) |
4343 | /* | |
4344 | * This one doesn't make too much sense ... We never try to write | |
4345 | * to the rbio, and an application program where rbio and wbio | |
4346 | * are separate couldn't even know what it should wait for. | |
4347 | * However if we ever set s->rwstate incorrectly (so that we have | |
4348 | * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and | |
4349 | * wbio *are* the same, this test works around that bug; so it | |
4350 | * might be safer to keep it. | |
4351 | */ | |
26a7d938 | 4352 | return SSL_ERROR_WANT_WRITE; |
8051ab2b MC |
4353 | else if (BIO_should_io_special(bio)) { |
4354 | reason = BIO_get_retry_reason(bio); | |
4355 | if (reason == BIO_RR_CONNECT) | |
26a7d938 | 4356 | return SSL_ERROR_WANT_CONNECT; |
8051ab2b | 4357 | else if (reason == BIO_RR_ACCEPT) |
26a7d938 | 4358 | return SSL_ERROR_WANT_ACCEPT; |
8051ab2b | 4359 | else |
26a7d938 | 4360 | return SSL_ERROR_SYSCALL; /* unknown */ |
0f113f3e | 4361 | } |
8051ab2b | 4362 | } |
0f113f3e | 4363 | |
8051ab2b | 4364 | if (SSL_want_write(s)) { |
69687aa8 | 4365 | /* Access wbio directly - in order to use the buffered bio if present */ |
38b051a1 | 4366 | bio = sc->wbio; |
8051ab2b | 4367 | if (BIO_should_write(bio)) |
26a7d938 | 4368 | return SSL_ERROR_WANT_WRITE; |
8051ab2b | 4369 | else if (BIO_should_read(bio)) |
2e7dc7cd | 4370 | /* |
8051ab2b | 4371 | * See above (SSL_want_read(s) with BIO_should_write(bio)) |
2e7dc7cd | 4372 | */ |
26a7d938 | 4373 | return SSL_ERROR_WANT_READ; |
8051ab2b MC |
4374 | else if (BIO_should_io_special(bio)) { |
4375 | reason = BIO_get_retry_reason(bio); | |
4376 | if (reason == BIO_RR_CONNECT) | |
26a7d938 | 4377 | return SSL_ERROR_WANT_CONNECT; |
8051ab2b | 4378 | else if (reason == BIO_RR_ACCEPT) |
26a7d938 | 4379 | return SSL_ERROR_WANT_ACCEPT; |
8051ab2b | 4380 | else |
26a7d938 | 4381 | return SSL_ERROR_SYSCALL; |
0f113f3e | 4382 | } |
07bbc92c | 4383 | } |
6b1bb98f | 4384 | if (SSL_want_x509_lookup(s)) |
26a7d938 | 4385 | return SSL_ERROR_WANT_X509_LOOKUP; |
0c3eb279 DDO |
4386 | if (SSL_want_retry_verify(s)) |
4387 | return SSL_ERROR_WANT_RETRY_VERIFY; | |
6b1bb98f | 4388 | if (SSL_want_async(s)) |
8051ab2b | 4389 | return SSL_ERROR_WANT_ASYNC; |
6b1bb98f | 4390 | if (SSL_want_async_job(s)) |
8051ab2b | 4391 | return SSL_ERROR_WANT_ASYNC_JOB; |
a9c0d8be DB |
4392 | if (SSL_want_client_hello_cb(s)) |
4393 | return SSL_ERROR_WANT_CLIENT_HELLO_CB; | |
8051ab2b | 4394 | |
38b051a1 TM |
4395 | if ((sc->shutdown & SSL_RECEIVED_SHUTDOWN) && |
4396 | (sc->s3.warn_alert == SSL_AD_CLOSE_NOTIFY)) | |
26a7d938 | 4397 | return SSL_ERROR_ZERO_RETURN; |
8051ab2b | 4398 | |
26a7d938 | 4399 | return SSL_ERROR_SYSCALL; |
0f113f3e | 4400 | } |
d02b48c6 | 4401 | |
add2f5ca MC |
4402 | static int ssl_do_handshake_intern(void *vargs) |
4403 | { | |
38b051a1 TM |
4404 | struct ssl_async_args *args = (struct ssl_async_args *)vargs; |
4405 | SSL *s = args->s; | |
4406 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); | |
add2f5ca | 4407 | |
38b051a1 TM |
4408 | if (sc == NULL) |
4409 | return -1; | |
add2f5ca | 4410 | |
38b051a1 | 4411 | return sc->handshake_func(s); |
add2f5ca MC |
4412 | } |
4413 | ||
4f43d0e7 | 4414 | int SSL_do_handshake(SSL *s) |
0f113f3e MC |
4415 | { |
4416 | int ret = 1; | |
38b051a1 TM |
4417 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4418 | ||
4419 | /* TODO(QUIC): Special handling for QUIC will be needed */ | |
4420 | if (sc == NULL) | |
4421 | return -1; | |
0f113f3e | 4422 | |
38b051a1 | 4423 | if (sc->handshake_func == NULL) { |
6849b73c | 4424 | ERR_raise(ERR_LIB_SSL, SSL_R_CONNECTION_TYPE_NOT_SET); |
add2f5ca | 4425 | return -1; |
0f113f3e MC |
4426 | } |
4427 | ||
38b051a1 | 4428 | ossl_statem_check_finish_init(sc, -1); |
49e7fe12 | 4429 | |
c7f47786 | 4430 | s->method->ssl_renegotiate_check(s, 0); |
0f113f3e MC |
4431 | |
4432 | if (SSL_in_init(s) || SSL_in_before(s)) { | |
38b051a1 | 4433 | if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) { |
add2f5ca MC |
4434 | struct ssl_async_args args; |
4435 | ||
09134f18 | 4436 | memset(&args, 0, sizeof(args)); |
add2f5ca MC |
4437 | args.s = s; |
4438 | ||
7fecbf6f | 4439 | ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern); |
add2f5ca | 4440 | } else { |
38b051a1 | 4441 | ret = sc->handshake_func(s); |
add2f5ca | 4442 | } |
0f113f3e | 4443 | } |
add2f5ca | 4444 | return ret; |
0f113f3e MC |
4445 | } |
4446 | ||
4f43d0e7 | 4447 | void SSL_set_accept_state(SSL *s) |
0f113f3e | 4448 | { |
38b051a1 TM |
4449 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4450 | ||
4451 | /* TODO(QUIC): Special handling for QUIC will be needed */ | |
4452 | if (sc == NULL) | |
4453 | return; | |
4454 | ||
4455 | sc->server = 1; | |
4456 | sc->shutdown = 0; | |
4457 | ossl_statem_clear(sc); | |
4458 | sc->handshake_func = s->method->ssl_accept; | |
4459 | clear_ciphers(sc); | |
0f113f3e | 4460 | } |
d02b48c6 | 4461 | |
4f43d0e7 | 4462 | void SSL_set_connect_state(SSL *s) |
0f113f3e | 4463 | { |
38b051a1 TM |
4464 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4465 | ||
4466 | /* TODO(QUIC): Special handling for QUIC will be needed */ | |
4467 | if (sc == NULL) | |
4468 | return; | |
4469 | ||
4470 | sc->server = 0; | |
4471 | sc->shutdown = 0; | |
4472 | ossl_statem_clear(sc); | |
4473 | sc->handshake_func = s->method->ssl_connect; | |
4474 | clear_ciphers(sc); | |
0f113f3e | 4475 | } |
d02b48c6 | 4476 | |
4f43d0e7 | 4477 | int ssl_undefined_function(SSL *s) |
0f113f3e | 4478 | { |
6849b73c | 4479 | ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
26a7d938 | 4480 | return 0; |
0f113f3e | 4481 | } |
d02b48c6 | 4482 | |
41a15c4f | 4483 | int ssl_undefined_void_function(void) |
0f113f3e | 4484 | { |
6849b73c | 4485 | ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
26a7d938 | 4486 | return 0; |
0f113f3e | 4487 | } |
41a15c4f | 4488 | |
0821bcd4 | 4489 | int ssl_undefined_const_function(const SSL *s) |
0f113f3e | 4490 | { |
26a7d938 | 4491 | return 0; |
0f113f3e | 4492 | } |
0821bcd4 | 4493 | |
2b8fa1d5 | 4494 | const SSL_METHOD *ssl_bad_method(int ver) |
0f113f3e | 4495 | { |
6849b73c | 4496 | ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
26a7d938 | 4497 | return NULL; |
0f113f3e | 4498 | } |
d02b48c6 | 4499 | |
3eb2aff4 | 4500 | const char *ssl_protocol_to_string(int version) |
7d650072 | 4501 | { |
1287dabd | 4502 | switch (version) |
2abacef1 MC |
4503 | { |
4504 | case TLS1_3_VERSION: | |
582a17d6 | 4505 | return "TLSv1.3"; |
2abacef1 MC |
4506 | |
4507 | case TLS1_2_VERSION: | |
7d650072 | 4508 | return "TLSv1.2"; |
2abacef1 MC |
4509 | |
4510 | case TLS1_1_VERSION: | |
7d650072 | 4511 | return "TLSv1.1"; |
2abacef1 MC |
4512 | |
4513 | case TLS1_VERSION: | |
ee3a6c64 | 4514 | return "TLSv1"; |
2abacef1 MC |
4515 | |
4516 | case SSL3_VERSION: | |
7d650072 | 4517 | return "SSLv3"; |
2abacef1 MC |
4518 | |
4519 | case DTLS1_BAD_VER: | |
7d650072 | 4520 | return "DTLSv0.9"; |
2abacef1 MC |
4521 | |
4522 | case DTLS1_VERSION: | |
7d650072 | 4523 | return "DTLSv1"; |
2abacef1 MC |
4524 | |
4525 | case DTLS1_2_VERSION: | |
7d650072 | 4526 | return "DTLSv1.2"; |
2abacef1 MC |
4527 | |
4528 | default: | |
4529 | return "unknown"; | |
4530 | } | |
0f113f3e | 4531 | } |
d02b48c6 | 4532 | |
7d650072 KR |
4533 | const char *SSL_get_version(const SSL *s) |
4534 | { | |
38b051a1 TM |
4535 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
4536 | ||
4537 | /* TODO(QUIC): Should QUIC return QUIC or TLSv1.3? */ | |
4538 | if (sc == NULL) | |
4539 | return NULL; | |
4540 | ||
4541 | return ssl_protocol_to_string(sc->version); | |
7d650072 KR |
4542 | } |
4543 | ||
98732979 | 4544 | static int dup_ca_names(STACK_OF(X509_NAME) **dst, STACK_OF(X509_NAME) *src) |
0f113f3e MC |
4545 | { |
4546 | STACK_OF(X509_NAME) *sk; | |
4547 | X509_NAME *xn; | |
98732979 MC |
4548 | int i; |
4549 | ||
4550 | if (src == NULL) { | |
4551 | *dst = NULL; | |
4552 | return 1; | |
4553 | } | |
4554 | ||
4555 | if ((sk = sk_X509_NAME_new_null()) == NULL) | |
4556 | return 0; | |
4557 | for (i = 0; i < sk_X509_NAME_num(src); i++) { | |
4558 | xn = X509_NAME_dup(sk_X509_NAME_value(src, i)); | |
4559 | if (xn == NULL) { | |
4560 | sk_X509_NAME_pop_free(sk, X509_NAME_free); | |
4561 | return 0; | |
4562 | } | |
4563 | if (sk_X509_NAME_insert(sk, xn, i) == 0) { | |
4564 | X509_NAME_free(xn); | |
4565 | sk_X509_NAME_pop_free(sk, X509_NAME_free); | |
4566 | return 0; | |
4567 | } | |
4568 | } | |
4569 | *dst = sk; | |
4570 | ||
4571 | return 1; | |
4572 | } | |
4573 | ||
4574 | SSL *SSL_dup(SSL *s) | |
4575 | { | |
0f113f3e MC |
4576 | SSL *ret; |
4577 | int i; | |
38b051a1 TM |
4578 | /* TODO(QUIC): Add a SSL_METHOD function for duplication */ |
4579 | SSL_CONNECTION *retsc; | |
4580 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); | |
4581 | ||
4582 | if (sc == NULL) | |
4583 | return NULL; | |
0f113f3e | 4584 | |
919ba009 VD |
4585 | /* If we're not quiescent, just up_ref! */ |
4586 | if (!SSL_in_init(s) || !SSL_in_before(s)) { | |
2f545ae4 | 4587 | CRYPTO_UP_REF(&s->references, &i, s->lock); |
919ba009 VD |
4588 | return s; |
4589 | } | |
4590 | ||
4591 | /* | |
4592 | * Otherwise, copy configuration state, and session if set. | |
4593 | */ | |
0f113f3e | 4594 | if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL) |
26a7d938 | 4595 | return NULL; |
38b051a1 TM |
4596 | if ((retsc = SSL_CONNECTION_FROM_SSL_ONLY(ret)) == NULL) |
4597 | goto err; | |
0f113f3e | 4598 | |
38b051a1 | 4599 | if (sc->session != NULL) { |
919ba009 VD |
4600 | /* |
4601 | * Arranges to share the same session via up_ref. This "copies" | |
4602 | * session-id, SSL_METHOD, sid_ctx, and 'cert' | |
4603 | */ | |
61986d32 | 4604 | if (!SSL_copy_session_id(ret, s)) |
17dd65e6 | 4605 | goto err; |
0f113f3e MC |
4606 | } else { |
4607 | /* | |
4608 | * No session has been established yet, so we have to expect that | |
4609 | * s->cert or ret->cert will be changed later -- they should not both | |
4610 | * point to the same object, and thus we can't use | |
4611 | * SSL_copy_session_id. | |
4612 | */ | |
919ba009 VD |
4613 | if (!SSL_set_ssl_method(ret, s->method)) |
4614 | goto err; | |
0f113f3e | 4615 | |
38b051a1 TM |
4616 | if (sc->cert != NULL) { |
4617 | ssl_cert_free(retsc->cert); | |
4618 | retsc->cert = ssl_cert_dup(sc->cert); | |
4619 | if (retsc->cert == NULL) | |
0f113f3e MC |
4620 | goto err; |
4621 | } | |
4622 | ||
38b051a1 TM |
4623 | if (!SSL_set_session_id_context(ret, sc->sid_ctx, |
4624 | (int)sc->sid_ctx_length)) | |
69f68237 | 4625 | goto err; |
0f113f3e MC |
4626 | } |
4627 | ||
38b051a1 | 4628 | if (!ssl_dane_dup(retsc, sc)) |
9f6b22b8 | 4629 | goto err; |
38b051a1 TM |
4630 | retsc->version = sc->version; |
4631 | retsc->options = sc->options; | |
4632 | retsc->min_proto_version = sc->min_proto_version; | |
4633 | retsc->max_proto_version = sc->max_proto_version; | |
4634 | retsc->mode = sc->mode; | |
0f113f3e MC |
4635 | SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s)); |
4636 | SSL_set_read_ahead(ret, SSL_get_read_ahead(s)); | |
38b051a1 TM |
4637 | retsc->msg_callback = sc->msg_callback; |
4638 | retsc->msg_callback_arg = sc->msg_callback_arg; | |
0f113f3e MC |
4639 | SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s)); |
4640 | SSL_set_verify_depth(ret, SSL_get_verify_depth(s)); | |
38b051a1 | 4641 | retsc->generate_session_id = sc->generate_session_id; |
0f113f3e MC |
4642 | |
4643 | SSL_set_info_callback(ret, SSL_get_info_callback(s)); | |
4644 | ||
0f113f3e MC |
4645 | /* copy app data, a little dangerous perhaps */ |
4646 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data)) | |
4647 | goto err; | |
4648 | ||
38b051a1 TM |
4649 | retsc->server = sc->server; |
4650 | if (sc->handshake_func) { | |
4651 | if (sc->server) | |
919ba009 VD |
4652 | SSL_set_accept_state(ret); |
4653 | else | |
4654 | SSL_set_connect_state(ret); | |
4655 | } | |
38b051a1 TM |
4656 | retsc->shutdown = sc->shutdown; |
4657 | retsc->hit = sc->hit; | |
0f113f3e | 4658 | |
38b051a1 TM |
4659 | retsc->default_passwd_callback = sc->default_passwd_callback; |
4660 | retsc->default_passwd_callback_userdata = sc->default_passwd_callback_userdata; | |
a974e64a | 4661 | |
38b051a1 | 4662 | X509_VERIFY_PARAM_inherit(retsc->param, sc->param); |
0f113f3e MC |
4663 | |
4664 | /* dup the cipher_list and cipher_list_by_id stacks */ | |
38b051a1 TM |
4665 | if (sc->cipher_list != NULL) { |
4666 | if ((retsc->cipher_list = sk_SSL_CIPHER_dup(sc->cipher_list)) == NULL) | |
0f113f3e MC |
4667 | goto err; |
4668 | } | |
38b051a1 TM |
4669 | if (sc->cipher_list_by_id != NULL) |
4670 | if ((retsc->cipher_list_by_id = sk_SSL_CIPHER_dup(sc->cipher_list_by_id)) | |
0f113f3e MC |
4671 | == NULL) |
4672 | goto err; | |
4673 | ||
4674 | /* Dup the client_CA list */ | |
38b051a1 TM |
4675 | if (!dup_ca_names(&retsc->ca_names, sc->ca_names) |
4676 | || !dup_ca_names(&retsc->client_ca_names, sc->client_ca_names)) | |
98732979 MC |
4677 | goto err; |
4678 | ||
66696478 | 4679 | return ret; |
0f113f3e | 4680 | |
0f113f3e | 4681 | err: |
66696478 RS |
4682 | SSL_free(ret); |
4683 | return NULL; | |
0f113f3e | 4684 | } |
d02b48c6 | 4685 | |
38b051a1 | 4686 | void ssl_clear_cipher_ctx(SSL_CONNECTION *s) |
0f113f3e MC |
4687 | { |
4688 | if (s->enc_read_ctx != NULL) { | |
846ec07d | 4689 | EVP_CIPHER_CTX_free(s->enc_read_ctx); |
0f113f3e MC |
4690 | s->enc_read_ctx = NULL; |
4691 | } | |
4692 | if (s->enc_write_ctx != NULL) { | |
846ec07d | 4693 | EVP_CIPHER_CTX_free(s->enc_write_ctx); |
0f113f3e MC |
4694 | s->enc_write_ctx = NULL; |
4695 | } | |
09b6c2ef | 4696 | #ifndef OPENSSL_NO_COMP |
efa7dd64 RS |
4697 | COMP_CTX_free(s->expand); |
4698 | s->expand = NULL; | |
4699 | COMP_CTX_free(s->compress); | |
4700 | s->compress = NULL; | |
0f113f3e MC |
4701 | #endif |
4702 | } | |
d02b48c6 | 4703 | |
0821bcd4 | 4704 | X509 *SSL_get_certificate(const SSL *s) |
0f113f3e | 4705 | { |
38b051a1 TM |
4706 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4707 | ||
4708 | if (sc == NULL) | |
4709 | return NULL; | |
4710 | ||
4711 | if (sc->cert != NULL) | |
4712 | return sc->cert->key->x509; | |
0f113f3e | 4713 | else |
26a7d938 | 4714 | return NULL; |
0f113f3e | 4715 | } |
d02b48c6 | 4716 | |
a25f9adc | 4717 | EVP_PKEY *SSL_get_privatekey(const SSL *s) |
0f113f3e | 4718 | { |
38b051a1 TM |
4719 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
4720 | ||
4721 | if (sc == NULL) | |
4722 | return NULL; | |
4723 | ||
4724 | if (sc->cert != NULL) | |
4725 | return sc->cert->key->privatekey; | |
0f113f3e | 4726 | else |
26a7d938 | 4727 | return NULL; |
0f113f3e | 4728 | } |
d02b48c6 | 4729 | |
a25f9adc | 4730 | X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) |
0f113f3e MC |
4731 | { |
4732 | if (ctx->cert != NULL) | |
4733 | return ctx->cert->key->x509; | |
4734 | else | |
4735 | return NULL; | |
4736 | } | |
a25f9adc DSH |
4737 | |
4738 | EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) | |
0f113f3e MC |
4739 | { |
4740 | if (ctx->cert != NULL) | |
4741 | return ctx->cert->key->privatekey; | |
4742 | else | |
4743 | return NULL; | |
4744 | } | |
a25f9adc | 4745 | |
babb3798 | 4746 | const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) |
0f113f3e | 4747 | { |
38b051a1 TM |
4748 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
4749 | ||
4750 | if (sc == NULL) | |
4751 | return NULL; | |
4752 | ||
4753 | if ((sc->session != NULL) && (sc->session->cipher != NULL)) | |
4754 | return sc->session->cipher; | |
26a7d938 | 4755 | return NULL; |
0f113f3e MC |
4756 | } |
4757 | ||
0aed6e44 BK |
4758 | const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s) |
4759 | { | |
38b051a1 TM |
4760 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
4761 | ||
4762 | if (sc == NULL) | |
4763 | return NULL; | |
4764 | ||
4765 | return sc->s3.tmp.new_cipher; | |
0aed6e44 BK |
4766 | } |
4767 | ||
3499327b | 4768 | const COMP_METHOD *SSL_get_current_compression(const SSL *s) |
0f113f3e | 4769 | { |
9a555706 | 4770 | #ifndef OPENSSL_NO_COMP |
38b051a1 TM |
4771 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s); |
4772 | ||
4773 | if (sc == NULL) | |
4774 | return NULL; | |
4775 | ||
4776 | return sc->compress ? COMP_CTX_get_method(sc->compress) : NULL; | |
9a555706 RS |
4777 | #else |
4778 | return NULL; | |
4779 | #endif | |
0f113f3e | 4780 | } |
377dcdba | 4781 | |
3499327b | 4782 | const COMP_METHOD *SSL_get_current_expansion(const SSL *s) |
0f113f3e | 4783 | { |
9a555706 | 4784 | #ifndef OPENSSL_NO_COMP |
38b051a1 TM |
4785 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s); |
4786 | ||
4787 | if (sc == NULL) | |
4788 | return NULL; | |
4789 | ||
4790 | return sc->expand ? COMP_CTX_get_method(sc->expand) : NULL; | |
9a555706 RS |
4791 | #else |
4792 | return NULL; | |
0f113f3e | 4793 | #endif |
9a555706 | 4794 | } |
0f113f3e | 4795 | |
38b051a1 | 4796 | int ssl_init_wbio_buffer(SSL_CONNECTION *s) |
0f113f3e MC |
4797 | { |
4798 | BIO *bbio; | |
4799 | ||
2e7dc7cd MC |
4800 | if (s->bbio != NULL) { |
4801 | /* Already buffered. */ | |
4802 | return 1; | |
0f113f3e | 4803 | } |
46417569 | 4804 | |
2e7dc7cd MC |
4805 | bbio = BIO_new(BIO_f_buffer()); |
4806 | if (bbio == NULL || !BIO_set_read_buffer_size(bbio, 1)) { | |
4807 | BIO_free(bbio); | |
6849b73c | 4808 | ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB); |
46417569 | 4809 | return 0; |
0f113f3e | 4810 | } |
2e7dc7cd MC |
4811 | s->bbio = bbio; |
4812 | s->wbio = BIO_push(bbio, s->wbio); | |
46417569 | 4813 | |
b5cf81f7 MC |
4814 | s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio); |
4815 | ||
46417569 | 4816 | return 1; |
0f113f3e | 4817 | } |
413c4f45 | 4818 | |
38b051a1 | 4819 | int ssl_free_wbio_buffer(SSL_CONNECTION *s) |
0f113f3e | 4820 | { |
62adbcee | 4821 | /* callers ensure s is never null */ |
0f113f3e | 4822 | if (s->bbio == NULL) |
b77f3ed1 | 4823 | return 1; |
0f113f3e | 4824 | |
2e7dc7cd | 4825 | s->wbio = BIO_pop(s->wbio); |
b5cf81f7 MC |
4826 | s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio); |
4827 | ||
0f113f3e MC |
4828 | BIO_free(s->bbio); |
4829 | s->bbio = NULL; | |
b77f3ed1 MC |
4830 | |
4831 | return 1; | |
0f113f3e MC |
4832 | } |
4833 | ||
4834 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) | |
4835 | { | |
4836 | ctx->quiet_shutdown = mode; | |
4837 | } | |
58964a49 | 4838 | |
0821bcd4 | 4839 | int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) |
0f113f3e | 4840 | { |
26a7d938 | 4841 | return ctx->quiet_shutdown; |
0f113f3e | 4842 | } |
58964a49 | 4843 | |
0f113f3e MC |
4844 | void SSL_set_quiet_shutdown(SSL *s, int mode) |
4845 | { | |
38b051a1 TM |
4846 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
4847 | ||
4848 | /* TODO(QUIC): Do we want this for QUIC? */ | |
4849 | if (sc == NULL) | |
4850 | return; | |
4851 | ||
4852 | sc->quiet_shutdown = mode; | |
0f113f3e | 4853 | } |
58964a49 | 4854 | |
0821bcd4 | 4855 | int SSL_get_quiet_shutdown(const SSL *s) |
0f113f3e | 4856 | { |
38b051a1 TM |
4857 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s); |
4858 | ||
4859 | /* TODO(QUIC): Do we want this for QUIC? */ | |
4860 | if (sc == NULL) | |
4861 | return 0; | |
4862 | ||
4863 | return sc->quiet_shutdown; | |
0f113f3e | 4864 | } |
58964a49 | 4865 | |
0f113f3e MC |
4866 | void SSL_set_shutdown(SSL *s, int mode) |
4867 | { | |
38b051a1 TM |
4868 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
4869 | ||
4870 | /* TODO(QUIC): Do we want this for QUIC? */ | |
4871 | if (sc == NULL) | |
4872 | return; | |
4873 | ||
4874 | sc->shutdown = mode; | |
0f113f3e | 4875 | } |
58964a49 | 4876 | |
0821bcd4 | 4877 | int SSL_get_shutdown(const SSL *s) |
0f113f3e | 4878 | { |
38b051a1 TM |
4879 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s); |
4880 | ||
4881 | /* TODO(QUIC): Do we want this for QUIC? */ | |
4882 | if (sc == NULL) | |
4883 | return 0; | |
4884 | ||
4885 | return sc->shutdown; | |
0f113f3e | 4886 | } |
58964a49 | 4887 | |
0821bcd4 | 4888 | int SSL_version(const SSL *s) |
0f113f3e | 4889 | { |
38b051a1 TM |
4890 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
4891 | ||
4892 | /* TODO(QUIC): Do we want to report QUIC version this way instead? */ | |
4893 | if (sc == NULL) | |
4894 | return 0; | |
4895 | ||
4896 | return sc->version; | |
6546e9b2 AG |
4897 | } |
4898 | ||
4899 | int SSL_client_version(const SSL *s) | |
4900 | { | |
38b051a1 TM |
4901 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
4902 | ||
4903 | /* TODO(QUIC): Do we want to report QUIC version this way instead? */ | |
4904 | if (sc == NULL) | |
4905 | return 0; | |
4906 | ||
4907 | return sc->client_version; | |
0f113f3e | 4908 | } |
58964a49 | 4909 | |
0821bcd4 | 4910 | SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) |
0f113f3e | 4911 | { |
6546e9b2 | 4912 | return ssl->ctx; |
0f113f3e MC |
4913 | } |
4914 | ||
4915 | SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) | |
4916 | { | |
24a0d393 | 4917 | CERT *new_cert; |
38b051a1 TM |
4918 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
4919 | ||
4920 | /* TODO(QUIC): Do we need this for QUIC support? */ | |
4921 | if (sc == NULL) | |
4922 | return NULL; | |
4923 | ||
0f113f3e MC |
4924 | if (ssl->ctx == ctx) |
4925 | return ssl->ctx; | |
0f113f3e | 4926 | if (ctx == NULL) |
38b051a1 | 4927 | ctx = sc->session_ctx; |
24a0d393 KR |
4928 | new_cert = ssl_cert_dup(ctx->cert); |
4929 | if (new_cert == NULL) { | |
4930 | return NULL; | |
0f113f3e | 4931 | } |
21181889 | 4932 | |
38b051a1 | 4933 | if (!custom_exts_copy_flags(&new_cert->custext, &sc->cert->custext)) { |
21181889 MC |
4934 | ssl_cert_free(new_cert); |
4935 | return NULL; | |
4936 | } | |
4937 | ||
38b051a1 TM |
4938 | ssl_cert_free(sc->cert); |
4939 | sc->cert = new_cert; | |
0f113f3e MC |
4940 | |
4941 | /* | |
4942 | * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH), | |
4943 | * so setter APIs must prevent invalid lengths from entering the system. | |
4944 | */ | |
38b051a1 | 4945 | if (!ossl_assert(sc->sid_ctx_length <= sizeof(sc->sid_ctx))) |
380a522f | 4946 | return NULL; |
0f113f3e MC |
4947 | |
4948 | /* | |
4949 | * If the session ID context matches that of the parent SSL_CTX, | |
4950 | * inherit it from the new SSL_CTX as well. If however the context does | |
4951 | * not match (i.e., it was set per-ssl with SSL_set_session_id_context), | |
4952 | * leave it unchanged. | |
4953 | */ | |
4954 | if ((ssl->ctx != NULL) && | |
38b051a1 TM |
4955 | (sc->sid_ctx_length == ssl->ctx->sid_ctx_length) && |
4956 | (memcmp(sc->sid_ctx, ssl->ctx->sid_ctx, sc->sid_ctx_length) == 0)) { | |
4957 | sc->sid_ctx_length = ctx->sid_ctx_length; | |
4958 | memcpy(&sc->sid_ctx, &ctx->sid_ctx, sizeof(sc->sid_ctx)); | |
0f113f3e MC |
4959 | } |
4960 | ||
16203f7b | 4961 | SSL_CTX_up_ref(ctx); |
a230b26e | 4962 | SSL_CTX_free(ssl->ctx); /* decrement reference count */ |
0f113f3e MC |
4963 | ssl->ctx = ctx; |
4964 | ||
16203f7b | 4965 | return ssl->ctx; |
0f113f3e | 4966 | } |
ed3883d2 | 4967 | |
4f43d0e7 | 4968 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) |
0f113f3e | 4969 | { |
d8652be0 MC |
4970 | return X509_STORE_set_default_paths_ex(ctx->cert_store, ctx->libctx, |
4971 | ctx->propq); | |
0f113f3e | 4972 | } |
58964a49 | 4973 | |
d84a7b20 MC |
4974 | int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx) |
4975 | { | |
4976 | X509_LOOKUP *lookup; | |
4977 | ||
4978 | lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir()); | |
4979 | if (lookup == NULL) | |
4980 | return 0; | |
6dcb100f RL |
4981 | |
4982 | /* We ignore errors, in case the directory doesn't exist */ | |
4983 | ERR_set_mark(); | |
4984 | ||
d84a7b20 MC |
4985 | X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT); |
4986 | ||
6dcb100f | 4987 | ERR_pop_to_mark(); |
d84a7b20 MC |
4988 | |
4989 | return 1; | |
4990 | } | |
4991 | ||
4992 | int SSL_CTX_set_default_verify_file(SSL_CTX *ctx) | |
4993 | { | |
4994 | X509_LOOKUP *lookup; | |
4995 | ||
4996 | lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file()); | |
4997 | if (lookup == NULL) | |
4998 | return 0; | |
4999 | ||
492bc359 | 5000 | /* We ignore errors, in case the file doesn't exist */ |
6dcb100f RL |
5001 | ERR_set_mark(); |
5002 | ||
d8652be0 MC |
5003 | X509_LOOKUP_load_file_ex(lookup, NULL, X509_FILETYPE_DEFAULT, ctx->libctx, |
5004 | ctx->propq); | |
d84a7b20 | 5005 | |
6dcb100f RL |
5006 | ERR_pop_to_mark(); |
5007 | ||
5008 | return 1; | |
5009 | } | |
5010 | ||
5011 | int SSL_CTX_set_default_verify_store(SSL_CTX *ctx) | |
5012 | { | |
5013 | X509_LOOKUP *lookup; | |
5014 | ||
5015 | lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_store()); | |
5016 | if (lookup == NULL) | |
5017 | return 0; | |
5018 | ||
5019 | /* We ignore errors, in case the directory doesn't exist */ | |
5020 | ERR_set_mark(); | |
5021 | ||
d8652be0 | 5022 | X509_LOOKUP_add_store_ex(lookup, NULL, ctx->libctx, ctx->propq); |
6dcb100f RL |
5023 | |
5024 | ERR_pop_to_mark(); | |
d84a7b20 MC |
5025 | |
5026 | return 1; | |
5027 | } | |
5028 | ||
6dcb100f RL |
5029 | int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile) |
5030 | { | |
d8652be0 MC |
5031 | return X509_STORE_load_file_ex(ctx->cert_store, CAfile, ctx->libctx, |
5032 | ctx->propq); | |
6dcb100f RL |
5033 | } |
5034 | ||
5035 | int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath) | |
5036 | { | |
5037 | return X509_STORE_load_path(ctx->cert_store, CApath); | |
5038 | } | |
5039 | ||
5040 | int SSL_CTX_load_verify_store(SSL_CTX *ctx, const char *CAstore) | |
5041 | { | |
d8652be0 MC |
5042 | return X509_STORE_load_store_ex(ctx->cert_store, CAstore, ctx->libctx, |
5043 | ctx->propq); | |
6dcb100f RL |
5044 | } |
5045 | ||
303c0028 | 5046 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, |
0f113f3e MC |
5047 | const char *CApath) |
5048 | { | |
6dcb100f RL |
5049 | if (CAfile == NULL && CApath == NULL) |
5050 | return 0; | |
5051 | if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile)) | |
5052 | return 0; | |
5053 | if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath)) | |
5054 | return 0; | |
5055 | return 1; | |
0f113f3e | 5056 | } |
58964a49 | 5057 | |
45d87a1f | 5058 | void SSL_set_info_callback(SSL *ssl, |
0f113f3e MC |
5059 | void (*cb) (const SSL *ssl, int type, int val)) |
5060 | { | |
38b051a1 TM |
5061 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
5062 | ||
5063 | if (sc == NULL) | |
5064 | return; | |
5065 | ||
5066 | sc->info_callback = cb; | |
0f113f3e MC |
5067 | } |
5068 | ||
5069 | /* | |
5070 | * One compiler (Diab DCC) doesn't like argument names in returned function | |
5071 | * pointer. | |
5072 | */ | |
5073 | void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ , | |
5074 | int /* type */ , | |
5075 | int /* val */ ) { | |
38b051a1 TM |
5076 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl); |
5077 | ||
5078 | if (sc == NULL) | |
5079 | return NULL; | |
5080 | ||
5081 | return sc->info_callback; | |
0f113f3e | 5082 | } |
58964a49 | 5083 | |
0f113f3e MC |
5084 | void SSL_set_verify_result(SSL *ssl, long arg) |
5085 | { | |
38b051a1 TM |
5086 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
5087 | ||
5088 | if (sc == NULL) | |
5089 | return; | |
5090 | ||
5091 | sc->verify_result = arg; | |
0f113f3e | 5092 | } |
58964a49 | 5093 | |
0821bcd4 | 5094 | long SSL_get_verify_result(const SSL *ssl) |
0f113f3e | 5095 | { |
38b051a1 TM |
5096 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl); |
5097 | ||
5098 | if (sc == NULL) | |
5099 | return 0; | |
5100 | ||
5101 | return sc->verify_result; | |
0f113f3e MC |
5102 | } |
5103 | ||
d9f1c639 | 5104 | size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen) |
858618e7 | 5105 | { |
38b051a1 TM |
5106 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl); |
5107 | ||
5108 | if (sc == NULL) | |
5109 | return 0; | |
5110 | ||
6b8f5d0d | 5111 | if (outlen == 0) |
38b051a1 TM |
5112 | return sizeof(sc->s3.client_random); |
5113 | if (outlen > sizeof(sc->s3.client_random)) | |
5114 | outlen = sizeof(sc->s3.client_random); | |
5115 | memcpy(out, sc->s3.client_random, outlen); | |
d9f1c639 | 5116 | return outlen; |
858618e7 NM |
5117 | } |
5118 | ||
d9f1c639 | 5119 | size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen) |
858618e7 | 5120 | { |
38b051a1 TM |
5121 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl); |
5122 | ||
5123 | if (sc == NULL) | |
5124 | return 0; | |
5125 | ||
6b8f5d0d | 5126 | if (outlen == 0) |
38b051a1 TM |
5127 | return sizeof(sc->s3.server_random); |
5128 | if (outlen > sizeof(sc->s3.server_random)) | |
5129 | outlen = sizeof(sc->s3.server_random); | |
5130 | memcpy(out, sc->s3.server_random, outlen); | |
d9f1c639 | 5131 | return outlen; |
858618e7 NM |
5132 | } |
5133 | ||
d9f1c639 | 5134 | size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, |
a230b26e | 5135 | unsigned char *out, size_t outlen) |
858618e7 | 5136 | { |
d9f1c639 MC |
5137 | if (outlen == 0) |
5138 | return session->master_key_length; | |
8c1a5343 | 5139 | if (outlen > session->master_key_length) |
858618e7 NM |
5140 | outlen = session->master_key_length; |
5141 | memcpy(out, session->master_key, outlen); | |
d9f1c639 | 5142 | return outlen; |
858618e7 NM |
5143 | } |
5144 | ||
725b0f1e | 5145 | int SSL_SESSION_set1_master_key(SSL_SESSION *sess, const unsigned char *in, |
911d63f2 MC |
5146 | size_t len) |
5147 | { | |
5148 | if (len > sizeof(sess->master_key)) | |
5149 | return 0; | |
5150 | ||
5151 | memcpy(sess->master_key, in, len); | |
5152 | sess->master_key_length = len; | |
911d63f2 MC |
5153 | return 1; |
5154 | } | |
5155 | ||
5156 | ||
0f113f3e MC |
5157 | int SSL_set_ex_data(SSL *s, int idx, void *arg) |
5158 | { | |
26a7d938 | 5159 | return CRYPTO_set_ex_data(&s->ex_data, idx, arg); |
0f113f3e MC |
5160 | } |
5161 | ||
5162 | void *SSL_get_ex_data(const SSL *s, int idx) | |
5163 | { | |
26a7d938 | 5164 | return CRYPTO_get_ex_data(&s->ex_data, idx); |
0f113f3e MC |
5165 | } |
5166 | ||
0f113f3e MC |
5167 | int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) |
5168 | { | |
26a7d938 | 5169 | return CRYPTO_set_ex_data(&s->ex_data, idx, arg); |
0f113f3e MC |
5170 | } |
5171 | ||
5172 | void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) | |
5173 | { | |
26a7d938 | 5174 | return CRYPTO_get_ex_data(&s->ex_data, idx); |
0f113f3e | 5175 | } |
58964a49 | 5176 | |
0821bcd4 | 5177 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) |
0f113f3e | 5178 | { |
26a7d938 | 5179 | return ctx->cert_store; |
0f113f3e | 5180 | } |
413c4f45 | 5181 | |
0f113f3e MC |
5182 | void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) |
5183 | { | |
222561fe | 5184 | X509_STORE_free(ctx->cert_store); |
0f113f3e MC |
5185 | ctx->cert_store = store; |
5186 | } | |
413c4f45 | 5187 | |
b50052db TS |
5188 | void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store) |
5189 | { | |
5190 | if (store != NULL) | |
5191 | X509_STORE_up_ref(store); | |
5192 | SSL_CTX_set_cert_store(ctx, store); | |
5193 | } | |
5194 | ||
0821bcd4 | 5195 | int SSL_want(const SSL *s) |
0f113f3e | 5196 | { |
38b051a1 TM |
5197 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5198 | ||
5199 | if (sc == NULL) | |
5200 | return SSL_NOTHING; | |
5201 | ||
5202 | return sc->rwstate; | |
0f113f3e | 5203 | } |
413c4f45 | 5204 | |
ddac1974 NL |
5205 | #ifndef OPENSSL_NO_PSK |
5206 | int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) | |
0f113f3e MC |
5207 | { |
5208 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { | |
6849b73c | 5209 | ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
0f113f3e MC |
5210 | return 0; |
5211 | } | |
df6da24b | 5212 | OPENSSL_free(ctx->cert->psk_identity_hint); |
0f113f3e | 5213 | if (identity_hint != NULL) { |
7644a9ae | 5214 | ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint); |
df6da24b | 5215 | if (ctx->cert->psk_identity_hint == NULL) |
0f113f3e MC |
5216 | return 0; |
5217 | } else | |
df6da24b | 5218 | ctx->cert->psk_identity_hint = NULL; |
0f113f3e MC |
5219 | return 1; |
5220 | } | |
ddac1974 NL |
5221 | |
5222 | int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) | |
0f113f3e | 5223 | { |
38b051a1 TM |
5224 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5225 | ||
5226 | if (sc == NULL) | |
0f113f3e MC |
5227 | return 0; |
5228 | ||
0f113f3e | 5229 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { |
6849b73c | 5230 | ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
0f113f3e MC |
5231 | return 0; |
5232 | } | |
38b051a1 | 5233 | OPENSSL_free(sc->cert->psk_identity_hint); |
0f113f3e | 5234 | if (identity_hint != NULL) { |
38b051a1 TM |
5235 | sc->cert->psk_identity_hint = OPENSSL_strdup(identity_hint); |
5236 | if (sc->cert->psk_identity_hint == NULL) | |
0f113f3e MC |
5237 | return 0; |
5238 | } else | |
38b051a1 | 5239 | sc->cert->psk_identity_hint = NULL; |
0f113f3e MC |
5240 | return 1; |
5241 | } | |
ddac1974 NL |
5242 | |
5243 | const char *SSL_get_psk_identity_hint(const SSL *s) | |
0f113f3e | 5244 | { |
38b051a1 TM |
5245 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5246 | ||
5247 | if (sc == NULL || sc->session == NULL) | |
0f113f3e | 5248 | return NULL; |
38b051a1 TM |
5249 | |
5250 | return sc->session->psk_identity_hint; | |
0f113f3e | 5251 | } |
ddac1974 NL |
5252 | |
5253 | const char *SSL_get_psk_identity(const SSL *s) | |
0f113f3e | 5254 | { |
38b051a1 TM |
5255 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5256 | ||
5257 | if (sc == NULL || sc->session == NULL) | |
0f113f3e | 5258 | return NULL; |
38b051a1 TM |
5259 | |
5260 | return sc->session->psk_identity; | |
0f113f3e | 5261 | } |
7806f3dd | 5262 | |
8cbfcc70 | 5263 | void SSL_set_psk_client_callback(SSL *s, SSL_psk_client_cb_func cb) |
0f113f3e | 5264 | { |
38b051a1 TM |
5265 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5266 | ||
5267 | if (sc == NULL) | |
5268 | return; | |
5269 | ||
5270 | sc->psk_client_callback = cb; | |
0f113f3e | 5271 | } |
7806f3dd | 5272 | |
8cbfcc70 | 5273 | void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb) |
0f113f3e MC |
5274 | { |
5275 | ctx->psk_client_callback = cb; | |
5276 | } | |
7806f3dd | 5277 | |
8cbfcc70 | 5278 | void SSL_set_psk_server_callback(SSL *s, SSL_psk_server_cb_func cb) |
0f113f3e | 5279 | { |
38b051a1 TM |
5280 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5281 | ||
5282 | if (sc == NULL) | |
5283 | return; | |
5284 | ||
5285 | sc->psk_server_callback = cb; | |
0f113f3e | 5286 | } |
7806f3dd | 5287 | |
8cbfcc70 | 5288 | void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb) |
0f113f3e MC |
5289 | { |
5290 | ctx->psk_server_callback = cb; | |
5291 | } | |
5292 | #endif | |
5293 | ||
f46184bd MC |
5294 | void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb) |
5295 | { | |
38b051a1 TM |
5296 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5297 | ||
5298 | if (sc == NULL) | |
5299 | return; | |
5300 | ||
5301 | sc->psk_find_session_cb = cb; | |
f46184bd MC |
5302 | } |
5303 | ||
5304 | void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx, | |
5305 | SSL_psk_find_session_cb_func cb) | |
5306 | { | |
5307 | ctx->psk_find_session_cb = cb; | |
5308 | } | |
5309 | ||
5310 | void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb) | |
5311 | { | |
38b051a1 TM |
5312 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5313 | ||
5314 | if (sc == NULL) | |
5315 | return; | |
5316 | ||
5317 | sc->psk_use_session_cb = cb; | |
f46184bd MC |
5318 | } |
5319 | ||
5320 | void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx, | |
5321 | SSL_psk_use_session_cb_func cb) | |
5322 | { | |
5323 | ctx->psk_use_session_cb = cb; | |
5324 | } | |
5325 | ||
0f113f3e MC |
5326 | void SSL_CTX_set_msg_callback(SSL_CTX *ctx, |
5327 | void (*cb) (int write_p, int version, | |
5328 | int content_type, const void *buf, | |
5329 | size_t len, SSL *ssl, void *arg)) | |
5330 | { | |
5331 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); | |
5332 | } | |
5333 | ||
5334 | void SSL_set_msg_callback(SSL *ssl, | |
5335 | void (*cb) (int write_p, int version, | |
5336 | int content_type, const void *buf, | |
5337 | size_t len, SSL *ssl, void *arg)) | |
5338 | { | |
5339 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); | |
5340 | } | |
a661b653 | 5341 | |
7c2d4fee | 5342 | void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx, |
0f113f3e MC |
5343 | int (*cb) (SSL *ssl, |
5344 | int | |
5345 | is_forward_secure)) | |
5346 | { | |
5347 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB, | |
5348 | (void (*)(void))cb); | |
5349 | } | |
5350 | ||
7c2d4fee | 5351 | void SSL_set_not_resumable_session_callback(SSL *ssl, |
0f113f3e MC |
5352 | int (*cb) (SSL *ssl, |
5353 | int is_forward_secure)) | |
5354 | { | |
5355 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB, | |
5356 | (void (*)(void))cb); | |
5357 | } | |
5358 | ||
c649d10d TS |
5359 | void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx, |
5360 | size_t (*cb) (SSL *ssl, int type, | |
5361 | size_t len, void *arg)) | |
5362 | { | |
5363 | ctx->record_padding_cb = cb; | |
5364 | } | |
5365 | ||
5366 | void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg) | |
5367 | { | |
5368 | ctx->record_padding_arg = arg; | |
5369 | } | |
5370 | ||
3499327b | 5371 | void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX *ctx) |
c649d10d TS |
5372 | { |
5373 | return ctx->record_padding_arg; | |
5374 | } | |
5375 | ||
5376 | int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size) | |
5377 | { | |
5378 | /* block size of 0 or 1 is basically no padding */ | |
5379 | if (block_size == 1) | |
5380 | ctx->block_padding = 0; | |
5381 | else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH) | |
5382 | ctx->block_padding = block_size; | |
5383 | else | |
5384 | return 0; | |
5385 | return 1; | |
5386 | } | |
5387 | ||
a6d36303 | 5388 | int SSL_set_record_padding_callback(SSL *ssl, |
c649d10d TS |
5389 | size_t (*cb) (SSL *ssl, int type, |
5390 | size_t len, void *arg)) | |
5391 | { | |
a6d36303 | 5392 | BIO *b; |
38b051a1 TM |
5393 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
5394 | ||
5395 | if (sc == NULL) | |
5396 | return 0; | |
a6d36303 VF |
5397 | |
5398 | b = SSL_get_wbio(ssl); | |
5399 | if (b == NULL || !BIO_get_ktls_send(b)) { | |
eb7d6c2a | 5400 | sc->rlayer.record_padding_cb = cb; |
a6d36303 VF |
5401 | return 1; |
5402 | } | |
5403 | return 0; | |
c649d10d TS |
5404 | } |
5405 | ||
5406 | void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg) | |
5407 | { | |
38b051a1 TM |
5408 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
5409 | ||
5410 | if (sc == NULL) | |
5411 | return; | |
5412 | ||
eb7d6c2a | 5413 | sc->rlayer.record_padding_arg = arg; |
c649d10d TS |
5414 | } |
5415 | ||
3499327b | 5416 | void *SSL_get_record_padding_callback_arg(const SSL *ssl) |
c649d10d | 5417 | { |
38b051a1 TM |
5418 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl); |
5419 | ||
5420 | if (sc == NULL) | |
5421 | return NULL; | |
5422 | ||
eb7d6c2a | 5423 | return sc->rlayer.record_padding_arg; |
c649d10d TS |
5424 | } |
5425 | ||
5426 | int SSL_set_block_padding(SSL *ssl, size_t block_size) | |
5427 | { | |
38b051a1 TM |
5428 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
5429 | ||
5430 | if (sc == NULL) | |
5431 | return 0; | |
5432 | ||
c649d10d TS |
5433 | /* block size of 0 or 1 is basically no padding */ |
5434 | if (block_size == 1) | |
eb7d6c2a | 5435 | sc->rlayer.block_padding = 0; |
c649d10d | 5436 | else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH) |
eb7d6c2a | 5437 | sc->rlayer.block_padding = block_size; |
c649d10d TS |
5438 | else |
5439 | return 0; | |
5440 | return 1; | |
5441 | } | |
5442 | ||
9d0a8bb7 MC |
5443 | int SSL_set_num_tickets(SSL *s, size_t num_tickets) |
5444 | { | |
38b051a1 TM |
5445 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5446 | ||
5447 | if (sc == NULL) | |
5448 | return 0; | |
5449 | ||
5450 | sc->num_tickets = num_tickets; | |
9d0a8bb7 MC |
5451 | |
5452 | return 1; | |
5453 | } | |
5454 | ||
3499327b | 5455 | size_t SSL_get_num_tickets(const SSL *s) |
9d0a8bb7 | 5456 | { |
38b051a1 TM |
5457 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5458 | ||
5459 | if (sc == NULL) | |
5460 | return 0; | |
5461 | ||
5462 | return sc->num_tickets; | |
9d0a8bb7 MC |
5463 | } |
5464 | ||
5465 | int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets) | |
5466 | { | |
5467 | ctx->num_tickets = num_tickets; | |
5468 | ||
5469 | return 1; | |
5470 | } | |
5471 | ||
3499327b | 5472 | size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx) |
9d0a8bb7 MC |
5473 | { |
5474 | return ctx->num_tickets; | |
5475 | } | |
5476 | ||
0f113f3e MC |
5477 | /* |
5478 | * Allocates new EVP_MD_CTX and sets pointer to it into given pointer | |
8483a003 | 5479 | * variable, freeing EVP_MD_CTX previously stored in that variable, if any. |
69687aa8 | 5480 | * If EVP_MD pointer is passed, initializes ctx with this |md|. |
48722ff5 | 5481 | * Returns the newly allocated ctx; |
8671b898 | 5482 | */ |
b948e2c5 | 5483 | |
0f113f3e | 5484 | EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md) |
b948e2c5 | 5485 | { |
0f113f3e | 5486 | ssl_clear_hash_ctx(hash); |
bfb0641f | 5487 | *hash = EVP_MD_CTX_new(); |
5f3d93e4 | 5488 | if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) { |
bfb0641f | 5489 | EVP_MD_CTX_free(*hash); |
5f3d93e4 MC |
5490 | *hash = NULL; |
5491 | return NULL; | |
5492 | } | |
0f113f3e | 5493 | return *hash; |
b948e2c5 | 5494 | } |
0f113f3e MC |
5495 | |
5496 | void ssl_clear_hash_ctx(EVP_MD_CTX **hash) | |
b948e2c5 DSH |
5497 | { |
5498 | ||
0d9824c1 | 5499 | EVP_MD_CTX_free(*hash); |
0f113f3e | 5500 | *hash = NULL; |
b948e2c5 | 5501 | } |
a661b653 | 5502 | |
48fbcbac | 5503 | /* Retrieve handshake hashes */ |
38b051a1 TM |
5504 | int ssl_handshake_hash(SSL_CONNECTION *s, |
5505 | unsigned char *out, size_t outlen, | |
8c1a5343 | 5506 | size_t *hashlen) |
48fbcbac | 5507 | { |
6e59a892 | 5508 | EVP_MD_CTX *ctx = NULL; |
555cbb32 | 5509 | EVP_MD_CTX *hdgst = s->s3.handshake_dgst; |
ed576acd | 5510 | int hashleni = EVP_MD_CTX_get_size(hdgst); |
8c1a5343 MC |
5511 | int ret = 0; |
5512 | ||
f63a17d6 | 5513 | if (hashleni < 0 || (size_t)hashleni > outlen) { |
c48ffbcc | 5514 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
28ba2541 | 5515 | goto err; |
f63a17d6 | 5516 | } |
8c1a5343 | 5517 | |
bfb0641f | 5518 | ctx = EVP_MD_CTX_new(); |
147ed5f9 TL |
5519 | if (ctx == NULL) { |
5520 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
6e59a892 | 5521 | goto err; |
147ed5f9 | 5522 | } |
8c1a5343 | 5523 | |
6e59a892 | 5524 | if (!EVP_MD_CTX_copy_ex(ctx, hdgst) |
f63a17d6 | 5525 | || EVP_DigestFinal_ex(ctx, out, NULL) <= 0) { |
c48ffbcc | 5526 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
8c1a5343 | 5527 | goto err; |
f63a17d6 | 5528 | } |
8c1a5343 MC |
5529 | |
5530 | *hashlen = hashleni; | |
5531 | ||
5532 | ret = 1; | |
48fbcbac | 5533 | err: |
bfb0641f | 5534 | EVP_MD_CTX_free(ctx); |
48fbcbac DSH |
5535 | return ret; |
5536 | } | |
5537 | ||
c04b66b1 | 5538 | int SSL_session_reused(const SSL *s) |
0f113f3e | 5539 | { |
38b051a1 TM |
5540 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5541 | ||
5542 | if (sc == NULL) | |
5543 | return 0; | |
5544 | ||
5545 | return sc->hit; | |
0f113f3e | 5546 | } |
08557cf2 | 5547 | |
69443116 | 5548 | int SSL_is_server(const SSL *s) |
0f113f3e | 5549 | { |
38b051a1 TM |
5550 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5551 | ||
5552 | if (sc == NULL) | |
5553 | return 0; | |
5554 | ||
5555 | return sc->server; | |
0f113f3e | 5556 | } |
87adf1fa | 5557 | |
00db8c60 | 5558 | #ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
47153c72 RS |
5559 | void SSL_set_debug(SSL *s, int debug) |
5560 | { | |
5561 | /* Old function was do-nothing anyway... */ | |
5562 | (void)s; | |
5563 | (void)debug; | |
5564 | } | |
5565 | #endif | |
5566 | ||
b362ccab | 5567 | void SSL_set_security_level(SSL *s, int level) |
0f113f3e | 5568 | { |
38b051a1 TM |
5569 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5570 | ||
5571 | if (sc == NULL) | |
5572 | return; | |
5573 | ||
5574 | sc->cert->sec_level = level; | |
0f113f3e | 5575 | } |
b362ccab DSH |
5576 | |
5577 | int SSL_get_security_level(const SSL *s) | |
0f113f3e | 5578 | { |
38b051a1 TM |
5579 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5580 | ||
5581 | if (sc == NULL) | |
5582 | return 0; | |
5583 | ||
5584 | return sc->cert->sec_level; | |
0f113f3e | 5585 | } |
b362ccab | 5586 | |
0f113f3e | 5587 | void SSL_set_security_callback(SSL *s, |
a230b26e EK |
5588 | int (*cb) (const SSL *s, const SSL_CTX *ctx, |
5589 | int op, int bits, int nid, | |
5590 | void *other, void *ex)) | |
0f113f3e | 5591 | { |
38b051a1 TM |
5592 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5593 | ||
5594 | if (sc == NULL) | |
5595 | return; | |
5596 | ||
5597 | sc->cert->sec_cb = cb; | |
0f113f3e | 5598 | } |
b362ccab | 5599 | |
a230b26e EK |
5600 | int (*SSL_get_security_callback(const SSL *s)) (const SSL *s, |
5601 | const SSL_CTX *ctx, int op, | |
5602 | int bits, int nid, void *other, | |
5603 | void *ex) { | |
38b051a1 TM |
5604 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5605 | ||
5606 | if (sc == NULL) | |
5607 | return NULL; | |
5608 | ||
5609 | return sc->cert->sec_cb; | |
0f113f3e | 5610 | } |
b362ccab DSH |
5611 | |
5612 | void SSL_set0_security_ex_data(SSL *s, void *ex) | |
0f113f3e | 5613 | { |
38b051a1 TM |
5614 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5615 | ||
5616 | if (sc == NULL) | |
5617 | return; | |
5618 | ||
5619 | sc->cert->sec_ex = ex; | |
0f113f3e | 5620 | } |
b362ccab DSH |
5621 | |
5622 | void *SSL_get0_security_ex_data(const SSL *s) | |
0f113f3e | 5623 | { |
38b051a1 TM |
5624 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5625 | ||
5626 | if (sc == NULL) | |
5627 | return NULL; | |
5628 | ||
5629 | return sc->cert->sec_ex; | |
0f113f3e | 5630 | } |
b362ccab DSH |
5631 | |
5632 | void SSL_CTX_set_security_level(SSL_CTX *ctx, int level) | |
0f113f3e MC |
5633 | { |
5634 | ctx->cert->sec_level = level; | |
5635 | } | |
b362ccab DSH |
5636 | |
5637 | int SSL_CTX_get_security_level(const SSL_CTX *ctx) | |
0f113f3e MC |
5638 | { |
5639 | return ctx->cert->sec_level; | |
5640 | } | |
b362ccab | 5641 | |
0f113f3e | 5642 | void SSL_CTX_set_security_callback(SSL_CTX *ctx, |
a230b26e EK |
5643 | int (*cb) (const SSL *s, const SSL_CTX *ctx, |
5644 | int op, int bits, int nid, | |
5645 | void *other, void *ex)) | |
0f113f3e MC |
5646 | { |
5647 | ctx->cert->sec_cb = cb; | |
5648 | } | |
b362ccab | 5649 | |
e4646a89 KR |
5650 | int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (const SSL *s, |
5651 | const SSL_CTX *ctx, | |
0f113f3e MC |
5652 | int op, int bits, |
5653 | int nid, | |
5654 | void *other, | |
5655 | void *ex) { | |
5656 | return ctx->cert->sec_cb; | |
5657 | } | |
b362ccab DSH |
5658 | |
5659 | void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex) | |
0f113f3e MC |
5660 | { |
5661 | ctx->cert->sec_ex = ex; | |
5662 | } | |
b362ccab DSH |
5663 | |
5664 | void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx) | |
0f113f3e MC |
5665 | { |
5666 | return ctx->cert->sec_ex; | |
5667 | } | |
b362ccab | 5668 | |
56bd1783 | 5669 | uint64_t SSL_CTX_get_options(const SSL_CTX *ctx) |
8106cb8b VD |
5670 | { |
5671 | return ctx->options; | |
5672 | } | |
a230b26e | 5673 | |
56bd1783 | 5674 | uint64_t SSL_get_options(const SSL *s) |
8106cb8b | 5675 | { |
38b051a1 TM |
5676 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5677 | ||
5678 | if (sc == NULL) | |
5679 | return 0; | |
5680 | ||
5681 | return sc->options; | |
8106cb8b | 5682 | } |
a230b26e | 5683 | |
56bd1783 | 5684 | uint64_t SSL_CTX_set_options(SSL_CTX *ctx, uint64_t op) |
8106cb8b VD |
5685 | { |
5686 | return ctx->options |= op; | |
5687 | } | |
a230b26e | 5688 | |
56bd1783 | 5689 | uint64_t SSL_set_options(SSL *s, uint64_t op) |
8106cb8b | 5690 | { |
38b051a1 | 5691 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4566dae7 | 5692 | OSSL_PARAM options[2], *opts = options; |
38b051a1 TM |
5693 | |
5694 | if (sc == NULL) | |
5695 | return 0; | |
5696 | ||
4566dae7 MC |
5697 | sc->options |= op; |
5698 | ||
5699 | *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS, | |
5700 | &sc->options); | |
5701 | *opts = OSSL_PARAM_construct_end(); | |
5702 | ||
5703 | /* Ignore return value */ | |
5704 | sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options); | |
5705 | ||
5706 | return sc->options; | |
8106cb8b | 5707 | } |
a230b26e | 5708 | |
56bd1783 | 5709 | uint64_t SSL_CTX_clear_options(SSL_CTX *ctx, uint64_t op) |
8106cb8b VD |
5710 | { |
5711 | return ctx->options &= ~op; | |
5712 | } | |
a230b26e | 5713 | |
56bd1783 | 5714 | uint64_t SSL_clear_options(SSL *s, uint64_t op) |
8106cb8b | 5715 | { |
38b051a1 TM |
5716 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5717 | ||
5718 | if (sc == NULL) | |
5719 | return 0; | |
5720 | ||
5721 | return sc->options &= ~op; | |
8106cb8b VD |
5722 | } |
5723 | ||
696178ed DSH |
5724 | STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s) |
5725 | { | |
38b051a1 TM |
5726 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5727 | ||
5728 | if (sc == NULL) | |
5729 | return NULL; | |
5730 | ||
5731 | return sc->verified_chain; | |
696178ed DSH |
5732 | } |
5733 | ||
0f113f3e | 5734 | IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id); |
ed29e82a RP |
5735 | |
5736 | #ifndef OPENSSL_NO_CT | |
5737 | ||
5738 | /* | |
5739 | * Moves SCTs from the |src| stack to the |dst| stack. | |
5740 | * The source of each SCT will be set to |origin|. | |
5741 | * If |dst| points to a NULL pointer, a new stack will be created and owned by | |
5742 | * the caller. | |
5743 | * Returns the number of SCTs moved, or a negative integer if an error occurs. | |
5744 | */ | |
a230b26e EK |
5745 | static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src, |
5746 | sct_source_t origin) | |
ed29e82a RP |
5747 | { |
5748 | int scts_moved = 0; | |
5749 | SCT *sct = NULL; | |
5750 | ||
5751 | if (*dst == NULL) { | |
5752 | *dst = sk_SCT_new_null(); | |
5753 | if (*dst == NULL) { | |
6849b73c | 5754 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
ed29e82a RP |
5755 | goto err; |
5756 | } | |
5757 | } | |
5758 | ||
30eba7f3 DDO |
5759 | while (sk_SCT_num(src) > 0) { |
5760 | sct = sk_SCT_pop(src); | |
ed29e82a RP |
5761 | if (SCT_set_source(sct, origin) != 1) |
5762 | goto err; | |
5763 | ||
5764 | if (sk_SCT_push(*dst, sct) <= 0) | |
5765 | goto err; | |
5766 | scts_moved += 1; | |
5767 | } | |
5768 | ||
5769 | return scts_moved; | |
a230b26e | 5770 | err: |
ed29e82a | 5771 | if (sct != NULL) |
a230b26e | 5772 | sk_SCT_push(src, sct); /* Put the SCT back */ |
cc7113e8 | 5773 | return -1; |
ed29e82a RP |
5774 | } |
5775 | ||
5776 | /* | |
a230b26e | 5777 | * Look for data collected during ServerHello and parse if found. |
6b13bd1d | 5778 | * Returns the number of SCTs extracted. |
a230b26e | 5779 | */ |
38b051a1 | 5780 | static int ct_extract_tls_extension_scts(SSL_CONNECTION *s) |
ed29e82a RP |
5781 | { |
5782 | int scts_extracted = 0; | |
5783 | ||
aff8c126 RS |
5784 | if (s->ext.scts != NULL) { |
5785 | const unsigned char *p = s->ext.scts; | |
5786 | STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->ext.scts_len); | |
ed29e82a RP |
5787 | |
5788 | scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION); | |
5789 | ||
5790 | SCT_LIST_free(scts); | |
5791 | } | |
5792 | ||
5793 | return scts_extracted; | |
5794 | } | |
5795 | ||
5796 | /* | |
5797 | * Checks for an OCSP response and then attempts to extract any SCTs found if it | |
5798 | * contains an SCT X509 extension. They will be stored in |s->scts|. | |
5799 | * Returns: | |
5800 | * - The number of SCTs extracted, assuming an OCSP response exists. | |
5801 | * - 0 if no OCSP response exists or it contains no SCTs. | |
5802 | * - A negative integer if an error occurs. | |
5803 | */ | |
38b051a1 | 5804 | static int ct_extract_ocsp_response_scts(SSL_CONNECTION *s) |
ed29e82a | 5805 | { |
a230b26e | 5806 | # ifndef OPENSSL_NO_OCSP |
ed29e82a RP |
5807 | int scts_extracted = 0; |
5808 | const unsigned char *p; | |
5809 | OCSP_BASICRESP *br = NULL; | |
5810 | OCSP_RESPONSE *rsp = NULL; | |
5811 | STACK_OF(SCT) *scts = NULL; | |
5812 | int i; | |
5813 | ||
aff8c126 | 5814 | if (s->ext.ocsp.resp == NULL || s->ext.ocsp.resp_len == 0) |
ed29e82a RP |
5815 | goto err; |
5816 | ||
aff8c126 RS |
5817 | p = s->ext.ocsp.resp; |
5818 | rsp = d2i_OCSP_RESPONSE(NULL, &p, (int)s->ext.ocsp.resp_len); | |
ed29e82a RP |
5819 | if (rsp == NULL) |
5820 | goto err; | |
5821 | ||
5822 | br = OCSP_response_get1_basic(rsp); | |
5823 | if (br == NULL) | |
5824 | goto err; | |
5825 | ||
5826 | for (i = 0; i < OCSP_resp_count(br); ++i) { | |
5827 | OCSP_SINGLERESP *single = OCSP_resp_get0(br, i); | |
5828 | ||
5829 | if (single == NULL) | |
5830 | continue; | |
5831 | ||
a230b26e EK |
5832 | scts = |
5833 | OCSP_SINGLERESP_get1_ext_d2i(single, NID_ct_cert_scts, NULL, NULL); | |
5834 | scts_extracted = | |
5835 | ct_move_scts(&s->scts, scts, SCT_SOURCE_OCSP_STAPLED_RESPONSE); | |
ed29e82a RP |
5836 | if (scts_extracted < 0) |
5837 | goto err; | |
5838 | } | |
a230b26e | 5839 | err: |
ed29e82a RP |
5840 | SCT_LIST_free(scts); |
5841 | OCSP_BASICRESP_free(br); | |
5842 | OCSP_RESPONSE_free(rsp); | |
5843 | return scts_extracted; | |
a230b26e | 5844 | # else |
3e41ac35 MC |
5845 | /* Behave as if no OCSP response exists */ |
5846 | return 0; | |
a230b26e | 5847 | # endif |
ed29e82a RP |
5848 | } |
5849 | ||
5850 | /* | |
5851 | * Attempts to extract SCTs from the peer certificate. | |
5852 | * Return the number of SCTs extracted, or a negative integer if an error | |
5853 | * occurs. | |
5854 | */ | |
38b051a1 | 5855 | static int ct_extract_x509v3_extension_scts(SSL_CONNECTION *s) |
ed29e82a RP |
5856 | { |
5857 | int scts_extracted = 0; | |
3f3c7d26 | 5858 | X509 *cert = s->session != NULL ? s->session->peer : NULL; |
ed29e82a RP |
5859 | |
5860 | if (cert != NULL) { | |
5861 | STACK_OF(SCT) *scts = | |
5862 | X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL); | |
5863 | ||
5864 | scts_extracted = | |
5865 | ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION); | |
5866 | ||
5867 | SCT_LIST_free(scts); | |
5868 | } | |
5869 | ||
5870 | return scts_extracted; | |
5871 | } | |
5872 | ||
5873 | /* | |
5874 | * Attempts to find all received SCTs by checking TLS extensions, the OCSP | |
5875 | * response (if it exists) and X509v3 extensions in the certificate. | |
5876 | * Returns NULL if an error occurs. | |
5877 | */ | |
5878 | const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s) | |
5879 | { | |
38b051a1 TM |
5880 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5881 | ||
5882 | if (sc == NULL) | |
5883 | return NULL; | |
5884 | ||
5885 | if (!sc->scts_parsed) { | |
5886 | if (ct_extract_tls_extension_scts(sc) < 0 || | |
5887 | ct_extract_ocsp_response_scts(sc) < 0 || | |
5888 | ct_extract_x509v3_extension_scts(sc) < 0) | |
ed29e82a RP |
5889 | goto err; |
5890 | ||
38b051a1 | 5891 | sc->scts_parsed = 1; |
ed29e82a | 5892 | } |
38b051a1 | 5893 | return sc->scts; |
a230b26e | 5894 | err: |
ed29e82a RP |
5895 | return NULL; |
5896 | } | |
5897 | ||
a230b26e | 5898 | static int ct_permissive(const CT_POLICY_EVAL_CTX * ctx, |
43341433 | 5899 | const STACK_OF(SCT) *scts, void *unused_arg) |
ed29e82a | 5900 | { |
43341433 VD |
5901 | return 1; |
5902 | } | |
5903 | ||
a230b26e | 5904 | static int ct_strict(const CT_POLICY_EVAL_CTX * ctx, |
43341433 VD |
5905 | const STACK_OF(SCT) *scts, void *unused_arg) |
5906 | { | |
5907 | int count = scts != NULL ? sk_SCT_num(scts) : 0; | |
5908 | int i; | |
ed29e82a | 5909 | |
43341433 VD |
5910 | for (i = 0; i < count; ++i) { |
5911 | SCT *sct = sk_SCT_value(scts, i); | |
5912 | int status = SCT_get_validation_status(sct); | |
5913 | ||
5914 | if (status == SCT_VALIDATION_STATUS_VALID) | |
5915 | return 1; | |
5916 | } | |
6849b73c | 5917 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_VALID_SCTS); |
43341433 VD |
5918 | return 0; |
5919 | } | |
5920 | ||
5921 | int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback, | |
5922 | void *arg) | |
5923 | { | |
38b051a1 TM |
5924 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
5925 | ||
5926 | if (sc == NULL) | |
5927 | return 0; | |
5928 | ||
ed29e82a RP |
5929 | /* |
5930 | * Since code exists that uses the custom extension handler for CT, look | |
5931 | * for this and throw an error if they have already registered to use CT. | |
5932 | */ | |
5933 | if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx, | |
a230b26e EK |
5934 | TLSEXT_TYPE_signed_certificate_timestamp)) |
5935 | { | |
6849b73c | 5936 | ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED); |
43341433 | 5937 | return 0; |
ed29e82a RP |
5938 | } |
5939 | ||
ed29e82a | 5940 | if (callback != NULL) { |
a230b26e EK |
5941 | /* |
5942 | * If we are validating CT, then we MUST accept SCTs served via OCSP | |
5943 | */ | |
ed29e82a | 5944 | if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp)) |
43341433 | 5945 | return 0; |
ed29e82a RP |
5946 | } |
5947 | ||
38b051a1 TM |
5948 | sc->ct_validation_callback = callback; |
5949 | sc->ct_validation_callback_arg = arg; | |
43341433 VD |
5950 | |
5951 | return 1; | |
ed29e82a RP |
5952 | } |
5953 | ||
43341433 | 5954 | int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx, |
a230b26e | 5955 | ssl_ct_validation_cb callback, void *arg) |
ed29e82a | 5956 | { |
ed29e82a RP |
5957 | /* |
5958 | * Since code exists that uses the custom extension handler for CT, look for | |
5959 | * this and throw an error if they have already registered to use CT. | |
5960 | */ | |
5961 | if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx, | |
a230b26e EK |
5962 | TLSEXT_TYPE_signed_certificate_timestamp)) |
5963 | { | |
6849b73c | 5964 | ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED); |
43341433 | 5965 | return 0; |
ed29e82a RP |
5966 | } |
5967 | ||
5968 | ctx->ct_validation_callback = callback; | |
5969 | ctx->ct_validation_callback_arg = arg; | |
43341433 | 5970 | return 1; |
ed29e82a RP |
5971 | } |
5972 | ||
43341433 | 5973 | int SSL_ct_is_enabled(const SSL *s) |
ed29e82a | 5974 | { |
38b051a1 TM |
5975 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
5976 | ||
5977 | if (sc == NULL) | |
5978 | return 0; | |
5979 | ||
5980 | return sc->ct_validation_callback != NULL; | |
ed29e82a RP |
5981 | } |
5982 | ||
43341433 | 5983 | int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx) |
ed29e82a | 5984 | { |
43341433 | 5985 | return ctx->ct_validation_callback != NULL; |
ed29e82a RP |
5986 | } |
5987 | ||
38b051a1 | 5988 | int ssl_validate_ct(SSL_CONNECTION *s) |
ed29e82a RP |
5989 | { |
5990 | int ret = 0; | |
3f3c7d26 | 5991 | X509 *cert = s->session != NULL ? s->session->peer : NULL; |
43341433 | 5992 | X509 *issuer; |
b9aec69a | 5993 | SSL_DANE *dane = &s->dane; |
ed29e82a RP |
5994 | CT_POLICY_EVAL_CTX *ctx = NULL; |
5995 | const STACK_OF(SCT) *scts; | |
5996 | ||
43341433 VD |
5997 | /* |
5998 | * If no callback is set, the peer is anonymous, or its chain is invalid, | |
5999 | * skip SCT validation - just return success. Applications that continue | |
6000 | * handshakes without certificates, with unverified chains, or pinned leaf | |
6001 | * certificates are outside the scope of the WebPKI and CT. | |
6002 | * | |
6003 | * The above exclusions notwithstanding the vast majority of peers will | |
6004 | * have rather ordinary certificate chains validated by typical | |
6005 | * applications that perform certificate verification and therefore will | |
6006 | * process SCTs when enabled. | |
6007 | */ | |
6008 | if (s->ct_validation_callback == NULL || cert == NULL || | |
6009 | s->verify_result != X509_V_OK || | |
a230b26e | 6010 | s->verified_chain == NULL || sk_X509_num(s->verified_chain) <= 1) |
ed29e82a RP |
6011 | return 1; |
6012 | ||
43341433 VD |
6013 | /* |
6014 | * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3) | |
6015 | * trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2 | |
6016 | */ | |
6017 | if (DANETLS_ENABLED(dane) && dane->mtlsa != NULL) { | |
6018 | switch (dane->mtlsa->usage) { | |
6019 | case DANETLS_USAGE_DANE_TA: | |
6020 | case DANETLS_USAGE_DANE_EE: | |
6021 | return 1; | |
6022 | } | |
ed29e82a RP |
6023 | } |
6024 | ||
38b051a1 TM |
6025 | ctx = CT_POLICY_EVAL_CTX_new_ex(SSL_CONNECTION_GET_CTX(s)->libctx, |
6026 | SSL_CONNECTION_GET_CTX(s)->propq); | |
ed29e82a | 6027 | if (ctx == NULL) { |
c48ffbcc | 6028 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); |
ed29e82a RP |
6029 | goto end; |
6030 | } | |
6031 | ||
43341433 | 6032 | issuer = sk_X509_value(s->verified_chain, 1); |
a1bb7708 RP |
6033 | CT_POLICY_EVAL_CTX_set1_cert(ctx, cert); |
6034 | CT_POLICY_EVAL_CTX_set1_issuer(ctx, issuer); | |
38b051a1 TM |
6035 | CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx, |
6036 | SSL_CONNECTION_GET_CTX(s)->ctlog_store); | |
6a71e06d | 6037 | CT_POLICY_EVAL_CTX_set_time( |
38b051a1 | 6038 | ctx, (uint64_t)SSL_SESSION_get_time(s->session) * 1000); |
ed29e82a | 6039 | |
38b051a1 | 6040 | scts = SSL_get0_peer_scts(SSL_CONNECTION_GET_SSL(s)); |
ed29e82a | 6041 | |
43341433 VD |
6042 | /* |
6043 | * This function returns success (> 0) only when all the SCTs are valid, 0 | |
6044 | * when some are invalid, and < 0 on various internal errors (out of | |
6045 | * memory, etc.). Having some, or even all, invalid SCTs is not sufficient | |
6046 | * reason to abort the handshake, that decision is up to the callback. | |
6047 | * Therefore, we error out only in the unexpected case that the return | |
6048 | * value is negative. | |
6049 | * | |
6050 | * XXX: One might well argue that the return value of this function is an | |
f430ba31 | 6051 | * unfortunate design choice. Its job is only to determine the validation |
43341433 VD |
6052 | * status of each of the provided SCTs. So long as it correctly separates |
6053 | * the wheat from the chaff it should return success. Failure in this case | |
6054 | * ought to correspond to an inability to carry out its duties. | |
6055 | */ | |
6056 | if (SCT_LIST_validate(scts, ctx) < 0) { | |
c48ffbcc | 6057 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_SCT_VERIFICATION_FAILED); |
ed29e82a RP |
6058 | goto end; |
6059 | } | |
6060 | ||
6061 | ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg); | |
6062 | if (ret < 0) | |
a230b26e | 6063 | ret = 0; /* This function returns 0 on failure */ |
f63a17d6 | 6064 | if (!ret) |
c48ffbcc | 6065 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_CALLBACK_FAILED); |
ed29e82a | 6066 | |
a230b26e | 6067 | end: |
ed29e82a | 6068 | CT_POLICY_EVAL_CTX_free(ctx); |
f75b34c8 VD |
6069 | /* |
6070 | * With SSL_VERIFY_NONE the session may be cached and re-used despite a | |
6071 | * failure return code here. Also the application may wish the complete | |
6072 | * the handshake, and then disconnect cleanly at a higher layer, after | |
6073 | * checking the verification status of the completed connection. | |
6074 | * | |
6075 | * We therefore force a certificate verification failure which will be | |
6076 | * visible via SSL_get_verify_result() and cached as part of any resumed | |
6077 | * session. | |
6078 | * | |
6079 | * Note: the permissive callback is for information gathering only, always | |
6080 | * returns success, and does not affect verification status. Only the | |
6081 | * strict callback or a custom application-specified callback can trigger | |
6082 | * connection failure or record a verification error. | |
6083 | */ | |
6084 | if (ret <= 0) | |
6085 | s->verify_result = X509_V_ERR_NO_VALID_SCTS; | |
ed29e82a RP |
6086 | return ret; |
6087 | } | |
6088 | ||
43341433 VD |
6089 | int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode) |
6090 | { | |
6091 | switch (validation_mode) { | |
6092 | default: | |
6849b73c | 6093 | ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE); |
43341433 VD |
6094 | return 0; |
6095 | case SSL_CT_VALIDATION_PERMISSIVE: | |
6096 | return SSL_CTX_set_ct_validation_callback(ctx, ct_permissive, NULL); | |
6097 | case SSL_CT_VALIDATION_STRICT: | |
6098 | return SSL_CTX_set_ct_validation_callback(ctx, ct_strict, NULL); | |
6099 | } | |
6100 | } | |
6101 | ||
6102 | int SSL_enable_ct(SSL *s, int validation_mode) | |
6103 | { | |
6104 | switch (validation_mode) { | |
6105 | default: | |
6849b73c | 6106 | ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE); |
43341433 VD |
6107 | return 0; |
6108 | case SSL_CT_VALIDATION_PERMISSIVE: | |
6109 | return SSL_set_ct_validation_callback(s, ct_permissive, NULL); | |
6110 | case SSL_CT_VALIDATION_STRICT: | |
6111 | return SSL_set_ct_validation_callback(s, ct_strict, NULL); | |
6112 | } | |
6113 | } | |
6114 | ||
ed29e82a RP |
6115 | int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx) |
6116 | { | |
328f36c5 | 6117 | return CTLOG_STORE_load_default_file(ctx->ctlog_store); |
ed29e82a RP |
6118 | } |
6119 | ||
6120 | int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path) | |
6121 | { | |
6122 | return CTLOG_STORE_load_file(ctx->ctlog_store, path); | |
6123 | } | |
6124 | ||
a230b26e | 6125 | void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE * logs) |
8359b57f RP |
6126 | { |
6127 | CTLOG_STORE_free(ctx->ctlog_store); | |
6128 | ctx->ctlog_store = logs; | |
6129 | } | |
6130 | ||
6131 | const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx) | |
6132 | { | |
6133 | return ctx->ctlog_store; | |
6134 | } | |
6135 | ||
6b1bb98f BK |
6136 | #endif /* OPENSSL_NO_CT */ |
6137 | ||
a9c0d8be DB |
6138 | void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb, |
6139 | void *arg) | |
6b1bb98f | 6140 | { |
a9c0d8be DB |
6141 | c->client_hello_cb = cb; |
6142 | c->client_hello_cb_arg = arg; | |
6b1bb98f BK |
6143 | } |
6144 | ||
a9c0d8be | 6145 | int SSL_client_hello_isv2(SSL *s) |
6b1bb98f | 6146 | { |
38b051a1 TM |
6147 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6148 | ||
6149 | if (sc == NULL) | |
6150 | return 0; | |
6151 | ||
6152 | if (sc->clienthello == NULL) | |
6b1bb98f | 6153 | return 0; |
38b051a1 | 6154 | return sc->clienthello->isv2; |
6b1bb98f BK |
6155 | } |
6156 | ||
a9c0d8be | 6157 | unsigned int SSL_client_hello_get0_legacy_version(SSL *s) |
6b1bb98f | 6158 | { |
38b051a1 TM |
6159 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6160 | ||
6161 | if (sc == NULL) | |
6162 | return 0; | |
6163 | ||
6164 | if (sc->clienthello == NULL) | |
6b1bb98f | 6165 | return 0; |
38b051a1 | 6166 | return sc->clienthello->legacy_version; |
6b1bb98f BK |
6167 | } |
6168 | ||
a9c0d8be | 6169 | size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out) |
6b1bb98f | 6170 | { |
38b051a1 TM |
6171 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6172 | ||
6173 | if (sc == NULL) | |
6174 | return 0; | |
6175 | ||
6176 | if (sc->clienthello == NULL) | |
6b1bb98f BK |
6177 | return 0; |
6178 | if (out != NULL) | |
38b051a1 | 6179 | *out = sc->clienthello->random; |
6b1bb98f BK |
6180 | return SSL3_RANDOM_SIZE; |
6181 | } | |
6182 | ||
a9c0d8be | 6183 | size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out) |
6b1bb98f | 6184 | { |
38b051a1 TM |
6185 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6186 | ||
6187 | if (sc == NULL) | |
6188 | return 0; | |
6189 | ||
6190 | if (sc->clienthello == NULL) | |
6b1bb98f BK |
6191 | return 0; |
6192 | if (out != NULL) | |
38b051a1 TM |
6193 | *out = sc->clienthello->session_id; |
6194 | return sc->clienthello->session_id_len; | |
6b1bb98f BK |
6195 | } |
6196 | ||
a9c0d8be | 6197 | size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out) |
6b1bb98f | 6198 | { |
38b051a1 TM |
6199 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6200 | ||
6201 | if (sc == NULL) | |
6202 | return 0; | |
6203 | ||
6204 | if (sc->clienthello == NULL) | |
6b1bb98f BK |
6205 | return 0; |
6206 | if (out != NULL) | |
38b051a1 TM |
6207 | *out = PACKET_data(&sc->clienthello->ciphersuites); |
6208 | return PACKET_remaining(&sc->clienthello->ciphersuites); | |
6b1bb98f BK |
6209 | } |
6210 | ||
a9c0d8be | 6211 | size_t SSL_client_hello_get0_compression_methods(SSL *s, const unsigned char **out) |
6b1bb98f | 6212 | { |
38b051a1 TM |
6213 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6214 | ||
6215 | if (sc == NULL) | |
6216 | return 0; | |
6217 | ||
6218 | if (sc->clienthello == NULL) | |
6b1bb98f BK |
6219 | return 0; |
6220 | if (out != NULL) | |
38b051a1 TM |
6221 | *out = sc->clienthello->compressions; |
6222 | return sc->clienthello->compressions_len; | |
6b1bb98f BK |
6223 | } |
6224 | ||
a9c0d8be | 6225 | int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen) |
193b5d76 BK |
6226 | { |
6227 | RAW_EXTENSION *ext; | |
6228 | int *present; | |
6229 | size_t num = 0, i; | |
38b051a1 | 6230 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
193b5d76 | 6231 | |
38b051a1 | 6232 | if (sc == NULL) |
193b5d76 | 6233 | return 0; |
38b051a1 TM |
6234 | |
6235 | if (sc->clienthello == NULL || out == NULL || outlen == NULL) | |
6236 | return 0; | |
6237 | for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) { | |
6238 | ext = sc->clienthello->pre_proc_exts + i; | |
193b5d76 BK |
6239 | if (ext->present) |
6240 | num++; | |
6241 | } | |
6fda11ae | 6242 | if (num == 0) { |
6243 | *out = NULL; | |
6244 | *outlen = 0; | |
6245 | return 1; | |
6246 | } | |
cdb10bae | 6247 | if ((present = OPENSSL_malloc(sizeof(*present) * num)) == NULL) { |
6849b73c | 6248 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
193b5d76 | 6249 | return 0; |
cdb10bae | 6250 | } |
38b051a1 TM |
6251 | for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) { |
6252 | ext = sc->clienthello->pre_proc_exts + i; | |
193b5d76 BK |
6253 | if (ext->present) { |
6254 | if (ext->received_order >= num) | |
6255 | goto err; | |
6256 | present[ext->received_order] = ext->type; | |
6257 | } | |
6258 | } | |
6259 | *out = present; | |
6260 | *outlen = num; | |
6261 | return 1; | |
6262 | err: | |
6263 | OPENSSL_free(present); | |
6264 | return 0; | |
6265 | } | |
6266 | ||
13a53fbf PL |
6267 | int SSL_client_hello_get_extension_order(SSL *s, uint16_t *exts, size_t *num_exts) |
6268 | { | |
6269 | RAW_EXTENSION *ext; | |
6270 | size_t num = 0, i; | |
38b051a1 TM |
6271 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6272 | ||
6273 | if (sc == NULL) | |
6274 | return 0; | |
13a53fbf | 6275 | |
38b051a1 | 6276 | if (sc->clienthello == NULL || num_exts == NULL) |
13a53fbf | 6277 | return 0; |
38b051a1 TM |
6278 | for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) { |
6279 | ext = sc->clienthello->pre_proc_exts + i; | |
13a53fbf PL |
6280 | if (ext->present) |
6281 | num++; | |
6282 | } | |
6283 | if (num == 0) { | |
6284 | *num_exts = 0; | |
6285 | return 1; | |
6286 | } | |
6287 | if (exts == NULL) { | |
6288 | *num_exts = num; | |
6289 | return 1; | |
6290 | } | |
6291 | if (*num_exts < num) | |
6292 | return 0; | |
38b051a1 TM |
6293 | for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) { |
6294 | ext = sc->clienthello->pre_proc_exts + i; | |
13a53fbf PL |
6295 | if (ext->present) { |
6296 | if (ext->received_order >= num) | |
6297 | return 0; | |
6298 | exts[ext->received_order] = ext->type; | |
6299 | } | |
6300 | } | |
6301 | *num_exts = num; | |
6302 | return 1; | |
6303 | } | |
6304 | ||
a9c0d8be | 6305 | int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out, |
6b1bb98f BK |
6306 | size_t *outlen) |
6307 | { | |
6308 | size_t i; | |
6309 | RAW_EXTENSION *r; | |
38b051a1 | 6310 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6b1bb98f | 6311 | |
38b051a1 | 6312 | if (sc == NULL) |
6b1bb98f | 6313 | return 0; |
38b051a1 TM |
6314 | |
6315 | if (sc->clienthello == NULL) | |
6316 | return 0; | |
6317 | for (i = 0; i < sc->clienthello->pre_proc_exts_len; ++i) { | |
6318 | r = sc->clienthello->pre_proc_exts + i; | |
6b1bb98f BK |
6319 | if (r->present && r->type == type) { |
6320 | if (out != NULL) | |
6321 | *out = PACKET_data(&r->data); | |
6322 | if (outlen != NULL) | |
6323 | *outlen = PACKET_remaining(&r->data); | |
6324 | return 1; | |
6325 | } | |
6326 | } | |
6327 | return 0; | |
6328 | } | |
2faa1b48 | 6329 | |
a58eb06d TS |
6330 | int SSL_free_buffers(SSL *ssl) |
6331 | { | |
38b051a1 TM |
6332 | RECORD_LAYER *rl; |
6333 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); | |
6334 | ||
6335 | if (sc == NULL) | |
6336 | return 0; | |
6337 | ||
6338 | rl = &sc->rlayer; | |
a58eb06d TS |
6339 | |
6340 | if (RECORD_LAYER_read_pending(rl) || RECORD_LAYER_write_pending(rl)) | |
6341 | return 0; | |
6342 | ||
6343 | RECORD_LAYER_release(rl); | |
6344 | return 1; | |
6345 | } | |
6346 | ||
6347 | int SSL_alloc_buffers(SSL *ssl) | |
6348 | { | |
38b051a1 TM |
6349 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
6350 | ||
6351 | if (sc == NULL) | |
6352 | return 0; | |
6353 | ||
6354 | return ssl3_setup_buffers(sc); | |
a58eb06d TS |
6355 | } |
6356 | ||
2faa1b48 CB |
6357 | void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb) |
6358 | { | |
6359 | ctx->keylog_callback = cb; | |
6360 | } | |
6361 | ||
6362 | SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx) | |
6363 | { | |
6364 | return ctx->keylog_callback; | |
6365 | } | |
6366 | ||
6367 | static int nss_keylog_int(const char *prefix, | |
38b051a1 | 6368 | SSL_CONNECTION *sc, |
2faa1b48 CB |
6369 | const uint8_t *parameter_1, |
6370 | size_t parameter_1_len, | |
6371 | const uint8_t *parameter_2, | |
6372 | size_t parameter_2_len) | |
6373 | { | |
6374 | char *out = NULL; | |
6375 | char *cursor = NULL; | |
6376 | size_t out_len = 0; | |
6377 | size_t i; | |
6378 | size_t prefix_len; | |
38b051a1 | 6379 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(sc); |
2faa1b48 | 6380 | |
38b051a1 | 6381 | if (sctx->keylog_callback == NULL) |
20870286 | 6382 | return 1; |
2faa1b48 CB |
6383 | |
6384 | /* | |
6385 | * Our output buffer will contain the following strings, rendered with | |
6386 | * space characters in between, terminated by a NULL character: first the | |
6387 | * prefix, then the first parameter, then the second parameter. The | |
6388 | * meaning of each parameter depends on the specific key material being | |
6389 | * logged. Note that the first and second parameters are encoded in | |
6390 | * hexadecimal, so we need a buffer that is twice their lengths. | |
6391 | */ | |
6392 | prefix_len = strlen(prefix); | |
e931f370 | 6393 | out_len = prefix_len + (2 * parameter_1_len) + (2 * parameter_2_len) + 3; |
2faa1b48 | 6394 | if ((out = cursor = OPENSSL_malloc(out_len)) == NULL) { |
38b051a1 | 6395 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); |
2faa1b48 CB |
6396 | return 0; |
6397 | } | |
6398 | ||
6399 | strcpy(cursor, prefix); | |
6400 | cursor += prefix_len; | |
6401 | *cursor++ = ' '; | |
6402 | ||
6403 | for (i = 0; i < parameter_1_len; i++) { | |
6404 | sprintf(cursor, "%02x", parameter_1[i]); | |
6405 | cursor += 2; | |
6406 | } | |
6407 | *cursor++ = ' '; | |
6408 | ||
6409 | for (i = 0; i < parameter_2_len; i++) { | |
6410 | sprintf(cursor, "%02x", parameter_2[i]); | |
6411 | cursor += 2; | |
6412 | } | |
6413 | *cursor = '\0'; | |
6414 | ||
38b051a1 | 6415 | sctx->keylog_callback(SSL_CONNECTION_GET_SSL(sc), (const char *)out); |
e931f370 | 6416 | OPENSSL_clear_free(out, out_len); |
2faa1b48 CB |
6417 | return 1; |
6418 | ||
6419 | } | |
6420 | ||
38b051a1 | 6421 | int ssl_log_rsa_client_key_exchange(SSL_CONNECTION *sc, |
2faa1b48 CB |
6422 | const uint8_t *encrypted_premaster, |
6423 | size_t encrypted_premaster_len, | |
6424 | const uint8_t *premaster, | |
6425 | size_t premaster_len) | |
6426 | { | |
6427 | if (encrypted_premaster_len < 8) { | |
38b051a1 | 6428 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2faa1b48 CB |
6429 | return 0; |
6430 | } | |
6431 | ||
f0deb4d3 | 6432 | /* We only want the first 8 bytes of the encrypted premaster as a tag. */ |
2faa1b48 | 6433 | return nss_keylog_int("RSA", |
38b051a1 | 6434 | sc, |
2faa1b48 | 6435 | encrypted_premaster, |
f0deb4d3 | 6436 | 8, |
2faa1b48 CB |
6437 | premaster, |
6438 | premaster_len); | |
6439 | } | |
6440 | ||
38b051a1 | 6441 | int ssl_log_secret(SSL_CONNECTION *sc, |
2c7bd692 CB |
6442 | const char *label, |
6443 | const uint8_t *secret, | |
6444 | size_t secret_len) | |
2faa1b48 | 6445 | { |
2c7bd692 | 6446 | return nss_keylog_int(label, |
38b051a1 TM |
6447 | sc, |
6448 | sc->s3.client_random, | |
2c7bd692 CB |
6449 | SSL3_RANDOM_SIZE, |
6450 | secret, | |
6451 | secret_len); | |
2faa1b48 CB |
6452 | } |
6453 | ||
ccb8e6e0 BK |
6454 | #define SSLV2_CIPHER_LEN 3 |
6455 | ||
38b051a1 | 6456 | int ssl_cache_cipherlist(SSL_CONNECTION *s, PACKET *cipher_suites, int sslv2format) |
ccb8e6e0 | 6457 | { |
ccb8e6e0 | 6458 | int n; |
ccb8e6e0 BK |
6459 | |
6460 | n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN; | |
6461 | ||
6462 | if (PACKET_remaining(cipher_suites) == 0) { | |
c48ffbcc | 6463 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED); |
90134d98 | 6464 | return 0; |
ccb8e6e0 BK |
6465 | } |
6466 | ||
6467 | if (PACKET_remaining(cipher_suites) % n != 0) { | |
c48ffbcc | 6468 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
90134d98 | 6469 | return 0; |
ccb8e6e0 BK |
6470 | } |
6471 | ||
555cbb32 TS |
6472 | OPENSSL_free(s->s3.tmp.ciphers_raw); |
6473 | s->s3.tmp.ciphers_raw = NULL; | |
6474 | s->s3.tmp.ciphers_rawlen = 0; | |
ccb8e6e0 BK |
6475 | |
6476 | if (sslv2format) { | |
6477 | size_t numciphers = PACKET_remaining(cipher_suites) / n; | |
6478 | PACKET sslv2ciphers = *cipher_suites; | |
6479 | unsigned int leadbyte; | |
6480 | unsigned char *raw; | |
6481 | ||
6482 | /* | |
6483 | * We store the raw ciphers list in SSLv3+ format so we need to do some | |
6484 | * preprocessing to convert the list first. If there are any SSLv2 only | |
6485 | * ciphersuites with a non-zero leading byte then we are going to | |
6486 | * slightly over allocate because we won't store those. But that isn't a | |
6487 | * problem. | |
6488 | */ | |
6489 | raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN); | |
555cbb32 | 6490 | s->s3.tmp.ciphers_raw = raw; |
ccb8e6e0 | 6491 | if (raw == NULL) { |
c48ffbcc | 6492 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); |
f63a17d6 | 6493 | return 0; |
ccb8e6e0 | 6494 | } |
555cbb32 | 6495 | for (s->s3.tmp.ciphers_rawlen = 0; |
ccb8e6e0 BK |
6496 | PACKET_remaining(&sslv2ciphers) > 0; |
6497 | raw += TLS_CIPHER_LEN) { | |
6498 | if (!PACKET_get_1(&sslv2ciphers, &leadbyte) | |
6499 | || (leadbyte == 0 | |
6500 | && !PACKET_copy_bytes(&sslv2ciphers, raw, | |
6501 | TLS_CIPHER_LEN)) | |
6502 | || (leadbyte != 0 | |
6503 | && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) { | |
c48ffbcc | 6504 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET); |
555cbb32 TS |
6505 | OPENSSL_free(s->s3.tmp.ciphers_raw); |
6506 | s->s3.tmp.ciphers_raw = NULL; | |
6507 | s->s3.tmp.ciphers_rawlen = 0; | |
f63a17d6 | 6508 | return 0; |
ccb8e6e0 BK |
6509 | } |
6510 | if (leadbyte == 0) | |
555cbb32 | 6511 | s->s3.tmp.ciphers_rawlen += TLS_CIPHER_LEN; |
ccb8e6e0 | 6512 | } |
555cbb32 TS |
6513 | } else if (!PACKET_memdup(cipher_suites, &s->s3.tmp.ciphers_raw, |
6514 | &s->s3.tmp.ciphers_rawlen)) { | |
c48ffbcc | 6515 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 6516 | return 0; |
ccb8e6e0 | 6517 | } |
90134d98 | 6518 | return 1; |
90134d98 BK |
6519 | } |
6520 | ||
6521 | int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len, | |
6522 | int isv2format, STACK_OF(SSL_CIPHER) **sk, | |
6523 | STACK_OF(SSL_CIPHER) **scsvs) | |
6524 | { | |
90134d98 | 6525 | PACKET pkt; |
38b051a1 TM |
6526 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6527 | ||
6528 | if (sc == NULL) | |
6529 | return 0; | |
90134d98 BK |
6530 | |
6531 | if (!PACKET_buf_init(&pkt, bytes, len)) | |
6532 | return 0; | |
38b051a1 | 6533 | return ossl_bytes_to_cipher_list(sc, &pkt, sk, scsvs, isv2format, 0); |
90134d98 BK |
6534 | } |
6535 | ||
38b051a1 TM |
6536 | int ossl_bytes_to_cipher_list(SSL_CONNECTION *s, PACKET *cipher_suites, |
6537 | STACK_OF(SSL_CIPHER) **skp, | |
6538 | STACK_OF(SSL_CIPHER) **scsvs_out, | |
6539 | int sslv2format, int fatal) | |
90134d98 BK |
6540 | { |
6541 | const SSL_CIPHER *c; | |
6542 | STACK_OF(SSL_CIPHER) *sk = NULL; | |
6543 | STACK_OF(SSL_CIPHER) *scsvs = NULL; | |
6544 | int n; | |
6545 | /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */ | |
6546 | unsigned char cipher[SSLV2_CIPHER_LEN]; | |
6547 | ||
6548 | n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN; | |
6549 | ||
6550 | if (PACKET_remaining(cipher_suites) == 0) { | |
f63a17d6 | 6551 | if (fatal) |
c48ffbcc | 6552 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED); |
f63a17d6 | 6553 | else |
6849b73c | 6554 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHERS_SPECIFIED); |
90134d98 BK |
6555 | return 0; |
6556 | } | |
6557 | ||
6558 | if (PACKET_remaining(cipher_suites) % n != 0) { | |
f63a17d6 | 6559 | if (fatal) |
c48ffbcc | 6560 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
f63a17d6 MC |
6561 | SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
6562 | else | |
6849b73c | 6563 | ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
90134d98 BK |
6564 | return 0; |
6565 | } | |
6566 | ||
6567 | sk = sk_SSL_CIPHER_new_null(); | |
6568 | scsvs = sk_SSL_CIPHER_new_null(); | |
6569 | if (sk == NULL || scsvs == NULL) { | |
f63a17d6 | 6570 | if (fatal) |
c48ffbcc | 6571 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); |
f63a17d6 | 6572 | else |
6849b73c | 6573 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
90134d98 BK |
6574 | goto err; |
6575 | } | |
ccb8e6e0 BK |
6576 | |
6577 | while (PACKET_copy_bytes(cipher_suites, cipher, n)) { | |
6578 | /* | |
6579 | * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the | |
6580 | * first byte set to zero, while true SSLv2 ciphers have a non-zero | |
6581 | * first byte. We don't support any true SSLv2 ciphers, so skip them. | |
6582 | */ | |
6583 | if (sslv2format && cipher[0] != '\0') | |
6584 | continue; | |
6585 | ||
ccb8e6e0 BK |
6586 | /* For SSLv2-compat, ignore leading 0-byte. */ |
6587 | c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher, 1); | |
6588 | if (c != NULL) { | |
90134d98 BK |
6589 | if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) || |
6590 | (!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) { | |
f63a17d6 | 6591 | if (fatal) |
c48ffbcc | 6592 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); |
f63a17d6 | 6593 | else |
6849b73c | 6594 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
ccb8e6e0 BK |
6595 | goto err; |
6596 | } | |
6597 | } | |
6598 | } | |
6599 | if (PACKET_remaining(cipher_suites) > 0) { | |
f63a17d6 | 6600 | if (fatal) |
c48ffbcc | 6601 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
f63a17d6 | 6602 | else |
6849b73c | 6603 | ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH); |
ccb8e6e0 BK |
6604 | goto err; |
6605 | } | |
6606 | ||
90134d98 BK |
6607 | if (skp != NULL) |
6608 | *skp = sk; | |
6609 | else | |
6610 | sk_SSL_CIPHER_free(sk); | |
6611 | if (scsvs_out != NULL) | |
6612 | *scsvs_out = scsvs; | |
6613 | else | |
6614 | sk_SSL_CIPHER_free(scsvs); | |
6615 | return 1; | |
ccb8e6e0 BK |
6616 | err: |
6617 | sk_SSL_CIPHER_free(sk); | |
90134d98 BK |
6618 | sk_SSL_CIPHER_free(scsvs); |
6619 | return 0; | |
ccb8e6e0 | 6620 | } |
3fc8d856 MC |
6621 | |
6622 | int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data) | |
6623 | { | |
6624 | ctx->max_early_data = max_early_data; | |
6625 | ||
6626 | return 1; | |
6627 | } | |
6628 | ||
46dcb945 | 6629 | uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx) |
3fc8d856 MC |
6630 | { |
6631 | return ctx->max_early_data; | |
6632 | } | |
6633 | ||
6634 | int SSL_set_max_early_data(SSL *s, uint32_t max_early_data) | |
6635 | { | |
38b051a1 TM |
6636 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6637 | ||
6638 | if (sc == NULL) | |
6639 | return 0; | |
6640 | ||
6641 | sc->max_early_data = max_early_data; | |
3fc8d856 MC |
6642 | |
6643 | return 1; | |
6644 | } | |
6645 | ||
a8e75d56 | 6646 | uint32_t SSL_get_max_early_data(const SSL *s) |
3fc8d856 | 6647 | { |
38b051a1 TM |
6648 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
6649 | ||
6650 | if (sc == NULL) | |
6651 | return 0; | |
6652 | ||
6653 | return sc->max_early_data; | |
3fc8d856 | 6654 | } |
ae3947de | 6655 | |
4e8548e8 MC |
6656 | int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data) |
6657 | { | |
6658 | ctx->recv_max_early_data = recv_max_early_data; | |
6659 | ||
6660 | return 1; | |
6661 | } | |
6662 | ||
6663 | uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx) | |
6664 | { | |
6665 | return ctx->recv_max_early_data; | |
6666 | } | |
6667 | ||
6668 | int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data) | |
6669 | { | |
38b051a1 TM |
6670 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6671 | ||
6672 | if (sc == NULL) | |
6673 | return 0; | |
6674 | ||
6675 | sc->recv_max_early_data = recv_max_early_data; | |
4e8548e8 MC |
6676 | |
6677 | return 1; | |
6678 | } | |
6679 | ||
6680 | uint32_t SSL_get_recv_max_early_data(const SSL *s) | |
6681 | { | |
38b051a1 TM |
6682 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
6683 | ||
6684 | if (sc == NULL) | |
6685 | return 0; | |
6686 | ||
6687 | return sc->recv_max_early_data; | |
4e8548e8 MC |
6688 | } |
6689 | ||
38b051a1 | 6690 | __owur unsigned int ssl_get_max_send_fragment(const SSL_CONNECTION *sc) |
cf72c757 F |
6691 | { |
6692 | /* Return any active Max Fragment Len extension */ | |
38b051a1 TM |
6693 | if (sc->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(sc->session)) |
6694 | return GET_MAX_FRAGMENT_LENGTH(sc->session); | |
cf72c757 F |
6695 | |
6696 | /* return current SSL connection setting */ | |
38b051a1 | 6697 | return sc->max_send_fragment; |
cf72c757 F |
6698 | } |
6699 | ||
38b051a1 | 6700 | __owur unsigned int ssl_get_split_send_fragment(const SSL_CONNECTION *sc) |
cf72c757 F |
6701 | { |
6702 | /* Return a value regarding an active Max Fragment Len extension */ | |
38b051a1 TM |
6703 | if (sc->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(sc->session) |
6704 | && sc->split_send_fragment > GET_MAX_FRAGMENT_LENGTH(sc->session)) | |
6705 | return GET_MAX_FRAGMENT_LENGTH(sc->session); | |
cf72c757 F |
6706 | |
6707 | /* else limit |split_send_fragment| to current |max_send_fragment| */ | |
38b051a1 TM |
6708 | if (sc->split_send_fragment > sc->max_send_fragment) |
6709 | return sc->max_send_fragment; | |
cf72c757 F |
6710 | |
6711 | /* return current SSL connection setting */ | |
38b051a1 | 6712 | return sc->split_send_fragment; |
cf72c757 | 6713 | } |
042c5753 MC |
6714 | |
6715 | int SSL_stateless(SSL *s) | |
6716 | { | |
6717 | int ret; | |
38b051a1 TM |
6718 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6719 | ||
6720 | /* TODO(QUIC): This will need further work. */ | |
6721 | if (sc == NULL) | |
6722 | return 0; | |
042c5753 MC |
6723 | |
6724 | /* Ensure there is no state left over from a previous invocation */ | |
6725 | if (!SSL_clear(s)) | |
d6bb50a5 | 6726 | return 0; |
042c5753 MC |
6727 | |
6728 | ERR_clear_error(); | |
6729 | ||
38b051a1 | 6730 | sc->s3.flags |= TLS1_FLAGS_STATELESS; |
042c5753 | 6731 | ret = SSL_accept(s); |
38b051a1 | 6732 | sc->s3.flags &= ~TLS1_FLAGS_STATELESS; |
042c5753 | 6733 | |
38b051a1 | 6734 | if (ret > 0 && sc->ext.cookieok) |
c36001c3 MC |
6735 | return 1; |
6736 | ||
38b051a1 | 6737 | if (sc->hello_retry_request == SSL_HRR_PENDING && !ossl_statem_in_error(sc)) |
e440f513 MC |
6738 | return 0; |
6739 | ||
6740 | return -1; | |
042c5753 | 6741 | } |
9d75dce3 | 6742 | |
e97be718 MC |
6743 | void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val) |
6744 | { | |
6745 | ctx->pha_enabled = val; | |
6746 | } | |
6747 | ||
32097b33 | 6748 | void SSL_set_post_handshake_auth(SSL *ssl, int val) |
9d75dce3 | 6749 | { |
38b051a1 TM |
6750 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
6751 | ||
6752 | if (sc == NULL) | |
6753 | return; | |
6754 | ||
6755 | sc->pha_enabled = val; | |
9d75dce3 TS |
6756 | } |
6757 | ||
6758 | int SSL_verify_client_post_handshake(SSL *ssl) | |
6759 | { | |
38b051a1 TM |
6760 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
6761 | ||
6762 | if (sc == NULL) | |
6763 | return 0; | |
6764 | ||
6765 | if (!SSL_CONNECTION_IS_TLS13(sc)) { | |
6849b73c | 6766 | ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION); |
9d75dce3 TS |
6767 | return 0; |
6768 | } | |
38b051a1 | 6769 | if (!sc->server) { |
6849b73c | 6770 | ERR_raise(ERR_LIB_SSL, SSL_R_NOT_SERVER); |
9d75dce3 TS |
6771 | return 0; |
6772 | } | |
6773 | ||
6774 | if (!SSL_is_init_finished(ssl)) { | |
6849b73c | 6775 | ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT); |
9d75dce3 TS |
6776 | return 0; |
6777 | } | |
6778 | ||
38b051a1 | 6779 | switch (sc->post_handshake_auth) { |
9d75dce3 | 6780 | case SSL_PHA_NONE: |
6849b73c | 6781 | ERR_raise(ERR_LIB_SSL, SSL_R_EXTENSION_NOT_RECEIVED); |
9d75dce3 TS |
6782 | return 0; |
6783 | default: | |
6784 | case SSL_PHA_EXT_SENT: | |
6849b73c | 6785 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
9d75dce3 TS |
6786 | return 0; |
6787 | case SSL_PHA_EXT_RECEIVED: | |
6788 | break; | |
6789 | case SSL_PHA_REQUEST_PENDING: | |
6849b73c | 6790 | ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_PENDING); |
9d75dce3 TS |
6791 | return 0; |
6792 | case SSL_PHA_REQUESTED: | |
6849b73c | 6793 | ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_SENT); |
9d75dce3 TS |
6794 | return 0; |
6795 | } | |
6796 | ||
38b051a1 | 6797 | sc->post_handshake_auth = SSL_PHA_REQUEST_PENDING; |
9d75dce3 TS |
6798 | |
6799 | /* checks verify_mode and algorithm_auth */ | |
38b051a1 TM |
6800 | if (!send_certificate_request(sc)) { |
6801 | sc->post_handshake_auth = SSL_PHA_EXT_RECEIVED; /* restore on error */ | |
6849b73c | 6802 | ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CONFIG); |
9d75dce3 TS |
6803 | return 0; |
6804 | } | |
6805 | ||
38b051a1 | 6806 | ossl_statem_set_in_init(sc, 1); |
9d75dce3 TS |
6807 | return 1; |
6808 | } | |
df0fed9a TS |
6809 | |
6810 | int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx, | |
6811 | SSL_CTX_generate_session_ticket_fn gen_cb, | |
6812 | SSL_CTX_decrypt_session_ticket_fn dec_cb, | |
6813 | void *arg) | |
6814 | { | |
6815 | ctx->generate_ticket_cb = gen_cb; | |
6816 | ctx->decrypt_ticket_cb = dec_cb; | |
6817 | ctx->ticket_cb_data = arg; | |
6818 | return 1; | |
6819 | } | |
c9598459 MC |
6820 | |
6821 | void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx, | |
6822 | SSL_allow_early_data_cb_fn cb, | |
6823 | void *arg) | |
6824 | { | |
6825 | ctx->allow_early_data_cb = cb; | |
6826 | ctx->allow_early_data_cb_data = arg; | |
6827 | } | |
6828 | ||
6829 | void SSL_set_allow_early_data_cb(SSL *s, | |
6830 | SSL_allow_early_data_cb_fn cb, | |
6831 | void *arg) | |
6832 | { | |
38b051a1 TM |
6833 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6834 | ||
6835 | if (sc == NULL) | |
6836 | return; | |
6837 | ||
6838 | sc->allow_early_data_cb = cb; | |
6839 | sc->allow_early_data_cb_data = arg; | |
c9598459 | 6840 | } |
c8f6c28a | 6841 | |
b4250010 | 6842 | const EVP_CIPHER *ssl_evp_cipher_fetch(OSSL_LIB_CTX *libctx, |
c8f6c28a MC |
6843 | int nid, |
6844 | const char *properties) | |
6845 | { | |
301fcb28 | 6846 | const EVP_CIPHER *ciph; |
5fcb97c6 | 6847 | |
301fcb28 MC |
6848 | ciph = tls_get_cipher_from_engine(nid); |
6849 | if (ciph != NULL) | |
6850 | return ciph; | |
0618b62c | 6851 | |
c8f6c28a | 6852 | /* |
301fcb28 MC |
6853 | * If there is no engine cipher then we do an explicit fetch. This may fail |
6854 | * and that could be ok | |
c8f6c28a | 6855 | */ |
5fcb97c6 MC |
6856 | ERR_set_mark(); |
6857 | ciph = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(nid), properties); | |
6858 | ERR_pop_to_mark(); | |
6859 | return ciph; | |
c8f6c28a MC |
6860 | } |
6861 | ||
6862 | ||
6863 | int ssl_evp_cipher_up_ref(const EVP_CIPHER *cipher) | |
6864 | { | |
6865 | /* Don't up-ref an implicit EVP_CIPHER */ | |
ed576acd | 6866 | if (EVP_CIPHER_get0_provider(cipher) == NULL) |
c8f6c28a MC |
6867 | return 1; |
6868 | ||
6869 | /* | |
6870 | * The cipher was explicitly fetched and therefore it is safe to cast | |
6871 | * away the const | |
6872 | */ | |
6873 | return EVP_CIPHER_up_ref((EVP_CIPHER *)cipher); | |
6874 | } | |
6875 | ||
6876 | void ssl_evp_cipher_free(const EVP_CIPHER *cipher) | |
6877 | { | |
6878 | if (cipher == NULL) | |
6879 | return; | |
6880 | ||
ed576acd | 6881 | if (EVP_CIPHER_get0_provider(cipher) != NULL) { |
c8f6c28a MC |
6882 | /* |
6883 | * The cipher was explicitly fetched and therefore it is safe to cast | |
6884 | * away the const | |
6885 | */ | |
6886 | EVP_CIPHER_free((EVP_CIPHER *)cipher); | |
6887 | } | |
6888 | } | |
6889 | ||
b4250010 | 6890 | const EVP_MD *ssl_evp_md_fetch(OSSL_LIB_CTX *libctx, |
c8f6c28a MC |
6891 | int nid, |
6892 | const char *properties) | |
6893 | { | |
301fcb28 | 6894 | const EVP_MD *md; |
5fcb97c6 | 6895 | |
301fcb28 MC |
6896 | md = tls_get_digest_from_engine(nid); |
6897 | if (md != NULL) | |
6898 | return md; | |
c8f6c28a MC |
6899 | |
6900 | /* Otherwise we do an explicit fetch */ | |
5fcb97c6 MC |
6901 | ERR_set_mark(); |
6902 | md = EVP_MD_fetch(libctx, OBJ_nid2sn(nid), properties); | |
6903 | ERR_pop_to_mark(); | |
6904 | return md; | |
c8f6c28a MC |
6905 | } |
6906 | ||
6907 | int ssl_evp_md_up_ref(const EVP_MD *md) | |
6908 | { | |
6909 | /* Don't up-ref an implicit EVP_MD */ | |
ed576acd | 6910 | if (EVP_MD_get0_provider(md) == NULL) |
c8f6c28a MC |
6911 | return 1; |
6912 | ||
6913 | /* | |
6914 | * The digest was explicitly fetched and therefore it is safe to cast | |
6915 | * away the const | |
6916 | */ | |
6917 | return EVP_MD_up_ref((EVP_MD *)md); | |
6918 | } | |
6919 | ||
6920 | void ssl_evp_md_free(const EVP_MD *md) | |
6921 | { | |
6922 | if (md == NULL) | |
6923 | return; | |
6924 | ||
ed576acd | 6925 | if (EVP_MD_get0_provider(md) != NULL) { |
c8f6c28a MC |
6926 | /* |
6927 | * The digest was explicitly fetched and therefore it is safe to cast | |
6928 | * away the const | |
6929 | */ | |
6930 | EVP_MD_free((EVP_MD *)md); | |
6931 | } | |
6932 | } | |
163f6dc1 MC |
6933 | |
6934 | int SSL_set0_tmp_dh_pkey(SSL *s, EVP_PKEY *dhpkey) | |
6935 | { | |
38b051a1 TM |
6936 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
6937 | ||
6938 | if (sc == NULL) | |
6939 | return 0; | |
6940 | ||
6941 | if (!ssl_security(sc, SSL_SECOP_TMP_DH, | |
ed576acd | 6942 | EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) { |
276d6c68 | 6943 | ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL); |
163f6dc1 MC |
6944 | return 0; |
6945 | } | |
38b051a1 TM |
6946 | EVP_PKEY_free(sc->cert->dh_tmp); |
6947 | sc->cert->dh_tmp = dhpkey; | |
163f6dc1 MC |
6948 | return 1; |
6949 | } | |
6950 | ||
6951 | int SSL_CTX_set0_tmp_dh_pkey(SSL_CTX *ctx, EVP_PKEY *dhpkey) | |
6952 | { | |
6953 | if (!ssl_ctx_security(ctx, SSL_SECOP_TMP_DH, | |
ed576acd | 6954 | EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) { |
276d6c68 | 6955 | ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL); |
163f6dc1 MC |
6956 | return 0; |
6957 | } | |
6958 | EVP_PKEY_free(ctx->cert->dh_tmp); | |
6959 | ctx->cert->dh_tmp = dhpkey; | |
6960 | return 1; | |
6961 | } |