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