]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * ! \file ssl/ssl_lib.c \brief Version independent SSL functions. | |
4f43d0e7 | 3 | */ |
58964a49 | 4 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
5 | * All rights reserved. |
6 | * | |
7 | * This package is an SSL implementation written | |
8 | * by Eric Young (eay@cryptsoft.com). | |
9 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 10 | * |
d02b48c6 RE |
11 | * This library is free for commercial and non-commercial use as long as |
12 | * the following conditions are aheared to. The following conditions | |
13 | * apply to all code found in this distribution, be it the RC4, RSA, | |
14 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
15 | * included with this distribution is covered by the same copyright terms | |
16 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 17 | * |
d02b48c6 RE |
18 | * Copyright remains Eric Young's, and as such any Copyright notices in |
19 | * the code are not to be removed. | |
20 | * If this package is used in a product, Eric Young should be given attribution | |
21 | * as the author of the parts of the library used. | |
22 | * This can be in the form of a textual message at program startup or | |
23 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 24 | * |
d02b48c6 RE |
25 | * Redistribution and use in source and binary forms, with or without |
26 | * modification, are permitted provided that the following conditions | |
27 | * are met: | |
28 | * 1. Redistributions of source code must retain the copyright | |
29 | * notice, this list of conditions and the following disclaimer. | |
30 | * 2. Redistributions in binary form must reproduce the above copyright | |
31 | * notice, this list of conditions and the following disclaimer in the | |
32 | * documentation and/or other materials provided with the distribution. | |
33 | * 3. All advertising materials mentioning features or use of this software | |
34 | * must display the following acknowledgement: | |
35 | * "This product includes cryptographic software written by | |
36 | * Eric Young (eay@cryptsoft.com)" | |
37 | * The word 'cryptographic' can be left out if the rouines from the library | |
38 | * being used are not cryptographic related :-). | |
0f113f3e | 39 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
40 | * the apps directory (application code) you must include an acknowledgement: |
41 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 42 | * |
d02b48c6 RE |
43 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
44 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
45 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
47 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
49 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
51 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
52 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
53 | * SUCH DAMAGE. | |
0f113f3e | 54 | * |
d02b48c6 RE |
55 | * The licence and distribution terms for any publically available version or |
56 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
57 | * copied and put under another distribution licence | |
58 | * [including the GNU Public Licence.] | |
59 | */ | |
bf21446a | 60 | /* ==================================================================== |
52b8dad8 | 61 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
bf21446a BM |
62 | * |
63 | * Redistribution and use in source and binary forms, with or without | |
64 | * modification, are permitted provided that the following conditions | |
65 | * are met: | |
66 | * | |
67 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 68 | * notice, this list of conditions and the following disclaimer. |
bf21446a BM |
69 | * |
70 | * 2. Redistributions in binary form must reproduce the above copyright | |
71 | * notice, this list of conditions and the following disclaimer in | |
72 | * the documentation and/or other materials provided with the | |
73 | * distribution. | |
74 | * | |
75 | * 3. All advertising materials mentioning features or use of this | |
76 | * software must display the following acknowledgment: | |
77 | * "This product includes software developed by the OpenSSL Project | |
78 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
79 | * | |
80 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
81 | * endorse or promote products derived from this software without | |
82 | * prior written permission. For written permission, please contact | |
83 | * openssl-core@openssl.org. | |
84 | * | |
85 | * 5. Products derived from this software may not be called "OpenSSL" | |
86 | * nor may "OpenSSL" appear in their names without prior written | |
87 | * permission of the OpenSSL Project. | |
88 | * | |
89 | * 6. Redistributions of any form whatsoever must retain the following | |
90 | * acknowledgment: | |
91 | * "This product includes software developed by the OpenSSL Project | |
92 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
93 | * | |
94 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
95 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
96 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
97 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
98 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
99 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
100 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
101 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
102 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
103 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
104 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
105 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
106 | * ==================================================================== | |
107 | * | |
108 | * This product includes cryptographic software written by Eric Young | |
109 | * (eay@cryptsoft.com). This product includes software written by Tim | |
110 | * Hudson (tjh@cryptsoft.com). | |
111 | * | |
112 | */ | |
ea262260 BM |
113 | /* ==================================================================== |
114 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
0f113f3e | 115 | * ECC cipher suite support in OpenSSL originally developed by |
ea262260 BM |
116 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
117 | */ | |
ddac1974 NL |
118 | /* ==================================================================== |
119 | * Copyright 2005 Nokia. All rights reserved. | |
120 | * | |
121 | * The portions of the attached software ("Contribution") is developed by | |
122 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source | |
123 | * license. | |
124 | * | |
125 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of | |
126 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites | |
127 | * support (see RFC 4279) to OpenSSL. | |
128 | * | |
129 | * No patent licenses or other rights except those expressly stated in | |
130 | * the OpenSSL open source license shall be deemed granted or received | |
131 | * expressly, by implication, estoppel, or otherwise. | |
132 | * | |
133 | * No assurances are provided by Nokia that the Contribution does not | |
134 | * infringe the patent or other intellectual property rights of any third | |
135 | * party or that the license provides you with all the necessary rights | |
136 | * to make use of the Contribution. | |
137 | * | |
138 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN | |
139 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA | |
140 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY | |
141 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR | |
142 | * OTHERWISE. | |
143 | */ | |
bbb8de09 | 144 | |
027e257b | 145 | #ifdef REF_CHECK |
0f113f3e | 146 | # include <assert.h> |
027e257b | 147 | #endif |
d02b48c6 | 148 | #include <stdio.h> |
7b63c0fa LJ |
149 | #include "ssl_locl.h" |
150 | #include "kssl_lcl.h" | |
ec577822 BM |
151 | #include <openssl/objects.h> |
152 | #include <openssl/lhash.h> | |
bb7cd4e3 | 153 | #include <openssl/x509v3.h> |
6434abbf | 154 | #include <openssl/rand.h> |
67c8e7f4 | 155 | #include <openssl/ocsp.h> |
3eeaab4b | 156 | #ifndef OPENSSL_NO_DH |
0f113f3e | 157 | # include <openssl/dh.h> |
3eeaab4b | 158 | #endif |
bdfe932d | 159 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
160 | # include <openssl/engine.h> |
161 | #endif | |
162 | ||
163 | const char *SSL_version_str = OPENSSL_VERSION_TEXT; | |
164 | ||
165 | SSL3_ENC_METHOD ssl3_undef_enc_method = { | |
166 | /* | |
167 | * evil casts, but these functions are only called if there's a library | |
168 | * bug | |
169 | */ | |
170 | (int (*)(SSL *, int))ssl_undefined_function, | |
171 | (int (*)(SSL *, unsigned char *, int))ssl_undefined_function, | |
172 | ssl_undefined_function, | |
173 | (int (*)(SSL *, unsigned char *, unsigned char *, int)) | |
174 | ssl_undefined_function, | |
175 | (int (*)(SSL *, int))ssl_undefined_function, | |
176 | (int (*)(SSL *, const char *, int, unsigned char *)) | |
177 | ssl_undefined_function, | |
178 | 0, /* finish_mac_length */ | |
179 | (int (*)(SSL *, int, unsigned char *))ssl_undefined_function, | |
180 | NULL, /* client_finished_label */ | |
181 | 0, /* client_finished_label_len */ | |
182 | NULL, /* server_finished_label */ | |
183 | 0, /* server_finished_label_len */ | |
184 | (int (*)(int))ssl_undefined_function, | |
185 | (int (*)(SSL *, unsigned char *, size_t, const char *, | |
186 | size_t, const unsigned char *, size_t, | |
187 | int use_context))ssl_undefined_function, | |
188 | }; | |
d02b48c6 | 189 | |
4f43d0e7 | 190 | int SSL_clear(SSL *s) |
0f113f3e | 191 | { |
d02b48c6 | 192 | |
0f113f3e MC |
193 | if (s->method == NULL) { |
194 | SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED); | |
195 | return (0); | |
196 | } | |
d02b48c6 | 197 | |
0f113f3e MC |
198 | if (ssl_clear_bad_session(s)) { |
199 | SSL_SESSION_free(s->session); | |
200 | s->session = NULL; | |
201 | } | |
d62bfb39 | 202 | |
0f113f3e MC |
203 | s->error = 0; |
204 | s->hit = 0; | |
205 | s->shutdown = 0; | |
d02b48c6 | 206 | |
dbd87ffc MC |
207 | #if 0 |
208 | /* | |
209 | * Disabled since version 1.10 of this file (early return not | |
210 | * needed because SSL_clear is not called when doing renegotiation) | |
211 | */ | |
212 | /* | |
213 | * This is set if we are doing dynamic renegotiation so keep | |
214 | * the old cipher. It is sort of a SSL_clear_lite :-) | |
215 | */ | |
0f113f3e MC |
216 | if (s->renegotiate) |
217 | return (1); | |
a2a01589 | 218 | #else |
0f113f3e MC |
219 | if (s->renegotiate) { |
220 | SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR); | |
221 | return 0; | |
222 | } | |
413c4f45 | 223 | #endif |
d02b48c6 | 224 | |
0f113f3e | 225 | s->type = 0; |
d02b48c6 | 226 | |
0f113f3e | 227 | s->state = SSL_ST_BEFORE | ((s->server) ? SSL_ST_ACCEPT : SSL_ST_CONNECT); |
413c4f45 | 228 | |
0f113f3e MC |
229 | s->version = s->method->version; |
230 | s->client_version = s->version; | |
231 | s->rwstate = SSL_NOTHING; | |
232 | s->rstate = SSL_ST_READ_HEADER; | |
544ebbce | 233 | #if 0 |
0f113f3e | 234 | s->read_ahead = s->ctx->read_ahead; |
544ebbce | 235 | #endif |
d02b48c6 | 236 | |
0f113f3e MC |
237 | if (s->init_buf != NULL) { |
238 | BUF_MEM_free(s->init_buf); | |
239 | s->init_buf = NULL; | |
240 | } | |
d02b48c6 | 241 | |
0f113f3e MC |
242 | ssl_clear_cipher_ctx(s); |
243 | ssl_clear_hash_ctx(&s->read_hash); | |
244 | ssl_clear_hash_ctx(&s->write_hash); | |
d02b48c6 | 245 | |
0f113f3e | 246 | s->first_packet = 0; |
d02b48c6 | 247 | |
0f113f3e MC |
248 | /* |
249 | * Check to see if we were changed into a different method, if so, revert | |
250 | * back if we are not doing session-id reuse. | |
251 | */ | |
252 | if (!s->in_handshake && (s->session == NULL) | |
253 | && (s->method != s->ctx->method)) { | |
254 | s->method->ssl_free(s); | |
255 | s->method = s->ctx->method; | |
256 | if (!s->method->ssl_new(s)) | |
257 | return (0); | |
258 | } else | |
0f113f3e MC |
259 | s->method->ssl_clear(s); |
260 | return (1); | |
261 | } | |
d02b48c6 | 262 | |
4f43d0e7 | 263 | /** Used to change an SSL_CTXs default SSL method type */ |
0f113f3e MC |
264 | int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth) |
265 | { | |
266 | STACK_OF(SSL_CIPHER) *sk; | |
267 | ||
268 | ctx->method = meth; | |
269 | ||
270 | sk = ssl_create_cipher_list(ctx->method, &(ctx->cipher_list), | |
271 | &(ctx->cipher_list_by_id), | |
272 | SSL_DEFAULT_CIPHER_LIST, ctx->cert); | |
273 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) { | |
274 | SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION, | |
275 | SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS); | |
276 | return (0); | |
277 | } | |
278 | return (1); | |
279 | } | |
d02b48c6 | 280 | |
4f43d0e7 | 281 | SSL *SSL_new(SSL_CTX *ctx) |
0f113f3e MC |
282 | { |
283 | SSL *s; | |
284 | ||
285 | if (ctx == NULL) { | |
286 | SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX); | |
287 | return (NULL); | |
288 | } | |
289 | if (ctx->method == NULL) { | |
290 | SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION); | |
291 | return (NULL); | |
292 | } | |
293 | ||
294 | s = (SSL *)OPENSSL_malloc(sizeof(SSL)); | |
295 | if (s == NULL) | |
296 | goto err; | |
297 | memset(s, 0, sizeof(SSL)); | |
298 | ||
299 | #ifndef OPENSSL_NO_KRB5 | |
300 | s->kssl_ctx = kssl_ctx_new(); | |
301 | #endif /* OPENSSL_NO_KRB5 */ | |
302 | ||
303 | s->options = ctx->options; | |
304 | s->mode = ctx->mode; | |
305 | s->max_cert_list = ctx->max_cert_list; | |
306 | ||
307 | if (ctx->cert != NULL) { | |
308 | /* | |
309 | * Earlier library versions used to copy the pointer to the CERT, not | |
310 | * its contents; only when setting new parameters for the per-SSL | |
311 | * copy, ssl_cert_new would be called (and the direct reference to | |
312 | * the per-SSL_CTX settings would be lost, but those still were | |
313 | * indirectly accessed for various purposes, and for that reason they | |
314 | * used to be known as s->ctx->default_cert). Now we don't look at the | |
315 | * SSL_CTX's CERT after having duplicated it once. | |
316 | */ | |
317 | ||
318 | s->cert = ssl_cert_dup(ctx->cert); | |
319 | if (s->cert == NULL) | |
320 | goto err; | |
321 | } else | |
322 | s->cert = NULL; /* Cannot really happen (see SSL_CTX_new) */ | |
323 | ||
324 | s->read_ahead = ctx->read_ahead; | |
325 | s->msg_callback = ctx->msg_callback; | |
326 | s->msg_callback_arg = ctx->msg_callback_arg; | |
327 | s->verify_mode = ctx->verify_mode; | |
328 | s->not_resumable_session_cb = ctx->not_resumable_session_cb; | |
5d7c222d | 329 | #if 0 |
0f113f3e MC |
330 | s->verify_depth = ctx->verify_depth; |
331 | #endif | |
332 | s->sid_ctx_length = ctx->sid_ctx_length; | |
333 | OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx); | |
334 | memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx)); | |
335 | s->verify_callback = ctx->default_verify_callback; | |
336 | s->generate_session_id = ctx->generate_session_id; | |
337 | ||
338 | s->param = X509_VERIFY_PARAM_new(); | |
339 | if (!s->param) | |
340 | goto err; | |
341 | X509_VERIFY_PARAM_inherit(s->param, ctx->param); | |
5d7c222d | 342 | #if 0 |
0f113f3e MC |
343 | s->purpose = ctx->purpose; |
344 | s->trust = ctx->trust; | |
5d7c222d | 345 | #endif |
0f113f3e MC |
346 | s->quiet_shutdown = ctx->quiet_shutdown; |
347 | s->max_send_fragment = ctx->max_send_fragment; | |
bf21446a | 348 | |
0f113f3e MC |
349 | CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); |
350 | s->ctx = ctx; | |
1aeb3da8 | 351 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e MC |
352 | s->tlsext_debug_cb = 0; |
353 | s->tlsext_debug_arg = NULL; | |
354 | s->tlsext_ticket_expected = 0; | |
355 | s->tlsext_status_type = -1; | |
356 | s->tlsext_status_expected = 0; | |
357 | s->tlsext_ocsp_ids = NULL; | |
358 | s->tlsext_ocsp_exts = NULL; | |
359 | s->tlsext_ocsp_resp = NULL; | |
360 | s->tlsext_ocsp_resplen = -1; | |
361 | CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); | |
362 | s->initial_ctx = ctx; | |
363 | # ifndef OPENSSL_NO_EC | |
364 | if (ctx->tlsext_ecpointformatlist) { | |
365 | s->tlsext_ecpointformatlist = | |
366 | BUF_memdup(ctx->tlsext_ecpointformatlist, | |
367 | ctx->tlsext_ecpointformatlist_length); | |
368 | if (!s->tlsext_ecpointformatlist) | |
369 | goto err; | |
370 | s->tlsext_ecpointformatlist_length = | |
371 | ctx->tlsext_ecpointformatlist_length; | |
372 | } | |
373 | if (ctx->tlsext_ellipticcurvelist) { | |
374 | s->tlsext_ellipticcurvelist = | |
375 | BUF_memdup(ctx->tlsext_ellipticcurvelist, | |
376 | ctx->tlsext_ellipticcurvelist_length); | |
377 | if (!s->tlsext_ellipticcurvelist) | |
378 | goto err; | |
379 | s->tlsext_ellipticcurvelist_length = | |
380 | ctx->tlsext_ellipticcurvelist_length; | |
381 | } | |
382 | # endif | |
bf48836c | 383 | # ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e | 384 | s->next_proto_negotiated = NULL; |
ee2ffc27 | 385 | # endif |
6f017a8f | 386 | |
0f113f3e MC |
387 | if (s->ctx->alpn_client_proto_list) { |
388 | s->alpn_client_proto_list = | |
389 | OPENSSL_malloc(s->ctx->alpn_client_proto_list_len); | |
390 | if (s->alpn_client_proto_list == NULL) | |
391 | goto err; | |
392 | memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list, | |
393 | s->ctx->alpn_client_proto_list_len); | |
394 | s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len; | |
395 | } | |
1aeb3da8 | 396 | #endif |
d02b48c6 | 397 | |
0f113f3e | 398 | s->verify_result = X509_V_OK; |
d02b48c6 | 399 | |
0f113f3e | 400 | s->method = ctx->method; |
d02b48c6 | 401 | |
0f113f3e MC |
402 | if (!s->method->ssl_new(s)) |
403 | goto err; | |
d02b48c6 | 404 | |
0f113f3e MC |
405 | s->references = 1; |
406 | s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1; | |
bf21446a | 407 | |
0f113f3e | 408 | SSL_clear(s); |
58964a49 | 409 | |
0f113f3e | 410 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); |
58964a49 | 411 | |
ddac1974 | 412 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
413 | s->psk_client_callback = ctx->psk_client_callback; |
414 | s->psk_server_callback = ctx->psk_server_callback; | |
ddac1974 NL |
415 | #endif |
416 | ||
0f113f3e MC |
417 | return (s); |
418 | err: | |
419 | if (s != NULL) | |
420 | SSL_free(s); | |
421 | SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE); | |
422 | return (NULL); | |
423 | } | |
d02b48c6 | 424 | |
0f113f3e MC |
425 | int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx, |
426 | unsigned int sid_ctx_len) | |
427 | { | |
428 | if (sid_ctx_len > sizeof ctx->sid_ctx) { | |
429 | SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT, | |
430 | SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); | |
431 | return 0; | |
432 | } | |
433 | ctx->sid_ctx_length = sid_ctx_len; | |
434 | memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len); | |
4eb77b26 BM |
435 | |
436 | return 1; | |
0f113f3e | 437 | } |
4eb77b26 | 438 | |
0f113f3e MC |
439 | int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx, |
440 | unsigned int sid_ctx_len) | |
441 | { | |
442 | if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { | |
443 | SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT, | |
444 | SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); | |
445 | return 0; | |
446 | } | |
447 | ssl->sid_ctx_length = sid_ctx_len; | |
448 | memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len); | |
b4cadc6e BL |
449 | |
450 | return 1; | |
0f113f3e | 451 | } |
b4cadc6e | 452 | |
dc644fe2 | 453 | int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) |
0f113f3e MC |
454 | { |
455 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); | |
456 | ctx->generate_session_id = cb; | |
457 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); | |
458 | return 1; | |
459 | } | |
dc644fe2 GT |
460 | |
461 | int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) | |
0f113f3e MC |
462 | { |
463 | CRYPTO_w_lock(CRYPTO_LOCK_SSL); | |
464 | ssl->generate_session_id = cb; | |
465 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL); | |
466 | return 1; | |
467 | } | |
dc644fe2 | 468 | |
f85c9904 | 469 | int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id, |
0f113f3e MC |
470 | unsigned int id_len) |
471 | { | |
472 | /* | |
473 | * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how | |
474 | * we can "construct" a session to give us the desired check - ie. to | |
475 | * find if there's a session in the hash table that would conflict with | |
476 | * any new session built out of this id/id_len and the ssl_version in use | |
477 | * by this SSL. | |
478 | */ | |
479 | SSL_SESSION r, *p; | |
480 | ||
481 | if (id_len > sizeof r.session_id) | |
482 | return 0; | |
483 | ||
484 | r.ssl_version = ssl->version; | |
485 | r.session_id_length = id_len; | |
486 | memcpy(r.session_id, id, id_len); | |
487 | ||
488 | CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); | |
489 | p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r); | |
490 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); | |
491 | return (p != NULL); | |
492 | } | |
dc644fe2 | 493 | |
bb7cd4e3 | 494 | int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) |
0f113f3e MC |
495 | { |
496 | return X509_VERIFY_PARAM_set_purpose(s->param, purpose); | |
497 | } | |
bb7cd4e3 DSH |
498 | |
499 | int SSL_set_purpose(SSL *s, int purpose) | |
0f113f3e MC |
500 | { |
501 | return X509_VERIFY_PARAM_set_purpose(s->param, purpose); | |
502 | } | |
926a56bf | 503 | |
bb7cd4e3 | 504 | int SSL_CTX_set_trust(SSL_CTX *s, int trust) |
0f113f3e MC |
505 | { |
506 | return X509_VERIFY_PARAM_set_trust(s->param, trust); | |
507 | } | |
bb7cd4e3 DSH |
508 | |
509 | int SSL_set_trust(SSL *s, int trust) | |
0f113f3e MC |
510 | { |
511 | return X509_VERIFY_PARAM_set_trust(s->param, trust); | |
512 | } | |
bb7cd4e3 | 513 | |
ccf11751 | 514 | int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) |
0f113f3e MC |
515 | { |
516 | return X509_VERIFY_PARAM_set1(ctx->param, vpm); | |
517 | } | |
ccf11751 DSH |
518 | |
519 | int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) | |
0f113f3e MC |
520 | { |
521 | return X509_VERIFY_PARAM_set1(ssl->param, vpm); | |
522 | } | |
ccf11751 | 523 | |
7af31968 | 524 | X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) |
0f113f3e MC |
525 | { |
526 | return ctx->param; | |
527 | } | |
7af31968 DSH |
528 | |
529 | X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) | |
0f113f3e MC |
530 | { |
531 | return ssl->param; | |
532 | } | |
7af31968 | 533 | |
a5ee80b9 | 534 | void SSL_certs_clear(SSL *s) |
0f113f3e MC |
535 | { |
536 | ssl_cert_clear_certs(s->cert); | |
537 | } | |
a5ee80b9 | 538 | |
4f43d0e7 | 539 | void SSL_free(SSL *s) |
0f113f3e MC |
540 | { |
541 | int i; | |
58964a49 | 542 | |
0f113f3e MC |
543 | if (s == NULL) |
544 | return; | |
e03ddfae | 545 | |
0f113f3e | 546 | i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL); |
58964a49 | 547 | #ifdef REF_PRINT |
0f113f3e | 548 | REF_PRINT("SSL", s); |
58964a49 | 549 | #endif |
0f113f3e MC |
550 | if (i > 0) |
551 | return; | |
58964a49 | 552 | #ifdef REF_CHECK |
0f113f3e MC |
553 | if (i < 0) { |
554 | fprintf(stderr, "SSL_free, bad reference count\n"); | |
555 | abort(); /* ok */ | |
556 | } | |
1aeb3da8 | 557 | #endif |
d02b48c6 | 558 | |
0f113f3e MC |
559 | if (s->param) |
560 | X509_VERIFY_PARAM_free(s->param); | |
561 | ||
562 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); | |
563 | ||
564 | if (s->bbio != NULL) { | |
565 | /* If the buffering BIO is in place, pop it off */ | |
566 | if (s->bbio == s->wbio) { | |
567 | s->wbio = BIO_pop(s->wbio); | |
568 | } | |
569 | BIO_free(s->bbio); | |
570 | s->bbio = NULL; | |
571 | } | |
572 | if (s->rbio != NULL) | |
573 | BIO_free_all(s->rbio); | |
574 | if ((s->wbio != NULL) && (s->wbio != s->rbio)) | |
575 | BIO_free_all(s->wbio); | |
576 | ||
577 | if (s->init_buf != NULL) | |
578 | BUF_MEM_free(s->init_buf); | |
579 | ||
580 | /* add extra stuff */ | |
581 | if (s->cipher_list != NULL) | |
582 | sk_SSL_CIPHER_free(s->cipher_list); | |
583 | if (s->cipher_list_by_id != NULL) | |
584 | sk_SSL_CIPHER_free(s->cipher_list_by_id); | |
585 | ||
586 | /* Make the next call work :-) */ | |
587 | if (s->session != NULL) { | |
588 | ssl_clear_bad_session(s); | |
589 | SSL_SESSION_free(s->session); | |
590 | } | |
591 | ||
592 | ssl_clear_cipher_ctx(s); | |
593 | ssl_clear_hash_ctx(&s->read_hash); | |
594 | ssl_clear_hash_ctx(&s->write_hash); | |
d02b48c6 | 595 | |
0f113f3e MC |
596 | if (s->cert != NULL) |
597 | ssl_cert_free(s->cert); | |
598 | /* Free up if allocated */ | |
d02b48c6 | 599 | |
0f113f3e MC |
600 | #ifndef OPENSSL_NO_TLSEXT |
601 | if (s->tlsext_hostname) | |
602 | OPENSSL_free(s->tlsext_hostname); | |
603 | if (s->initial_ctx) | |
604 | SSL_CTX_free(s->initial_ctx); | |
605 | # ifndef OPENSSL_NO_EC | |
606 | if (s->tlsext_ecpointformatlist) | |
607 | OPENSSL_free(s->tlsext_ecpointformatlist); | |
608 | if (s->tlsext_ellipticcurvelist) | |
609 | OPENSSL_free(s->tlsext_ellipticcurvelist); | |
610 | # endif /* OPENSSL_NO_EC */ | |
0f113f3e MC |
611 | if (s->tlsext_ocsp_exts) |
612 | sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free); | |
613 | if (s->tlsext_ocsp_ids) | |
614 | sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); | |
615 | if (s->tlsext_ocsp_resp) | |
616 | OPENSSL_free(s->tlsext_ocsp_resp); | |
617 | if (s->alpn_client_proto_list) | |
618 | OPENSSL_free(s->alpn_client_proto_list); | |
619 | #endif | |
620 | ||
621 | if (s->client_CA != NULL) | |
622 | sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free); | |
623 | ||
624 | if (s->method != NULL) | |
625 | s->method->ssl_free(s); | |
626 | ||
627 | if (s->ctx) | |
628 | SSL_CTX_free(s->ctx); | |
7c3908dd | 629 | |
0f113f3e MC |
630 | #ifndef OPENSSL_NO_KRB5 |
631 | if (s->kssl_ctx != NULL) | |
632 | kssl_ctx_free(s->kssl_ctx); | |
633 | #endif /* OPENSSL_NO_KRB5 */ | |
0e6c20da | 634 | |
bf48836c | 635 | #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) |
0f113f3e MC |
636 | if (s->next_proto_negotiated) |
637 | OPENSSL_free(s->next_proto_negotiated); | |
ee2ffc27 BL |
638 | #endif |
639 | ||
e783bae2 | 640 | #ifndef OPENSSL_NO_SRTP |
0f113f3e MC |
641 | if (s->srtp_profiles) |
642 | sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles); | |
643 | #endif | |
644 | ||
645 | OPENSSL_free(s); | |
646 | } | |
647 | ||
648 | void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) | |
649 | { | |
650 | /* | |
651 | * If the output buffering BIO is still in place, remove it | |
652 | */ | |
653 | if (s->bbio != NULL) { | |
654 | if (s->wbio == s->bbio) { | |
655 | s->wbio = s->wbio->next_bio; | |
656 | s->bbio->next_bio = NULL; | |
657 | } | |
658 | } | |
659 | if ((s->rbio != NULL) && (s->rbio != rbio)) | |
660 | BIO_free_all(s->rbio); | |
661 | if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio)) | |
662 | BIO_free_all(s->wbio); | |
663 | s->rbio = rbio; | |
664 | s->wbio = wbio; | |
665 | } | |
d02b48c6 | 666 | |
0821bcd4 | 667 | BIO *SSL_get_rbio(const SSL *s) |
0f113f3e MC |
668 | { |
669 | return (s->rbio); | |
670 | } | |
d02b48c6 | 671 | |
0821bcd4 | 672 | BIO *SSL_get_wbio(const SSL *s) |
0f113f3e MC |
673 | { |
674 | return (s->wbio); | |
675 | } | |
d02b48c6 | 676 | |
0821bcd4 | 677 | int SSL_get_fd(const SSL *s) |
0f113f3e MC |
678 | { |
679 | return (SSL_get_rfd(s)); | |
680 | } | |
24cbf3ef | 681 | |
0821bcd4 | 682 | int SSL_get_rfd(const SSL *s) |
0f113f3e MC |
683 | { |
684 | int ret = -1; | |
685 | BIO *b, *r; | |
686 | ||
687 | b = SSL_get_rbio(s); | |
688 | r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); | |
689 | if (r != NULL) | |
690 | BIO_get_fd(r, &ret); | |
691 | return (ret); | |
692 | } | |
d02b48c6 | 693 | |
0821bcd4 | 694 | int SSL_get_wfd(const SSL *s) |
0f113f3e MC |
695 | { |
696 | int ret = -1; | |
697 | BIO *b, *r; | |
698 | ||
699 | b = SSL_get_wbio(s); | |
700 | r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); | |
701 | if (r != NULL) | |
702 | BIO_get_fd(r, &ret); | |
703 | return (ret); | |
704 | } | |
24cbf3ef | 705 | |
bc36ee62 | 706 | #ifndef OPENSSL_NO_SOCK |
0f113f3e MC |
707 | int SSL_set_fd(SSL *s, int fd) |
708 | { | |
709 | int ret = 0; | |
710 | BIO *bio = NULL; | |
711 | ||
712 | bio = BIO_new(BIO_s_socket()); | |
713 | ||
714 | if (bio == NULL) { | |
715 | SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB); | |
716 | goto err; | |
717 | } | |
718 | BIO_set_fd(bio, fd, BIO_NOCLOSE); | |
719 | SSL_set_bio(s, bio, bio); | |
720 | ret = 1; | |
721 | err: | |
722 | return (ret); | |
723 | } | |
d02b48c6 | 724 | |
0f113f3e MC |
725 | int SSL_set_wfd(SSL *s, int fd) |
726 | { | |
727 | int ret = 0; | |
728 | BIO *bio = NULL; | |
729 | ||
730 | if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET) | |
731 | || ((int)BIO_get_fd(s->rbio, NULL) != fd)) { | |
732 | bio = BIO_new(BIO_s_socket()); | |
733 | ||
734 | if (bio == NULL) { | |
735 | SSLerr(SSL_F_SSL_SET_WFD, ERR_R_BUF_LIB); | |
736 | goto err; | |
737 | } | |
738 | BIO_set_fd(bio, fd, BIO_NOCLOSE); | |
739 | SSL_set_bio(s, SSL_get_rbio(s), bio); | |
740 | } else | |
741 | SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s)); | |
742 | ret = 1; | |
743 | err: | |
744 | return (ret); | |
745 | } | |
746 | ||
747 | int SSL_set_rfd(SSL *s, int fd) | |
748 | { | |
749 | int ret = 0; | |
750 | BIO *bio = NULL; | |
751 | ||
752 | if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET) | |
753 | || ((int)BIO_get_fd(s->wbio, NULL) != fd)) { | |
754 | bio = BIO_new(BIO_s_socket()); | |
755 | ||
756 | if (bio == NULL) { | |
757 | SSLerr(SSL_F_SSL_SET_RFD, ERR_R_BUF_LIB); | |
758 | goto err; | |
759 | } | |
760 | BIO_set_fd(bio, fd, BIO_NOCLOSE); | |
761 | SSL_set_bio(s, bio, SSL_get_wbio(s)); | |
762 | } else | |
763 | SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s)); | |
764 | ret = 1; | |
765 | err: | |
766 | return (ret); | |
767 | } | |
768 | #endif | |
ca03109c BM |
769 | |
770 | /* return length of latest Finished message we sent, copy to 'buf' */ | |
0821bcd4 | 771 | size_t SSL_get_finished(const SSL *s, void *buf, size_t count) |
0f113f3e MC |
772 | { |
773 | size_t ret = 0; | |
774 | ||
775 | if (s->s3 != NULL) { | |
776 | ret = s->s3->tmp.finish_md_len; | |
777 | if (count > ret) | |
778 | count = ret; | |
779 | memcpy(buf, s->s3->tmp.finish_md, count); | |
780 | } | |
781 | return ret; | |
782 | } | |
ca03109c BM |
783 | |
784 | /* return length of latest Finished message we expected, copy to 'buf' */ | |
0821bcd4 | 785 | size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) |
0f113f3e MC |
786 | { |
787 | size_t ret = 0; | |
ca03109c | 788 | |
0f113f3e MC |
789 | if (s->s3 != NULL) { |
790 | ret = s->s3->tmp.peer_finish_md_len; | |
791 | if (count > ret) | |
792 | count = ret; | |
793 | memcpy(buf, s->s3->tmp.peer_finish_md, count); | |
794 | } | |
795 | return ret; | |
796 | } | |
ca03109c | 797 | |
0821bcd4 | 798 | int SSL_get_verify_mode(const SSL *s) |
0f113f3e MC |
799 | { |
800 | return (s->verify_mode); | |
801 | } | |
d02b48c6 | 802 | |
0821bcd4 | 803 | int SSL_get_verify_depth(const SSL *s) |
0f113f3e MC |
804 | { |
805 | return X509_VERIFY_PARAM_get_depth(s->param); | |
806 | } | |
7f89714e | 807 | |
0f113f3e MC |
808 | int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) { |
809 | return (s->verify_callback); | |
810 | } | |
d02b48c6 | 811 | |
0821bcd4 | 812 | int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) |
0f113f3e MC |
813 | { |
814 | return (ctx->verify_mode); | |
815 | } | |
d02b48c6 | 816 | |
0821bcd4 | 817 | int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) |
0f113f3e MC |
818 | { |
819 | return X509_VERIFY_PARAM_get_depth(ctx->param); | |
820 | } | |
821 | ||
822 | int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) { | |
823 | return (ctx->default_verify_callback); | |
824 | } | |
825 | ||
826 | void SSL_set_verify(SSL *s, int mode, | |
827 | int (*callback) (int ok, X509_STORE_CTX *ctx)) | |
828 | { | |
829 | s->verify_mode = mode; | |
830 | if (callback != NULL) | |
831 | s->verify_callback = callback; | |
832 | } | |
833 | ||
834 | void SSL_set_verify_depth(SSL *s, int depth) | |
835 | { | |
836 | X509_VERIFY_PARAM_set_depth(s->param, depth); | |
837 | } | |
838 | ||
839 | void SSL_set_read_ahead(SSL *s, int yes) | |
840 | { | |
841 | s->read_ahead = yes; | |
842 | } | |
d02b48c6 | 843 | |
0821bcd4 | 844 | int SSL_get_read_ahead(const SSL *s) |
0f113f3e MC |
845 | { |
846 | return (s->read_ahead); | |
847 | } | |
d02b48c6 | 848 | |
0821bcd4 | 849 | int SSL_pending(const SSL *s) |
0f113f3e MC |
850 | { |
851 | /* | |
852 | * SSL_pending cannot work properly if read-ahead is enabled | |
853 | * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is | |
854 | * impossible to fix since SSL_pending cannot report errors that may be | |
855 | * observed while scanning the new data. (Note that SSL_pending() is | |
856 | * often used as a boolean value, so we'd better not return -1.) | |
857 | */ | |
858 | return (s->method->ssl_pending(s)); | |
859 | } | |
d02b48c6 | 860 | |
0821bcd4 | 861 | X509 *SSL_get_peer_certificate(const SSL *s) |
0f113f3e MC |
862 | { |
863 | X509 *r; | |
d02b48c6 | 864 | |
0f113f3e MC |
865 | if ((s == NULL) || (s->session == NULL)) |
866 | r = NULL; | |
867 | else | |
868 | r = s->session->peer; | |
d02b48c6 | 869 | |
0f113f3e MC |
870 | if (r == NULL) |
871 | return (r); | |
d02b48c6 | 872 | |
0f113f3e MC |
873 | CRYPTO_add(&r->references, 1, CRYPTO_LOCK_X509); |
874 | ||
875 | return (r); | |
876 | } | |
d02b48c6 | 877 | |
0821bcd4 | 878 | STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) |
0f113f3e MC |
879 | { |
880 | STACK_OF(X509) *r; | |
881 | ||
882 | if ((s == NULL) || (s->session == NULL) | |
883 | || (s->session->sess_cert == NULL)) | |
884 | r = NULL; | |
885 | else | |
886 | r = s->session->sess_cert->cert_chain; | |
887 | ||
888 | /* | |
889 | * If we are a client, cert_chain includes the peer's own certificate; if | |
890 | * we are a server, it does not. | |
891 | */ | |
892 | ||
893 | return (r); | |
894 | } | |
895 | ||
896 | /* | |
897 | * Now in theory, since the calling process own 't' it should be safe to | |
898 | * modify. We need to be able to read f without being hassled | |
899 | */ | |
900 | void SSL_copy_session_id(SSL *t, const SSL *f) | |
901 | { | |
902 | CERT *tmp; | |
903 | ||
904 | /* Do we need to to SSL locking? */ | |
905 | SSL_set_session(t, SSL_get_session(f)); | |
906 | ||
907 | /* | |
908 | * what if we are setup as SSLv2 but want to talk SSLv3 or vice-versa | |
909 | */ | |
910 | if (t->method != f->method) { | |
911 | t->method->ssl_free(t); /* cleanup current */ | |
912 | t->method = f->method; /* change method */ | |
913 | t->method->ssl_new(t); /* setup new */ | |
914 | } | |
915 | ||
916 | tmp = t->cert; | |
917 | if (f->cert != NULL) { | |
918 | CRYPTO_add(&f->cert->references, 1, CRYPTO_LOCK_SSL_CERT); | |
919 | t->cert = f->cert; | |
920 | } else | |
921 | t->cert = NULL; | |
922 | if (tmp != NULL) | |
923 | ssl_cert_free(tmp); | |
924 | SSL_set_session_id_context(t, f->sid_ctx, f->sid_ctx_length); | |
925 | } | |
d02b48c6 | 926 | |
58964a49 | 927 | /* Fix this so it checks all the valid key/cert options */ |
0821bcd4 | 928 | int SSL_CTX_check_private_key(const SSL_CTX *ctx) |
0f113f3e MC |
929 | { |
930 | if ((ctx == NULL) || | |
931 | (ctx->cert == NULL) || (ctx->cert->key->x509 == NULL)) { | |
932 | SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, | |
933 | SSL_R_NO_CERTIFICATE_ASSIGNED); | |
934 | return (0); | |
935 | } | |
936 | if (ctx->cert->key->privatekey == NULL) { | |
937 | SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, | |
938 | SSL_R_NO_PRIVATE_KEY_ASSIGNED); | |
939 | return (0); | |
940 | } | |
941 | return (X509_check_private_key | |
942 | (ctx->cert->key->x509, ctx->cert->key->privatekey)); | |
943 | } | |
d02b48c6 | 944 | |
58964a49 | 945 | /* Fix this function so that it takes an optional type parameter */ |
0821bcd4 | 946 | int SSL_check_private_key(const SSL *ssl) |
0f113f3e MC |
947 | { |
948 | if (ssl == NULL) { | |
949 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, ERR_R_PASSED_NULL_PARAMETER); | |
950 | return (0); | |
951 | } | |
952 | if (ssl->cert == NULL) { | |
953 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED); | |
954 | return 0; | |
955 | } | |
956 | if (ssl->cert->key->x509 == NULL) { | |
957 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED); | |
958 | return (0); | |
959 | } | |
960 | if (ssl->cert->key->privatekey == NULL) { | |
961 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED); | |
962 | return (0); | |
963 | } | |
964 | return (X509_check_private_key(ssl->cert->key->x509, | |
965 | ssl->cert->key->privatekey)); | |
966 | } | |
d02b48c6 | 967 | |
4f43d0e7 | 968 | int SSL_accept(SSL *s) |
0f113f3e MC |
969 | { |
970 | if (s->handshake_func == 0) | |
971 | /* Not properly initialized yet */ | |
972 | SSL_set_accept_state(s); | |
b31b04d9 | 973 | |
0f113f3e MC |
974 | return (s->method->ssl_accept(s)); |
975 | } | |
d02b48c6 | 976 | |
4f43d0e7 | 977 | int SSL_connect(SSL *s) |
0f113f3e MC |
978 | { |
979 | if (s->handshake_func == 0) | |
980 | /* Not properly initialized yet */ | |
981 | SSL_set_connect_state(s); | |
b31b04d9 | 982 | |
0f113f3e MC |
983 | return (s->method->ssl_connect(s)); |
984 | } | |
d02b48c6 | 985 | |
0821bcd4 | 986 | long SSL_get_default_timeout(const SSL *s) |
0f113f3e MC |
987 | { |
988 | return (s->method->get_timeout()); | |
989 | } | |
990 | ||
991 | int SSL_read(SSL *s, void *buf, int num) | |
992 | { | |
993 | if (s->handshake_func == 0) { | |
994 | SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED); | |
995 | return -1; | |
996 | } | |
997 | ||
998 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { | |
999 | s->rwstate = SSL_NOTHING; | |
1000 | return (0); | |
1001 | } | |
1002 | return (s->method->ssl_read(s, buf, num)); | |
1003 | } | |
1004 | ||
1005 | int SSL_peek(SSL *s, void *buf, int num) | |
1006 | { | |
1007 | if (s->handshake_func == 0) { | |
1008 | SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED); | |
1009 | return -1; | |
1010 | } | |
1011 | ||
1012 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { | |
1013 | return (0); | |
1014 | } | |
1015 | return (s->method->ssl_peek(s, buf, num)); | |
1016 | } | |
1017 | ||
1018 | int SSL_write(SSL *s, const void *buf, int num) | |
1019 | { | |
1020 | if (s->handshake_func == 0) { | |
1021 | SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED); | |
1022 | return -1; | |
1023 | } | |
1024 | ||
1025 | if (s->shutdown & SSL_SENT_SHUTDOWN) { | |
1026 | s->rwstate = SSL_NOTHING; | |
1027 | SSLerr(SSL_F_SSL_WRITE, SSL_R_PROTOCOL_IS_SHUTDOWN); | |
1028 | return (-1); | |
1029 | } | |
1030 | return (s->method->ssl_write(s, buf, num)); | |
1031 | } | |
d02b48c6 | 1032 | |
4f43d0e7 | 1033 | int SSL_shutdown(SSL *s) |
0f113f3e MC |
1034 | { |
1035 | /* | |
1036 | * Note that this function behaves differently from what one might | |
1037 | * expect. Return values are 0 for no success (yet), 1 for success; but | |
1038 | * calling it once is usually not enough, even if blocking I/O is used | |
1039 | * (see ssl3_shutdown). | |
1040 | */ | |
1041 | ||
1042 | if (s->handshake_func == 0) { | |
1043 | SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED); | |
1044 | return -1; | |
1045 | } | |
1046 | ||
1047 | if ((s != NULL) && !SSL_in_init(s)) | |
1048 | return (s->method->ssl_shutdown(s)); | |
1049 | else | |
1050 | return (1); | |
1051 | } | |
d02b48c6 | 1052 | |
4f43d0e7 | 1053 | int SSL_renegotiate(SSL *s) |
0f113f3e MC |
1054 | { |
1055 | if (s->renegotiate == 0) | |
1056 | s->renegotiate = 1; | |
44959ee4 | 1057 | |
0f113f3e | 1058 | s->new_session = 1; |
44959ee4 | 1059 | |
0f113f3e MC |
1060 | return (s->method->ssl_renegotiate(s)); |
1061 | } | |
d02b48c6 | 1062 | |
44959ee4 | 1063 | int SSL_renegotiate_abbreviated(SSL *s) |
0f113f3e MC |
1064 | { |
1065 | if (s->renegotiate == 0) | |
1066 | s->renegotiate = 1; | |
c519e89f | 1067 | |
0f113f3e | 1068 | s->new_session = 0; |
c519e89f | 1069 | |
0f113f3e MC |
1070 | return (s->method->ssl_renegotiate(s)); |
1071 | } | |
44959ee4 | 1072 | |
6b0e9fac | 1073 | int SSL_renegotiate_pending(SSL *s) |
0f113f3e MC |
1074 | { |
1075 | /* | |
1076 | * becomes true when negotiation is requested; false again once a | |
1077 | * handshake has finished | |
1078 | */ | |
1079 | return (s->renegotiate != 0); | |
1080 | } | |
1081 | ||
1082 | long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) | |
1083 | { | |
1084 | long l; | |
1085 | ||
1086 | switch (cmd) { | |
1087 | case SSL_CTRL_GET_READ_AHEAD: | |
1088 | return (s->read_ahead); | |
1089 | case SSL_CTRL_SET_READ_AHEAD: | |
1090 | l = s->read_ahead; | |
1091 | s->read_ahead = larg; | |
1092 | return (l); | |
1093 | ||
1094 | case SSL_CTRL_SET_MSG_CALLBACK_ARG: | |
1095 | s->msg_callback_arg = parg; | |
1096 | return 1; | |
1097 | ||
1098 | case SSL_CTRL_OPTIONS: | |
1099 | return (s->options |= larg); | |
1100 | case SSL_CTRL_CLEAR_OPTIONS: | |
1101 | return (s->options &= ~larg); | |
1102 | case SSL_CTRL_MODE: | |
1103 | return (s->mode |= larg); | |
1104 | case SSL_CTRL_CLEAR_MODE: | |
1105 | return (s->mode &= ~larg); | |
1106 | case SSL_CTRL_GET_MAX_CERT_LIST: | |
1107 | return (s->max_cert_list); | |
1108 | case SSL_CTRL_SET_MAX_CERT_LIST: | |
1109 | l = s->max_cert_list; | |
1110 | s->max_cert_list = larg; | |
1111 | return (l); | |
1112 | case SSL_CTRL_SET_MAX_SEND_FRAGMENT: | |
1113 | if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) | |
1114 | return 0; | |
1115 | s->max_send_fragment = larg; | |
1116 | return 1; | |
1117 | case SSL_CTRL_GET_RI_SUPPORT: | |
1118 | if (s->s3) | |
1119 | return s->s3->send_connection_binding; | |
1120 | else | |
1121 | return 0; | |
1122 | case SSL_CTRL_CERT_FLAGS: | |
1123 | return (s->cert->cert_flags |= larg); | |
1124 | case SSL_CTRL_CLEAR_CERT_FLAGS: | |
1125 | return (s->cert->cert_flags &= ~larg); | |
1126 | ||
1127 | case SSL_CTRL_GET_RAW_CIPHERLIST: | |
1128 | if (parg) { | |
1129 | if (s->cert->ciphers_raw == NULL) | |
1130 | return 0; | |
1131 | *(unsigned char **)parg = s->cert->ciphers_raw; | |
1132 | return (int)s->cert->ciphers_rawlen; | |
1133 | } else | |
1134 | return ssl_put_cipher_by_char(s, NULL, NULL); | |
c5364614 | 1135 | case SSL_CTRL_GET_EXTMS_SUPPORT: |
156a8722 DSH |
1136 | if (!s->session || SSL_in_init(s) || s->in_handshake) |
1137 | return -1; | |
1138 | if (s->session->flags & SSL_SESS_FLAG_EXTMS) | |
c5364614 DSH |
1139 | return 1; |
1140 | else | |
1141 | return 0; | |
0f113f3e MC |
1142 | default: |
1143 | return (s->method->ssl_ctrl(s, cmd, larg, parg)); | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void)) | |
1148 | { | |
1149 | switch (cmd) { | |
1150 | case SSL_CTRL_SET_MSG_CALLBACK: | |
1151 | s->msg_callback = (void (*) | |
1152 | (int write_p, int version, int content_type, | |
1153 | const void *buf, size_t len, SSL *ssl, | |
1154 | void *arg))(fp); | |
1155 | return 1; | |
1156 | ||
1157 | default: | |
1158 | return (s->method->ssl_callback_ctrl(s, cmd, fp)); | |
1159 | } | |
1160 | } | |
d3442bc7 | 1161 | |
3c1d6bbc | 1162 | LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) |
0f113f3e MC |
1163 | { |
1164 | return ctx->sessions; | |
1165 | } | |
1166 | ||
1167 | long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) | |
1168 | { | |
1169 | long l; | |
1170 | /* For some cases with ctx == NULL perform syntax checks */ | |
1171 | if (ctx == NULL) { | |
1172 | switch (cmd) { | |
14536c8c | 1173 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1174 | case SSL_CTRL_SET_CURVES_LIST: |
1175 | return tls1_set_curves_list(NULL, NULL, parg); | |
1176 | #endif | |
1177 | case SSL_CTRL_SET_SIGALGS_LIST: | |
1178 | case SSL_CTRL_SET_CLIENT_SIGALGS_LIST: | |
1179 | return tls1_set_sigalgs_list(NULL, parg, 0); | |
1180 | default: | |
1181 | return 0; | |
1182 | } | |
1183 | } | |
1184 | ||
1185 | switch (cmd) { | |
1186 | case SSL_CTRL_GET_READ_AHEAD: | |
1187 | return (ctx->read_ahead); | |
1188 | case SSL_CTRL_SET_READ_AHEAD: | |
1189 | l = ctx->read_ahead; | |
1190 | ctx->read_ahead = larg; | |
1191 | return (l); | |
1192 | ||
1193 | case SSL_CTRL_SET_MSG_CALLBACK_ARG: | |
1194 | ctx->msg_callback_arg = parg; | |
1195 | return 1; | |
1196 | ||
1197 | case SSL_CTRL_GET_MAX_CERT_LIST: | |
1198 | return (ctx->max_cert_list); | |
1199 | case SSL_CTRL_SET_MAX_CERT_LIST: | |
1200 | l = ctx->max_cert_list; | |
1201 | ctx->max_cert_list = larg; | |
1202 | return (l); | |
1203 | ||
1204 | case SSL_CTRL_SET_SESS_CACHE_SIZE: | |
1205 | l = ctx->session_cache_size; | |
1206 | ctx->session_cache_size = larg; | |
1207 | return (l); | |
1208 | case SSL_CTRL_GET_SESS_CACHE_SIZE: | |
1209 | return (ctx->session_cache_size); | |
1210 | case SSL_CTRL_SET_SESS_CACHE_MODE: | |
1211 | l = ctx->session_cache_mode; | |
1212 | ctx->session_cache_mode = larg; | |
1213 | return (l); | |
1214 | case SSL_CTRL_GET_SESS_CACHE_MODE: | |
1215 | return (ctx->session_cache_mode); | |
1216 | ||
1217 | case SSL_CTRL_SESS_NUMBER: | |
1218 | return (lh_SSL_SESSION_num_items(ctx->sessions)); | |
1219 | case SSL_CTRL_SESS_CONNECT: | |
1220 | return (ctx->stats.sess_connect); | |
1221 | case SSL_CTRL_SESS_CONNECT_GOOD: | |
1222 | return (ctx->stats.sess_connect_good); | |
1223 | case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: | |
1224 | return (ctx->stats.sess_connect_renegotiate); | |
1225 | case SSL_CTRL_SESS_ACCEPT: | |
1226 | return (ctx->stats.sess_accept); | |
1227 | case SSL_CTRL_SESS_ACCEPT_GOOD: | |
1228 | return (ctx->stats.sess_accept_good); | |
1229 | case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: | |
1230 | return (ctx->stats.sess_accept_renegotiate); | |
1231 | case SSL_CTRL_SESS_HIT: | |
1232 | return (ctx->stats.sess_hit); | |
1233 | case SSL_CTRL_SESS_CB_HIT: | |
1234 | return (ctx->stats.sess_cb_hit); | |
1235 | case SSL_CTRL_SESS_MISSES: | |
1236 | return (ctx->stats.sess_miss); | |
1237 | case SSL_CTRL_SESS_TIMEOUTS: | |
1238 | return (ctx->stats.sess_timeout); | |
1239 | case SSL_CTRL_SESS_CACHE_FULL: | |
1240 | return (ctx->stats.sess_cache_full); | |
1241 | case SSL_CTRL_OPTIONS: | |
1242 | return (ctx->options |= larg); | |
1243 | case SSL_CTRL_CLEAR_OPTIONS: | |
1244 | return (ctx->options &= ~larg); | |
1245 | case SSL_CTRL_MODE: | |
1246 | return (ctx->mode |= larg); | |
1247 | case SSL_CTRL_CLEAR_MODE: | |
1248 | return (ctx->mode &= ~larg); | |
1249 | case SSL_CTRL_SET_MAX_SEND_FRAGMENT: | |
1250 | if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) | |
1251 | return 0; | |
1252 | ctx->max_send_fragment = larg; | |
1253 | return 1; | |
1254 | case SSL_CTRL_CERT_FLAGS: | |
1255 | return (ctx->cert->cert_flags |= larg); | |
1256 | case SSL_CTRL_CLEAR_CERT_FLAGS: | |
1257 | return (ctx->cert->cert_flags &= ~larg); | |
1258 | default: | |
1259 | return (ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg)); | |
1260 | } | |
1261 | } | |
1262 | ||
1263 | long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void)) | |
1264 | { | |
1265 | switch (cmd) { | |
1266 | case SSL_CTRL_SET_MSG_CALLBACK: | |
1267 | ctx->msg_callback = (void (*) | |
1268 | (int write_p, int version, int content_type, | |
1269 | const void *buf, size_t len, SSL *ssl, | |
1270 | void *arg))(fp); | |
1271 | return 1; | |
1272 | ||
1273 | default: | |
1274 | return (ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp)); | |
1275 | } | |
1276 | } | |
d3442bc7 | 1277 | |
ccd86b68 | 1278 | int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b) |
0f113f3e MC |
1279 | { |
1280 | long l; | |
1281 | ||
1282 | l = a->id - b->id; | |
1283 | if (l == 0L) | |
1284 | return (0); | |
1285 | else | |
1286 | return ((l > 0) ? 1 : -1); | |
1287 | } | |
1288 | ||
1289 | int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap, | |
1290 | const SSL_CIPHER *const *bp) | |
1291 | { | |
1292 | long l; | |
1293 | ||
1294 | l = (*ap)->id - (*bp)->id; | |
1295 | if (l == 0L) | |
1296 | return (0); | |
1297 | else | |
1298 | return ((l > 0) ? 1 : -1); | |
1299 | } | |
d02b48c6 | 1300 | |
4f43d0e7 | 1301 | /** return a STACK of the ciphers available for the SSL and in order of |
d02b48c6 | 1302 | * preference */ |
0821bcd4 | 1303 | STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) |
0f113f3e MC |
1304 | { |
1305 | if (s != NULL) { | |
1306 | if (s->cipher_list != NULL) { | |
1307 | return (s->cipher_list); | |
1308 | } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) { | |
1309 | return (s->ctx->cipher_list); | |
1310 | } | |
1311 | } | |
1312 | return (NULL); | |
1313 | } | |
1314 | ||
8b8e5bed | 1315 | STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s) |
0f113f3e MC |
1316 | { |
1317 | STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers; | |
1318 | int i; | |
1319 | ciphers = SSL_get_ciphers(s); | |
1320 | if (!ciphers) | |
1321 | return NULL; | |
1322 | ssl_set_client_disabled(s); | |
1323 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { | |
1324 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i); | |
1325 | if (!ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED)) { | |
1326 | if (!sk) | |
1327 | sk = sk_SSL_CIPHER_new_null(); | |
1328 | if (!sk) | |
1329 | return NULL; | |
1330 | if (!sk_SSL_CIPHER_push(sk, c)) { | |
1331 | sk_SSL_CIPHER_free(sk); | |
1332 | return NULL; | |
1333 | } | |
1334 | } | |
1335 | } | |
1336 | return sk; | |
1337 | } | |
8b8e5bed | 1338 | |
4f43d0e7 | 1339 | /** return a STACK of the ciphers available for the SSL and in order of |
d02b48c6 | 1340 | * algorithm id */ |
f73e07cf | 1341 | STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) |
0f113f3e MC |
1342 | { |
1343 | if (s != NULL) { | |
1344 | if (s->cipher_list_by_id != NULL) { | |
1345 | return (s->cipher_list_by_id); | |
1346 | } else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) { | |
1347 | return (s->ctx->cipher_list_by_id); | |
1348 | } | |
1349 | } | |
1350 | return (NULL); | |
1351 | } | |
d02b48c6 | 1352 | |
4f43d0e7 | 1353 | /** The old interface to get the same thing as SSL_get_ciphers() */ |
0f113f3e MC |
1354 | const char *SSL_get_cipher_list(const SSL *s, int n) |
1355 | { | |
1356 | SSL_CIPHER *c; | |
1357 | STACK_OF(SSL_CIPHER) *sk; | |
1358 | ||
1359 | if (s == NULL) | |
1360 | return (NULL); | |
1361 | sk = SSL_get_ciphers(s); | |
1362 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n)) | |
1363 | return (NULL); | |
1364 | c = sk_SSL_CIPHER_value(sk, n); | |
1365 | if (c == NULL) | |
1366 | return (NULL); | |
1367 | return (c->name); | |
1368 | } | |
d02b48c6 | 1369 | |
25f923dd | 1370 | /** specify the ciphers to be used by default by the SSL_CTX */ |
018e57c7 | 1371 | int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) |
0f113f3e MC |
1372 | { |
1373 | STACK_OF(SSL_CIPHER) *sk; | |
1374 | ||
1375 | sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list, | |
1376 | &ctx->cipher_list_by_id, str, ctx->cert); | |
1377 | /* | |
1378 | * ssl_create_cipher_list may return an empty stack if it was unable to | |
1379 | * find a cipher matching the given rule string (for example if the rule | |
1380 | * string specifies a cipher which has been disabled). This is not an | |
1381 | * error as far as ssl_create_cipher_list is concerned, and hence | |
1382 | * ctx->cipher_list and ctx->cipher_list_by_id has been updated. | |
1383 | */ | |
1384 | if (sk == NULL) | |
1385 | return 0; | |
1386 | else if (sk_SSL_CIPHER_num(sk) == 0) { | |
1387 | SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); | |
1388 | return 0; | |
1389 | } | |
1390 | return 1; | |
1391 | } | |
d02b48c6 | 1392 | |
4f43d0e7 | 1393 | /** specify the ciphers to be used by the SSL */ |
0f113f3e MC |
1394 | int SSL_set_cipher_list(SSL *s, const char *str) |
1395 | { | |
1396 | STACK_OF(SSL_CIPHER) *sk; | |
1397 | ||
1398 | sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list, | |
1399 | &s->cipher_list_by_id, str, s->cert); | |
1400 | /* see comment in SSL_CTX_set_cipher_list */ | |
1401 | if (sk == NULL) | |
1402 | return 0; | |
1403 | else if (sk_SSL_CIPHER_num(sk) == 0) { | |
1404 | SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); | |
1405 | return 0; | |
1406 | } | |
1407 | return 1; | |
1408 | } | |
d02b48c6 RE |
1409 | |
1410 | /* works well for SSLv2, not so good for SSLv3 */ | |
0f113f3e MC |
1411 | char *SSL_get_shared_ciphers(const SSL *s, char *buf, int len) |
1412 | { | |
1413 | char *p; | |
1414 | STACK_OF(SSL_CIPHER) *sk; | |
1415 | SSL_CIPHER *c; | |
1416 | int i; | |
1417 | ||
1418 | if ((s->session == NULL) || (s->session->ciphers == NULL) || (len < 2)) | |
1419 | return (NULL); | |
1420 | ||
1421 | p = buf; | |
1422 | sk = s->session->ciphers; | |
1423 | ||
1424 | if (sk_SSL_CIPHER_num(sk) == 0) | |
1425 | return NULL; | |
1426 | ||
1427 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { | |
1428 | int n; | |
1429 | ||
1430 | c = sk_SSL_CIPHER_value(sk, i); | |
1431 | n = strlen(c->name); | |
1432 | if (n + 1 > len) { | |
1433 | if (p != buf) | |
1434 | --p; | |
1435 | *p = '\0'; | |
1436 | return buf; | |
1437 | } | |
1438 | strcpy(p, c->name); | |
1439 | p += n; | |
1440 | *(p++) = ':'; | |
1441 | len -= n + 1; | |
1442 | } | |
1443 | p[-1] = '\0'; | |
1444 | return (buf); | |
1445 | } | |
1446 | ||
1447 | int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, | |
1448 | unsigned char *p, | |
1449 | int (*put_cb) (const SSL_CIPHER *, | |
1450 | unsigned char *)) | |
1451 | { | |
1452 | int i, j = 0; | |
1453 | SSL_CIPHER *c; | |
1454 | unsigned char *q; | |
1455 | int empty_reneg_info_scsv = !s->renegotiate; | |
1456 | /* Set disabled masks for this session */ | |
1457 | ssl_set_client_disabled(s); | |
1458 | ||
1459 | if (sk == NULL) | |
1460 | return (0); | |
1461 | q = p; | |
1462 | if (put_cb == NULL) | |
1463 | put_cb = s->method->put_cipher_by_char; | |
1464 | ||
1465 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { | |
1466 | c = sk_SSL_CIPHER_value(sk, i); | |
1467 | /* Skip disabled ciphers */ | |
1468 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED)) | |
1469 | continue; | |
323fa645 | 1470 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
0f113f3e MC |
1471 | if (c->id == SSL3_CK_SCSV) { |
1472 | if (!empty_reneg_info_scsv) | |
1473 | continue; | |
1474 | else | |
1475 | empty_reneg_info_scsv = 0; | |
1476 | } | |
1477 | #endif | |
1478 | j = put_cb(c, p); | |
1479 | p += j; | |
1480 | } | |
1481 | /* | |
1482 | * If p == q, no ciphers; caller indicates an error. Otherwise, add | |
1483 | * applicable SCSVs. | |
1484 | */ | |
1485 | if (p != q) { | |
1486 | if (empty_reneg_info_scsv) { | |
1487 | static SSL_CIPHER scsv = { | |
1488 | 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
1489 | }; | |
1490 | j = put_cb(&scsv, p); | |
1491 | p += j; | |
13f6d57b | 1492 | #ifdef OPENSSL_RI_DEBUG |
0f113f3e MC |
1493 | fprintf(stderr, |
1494 | "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n"); | |
1495 | #endif | |
1496 | } | |
1497 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) { | |
1498 | static SSL_CIPHER scsv = { | |
1499 | 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
1500 | }; | |
1501 | j = put_cb(&scsv, p); | |
1502 | p += j; | |
1503 | } | |
1504 | } | |
1505 | ||
1506 | return (p - q); | |
1507 | } | |
1508 | ||
1509 | STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, unsigned char *p, | |
1510 | int num, | |
1511 | STACK_OF(SSL_CIPHER) **skp) | |
1512 | { | |
1513 | const SSL_CIPHER *c; | |
1514 | STACK_OF(SSL_CIPHER) *sk; | |
1515 | int i, n; | |
1516 | ||
1517 | if (s->s3) | |
1518 | s->s3->send_connection_binding = 0; | |
1519 | ||
1520 | n = ssl_put_cipher_by_char(s, NULL, NULL); | |
1521 | if (n == 0 || (num % n) != 0) { | |
1522 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, | |
1523 | SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); | |
1524 | return (NULL); | |
1525 | } | |
1526 | if ((skp == NULL) || (*skp == NULL)) | |
1527 | sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */ | |
1528 | else { | |
1529 | sk = *skp; | |
1530 | sk_SSL_CIPHER_zero(sk); | |
1531 | } | |
1532 | ||
1533 | if (s->cert->ciphers_raw) | |
1534 | OPENSSL_free(s->cert->ciphers_raw); | |
1535 | s->cert->ciphers_raw = BUF_memdup(p, num); | |
1536 | if (s->cert->ciphers_raw == NULL) { | |
1537 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE); | |
1538 | goto err; | |
1539 | } | |
1540 | s->cert->ciphers_rawlen = (size_t)num; | |
1541 | ||
1542 | for (i = 0; i < num; i += n) { | |
1543 | /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */ | |
1544 | if (s->s3 && (n != 3 || !p[0]) && | |
1545 | (p[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) && | |
1546 | (p[n - 1] == (SSL3_CK_SCSV & 0xff))) { | |
1547 | /* SCSV fatal if renegotiating */ | |
1548 | if (s->renegotiate) { | |
1549 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, | |
1550 | SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); | |
1551 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
1552 | goto err; | |
1553 | } | |
1554 | s->s3->send_connection_binding = 1; | |
1555 | p += n; | |
13f6d57b | 1556 | #ifdef OPENSSL_RI_DEBUG |
0f113f3e MC |
1557 | fprintf(stderr, "SCSV received by server\n"); |
1558 | #endif | |
1559 | continue; | |
1560 | } | |
1561 | ||
1562 | /* Check for TLS_FALLBACK_SCSV */ | |
1563 | if ((n != 3 || !p[0]) && | |
1564 | (p[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) && | |
1565 | (p[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) { | |
1566 | /* | |
1567 | * The SCSV indicates that the client previously tried a higher | |
1568 | * version. Fail if the current version is an unexpected | |
1569 | * downgrade. | |
1570 | */ | |
1571 | if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL)) { | |
1572 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, | |
1573 | SSL_R_INAPPROPRIATE_FALLBACK); | |
1574 | if (s->s3) | |
1575 | ssl3_send_alert(s, SSL3_AL_FATAL, | |
1576 | SSL_AD_INAPPROPRIATE_FALLBACK); | |
1577 | goto err; | |
1578 | } | |
1579 | p += n; | |
1580 | continue; | |
1581 | } | |
1582 | ||
1583 | c = ssl_get_cipher_by_char(s, p); | |
1584 | p += n; | |
1585 | if (c != NULL) { | |
1586 | if (!sk_SSL_CIPHER_push(sk, c)) { | |
1587 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE); | |
1588 | goto err; | |
1589 | } | |
1590 | } | |
1591 | } | |
d02b48c6 | 1592 | |
0f113f3e MC |
1593 | if (skp != NULL) |
1594 | *skp = sk; | |
1595 | return (sk); | |
1596 | err: | |
1597 | if ((skp == NULL) || (*skp == NULL)) | |
1598 | sk_SSL_CIPHER_free(sk); | |
1599 | return (NULL); | |
1600 | } | |
f1fd4544 | 1601 | |
34449617 | 1602 | #ifndef OPENSSL_NO_TLSEXT |
52b8dad8 | 1603 | /** return a servername extension value if provided in Client Hello, or NULL. |
f1fd4544 | 1604 | * So far, only host_name types are defined (RFC 3546). |
ed3883d2 BM |
1605 | */ |
1606 | ||
f1fd4544 | 1607 | const char *SSL_get_servername(const SSL *s, const int type) |
0f113f3e MC |
1608 | { |
1609 | if (type != TLSEXT_NAMETYPE_host_name) | |
1610 | return NULL; | |
a13c20f6 | 1611 | |
0f113f3e MC |
1612 | return s->session && !s->tlsext_hostname ? |
1613 | s->session->tlsext_hostname : s->tlsext_hostname; | |
1614 | } | |
ed3883d2 | 1615 | |
f1fd4544 | 1616 | int SSL_get_servername_type(const SSL *s) |
0f113f3e MC |
1617 | { |
1618 | if (s->session | |
1619 | && (!s->tlsext_hostname ? s->session-> | |
1620 | tlsext_hostname : s->tlsext_hostname)) | |
1621 | return TLSEXT_NAMETYPE_host_name; | |
1622 | return -1; | |
1623 | } | |
ee2ffc27 | 1624 | |
0f113f3e MC |
1625 | /* |
1626 | * SSL_select_next_proto implements the standard protocol selection. It is | |
ee2ffc27 | 1627 | * expected that this function is called from the callback set by |
0f113f3e MC |
1628 | * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a |
1629 | * vector of 8-bit, length prefixed byte strings. The length byte itself is | |
1630 | * not included in the length. A byte string of length 0 is invalid. No byte | |
1631 | * string may be truncated. The current, but experimental algorithm for | |
1632 | * selecting the protocol is: 1) If the server doesn't support NPN then this | |
1633 | * is indicated to the callback. In this case, the client application has to | |
1634 | * abort the connection or have a default application level protocol. 2) If | |
1635 | * the server supports NPN, but advertises an empty list then the client | |
1636 | * selects the first protcol in its list, but indicates via the API that this | |
1637 | * fallback case was enacted. 3) Otherwise, the client finds the first | |
1638 | * protocol in the server's list that it supports and selects this protocol. | |
1639 | * This is because it's assumed that the server has better information about | |
1640 | * which protocol a client should use. 4) If the client doesn't support any | |
1641 | * of the server's advertised protocols, then this is treated the same as | |
1642 | * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was | |
1643 | * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached. | |
ee2ffc27 | 1644 | */ |
0f113f3e MC |
1645 | int SSL_select_next_proto(unsigned char **out, unsigned char *outlen, |
1646 | const unsigned char *server, | |
1647 | unsigned int server_len, | |
1648 | const unsigned char *client, | |
1649 | unsigned int client_len) | |
1650 | { | |
1651 | unsigned int i, j; | |
1652 | const unsigned char *result; | |
1653 | int status = OPENSSL_NPN_UNSUPPORTED; | |
1654 | ||
1655 | /* | |
1656 | * For each protocol in server preference order, see if we support it. | |
1657 | */ | |
1658 | for (i = 0; i < server_len;) { | |
1659 | for (j = 0; j < client_len;) { | |
1660 | if (server[i] == client[j] && | |
1661 | memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) { | |
1662 | /* We found a match */ | |
1663 | result = &server[i]; | |
1664 | status = OPENSSL_NPN_NEGOTIATED; | |
1665 | goto found; | |
1666 | } | |
1667 | j += client[j]; | |
1668 | j++; | |
1669 | } | |
1670 | i += server[i]; | |
1671 | i++; | |
1672 | } | |
1673 | ||
1674 | /* There's no overlap between our protocols and the server's list. */ | |
1675 | result = client; | |
1676 | status = OPENSSL_NPN_NO_OVERLAP; | |
1677 | ||
1678 | found: | |
1679 | *out = (unsigned char *)result + 1; | |
1680 | *outlen = result[0]; | |
1681 | return status; | |
1682 | } | |
ee2ffc27 | 1683 | |
2911575c | 1684 | # ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
1685 | /* |
1686 | * SSL_get0_next_proto_negotiated sets *data and *len to point to the | |
1687 | * client's requested protocol for this connection and returns 0. If the | |
1688 | * client didn't request any protocol, then *data is set to NULL. Note that | |
1689 | * the client can request any protocol it chooses. The value returned from | |
1690 | * this function need not be a member of the list of supported protocols | |
ee2ffc27 BL |
1691 | * provided by the callback. |
1692 | */ | |
0f113f3e MC |
1693 | void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, |
1694 | unsigned *len) | |
1695 | { | |
1696 | *data = s->next_proto_negotiated; | |
1697 | if (!*data) { | |
1698 | *len = 0; | |
1699 | } else { | |
1700 | *len = s->next_proto_negotiated_len; | |
1701 | } | |
1702 | } | |
1703 | ||
1704 | /* | |
1705 | * SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when | |
1706 | * a TLS server needs a list of supported protocols for Next Protocol | |
1707 | * Negotiation. The returned list must be in wire format. The list is | |
1708 | * returned by setting |out| to point to it and |outlen| to its length. This | |
1709 | * memory will not be modified, but one should assume that the SSL* keeps a | |
1710 | * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it | |
1711 | * wishes to advertise. Otherwise, no such extension will be included in the | |
1712 | * ServerHello. | |
1713 | */ | |
1714 | void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, | |
1715 | int (*cb) (SSL *ssl, | |
1716 | const unsigned char | |
1717 | **out, | |
1718 | unsigned int *outlen, | |
1719 | void *arg), void *arg) | |
1720 | { | |
1721 | ctx->next_protos_advertised_cb = cb; | |
1722 | ctx->next_protos_advertised_cb_arg = arg; | |
1723 | } | |
1724 | ||
1725 | /* | |
1726 | * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a | |
ee2ffc27 BL |
1727 | * client needs to select a protocol from the server's provided list. |out| |
1728 | * must be set to point to the selected protocol (which may be within |in|). | |
0f113f3e MC |
1729 | * The length of the protocol name must be written into |outlen|. The |
1730 | * server's advertised protocols are provided in |in| and |inlen|. The | |
1731 | * callback can assume that |in| is syntactically valid. The client must | |
1732 | * select a protocol. It is fatal to the connection if this callback returns | |
1733 | * a value other than SSL_TLSEXT_ERR_OK. | |
ee2ffc27 | 1734 | */ |
0f113f3e MC |
1735 | void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, |
1736 | int (*cb) (SSL *s, unsigned char **out, | |
1737 | unsigned char *outlen, | |
1738 | const unsigned char *in, | |
1739 | unsigned int inlen, | |
1740 | void *arg), void *arg) | |
1741 | { | |
1742 | ctx->next_proto_select_cb = cb; | |
1743 | ctx->next_proto_select_cb_arg = arg; | |
1744 | } | |
ee2ffc27 | 1745 | # endif |
a398f821 | 1746 | |
0f113f3e MC |
1747 | /* |
1748 | * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|. | |
6f017a8f | 1749 | * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit |
0f113f3e MC |
1750 | * length-prefixed strings). Returns 0 on success. |
1751 | */ | |
1752 | int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos, | |
1753 | unsigned protos_len) | |
1754 | { | |
1755 | if (ctx->alpn_client_proto_list) | |
1756 | OPENSSL_free(ctx->alpn_client_proto_list); | |
1757 | ||
1758 | ctx->alpn_client_proto_list = OPENSSL_malloc(protos_len); | |
1759 | if (!ctx->alpn_client_proto_list) | |
1760 | return 1; | |
1761 | memcpy(ctx->alpn_client_proto_list, protos, protos_len); | |
1762 | ctx->alpn_client_proto_list_len = protos_len; | |
1763 | ||
1764 | return 0; | |
1765 | } | |
1766 | ||
1767 | /* | |
1768 | * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|. | |
6f017a8f | 1769 | * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit |
0f113f3e MC |
1770 | * length-prefixed strings). Returns 0 on success. |
1771 | */ | |
1772 | int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos, | |
1773 | unsigned protos_len) | |
1774 | { | |
1775 | if (ssl->alpn_client_proto_list) | |
1776 | OPENSSL_free(ssl->alpn_client_proto_list); | |
1777 | ||
1778 | ssl->alpn_client_proto_list = OPENSSL_malloc(protos_len); | |
1779 | if (!ssl->alpn_client_proto_list) | |
1780 | return 1; | |
1781 | memcpy(ssl->alpn_client_proto_list, protos, protos_len); | |
1782 | ssl->alpn_client_proto_list_len = protos_len; | |
1783 | ||
1784 | return 0; | |
1785 | } | |
1786 | ||
1787 | /* | |
1788 | * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is | |
1789 | * called during ClientHello processing in order to select an ALPN protocol | |
1790 | * from the client's list of offered protocols. | |
1791 | */ | |
1792 | void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, | |
1793 | int (*cb) (SSL *ssl, | |
1794 | const unsigned char **out, | |
1795 | unsigned char *outlen, | |
1796 | const unsigned char *in, | |
1797 | unsigned int inlen, | |
1798 | void *arg), void *arg) | |
1799 | { | |
1800 | ctx->alpn_select_cb = cb; | |
1801 | ctx->alpn_select_cb_arg = arg; | |
1802 | } | |
1803 | ||
1804 | /* | |
1805 | * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from | |
1806 | * |ssl|. On return it sets |*data| to point to |*len| bytes of protocol name | |
1807 | * (not including the leading length-prefix byte). If the server didn't | |
1808 | * respond with a negotiated protocol then |*len| will be zero. | |
1809 | */ | |
6f017a8f | 1810 | void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data, |
0f113f3e MC |
1811 | unsigned *len) |
1812 | { | |
1813 | *data = NULL; | |
1814 | if (ssl->s3) | |
1815 | *data = ssl->s3->alpn_selected; | |
1816 | if (*data == NULL) | |
1817 | *len = 0; | |
1818 | else | |
1819 | *len = ssl->s3->alpn_selected_len; | |
1820 | } | |
1821 | ||
1822 | #endif /* !OPENSSL_NO_TLSEXT */ | |
f1fd4544 | 1823 | |
74b4b494 | 1824 | int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen, |
0f113f3e MC |
1825 | const char *label, size_t llen, |
1826 | const unsigned char *p, size_t plen, | |
1827 | int use_context) | |
1828 | { | |
1829 | if (s->version < TLS1_VERSION) | |
1830 | return -1; | |
e0af0405 | 1831 | |
0f113f3e MC |
1832 | return s->method->ssl3_enc->export_keying_material(s, out, olen, label, |
1833 | llen, p, plen, | |
1834 | use_context); | |
1835 | } | |
e0af0405 | 1836 | |
3c1d6bbc | 1837 | static unsigned long ssl_session_hash(const SSL_SESSION *a) |
0f113f3e MC |
1838 | { |
1839 | unsigned long l; | |
1840 | ||
1841 | l = (unsigned long) | |
1842 | ((unsigned int)a->session_id[0]) | | |
1843 | ((unsigned int)a->session_id[1] << 8L) | | |
1844 | ((unsigned long)a->session_id[2] << 16L) | | |
1845 | ((unsigned long)a->session_id[3] << 24L); | |
1846 | return (l); | |
1847 | } | |
1848 | ||
1849 | /* | |
1850 | * NB: If this function (or indeed the hash function which uses a sort of | |
dc644fe2 | 1851 | * coarser function than this one) is changed, ensure |
0f113f3e MC |
1852 | * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on |
1853 | * being able to construct an SSL_SESSION that will collide with any existing | |
1854 | * session with a matching session ID. | |
1855 | */ | |
1856 | static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) | |
1857 | { | |
1858 | if (a->ssl_version != b->ssl_version) | |
1859 | return (1); | |
1860 | if (a->session_id_length != b->session_id_length) | |
1861 | return (1); | |
1862 | return (memcmp(a->session_id, b->session_id, a->session_id_length)); | |
1863 | } | |
1864 | ||
1865 | /* | |
1866 | * These wrapper functions should remain rather than redeclaring | |
d0fa136c | 1867 | * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each |
0f113f3e MC |
1868 | * variable. The reason is that the functions aren't static, they're exposed |
1869 | * via ssl.h. | |
1870 | */ | |
3c1d6bbc BL |
1871 | static IMPLEMENT_LHASH_HASH_FN(ssl_session, SSL_SESSION) |
1872 | static IMPLEMENT_LHASH_COMP_FN(ssl_session, SSL_SESSION) | |
97b17195 | 1873 | |
4ebb342f | 1874 | SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) |
0f113f3e MC |
1875 | { |
1876 | SSL_CTX *ret = NULL; | |
1877 | ||
1878 | if (meth == NULL) { | |
1879 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED); | |
1880 | return (NULL); | |
1881 | } | |
1882 | ||
1883 | if (FIPS_mode() && (meth->version < TLS1_VERSION)) { | |
1884 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE); | |
1885 | return NULL; | |
1886 | } | |
1887 | ||
1888 | if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) { | |
1889 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); | |
1890 | goto err; | |
1891 | } | |
1892 | ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); | |
1893 | if (ret == NULL) | |
1894 | goto err; | |
1895 | ||
1896 | memset(ret, 0, sizeof(SSL_CTX)); | |
1897 | ||
1898 | ret->method = meth; | |
1899 | ||
1900 | ret->cert_store = NULL; | |
1901 | ret->session_cache_mode = SSL_SESS_CACHE_SERVER; | |
1902 | ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; | |
1903 | ret->session_cache_head = NULL; | |
1904 | ret->session_cache_tail = NULL; | |
1905 | ||
1906 | /* We take the system default */ | |
1907 | ret->session_timeout = meth->get_timeout(); | |
1908 | ||
1909 | ret->new_session_cb = 0; | |
1910 | ret->remove_session_cb = 0; | |
1911 | ret->get_session_cb = 0; | |
1912 | ret->generate_session_id = 0; | |
1913 | ||
1914 | memset((char *)&ret->stats, 0, sizeof(ret->stats)); | |
1915 | ||
1916 | ret->references = 1; | |
1917 | ret->quiet_shutdown = 0; | |
1918 | ||
35a1cc90 | 1919 | /* ret->cipher=NULL;*/ |
3a83462d | 1920 | /*- |
35a1cc90 MC |
1921 | ret->s2->challenge=NULL; |
1922 | ret->master_key=NULL; | |
1923 | ret->s2->conn_id=NULL; */ | |
d02b48c6 | 1924 | |
0f113f3e | 1925 | ret->info_callback = NULL; |
d02b48c6 | 1926 | |
0f113f3e MC |
1927 | ret->app_verify_callback = 0; |
1928 | ret->app_verify_arg = NULL; | |
d02b48c6 | 1929 | |
0f113f3e MC |
1930 | ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; |
1931 | ret->read_ahead = 0; | |
1932 | ret->msg_callback = 0; | |
1933 | ret->msg_callback_arg = NULL; | |
1934 | ret->verify_mode = SSL_VERIFY_NONE; | |
5d7c222d | 1935 | #if 0 |
0f113f3e MC |
1936 | ret->verify_depth = -1; /* Don't impose a limit (but x509_lu.c does) */ |
1937 | #endif | |
1938 | ret->sid_ctx_length = 0; | |
1939 | ret->default_verify_callback = NULL; | |
1940 | if ((ret->cert = ssl_cert_new()) == NULL) | |
1941 | goto err; | |
1942 | ||
1943 | ret->default_passwd_callback = 0; | |
1944 | ret->default_passwd_callback_userdata = NULL; | |
1945 | ret->client_cert_cb = 0; | |
1946 | ret->app_gen_cookie_cb = 0; | |
1947 | ret->app_verify_cookie_cb = 0; | |
1948 | ||
1949 | ret->sessions = lh_SSL_SESSION_new(); | |
1950 | if (ret->sessions == NULL) | |
1951 | goto err; | |
1952 | ret->cert_store = X509_STORE_new(); | |
1953 | if (ret->cert_store == NULL) | |
1954 | goto err; | |
1955 | ||
1956 | ssl_create_cipher_list(ret->method, | |
1957 | &ret->cipher_list, &ret->cipher_list_by_id, | |
1958 | SSL_DEFAULT_CIPHER_LIST, ret->cert); | |
1959 | if (ret->cipher_list == NULL || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) { | |
1960 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS); | |
1961 | goto err2; | |
1962 | } | |
1963 | ||
1964 | ret->param = X509_VERIFY_PARAM_new(); | |
1965 | if (!ret->param) | |
1966 | goto err; | |
1967 | ||
1968 | if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) { | |
1969 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES); | |
1970 | goto err2; | |
1971 | } | |
1972 | if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) { | |
1973 | SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES); | |
1974 | goto err2; | |
1975 | } | |
1976 | ||
1977 | if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL) | |
1978 | goto err; | |
1979 | ||
1980 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data); | |
1981 | ||
1982 | ret->extra_certs = NULL; | |
1983 | /* No compression for DTLS */ | |
1984 | if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS)) | |
1985 | ret->comp_methods = SSL_COMP_get_compression_methods(); | |
1986 | ||
1987 | ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; | |
566dda07 | 1988 | |
ed3883d2 | 1989 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e MC |
1990 | ret->tlsext_servername_callback = 0; |
1991 | ret->tlsext_servername_arg = NULL; | |
1992 | /* Setup RFC4507 ticket keys */ | |
1993 | if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0) | |
1994 | || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0) | |
1995 | || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0)) | |
1996 | ret->options |= SSL_OP_NO_TICKET; | |
6434abbf | 1997 | |
0f113f3e MC |
1998 | ret->tlsext_status_cb = 0; |
1999 | ret->tlsext_status_arg = NULL; | |
67c8e7f4 | 2000 | |
bf48836c | 2001 | # ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
2002 | ret->next_protos_advertised_cb = 0; |
2003 | ret->next_proto_select_cb = 0; | |
ee2ffc27 | 2004 | # endif |
ddac1974 NL |
2005 | #endif |
2006 | #ifndef OPENSSL_NO_PSK | |
0f113f3e MC |
2007 | ret->psk_identity_hint = NULL; |
2008 | ret->psk_client_callback = NULL; | |
2009 | ret->psk_server_callback = NULL; | |
8671b898 | 2010 | #endif |
edc032b5 | 2011 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 2012 | SSL_CTX_SRP_CTX_init(ret); |
edc032b5 | 2013 | #endif |
4db9677b | 2014 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
2015 | ret->client_cert_engine = NULL; |
2016 | # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO | |
2017 | # define eng_strx(x) #x | |
2018 | # define eng_str(x) eng_strx(x) | |
2019 | /* Use specific client engine automatically... ignore errors */ | |
2020 | { | |
2021 | ENGINE *eng; | |
2022 | eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); | |
2023 | if (!eng) { | |
2024 | ERR_clear_error(); | |
2025 | ENGINE_load_builtin_engines(); | |
2026 | eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); | |
2027 | } | |
2028 | if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng)) | |
2029 | ERR_clear_error(); | |
2030 | } | |
2031 | # endif | |
2032 | #endif | |
2033 | /* | |
2034 | * Default is to connect to non-RI servers. When RI is more widely | |
2035 | * deployed might change this. | |
2036 | */ | |
2037 | ret->options |= SSL_OP_LEGACY_SERVER_CONNECT; | |
2038 | ||
2039 | return (ret); | |
2040 | err: | |
2041 | SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE); | |
2042 | err2: | |
2043 | if (ret != NULL) | |
2044 | SSL_CTX_free(ret); | |
2045 | return (NULL); | |
2046 | } | |
d02b48c6 | 2047 | |
4f43d0e7 | 2048 | void SSL_CTX_free(SSL_CTX *a) |
0f113f3e MC |
2049 | { |
2050 | int i; | |
d02b48c6 | 2051 | |
0f113f3e MC |
2052 | if (a == NULL) |
2053 | return; | |
d02b48c6 | 2054 | |
0f113f3e | 2055 | i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX); |
58964a49 | 2056 | #ifdef REF_PRINT |
0f113f3e | 2057 | REF_PRINT("SSL_CTX", a); |
58964a49 | 2058 | #endif |
0f113f3e MC |
2059 | if (i > 0) |
2060 | return; | |
d02b48c6 | 2061 | #ifdef REF_CHECK |
0f113f3e MC |
2062 | if (i < 0) { |
2063 | fprintf(stderr, "SSL_CTX_free, bad reference count\n"); | |
2064 | abort(); /* ok */ | |
2065 | } | |
2066 | #endif | |
2067 | ||
2068 | if (a->param) | |
2069 | X509_VERIFY_PARAM_free(a->param); | |
2070 | ||
2071 | /* | |
2072 | * Free internal session cache. However: the remove_cb() may reference | |
2073 | * the ex_data of SSL_CTX, thus the ex_data store can only be removed | |
2074 | * after the sessions were flushed. | |
2075 | * As the ex_data handling routines might also touch the session cache, | |
2076 | * the most secure solution seems to be: empty (flush) the cache, then | |
2077 | * free ex_data, then finally free the cache. | |
2078 | * (See ticket [openssl.org #212].) | |
2079 | */ | |
2080 | if (a->sessions != NULL) | |
2081 | SSL_CTX_flush_sessions(a, 0); | |
2082 | ||
2083 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data); | |
2084 | ||
2085 | if (a->sessions != NULL) | |
2086 | lh_SSL_SESSION_free(a->sessions); | |
2087 | ||
2088 | if (a->cert_store != NULL) | |
2089 | X509_STORE_free(a->cert_store); | |
2090 | if (a->cipher_list != NULL) | |
2091 | sk_SSL_CIPHER_free(a->cipher_list); | |
2092 | if (a->cipher_list_by_id != NULL) | |
2093 | sk_SSL_CIPHER_free(a->cipher_list_by_id); | |
2094 | if (a->cert != NULL) | |
2095 | ssl_cert_free(a->cert); | |
2096 | if (a->client_CA != NULL) | |
2097 | sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free); | |
2098 | if (a->extra_certs != NULL) | |
2099 | sk_X509_pop_free(a->extra_certs, X509_free); | |
2100 | #if 0 /* This should never be done, since it | |
2101 | * removes a global database */ | |
2102 | if (a->comp_methods != NULL) | |
2103 | sk_SSL_COMP_pop_free(a->comp_methods, SSL_COMP_free); | |
cd9b7d7c | 2104 | #else |
0f113f3e | 2105 | a->comp_methods = NULL; |
cd9b7d7c | 2106 | #endif |
ddac1974 | 2107 | |
e783bae2 | 2108 | #ifndef OPENSSL_NO_SRTP |
0f113f3e MC |
2109 | if (a->srtp_profiles) |
2110 | sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles); | |
e783bae2 | 2111 | #endif |
333f926d | 2112 | |
ddac1974 | 2113 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
2114 | if (a->psk_identity_hint) |
2115 | OPENSSL_free(a->psk_identity_hint); | |
bdfe932d | 2116 | #endif |
edc032b5 | 2117 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 2118 | SSL_CTX_SRP_CTX_free(a); |
edc032b5 | 2119 | #endif |
bdfe932d | 2120 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
2121 | if (a->client_cert_engine) |
2122 | ENGINE_finish(a->client_cert_engine); | |
ddac1974 | 2123 | #endif |
8671b898 | 2124 | |
d0595f17 DSH |
2125 | #ifndef OPENSSL_NO_TLSEXT |
2126 | # ifndef OPENSSL_NO_EC | |
0f113f3e MC |
2127 | if (a->tlsext_ecpointformatlist) |
2128 | OPENSSL_free(a->tlsext_ecpointformatlist); | |
2129 | if (a->tlsext_ellipticcurvelist) | |
2130 | OPENSSL_free(a->tlsext_ellipticcurvelist); | |
2131 | # endif /* OPENSSL_NO_EC */ | |
2132 | if (a->alpn_client_proto_list != NULL) | |
2133 | OPENSSL_free(a->alpn_client_proto_list); | |
8671b898 BL |
2134 | #endif |
2135 | ||
0f113f3e MC |
2136 | OPENSSL_free(a); |
2137 | } | |
d02b48c6 | 2138 | |
3ae76679 | 2139 | void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) |
0f113f3e MC |
2140 | { |
2141 | ctx->default_passwd_callback = cb; | |
2142 | } | |
2143 | ||
2144 | void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) | |
2145 | { | |
2146 | ctx->default_passwd_callback_userdata = u; | |
2147 | } | |
2148 | ||
2149 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, | |
2150 | int (*cb) (X509_STORE_CTX *, void *), | |
2151 | void *arg) | |
2152 | { | |
2153 | ctx->app_verify_callback = cb; | |
2154 | ctx->app_verify_arg = arg; | |
2155 | } | |
2156 | ||
2157 | void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, | |
2158 | int (*cb) (int, X509_STORE_CTX *)) | |
2159 | { | |
2160 | ctx->verify_mode = mode; | |
2161 | ctx->default_verify_callback = cb; | |
2162 | } | |
2163 | ||
2164 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) | |
2165 | { | |
2166 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); | |
2167 | } | |
2168 | ||
2169 | void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), | |
2170 | void *arg) | |
2171 | { | |
2172 | ssl_cert_set_cert_cb(c->cert, cb, arg); | |
2173 | } | |
2174 | ||
2175 | void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg) | |
2176 | { | |
2177 | ssl_cert_set_cert_cb(s->cert, cb, arg); | |
2178 | } | |
18d71588 | 2179 | |
babb3798 | 2180 | void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher) |
0f113f3e MC |
2181 | { |
2182 | CERT_PKEY *cpk; | |
2183 | int rsa_enc, rsa_tmp, rsa_sign, dh_tmp, dh_rsa, dh_dsa, dsa_sign; | |
2184 | int rsa_enc_export, dh_rsa_export, dh_dsa_export; | |
2185 | int rsa_tmp_export, dh_tmp_export, kl; | |
2186 | unsigned long mask_k, mask_a, emask_k, emask_a; | |
14536c8c | 2187 | #ifndef OPENSSL_NO_ECDSA |
0f113f3e | 2188 | int have_ecc_cert, ecdsa_ok, ecc_pkey_size; |
14536c8c | 2189 | #endif |
3eeaab4b | 2190 | #ifndef OPENSSL_NO_ECDH |
0f113f3e | 2191 | int have_ecdh_tmp, ecdh_ok; |
3eeaab4b | 2192 | #endif |
14536c8c | 2193 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
2194 | X509 *x = NULL; |
2195 | EVP_PKEY *ecc_pkey = NULL; | |
2196 | int signature_nid = 0, pk_nid = 0, md_nid = 0; | |
14536c8c | 2197 | #endif |
0f113f3e MC |
2198 | if (c == NULL) |
2199 | return; | |
d02b48c6 | 2200 | |
0f113f3e | 2201 | kl = SSL_C_EXPORT_PKEYLENGTH(cipher); |
60e31c3a | 2202 | |
bc36ee62 | 2203 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
2204 | rsa_tmp = (c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL); |
2205 | rsa_tmp_export = (c->rsa_tmp_cb != NULL || | |
2206 | (rsa_tmp && RSA_size(c->rsa_tmp) * 8 <= kl)); | |
d02b48c6 | 2207 | #else |
0f113f3e | 2208 | rsa_tmp = rsa_tmp_export = 0; |
d02b48c6 | 2209 | #endif |
bc36ee62 | 2210 | #ifndef OPENSSL_NO_DH |
0f113f3e MC |
2211 | dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL || c->dh_tmp_auto); |
2212 | dh_tmp_export = !c->dh_tmp_auto && (c->dh_tmp_cb != NULL || | |
2213 | (dh_tmp | |
2214 | && DH_size(c->dh_tmp) * 8 <= kl)); | |
d02b48c6 | 2215 | #else |
0f113f3e | 2216 | dh_tmp = dh_tmp_export = 0; |
d02b48c6 RE |
2217 | #endif |
2218 | ||
ea262260 | 2219 | #ifndef OPENSSL_NO_ECDH |
0f113f3e MC |
2220 | have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto); |
2221 | #endif | |
2222 | cpk = &(c->pkeys[SSL_PKEY_RSA_ENC]); | |
2223 | rsa_enc = cpk->valid_flags & CERT_PKEY_VALID; | |
2224 | rsa_enc_export = (rsa_enc && EVP_PKEY_size(cpk->privatekey) * 8 <= kl); | |
2225 | cpk = &(c->pkeys[SSL_PKEY_RSA_SIGN]); | |
2226 | rsa_sign = cpk->valid_flags & CERT_PKEY_SIGN; | |
2227 | cpk = &(c->pkeys[SSL_PKEY_DSA_SIGN]); | |
2228 | dsa_sign = cpk->valid_flags & CERT_PKEY_SIGN; | |
2229 | cpk = &(c->pkeys[SSL_PKEY_DH_RSA]); | |
2230 | dh_rsa = cpk->valid_flags & CERT_PKEY_VALID; | |
2231 | dh_rsa_export = (dh_rsa && EVP_PKEY_size(cpk->privatekey) * 8 <= kl); | |
2232 | cpk = &(c->pkeys[SSL_PKEY_DH_DSA]); | |
d02b48c6 | 2233 | /* FIX THIS EAY EAY EAY */ |
0f113f3e MC |
2234 | dh_dsa = cpk->valid_flags & CERT_PKEY_VALID; |
2235 | dh_dsa_export = (dh_dsa && EVP_PKEY_size(cpk->privatekey) * 8 <= kl); | |
2236 | cpk = &(c->pkeys[SSL_PKEY_ECC]); | |
14536c8c | 2237 | #ifndef OPENSSL_NO_EC |
0f113f3e | 2238 | have_ecc_cert = cpk->valid_flags & CERT_PKEY_VALID; |
14536c8c | 2239 | #endif |
0f113f3e MC |
2240 | mask_k = 0; |
2241 | mask_a = 0; | |
2242 | emask_k = 0; | |
2243 | emask_a = 0; | |
0e1dba93 | 2244 | |
d02b48c6 | 2245 | #ifdef CIPHER_DEBUG |
0f113f3e MC |
2246 | fprintf(stderr, |
2247 | "rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n", | |
2248 | rsa_tmp, rsa_tmp_export, dh_tmp, have_ecdh_tmp, rsa_enc, | |
2249 | rsa_enc_export, rsa_sign, dsa_sign, dh_rsa, dh_dsa); | |
2250 | #endif | |
2251 | ||
2252 | cpk = &(c->pkeys[SSL_PKEY_GOST01]); | |
2253 | if (cpk->x509 != NULL && cpk->privatekey != NULL) { | |
2254 | mask_k |= SSL_kGOST; | |
2255 | mask_a |= SSL_aGOST01; | |
2256 | } | |
2257 | cpk = &(c->pkeys[SSL_PKEY_GOST94]); | |
2258 | if (cpk->x509 != NULL && cpk->privatekey != NULL) { | |
2259 | mask_k |= SSL_kGOST; | |
2260 | mask_a |= SSL_aGOST94; | |
2261 | } | |
2262 | ||
2263 | if (rsa_enc || (rsa_tmp && rsa_sign)) | |
2264 | mask_k |= SSL_kRSA; | |
2265 | if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc))) | |
2266 | emask_k |= SSL_kRSA; | |
d02b48c6 RE |
2267 | |
2268 | #if 0 | |
0f113f3e MC |
2269 | /* The match needs to be both kDHE and aRSA or aDSA, so don't worry */ |
2270 | if ((dh_tmp || dh_rsa || dh_dsa) && (rsa_enc || rsa_sign || dsa_sign)) | |
2271 | mask_k |= SSL_kDHE; | |
2272 | if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) && | |
2273 | (rsa_enc || rsa_sign || dsa_sign)) | |
2274 | emask_k |= SSL_kDHE; | |
d02b48c6 RE |
2275 | #endif |
2276 | ||
0f113f3e MC |
2277 | if (dh_tmp_export) |
2278 | emask_k |= SSL_kDHE; | |
d02b48c6 | 2279 | |
0f113f3e MC |
2280 | if (dh_tmp) |
2281 | mask_k |= SSL_kDHE; | |
d02b48c6 | 2282 | |
0f113f3e MC |
2283 | if (dh_rsa) |
2284 | mask_k |= SSL_kDHr; | |
2285 | if (dh_rsa_export) | |
2286 | emask_k |= SSL_kDHr; | |
d02b48c6 | 2287 | |
0f113f3e MC |
2288 | if (dh_dsa) |
2289 | mask_k |= SSL_kDHd; | |
2290 | if (dh_dsa_export) | |
2291 | emask_k |= SSL_kDHd; | |
d02b48c6 | 2292 | |
0f113f3e MC |
2293 | if (emask_k & (SSL_kDHr | SSL_kDHd)) |
2294 | mask_a |= SSL_aDH; | |
8e1dc4d7 | 2295 | |
0f113f3e MC |
2296 | if (rsa_enc || rsa_sign) { |
2297 | mask_a |= SSL_aRSA; | |
2298 | emask_a |= SSL_aRSA; | |
2299 | } | |
d02b48c6 | 2300 | |
0f113f3e MC |
2301 | if (dsa_sign) { |
2302 | mask_a |= SSL_aDSS; | |
2303 | emask_a |= SSL_aDSS; | |
2304 | } | |
d02b48c6 | 2305 | |
0f113f3e MC |
2306 | mask_a |= SSL_aNULL; |
2307 | emask_a |= SSL_aNULL; | |
d02b48c6 | 2308 | |
bc36ee62 | 2309 | #ifndef OPENSSL_NO_KRB5 |
0f113f3e MC |
2310 | mask_k |= SSL_kKRB5; |
2311 | mask_a |= SSL_aKRB5; | |
2312 | emask_k |= SSL_kKRB5; | |
2313 | emask_a |= SSL_aKRB5; | |
f9b3bff6 RL |
2314 | #endif |
2315 | ||
0f113f3e MC |
2316 | /* |
2317 | * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites | |
2318 | * depending on the key usage extension. | |
2319 | */ | |
14536c8c | 2320 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
2321 | if (have_ecc_cert) { |
2322 | cpk = &c->pkeys[SSL_PKEY_ECC]; | |
2323 | x = cpk->x509; | |
2324 | /* This call populates extension flags (ex_flags) */ | |
2325 | X509_check_purpose(x, -1, 0); | |
2326 | # ifndef OPENSSL_NO_ECDH | |
2327 | ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ? | |
2328 | (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1; | |
2329 | # endif | |
2330 | ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ? | |
2331 | (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1; | |
2332 | if (!(cpk->valid_flags & CERT_PKEY_SIGN)) | |
2333 | ecdsa_ok = 0; | |
2334 | ecc_pkey = X509_get_pubkey(x); | |
2335 | ecc_pkey_size = (ecc_pkey != NULL) ? EVP_PKEY_bits(ecc_pkey) : 0; | |
2336 | EVP_PKEY_free(ecc_pkey); | |
2337 | if ((x->sig_alg) && (x->sig_alg->algorithm)) { | |
2338 | signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); | |
2339 | OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); | |
2340 | } | |
2341 | # ifndef OPENSSL_NO_ECDH | |
2342 | if (ecdh_ok) { | |
2343 | ||
2344 | if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa) { | |
2345 | mask_k |= SSL_kECDHr; | |
2346 | mask_a |= SSL_aECDH; | |
2347 | if (ecc_pkey_size <= 163) { | |
2348 | emask_k |= SSL_kECDHr; | |
2349 | emask_a |= SSL_aECDH; | |
2350 | } | |
2351 | } | |
2352 | ||
2353 | if (pk_nid == NID_X9_62_id_ecPublicKey) { | |
2354 | mask_k |= SSL_kECDHe; | |
2355 | mask_a |= SSL_aECDH; | |
2356 | if (ecc_pkey_size <= 163) { | |
2357 | emask_k |= SSL_kECDHe; | |
2358 | emask_a |= SSL_aECDH; | |
2359 | } | |
2360 | } | |
2361 | } | |
2362 | # endif | |
2363 | # ifndef OPENSSL_NO_ECDSA | |
2364 | if (ecdsa_ok) { | |
2365 | mask_a |= SSL_aECDSA; | |
2366 | emask_a |= SSL_aECDSA; | |
2367 | } | |
2368 | # endif | |
2369 | } | |
14536c8c | 2370 | #endif |
ea262260 BM |
2371 | |
2372 | #ifndef OPENSSL_NO_ECDH | |
0f113f3e MC |
2373 | if (have_ecdh_tmp) { |
2374 | mask_k |= SSL_kECDHE; | |
2375 | emask_k |= SSL_kECDHE; | |
2376 | } | |
ea262260 | 2377 | #endif |
ddac1974 NL |
2378 | |
2379 | #ifndef OPENSSL_NO_PSK | |
0f113f3e MC |
2380 | mask_k |= SSL_kPSK; |
2381 | mask_a |= SSL_aPSK; | |
2382 | emask_k |= SSL_kPSK; | |
2383 | emask_a |= SSL_aPSK; | |
ddac1974 NL |
2384 | #endif |
2385 | ||
0f113f3e MC |
2386 | c->mask_k = mask_k; |
2387 | c->mask_a = mask_a; | |
2388 | c->export_mask_k = emask_k; | |
2389 | c->export_mask_a = emask_a; | |
2390 | c->valid = 1; | |
2391 | } | |
d02b48c6 | 2392 | |
ea262260 BM |
2393 | /* This handy macro borrowed from crypto/x509v3/v3_purp.c */ |
2394 | #define ku_reject(x, usage) \ | |
0f113f3e | 2395 | (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) |
ea262260 | 2396 | |
ef236ec3 DSH |
2397 | #ifndef OPENSSL_NO_EC |
2398 | ||
a2f9200f | 2399 | int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) |
0f113f3e MC |
2400 | { |
2401 | unsigned long alg_k, alg_a; | |
2402 | EVP_PKEY *pkey = NULL; | |
2403 | int keysize = 0; | |
2404 | int signature_nid = 0, md_nid = 0, pk_nid = 0; | |
2405 | const SSL_CIPHER *cs = s->s3->tmp.new_cipher; | |
2406 | ||
2407 | alg_k = cs->algorithm_mkey; | |
2408 | alg_a = cs->algorithm_auth; | |
2409 | ||
2410 | if (SSL_C_IS_EXPORT(cs)) { | |
2411 | /* ECDH key length in export ciphers must be <= 163 bits */ | |
2412 | pkey = X509_get_pubkey(x); | |
2413 | if (pkey == NULL) | |
2414 | return 0; | |
2415 | keysize = EVP_PKEY_bits(pkey); | |
2416 | EVP_PKEY_free(pkey); | |
2417 | if (keysize > 163) | |
2418 | return 0; | |
2419 | } | |
2420 | ||
2421 | /* This call populates the ex_flags field correctly */ | |
2422 | X509_check_purpose(x, -1, 0); | |
2423 | if ((x->sig_alg) && (x->sig_alg->algorithm)) { | |
2424 | signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); | |
2425 | OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); | |
2426 | } | |
2427 | if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr) { | |
2428 | /* key usage, if present, must allow key agreement */ | |
2429 | if (ku_reject(x, X509v3_KU_KEY_AGREEMENT)) { | |
2430 | SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, | |
2431 | SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT); | |
2432 | return 0; | |
2433 | } | |
2434 | if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION) { | |
2435 | /* signature alg must be ECDSA */ | |
2436 | if (pk_nid != NID_X9_62_id_ecPublicKey) { | |
2437 | SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, | |
2438 | SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE); | |
2439 | return 0; | |
2440 | } | |
2441 | } | |
2442 | if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION) { | |
2443 | /* signature alg must be RSA */ | |
2444 | ||
2445 | if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa) { | |
2446 | SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, | |
2447 | SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE); | |
2448 | return 0; | |
2449 | } | |
2450 | } | |
2451 | } | |
2452 | if (alg_a & SSL_aECDSA) { | |
2453 | /* key usage, if present, must allow signing */ | |
2454 | if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) { | |
2455 | SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, | |
2456 | SSL_R_ECC_CERT_NOT_FOR_SIGNING); | |
2457 | return 0; | |
2458 | } | |
2459 | } | |
2460 | ||
2461 | return 1; /* all checks are ok */ | |
2462 | } | |
ea262260 | 2463 | |
ef236ec3 DSH |
2464 | #endif |
2465 | ||
2daceb03 | 2466 | static int ssl_get_server_cert_index(const SSL *s) |
0f113f3e MC |
2467 | { |
2468 | int idx; | |
2469 | idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); | |
2470 | if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) | |
2471 | idx = SSL_PKEY_RSA_SIGN; | |
2472 | if (idx == -1) | |
2473 | SSLerr(SSL_F_SSL_GET_SERVER_CERT_INDEX, ERR_R_INTERNAL_ERROR); | |
2474 | return idx; | |
2475 | } | |
a9e1c50b | 2476 | |
2daceb03 | 2477 | CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) |
0f113f3e MC |
2478 | { |
2479 | CERT *c; | |
2480 | int i; | |
ea262260 | 2481 | |
0f113f3e MC |
2482 | c = s->cert; |
2483 | if (!s->s3 || !s->s3->tmp.new_cipher) | |
2484 | return NULL; | |
2485 | ssl_set_cert_masks(c, s->s3->tmp.new_cipher); | |
a9e1c50b | 2486 | |
ed83ba53 | 2487 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
0f113f3e MC |
2488 | /* |
2489 | * Broken protocol test: return last used certificate: which may mismatch | |
2490 | * the one expected. | |
2491 | */ | |
2492 | if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) | |
2493 | return c->key; | |
ed83ba53 DSH |
2494 | #endif |
2495 | ||
0f113f3e | 2496 | i = ssl_get_server_cert_index(s); |
a9e1c50b | 2497 | |
0f113f3e MC |
2498 | /* This may or may not be an error. */ |
2499 | if (i < 0) | |
2500 | return NULL; | |
a9e1c50b | 2501 | |
0f113f3e MC |
2502 | /* May be NULL. */ |
2503 | return &c->pkeys[i]; | |
2504 | } | |
d02b48c6 | 2505 | |
0f113f3e MC |
2506 | EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, |
2507 | const EVP_MD **pmd) | |
2508 | { | |
2509 | unsigned long alg_a; | |
2510 | CERT *c; | |
2511 | int idx = -1; | |
d02b48c6 | 2512 | |
0f113f3e MC |
2513 | alg_a = cipher->algorithm_auth; |
2514 | c = s->cert; | |
d02b48c6 | 2515 | |
ed83ba53 | 2516 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
0f113f3e MC |
2517 | /* |
2518 | * Broken protocol test: use last key: which may mismatch the one | |
2519 | * expected. | |
2520 | */ | |
2521 | if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) | |
2522 | idx = c->key - c->pkeys; | |
2523 | else | |
2524 | #endif | |
2525 | ||
2526 | if ((alg_a & SSL_aDSS) && | |
2527 | (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL)) | |
2528 | idx = SSL_PKEY_DSA_SIGN; | |
2529 | else if (alg_a & SSL_aRSA) { | |
2530 | if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) | |
2531 | idx = SSL_PKEY_RSA_SIGN; | |
2532 | else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) | |
2533 | idx = SSL_PKEY_RSA_ENC; | |
2534 | } else if ((alg_a & SSL_aECDSA) && | |
2535 | (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) | |
2536 | idx = SSL_PKEY_ECC; | |
2537 | if (idx == -1) { | |
2538 | SSLerr(SSL_F_SSL_GET_SIGN_PKEY, ERR_R_INTERNAL_ERROR); | |
2539 | return (NULL); | |
2540 | } | |
2541 | if (pmd) | |
2542 | *pmd = c->pkeys[idx].digest; | |
2543 | return c->pkeys[idx].privatekey; | |
2544 | } | |
d02b48c6 | 2545 | |
a9e1c50b | 2546 | #ifndef OPENSSL_NO_TLSEXT |
a398f821 | 2547 | int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo, |
0f113f3e MC |
2548 | size_t *serverinfo_length) |
2549 | { | |
2550 | CERT *c = NULL; | |
2551 | int i = 0; | |
2552 | *serverinfo_length = 0; | |
2553 | ||
2554 | c = s->cert; | |
2555 | i = ssl_get_server_cert_index(s); | |
2556 | ||
2557 | if (i == -1) | |
2558 | return 0; | |
2559 | if (c->pkeys[i].serverinfo == NULL) | |
2560 | return 0; | |
2561 | ||
2562 | *serverinfo = c->pkeys[i].serverinfo; | |
2563 | *serverinfo_length = c->pkeys[i].serverinfo_length; | |
2564 | return 1; | |
2565 | } | |
2566 | #endif | |
2567 | ||
2568 | void ssl_update_cache(SSL *s, int mode) | |
2569 | { | |
2570 | int i; | |
2571 | ||
2572 | /* | |
2573 | * If the session_id_length is 0, we are not supposed to cache it, and it | |
2574 | * would be rather hard to do anyway :-) | |
2575 | */ | |
2576 | if (s->session->session_id_length == 0) | |
2577 | return; | |
2578 | ||
2579 | i = s->session_ctx->session_cache_mode; | |
2580 | if ((i & mode) && (!s->hit) | |
2581 | && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) | |
2582 | || SSL_CTX_add_session(s->session_ctx, s->session)) | |
2583 | && (s->session_ctx->new_session_cb != NULL)) { | |
2584 | CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION); | |
2585 | if (!s->session_ctx->new_session_cb(s, s->session)) | |
2586 | SSL_SESSION_free(s->session); | |
2587 | } | |
2588 | ||
2589 | /* auto flush every 255 connections */ | |
2590 | if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) { | |
2591 | if ((((mode & SSL_SESS_CACHE_CLIENT) | |
2592 | ? s->session_ctx->stats.sess_connect_good | |
2593 | : s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) { | |
2594 | SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL)); | |
2595 | } | |
2596 | } | |
2597 | } | |
d02b48c6 | 2598 | |
ba168244 | 2599 | const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx) |
0f113f3e MC |
2600 | { |
2601 | return ctx->method; | |
2602 | } | |
ba168244 | 2603 | |
4ebb342f | 2604 | const SSL_METHOD *SSL_get_ssl_method(SSL *s) |
0f113f3e MC |
2605 | { |
2606 | return (s->method); | |
2607 | } | |
d02b48c6 | 2608 | |
4ebb342f | 2609 | int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth) |
0f113f3e MC |
2610 | { |
2611 | int conn = -1; | |
2612 | int ret = 1; | |
2613 | ||
2614 | if (s->method != meth) { | |
2615 | if (s->handshake_func != NULL) | |
2616 | conn = (s->handshake_func == s->method->ssl_connect); | |
2617 | ||
2618 | if (s->method->version == meth->version) | |
2619 | s->method = meth; | |
2620 | else { | |
2621 | s->method->ssl_free(s); | |
2622 | s->method = meth; | |
2623 | ret = s->method->ssl_new(s); | |
2624 | } | |
2625 | ||
2626 | if (conn == 1) | |
2627 | s->handshake_func = meth->ssl_connect; | |
2628 | else if (conn == 0) | |
2629 | s->handshake_func = meth->ssl_accept; | |
2630 | } | |
2631 | return (ret); | |
2632 | } | |
2633 | ||
2634 | int SSL_get_error(const SSL *s, int i) | |
2635 | { | |
2636 | int reason; | |
2637 | unsigned long l; | |
2638 | BIO *bio; | |
2639 | ||
2640 | if (i > 0) | |
2641 | return (SSL_ERROR_NONE); | |
2642 | ||
2643 | /* | |
2644 | * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc, | |
2645 | * where we do encode the error | |
2646 | */ | |
2647 | if ((l = ERR_peek_error()) != 0) { | |
2648 | if (ERR_GET_LIB(l) == ERR_LIB_SYS) | |
2649 | return (SSL_ERROR_SYSCALL); | |
2650 | else | |
2651 | return (SSL_ERROR_SSL); | |
2652 | } | |
2653 | ||
2654 | if ((i < 0) && SSL_want_read(s)) { | |
2655 | bio = SSL_get_rbio(s); | |
2656 | if (BIO_should_read(bio)) | |
2657 | return (SSL_ERROR_WANT_READ); | |
2658 | else if (BIO_should_write(bio)) | |
2659 | /* | |
2660 | * This one doesn't make too much sense ... We never try to write | |
2661 | * to the rbio, and an application program where rbio and wbio | |
2662 | * are separate couldn't even know what it should wait for. | |
2663 | * However if we ever set s->rwstate incorrectly (so that we have | |
2664 | * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and | |
2665 | * wbio *are* the same, this test works around that bug; so it | |
2666 | * might be safer to keep it. | |
2667 | */ | |
2668 | return (SSL_ERROR_WANT_WRITE); | |
2669 | else if (BIO_should_io_special(bio)) { | |
2670 | reason = BIO_get_retry_reason(bio); | |
2671 | if (reason == BIO_RR_CONNECT) | |
2672 | return (SSL_ERROR_WANT_CONNECT); | |
2673 | else if (reason == BIO_RR_ACCEPT) | |
2674 | return (SSL_ERROR_WANT_ACCEPT); | |
2675 | else | |
2676 | return (SSL_ERROR_SYSCALL); /* unknown */ | |
2677 | } | |
2678 | } | |
2679 | ||
2680 | if ((i < 0) && SSL_want_write(s)) { | |
2681 | bio = SSL_get_wbio(s); | |
2682 | if (BIO_should_write(bio)) | |
2683 | return (SSL_ERROR_WANT_WRITE); | |
2684 | else if (BIO_should_read(bio)) | |
2685 | /* | |
2686 | * See above (SSL_want_read(s) with BIO_should_write(bio)) | |
2687 | */ | |
2688 | return (SSL_ERROR_WANT_READ); | |
2689 | else if (BIO_should_io_special(bio)) { | |
2690 | reason = BIO_get_retry_reason(bio); | |
2691 | if (reason == BIO_RR_CONNECT) | |
2692 | return (SSL_ERROR_WANT_CONNECT); | |
2693 | else if (reason == BIO_RR_ACCEPT) | |
2694 | return (SSL_ERROR_WANT_ACCEPT); | |
2695 | else | |
2696 | return (SSL_ERROR_SYSCALL); | |
2697 | } | |
2698 | } | |
2699 | if ((i < 0) && SSL_want_x509_lookup(s)) { | |
2700 | return (SSL_ERROR_WANT_X509_LOOKUP); | |
2701 | } | |
2702 | ||
2703 | if (i == 0) { | |
2704 | if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && | |
2705 | (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) | |
2706 | return (SSL_ERROR_ZERO_RETURN); | |
2707 | } | |
2708 | return (SSL_ERROR_SYSCALL); | |
2709 | } | |
d02b48c6 | 2710 | |
4f43d0e7 | 2711 | int SSL_do_handshake(SSL *s) |
0f113f3e MC |
2712 | { |
2713 | int ret = 1; | |
2714 | ||
2715 | if (s->handshake_func == NULL) { | |
2716 | SSLerr(SSL_F_SSL_DO_HANDSHAKE, SSL_R_CONNECTION_TYPE_NOT_SET); | |
2717 | return (-1); | |
2718 | } | |
2719 | ||
2720 | s->method->ssl_renegotiate_check(s); | |
2721 | ||
2722 | if (SSL_in_init(s) || SSL_in_before(s)) { | |
2723 | ret = s->handshake_func(s); | |
2724 | } | |
2725 | return (ret); | |
2726 | } | |
2727 | ||
2728 | /* | |
2729 | * For the next 2 functions, SSL_clear() sets shutdown and so one of these | |
2730 | * calls will reset it | |
2731 | */ | |
4f43d0e7 | 2732 | void SSL_set_accept_state(SSL *s) |
0f113f3e MC |
2733 | { |
2734 | s->server = 1; | |
2735 | s->shutdown = 0; | |
2736 | s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE; | |
2737 | s->handshake_func = s->method->ssl_accept; | |
2738 | /* clear the current cipher */ | |
2739 | ssl_clear_cipher_ctx(s); | |
2740 | ssl_clear_hash_ctx(&s->read_hash); | |
2741 | ssl_clear_hash_ctx(&s->write_hash); | |
2742 | } | |
d02b48c6 | 2743 | |
4f43d0e7 | 2744 | void SSL_set_connect_state(SSL *s) |
0f113f3e MC |
2745 | { |
2746 | s->server = 0; | |
2747 | s->shutdown = 0; | |
2748 | s->state = SSL_ST_CONNECT | SSL_ST_BEFORE; | |
2749 | s->handshake_func = s->method->ssl_connect; | |
2750 | /* clear the current cipher */ | |
2751 | ssl_clear_cipher_ctx(s); | |
2752 | ssl_clear_hash_ctx(&s->read_hash); | |
2753 | ssl_clear_hash_ctx(&s->write_hash); | |
2754 | } | |
d02b48c6 | 2755 | |
4f43d0e7 | 2756 | int ssl_undefined_function(SSL *s) |
0f113f3e MC |
2757 | { |
2758 | SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
2759 | return (0); | |
2760 | } | |
d02b48c6 | 2761 | |
41a15c4f | 2762 | int ssl_undefined_void_function(void) |
0f113f3e MC |
2763 | { |
2764 | SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION, | |
2765 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
2766 | return (0); | |
2767 | } | |
41a15c4f | 2768 | |
0821bcd4 | 2769 | int ssl_undefined_const_function(const SSL *s) |
0f113f3e MC |
2770 | { |
2771 | SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION, | |
2772 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
2773 | return (0); | |
2774 | } | |
0821bcd4 | 2775 | |
4f43d0e7 | 2776 | SSL_METHOD *ssl_bad_method(int ver) |
0f113f3e MC |
2777 | { |
2778 | SSLerr(SSL_F_SSL_BAD_METHOD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
2779 | return (NULL); | |
2780 | } | |
d02b48c6 | 2781 | |
0821bcd4 | 2782 | const char *SSL_get_version(const SSL *s) |
0f113f3e MC |
2783 | { |
2784 | if (s->version == TLS1_2_VERSION) | |
2785 | return ("TLSv1.2"); | |
2786 | else if (s->version == TLS1_1_VERSION) | |
2787 | return ("TLSv1.1"); | |
2788 | else if (s->version == TLS1_VERSION) | |
2789 | return ("TLSv1"); | |
2790 | else if (s->version == SSL3_VERSION) | |
2791 | return ("SSLv3"); | |
2792 | else | |
2793 | return ("unknown"); | |
2794 | } | |
d02b48c6 | 2795 | |
4f43d0e7 | 2796 | SSL *SSL_dup(SSL *s) |
0f113f3e MC |
2797 | { |
2798 | STACK_OF(X509_NAME) *sk; | |
2799 | X509_NAME *xn; | |
2800 | SSL *ret; | |
2801 | int i; | |
2802 | ||
2803 | if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL) | |
2804 | return (NULL); | |
2805 | ||
2806 | ret->version = s->version; | |
2807 | ret->type = s->type; | |
2808 | ret->method = s->method; | |
2809 | ||
2810 | if (s->session != NULL) { | |
2811 | /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */ | |
2812 | SSL_copy_session_id(ret, s); | |
2813 | } else { | |
2814 | /* | |
2815 | * No session has been established yet, so we have to expect that | |
2816 | * s->cert or ret->cert will be changed later -- they should not both | |
2817 | * point to the same object, and thus we can't use | |
2818 | * SSL_copy_session_id. | |
2819 | */ | |
2820 | ||
2821 | ret->method->ssl_free(ret); | |
2822 | ret->method = s->method; | |
2823 | ret->method->ssl_new(ret); | |
2824 | ||
2825 | if (s->cert != NULL) { | |
2826 | if (ret->cert != NULL) { | |
2827 | ssl_cert_free(ret->cert); | |
2828 | } | |
2829 | ret->cert = ssl_cert_dup(s->cert); | |
2830 | if (ret->cert == NULL) | |
2831 | goto err; | |
2832 | } | |
2833 | ||
2834 | SSL_set_session_id_context(ret, s->sid_ctx, s->sid_ctx_length); | |
2835 | } | |
2836 | ||
2837 | ret->options = s->options; | |
2838 | ret->mode = s->mode; | |
2839 | SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s)); | |
2840 | SSL_set_read_ahead(ret, SSL_get_read_ahead(s)); | |
2841 | ret->msg_callback = s->msg_callback; | |
2842 | ret->msg_callback_arg = s->msg_callback_arg; | |
2843 | SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s)); | |
2844 | SSL_set_verify_depth(ret, SSL_get_verify_depth(s)); | |
2845 | ret->generate_session_id = s->generate_session_id; | |
2846 | ||
2847 | SSL_set_info_callback(ret, SSL_get_info_callback(s)); | |
2848 | ||
2849 | ret->debug = s->debug; | |
2850 | ||
2851 | /* copy app data, a little dangerous perhaps */ | |
2852 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data)) | |
2853 | goto err; | |
2854 | ||
2855 | /* setup rbio, and wbio */ | |
2856 | if (s->rbio != NULL) { | |
2857 | if (!BIO_dup_state(s->rbio, (char *)&ret->rbio)) | |
2858 | goto err; | |
2859 | } | |
2860 | if (s->wbio != NULL) { | |
2861 | if (s->wbio != s->rbio) { | |
2862 | if (!BIO_dup_state(s->wbio, (char *)&ret->wbio)) | |
2863 | goto err; | |
2864 | } else | |
2865 | ret->wbio = ret->rbio; | |
2866 | } | |
2867 | ret->rwstate = s->rwstate; | |
2868 | ret->in_handshake = s->in_handshake; | |
2869 | ret->handshake_func = s->handshake_func; | |
2870 | ret->server = s->server; | |
2871 | ret->renegotiate = s->renegotiate; | |
2872 | ret->new_session = s->new_session; | |
2873 | ret->quiet_shutdown = s->quiet_shutdown; | |
2874 | ret->shutdown = s->shutdown; | |
2875 | ret->state = s->state; /* SSL_dup does not really work at any state, | |
2876 | * though */ | |
2877 | ret->rstate = s->rstate; | |
2878 | ret->init_num = 0; /* would have to copy ret->init_buf, | |
2879 | * ret->init_msg, ret->init_num, | |
2880 | * ret->init_off */ | |
2881 | ret->hit = s->hit; | |
2882 | ||
2883 | X509_VERIFY_PARAM_inherit(ret->param, s->param); | |
2884 | ||
2885 | /* dup the cipher_list and cipher_list_by_id stacks */ | |
2886 | if (s->cipher_list != NULL) { | |
2887 | if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) | |
2888 | goto err; | |
2889 | } | |
2890 | if (s->cipher_list_by_id != NULL) | |
2891 | if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id)) | |
2892 | == NULL) | |
2893 | goto err; | |
2894 | ||
2895 | /* Dup the client_CA list */ | |
2896 | if (s->client_CA != NULL) { | |
2897 | if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL) | |
2898 | goto err; | |
2899 | ret->client_CA = sk; | |
2900 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { | |
2901 | xn = sk_X509_NAME_value(sk, i); | |
2902 | if (sk_X509_NAME_set(sk, i, X509_NAME_dup(xn)) == NULL) { | |
2903 | X509_NAME_free(xn); | |
2904 | goto err; | |
2905 | } | |
2906 | } | |
2907 | } | |
2908 | ||
2909 | if (0) { | |
2910 | err: | |
2911 | if (ret != NULL) | |
2912 | SSL_free(ret); | |
2913 | ret = NULL; | |
2914 | } | |
2915 | return (ret); | |
2916 | } | |
d02b48c6 | 2917 | |
4f43d0e7 | 2918 | void ssl_clear_cipher_ctx(SSL *s) |
0f113f3e MC |
2919 | { |
2920 | if (s->enc_read_ctx != NULL) { | |
2921 | EVP_CIPHER_CTX_cleanup(s->enc_read_ctx); | |
2922 | OPENSSL_free(s->enc_read_ctx); | |
2923 | s->enc_read_ctx = NULL; | |
2924 | } | |
2925 | if (s->enc_write_ctx != NULL) { | |
2926 | EVP_CIPHER_CTX_cleanup(s->enc_write_ctx); | |
2927 | OPENSSL_free(s->enc_write_ctx); | |
2928 | s->enc_write_ctx = NULL; | |
2929 | } | |
09b6c2ef | 2930 | #ifndef OPENSSL_NO_COMP |
0f113f3e MC |
2931 | if (s->expand != NULL) { |
2932 | COMP_CTX_free(s->expand); | |
2933 | s->expand = NULL; | |
2934 | } | |
2935 | if (s->compress != NULL) { | |
2936 | COMP_CTX_free(s->compress); | |
2937 | s->compress = NULL; | |
2938 | } | |
2939 | #endif | |
2940 | } | |
d02b48c6 | 2941 | |
0821bcd4 | 2942 | X509 *SSL_get_certificate(const SSL *s) |
0f113f3e MC |
2943 | { |
2944 | if (s->cert != NULL) | |
2945 | return (s->cert->key->x509); | |
2946 | else | |
2947 | return (NULL); | |
2948 | } | |
d02b48c6 | 2949 | |
a25f9adc | 2950 | EVP_PKEY *SSL_get_privatekey(const SSL *s) |
0f113f3e MC |
2951 | { |
2952 | if (s->cert != NULL) | |
2953 | return (s->cert->key->privatekey); | |
2954 | else | |
2955 | return (NULL); | |
2956 | } | |
d02b48c6 | 2957 | |
a25f9adc | 2958 | X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) |
0f113f3e MC |
2959 | { |
2960 | if (ctx->cert != NULL) | |
2961 | return ctx->cert->key->x509; | |
2962 | else | |
2963 | return NULL; | |
2964 | } | |
a25f9adc DSH |
2965 | |
2966 | EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) | |
0f113f3e MC |
2967 | { |
2968 | if (ctx->cert != NULL) | |
2969 | return ctx->cert->key->privatekey; | |
2970 | else | |
2971 | return NULL; | |
2972 | } | |
a25f9adc | 2973 | |
babb3798 | 2974 | const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) |
0f113f3e MC |
2975 | { |
2976 | if ((s->session != NULL) && (s->session->cipher != NULL)) | |
2977 | return (s->session->cipher); | |
2978 | return (NULL); | |
2979 | } | |
2980 | ||
09b6c2ef DSH |
2981 | #ifdef OPENSSL_NO_COMP |
2982 | const void *SSL_get_current_compression(SSL *s) | |
0f113f3e MC |
2983 | { |
2984 | return NULL; | |
2985 | } | |
2986 | ||
09b6c2ef | 2987 | const void *SSL_get_current_expansion(SSL *s) |
0f113f3e MC |
2988 | { |
2989 | return NULL; | |
2990 | } | |
09b6c2ef | 2991 | #else |
d02b48c6 | 2992 | |
377dcdba | 2993 | const COMP_METHOD *SSL_get_current_compression(SSL *s) |
0f113f3e MC |
2994 | { |
2995 | if (s->compress != NULL) | |
2996 | return (s->compress->meth); | |
2997 | return (NULL); | |
2998 | } | |
377dcdba RL |
2999 | |
3000 | const COMP_METHOD *SSL_get_current_expansion(SSL *s) | |
0f113f3e MC |
3001 | { |
3002 | if (s->expand != NULL) | |
3003 | return (s->expand->meth); | |
3004 | return (NULL); | |
3005 | } | |
3006 | #endif | |
3007 | ||
3008 | int ssl_init_wbio_buffer(SSL *s, int push) | |
3009 | { | |
3010 | BIO *bbio; | |
3011 | ||
3012 | if (s->bbio == NULL) { | |
3013 | bbio = BIO_new(BIO_f_buffer()); | |
3014 | if (bbio == NULL) | |
3015 | return (0); | |
3016 | s->bbio = bbio; | |
3017 | } else { | |
3018 | bbio = s->bbio; | |
3019 | if (s->bbio == s->wbio) | |
3020 | s->wbio = BIO_pop(s->wbio); | |
3021 | } | |
3022 | (void)BIO_reset(bbio); | |
3023 | /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ | |
3024 | if (!BIO_set_read_buffer_size(bbio, 1)) { | |
3025 | SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER, ERR_R_BUF_LIB); | |
3026 | return (0); | |
3027 | } | |
3028 | if (push) { | |
3029 | if (s->wbio != bbio) | |
3030 | s->wbio = BIO_push(bbio, s->wbio); | |
3031 | } else { | |
3032 | if (s->wbio == bbio) | |
3033 | s->wbio = BIO_pop(bbio); | |
3034 | } | |
3035 | return (1); | |
3036 | } | |
413c4f45 | 3037 | |
4f43d0e7 | 3038 | void ssl_free_wbio_buffer(SSL *s) |
0f113f3e MC |
3039 | { |
3040 | if (s->bbio == NULL) | |
3041 | return; | |
3042 | ||
3043 | if (s->bbio == s->wbio) { | |
3044 | /* remove buffering */ | |
3045 | s->wbio = BIO_pop(s->wbio); | |
3046 | #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids | |
3047 | * adding one more preprocessor symbol */ | |
3048 | assert(s->wbio != NULL); | |
3049 | #endif | |
3050 | } | |
3051 | BIO_free(s->bbio); | |
3052 | s->bbio = NULL; | |
3053 | } | |
3054 | ||
3055 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) | |
3056 | { | |
3057 | ctx->quiet_shutdown = mode; | |
3058 | } | |
58964a49 | 3059 | |
0821bcd4 | 3060 | int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) |
0f113f3e MC |
3061 | { |
3062 | return (ctx->quiet_shutdown); | |
3063 | } | |
58964a49 | 3064 | |
0f113f3e MC |
3065 | void SSL_set_quiet_shutdown(SSL *s, int mode) |
3066 | { | |
3067 | s->quiet_shutdown = mode; | |
3068 | } | |
58964a49 | 3069 | |
0821bcd4 | 3070 | int SSL_get_quiet_shutdown(const SSL *s) |
0f113f3e MC |
3071 | { |
3072 | return (s->quiet_shutdown); | |
3073 | } | |
58964a49 | 3074 | |
0f113f3e MC |
3075 | void SSL_set_shutdown(SSL *s, int mode) |
3076 | { | |
3077 | s->shutdown = mode; | |
3078 | } | |
58964a49 | 3079 | |
0821bcd4 | 3080 | int SSL_get_shutdown(const SSL *s) |
0f113f3e MC |
3081 | { |
3082 | return (s->shutdown); | |
3083 | } | |
58964a49 | 3084 | |
0821bcd4 | 3085 | int SSL_version(const SSL *s) |
0f113f3e MC |
3086 | { |
3087 | return (s->version); | |
3088 | } | |
58964a49 | 3089 | |
0821bcd4 | 3090 | SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) |
0f113f3e MC |
3091 | { |
3092 | return (ssl->ctx); | |
3093 | } | |
3094 | ||
3095 | SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) | |
3096 | { | |
3097 | CERT *ocert = ssl->cert; | |
3098 | if (ssl->ctx == ctx) | |
3099 | return ssl->ctx; | |
367eb1f1 | 3100 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e MC |
3101 | if (ctx == NULL) |
3102 | ctx = ssl->initial_ctx; | |
3103 | #endif | |
3104 | ssl->cert = ssl_cert_dup(ctx->cert); | |
3105 | if (ocert) { | |
3106 | /* Preserve any already negotiated parameters */ | |
3107 | if (ssl->server) { | |
3108 | ssl->cert->peer_sigalgs = ocert->peer_sigalgs; | |
3109 | ssl->cert->peer_sigalgslen = ocert->peer_sigalgslen; | |
3110 | ocert->peer_sigalgs = NULL; | |
3111 | ssl->cert->ciphers_raw = ocert->ciphers_raw; | |
3112 | ssl->cert->ciphers_rawlen = ocert->ciphers_rawlen; | |
3113 | ocert->ciphers_raw = NULL; | |
3114 | } | |
3115 | ssl_cert_free(ocert); | |
3116 | } | |
3117 | ||
3118 | /* | |
3119 | * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH), | |
3120 | * so setter APIs must prevent invalid lengths from entering the system. | |
3121 | */ | |
3122 | OPENSSL_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)); | |
3123 | ||
3124 | /* | |
3125 | * If the session ID context matches that of the parent SSL_CTX, | |
3126 | * inherit it from the new SSL_CTX as well. If however the context does | |
3127 | * not match (i.e., it was set per-ssl with SSL_set_session_id_context), | |
3128 | * leave it unchanged. | |
3129 | */ | |
3130 | if ((ssl->ctx != NULL) && | |
3131 | (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) && | |
3132 | (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) { | |
3133 | ssl->sid_ctx_length = ctx->sid_ctx_length; | |
3134 | memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx)); | |
3135 | } | |
3136 | ||
3137 | CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); | |
3138 | if (ssl->ctx != NULL) | |
3139 | SSL_CTX_free(ssl->ctx); /* decrement reference count */ | |
3140 | ssl->ctx = ctx; | |
3141 | ||
3142 | return (ssl->ctx); | |
3143 | } | |
ed3883d2 | 3144 | |
bc36ee62 | 3145 | #ifndef OPENSSL_NO_STDIO |
4f43d0e7 | 3146 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) |
0f113f3e MC |
3147 | { |
3148 | return (X509_STORE_set_default_paths(ctx->cert_store)); | |
3149 | } | |
58964a49 | 3150 | |
303c0028 | 3151 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, |
0f113f3e MC |
3152 | const char *CApath) |
3153 | { | |
3154 | return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath)); | |
3155 | } | |
dfeab068 | 3156 | #endif |
58964a49 | 3157 | |
45d87a1f | 3158 | void SSL_set_info_callback(SSL *ssl, |
0f113f3e MC |
3159 | void (*cb) (const SSL *ssl, int type, int val)) |
3160 | { | |
3161 | ssl->info_callback = cb; | |
3162 | } | |
3163 | ||
3164 | /* | |
3165 | * One compiler (Diab DCC) doesn't like argument names in returned function | |
3166 | * pointer. | |
3167 | */ | |
3168 | void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ , | |
3169 | int /* type */ , | |
3170 | int /* val */ ) { | |
3171 | return ssl->info_callback; | |
3172 | } | |
58964a49 | 3173 | |
0821bcd4 | 3174 | int SSL_state(const SSL *ssl) |
0f113f3e MC |
3175 | { |
3176 | return (ssl->state); | |
3177 | } | |
58964a49 | 3178 | |
08557cf2 | 3179 | void SSL_set_state(SSL *ssl, int state) |
0f113f3e MC |
3180 | { |
3181 | ssl->state = state; | |
3182 | } | |
08557cf2 | 3183 | |
0f113f3e MC |
3184 | void SSL_set_verify_result(SSL *ssl, long arg) |
3185 | { | |
3186 | ssl->verify_result = arg; | |
3187 | } | |
58964a49 | 3188 | |
0821bcd4 | 3189 | long SSL_get_verify_result(const SSL *ssl) |
0f113f3e MC |
3190 | { |
3191 | return (ssl->verify_result); | |
3192 | } | |
3193 | ||
3194 | int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | |
3195 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | |
3196 | { | |
3197 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, | |
3198 | new_func, dup_func, free_func); | |
3199 | } | |
3200 | ||
3201 | int SSL_set_ex_data(SSL *s, int idx, void *arg) | |
3202 | { | |
3203 | return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); | |
3204 | } | |
3205 | ||
3206 | void *SSL_get_ex_data(const SSL *s, int idx) | |
3207 | { | |
3208 | return (CRYPTO_get_ex_data(&s->ex_data, idx)); | |
3209 | } | |
3210 | ||
3211 | int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | |
3212 | CRYPTO_EX_dup *dup_func, | |
3213 | CRYPTO_EX_free *free_func) | |
3214 | { | |
3215 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, | |
3216 | new_func, dup_func, free_func); | |
3217 | } | |
3218 | ||
3219 | int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) | |
3220 | { | |
3221 | return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); | |
3222 | } | |
3223 | ||
3224 | void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) | |
3225 | { | |
3226 | return (CRYPTO_get_ex_data(&s->ex_data, idx)); | |
3227 | } | |
58964a49 | 3228 | |
4f43d0e7 | 3229 | int ssl_ok(SSL *s) |
0f113f3e MC |
3230 | { |
3231 | return (1); | |
3232 | } | |
dfeab068 | 3233 | |
0821bcd4 | 3234 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) |
0f113f3e MC |
3235 | { |
3236 | return (ctx->cert_store); | |
3237 | } | |
413c4f45 | 3238 | |
0f113f3e MC |
3239 | void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) |
3240 | { | |
3241 | if (ctx->cert_store != NULL) | |
3242 | X509_STORE_free(ctx->cert_store); | |
3243 | ctx->cert_store = store; | |
3244 | } | |
413c4f45 | 3245 | |
0821bcd4 | 3246 | int SSL_want(const SSL *s) |
0f113f3e MC |
3247 | { |
3248 | return (s->rwstate); | |
3249 | } | |
413c4f45 | 3250 | |
0f113f3e | 3251 | /** |
4f43d0e7 BL |
3252 | * \brief Set the callback for generating temporary RSA keys. |
3253 | * \param ctx the SSL context. | |
3254 | * \param cb the callback | |
3255 | */ | |
3256 | ||
bc36ee62 | 3257 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
3258 | void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb) (SSL *ssl, |
3259 | int is_export, | |
3260 | int keylength)) | |
3261 | { | |
3262 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb); | |
3263 | } | |
79df9d62 | 3264 | |
0f113f3e MC |
3265 | void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb) (SSL *ssl, |
3266 | int is_export, | |
3267 | int keylength)) | |
3268 | { | |
3269 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb); | |
3270 | } | |
79df9d62 | 3271 | #endif |
f8c3c05d | 3272 | |
4f43d0e7 | 3273 | #ifdef DOXYGEN |
0f113f3e | 3274 | /** |
4f43d0e7 BL |
3275 | * \brief The RSA temporary key callback function. |
3276 | * \param ssl the SSL session. | |
df63a389 UM |
3277 | * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite. |
3278 | * \param keylength if \c is_export is \c TRUE, then \c keylength is the size | |
3279 | * of the required key in bits. | |
4f43d0e7 BL |
3280 | * \return the temporary RSA key. |
3281 | * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback | |
3282 | */ | |
3283 | ||
0f113f3e MC |
3284 | RSA *cb(SSL *ssl, int is_export, int keylength) |
3285 | { | |
3286 | } | |
4f43d0e7 BL |
3287 | #endif |
3288 | ||
0f113f3e | 3289 | /** |
4f43d0e7 BL |
3290 | * \brief Set the callback for generating temporary DH keys. |
3291 | * \param ctx the SSL context. | |
3292 | * \param dh the callback | |
3293 | */ | |
3294 | ||
bc36ee62 | 3295 | #ifndef OPENSSL_NO_DH |
0f113f3e MC |
3296 | void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, |
3297 | DH *(*dh) (SSL *ssl, int is_export, | |
3298 | int keylength)) | |
3299 | { | |
3300 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh); | |
3301 | } | |
f8c3c05d | 3302 | |
0f113f3e MC |
3303 | void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh) (SSL *ssl, int is_export, |
3304 | int keylength)) | |
3305 | { | |
3306 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh); | |
3307 | } | |
79df9d62 | 3308 | #endif |
15d21c2d | 3309 | |
ea262260 | 3310 | #ifndef OPENSSL_NO_ECDH |
0f113f3e MC |
3311 | void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, |
3312 | EC_KEY *(*ecdh) (SSL *ssl, int is_export, | |
3313 | int keylength)) | |
3314 | { | |
3315 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, | |
3316 | (void (*)(void))ecdh); | |
3317 | } | |
ea262260 | 3318 | |
0f113f3e MC |
3319 | void SSL_set_tmp_ecdh_callback(SSL *ssl, |
3320 | EC_KEY *(*ecdh) (SSL *ssl, int is_export, | |
3321 | int keylength)) | |
3322 | { | |
3323 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh); | |
3324 | } | |
ea262260 BM |
3325 | #endif |
3326 | ||
ddac1974 NL |
3327 | #ifndef OPENSSL_NO_PSK |
3328 | int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) | |
0f113f3e MC |
3329 | { |
3330 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { | |
3331 | SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, | |
3332 | SSL_R_DATA_LENGTH_TOO_LONG); | |
3333 | return 0; | |
3334 | } | |
3335 | if (ctx->psk_identity_hint != NULL) | |
3336 | OPENSSL_free(ctx->psk_identity_hint); | |
3337 | if (identity_hint != NULL) { | |
3338 | ctx->psk_identity_hint = BUF_strdup(identity_hint); | |
3339 | if (ctx->psk_identity_hint == NULL) | |
3340 | return 0; | |
3341 | } else | |
3342 | ctx->psk_identity_hint = NULL; | |
3343 | return 1; | |
3344 | } | |
ddac1974 NL |
3345 | |
3346 | int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) | |
0f113f3e MC |
3347 | { |
3348 | if (s == NULL) | |
3349 | return 0; | |
3350 | ||
3351 | if (s->session == NULL) | |
3352 | return 1; /* session not created yet, ignored */ | |
3353 | ||
3354 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { | |
3355 | SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG); | |
3356 | return 0; | |
3357 | } | |
3358 | if (s->session->psk_identity_hint != NULL) | |
3359 | OPENSSL_free(s->session->psk_identity_hint); | |
3360 | if (identity_hint != NULL) { | |
3361 | s->session->psk_identity_hint = BUF_strdup(identity_hint); | |
3362 | if (s->session->psk_identity_hint == NULL) | |
3363 | return 0; | |
3364 | } else | |
3365 | s->session->psk_identity_hint = NULL; | |
3366 | return 1; | |
3367 | } | |
ddac1974 NL |
3368 | |
3369 | const char *SSL_get_psk_identity_hint(const SSL *s) | |
0f113f3e MC |
3370 | { |
3371 | if (s == NULL || s->session == NULL) | |
3372 | return NULL; | |
3373 | return (s->session->psk_identity_hint); | |
3374 | } | |
ddac1974 NL |
3375 | |
3376 | const char *SSL_get_psk_identity(const SSL *s) | |
0f113f3e MC |
3377 | { |
3378 | if (s == NULL || s->session == NULL) | |
3379 | return NULL; | |
3380 | return (s->session->psk_identity); | |
3381 | } | |
7806f3dd | 3382 | |
52b8dad8 | 3383 | void SSL_set_psk_client_callback(SSL *s, |
0f113f3e MC |
3384 | unsigned int (*cb) (SSL *ssl, |
3385 | const char *hint, | |
3386 | char *identity, | |
3387 | unsigned int | |
3388 | max_identity_len, | |
3389 | unsigned char *psk, | |
3390 | unsigned int | |
3391 | max_psk_len)) | |
3392 | { | |
3393 | s->psk_client_callback = cb; | |
3394 | } | |
7806f3dd NL |
3395 | |
3396 | void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, | |
0f113f3e MC |
3397 | unsigned int (*cb) (SSL *ssl, |
3398 | const char *hint, | |
3399 | char *identity, | |
3400 | unsigned int | |
3401 | max_identity_len, | |
3402 | unsigned char *psk, | |
3403 | unsigned int | |
3404 | max_psk_len)) | |
3405 | { | |
3406 | ctx->psk_client_callback = cb; | |
3407 | } | |
7806f3dd | 3408 | |
52b8dad8 | 3409 | void SSL_set_psk_server_callback(SSL *s, |
0f113f3e MC |
3410 | unsigned int (*cb) (SSL *ssl, |
3411 | const char *identity, | |
3412 | unsigned char *psk, | |
3413 | unsigned int | |
3414 | max_psk_len)) | |
3415 | { | |
3416 | s->psk_server_callback = cb; | |
3417 | } | |
7806f3dd NL |
3418 | |
3419 | void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, | |
0f113f3e MC |
3420 | unsigned int (*cb) (SSL *ssl, |
3421 | const char *identity, | |
3422 | unsigned char *psk, | |
3423 | unsigned int | |
3424 | max_psk_len)) | |
3425 | { | |
3426 | ctx->psk_server_callback = cb; | |
3427 | } | |
3428 | #endif | |
3429 | ||
3430 | void SSL_CTX_set_msg_callback(SSL_CTX *ctx, | |
3431 | void (*cb) (int write_p, int version, | |
3432 | int content_type, const void *buf, | |
3433 | size_t len, SSL *ssl, void *arg)) | |
3434 | { | |
3435 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); | |
3436 | } | |
3437 | ||
3438 | void SSL_set_msg_callback(SSL *ssl, | |
3439 | void (*cb) (int write_p, int version, | |
3440 | int content_type, const void *buf, | |
3441 | size_t len, SSL *ssl, void *arg)) | |
3442 | { | |
3443 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); | |
3444 | } | |
a661b653 | 3445 | |
7c2d4fee | 3446 | void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx, |
0f113f3e MC |
3447 | int (*cb) (SSL *ssl, |
3448 | int | |
3449 | is_forward_secure)) | |
3450 | { | |
3451 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB, | |
3452 | (void (*)(void))cb); | |
3453 | } | |
3454 | ||
7c2d4fee | 3455 | void SSL_set_not_resumable_session_callback(SSL *ssl, |
0f113f3e MC |
3456 | int (*cb) (SSL *ssl, |
3457 | int is_forward_secure)) | |
3458 | { | |
3459 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB, | |
3460 | (void (*)(void))cb); | |
3461 | } | |
3462 | ||
3463 | /* | |
3464 | * Allocates new EVP_MD_CTX and sets pointer to it into given pointer | |
3465 | * vairable, freeing EVP_MD_CTX previously stored in that variable, if any. | |
3466 | * If EVP_MD pointer is passed, initializes ctx with this md Returns newly | |
3467 | * allocated ctx; | |
8671b898 | 3468 | */ |
b948e2c5 | 3469 | |
0f113f3e | 3470 | EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md) |
b948e2c5 | 3471 | { |
0f113f3e MC |
3472 | ssl_clear_hash_ctx(hash); |
3473 | *hash = EVP_MD_CTX_create(); | |
3474 | if (md) | |
3475 | EVP_DigestInit_ex(*hash, md, NULL); | |
3476 | return *hash; | |
b948e2c5 | 3477 | } |
0f113f3e MC |
3478 | |
3479 | void ssl_clear_hash_ctx(EVP_MD_CTX **hash) | |
b948e2c5 DSH |
3480 | { |
3481 | ||
0f113f3e MC |
3482 | if (*hash) |
3483 | EVP_MD_CTX_destroy(*hash); | |
3484 | *hash = NULL; | |
b948e2c5 | 3485 | } |
a661b653 | 3486 | |
48fbcbac DSH |
3487 | /* Retrieve handshake hashes */ |
3488 | int ssl_handshake_hash(SSL *s, unsigned char *out, int outlen) | |
3489 | { | |
3490 | unsigned char *p = out; | |
3491 | int idx, ret = 0; | |
3492 | long mask; | |
3493 | EVP_MD_CTX ctx; | |
3494 | const EVP_MD *md; | |
3495 | EVP_MD_CTX_init(&ctx); | |
3496 | for (idx = 0; ssl_get_handshake_digest(idx, &mask, &md); idx++) { | |
3497 | if (mask & ssl_get_algorithm2(s)) { | |
3498 | int hashsize = EVP_MD_size(md); | |
3499 | EVP_MD_CTX *hdgst = s->s3->handshake_dgst[idx]; | |
3500 | if (!hdgst || hashsize < 0 || hashsize > outlen) | |
3501 | goto err; | |
3502 | if (!EVP_MD_CTX_copy_ex(&ctx, hdgst)) | |
3503 | goto err; | |
3504 | if (!EVP_DigestFinal_ex(&ctx, p, NULL)) | |
3505 | goto err; | |
3506 | p += hashsize; | |
3507 | outlen -= hashsize; | |
3508 | } | |
3509 | } | |
3510 | ret = p - out; | |
3511 | err: | |
3512 | EVP_MD_CTX_cleanup(&ctx); | |
3513 | return ret; | |
3514 | } | |
3515 | ||
08557cf2 | 3516 | void SSL_set_debug(SSL *s, int debug) |
0f113f3e MC |
3517 | { |
3518 | s->debug = debug; | |
3519 | } | |
08557cf2 DSH |
3520 | |
3521 | int SSL_cache_hit(SSL *s) | |
0f113f3e MC |
3522 | { |
3523 | return s->hit; | |
3524 | } | |
08557cf2 | 3525 | |
87adf1fa | 3526 | int SSL_is_server(SSL *s) |
0f113f3e MC |
3527 | { |
3528 | return s->server; | |
3529 | } | |
87adf1fa | 3530 | |
b362ccab | 3531 | void SSL_set_security_level(SSL *s, int level) |
0f113f3e MC |
3532 | { |
3533 | s->cert->sec_level = level; | |
3534 | } | |
b362ccab DSH |
3535 | |
3536 | int SSL_get_security_level(const SSL *s) | |
0f113f3e MC |
3537 | { |
3538 | return s->cert->sec_level; | |
3539 | } | |
b362ccab | 3540 | |
0f113f3e MC |
3541 | void SSL_set_security_callback(SSL *s, |
3542 | int (*cb) (SSL *s, SSL_CTX *ctx, int op, | |
3543 | int bits, int nid, void *other, | |
3544 | void *ex)) | |
3545 | { | |
3546 | s->cert->sec_cb = cb; | |
3547 | } | |
b362ccab | 3548 | |
0f113f3e MC |
3549 | int (*SSL_get_security_callback(const SSL *s)) (SSL *s, SSL_CTX *ctx, int op, |
3550 | int bits, int nid, | |
3551 | void *other, void *ex) { | |
3552 | return s->cert->sec_cb; | |
3553 | } | |
b362ccab DSH |
3554 | |
3555 | void SSL_set0_security_ex_data(SSL *s, void *ex) | |
0f113f3e MC |
3556 | { |
3557 | s->cert->sec_ex = ex; | |
3558 | } | |
b362ccab DSH |
3559 | |
3560 | void *SSL_get0_security_ex_data(const SSL *s) | |
0f113f3e MC |
3561 | { |
3562 | return s->cert->sec_ex; | |
3563 | } | |
b362ccab DSH |
3564 | |
3565 | void SSL_CTX_set_security_level(SSL_CTX *ctx, int level) | |
0f113f3e MC |
3566 | { |
3567 | ctx->cert->sec_level = level; | |
3568 | } | |
b362ccab DSH |
3569 | |
3570 | int SSL_CTX_get_security_level(const SSL_CTX *ctx) | |
0f113f3e MC |
3571 | { |
3572 | return ctx->cert->sec_level; | |
3573 | } | |
b362ccab | 3574 | |
0f113f3e MC |
3575 | void SSL_CTX_set_security_callback(SSL_CTX *ctx, |
3576 | int (*cb) (SSL *s, SSL_CTX *ctx, int op, | |
3577 | int bits, int nid, void *other, | |
3578 | void *ex)) | |
3579 | { | |
3580 | ctx->cert->sec_cb = cb; | |
3581 | } | |
b362ccab | 3582 | |
0f113f3e MC |
3583 | int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (SSL *s, |
3584 | SSL_CTX *ctx, | |
3585 | int op, int bits, | |
3586 | int nid, | |
3587 | void *other, | |
3588 | void *ex) { | |
3589 | return ctx->cert->sec_cb; | |
3590 | } | |
b362ccab DSH |
3591 | |
3592 | void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex) | |
0f113f3e MC |
3593 | { |
3594 | ctx->cert->sec_ex = ex; | |
3595 | } | |
b362ccab DSH |
3596 | |
3597 | void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx) | |
0f113f3e MC |
3598 | { |
3599 | return ctx->cert->sec_ex; | |
3600 | } | |
b362ccab | 3601 | |
0f113f3e | 3602 | IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id); |