]>
Commit | Line | Data |
---|---|---|
4f43d0e7 BL |
1 | /*! \file ssl/ssl_lib.c |
2 | * \brief Version independent SSL functions. | |
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. | |
10 | * | |
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). | |
17 | * | |
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. | |
24 | * | |
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 :-). | |
39 | * 4. If you include any Windows specific code (or a derivative thereof) from | |
40 | * the apps directory (application code) you must include an acknowledgement: | |
41 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
42 | * | |
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. | |
54 | * | |
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 | |
68 | * notice, this list of conditions and the following disclaimer. | |
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. | |
115 | * ECC cipher suite support in OpenSSL originally developed by | |
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 BM |
145 | #ifdef REF_CHECK |
146 | # include <assert.h> | |
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 |
60a938c6 | 157 | #include <openssl/dh.h> |
3eeaab4b | 158 | #endif |
d02b48c6 | 159 | |
9d1a01be | 160 | const char *SSL_version_str=OPENSSL_VERSION_TEXT; |
58964a49 | 161 | |
3ed449e9 | 162 | SSL3_ENC_METHOD ssl3_undef_enc_method={ |
dd9d233e | 163 | /* evil casts, but these functions are only called if there's a library bug */ |
245206ea BM |
164 | (int (*)(SSL *,int))ssl_undefined_function, |
165 | (int (*)(SSL *, unsigned char *, int))ssl_undefined_function, | |
58964a49 | 166 | ssl_undefined_function, |
245206ea BM |
167 | (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function, |
168 | (int (*)(SSL*, int))ssl_undefined_function, | |
81025661 | 169 | (int (*)(SSL *, const char*, int, unsigned char *))ssl_undefined_function, |
8215e7a9 | 170 | 0, /* finish_mac_length */ |
8164032a | 171 | (int (*)(SSL *, int, unsigned char *))ssl_undefined_function, |
8215e7a9 NL |
172 | NULL, /* client_finished_label */ |
173 | 0, /* client_finished_label_len */ | |
174 | NULL, /* server_finished_label */ | |
175 | 0, /* server_finished_label_len */ | |
176 | (int (*)(int))ssl_undefined_function | |
58964a49 | 177 | }; |
d02b48c6 | 178 | |
4f43d0e7 | 179 | int SSL_clear(SSL *s) |
d02b48c6 | 180 | { |
d02b48c6 | 181 | |
413c4f45 MC |
182 | if (s->method == NULL) |
183 | { | |
184 | SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED); | |
185 | return(0); | |
186 | } | |
d02b48c6 | 187 | |
d62bfb39 LJ |
188 | if (ssl_clear_bad_session(s)) |
189 | { | |
190 | SSL_SESSION_free(s->session); | |
191 | s->session=NULL; | |
192 | } | |
193 | ||
d02b48c6 RE |
194 | s->error=0; |
195 | s->hit=0; | |
413c4f45 | 196 | s->shutdown=0; |
d02b48c6 | 197 | |
a2a01589 BM |
198 | #if 0 /* Disabled since version 1.10 of this file (early return not |
199 | * needed because SSL_clear is not called when doing renegotiation) */ | |
d02b48c6 RE |
200 | /* This is set if we are doing dynamic renegotiation so keep |
201 | * the old cipher. It is sort of a SSL_clear_lite :-) */ | |
413c4f45 | 202 | if (s->new_session) return(1); |
a2a01589 BM |
203 | #else |
204 | if (s->new_session) | |
205 | { | |
5277d7cb | 206 | SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR); |
a2a01589 BM |
207 | return 0; |
208 | } | |
413c4f45 | 209 | #endif |
d02b48c6 | 210 | |
d02b48c6 RE |
211 | s->type=0; |
212 | ||
413c4f45 MC |
213 | s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT); |
214 | ||
d02b48c6 | 215 | s->version=s->method->version; |
413c4f45 | 216 | s->client_version=s->version; |
d02b48c6 | 217 | s->rwstate=SSL_NOTHING; |
d02b48c6 | 218 | s->rstate=SSL_ST_READ_HEADER; |
544ebbce | 219 | #if 0 |
413c4f45 | 220 | s->read_ahead=s->ctx->read_ahead; |
544ebbce | 221 | #endif |
d02b48c6 RE |
222 | |
223 | if (s->init_buf != NULL) | |
224 | { | |
225 | BUF_MEM_free(s->init_buf); | |
226 | s->init_buf=NULL; | |
227 | } | |
228 | ||
229 | ssl_clear_cipher_ctx(s); | |
b948e2c5 DSH |
230 | ssl_clear_hash_ctx(&s->read_hash); |
231 | ssl_clear_hash_ctx(&s->write_hash); | |
d02b48c6 | 232 | |
d02b48c6 RE |
233 | s->first_packet=0; |
234 | ||
413c4f45 MC |
235 | #if 1 |
236 | /* Check to see if we were changed into a different method, if | |
237 | * so, revert back if we are not doing session-id reuse. */ | |
979689aa | 238 | if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method)) |
413c4f45 MC |
239 | { |
240 | s->method->ssl_free(s); | |
241 | s->method=s->ctx->method; | |
242 | if (!s->method->ssl_new(s)) | |
243 | return(0); | |
244 | } | |
245 | else | |
246 | #endif | |
247 | s->method->ssl_clear(s); | |
248 | return(1); | |
d02b48c6 RE |
249 | } |
250 | ||
4f43d0e7 | 251 | /** Used to change an SSL_CTXs default SSL method type */ |
4ebb342f | 252 | int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth) |
d02b48c6 | 253 | { |
f73e07cf | 254 | STACK_OF(SSL_CIPHER) *sk; |
d02b48c6 RE |
255 | |
256 | ctx->method=meth; | |
257 | ||
258 | sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list), | |
259 | &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST); | |
f73e07cf | 260 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) |
d02b48c6 RE |
261 | { |
262 | SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS); | |
263 | return(0); | |
264 | } | |
265 | return(1); | |
266 | } | |
267 | ||
4f43d0e7 | 268 | SSL *SSL_new(SSL_CTX *ctx) |
d02b48c6 RE |
269 | { |
270 | SSL *s; | |
271 | ||
272 | if (ctx == NULL) | |
273 | { | |
274 | SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX); | |
275 | return(NULL); | |
276 | } | |
277 | if (ctx->method == NULL) | |
278 | { | |
279 | SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION); | |
280 | return(NULL); | |
281 | } | |
282 | ||
26a3a48d | 283 | s=(SSL *)OPENSSL_malloc(sizeof(SSL)); |
d02b48c6 RE |
284 | if (s == NULL) goto err; |
285 | memset(s,0,sizeof(SSL)); | |
286 | ||
bc36ee62 | 287 | #ifndef OPENSSL_NO_KRB5 |
f9b3bff6 | 288 | s->kssl_ctx = kssl_ctx_new(); |
bc36ee62 | 289 | #endif /* OPENSSL_NO_KRB5 */ |
f9b3bff6 | 290 | |
bf21446a BM |
291 | s->options=ctx->options; |
292 | s->mode=ctx->mode; | |
293 | s->max_cert_list=ctx->max_cert_list; | |
294 | ||
ca8e5b9b | 295 | if (ctx->cert != NULL) |
d02b48c6 | 296 | { |
ca8e5b9b BM |
297 | /* Earlier library versions used to copy the pointer to |
298 | * the CERT, not its contents; only when setting new | |
299 | * parameters for the per-SSL copy, ssl_cert_new would be | |
300 | * called (and the direct reference to the per-SSL_CTX | |
301 | * settings would be lost, but those still were indirectly | |
302 | * accessed for various purposes, and for that reason they | |
303 | * used to be known as s->ctx->default_cert). | |
304 | * Now we don't look at the SSL_CTX's CERT after having | |
305 | * duplicated it once. */ | |
306 | ||
307 | s->cert = ssl_cert_dup(ctx->cert); | |
308 | if (s->cert == NULL) | |
309 | goto err; | |
d02b48c6 RE |
310 | } |
311 | else | |
ca8e5b9b | 312 | s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */ |
bf21446a BM |
313 | |
314 | s->read_ahead=ctx->read_ahead; | |
315 | s->msg_callback=ctx->msg_callback; | |
316 | s->msg_callback_arg=ctx->msg_callback_arg; | |
413c4f45 | 317 | s->verify_mode=ctx->verify_mode; |
5d7c222d | 318 | #if 0 |
7f89714e | 319 | s->verify_depth=ctx->verify_depth; |
5d7c222d | 320 | #endif |
bf21446a | 321 | s->sid_ctx_length=ctx->sid_ctx_length; |
54a656ef | 322 | OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx); |
bf21446a | 323 | memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx)); |
d02b48c6 | 324 | s->verify_callback=ctx->default_verify_callback; |
dc644fe2 | 325 | s->generate_session_id=ctx->generate_session_id; |
5d7c222d DSH |
326 | |
327 | s->param = X509_VERIFY_PARAM_new(); | |
328 | if (!s->param) | |
329 | goto err; | |
330 | X509_VERIFY_PARAM_inherit(s->param, ctx->param); | |
331 | #if 0 | |
13938ace DSH |
332 | s->purpose = ctx->purpose; |
333 | s->trust = ctx->trust; | |
5d7c222d | 334 | #endif |
bf21446a | 335 | s->quiet_shutdown=ctx->quiet_shutdown; |
566dda07 | 336 | s->max_send_fragment = ctx->max_send_fragment; |
bf21446a | 337 | |
d02b48c6 RE |
338 | CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); |
339 | s->ctx=ctx; | |
1aeb3da8 | 340 | #ifndef OPENSSL_NO_TLSEXT |
6434abbf DSH |
341 | s->tlsext_debug_cb = 0; |
342 | s->tlsext_debug_arg = NULL; | |
343 | s->tlsext_ticket_expected = 0; | |
67c8e7f4 DSH |
344 | s->tlsext_status_type = -1; |
345 | s->tlsext_status_expected = 0; | |
346 | s->tlsext_ocsp_ids = NULL; | |
347 | s->tlsext_ocsp_exts = NULL; | |
348 | s->tlsext_ocsp_resp = NULL; | |
349 | s->tlsext_ocsp_resplen = -1; | |
1aeb3da8 BM |
350 | CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); |
351 | s->initial_ctx=ctx; | |
352 | #endif | |
d02b48c6 RE |
353 | |
354 | s->verify_result=X509_V_OK; | |
355 | ||
356 | s->method=ctx->method; | |
357 | ||
358 | if (!s->method->ssl_new(s)) | |
d02b48c6 | 359 | goto err; |
d02b48c6 | 360 | |
58964a49 | 361 | s->references=1; |
413c4f45 | 362 | s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1; |
bf21446a | 363 | |
d02b48c6 | 364 | SSL_clear(s); |
58964a49 | 365 | |
79aa04ef | 366 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); |
58964a49 | 367 | |
ddac1974 NL |
368 | #ifndef OPENSSL_NO_PSK |
369 | s->psk_client_callback=ctx->psk_client_callback; | |
370 | s->psk_server_callback=ctx->psk_server_callback; | |
371 | #endif | |
372 | ||
d02b48c6 RE |
373 | return(s); |
374 | err: | |
ca8e5b9b BM |
375 | if (s != NULL) |
376 | { | |
377 | if (s->cert != NULL) | |
378 | ssl_cert_free(s->cert); | |
379 | if (s->ctx != NULL) | |
380 | SSL_CTX_free(s->ctx); /* decrement reference count */ | |
26a3a48d | 381 | OPENSSL_free(s); |
ca8e5b9b | 382 | } |
d02b48c6 RE |
383 | SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE); |
384 | return(NULL); | |
385 | } | |
386 | ||
4eb77b26 BM |
387 | int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx, |
388 | unsigned int sid_ctx_len) | |
389 | { | |
54a656ef | 390 | if(sid_ctx_len > sizeof ctx->sid_ctx) |
4eb77b26 BM |
391 | { |
392 | SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); | |
393 | return 0; | |
394 | } | |
395 | ctx->sid_ctx_length=sid_ctx_len; | |
396 | memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len); | |
397 | ||
398 | return 1; | |
399 | } | |
400 | ||
b4cadc6e BL |
401 | int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx, |
402 | unsigned int sid_ctx_len) | |
403 | { | |
404 | if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) | |
405 | { | |
406 | SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); | |
407 | return 0; | |
408 | } | |
409 | ssl->sid_ctx_length=sid_ctx_len; | |
410 | memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len); | |
411 | ||
412 | return 1; | |
413 | } | |
414 | ||
dc644fe2 GT |
415 | int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) |
416 | { | |
417 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); | |
418 | ctx->generate_session_id = cb; | |
419 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); | |
420 | return 1; | |
421 | } | |
422 | ||
423 | int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) | |
424 | { | |
425 | CRYPTO_w_lock(CRYPTO_LOCK_SSL); | |
426 | ssl->generate_session_id = cb; | |
427 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL); | |
428 | return 1; | |
429 | } | |
430 | ||
f85c9904 | 431 | int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id, |
dc644fe2 GT |
432 | unsigned int id_len) |
433 | { | |
434 | /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how | |
435 | * we can "construct" a session to give us the desired check - ie. to | |
436 | * find if there's a session in the hash table that would conflict with | |
437 | * any new session built out of this id/id_len and the ssl_version in | |
f85c9904 | 438 | * use by this SSL. */ |
dc644fe2 | 439 | SSL_SESSION r, *p; |
54a656ef BL |
440 | |
441 | if(id_len > sizeof r.session_id) | |
442 | return 0; | |
443 | ||
f85c9904 | 444 | r.ssl_version = ssl->version; |
dc644fe2 GT |
445 | r.session_id_length = id_len; |
446 | memcpy(r.session_id, id, id_len); | |
ec0f1959 GT |
447 | /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a |
448 | * callback is calling us to check the uniqueness of a shorter ID, it | |
449 | * must be compared as a padded-out ID because that is what it will be | |
450 | * converted to when the callback has finished choosing it. */ | |
451 | if((r.ssl_version == SSL2_VERSION) && | |
452 | (id_len < SSL2_SSL_SESSION_ID_LENGTH)) | |
453 | { | |
454 | memset(r.session_id + id_len, 0, | |
455 | SSL2_SSL_SESSION_ID_LENGTH - id_len); | |
456 | r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH; | |
457 | } | |
dc644fe2 GT |
458 | |
459 | CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); | |
f85c9904 | 460 | p = (SSL_SESSION *)lh_retrieve(ssl->ctx->sessions, &r); |
dc644fe2 GT |
461 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); |
462 | return (p != NULL); | |
463 | } | |
464 | ||
bb7cd4e3 | 465 | int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) |
bf21446a | 466 | { |
5d7c222d | 467 | return X509_VERIFY_PARAM_set_purpose(s->param, purpose); |
bf21446a | 468 | } |
bb7cd4e3 DSH |
469 | |
470 | int SSL_set_purpose(SSL *s, int purpose) | |
bf21446a | 471 | { |
5d7c222d | 472 | return X509_VERIFY_PARAM_set_purpose(s->param, purpose); |
bf21446a | 473 | } |
926a56bf | 474 | |
bb7cd4e3 | 475 | int SSL_CTX_set_trust(SSL_CTX *s, int trust) |
bf21446a | 476 | { |
5d7c222d | 477 | return X509_VERIFY_PARAM_set_trust(s->param, trust); |
bf21446a | 478 | } |
bb7cd4e3 DSH |
479 | |
480 | int SSL_set_trust(SSL *s, int trust) | |
bf21446a | 481 | { |
5d7c222d | 482 | return X509_VERIFY_PARAM_set_trust(s->param, trust); |
bf21446a | 483 | } |
bb7cd4e3 | 484 | |
4f43d0e7 | 485 | void SSL_free(SSL *s) |
d02b48c6 | 486 | { |
58964a49 RE |
487 | int i; |
488 | ||
e03ddfae BL |
489 | if(s == NULL) |
490 | return; | |
491 | ||
58964a49 RE |
492 | i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL); |
493 | #ifdef REF_PRINT | |
494 | REF_PRINT("SSL",s); | |
495 | #endif | |
496 | if (i > 0) return; | |
497 | #ifdef REF_CHECK | |
498 | if (i < 0) | |
499 | { | |
500 | fprintf(stderr,"SSL_free, bad reference count\n"); | |
501 | abort(); /* ok */ | |
502 | } | |
503 | #endif | |
504 | ||
5d7c222d DSH |
505 | if (s->param) |
506 | X509_VERIFY_PARAM_free(s->param); | |
507 | ||
79aa04ef | 508 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); |
58964a49 | 509 | |
d02b48c6 RE |
510 | if (s->bbio != NULL) |
511 | { | |
512 | /* If the buffering BIO is in place, pop it off */ | |
513 | if (s->bbio == s->wbio) | |
514 | { | |
515 | s->wbio=BIO_pop(s->wbio); | |
516 | } | |
517 | BIO_free(s->bbio); | |
58964a49 | 518 | s->bbio=NULL; |
d02b48c6 RE |
519 | } |
520 | if (s->rbio != NULL) | |
521 | BIO_free_all(s->rbio); | |
522 | if ((s->wbio != NULL) && (s->wbio != s->rbio)) | |
523 | BIO_free_all(s->wbio); | |
524 | ||
525 | if (s->init_buf != NULL) BUF_MEM_free(s->init_buf); | |
526 | ||
527 | /* add extra stuff */ | |
f73e07cf BL |
528 | if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list); |
529 | if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id); | |
d02b48c6 RE |
530 | |
531 | /* Make the next call work :-) */ | |
532 | if (s->session != NULL) | |
533 | { | |
534 | ssl_clear_bad_session(s); | |
535 | SSL_SESSION_free(s->session); | |
536 | } | |
537 | ||
538 | ssl_clear_cipher_ctx(s); | |
b948e2c5 DSH |
539 | ssl_clear_hash_ctx(&s->read_hash); |
540 | ssl_clear_hash_ctx(&s->write_hash); | |
d02b48c6 RE |
541 | |
542 | if (s->cert != NULL) ssl_cert_free(s->cert); | |
543 | /* Free up if allocated */ | |
544 | ||
545 | if (s->ctx) SSL_CTX_free(s->ctx); | |
1aeb3da8 BM |
546 | #ifndef OPENSSL_NO_TLSEXT |
547 | if (s->initial_ctx) SSL_CTX_free(s->initial_ctx); | |
dc1d1b69 BM |
548 | #ifndef OPENSSL_NO_EC |
549 | if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist); | |
550 | if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist); | |
551 | #endif /* OPENSSL_NO_EC */ | |
761772d7 | 552 | if (s->tlsext_opaque_prf_input) OPENSSL_free(s->tlsext_opaque_prf_input); |
67c8e7f4 DSH |
553 | if (s->tlsext_ocsp_exts) |
554 | sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, | |
555 | X509_EXTENSION_free); | |
556 | if (s->tlsext_ocsp_ids) | |
557 | sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); | |
558 | if (s->tlsext_ocsp_resp) | |
559 | OPENSSL_free(s->tlsext_ocsp_resp); | |
1aeb3da8 | 560 | #endif |
d02b48c6 RE |
561 | |
562 | if (s->client_CA != NULL) | |
f73e07cf | 563 | sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free); |
d02b48c6 RE |
564 | |
565 | if (s->method != NULL) s->method->ssl_free(s); | |
566 | ||
0e6c20da RL |
567 | #ifndef OPENSSL_NO_KRB5 |
568 | if (s->kssl_ctx != NULL) | |
569 | kssl_ctx_free(s->kssl_ctx); | |
570 | #endif /* OPENSSL_NO_KRB5 */ | |
571 | ||
26a3a48d | 572 | OPENSSL_free(s); |
d02b48c6 RE |
573 | } |
574 | ||
4f43d0e7 | 575 | void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio) |
d02b48c6 RE |
576 | { |
577 | /* If the output buffering BIO is still in place, remove it | |
578 | */ | |
579 | if (s->bbio != NULL) | |
580 | { | |
581 | if (s->wbio == s->bbio) | |
582 | { | |
583 | s->wbio=s->wbio->next_bio; | |
584 | s->bbio->next_bio=NULL; | |
585 | } | |
586 | } | |
587 | if ((s->rbio != NULL) && (s->rbio != rbio)) | |
588 | BIO_free_all(s->rbio); | |
589 | if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio)) | |
590 | BIO_free_all(s->wbio); | |
591 | s->rbio=rbio; | |
592 | s->wbio=wbio; | |
593 | } | |
594 | ||
0821bcd4 | 595 | BIO *SSL_get_rbio(const SSL *s) |
d02b48c6 RE |
596 | { return(s->rbio); } |
597 | ||
0821bcd4 | 598 | BIO *SSL_get_wbio(const SSL *s) |
d02b48c6 RE |
599 | { return(s->wbio); } |
600 | ||
0821bcd4 | 601 | int SSL_get_fd(const SSL *s) |
24cbf3ef | 602 | { |
fa293e4e | 603 | return(SSL_get_rfd(s)); |
24cbf3ef RL |
604 | } |
605 | ||
0821bcd4 | 606 | int SSL_get_rfd(const SSL *s) |
d02b48c6 RE |
607 | { |
608 | int ret= -1; | |
609 | BIO *b,*r; | |
610 | ||
611 | b=SSL_get_rbio(s); | |
612 | r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); | |
613 | if (r != NULL) | |
614 | BIO_get_fd(r,&ret); | |
615 | return(ret); | |
616 | } | |
617 | ||
0821bcd4 | 618 | int SSL_get_wfd(const SSL *s) |
24cbf3ef RL |
619 | { |
620 | int ret= -1; | |
621 | BIO *b,*r; | |
622 | ||
623 | b=SSL_get_wbio(s); | |
624 | r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); | |
625 | if (r != NULL) | |
626 | BIO_get_fd(r,&ret); | |
627 | return(ret); | |
628 | } | |
629 | ||
bc36ee62 | 630 | #ifndef OPENSSL_NO_SOCK |
4f43d0e7 | 631 | int SSL_set_fd(SSL *s,int fd) |
d02b48c6 RE |
632 | { |
633 | int ret=0; | |
634 | BIO *bio=NULL; | |
635 | ||
636 | bio=BIO_new(BIO_s_socket()); | |
637 | ||
638 | if (bio == NULL) | |
639 | { | |
640 | SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB); | |
641 | goto err; | |
642 | } | |
643 | BIO_set_fd(bio,fd,BIO_NOCLOSE); | |
644 | SSL_set_bio(s,bio,bio); | |
645 | ret=1; | |
646 | err: | |
647 | return(ret); | |
648 | } | |
649 | ||
4f43d0e7 | 650 | int SSL_set_wfd(SSL *s,int fd) |
d02b48c6 RE |
651 | { |
652 | int ret=0; | |
653 | BIO *bio=NULL; | |
654 | ||
58964a49 RE |
655 | if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET) |
656 | || ((int)BIO_get_fd(s->rbio,NULL) != fd)) | |
657 | { | |
658 | bio=BIO_new(BIO_s_socket()); | |
d02b48c6 | 659 | |
58964a49 RE |
660 | if (bio == NULL) |
661 | { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; } | |
662 | BIO_set_fd(bio,fd,BIO_NOCLOSE); | |
663 | SSL_set_bio(s,SSL_get_rbio(s),bio); | |
664 | } | |
665 | else | |
666 | SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s)); | |
d02b48c6 RE |
667 | ret=1; |
668 | err: | |
669 | return(ret); | |
670 | } | |
671 | ||
4f43d0e7 | 672 | int SSL_set_rfd(SSL *s,int fd) |
d02b48c6 RE |
673 | { |
674 | int ret=0; | |
675 | BIO *bio=NULL; | |
676 | ||
58964a49 RE |
677 | if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET) |
678 | || ((int)BIO_get_fd(s->wbio,NULL) != fd)) | |
d02b48c6 | 679 | { |
58964a49 RE |
680 | bio=BIO_new(BIO_s_socket()); |
681 | ||
682 | if (bio == NULL) | |
683 | { | |
684 | SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB); | |
685 | goto err; | |
686 | } | |
687 | BIO_set_fd(bio,fd,BIO_NOCLOSE); | |
688 | SSL_set_bio(s,bio,SSL_get_wbio(s)); | |
d02b48c6 | 689 | } |
58964a49 RE |
690 | else |
691 | SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s)); | |
d02b48c6 RE |
692 | ret=1; |
693 | err: | |
694 | return(ret); | |
695 | } | |
696 | #endif | |
697 | ||
ca03109c BM |
698 | |
699 | /* return length of latest Finished message we sent, copy to 'buf' */ | |
0821bcd4 | 700 | size_t SSL_get_finished(const SSL *s, void *buf, size_t count) |
ca03109c BM |
701 | { |
702 | size_t ret = 0; | |
703 | ||
704 | if (s->s3 != NULL) | |
705 | { | |
706 | ret = s->s3->tmp.finish_md_len; | |
707 | if (count > ret) | |
708 | count = ret; | |
709 | memcpy(buf, s->s3->tmp.finish_md, count); | |
710 | } | |
711 | return ret; | |
712 | } | |
713 | ||
714 | /* return length of latest Finished message we expected, copy to 'buf' */ | |
0821bcd4 | 715 | size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) |
ca03109c BM |
716 | { |
717 | size_t ret = 0; | |
718 | ||
719 | if (s->s3 != NULL) | |
720 | { | |
721 | ret = s->s3->tmp.peer_finish_md_len; | |
722 | if (count > ret) | |
723 | count = ret; | |
724 | memcpy(buf, s->s3->tmp.peer_finish_md, count); | |
725 | } | |
726 | return ret; | |
727 | } | |
728 | ||
729 | ||
0821bcd4 | 730 | int SSL_get_verify_mode(const SSL *s) |
d02b48c6 RE |
731 | { |
732 | return(s->verify_mode); | |
733 | } | |
734 | ||
0821bcd4 | 735 | int SSL_get_verify_depth(const SSL *s) |
7f89714e | 736 | { |
5d7c222d | 737 | return X509_VERIFY_PARAM_get_depth(s->param); |
7f89714e BM |
738 | } |
739 | ||
0821bcd4 | 740 | int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *) |
d02b48c6 RE |
741 | { |
742 | return(s->verify_callback); | |
743 | } | |
744 | ||
0821bcd4 | 745 | int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) |
d02b48c6 | 746 | { |
413c4f45 | 747 | return(ctx->verify_mode); |
d02b48c6 RE |
748 | } |
749 | ||
0821bcd4 | 750 | int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) |
7f89714e | 751 | { |
5d7c222d | 752 | return X509_VERIFY_PARAM_get_depth(ctx->param); |
7f89714e BM |
753 | } |
754 | ||
0821bcd4 | 755 | int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *) |
d02b48c6 RE |
756 | { |
757 | return(ctx->default_verify_callback); | |
758 | } | |
759 | ||
49bc2624 BL |
760 | void SSL_set_verify(SSL *s,int mode, |
761 | int (*callback)(int ok,X509_STORE_CTX *ctx)) | |
d02b48c6 RE |
762 | { |
763 | s->verify_mode=mode; | |
764 | if (callback != NULL) | |
765 | s->verify_callback=callback; | |
766 | } | |
767 | ||
7f89714e BM |
768 | void SSL_set_verify_depth(SSL *s,int depth) |
769 | { | |
5d7c222d | 770 | X509_VERIFY_PARAM_set_depth(s->param, depth); |
7f89714e BM |
771 | } |
772 | ||
4f43d0e7 | 773 | void SSL_set_read_ahead(SSL *s,int yes) |
d02b48c6 RE |
774 | { |
775 | s->read_ahead=yes; | |
776 | } | |
777 | ||
0821bcd4 | 778 | int SSL_get_read_ahead(const SSL *s) |
d02b48c6 RE |
779 | { |
780 | return(s->read_ahead); | |
781 | } | |
782 | ||
0821bcd4 | 783 | int SSL_pending(const SSL *s) |
d02b48c6 | 784 | { |
24b44446 BM |
785 | /* SSL_pending cannot work properly if read-ahead is enabled |
786 | * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), | |
787 | * and it is impossible to fix since SSL_pending cannot report | |
788 | * errors that may be observed while scanning the new data. | |
789 | * (Note that SSL_pending() is often used as a boolean value, | |
790 | * so we'd better not return -1.) | |
791 | */ | |
d02b48c6 RE |
792 | return(s->method->ssl_pending(s)); |
793 | } | |
794 | ||
0821bcd4 | 795 | X509 *SSL_get_peer_certificate(const SSL *s) |
d02b48c6 RE |
796 | { |
797 | X509 *r; | |
798 | ||
799 | if ((s == NULL) || (s->session == NULL)) | |
800 | r=NULL; | |
801 | else | |
802 | r=s->session->peer; | |
803 | ||
804 | if (r == NULL) return(r); | |
805 | ||
806 | CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509); | |
807 | ||
808 | return(r); | |
809 | } | |
810 | ||
0821bcd4 | 811 | STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) |
d02b48c6 | 812 | { |
f73e07cf | 813 | STACK_OF(X509) *r; |
d02b48c6 | 814 | |
9d5cceac | 815 | if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL)) |
d02b48c6 RE |
816 | r=NULL; |
817 | else | |
9d5cceac | 818 | r=s->session->sess_cert->cert_chain; |
d02b48c6 | 819 | |
98e04f9e BM |
820 | /* If we are a client, cert_chain includes the peer's own |
821 | * certificate; if we are a server, it does not. */ | |
822 | ||
d02b48c6 RE |
823 | return(r); |
824 | } | |
825 | ||
826 | /* Now in theory, since the calling process own 't' it should be safe to | |
827 | * modify. We need to be able to read f without being hassled */ | |
0821bcd4 | 828 | void SSL_copy_session_id(SSL *t,const SSL *f) |
d02b48c6 RE |
829 | { |
830 | CERT *tmp; | |
831 | ||
832 | /* Do we need to to SSL locking? */ | |
833 | SSL_set_session(t,SSL_get_session(f)); | |
834 | ||
835 | /* what if we are setup as SSLv2 but want to talk SSLv3 or | |
836 | * vice-versa */ | |
837 | if (t->method != f->method) | |
838 | { | |
839 | t->method->ssl_free(t); /* cleanup current */ | |
840 | t->method=f->method; /* change method */ | |
841 | t->method->ssl_new(t); /* setup new */ | |
842 | } | |
843 | ||
844 | tmp=t->cert; | |
845 | if (f->cert != NULL) | |
846 | { | |
847 | CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT); | |
848 | t->cert=f->cert; | |
849 | } | |
850 | else | |
851 | t->cert=NULL; | |
852 | if (tmp != NULL) ssl_cert_free(tmp); | |
b4cadc6e | 853 | SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length); |
d02b48c6 RE |
854 | } |
855 | ||
58964a49 | 856 | /* Fix this so it checks all the valid key/cert options */ |
0821bcd4 | 857 | int SSL_CTX_check_private_key(const SSL_CTX *ctx) |
d02b48c6 RE |
858 | { |
859 | if ( (ctx == NULL) || | |
ca8e5b9b BM |
860 | (ctx->cert == NULL) || |
861 | (ctx->cert->key->x509 == NULL)) | |
d02b48c6 RE |
862 | { |
863 | SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); | |
864 | return(0); | |
865 | } | |
ca8e5b9b | 866 | if (ctx->cert->key->privatekey == NULL) |
d02b48c6 RE |
867 | { |
868 | SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); | |
869 | return(0); | |
870 | } | |
ca8e5b9b | 871 | return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey)); |
d02b48c6 RE |
872 | } |
873 | ||
58964a49 | 874 | /* Fix this function so that it takes an optional type parameter */ |
0821bcd4 | 875 | int SSL_check_private_key(const SSL *ssl) |
d02b48c6 RE |
876 | { |
877 | if (ssl == NULL) | |
878 | { | |
879 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER); | |
880 | return(0); | |
881 | } | |
f3e67ac1 | 882 | if (ssl->cert == NULL) |
2b8e4959 | 883 | { |
52b8dad8 | 884 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); |
f3e67ac1 | 885 | return 0; |
2b8e4959 | 886 | } |
d02b48c6 RE |
887 | if (ssl->cert->key->x509 == NULL) |
888 | { | |
889 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); | |
890 | return(0); | |
891 | } | |
892 | if (ssl->cert->key->privatekey == NULL) | |
893 | { | |
894 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); | |
895 | return(0); | |
896 | } | |
897 | return(X509_check_private_key(ssl->cert->key->x509, | |
898 | ssl->cert->key->privatekey)); | |
899 | } | |
900 | ||
4f43d0e7 | 901 | int SSL_accept(SSL *s) |
d02b48c6 | 902 | { |
b31b04d9 BM |
903 | if (s->handshake_func == 0) |
904 | /* Not properly initialized yet */ | |
905 | SSL_set_accept_state(s); | |
906 | ||
d02b48c6 RE |
907 | return(s->method->ssl_accept(s)); |
908 | } | |
909 | ||
4f43d0e7 | 910 | int SSL_connect(SSL *s) |
d02b48c6 | 911 | { |
b31b04d9 BM |
912 | if (s->handshake_func == 0) |
913 | /* Not properly initialized yet */ | |
914 | SSL_set_connect_state(s); | |
915 | ||
d02b48c6 RE |
916 | return(s->method->ssl_connect(s)); |
917 | } | |
918 | ||
0821bcd4 | 919 | long SSL_get_default_timeout(const SSL *s) |
d02b48c6 RE |
920 | { |
921 | return(s->method->get_timeout()); | |
922 | } | |
923 | ||
e34cfcf7 | 924 | int SSL_read(SSL *s,void *buf,int num) |
d02b48c6 | 925 | { |
b31b04d9 BM |
926 | if (s->handshake_func == 0) |
927 | { | |
ff712220 | 928 | SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED); |
b31b04d9 BM |
929 | return -1; |
930 | } | |
931 | ||
d02b48c6 RE |
932 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) |
933 | { | |
934 | s->rwstate=SSL_NOTHING; | |
935 | return(0); | |
936 | } | |
937 | return(s->method->ssl_read(s,buf,num)); | |
938 | } | |
939 | ||
e34cfcf7 | 940 | int SSL_peek(SSL *s,void *buf,int num) |
d02b48c6 | 941 | { |
5451e0d9 BM |
942 | if (s->handshake_func == 0) |
943 | { | |
aa4ce731 | 944 | SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED); |
5451e0d9 BM |
945 | return -1; |
946 | } | |
947 | ||
d02b48c6 RE |
948 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) |
949 | { | |
950 | return(0); | |
951 | } | |
952 | return(s->method->ssl_peek(s,buf,num)); | |
953 | } | |
954 | ||
e34cfcf7 | 955 | int SSL_write(SSL *s,const void *buf,int num) |
d02b48c6 | 956 | { |
b31b04d9 BM |
957 | if (s->handshake_func == 0) |
958 | { | |
ff712220 | 959 | SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED); |
b31b04d9 BM |
960 | return -1; |
961 | } | |
962 | ||
d02b48c6 RE |
963 | if (s->shutdown & SSL_SENT_SHUTDOWN) |
964 | { | |
965 | s->rwstate=SSL_NOTHING; | |
966 | SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN); | |
967 | return(-1); | |
968 | } | |
969 | return(s->method->ssl_write(s,buf,num)); | |
970 | } | |
971 | ||
4f43d0e7 | 972 | int SSL_shutdown(SSL *s) |
d02b48c6 | 973 | { |
d3407350 | 974 | /* Note that this function behaves differently from what one might |
e2e3d5ce BM |
975 | * expect. Return values are 0 for no success (yet), |
976 | * 1 for success; but calling it once is usually not enough, | |
977 | * even if blocking I/O is used (see ssl3_shutdown). | |
978 | */ | |
979 | ||
b31b04d9 BM |
980 | if (s->handshake_func == 0) |
981 | { | |
ff712220 | 982 | SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED); |
b31b04d9 BM |
983 | return -1; |
984 | } | |
985 | ||
d02b48c6 RE |
986 | if ((s != NULL) && !SSL_in_init(s)) |
987 | return(s->method->ssl_shutdown(s)); | |
988 | else | |
989 | return(1); | |
990 | } | |
991 | ||
4f43d0e7 | 992 | int SSL_renegotiate(SSL *s) |
d02b48c6 | 993 | { |
8c74b5e5 BM |
994 | if (s->new_session == 0) |
995 | { | |
996 | s->new_session=1; | |
997 | } | |
d02b48c6 RE |
998 | return(s->method->ssl_renegotiate(s)); |
999 | } | |
1000 | ||
6b0e9fac BM |
1001 | int SSL_renegotiate_pending(SSL *s) |
1002 | { | |
1003 | /* becomes true when negotiation is requested; | |
1004 | * false again once a handshake has finished */ | |
1005 | return (s->new_session != 0); | |
1006 | } | |
1007 | ||
a661b653 | 1008 | long SSL_ctrl(SSL *s,int cmd,long larg,void *parg) |
d02b48c6 | 1009 | { |
413c4f45 MC |
1010 | long l; |
1011 | ||
1012 | switch (cmd) | |
1013 | { | |
1014 | case SSL_CTRL_GET_READ_AHEAD: | |
1015 | return(s->read_ahead); | |
1016 | case SSL_CTRL_SET_READ_AHEAD: | |
1017 | l=s->read_ahead; | |
1018 | s->read_ahead=larg; | |
1019 | return(l); | |
bf21446a BM |
1020 | |
1021 | case SSL_CTRL_SET_MSG_CALLBACK_ARG: | |
1022 | s->msg_callback_arg = parg; | |
1023 | return 1; | |
1024 | ||
413c4f45 MC |
1025 | case SSL_CTRL_OPTIONS: |
1026 | return(s->options|=larg); | |
e1056435 BM |
1027 | case SSL_CTRL_MODE: |
1028 | return(s->mode|=larg); | |
c0f5dd07 LJ |
1029 | case SSL_CTRL_GET_MAX_CERT_LIST: |
1030 | return(s->max_cert_list); | |
1031 | case SSL_CTRL_SET_MAX_CERT_LIST: | |
1032 | l=s->max_cert_list; | |
1033 | s->max_cert_list=larg; | |
1034 | return(l); | |
36d16f8e BL |
1035 | case SSL_CTRL_SET_MTU: |
1036 | if (SSL_version(s) == DTLS1_VERSION) | |
1037 | { | |
1038 | s->d1->mtu = larg; | |
1039 | return larg; | |
1040 | } | |
1041 | return 0; | |
566dda07 DSH |
1042 | case SSL_CTRL_SET_MAX_SEND_FRAGMENT: |
1043 | if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) | |
1044 | return 0; | |
1045 | s->max_send_fragment = larg; | |
1046 | return 1; | |
413c4f45 MC |
1047 | default: |
1048 | return(s->method->ssl_ctrl(s,cmd,larg,parg)); | |
1049 | } | |
d02b48c6 RE |
1050 | } |
1051 | ||
41a15c4f | 1052 | long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) |
d3442bc7 RL |
1053 | { |
1054 | switch(cmd) | |
1055 | { | |
bf21446a | 1056 | case SSL_CTRL_SET_MSG_CALLBACK: |
a661b653 | 1057 | s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); |
bf21446a BM |
1058 | return 1; |
1059 | ||
d3442bc7 RL |
1060 | default: |
1061 | return(s->method->ssl_callback_ctrl(s,cmd,fp)); | |
1062 | } | |
1063 | } | |
1064 | ||
1e7396be RL |
1065 | struct lhash_st *SSL_CTX_sessions(SSL_CTX *ctx) |
1066 | { | |
1067 | return ctx->sessions; | |
1068 | } | |
1069 | ||
a661b653 | 1070 | long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg) |
d02b48c6 | 1071 | { |
413c4f45 MC |
1072 | long l; |
1073 | ||
1074 | switch (cmd) | |
1075 | { | |
1076 | case SSL_CTRL_GET_READ_AHEAD: | |
1077 | return(ctx->read_ahead); | |
1078 | case SSL_CTRL_SET_READ_AHEAD: | |
1079 | l=ctx->read_ahead; | |
1080 | ctx->read_ahead=larg; | |
1081 | return(l); | |
bf21446a BM |
1082 | |
1083 | case SSL_CTRL_SET_MSG_CALLBACK_ARG: | |
1084 | ctx->msg_callback_arg = parg; | |
1085 | return 1; | |
1086 | ||
c0f5dd07 LJ |
1087 | case SSL_CTRL_GET_MAX_CERT_LIST: |
1088 | return(ctx->max_cert_list); | |
1089 | case SSL_CTRL_SET_MAX_CERT_LIST: | |
1090 | l=ctx->max_cert_list; | |
1091 | ctx->max_cert_list=larg; | |
1092 | return(l); | |
413c4f45 MC |
1093 | |
1094 | case SSL_CTRL_SET_SESS_CACHE_SIZE: | |
1095 | l=ctx->session_cache_size; | |
1096 | ctx->session_cache_size=larg; | |
1097 | return(l); | |
1098 | case SSL_CTRL_GET_SESS_CACHE_SIZE: | |
1099 | return(ctx->session_cache_size); | |
1100 | case SSL_CTRL_SET_SESS_CACHE_MODE: | |
1101 | l=ctx->session_cache_mode; | |
1102 | ctx->session_cache_mode=larg; | |
1103 | return(l); | |
1104 | case SSL_CTRL_GET_SESS_CACHE_MODE: | |
1105 | return(ctx->session_cache_mode); | |
1106 | ||
1107 | case SSL_CTRL_SESS_NUMBER: | |
1108 | return(ctx->sessions->num_items); | |
1109 | case SSL_CTRL_SESS_CONNECT: | |
1110 | return(ctx->stats.sess_connect); | |
1111 | case SSL_CTRL_SESS_CONNECT_GOOD: | |
1112 | return(ctx->stats.sess_connect_good); | |
1113 | case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: | |
1114 | return(ctx->stats.sess_connect_renegotiate); | |
1115 | case SSL_CTRL_SESS_ACCEPT: | |
1116 | return(ctx->stats.sess_accept); | |
1117 | case SSL_CTRL_SESS_ACCEPT_GOOD: | |
1118 | return(ctx->stats.sess_accept_good); | |
1119 | case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: | |
1120 | return(ctx->stats.sess_accept_renegotiate); | |
1121 | case SSL_CTRL_SESS_HIT: | |
1122 | return(ctx->stats.sess_hit); | |
1123 | case SSL_CTRL_SESS_CB_HIT: | |
1124 | return(ctx->stats.sess_cb_hit); | |
1125 | case SSL_CTRL_SESS_MISSES: | |
1126 | return(ctx->stats.sess_miss); | |
1127 | case SSL_CTRL_SESS_TIMEOUTS: | |
1128 | return(ctx->stats.sess_timeout); | |
1129 | case SSL_CTRL_SESS_CACHE_FULL: | |
1130 | return(ctx->stats.sess_cache_full); | |
1131 | case SSL_CTRL_OPTIONS: | |
1132 | return(ctx->options|=larg); | |
e1056435 BM |
1133 | case SSL_CTRL_MODE: |
1134 | return(ctx->mode|=larg); | |
566dda07 DSH |
1135 | case SSL_CTRL_SET_MAX_SEND_FRAGMENT: |
1136 | if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) | |
1137 | return 0; | |
1138 | ctx->max_send_fragment = larg; | |
1139 | return 1; | |
413c4f45 MC |
1140 | default: |
1141 | return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg)); | |
1142 | } | |
d02b48c6 RE |
1143 | } |
1144 | ||
41a15c4f | 1145 | long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) |
d3442bc7 RL |
1146 | { |
1147 | switch(cmd) | |
1148 | { | |
bf21446a | 1149 | case SSL_CTRL_SET_MSG_CALLBACK: |
a661b653 | 1150 | ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); |
bf21446a BM |
1151 | return 1; |
1152 | ||
d3442bc7 RL |
1153 | default: |
1154 | return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp)); | |
1155 | } | |
1156 | } | |
1157 | ||
ccd86b68 | 1158 | int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b) |
d02b48c6 RE |
1159 | { |
1160 | long l; | |
1161 | ||
1162 | l=a->id-b->id; | |
1163 | if (l == 0L) | |
1164 | return(0); | |
1165 | else | |
1166 | return((l > 0)?1:-1); | |
1167 | } | |
1168 | ||
ccd86b68 GT |
1169 | int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap, |
1170 | const SSL_CIPHER * const *bp) | |
d02b48c6 RE |
1171 | { |
1172 | long l; | |
1173 | ||
1174 | l=(*ap)->id-(*bp)->id; | |
1175 | if (l == 0L) | |
1176 | return(0); | |
1177 | else | |
1178 | return((l > 0)?1:-1); | |
1179 | } | |
1180 | ||
4f43d0e7 | 1181 | /** return a STACK of the ciphers available for the SSL and in order of |
d02b48c6 | 1182 | * preference */ |
0821bcd4 | 1183 | STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) |
d02b48c6 | 1184 | { |
28b958f7 | 1185 | if (s != NULL) |
d02b48c6 | 1186 | { |
28b958f7 RL |
1187 | if (s->cipher_list != NULL) |
1188 | { | |
1189 | return(s->cipher_list); | |
1190 | } | |
1191 | else if ((s->ctx != NULL) && | |
1192 | (s->ctx->cipher_list != NULL)) | |
1193 | { | |
1194 | return(s->ctx->cipher_list); | |
1195 | } | |
d02b48c6 RE |
1196 | } |
1197 | return(NULL); | |
1198 | } | |
1199 | ||
4f43d0e7 | 1200 | /** return a STACK of the ciphers available for the SSL and in order of |
d02b48c6 | 1201 | * algorithm id */ |
f73e07cf | 1202 | STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) |
d02b48c6 | 1203 | { |
28b958f7 | 1204 | if (s != NULL) |
d02b48c6 | 1205 | { |
28b958f7 RL |
1206 | if (s->cipher_list_by_id != NULL) |
1207 | { | |
1208 | return(s->cipher_list_by_id); | |
1209 | } | |
1210 | else if ((s->ctx != NULL) && | |
1211 | (s->ctx->cipher_list_by_id != NULL)) | |
1212 | { | |
1213 | return(s->ctx->cipher_list_by_id); | |
1214 | } | |
d02b48c6 RE |
1215 | } |
1216 | return(NULL); | |
1217 | } | |
1218 | ||
4f43d0e7 | 1219 | /** The old interface to get the same thing as SSL_get_ciphers() */ |
0821bcd4 | 1220 | const char *SSL_get_cipher_list(const SSL *s,int n) |
d02b48c6 RE |
1221 | { |
1222 | SSL_CIPHER *c; | |
f73e07cf | 1223 | STACK_OF(SSL_CIPHER) *sk; |
d02b48c6 RE |
1224 | |
1225 | if (s == NULL) return(NULL); | |
1226 | sk=SSL_get_ciphers(s); | |
f73e07cf | 1227 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n)) |
d02b48c6 | 1228 | return(NULL); |
f73e07cf | 1229 | c=sk_SSL_CIPHER_value(sk,n); |
d02b48c6 RE |
1230 | if (c == NULL) return(NULL); |
1231 | return(c->name); | |
1232 | } | |
1233 | ||
25f923dd | 1234 | /** specify the ciphers to be used by default by the SSL_CTX */ |
018e57c7 | 1235 | int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) |
d02b48c6 | 1236 | { |
f73e07cf | 1237 | STACK_OF(SSL_CIPHER) *sk; |
d02b48c6 RE |
1238 | |
1239 | sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list, | |
1240 | &ctx->cipher_list_by_id,str); | |
f0747cd9 NL |
1241 | /* ssl_create_cipher_list may return an empty stack if it |
1242 | * was unable to find a cipher matching the given rule string | |
1243 | * (for example if the rule string specifies a cipher which | |
52b8dad8 BM |
1244 | * has been disabled). This is not an error as far as |
1245 | * ssl_create_cipher_list is concerned, and hence | |
f0747cd9 NL |
1246 | * ctx->cipher_list and ctx->cipher_list_by_id has been |
1247 | * updated. */ | |
1248 | if (sk == NULL) | |
1249 | return 0; | |
1250 | else if (sk_SSL_CIPHER_num(sk) == 0) | |
1251 | { | |
1252 | SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); | |
1253 | return 0; | |
1254 | } | |
1255 | return 1; | |
d02b48c6 RE |
1256 | } |
1257 | ||
4f43d0e7 | 1258 | /** specify the ciphers to be used by the SSL */ |
018e57c7 | 1259 | int SSL_set_cipher_list(SSL *s,const char *str) |
d02b48c6 | 1260 | { |
f73e07cf | 1261 | STACK_OF(SSL_CIPHER) *sk; |
d02b48c6 RE |
1262 | |
1263 | sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list, | |
1264 | &s->cipher_list_by_id,str); | |
f0747cd9 NL |
1265 | /* see comment in SSL_CTX_set_cipher_list */ |
1266 | if (sk == NULL) | |
1267 | return 0; | |
1268 | else if (sk_SSL_CIPHER_num(sk) == 0) | |
1269 | { | |
1270 | SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); | |
1271 | return 0; | |
1272 | } | |
1273 | return 1; | |
d02b48c6 RE |
1274 | } |
1275 | ||
1276 | /* works well for SSLv2, not so good for SSLv3 */ | |
0821bcd4 | 1277 | char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len) |
d02b48c6 | 1278 | { |
e778802f | 1279 | char *p; |
f73e07cf | 1280 | STACK_OF(SSL_CIPHER) *sk; |
d02b48c6 RE |
1281 | SSL_CIPHER *c; |
1282 | int i; | |
1283 | ||
1284 | if ((s->session == NULL) || (s->session->ciphers == NULL) || | |
1285 | (len < 2)) | |
1286 | return(NULL); | |
1287 | ||
1288 | p=buf; | |
1289 | sk=s->session->ciphers; | |
f73e07cf | 1290 | for (i=0; i<sk_SSL_CIPHER_num(sk); i++) |
d02b48c6 | 1291 | { |
4f19a9cb DSH |
1292 | int n; |
1293 | ||
f73e07cf | 1294 | c=sk_SSL_CIPHER_value(sk,i); |
4f19a9cb DSH |
1295 | n=strlen(c->name); |
1296 | if (n+1 > len) | |
d02b48c6 | 1297 | { |
4f19a9cb DSH |
1298 | if (p != buf) |
1299 | --p; | |
1300 | *p='\0'; | |
1301 | return buf; | |
d02b48c6 | 1302 | } |
4f19a9cb DSH |
1303 | strcpy(p,c->name); |
1304 | p+=n; | |
d02b48c6 | 1305 | *(p++)=':'; |
4f19a9cb | 1306 | len-=n+1; |
d02b48c6 RE |
1307 | } |
1308 | p[-1]='\0'; | |
1309 | return(buf); | |
1310 | } | |
1311 | ||
c6c2e313 | 1312 | int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p, |
52b8dad8 | 1313 | int (*put_cb)(const SSL_CIPHER *, unsigned char *)) |
d02b48c6 RE |
1314 | { |
1315 | int i,j=0; | |
1316 | SSL_CIPHER *c; | |
1317 | unsigned char *q; | |
a5224c34 | 1318 | #ifndef OPENSSL_NO_KRB5 |
52b8dad8 | 1319 | int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx); |
a5224c34 | 1320 | #endif /* OPENSSL_NO_KRB5 */ |
d02b48c6 RE |
1321 | |
1322 | if (sk == NULL) return(0); | |
1323 | q=p; | |
1324 | ||
f73e07cf | 1325 | for (i=0; i<sk_SSL_CIPHER_num(sk); i++) |
d02b48c6 | 1326 | { |
f73e07cf | 1327 | c=sk_SSL_CIPHER_value(sk,i); |
a5224c34 | 1328 | #ifndef OPENSSL_NO_KRB5 |
52b8dad8 BM |
1329 | if (((c->algorithm_mkey & SSL_kKRB5) || (c->algorithm_auth & SSL_aKRB5)) && |
1330 | nokrb5) | |
1331 | continue; | |
1332 | #endif /* OPENSSL_NO_KRB5 */ | |
ddac1974 NL |
1333 | #ifndef OPENSSL_NO_PSK |
1334 | /* with PSK there must be client callback set */ | |
52b8dad8 BM |
1335 | if (((c->algorithm_mkey & SSL_kPSK) || (c->algorithm_auth & SSL_aPSK)) && |
1336 | s->psk_client_callback == NULL) | |
ddac1974 NL |
1337 | continue; |
1338 | #endif /* OPENSSL_NO_PSK */ | |
c6c2e313 | 1339 | j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p); |
d02b48c6 RE |
1340 | p+=j; |
1341 | } | |
1342 | return(p-q); | |
1343 | } | |
1344 | ||
f73e07cf BL |
1345 | STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num, |
1346 | STACK_OF(SSL_CIPHER) **skp) | |
d02b48c6 RE |
1347 | { |
1348 | SSL_CIPHER *c; | |
f73e07cf | 1349 | STACK_OF(SSL_CIPHER) *sk; |
d02b48c6 RE |
1350 | int i,n; |
1351 | ||
1352 | n=ssl_put_cipher_by_char(s,NULL,NULL); | |
1353 | if ((num%n) != 0) | |
1354 | { | |
1355 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); | |
1356 | return(NULL); | |
1357 | } | |
1358 | if ((skp == NULL) || (*skp == NULL)) | |
62324627 | 1359 | sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */ |
d02b48c6 RE |
1360 | else |
1361 | { | |
1362 | sk= *skp; | |
f73e07cf | 1363 | sk_SSL_CIPHER_zero(sk); |
d02b48c6 RE |
1364 | } |
1365 | ||
1366 | for (i=0; i<num; i+=n) | |
1367 | { | |
1368 | c=ssl_get_cipher_by_char(s,p); | |
1369 | p+=n; | |
1370 | if (c != NULL) | |
1371 | { | |
f73e07cf | 1372 | if (!sk_SSL_CIPHER_push(sk,c)) |
d02b48c6 RE |
1373 | { |
1374 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE); | |
1375 | goto err; | |
1376 | } | |
1377 | } | |
1378 | } | |
1379 | ||
1380 | if (skp != NULL) | |
1381 | *skp=sk; | |
1382 | return(sk); | |
1383 | err: | |
1384 | if ((skp == NULL) || (*skp == NULL)) | |
f73e07cf | 1385 | sk_SSL_CIPHER_free(sk); |
d02b48c6 RE |
1386 | return(NULL); |
1387 | } | |
1388 | ||
f1fd4544 | 1389 | |
34449617 | 1390 | #ifndef OPENSSL_NO_TLSEXT |
52b8dad8 | 1391 | /** return a servername extension value if provided in Client Hello, or NULL. |
f1fd4544 | 1392 | * So far, only host_name types are defined (RFC 3546). |
ed3883d2 BM |
1393 | */ |
1394 | ||
f1fd4544 BM |
1395 | const char *SSL_get_servername(const SSL *s, const int type) |
1396 | { | |
1397 | if (type != TLSEXT_NAMETYPE_host_name) | |
ed3883d2 | 1398 | return NULL; |
a13c20f6 BM |
1399 | |
1400 | return s->session && !s->tlsext_hostname ? | |
f1fd4544 BM |
1401 | s->session->tlsext_hostname : |
1402 | s->tlsext_hostname; | |
1403 | } | |
ed3883d2 | 1404 | |
f1fd4544 BM |
1405 | int SSL_get_servername_type(const SSL *s) |
1406 | { | |
52b8dad8 | 1407 | if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) |
f1fd4544 | 1408 | return TLSEXT_NAMETYPE_host_name; |
ed3883d2 | 1409 | return -1; |
f1fd4544 | 1410 | } |
ed3883d2 | 1411 | #endif |
f1fd4544 | 1412 | |
0821bcd4 | 1413 | unsigned long SSL_SESSION_hash(const SSL_SESSION *a) |
d02b48c6 RE |
1414 | { |
1415 | unsigned long l; | |
1416 | ||
dfeab068 RE |
1417 | l=(unsigned long) |
1418 | ((unsigned int) a->session_id[0] )| | |
1419 | ((unsigned int) a->session_id[1]<< 8L)| | |
1420 | ((unsigned long)a->session_id[2]<<16L)| | |
1421 | ((unsigned long)a->session_id[3]<<24L); | |
d02b48c6 RE |
1422 | return(l); |
1423 | } | |
1424 | ||
dc644fe2 GT |
1425 | /* NB: If this function (or indeed the hash function which uses a sort of |
1426 | * coarser function than this one) is changed, ensure | |
1427 | * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being | |
1428 | * able to construct an SSL_SESSION that will collide with any existing session | |
1429 | * with a matching session ID. */ | |
0821bcd4 | 1430 | int SSL_SESSION_cmp(const SSL_SESSION *a,const SSL_SESSION *b) |
d02b48c6 | 1431 | { |
58964a49 RE |
1432 | if (a->ssl_version != b->ssl_version) |
1433 | return(1); | |
1434 | if (a->session_id_length != b->session_id_length) | |
1435 | return(1); | |
1436 | return(memcmp(a->session_id,b->session_id,a->session_id_length)); | |
d02b48c6 RE |
1437 | } |
1438 | ||
d0fa136c GT |
1439 | /* These wrapper functions should remain rather than redeclaring |
1440 | * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each | |
1441 | * variable. The reason is that the functions aren't static, they're exposed via | |
1442 | * ssl.h. */ | |
97b17195 GT |
1443 | static IMPLEMENT_LHASH_HASH_FN(SSL_SESSION_hash, SSL_SESSION *) |
1444 | static IMPLEMENT_LHASH_COMP_FN(SSL_SESSION_cmp, SSL_SESSION *) | |
1445 | ||
4ebb342f | 1446 | SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) |
d02b48c6 | 1447 | { |
dfeab068 | 1448 | SSL_CTX *ret=NULL; |
d02b48c6 RE |
1449 | |
1450 | if (meth == NULL) | |
1451 | { | |
1452 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED); | |
1453 | return(NULL); | |
1454 | } | |
dfeab068 RE |
1455 | |
1456 | if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) | |
1457 | { | |
1458 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); | |
1459 | goto err; | |
1460 | } | |
26a3a48d | 1461 | ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); |
d02b48c6 RE |
1462 | if (ret == NULL) |
1463 | goto err; | |
1464 | ||
1465 | memset(ret,0,sizeof(SSL_CTX)); | |
1466 | ||
1467 | ret->method=meth; | |
1468 | ||
1469 | ret->cert_store=NULL; | |
1470 | ret->session_cache_mode=SSL_SESS_CACHE_SERVER; | |
58964a49 RE |
1471 | ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; |
1472 | ret->session_cache_head=NULL; | |
1473 | ret->session_cache_tail=NULL; | |
d02b48c6 RE |
1474 | |
1475 | /* We take the system default */ | |
1476 | ret->session_timeout=meth->get_timeout(); | |
1477 | ||
bf21446a BM |
1478 | ret->new_session_cb=0; |
1479 | ret->remove_session_cb=0; | |
1480 | ret->get_session_cb=0; | |
1481 | ret->generate_session_id=0; | |
d02b48c6 | 1482 | |
413c4f45 | 1483 | memset((char *)&ret->stats,0,sizeof(ret->stats)); |
d02b48c6 RE |
1484 | |
1485 | ret->references=1; | |
1486 | ret->quiet_shutdown=0; | |
1487 | ||
1488 | /* ret->cipher=NULL;*/ | |
1489 | /* ret->s2->challenge=NULL; | |
1490 | ret->master_key=NULL; | |
1491 | ret->key_arg=NULL; | |
1492 | ret->s2->conn_id=NULL; */ | |
1493 | ||
45d87a1f | 1494 | ret->info_callback=NULL; |
d02b48c6 | 1495 | |
bf21446a | 1496 | ret->app_verify_callback=0; |
d02b48c6 RE |
1497 | ret->app_verify_arg=NULL; |
1498 | ||
c0f5dd07 | 1499 | ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT; |
413c4f45 | 1500 | ret->read_ahead=0; |
bf21446a BM |
1501 | ret->msg_callback=0; |
1502 | ret->msg_callback_arg=NULL; | |
413c4f45 | 1503 | ret->verify_mode=SSL_VERIFY_NONE; |
5d7c222d | 1504 | #if 0 |
7f89714e | 1505 | ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */ |
5d7c222d | 1506 | #endif |
bf21446a | 1507 | ret->sid_ctx_length=0; |
d02b48c6 | 1508 | ret->default_verify_callback=NULL; |
ca8e5b9b | 1509 | if ((ret->cert=ssl_cert_new()) == NULL) |
d02b48c6 RE |
1510 | goto err; |
1511 | ||
bf21446a | 1512 | ret->default_passwd_callback=0; |
74678cc2 | 1513 | ret->default_passwd_callback_userdata=NULL; |
bf21446a | 1514 | ret->client_cert_cb=0; |
f0747cd9 NL |
1515 | ret->app_gen_cookie_cb=0; |
1516 | ret->app_verify_cookie_cb=0; | |
d02b48c6 | 1517 | |
97b17195 GT |
1518 | ret->sessions=lh_new(LHASH_HASH_FN(SSL_SESSION_hash), |
1519 | LHASH_COMP_FN(SSL_SESSION_cmp)); | |
d02b48c6 RE |
1520 | if (ret->sessions == NULL) goto err; |
1521 | ret->cert_store=X509_STORE_new(); | |
1522 | if (ret->cert_store == NULL) goto err; | |
1523 | ||
1524 | ssl_create_cipher_list(ret->method, | |
1525 | &ret->cipher_list,&ret->cipher_list_by_id, | |
1526 | SSL_DEFAULT_CIPHER_LIST); | |
f73e07cf BL |
1527 | if (ret->cipher_list == NULL |
1528 | || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) | |
d02b48c6 RE |
1529 | { |
1530 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS); | |
1531 | goto err2; | |
1532 | } | |
1533 | ||
5d7c222d DSH |
1534 | ret->param = X509_VERIFY_PARAM_new(); |
1535 | if (!ret->param) | |
1536 | goto err; | |
1537 | ||
58964a49 RE |
1538 | if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL) |
1539 | { | |
1540 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES); | |
1541 | goto err2; | |
1542 | } | |
1543 | if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL) | |
1544 | { | |
1545 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES); | |
1546 | goto err2; | |
1547 | } | |
1548 | if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL) | |
1549 | { | |
1550 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES); | |
1551 | goto err2; | |
1552 | } | |
1553 | ||
f73e07cf | 1554 | if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL) |
d02b48c6 RE |
1555 | goto err; |
1556 | ||
79aa04ef | 1557 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data); |
58964a49 | 1558 | |
dfeab068 | 1559 | ret->extra_certs=NULL; |
413c4f45 | 1560 | ret->comp_methods=SSL_COMP_get_compression_methods(); |
dfeab068 | 1561 | |
566dda07 DSH |
1562 | ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
1563 | ||
ed3883d2 | 1564 | #ifndef OPENSSL_NO_TLSEXT |
f1fd4544 | 1565 | ret->tlsext_servername_callback = 0; |
ed3883d2 | 1566 | ret->tlsext_servername_arg = NULL; |
6434abbf DSH |
1567 | /* Setup RFC4507 ticket keys */ |
1568 | if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0) | |
1569 | || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0) | |
1570 | || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0)) | |
1571 | ret->options |= SSL_OP_NO_TICKET; | |
1572 | ||
67c8e7f4 DSH |
1573 | ret->tlsext_status_cb = 0; |
1574 | ret->tlsext_status_arg = NULL; | |
1575 | ||
ddac1974 NL |
1576 | #endif |
1577 | #ifndef OPENSSL_NO_PSK | |
1578 | ret->psk_identity_hint=NULL; | |
1579 | ret->psk_client_callback=NULL; | |
1580 | ret->psk_server_callback=NULL; | |
ed3883d2 | 1581 | #endif |
d02b48c6 RE |
1582 | return(ret); |
1583 | err: | |
1584 | SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE); | |
1585 | err2: | |
1586 | if (ret != NULL) SSL_CTX_free(ret); | |
1587 | return(NULL); | |
1588 | } | |
1589 | ||
0826c85f | 1590 | #if 0 |
f73e07cf | 1591 | static void SSL_COMP_free(SSL_COMP *comp) |
26a3a48d | 1592 | { OPENSSL_free(comp); } |
0826c85f | 1593 | #endif |
f73e07cf | 1594 | |
4f43d0e7 | 1595 | void SSL_CTX_free(SSL_CTX *a) |
d02b48c6 RE |
1596 | { |
1597 | int i; | |
1598 | ||
1599 | if (a == NULL) return; | |
1600 | ||
1601 | i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX); | |
58964a49 RE |
1602 | #ifdef REF_PRINT |
1603 | REF_PRINT("SSL_CTX",a); | |
1604 | #endif | |
d02b48c6 RE |
1605 | if (i > 0) return; |
1606 | #ifdef REF_CHECK | |
1607 | if (i < 0) | |
1608 | { | |
1609 | fprintf(stderr,"SSL_CTX_free, bad reference count\n"); | |
1610 | abort(); /* ok */ | |
1611 | } | |
1612 | #endif | |
1613 | ||
5d7c222d DSH |
1614 | if (a->param) |
1615 | X509_VERIFY_PARAM_free(a->param); | |
1616 | ||
82a20fb0 LJ |
1617 | /* |
1618 | * Free internal session cache. However: the remove_cb() may reference | |
1619 | * the ex_data of SSL_CTX, thus the ex_data store can only be removed | |
1620 | * after the sessions were flushed. | |
1621 | * As the ex_data handling routines might also touch the session cache, | |
1622 | * the most secure solution seems to be: empty (flush) the cache, then | |
1623 | * free ex_data, then finally free the cache. | |
1624 | * (See ticket [openssl.org #212].) | |
1625 | */ | |
d02b48c6 | 1626 | if (a->sessions != NULL) |
d02b48c6 | 1627 | SSL_CTX_flush_sessions(a,0); |
82a20fb0 LJ |
1628 | |
1629 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data); | |
1630 | ||
1631 | if (a->sessions != NULL) | |
d02b48c6 | 1632 | lh_free(a->sessions); |
82a20fb0 | 1633 | |
d02b48c6 RE |
1634 | if (a->cert_store != NULL) |
1635 | X509_STORE_free(a->cert_store); | |
1636 | if (a->cipher_list != NULL) | |
f73e07cf | 1637 | sk_SSL_CIPHER_free(a->cipher_list); |
d02b48c6 | 1638 | if (a->cipher_list_by_id != NULL) |
f73e07cf | 1639 | sk_SSL_CIPHER_free(a->cipher_list_by_id); |
ca8e5b9b BM |
1640 | if (a->cert != NULL) |
1641 | ssl_cert_free(a->cert); | |
d02b48c6 | 1642 | if (a->client_CA != NULL) |
f73e07cf | 1643 | sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free); |
dfeab068 | 1644 | if (a->extra_certs != NULL) |
f73e07cf | 1645 | sk_X509_pop_free(a->extra_certs,X509_free); |
cd9b7d7c | 1646 | #if 0 /* This should never be done, since it removes a global database */ |
413c4f45 | 1647 | if (a->comp_methods != NULL) |
f73e07cf | 1648 | sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free); |
cd9b7d7c RL |
1649 | #else |
1650 | a->comp_methods = NULL; | |
1651 | #endif | |
ddac1974 NL |
1652 | |
1653 | #ifndef OPENSSL_NO_PSK | |
1654 | if (a->psk_identity_hint) | |
1655 | OPENSSL_free(a->psk_identity_hint); | |
1656 | #endif | |
26a3a48d | 1657 | OPENSSL_free(a); |
d02b48c6 RE |
1658 | } |
1659 | ||
3ae76679 | 1660 | void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) |
d02b48c6 RE |
1661 | { |
1662 | ctx->default_passwd_callback=cb; | |
1663 | } | |
1664 | ||
74678cc2 BM |
1665 | void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u) |
1666 | { | |
1667 | ctx->default_passwd_callback_userdata=u; | |
1668 | } | |
1669 | ||
023ec151 | 1670 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg) |
d02b48c6 RE |
1671 | { |
1672 | ctx->app_verify_callback=cb; | |
023ec151 | 1673 | ctx->app_verify_arg=arg; |
d02b48c6 RE |
1674 | } |
1675 | ||
4f43d0e7 | 1676 | void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *)) |
d02b48c6 | 1677 | { |
413c4f45 | 1678 | ctx->verify_mode=mode; |
d02b48c6 | 1679 | ctx->default_verify_callback=cb; |
d02b48c6 RE |
1680 | } |
1681 | ||
7f89714e BM |
1682 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) |
1683 | { | |
5d7c222d | 1684 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
7f89714e BM |
1685 | } |
1686 | ||
ca8e5b9b | 1687 | void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher) |
d02b48c6 RE |
1688 | { |
1689 | CERT_PKEY *cpk; | |
1690 | int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign; | |
1691 | int rsa_enc_export,dh_rsa_export,dh_dsa_export; | |
60e31c3a | 1692 | int rsa_tmp_export,dh_tmp_export,kl; |
52b8dad8 | 1693 | unsigned long mask_k,mask_a,emask_k,emask_a; |
3eeaab4b NL |
1694 | int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size; |
1695 | #ifndef OPENSSL_NO_ECDH | |
1696 | int have_ecdh_tmp; | |
1697 | #endif | |
ea262260 BM |
1698 | X509 *x = NULL; |
1699 | EVP_PKEY *ecc_pkey = NULL; | |
1700 | int signature_nid = 0; | |
d02b48c6 | 1701 | |
f415fa32 | 1702 | if (c == NULL) return; |
d02b48c6 | 1703 | |
60e31c3a BL |
1704 | kl=SSL_C_EXPORT_PKEYLENGTH(cipher); |
1705 | ||
bc36ee62 | 1706 | #ifndef OPENSSL_NO_RSA |
ca8e5b9b BM |
1707 | rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL); |
1708 | rsa_tmp_export=(c->rsa_tmp_cb != NULL || | |
60e31c3a | 1709 | (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl)); |
d02b48c6 RE |
1710 | #else |
1711 | rsa_tmp=rsa_tmp_export=0; | |
1712 | #endif | |
bc36ee62 | 1713 | #ifndef OPENSSL_NO_DH |
ca8e5b9b BM |
1714 | dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL); |
1715 | dh_tmp_export=(c->dh_tmp_cb != NULL || | |
60e31c3a | 1716 | (dh_tmp && DH_size(c->dh_tmp)*8 <= kl)); |
d02b48c6 RE |
1717 | #else |
1718 | dh_tmp=dh_tmp_export=0; | |
1719 | #endif | |
1720 | ||
ea262260 BM |
1721 | #ifndef OPENSSL_NO_ECDH |
1722 | have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL); | |
1723 | #endif | |
d02b48c6 | 1724 | cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]); |
60e31c3a BL |
1725 | rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL); |
1726 | rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl); | |
d02b48c6 | 1727 | cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]); |
60e31c3a | 1728 | rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); |
d02b48c6 | 1729 | cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]); |
60e31c3a | 1730 | dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); |
d02b48c6 | 1731 | cpk= &(c->pkeys[SSL_PKEY_DH_RSA]); |
60e31c3a BL |
1732 | dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL); |
1733 | dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); | |
d02b48c6 RE |
1734 | cpk= &(c->pkeys[SSL_PKEY_DH_DSA]); |
1735 | /* FIX THIS EAY EAY EAY */ | |
60e31c3a BL |
1736 | dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL); |
1737 | dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); | |
ea262260 BM |
1738 | cpk= &(c->pkeys[SSL_PKEY_ECC]); |
1739 | have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL); | |
52b8dad8 BM |
1740 | mask_k=0; |
1741 | mask_a=0; | |
1742 | emask_k=0; | |
1743 | emask_a=0; | |
d02b48c6 | 1744 | |
0e1dba93 DSH |
1745 | |
1746 | ||
d02b48c6 | 1747 | #ifdef CIPHER_DEBUG |
076944d9 | 1748 | printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n", |
2afe3167 | 1749 | rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp, |
f415fa32 | 1750 | rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa); |
d02b48c6 | 1751 | #endif |
0e1dba93 DSH |
1752 | |
1753 | cpk = &(c->pkeys[SSL_PKEY_GOST01]); | |
1754 | if (cpk->x509 != NULL && cpk->privatekey !=NULL) { | |
1755 | mask_k |= SSL_kGOST; | |
1756 | mask_a |= SSL_aGOST01; | |
1757 | } | |
1758 | cpk = &(c->pkeys[SSL_PKEY_GOST94]); | |
1759 | if (cpk->x509 != NULL && cpk->privatekey !=NULL) { | |
1760 | mask_k |= SSL_kGOST; | |
1761 | mask_a |= SSL_aGOST94; | |
1762 | } | |
d02b48c6 RE |
1763 | |
1764 | if (rsa_enc || (rsa_tmp && rsa_sign)) | |
52b8dad8 | 1765 | mask_k|=SSL_kRSA; |
f415fa32 | 1766 | if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc))) |
52b8dad8 | 1767 | emask_k|=SSL_kRSA; |
d02b48c6 RE |
1768 | |
1769 | #if 0 | |
1770 | /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */ | |
52b8dad8 | 1771 | if ( (dh_tmp || dh_rsa || dh_dsa) && |
d02b48c6 | 1772 | (rsa_enc || rsa_sign || dsa_sign)) |
52b8dad8 | 1773 | mask_k|=SSL_kEDH; |
d02b48c6 RE |
1774 | if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) && |
1775 | (rsa_enc || rsa_sign || dsa_sign)) | |
52b8dad8 | 1776 | emask_k|=SSL_kEDH; |
d02b48c6 RE |
1777 | #endif |
1778 | ||
52b8dad8 BM |
1779 | if (dh_tmp_export) |
1780 | emask_k|=SSL_kEDH; | |
d02b48c6 RE |
1781 | |
1782 | if (dh_tmp) | |
52b8dad8 | 1783 | mask_k|=SSL_kEDH; |
d02b48c6 | 1784 | |
52b8dad8 BM |
1785 | if (dh_rsa) mask_k|=SSL_kDHr; |
1786 | if (dh_rsa_export) emask_k|=SSL_kDHr; | |
d02b48c6 | 1787 | |
52b8dad8 BM |
1788 | if (dh_dsa) mask_k|=SSL_kDHd; |
1789 | if (dh_dsa_export) emask_k|=SSL_kDHd; | |
d02b48c6 RE |
1790 | |
1791 | if (rsa_enc || rsa_sign) | |
1792 | { | |
52b8dad8 BM |
1793 | mask_a|=SSL_aRSA; |
1794 | emask_a|=SSL_aRSA; | |
d02b48c6 RE |
1795 | } |
1796 | ||
1797 | if (dsa_sign) | |
1798 | { | |
52b8dad8 BM |
1799 | mask_a|=SSL_aDSS; |
1800 | emask_a|=SSL_aDSS; | |
d02b48c6 RE |
1801 | } |
1802 | ||
52b8dad8 BM |
1803 | mask_a|=SSL_aNULL; |
1804 | emask_a|=SSL_aNULL; | |
d02b48c6 | 1805 | |
bc36ee62 | 1806 | #ifndef OPENSSL_NO_KRB5 |
52b8dad8 BM |
1807 | mask_k|=SSL_kKRB5; |
1808 | mask_a|=SSL_aKRB5; | |
1809 | emask_k|=SSL_kKRB5; | |
1810 | emask_a|=SSL_aKRB5; | |
f9b3bff6 RL |
1811 | #endif |
1812 | ||
ea262260 BM |
1813 | /* An ECC certificate may be usable for ECDH and/or |
1814 | * ECDSA cipher suites depending on the key usage extension. | |
1815 | */ | |
1816 | if (have_ecc_cert) | |
1817 | { | |
52b8dad8 | 1818 | /* This call populates extension flags (ex_flags) */ |
ea262260 BM |
1819 | x = (c->pkeys[SSL_PKEY_ECC]).x509; |
1820 | X509_check_purpose(x, -1, 0); | |
1821 | ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ? | |
1822 | (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1; | |
1823 | ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ? | |
1824 | (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1; | |
1825 | ecc_pkey = X509_get_pubkey(x); | |
52b8dad8 | 1826 | ecc_pkey_size = (ecc_pkey != NULL) ? |
ea262260 BM |
1827 | EVP_PKEY_bits(ecc_pkey) : 0; |
1828 | EVP_PKEY_free(ecc_pkey); | |
1829 | if ((x->sig_alg) && (x->sig_alg->algorithm)) | |
1830 | signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); | |
1831 | #ifndef OPENSSL_NO_ECDH | |
1832 | if (ecdh_ok) | |
1833 | { | |
076944d9 BM |
1834 | const char *sig = OBJ_nid2ln(signature_nid); |
1835 | if (sig == NULL) | |
1836 | { | |
1837 | ERR_clear_error(); | |
1838 | sig = "unknown"; | |
1839 | } | |
1840 | ||
1841 | if (strstr(sig, "WithRSA")) | |
ea262260 | 1842 | { |
52b8dad8 BM |
1843 | mask_k|=SSL_kECDHr; |
1844 | mask_a|=SSL_aECDH; | |
ea262260 | 1845 | if (ecc_pkey_size <= 163) |
52b8dad8 BM |
1846 | { |
1847 | emask_k|=SSL_kECDHr; | |
1848 | emask_a|=SSL_aECDH; | |
1849 | } | |
ea262260 | 1850 | } |
076944d9 | 1851 | |
ea262260 BM |
1852 | if (signature_nid == NID_ecdsa_with_SHA1) |
1853 | { | |
52b8dad8 BM |
1854 | mask_k|=SSL_kECDHe; |
1855 | mask_a|=SSL_aECDH; | |
ea262260 | 1856 | if (ecc_pkey_size <= 163) |
52b8dad8 BM |
1857 | { |
1858 | emask_k|=SSL_kECDHe; | |
1859 | emask_a|=SSL_aECDH; | |
1860 | } | |
ea262260 BM |
1861 | } |
1862 | } | |
1863 | #endif | |
1864 | #ifndef OPENSSL_NO_ECDSA | |
1865 | if (ecdsa_ok) | |
1866 | { | |
52b8dad8 BM |
1867 | mask_a|=SSL_aECDSA; |
1868 | emask_a|=SSL_aECDSA; | |
ea262260 BM |
1869 | } |
1870 | #endif | |
1871 | } | |
1872 | ||
1873 | #ifndef OPENSSL_NO_ECDH | |
1874 | if (have_ecdh_tmp) | |
1875 | { | |
52b8dad8 BM |
1876 | mask_k|=SSL_kEECDH; |
1877 | emask_k|=SSL_kEECDH; | |
ea262260 BM |
1878 | } |
1879 | #endif | |
ddac1974 NL |
1880 | |
1881 | #ifndef OPENSSL_NO_PSK | |
52b8dad8 BM |
1882 | mask_k |= SSL_kPSK; |
1883 | mask_a |= SSL_aPSK; | |
1884 | emask_k |= SSL_kPSK; | |
1885 | emask_a |= SSL_aPSK; | |
ddac1974 NL |
1886 | #endif |
1887 | ||
52b8dad8 BM |
1888 | c->mask_k=mask_k; |
1889 | c->mask_a=mask_a; | |
1890 | c->export_mask_k=emask_k; | |
1891 | c->export_mask_a=emask_a; | |
d02b48c6 RE |
1892 | c->valid=1; |
1893 | } | |
1894 | ||
ea262260 BM |
1895 | /* This handy macro borrowed from crypto/x509v3/v3_purp.c */ |
1896 | #define ku_reject(x, usage) \ | |
1897 | (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) | |
1898 | ||
ed3ecd80 | 1899 | int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs) |
ea262260 | 1900 | { |
52b8dad8 | 1901 | unsigned long alg_k, alg_a; |
ea262260 BM |
1902 | EVP_PKEY *pkey = NULL; |
1903 | int keysize = 0; | |
1904 | int signature_nid = 0; | |
1905 | ||
52b8dad8 BM |
1906 | alg_k = cs->algorithm_mkey; |
1907 | alg_a = cs->algorithm_auth; | |
1908 | ||
ea262260 BM |
1909 | if (SSL_C_IS_EXPORT(cs)) |
1910 | { | |
1911 | /* ECDH key length in export ciphers must be <= 163 bits */ | |
1912 | pkey = X509_get_pubkey(x); | |
1913 | if (pkey == NULL) return 0; | |
1914 | keysize = EVP_PKEY_bits(pkey); | |
1915 | EVP_PKEY_free(pkey); | |
1916 | if (keysize > 163) return 0; | |
1917 | } | |
1918 | ||
1919 | /* This call populates the ex_flags field correctly */ | |
1920 | X509_check_purpose(x, -1, 0); | |
1921 | if ((x->sig_alg) && (x->sig_alg->algorithm)) | |
1922 | signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); | |
52b8dad8 | 1923 | if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr) |
ea262260 BM |
1924 | { |
1925 | /* key usage, if present, must allow key agreement */ | |
1926 | if (ku_reject(x, X509v3_KU_KEY_AGREEMENT)) | |
1927 | { | |
ed3ecd80 | 1928 | SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT); |
ea262260 BM |
1929 | return 0; |
1930 | } | |
52b8dad8 | 1931 | if (alg_k & SSL_kECDHe) |
ea262260 BM |
1932 | { |
1933 | /* signature alg must be ECDSA */ | |
1934 | if (signature_nid != NID_ecdsa_with_SHA1) | |
1935 | { | |
ed3ecd80 | 1936 | SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE); |
ea262260 BM |
1937 | return 0; |
1938 | } | |
1939 | } | |
52b8dad8 | 1940 | if (alg_k & SSL_kECDHr) |
ea262260 BM |
1941 | { |
1942 | /* signature alg must be RSA */ | |
076944d9 BM |
1943 | |
1944 | const char *sig = OBJ_nid2ln(signature_nid); | |
1945 | if (sig == NULL) | |
ea262260 | 1946 | { |
076944d9 BM |
1947 | ERR_clear_error(); |
1948 | sig = "unknown"; | |
ea262260 | 1949 | } |
076944d9 | 1950 | if (strstr(sig, "WithRSA") == NULL) |
ed3ecd80 BM |
1951 | { |
1952 | SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE); | |
076944d9 | 1953 | return 0; |
ed3ecd80 | 1954 | } |
ea262260 | 1955 | } |
52b8dad8 BM |
1956 | } |
1957 | if (alg_a & SSL_aECDSA) | |
ea262260 BM |
1958 | { |
1959 | /* key usage, if present, must allow signing */ | |
1960 | if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) | |
1961 | { | |
ed3ecd80 | 1962 | SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
ea262260 BM |
1963 | return 0; |
1964 | } | |
1965 | } | |
1966 | ||
1967 | return 1; /* all checks are ok */ | |
1968 | } | |
1969 | ||
d02b48c6 | 1970 | /* THIS NEEDS CLEANING UP */ |
4f43d0e7 | 1971 | X509 *ssl_get_server_send_cert(SSL *s) |
d02b48c6 | 1972 | { |
52b8dad8 | 1973 | unsigned long alg_k,alg_a,mask_k,mask_a; |
d02b48c6 | 1974 | CERT *c; |
df63a389 | 1975 | int i,is_export; |
d02b48c6 RE |
1976 | |
1977 | c=s->cert; | |
ca8e5b9b | 1978 | ssl_set_cert_masks(c, s->s3->tmp.new_cipher); |
018e57c7 | 1979 | is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher); |
52b8dad8 BM |
1980 | if (is_export) |
1981 | { | |
1982 | mask_k = c->export_mask_k; | |
1983 | mask_a = c->export_mask_a; | |
1984 | } | |
1985 | else | |
1986 | { | |
1987 | mask_k = c->mask_k; | |
1988 | mask_a = c->mask_a; | |
1989 | } | |
1990 | ||
1991 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
1992 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
d02b48c6 | 1993 | |
882d29dd | 1994 | if (alg_k & (SSL_kECDHr|SSL_kECDHe)) |
ea262260 | 1995 | { |
89bbe14c | 1996 | /* we don't need to look at SSL_kEECDH |
ea262260 BM |
1997 | * since no certificate is needed for |
1998 | * anon ECDH and for authenticated | |
89bbe14c | 1999 | * EECDH, the check for the auth |
ea262260 BM |
2000 | * algorithm will set i correctly |
2001 | * NOTE: For ECDH-RSA, we need an ECC | |
89bbe14c | 2002 | * not an RSA cert but for EECDH-RSA |
ea262260 BM |
2003 | * we need an RSA cert. Placing the |
2004 | * checks for SSL_kECDH before RSA | |
2005 | * checks ensures the correct cert is chosen. | |
2006 | */ | |
2007 | i=SSL_PKEY_ECC; | |
2008 | } | |
52b8dad8 | 2009 | else if (alg_a & SSL_aECDSA) |
ea262260 BM |
2010 | { |
2011 | i=SSL_PKEY_ECC; | |
2012 | } | |
52b8dad8 | 2013 | else if (alg_k & SSL_kDHr) |
d02b48c6 | 2014 | i=SSL_PKEY_DH_RSA; |
52b8dad8 | 2015 | else if (alg_k & SSL_kDHd) |
d02b48c6 | 2016 | i=SSL_PKEY_DH_DSA; |
52b8dad8 | 2017 | else if (alg_a & SSL_aDSS) |
d02b48c6 | 2018 | i=SSL_PKEY_DSA_SIGN; |
52b8dad8 | 2019 | else if (alg_a & SSL_aRSA) |
d02b48c6 RE |
2020 | { |
2021 | if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL) | |
2022 | i=SSL_PKEY_RSA_SIGN; | |
2023 | else | |
2024 | i=SSL_PKEY_RSA_ENC; | |
2025 | } | |
52b8dad8 | 2026 | else if (alg_a & SSL_aKRB5) |
f9b3bff6 RL |
2027 | { |
2028 | /* VRS something else here? */ | |
2029 | return(NULL); | |
2030 | } | |
0e1dba93 DSH |
2031 | else if (alg_a & SSL_aGOST94) |
2032 | i=SSL_PKEY_GOST94; | |
2033 | else if (alg_a & SSL_aGOST01) | |
2034 | i=SSL_PKEY_GOST01; | |
52b8dad8 | 2035 | else /* if (alg_a & SSL_aNULL) */ |
d02b48c6 | 2036 | { |
5277d7cb | 2037 | SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,ERR_R_INTERNAL_ERROR); |
d02b48c6 RE |
2038 | return(NULL); |
2039 | } | |
2040 | if (c->pkeys[i].x509 == NULL) return(NULL); | |
ea262260 | 2041 | |
d02b48c6 RE |
2042 | return(c->pkeys[i].x509); |
2043 | } | |
2044 | ||
4f43d0e7 | 2045 | EVP_PKEY *ssl_get_sign_pkey(SSL *s,SSL_CIPHER *cipher) |
d02b48c6 | 2046 | { |
52b8dad8 | 2047 | unsigned long alg_a; |
d02b48c6 RE |
2048 | CERT *c; |
2049 | ||
52b8dad8 | 2050 | alg_a = cipher->algorithm_auth; |
d02b48c6 RE |
2051 | c=s->cert; |
2052 | ||
52b8dad8 | 2053 | if ((alg_a & SSL_aDSS) && |
d02b48c6 RE |
2054 | (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL)) |
2055 | return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey); | |
52b8dad8 | 2056 | else if (alg_a & SSL_aRSA) |
d02b48c6 RE |
2057 | { |
2058 | if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) | |
2059 | return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey); | |
2060 | else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) | |
2061 | return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey); | |
2062 | else | |
2063 | return(NULL); | |
2064 | } | |
52b8dad8 | 2065 | else if ((alg_a & SSL_aECDSA) && |
ea262260 BM |
2066 | (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) |
2067 | return(c->pkeys[SSL_PKEY_ECC].privatekey); | |
52b8dad8 | 2068 | else /* if (alg_a & SSL_aNULL) */ |
d02b48c6 | 2069 | { |
5277d7cb | 2070 | SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR); |
d02b48c6 RE |
2071 | return(NULL); |
2072 | } | |
2073 | } | |
2074 | ||
4f43d0e7 | 2075 | void ssl_update_cache(SSL *s,int mode) |
d02b48c6 | 2076 | { |
58964a49 RE |
2077 | int i; |
2078 | ||
2079 | /* If the session_id_length is 0, we are not supposed to cache it, | |
2080 | * and it would be rather hard to do anyway :-) */ | |
2081 | if (s->session->session_id_length == 0) return; | |
2082 | ||
a13c20f6 | 2083 | i=s->session_ctx->session_cache_mode; |
4de920c9 | 2084 | if ((i & mode) && (!s->hit) |
e0db2eed | 2085 | && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) |
a13c20f6 BM |
2086 | || SSL_CTX_add_session(s->session_ctx,s->session)) |
2087 | && (s->session_ctx->new_session_cb != NULL)) | |
d02b48c6 | 2088 | { |
58964a49 | 2089 | CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION); |
a13c20f6 | 2090 | if (!s->session_ctx->new_session_cb(s,s->session)) |
d02b48c6 RE |
2091 | SSL_SESSION_free(s->session); |
2092 | } | |
2093 | ||
2094 | /* auto flush every 255 connections */ | |
58964a49 RE |
2095 | if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && |
2096 | ((i & mode) == mode)) | |
2097 | { | |
2098 | if ( (((mode & SSL_SESS_CACHE_CLIENT) | |
a13c20f6 BM |
2099 | ?s->session_ctx->stats.sess_connect_good |
2100 | :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) | |
58964a49 | 2101 | { |
a13c20f6 | 2102 | SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL)); |
58964a49 RE |
2103 | } |
2104 | } | |
d02b48c6 RE |
2105 | } |
2106 | ||
4ebb342f | 2107 | const SSL_METHOD *SSL_get_ssl_method(SSL *s) |
d02b48c6 RE |
2108 | { |
2109 | return(s->method); | |
2110 | } | |
2111 | ||
4ebb342f | 2112 | int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth) |
d02b48c6 RE |
2113 | { |
2114 | int conn= -1; | |
2115 | int ret=1; | |
2116 | ||
2117 | if (s->method != meth) | |
2118 | { | |
2119 | if (s->handshake_func != NULL) | |
2120 | conn=(s->handshake_func == s->method->ssl_connect); | |
2121 | ||
2122 | if (s->method->version == meth->version) | |
2123 | s->method=meth; | |
2124 | else | |
2125 | { | |
2126 | s->method->ssl_free(s); | |
2127 | s->method=meth; | |
2128 | ret=s->method->ssl_new(s); | |
2129 | } | |
2130 | ||
2131 | if (conn == 1) | |
2132 | s->handshake_func=meth->ssl_connect; | |
2133 | else if (conn == 0) | |
2134 | s->handshake_func=meth->ssl_accept; | |
2135 | } | |
2136 | return(ret); | |
2137 | } | |
2138 | ||
0821bcd4 | 2139 | int SSL_get_error(const SSL *s,int i) |
d02b48c6 RE |
2140 | { |
2141 | int reason; | |
413c4f45 | 2142 | unsigned long l; |
d02b48c6 RE |
2143 | BIO *bio; |
2144 | ||
2145 | if (i > 0) return(SSL_ERROR_NONE); | |
2146 | ||
413c4f45 MC |
2147 | /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake |
2148 | * etc, where we do encode the error */ | |
2149 | if ((l=ERR_peek_error()) != 0) | |
2150 | { | |
2151 | if (ERR_GET_LIB(l) == ERR_LIB_SYS) | |
2152 | return(SSL_ERROR_SYSCALL); | |
2153 | else | |
2154 | return(SSL_ERROR_SSL); | |
2155 | } | |
d02b48c6 RE |
2156 | |
2157 | if ((i < 0) && SSL_want_read(s)) | |
2158 | { | |
2159 | bio=SSL_get_rbio(s); | |
2160 | if (BIO_should_read(bio)) | |
2161 | return(SSL_ERROR_WANT_READ); | |
2162 | else if (BIO_should_write(bio)) | |
3a66e306 BM |
2163 | /* This one doesn't make too much sense ... We never try |
2164 | * to write to the rbio, and an application program where | |
2165 | * rbio and wbio are separate couldn't even know what it | |
2166 | * should wait for. | |
2167 | * However if we ever set s->rwstate incorrectly | |
2168 | * (so that we have SSL_want_read(s) instead of | |
2169 | * SSL_want_write(s)) and rbio and wbio *are* the same, | |
2170 | * this test works around that bug; so it might be safer | |
2171 | * to keep it. */ | |
d02b48c6 RE |
2172 | return(SSL_ERROR_WANT_WRITE); |
2173 | else if (BIO_should_io_special(bio)) | |
2174 | { | |
2175 | reason=BIO_get_retry_reason(bio); | |
2176 | if (reason == BIO_RR_CONNECT) | |
2177 | return(SSL_ERROR_WANT_CONNECT); | |
924046ce DSH |
2178 | else if (reason == BIO_RR_ACCEPT) |
2179 | return(SSL_ERROR_WANT_ACCEPT); | |
d02b48c6 RE |
2180 | else |
2181 | return(SSL_ERROR_SYSCALL); /* unknown */ | |
2182 | } | |
2183 | } | |
2184 | ||
2185 | if ((i < 0) && SSL_want_write(s)) | |
2186 | { | |
2187 | bio=SSL_get_wbio(s); | |
2188 | if (BIO_should_write(bio)) | |
2189 | return(SSL_ERROR_WANT_WRITE); | |
2190 | else if (BIO_should_read(bio)) | |
3a66e306 | 2191 | /* See above (SSL_want_read(s) with BIO_should_write(bio)) */ |
d02b48c6 RE |
2192 | return(SSL_ERROR_WANT_READ); |
2193 | else if (BIO_should_io_special(bio)) | |
2194 | { | |
2195 | reason=BIO_get_retry_reason(bio); | |
2196 | if (reason == BIO_RR_CONNECT) | |
2197 | return(SSL_ERROR_WANT_CONNECT); | |
924046ce DSH |
2198 | else if (reason == BIO_RR_ACCEPT) |
2199 | return(SSL_ERROR_WANT_ACCEPT); | |
d02b48c6 RE |
2200 | else |
2201 | return(SSL_ERROR_SYSCALL); | |
2202 | } | |
2203 | } | |
2204 | if ((i < 0) && SSL_want_x509_lookup(s)) | |
2205 | { | |
2206 | return(SSL_ERROR_WANT_X509_LOOKUP); | |
2207 | } | |
2208 | ||
2209 | if (i == 0) | |
2210 | { | |
58964a49 | 2211 | if (s->version == SSL2_VERSION) |
d02b48c6 RE |
2212 | { |
2213 | /* assume it is the socket being closed */ | |
2214 | return(SSL_ERROR_ZERO_RETURN); | |
2215 | } | |
2216 | else | |
2217 | { | |
2218 | if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && | |
58964a49 | 2219 | (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) |
d02b48c6 RE |
2220 | return(SSL_ERROR_ZERO_RETURN); |
2221 | } | |
2222 | } | |
2223 | return(SSL_ERROR_SYSCALL); | |
2224 | } | |
2225 | ||
4f43d0e7 | 2226 | int SSL_do_handshake(SSL *s) |
d02b48c6 | 2227 | { |
58964a49 RE |
2228 | int ret=1; |
2229 | ||
d02b48c6 RE |
2230 | if (s->handshake_func == NULL) |
2231 | { | |
58964a49 | 2232 | SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET); |
d02b48c6 RE |
2233 | return(-1); |
2234 | } | |
dfeab068 RE |
2235 | |
2236 | s->method->ssl_renegotiate_check(s); | |
2237 | ||
d02b48c6 | 2238 | if (SSL_in_init(s) || SSL_in_before(s)) |
58964a49 RE |
2239 | { |
2240 | ret=s->handshake_func(s); | |
2241 | } | |
2242 | return(ret); | |
d02b48c6 RE |
2243 | } |
2244 | ||
2245 | /* For the next 2 functions, SSL_clear() sets shutdown and so | |
2246 | * one of these calls will reset it */ | |
4f43d0e7 | 2247 | void SSL_set_accept_state(SSL *s) |
d02b48c6 | 2248 | { |
413c4f45 | 2249 | s->server=1; |
d02b48c6 RE |
2250 | s->shutdown=0; |
2251 | s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE; | |
2252 | s->handshake_func=s->method->ssl_accept; | |
2253 | /* clear the current cipher */ | |
2254 | ssl_clear_cipher_ctx(s); | |
b948e2c5 DSH |
2255 | ssl_clear_hash_ctx(&s->read_hash); |
2256 | ssl_clear_hash_ctx(&s->write_hash); | |
d02b48c6 RE |
2257 | } |
2258 | ||
4f43d0e7 | 2259 | void SSL_set_connect_state(SSL *s) |
d02b48c6 | 2260 | { |
413c4f45 | 2261 | s->server=0; |
d02b48c6 RE |
2262 | s->shutdown=0; |
2263 | s->state=SSL_ST_CONNECT|SSL_ST_BEFORE; | |
2264 | s->handshake_func=s->method->ssl_connect; | |
2265 | /* clear the current cipher */ | |
2266 | ssl_clear_cipher_ctx(s); | |
b948e2c5 DSH |
2267 | ssl_clear_hash_ctx(&s->read_hash); |
2268 | ssl_clear_hash_ctx(&s->write_hash); | |
d02b48c6 RE |
2269 | } |
2270 | ||
4f43d0e7 | 2271 | int ssl_undefined_function(SSL *s) |
d02b48c6 RE |
2272 | { |
2273 | SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
2274 | return(0); | |
2275 | } | |
2276 | ||
41a15c4f BL |
2277 | int ssl_undefined_void_function(void) |
2278 | { | |
2279 | SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
2280 | return(0); | |
2281 | } | |
2282 | ||
0821bcd4 BL |
2283 | int ssl_undefined_const_function(const SSL *s) |
2284 | { | |
2285 | SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
2286 | return(0); | |
2287 | } | |
2288 | ||
4f43d0e7 | 2289 | SSL_METHOD *ssl_bad_method(int ver) |
d02b48c6 RE |
2290 | { |
2291 | SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
2292 | return(NULL); | |
2293 | } | |
2294 | ||
0821bcd4 | 2295 | const char *SSL_get_version(const SSL *s) |
d02b48c6 | 2296 | { |
58964a49 RE |
2297 | if (s->version == TLS1_VERSION) |
2298 | return("TLSv1"); | |
2299 | else if (s->version == SSL3_VERSION) | |
d02b48c6 | 2300 | return("SSLv3"); |
58964a49 | 2301 | else if (s->version == SSL2_VERSION) |
d02b48c6 RE |
2302 | return("SSLv2"); |
2303 | else | |
2304 | return("unknown"); | |
2305 | } | |
2306 | ||
4f43d0e7 | 2307 | SSL *SSL_dup(SSL *s) |
8a41eb70 | 2308 | { |
f73e07cf | 2309 | STACK_OF(X509_NAME) *sk; |
d02b48c6 | 2310 | X509_NAME *xn; |
b1c4fe36 | 2311 | SSL *ret; |
d02b48c6 | 2312 | int i; |
52b8dad8 | 2313 | |
b4cadc6e BL |
2314 | if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) |
2315 | return(NULL); | |
bf21446a BM |
2316 | |
2317 | ret->version = s->version; | |
2318 | ret->type = s->type; | |
2319 | ret->method = s->method; | |
2320 | ||
8a41eb70 BM |
2321 | if (s->session != NULL) |
2322 | { | |
2323 | /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */ | |
2324 | SSL_copy_session_id(ret,s); | |
2325 | } | |
2326 | else | |
2327 | { | |
2328 | /* No session has been established yet, so we have to expect | |
2329 | * that s->cert or ret->cert will be changed later -- | |
2330 | * they should not both point to the same object, | |
2331 | * and thus we can't use SSL_copy_session_id. */ | |
2332 | ||
2e60ea76 | 2333 | ret->method->ssl_free(ret); |
8a41eb70 BM |
2334 | ret->method = s->method; |
2335 | ret->method->ssl_new(ret); | |
2336 | ||
2337 | if (s->cert != NULL) | |
2338 | { | |
34d69d3b RL |
2339 | if (ret->cert != NULL) |
2340 | { | |
2341 | ssl_cert_free(ret->cert); | |
2342 | } | |
8a41eb70 BM |
2343 | ret->cert = ssl_cert_dup(s->cert); |
2344 | if (ret->cert == NULL) | |
2345 | goto err; | |
2346 | } | |
2347 | ||
2348 | SSL_set_session_id_context(ret, | |
2349 | s->sid_ctx, s->sid_ctx_length); | |
2350 | } | |
d02b48c6 | 2351 | |
bf21446a BM |
2352 | ret->options=s->options; |
2353 | ret->mode=s->mode; | |
c0f5dd07 | 2354 | SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s)); |
d02b48c6 | 2355 | SSL_set_read_ahead(ret,SSL_get_read_ahead(s)); |
bf21446a BM |
2356 | ret->msg_callback = s->msg_callback; |
2357 | ret->msg_callback_arg = s->msg_callback_arg; | |
d02b48c6 RE |
2358 | SSL_set_verify(ret,SSL_get_verify_mode(s), |
2359 | SSL_get_verify_callback(s)); | |
7f89714e | 2360 | SSL_set_verify_depth(ret,SSL_get_verify_depth(s)); |
bf21446a | 2361 | ret->generate_session_id = s->generate_session_id; |
d02b48c6 RE |
2362 | |
2363 | SSL_set_info_callback(ret,SSL_get_info_callback(s)); | |
2364 | ||
2365 | ret->debug=s->debug; | |
2366 | ||
2367 | /* copy app data, a little dangerous perhaps */ | |
79aa04ef | 2368 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data)) |
58964a49 | 2369 | goto err; |
d02b48c6 RE |
2370 | |
2371 | /* setup rbio, and wbio */ | |
2372 | if (s->rbio != NULL) | |
2373 | { | |
2374 | if (!BIO_dup_state(s->rbio,(char *)&ret->rbio)) | |
2375 | goto err; | |
2376 | } | |
2377 | if (s->wbio != NULL) | |
2378 | { | |
2379 | if (s->wbio != s->rbio) | |
2380 | { | |
58964a49 | 2381 | if (!BIO_dup_state(s->wbio,(char *)&ret->wbio)) |
d02b48c6 RE |
2382 | goto err; |
2383 | } | |
2384 | else | |
2385 | ret->wbio=ret->rbio; | |
2386 | } | |
bf21446a BM |
2387 | ret->rwstate = s->rwstate; |
2388 | ret->in_handshake = s->in_handshake; | |
2389 | ret->handshake_func = s->handshake_func; | |
2390 | ret->server = s->server; | |
2391 | ret->new_session = s->new_session; | |
2392 | ret->quiet_shutdown = s->quiet_shutdown; | |
2393 | ret->shutdown=s->shutdown; | |
2394 | ret->state=s->state; /* SSL_dup does not really work at any state, though */ | |
2395 | ret->rstate=s->rstate; | |
2396 | ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */ | |
2397 | ret->hit=s->hit; | |
5d7c222d DSH |
2398 | |
2399 | X509_VERIFY_PARAM_inherit(ret->param, s->param); | |
d02b48c6 RE |
2400 | |
2401 | /* dup the cipher_list and cipher_list_by_id stacks */ | |
2402 | if (s->cipher_list != NULL) | |
2403 | { | |
f73e07cf | 2404 | if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) |
d02b48c6 RE |
2405 | goto err; |
2406 | } | |
2407 | if (s->cipher_list_by_id != NULL) | |
f73e07cf | 2408 | if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id)) |
d02b48c6 RE |
2409 | == NULL) |
2410 | goto err; | |
2411 | ||
2412 | /* Dup the client_CA list */ | |
2413 | if (s->client_CA != NULL) | |
2414 | { | |
f73e07cf | 2415 | if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err; |
d02b48c6 | 2416 | ret->client_CA=sk; |
f73e07cf | 2417 | for (i=0; i<sk_X509_NAME_num(sk); i++) |
d02b48c6 | 2418 | { |
f73e07cf BL |
2419 | xn=sk_X509_NAME_value(sk,i); |
2420 | if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL) | |
d02b48c6 RE |
2421 | { |
2422 | X509_NAME_free(xn); | |
2423 | goto err; | |
2424 | } | |
2425 | } | |
2426 | } | |
2427 | ||
d02b48c6 RE |
2428 | if (0) |
2429 | { | |
2430 | err: | |
2431 | if (ret != NULL) SSL_free(ret); | |
2432 | ret=NULL; | |
2433 | } | |
2434 | return(ret); | |
2435 | } | |
2436 | ||
4f43d0e7 | 2437 | void ssl_clear_cipher_ctx(SSL *s) |
d02b48c6 | 2438 | { |
8a41eb70 BM |
2439 | if (s->enc_read_ctx != NULL) |
2440 | { | |
2441 | EVP_CIPHER_CTX_cleanup(s->enc_read_ctx); | |
26a3a48d | 2442 | OPENSSL_free(s->enc_read_ctx); |
8a41eb70 BM |
2443 | s->enc_read_ctx=NULL; |
2444 | } | |
2445 | if (s->enc_write_ctx != NULL) | |
2446 | { | |
2447 | EVP_CIPHER_CTX_cleanup(s->enc_write_ctx); | |
26a3a48d | 2448 | OPENSSL_free(s->enc_write_ctx); |
8a41eb70 BM |
2449 | s->enc_write_ctx=NULL; |
2450 | } | |
09b6c2ef | 2451 | #ifndef OPENSSL_NO_COMP |
413c4f45 MC |
2452 | if (s->expand != NULL) |
2453 | { | |
2454 | COMP_CTX_free(s->expand); | |
2455 | s->expand=NULL; | |
2456 | } | |
2457 | if (s->compress != NULL) | |
2458 | { | |
2459 | COMP_CTX_free(s->compress); | |
2460 | s->compress=NULL; | |
2461 | } | |
09b6c2ef | 2462 | #endif |
d02b48c6 RE |
2463 | } |
2464 | ||
58964a49 | 2465 | /* Fix this function so that it takes an optional type parameter */ |
0821bcd4 | 2466 | X509 *SSL_get_certificate(const SSL *s) |
d02b48c6 RE |
2467 | { |
2468 | if (s->cert != NULL) | |
2469 | return(s->cert->key->x509); | |
2470 | else | |
2471 | return(NULL); | |
2472 | } | |
2473 | ||
58964a49 | 2474 | /* Fix this function so that it takes an optional type parameter */ |
4f43d0e7 | 2475 | EVP_PKEY *SSL_get_privatekey(SSL *s) |
d02b48c6 RE |
2476 | { |
2477 | if (s->cert != NULL) | |
2478 | return(s->cert->key->privatekey); | |
2479 | else | |
2480 | return(NULL); | |
2481 | } | |
2482 | ||
0821bcd4 | 2483 | SSL_CIPHER *SSL_get_current_cipher(const SSL *s) |
d02b48c6 | 2484 | { |
b1c4fe36 BM |
2485 | if ((s->session != NULL) && (s->session->cipher != NULL)) |
2486 | return(s->session->cipher); | |
2487 | return(NULL); | |
d02b48c6 | 2488 | } |
09b6c2ef DSH |
2489 | #ifdef OPENSSL_NO_COMP |
2490 | const void *SSL_get_current_compression(SSL *s) | |
2491 | { | |
2492 | return NULL; | |
2493 | } | |
2494 | const void *SSL_get_current_expansion(SSL *s) | |
2495 | { | |
2496 | return NULL; | |
2497 | } | |
2498 | #else | |
d02b48c6 | 2499 | |
377dcdba RL |
2500 | const COMP_METHOD *SSL_get_current_compression(SSL *s) |
2501 | { | |
2502 | if (s->compress != NULL) | |
2503 | return(s->compress->meth); | |
2504 | return(NULL); | |
2505 | } | |
2506 | ||
2507 | const COMP_METHOD *SSL_get_current_expansion(SSL *s) | |
2508 | { | |
2509 | if (s->expand != NULL) | |
2510 | return(s->expand->meth); | |
2511 | return(NULL); | |
2512 | } | |
09b6c2ef | 2513 | #endif |
377dcdba | 2514 | |
4f43d0e7 | 2515 | int ssl_init_wbio_buffer(SSL *s,int push) |
58964a49 RE |
2516 | { |
2517 | BIO *bbio; | |
2518 | ||
2519 | if (s->bbio == NULL) | |
2520 | { | |
2521 | bbio=BIO_new(BIO_f_buffer()); | |
2522 | if (bbio == NULL) return(0); | |
2523 | s->bbio=bbio; | |
2524 | } | |
2525 | else | |
2526 | { | |
2527 | bbio=s->bbio; | |
2528 | if (s->bbio == s->wbio) | |
2529 | s->wbio=BIO_pop(s->wbio); | |
2530 | } | |
d58d092b | 2531 | (void)BIO_reset(bbio); |
58964a49 RE |
2532 | /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ |
2533 | if (!BIO_set_read_buffer_size(bbio,1)) | |
2534 | { | |
2535 | SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB); | |
2536 | return(0); | |
2537 | } | |
2538 | if (push) | |
2539 | { | |
2540 | if (s->wbio != bbio) | |
2541 | s->wbio=BIO_push(bbio,s->wbio); | |
2542 | } | |
2543 | else | |
2544 | { | |
2545 | if (s->wbio == bbio) | |
2546 | s->wbio=BIO_pop(bbio); | |
2547 | } | |
2548 | return(1); | |
2549 | } | |
413c4f45 | 2550 | |
4f43d0e7 | 2551 | void ssl_free_wbio_buffer(SSL *s) |
413c4f45 | 2552 | { |
413c4f45 MC |
2553 | if (s->bbio == NULL) return; |
2554 | ||
2555 | if (s->bbio == s->wbio) | |
2556 | { | |
2557 | /* remove buffering */ | |
bbb8de09 BM |
2558 | s->wbio=BIO_pop(s->wbio); |
2559 | #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */ | |
2560 | assert(s->wbio != NULL); | |
52b8dad8 | 2561 | #endif |
bbb8de09 | 2562 | } |
413c4f45 MC |
2563 | BIO_free(s->bbio); |
2564 | s->bbio=NULL; | |
2565 | } | |
58964a49 | 2566 | |
4f43d0e7 | 2567 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode) |
58964a49 RE |
2568 | { |
2569 | ctx->quiet_shutdown=mode; | |
2570 | } | |
2571 | ||
0821bcd4 | 2572 | int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) |
58964a49 RE |
2573 | { |
2574 | return(ctx->quiet_shutdown); | |
2575 | } | |
2576 | ||
4f43d0e7 | 2577 | void SSL_set_quiet_shutdown(SSL *s,int mode) |
58964a49 RE |
2578 | { |
2579 | s->quiet_shutdown=mode; | |
2580 | } | |
2581 | ||
0821bcd4 | 2582 | int SSL_get_quiet_shutdown(const SSL *s) |
58964a49 RE |
2583 | { |
2584 | return(s->quiet_shutdown); | |
2585 | } | |
2586 | ||
4f43d0e7 | 2587 | void SSL_set_shutdown(SSL *s,int mode) |
58964a49 RE |
2588 | { |
2589 | s->shutdown=mode; | |
2590 | } | |
2591 | ||
0821bcd4 | 2592 | int SSL_get_shutdown(const SSL *s) |
58964a49 RE |
2593 | { |
2594 | return(s->shutdown); | |
2595 | } | |
2596 | ||
0821bcd4 | 2597 | int SSL_version(const SSL *s) |
58964a49 RE |
2598 | { |
2599 | return(s->version); | |
2600 | } | |
2601 | ||
0821bcd4 | 2602 | SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) |
58964a49 RE |
2603 | { |
2604 | return(ssl->ctx); | |
2605 | } | |
2606 | ||
ed3883d2 BM |
2607 | SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx) |
2608 | { | |
52b8dad8 | 2609 | if (ssl->ctx == ctx) |
a13c20f6 | 2610 | return ssl->ctx; |
367eb1f1 | 2611 | #ifndef OPENSSL_NO_TLSEXT |
a13c20f6 BM |
2612 | if (ctx == NULL) |
2613 | ctx = ssl->initial_ctx; | |
367eb1f1 | 2614 | #endif |
ed3883d2 BM |
2615 | if (ssl->cert != NULL) |
2616 | ssl_cert_free(ssl->cert); | |
2617 | ssl->cert = ssl_cert_dup(ctx->cert); | |
2618 | CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); | |
2619 | if (ssl->ctx != NULL) | |
2620 | SSL_CTX_free(ssl->ctx); /* decrement reference count */ | |
2621 | ssl->ctx = ctx; | |
2622 | return(ssl->ctx); | |
2623 | } | |
2624 | ||
bc36ee62 | 2625 | #ifndef OPENSSL_NO_STDIO |
4f43d0e7 | 2626 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) |
58964a49 RE |
2627 | { |
2628 | return(X509_STORE_set_default_paths(ctx->cert_store)); | |
2629 | } | |
2630 | ||
303c0028 BM |
2631 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, |
2632 | const char *CApath) | |
58964a49 RE |
2633 | { |
2634 | return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath)); | |
2635 | } | |
dfeab068 | 2636 | #endif |
58964a49 | 2637 | |
45d87a1f | 2638 | void SSL_set_info_callback(SSL *ssl, |
7806f3dd | 2639 | void (*cb)(const SSL *ssl,int type,int val)) |
58964a49 RE |
2640 | { |
2641 | ssl->info_callback=cb; | |
2642 | } | |
2643 | ||
543b4ecc RL |
2644 | /* One compiler (Diab DCC) doesn't like argument names in returned |
2645 | function pointer. */ | |
52b8dad8 | 2646 | void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/) |
58964a49 | 2647 | { |
45d87a1f | 2648 | return ssl->info_callback; |
58964a49 RE |
2649 | } |
2650 | ||
0821bcd4 | 2651 | int SSL_state(const SSL *ssl) |
58964a49 RE |
2652 | { |
2653 | return(ssl->state); | |
2654 | } | |
2655 | ||
4f43d0e7 | 2656 | void SSL_set_verify_result(SSL *ssl,long arg) |
58964a49 RE |
2657 | { |
2658 | ssl->verify_result=arg; | |
2659 | } | |
2660 | ||
0821bcd4 | 2661 | long SSL_get_verify_result(const SSL *ssl) |
58964a49 RE |
2662 | { |
2663 | return(ssl->verify_result); | |
2664 | } | |
2665 | ||
dd9d233e DSH |
2666 | int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, |
2667 | CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) | |
b1c4fe36 | 2668 | { |
79aa04ef GT |
2669 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, |
2670 | new_func, dup_func, free_func); | |
b1c4fe36 | 2671 | } |
58964a49 | 2672 | |
4f43d0e7 | 2673 | int SSL_set_ex_data(SSL *s,int idx,void *arg) |
58964a49 RE |
2674 | { |
2675 | return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); | |
2676 | } | |
2677 | ||
0821bcd4 | 2678 | void *SSL_get_ex_data(const SSL *s,int idx) |
58964a49 RE |
2679 | { |
2680 | return(CRYPTO_get_ex_data(&s->ex_data,idx)); | |
2681 | } | |
2682 | ||
dd9d233e DSH |
2683 | int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, |
2684 | CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) | |
b1c4fe36 | 2685 | { |
79aa04ef GT |
2686 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, |
2687 | new_func, dup_func, free_func); | |
b1c4fe36 | 2688 | } |
58964a49 | 2689 | |
4f43d0e7 | 2690 | int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg) |
58964a49 RE |
2691 | { |
2692 | return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); | |
2693 | } | |
2694 | ||
0821bcd4 | 2695 | void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx) |
58964a49 RE |
2696 | { |
2697 | return(CRYPTO_get_ex_data(&s->ex_data,idx)); | |
2698 | } | |
2699 | ||
4f43d0e7 | 2700 | int ssl_ok(SSL *s) |
dfeab068 RE |
2701 | { |
2702 | return(1); | |
2703 | } | |
2704 | ||
0821bcd4 | 2705 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) |
413c4f45 MC |
2706 | { |
2707 | return(ctx->cert_store); | |
2708 | } | |
2709 | ||
4f43d0e7 | 2710 | void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store) |
413c4f45 MC |
2711 | { |
2712 | if (ctx->cert_store != NULL) | |
2713 | X509_STORE_free(ctx->cert_store); | |
2714 | ctx->cert_store=store; | |
2715 | } | |
2716 | ||
0821bcd4 | 2717 | int SSL_want(const SSL *s) |
413c4f45 MC |
2718 | { |
2719 | return(s->rwstate); | |
2720 | } | |
2721 | ||
4f43d0e7 BL |
2722 | /*! |
2723 | * \brief Set the callback for generating temporary RSA keys. | |
2724 | * \param ctx the SSL context. | |
2725 | * \param cb the callback | |
2726 | */ | |
2727 | ||
bc36ee62 | 2728 | #ifndef OPENSSL_NO_RSA |
df63a389 UM |
2729 | void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl, |
2730 | int is_export, | |
60e31c3a | 2731 | int keylength)) |
a9188d4e | 2732 | { |
41a15c4f | 2733 | SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); |
a9188d4e | 2734 | } |
79df9d62 | 2735 | |
d3442bc7 RL |
2736 | void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl, |
2737 | int is_export, | |
2738 | int keylength)) | |
a9188d4e | 2739 | { |
41a15c4f | 2740 | SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); |
a9188d4e | 2741 | } |
79df9d62 | 2742 | #endif |
f8c3c05d | 2743 | |
4f43d0e7 BL |
2744 | #ifdef DOXYGEN |
2745 | /*! | |
2746 | * \brief The RSA temporary key callback function. | |
2747 | * \param ssl the SSL session. | |
df63a389 UM |
2748 | * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite. |
2749 | * \param keylength if \c is_export is \c TRUE, then \c keylength is the size | |
2750 | * of the required key in bits. | |
4f43d0e7 BL |
2751 | * \return the temporary RSA key. |
2752 | * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback | |
2753 | */ | |
2754 | ||
df63a389 | 2755 | RSA *cb(SSL *ssl,int is_export,int keylength) |
4f43d0e7 BL |
2756 | {} |
2757 | #endif | |
2758 | ||
2759 | /*! | |
2760 | * \brief Set the callback for generating temporary DH keys. | |
2761 | * \param ctx the SSL context. | |
2762 | * \param dh the callback | |
2763 | */ | |
2764 | ||
bc36ee62 | 2765 | #ifndef OPENSSL_NO_DH |
df63a389 | 2766 | void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export, |
52b8dad8 | 2767 | int keylength)) |
a661b653 | 2768 | { |
41a15c4f | 2769 | SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); |
a661b653 | 2770 | } |
f8c3c05d | 2771 | |
df63a389 | 2772 | void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export, |
52b8dad8 | 2773 | int keylength)) |
a661b653 | 2774 | { |
41a15c4f | 2775 | SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); |
a661b653 | 2776 | } |
79df9d62 | 2777 | #endif |
15d21c2d | 2778 | |
ea262260 BM |
2779 | #ifndef OPENSSL_NO_ECDH |
2780 | void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export, | |
52b8dad8 | 2781 | int keylength)) |
ea262260 | 2782 | { |
41a15c4f | 2783 | SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); |
ea262260 BM |
2784 | } |
2785 | ||
2786 | void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export, | |
52b8dad8 | 2787 | int keylength)) |
ea262260 | 2788 | { |
41a15c4f | 2789 | SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); |
ea262260 BM |
2790 | } |
2791 | #endif | |
2792 | ||
ddac1974 NL |
2793 | #ifndef OPENSSL_NO_PSK |
2794 | int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) | |
2795 | { | |
2796 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) | |
2797 | { | |
2798 | SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG); | |
2799 | return 0; | |
2800 | } | |
2801 | if (ctx->psk_identity_hint != NULL) | |
2802 | OPENSSL_free(ctx->psk_identity_hint); | |
2803 | if (identity_hint != NULL) | |
2804 | { | |
2805 | ctx->psk_identity_hint = BUF_strdup(identity_hint); | |
2806 | if (ctx->psk_identity_hint == NULL) | |
2807 | return 0; | |
2808 | } | |
2809 | else | |
2810 | ctx->psk_identity_hint = NULL; | |
2811 | return 1; | |
2812 | } | |
2813 | ||
2814 | int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) | |
2815 | { | |
2816 | if (s == NULL) | |
2817 | return 0; | |
2818 | ||
2819 | if (s->session == NULL) | |
2820 | return 1; /* session not created yet, ignored */ | |
2821 | ||
2822 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) | |
2823 | { | |
2824 | SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG); | |
2825 | return 0; | |
2826 | } | |
2827 | if (s->session->psk_identity_hint != NULL) | |
2828 | OPENSSL_free(s->session->psk_identity_hint); | |
2829 | if (identity_hint != NULL) | |
2830 | { | |
2831 | s->session->psk_identity_hint = BUF_strdup(identity_hint); | |
2832 | if (s->session->psk_identity_hint == NULL) | |
2833 | return 0; | |
2834 | } | |
2835 | else | |
2836 | s->session->psk_identity_hint = NULL; | |
2837 | return 1; | |
2838 | } | |
2839 | ||
2840 | const char *SSL_get_psk_identity_hint(const SSL *s) | |
2841 | { | |
2842 | if (s == NULL || s->session == NULL) | |
2843 | return NULL; | |
2844 | return(s->session->psk_identity_hint); | |
2845 | } | |
2846 | ||
2847 | const char *SSL_get_psk_identity(const SSL *s) | |
2848 | { | |
2849 | if (s == NULL || s->session == NULL) | |
2850 | return NULL; | |
2851 | return(s->session->psk_identity); | |
2852 | } | |
7806f3dd | 2853 | |
52b8dad8 BM |
2854 | void SSL_set_psk_client_callback(SSL *s, |
2855 | unsigned int (*cb)(SSL *ssl, const char *hint, | |
2856 | char *identity, unsigned int max_identity_len, unsigned char *psk, | |
2857 | unsigned int max_psk_len)) | |
7806f3dd | 2858 | { |
52b8dad8 | 2859 | s->psk_client_callback = cb; |
7806f3dd NL |
2860 | } |
2861 | ||
2862 | void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, | |
52b8dad8 BM |
2863 | unsigned int (*cb)(SSL *ssl, const char *hint, |
2864 | char *identity, unsigned int max_identity_len, unsigned char *psk, | |
2865 | unsigned int max_psk_len)) | |
7806f3dd | 2866 | { |
52b8dad8 | 2867 | ctx->psk_client_callback = cb; |
7806f3dd NL |
2868 | } |
2869 | ||
52b8dad8 BM |
2870 | void SSL_set_psk_server_callback(SSL *s, |
2871 | unsigned int (*cb)(SSL *ssl, const char *identity, | |
2872 | unsigned char *psk, unsigned int max_psk_len)) | |
7806f3dd | 2873 | { |
52b8dad8 | 2874 | s->psk_server_callback = cb; |
7806f3dd NL |
2875 | } |
2876 | ||
2877 | void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, | |
52b8dad8 BM |
2878 | unsigned int (*cb)(SSL *ssl, const char *identity, |
2879 | unsigned char *psk, unsigned int max_psk_len)) | |
7806f3dd | 2880 | { |
52b8dad8 | 2881 | ctx->psk_server_callback = cb; |
7806f3dd | 2882 | } |
ddac1974 | 2883 | #endif |
a661b653 BM |
2884 | |
2885 | void SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) | |
2886 | { | |
41a15c4f | 2887 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); |
a661b653 BM |
2888 | } |
2889 | void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) | |
2890 | { | |
41a15c4f | 2891 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); |
a661b653 BM |
2892 | } |
2893 | ||
b948e2c5 DSH |
2894 | /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer |
2895 | * vairable, freeing EVP_MD_CTX previously stored in that variable, if | |
2896 | * any. If EVP_MD pointer is passed, initializes ctx with this md | |
2897 | * Returns newly allocated ctx; | |
2898 | */ | |
2899 | ||
2900 | EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md) | |
2901 | { | |
2902 | ssl_clear_hash_ctx(hash); | |
2903 | *hash = EVP_MD_CTX_create(); | |
2904 | if (md) EVP_DigestInit_ex(*hash,md,NULL); | |
2905 | return *hash; | |
2906 | } | |
2907 | void ssl_clear_hash_ctx(EVP_MD_CTX **hash) | |
2908 | { | |
2909 | ||
2910 | if (*hash) EVP_MD_CTX_destroy(*hash); | |
2911 | *hash=NULL; | |
2912 | } | |
a661b653 | 2913 | |
bc36ee62 | 2914 | #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16) |
58964a49 RE |
2915 | #include "../crypto/bio/bss_file.c" |
2916 | #endif | |
f73e07cf BL |
2917 | |
2918 | IMPLEMENT_STACK_OF(SSL_CIPHER) | |
2919 | IMPLEMENT_STACK_OF(SSL_COMP) |