]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/s3_lib.c
The BN_mul bug test apparently is no longer needed
[thirdparty/openssl.git] / ssl / s3_lib.c
CommitLineData
d02b48c6 1/* ssl/s3_lib.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
5a4fbc69
BM
58/* ====================================================================
59 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
d02b48c6
RE
111
112#include <stdio.h>
d02f751c
UM
113#include <openssl/md5.h>
114#include <openssl/sha.h>
ec577822 115#include <openssl/objects.h>
d02b48c6
RE
116#include "ssl_locl.h"
117
e778802f 118const char *ssl3_version_str="SSLv3" OPENSSL_VERSION_PTEXT;
d02b48c6
RE
119
120#define SSL3_NUM_CIPHERS (sizeof(ssl3_ciphers)/sizeof(SSL_CIPHER))
121
d02b48c6 122static long ssl3_default_timeout(void );
7d7d2cbc 123
7f0dae32 124OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
d02b48c6
RE
125/* The RSA ciphers */
126/* Cipher 01 */
127 {
128 1,
129 SSL3_TXT_RSA_NULL_MD5,
130 SSL3_CK_RSA_NULL_MD5,
018e57c7
DSH
131 SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_MD5|SSL_SSLV3,
132 SSL_NOT_EXP,
133 0,
134 0,
d02b48c6
RE
135 0,
136 SSL_ALL_CIPHERS,
018e57c7 137 SSL_ALL_STRENGTHS,
d02b48c6
RE
138 },
139/* Cipher 02 */
140 {
141 1,
142 SSL3_TXT_RSA_NULL_SHA,
143 SSL3_CK_RSA_NULL_SHA,
018e57c7
DSH
144 SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_SHA1|SSL_SSLV3,
145 SSL_NOT_EXP,
146 0,
147 0,
d02b48c6
RE
148 0,
149 SSL_ALL_CIPHERS,
018e57c7 150 SSL_ALL_STRENGTHS,
d02b48c6
RE
151 },
152
153/* anon DH */
154/* Cipher 17 */
155 {
156 1,
157 SSL3_TXT_ADH_RC4_40_MD5,
158 SSL3_CK_ADH_RC4_40_MD5,
018e57c7
DSH
159 SSL_kEDH |SSL_aNULL|SSL_RC4 |SSL_MD5 |SSL_SSLV3,
160 SSL_EXPORT|SSL_EXP40,
d02b48c6 161 0,
018e57c7
DSH
162 40,
163 128,
d02b48c6 164 SSL_ALL_CIPHERS,
018e57c7 165 SSL_ALL_STRENGTHS,
d02b48c6
RE
166 },
167/* Cipher 18 */
168 {
169 1,
170 SSL3_TXT_ADH_RC4_128_MD5,
171 SSL3_CK_ADH_RC4_128_MD5,
018e57c7
DSH
172 SSL_kEDH |SSL_aNULL|SSL_RC4 |SSL_MD5 |SSL_SSLV3,
173 SSL_NOT_EXP,
d02b48c6 174 0,
018e57c7
DSH
175 128,
176 128,
d02b48c6 177 SSL_ALL_CIPHERS,
018e57c7 178 SSL_ALL_STRENGTHS,
d02b48c6
RE
179 },
180/* Cipher 19 */
181 {
182 1,
183 SSL3_TXT_ADH_DES_40_CBC_SHA,
184 SSL3_CK_ADH_DES_40_CBC_SHA,
018e57c7
DSH
185 SSL_kEDH |SSL_aNULL|SSL_DES|SSL_SHA1|SSL_SSLV3,
186 SSL_EXPORT|SSL_EXP40,
d02b48c6 187 0,
018e57c7
DSH
188 40,
189 128,
d02b48c6 190 SSL_ALL_CIPHERS,
018e57c7 191 SSL_ALL_STRENGTHS,
d02b48c6
RE
192 },
193/* Cipher 1A */
194 {
195 1,
196 SSL3_TXT_ADH_DES_64_CBC_SHA,
197 SSL3_CK_ADH_DES_64_CBC_SHA,
018e57c7
DSH
198 SSL_kEDH |SSL_aNULL|SSL_DES |SSL_SHA1|SSL_SSLV3,
199 SSL_NOT_EXP,
d02b48c6 200 0,
018e57c7
DSH
201 56,
202 56,
d02b48c6 203 SSL_ALL_CIPHERS,
018e57c7 204 SSL_ALL_STRENGTHS,
d02b48c6
RE
205 },
206/* Cipher 1B */
207 {
208 1,
58964a49
RE
209 SSL3_TXT_ADH_DES_192_CBC_SHA,
210 SSL3_CK_ADH_DES_192_CBC_SHA,
018e57c7
DSH
211 SSL_kEDH |SSL_aNULL|SSL_3DES |SSL_SHA1|SSL_SSLV3,
212 SSL_NOT_EXP,
d02b48c6 213 0,
018e57c7
DSH
214 168,
215 168,
d02b48c6 216 SSL_ALL_CIPHERS,
018e57c7 217 SSL_ALL_STRENGTHS,
d02b48c6
RE
218 },
219
220/* RSA again */
221/* Cipher 03 */
222 {
223 1,
224 SSL3_TXT_RSA_RC4_40_MD5,
225 SSL3_CK_RSA_RC4_40_MD5,
018e57c7
DSH
226 SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_MD5 |SSL_SSLV3,
227 SSL_EXPORT|SSL_EXP40,
d02b48c6 228 0,
018e57c7
DSH
229 40,
230 128,
d02b48c6 231 SSL_ALL_CIPHERS,
018e57c7 232 SSL_ALL_STRENGTHS,
d02b48c6
RE
233 },
234/* Cipher 04 */
235 {
236 1,
237 SSL3_TXT_RSA_RC4_128_MD5,
238 SSL3_CK_RSA_RC4_128_MD5,
018e57c7
DSH
239 SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_MD5|SSL_SSLV3,
240 SSL_NOT_EXP|SSL_MEDIUM,
d02b48c6 241 0,
018e57c7
DSH
242 128,
243 128,
d02b48c6 244 SSL_ALL_CIPHERS,
018e57c7 245 SSL_ALL_STRENGTHS,
d02b48c6
RE
246 },
247/* Cipher 05 */
248 {
249 1,
250 SSL3_TXT_RSA_RC4_128_SHA,
251 SSL3_CK_RSA_RC4_128_SHA,
018e57c7
DSH
252 SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_SHA1|SSL_SSLV3,
253 SSL_NOT_EXP|SSL_MEDIUM,
d02b48c6 254 0,
018e57c7
DSH
255 128,
256 128,
d02b48c6 257 SSL_ALL_CIPHERS,
018e57c7 258 SSL_ALL_STRENGTHS,
d02b48c6
RE
259 },
260/* Cipher 06 */
261 {
262 1,
263 SSL3_TXT_RSA_RC2_40_MD5,
264 SSL3_CK_RSA_RC2_40_MD5,
018e57c7
DSH
265 SSL_kRSA|SSL_aRSA|SSL_RC2 |SSL_MD5 |SSL_SSLV3,
266 SSL_EXPORT|SSL_EXP40,
d02b48c6 267 0,
018e57c7
DSH
268 40,
269 128,
d02b48c6 270 SSL_ALL_CIPHERS,
018e57c7 271 SSL_ALL_STRENGTHS,
d02b48c6
RE
272 },
273/* Cipher 07 */
274 {
275 1,
276 SSL3_TXT_RSA_IDEA_128_SHA,
277 SSL3_CK_RSA_IDEA_128_SHA,
018e57c7
DSH
278 SSL_kRSA|SSL_aRSA|SSL_IDEA |SSL_SHA1|SSL_SSLV3,
279 SSL_NOT_EXP|SSL_MEDIUM,
d02b48c6 280 0,
018e57c7
DSH
281 128,
282 128,
d02b48c6 283 SSL_ALL_CIPHERS,
018e57c7 284 SSL_ALL_STRENGTHS,
d02b48c6
RE
285 },
286/* Cipher 08 */
287 {
288 1,
289 SSL3_TXT_RSA_DES_40_CBC_SHA,
290 SSL3_CK_RSA_DES_40_CBC_SHA,
018e57c7
DSH
291 SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_SSLV3,
292 SSL_EXPORT|SSL_EXP40,
d02b48c6 293 0,
018e57c7
DSH
294 40,
295 56,
d02b48c6 296 SSL_ALL_CIPHERS,
018e57c7 297 SSL_ALL_STRENGTHS,
d02b48c6
RE
298 },
299/* Cipher 09 */
300 {
301 1,
302 SSL3_TXT_RSA_DES_64_CBC_SHA,
303 SSL3_CK_RSA_DES_64_CBC_SHA,
018e57c7
DSH
304 SSL_kRSA|SSL_aRSA|SSL_DES |SSL_SHA1|SSL_SSLV3,
305 SSL_NOT_EXP|SSL_LOW,
d02b48c6 306 0,
018e57c7
DSH
307 56,
308 56,
d02b48c6 309 SSL_ALL_CIPHERS,
018e57c7 310 SSL_ALL_STRENGTHS,
d02b48c6
RE
311 },
312/* Cipher 0A */
313 {
314 1,
315 SSL3_TXT_RSA_DES_192_CBC3_SHA,
316 SSL3_CK_RSA_DES_192_CBC3_SHA,
018e57c7
DSH
317 SSL_kRSA|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
318 SSL_NOT_EXP|SSL_HIGH,
d02b48c6 319 0,
018e57c7
DSH
320 168,
321 168,
d02b48c6 322 SSL_ALL_CIPHERS,
018e57c7 323 SSL_ALL_STRENGTHS,
d02b48c6
RE
324 },
325
326/* The DH ciphers */
327/* Cipher 0B */
328 {
329 0,
330 SSL3_TXT_DH_DSS_DES_40_CBC_SHA,
331 SSL3_CK_DH_DSS_DES_40_CBC_SHA,
018e57c7
DSH
332 SSL_kDHd |SSL_aDH|SSL_DES|SSL_SHA1|SSL_SSLV3,
333 SSL_EXPORT|SSL_EXP40,
d02b48c6 334 0,
018e57c7
DSH
335 40,
336 56,
d02b48c6 337 SSL_ALL_CIPHERS,
018e57c7 338 SSL_ALL_STRENGTHS,
d02b48c6
RE
339 },
340/* Cipher 0C */
341 {
342 0,
343 SSL3_TXT_DH_DSS_DES_64_CBC_SHA,
344 SSL3_CK_DH_DSS_DES_64_CBC_SHA,
018e57c7
DSH
345 SSL_kDHd |SSL_aDH|SSL_DES |SSL_SHA1|SSL_SSLV3,
346 SSL_NOT_EXP|SSL_LOW,
d02b48c6 347 0,
018e57c7
DSH
348 56,
349 56,
d02b48c6 350 SSL_ALL_CIPHERS,
018e57c7 351 SSL_ALL_STRENGTHS,
d02b48c6
RE
352 },
353/* Cipher 0D */
354 {
355 0,
356 SSL3_TXT_DH_DSS_DES_192_CBC3_SHA,
357 SSL3_CK_DH_DSS_DES_192_CBC3_SHA,
018e57c7
DSH
358 SSL_kDHd |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
359 SSL_NOT_EXP|SSL_HIGH,
d02b48c6 360 0,
018e57c7
DSH
361 168,
362 168,
d02b48c6 363 SSL_ALL_CIPHERS,
018e57c7 364 SSL_ALL_STRENGTHS,
d02b48c6
RE
365 },
366/* Cipher 0E */
367 {
368 0,
369 SSL3_TXT_DH_RSA_DES_40_CBC_SHA,
370 SSL3_CK_DH_RSA_DES_40_CBC_SHA,
018e57c7
DSH
371 SSL_kDHr |SSL_aDH|SSL_DES|SSL_SHA1|SSL_SSLV3,
372 SSL_EXPORT|SSL_EXP40,
d02b48c6 373 0,
018e57c7
DSH
374 40,
375 56,
d02b48c6 376 SSL_ALL_CIPHERS,
018e57c7 377 SSL_ALL_STRENGTHS,
d02b48c6
RE
378 },
379/* Cipher 0F */
380 {
381 0,
382 SSL3_TXT_DH_RSA_DES_64_CBC_SHA,
383 SSL3_CK_DH_RSA_DES_64_CBC_SHA,
018e57c7
DSH
384 SSL_kDHr |SSL_aDH|SSL_DES |SSL_SHA1|SSL_SSLV3,
385 SSL_NOT_EXP|SSL_LOW,
d02b48c6 386 0,
018e57c7
DSH
387 56,
388 56,
d02b48c6 389 SSL_ALL_CIPHERS,
018e57c7 390 SSL_ALL_STRENGTHS,
d02b48c6
RE
391 },
392/* Cipher 10 */
393 {
394 0,
395 SSL3_TXT_DH_RSA_DES_192_CBC3_SHA,
396 SSL3_CK_DH_RSA_DES_192_CBC3_SHA,
018e57c7
DSH
397 SSL_kDHr |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
398 SSL_NOT_EXP|SSL_HIGH,
d02b48c6 399 0,
018e57c7
DSH
400 168,
401 168,
d02b48c6 402 SSL_ALL_CIPHERS,
018e57c7 403 SSL_ALL_STRENGTHS,
d02b48c6
RE
404 },
405
406/* The Ephemeral DH ciphers */
407/* Cipher 11 */
408 {
409 1,
410 SSL3_TXT_EDH_DSS_DES_40_CBC_SHA,
411 SSL3_CK_EDH_DSS_DES_40_CBC_SHA,
018e57c7
DSH
412 SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA1|SSL_SSLV3,
413 SSL_EXPORT|SSL_EXP40,
d02b48c6 414 0,
018e57c7
DSH
415 40,
416 56,
d02b48c6 417 SSL_ALL_CIPHERS,
018e57c7 418 SSL_ALL_STRENGTHS,
d02b48c6
RE
419 },
420/* Cipher 12 */
421 {
422 1,
423 SSL3_TXT_EDH_DSS_DES_64_CBC_SHA,
424 SSL3_CK_EDH_DSS_DES_64_CBC_SHA,
018e57c7
DSH
425 SSL_kEDH|SSL_aDSS|SSL_DES |SSL_SHA1|SSL_SSLV3,
426 SSL_NOT_EXP|SSL_LOW,
d02b48c6 427 0,
018e57c7
DSH
428 56,
429 56,
d02b48c6 430 SSL_ALL_CIPHERS,
018e57c7 431 SSL_ALL_STRENGTHS,
d02b48c6
RE
432 },
433/* Cipher 13 */
434 {
435 1,
436 SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA,
437 SSL3_CK_EDH_DSS_DES_192_CBC3_SHA,
018e57c7
DSH
438 SSL_kEDH|SSL_aDSS|SSL_3DES |SSL_SHA1|SSL_SSLV3,
439 SSL_NOT_EXP|SSL_HIGH,
d02b48c6 440 0,
018e57c7
DSH
441 168,
442 168,
d02b48c6 443 SSL_ALL_CIPHERS,
018e57c7 444 SSL_ALL_STRENGTHS,
d02b48c6
RE
445 },
446/* Cipher 14 */
447 {
448 1,
449 SSL3_TXT_EDH_RSA_DES_40_CBC_SHA,
450 SSL3_CK_EDH_RSA_DES_40_CBC_SHA,
018e57c7
DSH
451 SSL_kEDH|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_SSLV3,
452 SSL_EXPORT|SSL_EXP40,
d02b48c6 453 0,
018e57c7
DSH
454 40,
455 56,
d02b48c6 456 SSL_ALL_CIPHERS,
018e57c7 457 SSL_ALL_STRENGTHS,
d02b48c6
RE
458 },
459/* Cipher 15 */
460 {
461 1,
462 SSL3_TXT_EDH_RSA_DES_64_CBC_SHA,
463 SSL3_CK_EDH_RSA_DES_64_CBC_SHA,
018e57c7
DSH
464 SSL_kEDH|SSL_aRSA|SSL_DES |SSL_SHA1|SSL_SSLV3,
465 SSL_NOT_EXP|SSL_LOW,
d02b48c6 466 0,
018e57c7
DSH
467 56,
468 56,
d02b48c6 469 SSL_ALL_CIPHERS,
018e57c7 470 SSL_ALL_STRENGTHS,
d02b48c6
RE
471 },
472/* Cipher 16 */
473 {
474 1,
475 SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA,
476 SSL3_CK_EDH_RSA_DES_192_CBC3_SHA,
018e57c7
DSH
477 SSL_kEDH|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
478 SSL_NOT_EXP|SSL_HIGH,
d02b48c6 479 0,
018e57c7
DSH
480 168,
481 168,
d02b48c6 482 SSL_ALL_CIPHERS,
018e57c7 483 SSL_ALL_STRENGTHS,
d02b48c6
RE
484 },
485
486/* Fortezza */
487/* Cipher 1C */
488 {
489 0,
490 SSL3_TXT_FZA_DMS_NULL_SHA,
491 SSL3_CK_FZA_DMS_NULL_SHA,
018e57c7
DSH
492 SSL_kFZA|SSL_aFZA |SSL_eNULL |SSL_SHA1|SSL_SSLV3,
493 SSL_NOT_EXP,
494 0,
495 0,
d02b48c6
RE
496 0,
497 SSL_ALL_CIPHERS,
018e57c7 498 SSL_ALL_STRENGTHS,
d02b48c6
RE
499 },
500
501/* Cipher 1D */
502 {
503 0,
504 SSL3_TXT_FZA_DMS_FZA_SHA,
505 SSL3_CK_FZA_DMS_FZA_SHA,
018e57c7
DSH
506 SSL_kFZA|SSL_aFZA |SSL_eFZA |SSL_SHA1|SSL_SSLV3,
507 SSL_NOT_EXP,
508 0,
509 0,
d02b48c6
RE
510 0,
511 SSL_ALL_CIPHERS,
018e57c7 512 SSL_ALL_STRENGTHS,
d02b48c6
RE
513 },
514
515/* Cipher 1E */
516 {
517 0,
518 SSL3_TXT_FZA_DMS_RC4_SHA,
519 SSL3_CK_FZA_DMS_RC4_SHA,
018e57c7
DSH
520 SSL_kFZA|SSL_aFZA |SSL_RC4 |SSL_SHA1|SSL_SSLV3,
521 SSL_NOT_EXP,
d02b48c6 522 0,
018e57c7
DSH
523 128,
524 128,
d02b48c6 525 SSL_ALL_CIPHERS,
018e57c7 526 SSL_ALL_STRENGTHS,
d02b48c6
RE
527 },
528
f9b3bff6
RL
529#ifndef NO_KRB5
530/* The Kerberos ciphers
531** 20000107 VRS: And the first shall be last,
532** in hopes of avoiding the lynx ssl renegotiation problem.
533*/
534/* Cipher 21 VRS */
535 {
536 1,
537 SSL3_TXT_KRB5_DES_40_CBC_SHA,
538 SSL3_CK_KRB5_DES_40_CBC_SHA,
539 SSL_kKRB5|SSL_aKRB5| SSL_DES|SSL_SHA1 |SSL_SSLV3,
540 SSL_EXPORT|SSL_EXP40,
541 0,
542 40,
543 56,
544 SSL_ALL_CIPHERS,
545 SSL_ALL_STRENGTHS,
546 },
547
548/* Cipher 22 VRS */
549 {
550 1,
551 SSL3_TXT_KRB5_DES_40_CBC_MD5,
552 SSL3_CK_KRB5_DES_40_CBC_MD5,
553 SSL_kKRB5|SSL_aKRB5| SSL_DES|SSL_MD5 |SSL_SSLV3,
554 SSL_EXPORT|SSL_EXP40,
555 0,
556 40,
557 56,
558 SSL_ALL_CIPHERS,
559 SSL_ALL_STRENGTHS,
560 },
561
562/* Cipher 23 VRS */
563 {
564 1,
565 SSL3_TXT_KRB5_DES_64_CBC_SHA,
566 SSL3_CK_KRB5_DES_64_CBC_SHA,
567 SSL_kKRB5|SSL_aKRB5| SSL_DES|SSL_SHA1 |SSL_SSLV3,
568 SSL_NOT_EXP|SSL_LOW,
569 0,
570 56,
571 56,
572 SSL_ALL_CIPHERS,
573 SSL_ALL_STRENGTHS,
574 },
575
576/* Cipher 24 VRS */
577 {
578 1,
579 SSL3_TXT_KRB5_DES_64_CBC_MD5,
580 SSL3_CK_KRB5_DES_64_CBC_MD5,
581 SSL_kKRB5|SSL_aKRB5| SSL_DES|SSL_MD5 |SSL_SSLV3,
582 SSL_NOT_EXP|SSL_LOW,
583 0,
584 56,
585 56,
586 SSL_ALL_CIPHERS,
587 SSL_ALL_STRENGTHS,
588 },
589
590/* Cipher 25 VRS */
591 {
592 1,
593 SSL3_TXT_KRB5_DES_192_CBC3_SHA,
594 SSL3_CK_KRB5_DES_192_CBC3_SHA,
595 SSL_kKRB5|SSL_aKRB5| SSL_3DES|SSL_SHA1 |SSL_SSLV3,
596 SSL_NOT_EXP|SSL_HIGH,
597 0,
598 112,
599 168,
600 SSL_ALL_CIPHERS,
601 SSL_ALL_STRENGTHS,
602 },
603
604/* Cipher 26 VRS */
605 {
606 1,
607 SSL3_TXT_KRB5_DES_192_CBC3_MD5,
608 SSL3_CK_KRB5_DES_192_CBC3_MD5,
609 SSL_kKRB5|SSL_aKRB5| SSL_3DES|SSL_MD5 |SSL_SSLV3,
610 SSL_NOT_EXP|SSL_HIGH,
611 0,
612 112,
613 168,
614 SSL_ALL_CIPHERS,
615 SSL_ALL_STRENGTHS,
616 },
617#endif /* NO_KRB5 */
bc348244 618#if TLS1_ALLOW_EXPERIMENTAL_CIPHERSUITES
06ab81f9
BL
619 /* New TLS Export CipherSuites */
620 /* Cipher 60 */
621 {
622 1,
abed0b8a
BL
623 TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_MD5,
624 TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_MD5,
018e57c7
DSH
625 SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_TLSV1,
626 SSL_EXPORT|SSL_EXP56,
06ab81f9 627 0,
018e57c7
DSH
628 56,
629 128,
630 SSL_ALL_CIPHERS,
631 SSL_ALL_STRENGTHS,
06ab81f9
BL
632 },
633 /* Cipher 61 */
634 {
635 1,
abed0b8a
BL
636 TLS1_TXT_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
637 TLS1_CK_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
018e57c7
DSH
638 SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_TLSV1,
639 SSL_EXPORT|SSL_EXP56,
06ab81f9 640 0,
018e57c7
DSH
641 56,
642 128,
643 SSL_ALL_CIPHERS,
644 SSL_ALL_STRENGTHS,
06ab81f9
BL
645 },
646 /* Cipher 62 */
647 {
648 1,
abed0b8a
BL
649 TLS1_TXT_RSA_EXPORT1024_WITH_DES_CBC_SHA,
650 TLS1_CK_RSA_EXPORT1024_WITH_DES_CBC_SHA,
018e57c7
DSH
651 SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA|SSL_TLSV1,
652 SSL_EXPORT|SSL_EXP56,
06ab81f9 653 0,
018e57c7
DSH
654 56,
655 56,
656 SSL_ALL_CIPHERS,
657 SSL_ALL_STRENGTHS,
06ab81f9 658 },
abed0b8a
BL
659 /* Cipher 63 */
660 {
661 1,
662 TLS1_TXT_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
663 TLS1_CK_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
018e57c7
DSH
664 SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA|SSL_TLSV1,
665 SSL_EXPORT|SSL_EXP56,
abed0b8a 666 0,
018e57c7
DSH
667 56,
668 56,
669 SSL_ALL_CIPHERS,
670 SSL_ALL_STRENGTHS,
abed0b8a
BL
671 },
672 /* Cipher 64 */
673 {
674 1,
675 TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_SHA,
676 TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_SHA,
018e57c7
DSH
677 SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA|SSL_TLSV1,
678 SSL_EXPORT|SSL_EXP56,
abed0b8a 679 0,
018e57c7
DSH
680 56,
681 128,
682 SSL_ALL_CIPHERS,
683 SSL_ALL_STRENGTHS,
abed0b8a
BL
684 },
685 /* Cipher 65 */
686 {
687 1,
688 TLS1_TXT_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
689 TLS1_CK_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
018e57c7
DSH
690 SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_TLSV1,
691 SSL_EXPORT|SSL_EXP56,
abed0b8a 692 0,
018e57c7
DSH
693 56,
694 128,
695 SSL_ALL_CIPHERS,
696 SSL_ALL_STRENGTHS,
abed0b8a
BL
697 },
698 /* Cipher 66 */
699 {
700 1,
701 TLS1_TXT_DHE_DSS_WITH_RC4_128_SHA,
702 TLS1_CK_DHE_DSS_WITH_RC4_128_SHA,
703 SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_TLSV1,
018e57c7 704 SSL_NOT_EXP,
abed0b8a 705 0,
018e57c7
DSH
706 128,
707 128,
708 SSL_ALL_CIPHERS,
709 SSL_ALL_STRENGTHS
abed0b8a 710 },
bc348244 711#endif
06ab81f9 712
d02b48c6
RE
713/* end of list */
714 };
715
58964a49
RE
716static SSL3_ENC_METHOD SSLv3_enc_data={
717 ssl3_enc,
718 ssl3_mac,
719 ssl3_setup_key_block,
720 ssl3_generate_master_secret,
721 ssl3_change_cipher_state,
722 ssl3_final_finish_mac,
723 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
724 ssl3_cert_verify_mac,
725 SSL3_MD_CLIENT_FINISHED_CONST,4,
726 SSL3_MD_SERVER_FINISHED_CONST,4,
727 ssl3_alert_code,
728 };
729
d02b48c6 730static SSL_METHOD SSLv3_data= {
58964a49 731 SSL3_VERSION,
d02b48c6
RE
732 ssl3_new,
733 ssl3_clear,
734 ssl3_free,
735 ssl_undefined_function,
736 ssl_undefined_function,
737 ssl3_read,
738 ssl3_peek,
739 ssl3_write,
740 ssl3_shutdown,
741 ssl3_renegotiate,
dfeab068 742 ssl3_renegotiate_check,
d02b48c6
RE
743 ssl3_ctrl,
744 ssl3_ctx_ctrl,
745 ssl3_get_cipher_by_char,
746 ssl3_put_cipher_by_char,
747 ssl3_pending,
748 ssl3_num_ciphers,
749 ssl3_get_cipher,
750 ssl_bad_method,
751 ssl3_default_timeout,
58964a49 752 &SSLv3_enc_data,
d3442bc7
RL
753 ssl_undefined_function,
754 ssl3_callback_ctrl,
755 ssl3_ctx_callback_ctrl,
a9188d4e
RL
756 };
757
6b691a5c 758static long ssl3_default_timeout(void)
d02b48c6
RE
759 {
760 /* 2 hours, the 24 hours mentioned in the SSLv3 spec
761 * is way too long for http, the cache would over fill */
762 return(60*60*2);
763 }
764
6b691a5c 765SSL_METHOD *sslv3_base_method(void)
d02b48c6
RE
766 {
767 return(&SSLv3_data);
768 }
769
6b691a5c 770int ssl3_num_ciphers(void)
d02b48c6
RE
771 {
772 return(SSL3_NUM_CIPHERS);
773 }
774
6b691a5c 775SSL_CIPHER *ssl3_get_cipher(unsigned int u)
d02b48c6
RE
776 {
777 if (u < SSL3_NUM_CIPHERS)
778 return(&(ssl3_ciphers[SSL3_NUM_CIPHERS-1-u]));
779 else
780 return(NULL);
781 }
782
6b691a5c 783int ssl3_pending(SSL *s)
d02b48c6 784 {
24b44446
BM
785 /* The problem is that it may not be the correct record type */
786 return(s->s3->rrec.length); /* FIXME */
d02b48c6
RE
787 }
788
6b691a5c 789int ssl3_new(SSL *s)
d02b48c6 790 {
b35e9050 791 SSL3_STATE *s3;
d02b48c6 792
26a3a48d 793 if ((s3=OPENSSL_malloc(sizeof *s3)) == NULL) goto err;
b35e9050 794 memset(s3,0,sizeof *s3);
d02b48c6
RE
795
796 s->s3=s3;
d02b48c6 797
58964a49 798 s->method->ssl_clear(s);
d02b48c6
RE
799 return(1);
800err:
801 return(0);
802 }
803
6b691a5c 804void ssl3_free(SSL *s)
d02b48c6 805 {
e03ddfae
BL
806 if(s == NULL)
807 return;
808
d02b48c6
RE
809 ssl3_cleanup_key_block(s);
810 if (s->s3->rbuf.buf != NULL)
26a3a48d 811 OPENSSL_free(s->s3->rbuf.buf);
d02b48c6 812 if (s->s3->wbuf.buf != NULL)
26a3a48d 813 OPENSSL_free(s->s3->wbuf.buf);
dfeab068 814 if (s->s3->rrec.comp != NULL)
26a3a48d 815 OPENSSL_free(s->s3->rrec.comp);
d02b48c6
RE
816#ifndef NO_DH
817 if (s->s3->tmp.dh != NULL)
818 DH_free(s->s3->tmp.dh);
819#endif
820 if (s->s3->tmp.ca_names != NULL)
f73e07cf 821 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
b35e9050 822 memset(s->s3,0,sizeof *s->s3);
26a3a48d 823 OPENSSL_free(s->s3);
d02b48c6
RE
824 s->s3=NULL;
825 }
826
6b691a5c 827void ssl3_clear(SSL *s)
d02b48c6
RE
828 {
829 unsigned char *rp,*wp;
830
831 ssl3_cleanup_key_block(s);
832 if (s->s3->tmp.ca_names != NULL)
f73e07cf 833 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
d02b48c6 834
413c4f45
MC
835 if (s->s3->rrec.comp != NULL)
836 {
26a3a48d 837 OPENSSL_free(s->s3->rrec.comp);
413c4f45
MC
838 s->s3->rrec.comp=NULL;
839 }
a2a01589
BM
840#ifndef NO_DH
841 if (s->s3->tmp.dh != NULL)
842 DH_free(s->s3->tmp.dh);
843#endif
413c4f45 844
d02b48c6
RE
845 rp=s->s3->rbuf.buf;
846 wp=s->s3->wbuf.buf;
847
b35e9050 848 memset(s->s3,0,sizeof *s->s3);
58964a49
RE
849 if (rp != NULL) s->s3->rbuf.buf=rp;
850 if (wp != NULL) s->s3->wbuf.buf=wp;
dfeab068 851
413c4f45 852 ssl_free_wbio_buffer(s);
dfeab068 853
d02b48c6 854 s->packet_length=0;
58964a49
RE
855 s->s3->renegotiate=0;
856 s->s3->total_renegotiations=0;
857 s->s3->num_renegotiations=0;
858 s->s3->in_read_app_data=0;
859 s->version=SSL3_VERSION;
d02b48c6
RE
860 }
861
6b691a5c 862long ssl3_ctrl(SSL *s, int cmd, long larg, char *parg)
d02b48c6 863 {
58964a49
RE
864 int ret=0;
865
15d21c2d
RE
866#if !defined(NO_DSA) || !defined(NO_RSA)
867 if (
868#ifndef NO_RSA
869 cmd == SSL_CTRL_SET_TMP_RSA ||
870 cmd == SSL_CTRL_SET_TMP_RSA_CB ||
871#endif
872#ifndef NO_DSA
873 cmd == SSL_CTRL_SET_TMP_DH ||
874 cmd == SSL_CTRL_SET_TMP_DH_CB ||
875#endif
876 0)
877 {
ca8e5b9b 878 if (!ssl_cert_inst(&s->cert))
15d21c2d
RE
879 {
880 SSLerr(SSL_F_SSL3_CTRL, ERR_R_MALLOC_FAILURE);
881 return(0);
882 }
883 }
884#endif
885
58964a49
RE
886 switch (cmd)
887 {
888 case SSL_CTRL_GET_SESSION_REUSED:
889 ret=s->hit;
890 break;
891 case SSL_CTRL_GET_CLIENT_CERT_REQUEST:
892 break;
893 case SSL_CTRL_GET_NUM_RENEGOTIATIONS:
894 ret=s->s3->num_renegotiations;
895 break;
896 case SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS:
897 ret=s->s3->num_renegotiations;
898 s->s3->num_renegotiations=0;
899 break;
900 case SSL_CTRL_GET_TOTAL_RENEGOTIATIONS:
901 ret=s->s3->total_renegotiations;
902 break;
dfeab068 903 case SSL_CTRL_GET_FLAGS:
651d0aff 904 ret=(int)(s->s3->flags);
dfeab068 905 break;
15d21c2d
RE
906#ifndef NO_RSA
907 case SSL_CTRL_NEED_TMP_RSA:
908 if ((s->cert != NULL) && (s->cert->rsa_tmp == NULL) &&
909 ((s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
910 (EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8))))
911 ret = 1;
912 break;
913 case SSL_CTRL_SET_TMP_RSA:
914 {
915 RSA *rsa = (RSA *)parg;
e11f0de6
BM
916 if (rsa == NULL)
917 {
15d21c2d
RE
918 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
919 return(ret);
e11f0de6
BM
920 }
921 if ((rsa = RSAPrivateKey_dup(rsa)) == NULL)
922 {
15d21c2d
RE
923 SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB);
924 return(ret);
e11f0de6 925 }
15d21c2d
RE
926 if (s->cert->rsa_tmp != NULL)
927 RSA_free(s->cert->rsa_tmp);
928 s->cert->rsa_tmp = rsa;
929 ret = 1;
930 }
931 break;
932 case SSL_CTRL_SET_TMP_RSA_CB:
a9188d4e 933 {
d3442bc7
RL
934 SSLerr(SSL_F_SSL3_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
935 return(ret);
a9188d4e 936 }
15d21c2d
RE
937 break;
938#endif
939#ifndef NO_DH
940 case SSL_CTRL_SET_TMP_DH:
941 {
942 DH *dh = (DH *)parg;
e11f0de6
BM
943 if (dh == NULL)
944 {
15d21c2d
RE
945 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
946 return(ret);
e11f0de6
BM
947 }
948 if ((dh = DHparams_dup(dh)) == NULL)
949 {
15d21c2d
RE
950 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
951 return(ret);
e11f0de6
BM
952 }
953 if (!(s->options & SSL_OP_SINGLE_DH_USE))
954 {
955 if (!DH_generate_key(dh))
956 {
957 DH_free(dh);
958 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
959 return(ret);
960 }
961 }
15d21c2d
RE
962 if (s->cert->dh_tmp != NULL)
963 DH_free(s->cert->dh_tmp);
964 s->cert->dh_tmp = dh;
965 ret = 1;
966 }
967 break;
968 case SSL_CTRL_SET_TMP_DH_CB:
a9188d4e 969 {
d3442bc7
RL
970 SSLerr(SSL_F_SSL3_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
971 return(ret);
972 }
973 break;
974#endif
975 default:
976 break;
977 }
978 return(ret);
979 }
a9188d4e 980
d3442bc7
RL
981long ssl3_callback_ctrl(SSL *s, int cmd, void (*fp)())
982 {
983 int ret=0;
984
985#if !defined(NO_DSA) || !defined(NO_RSA)
986 if (
987#ifndef NO_RSA
988 cmd == SSL_CTRL_SET_TMP_RSA_CB ||
989#endif
990#ifndef NO_DSA
991 cmd == SSL_CTRL_SET_TMP_DH_CB ||
992#endif
993 0)
994 {
995 if (!ssl_cert_inst(&s->cert))
e11f0de6 996 {
448e2f9b 997 SSLerr(SSL_F_SSL3_CALLBACK_CTRL, ERR_R_MALLOC_FAILURE);
d3442bc7
RL
998 return(0);
999 }
1000 }
1001#endif
1002
1003 switch (cmd)
1004 {
1005#ifndef NO_RSA
1006 case SSL_CTRL_SET_TMP_RSA_CB:
1007 {
1008 s->cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp;
1009 }
1010 break;
1011#endif
1012#ifndef NO_DH
1013 case SSL_CTRL_SET_TMP_DH_CB:
1014 {
1015 s->cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp;
a9188d4e 1016 }
15d21c2d
RE
1017 break;
1018#endif
58964a49
RE
1019 default:
1020 break;
1021 }
1022 return(ret);
d02b48c6
RE
1023 }
1024
6b691a5c 1025long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, char *parg)
d02b48c6
RE
1026 {
1027 CERT *cert;
1028
ca8e5b9b 1029 cert=ctx->cert;
d02b48c6
RE
1030
1031 switch (cmd)
1032 {
1033#ifndef NO_RSA
1034 case SSL_CTRL_NEED_TMP_RSA:
1035 if ( (cert->rsa_tmp == NULL) &&
1036 ((cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
1037 (EVP_PKEY_size(cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8)))
1038 )
1039 return(1);
1040 else
1041 return(0);
dfeab068 1042 /* break; */
d02b48c6
RE
1043 case SSL_CTRL_SET_TMP_RSA:
1044 {
1045 RSA *rsa;
1046 int i;
1047
1048 rsa=(RSA *)parg;
1049 i=1;
1050 if (rsa == NULL)
1051 i=0;
1052 else
1053 {
1054 if ((rsa=RSAPrivateKey_dup(rsa)) == NULL)
1055 i=0;
1056 }
1057 if (!i)
1058 {
1059 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_RSA_LIB);
1060 return(0);
1061 }
1062 else
1063 {
1064 if (cert->rsa_tmp != NULL)
1065 RSA_free(cert->rsa_tmp);
1066 cert->rsa_tmp=rsa;
1067 return(1);
1068 }
1069 }
dfeab068 1070 /* break; */
d02b48c6 1071 case SSL_CTRL_SET_TMP_RSA_CB:
a9188d4e 1072 {
d3442bc7
RL
1073 SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1074 return(0);
a9188d4e 1075 }
d02b48c6
RE
1076 break;
1077#endif
1078#ifndef NO_DH
1079 case SSL_CTRL_SET_TMP_DH:
1080 {
1081 DH *new=NULL,*dh;
1082
1083 dh=(DH *)parg;
e11f0de6 1084 if ((new=DHparams_dup(dh)) == NULL)
d02b48c6
RE
1085 {
1086 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
e11f0de6 1087 return 0;
d02b48c6 1088 }
e11f0de6 1089 if (!(ctx->options & SSL_OP_SINGLE_DH_USE))
d02b48c6 1090 {
e11f0de6
BM
1091 if (!DH_generate_key(new))
1092 {
1093 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
1094 DH_free(new);
1095 return 0;
1096 }
d02b48c6 1097 }
e11f0de6
BM
1098 if (cert->dh_tmp != NULL)
1099 DH_free(cert->dh_tmp);
1100 cert->dh_tmp=new;
1101 return 1;
d02b48c6 1102 }
dfeab068 1103 /*break; */
d02b48c6 1104 case SSL_CTRL_SET_TMP_DH_CB:
a9188d4e 1105 {
d3442bc7
RL
1106 SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1107 return(0);
a9188d4e 1108 }
d02b48c6
RE
1109 break;
1110#endif
651d0aff 1111 /* A Thawte special :-) */
dfeab068
RE
1112 case SSL_CTRL_EXTRA_CHAIN_CERT:
1113 if (ctx->extra_certs == NULL)
1114 {
f73e07cf 1115 if ((ctx->extra_certs=sk_X509_new_null()) == NULL)
dfeab068
RE
1116 return(0);
1117 }
f73e07cf 1118 sk_X509_push(ctx->extra_certs,(X509 *)parg);
dfeab068
RE
1119 break;
1120
d02b48c6
RE
1121 default:
1122 return(0);
1123 }
1124 return(1);
1125 }
1126
d3442bc7
RL
1127long ssl3_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)())
1128 {
1129 CERT *cert;
1130
1131 cert=ctx->cert;
1132
1133 switch (cmd)
1134 {
1135#ifndef NO_RSA
1136 case SSL_CTRL_SET_TMP_RSA_CB:
1137 {
1138 cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp;
1139 }
1140 break;
1141#endif
1142#ifndef NO_DH
1143 case SSL_CTRL_SET_TMP_DH_CB:
1144 {
1145 cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp;
1146 }
1147 break;
1148#endif
1149 default:
1150 return(0);
1151 }
1152 return(1);
1153 }
1154
d02b48c6
RE
1155/* This function needs to check if the ciphers required are actually
1156 * available */
6b691a5c 1157SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p)
d02b48c6
RE
1158 {
1159 static int init=1;
1160 static SSL_CIPHER *sorted[SSL3_NUM_CIPHERS];
1161 SSL_CIPHER c,*cp= &c,**cpp;
1162 unsigned long id;
1163 int i;
1164
1165 if (init)
1166 {
5cc146f3 1167 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
d02b48c6
RE
1168
1169 for (i=0; i<SSL3_NUM_CIPHERS; i++)
1170 sorted[i]= &(ssl3_ciphers[i]);
1171
1172 qsort( (char *)sorted,
1173 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
1174 FP_ICC ssl_cipher_ptr_id_cmp);
5cc146f3
BM
1175
1176 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
1177
1178 init=0;
d02b48c6
RE
1179 }
1180
1181 id=0x03000000L|((unsigned long)p[0]<<8L)|(unsigned long)p[1];
1182 c.id=id;
1183 cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
1184 (char *)sorted,
1185 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
13083215 1186 FP_ICC ssl_cipher_ptr_id_cmp);
d02b48c6
RE
1187 if ((cpp == NULL) || !(*cpp)->valid)
1188 return(NULL);
1189 else
1190 return(*cpp);
1191 }
1192
6b691a5c 1193int ssl3_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
d02b48c6
RE
1194 {
1195 long l;
1196
1197 if (p != NULL)
1198 {
1199 l=c->id;
1200 if ((l & 0xff000000) != 0x03000000) return(0);
1201 p[0]=((unsigned char)(l>> 8L))&0xFF;
1202 p[1]=((unsigned char)(l ))&0xFF;
1203 }
1204 return(2);
1205 }
1206
6b691a5c
UM
1207SSL_CIPHER *ssl3_choose_cipher(SSL *s, STACK_OF(SSL_CIPHER) *have,
1208 STACK_OF(SSL_CIPHER) *pref)
d02b48c6
RE
1209 {
1210 SSL_CIPHER *c,*ret=NULL;
1211 int i,j,ok;
1212 CERT *cert;
1213 unsigned long alg,mask,emask;
1214
ca8e5b9b
BM
1215 /* Let's see which ciphers we can support */
1216 cert=s->cert;
d02b48c6 1217
f73e07cf 1218 sk_SSL_CIPHER_set_cmp_func(pref,ssl_cipher_ptr_id_cmp);
d02b48c6 1219
f415fa32 1220#ifdef CIPHER_DEBUG
f9b3bff6
RL
1221 printf("Have %d from %p:\n", sk_SSL_CIPHER_num(pref), pref);
1222 for(i=0 ; i < sk_SSL_CIPHER_num(pref) ; ++i)
f415fa32 1223 {
f9b3bff6 1224 c=sk_SSL_CIPHER_value(pref,i);
f415fa32
BL
1225 printf("%p:%s\n",c,c->name);
1226 }
1227#endif
1228
f73e07cf 1229 for (i=0; i<sk_SSL_CIPHER_num(have); i++)
d02b48c6 1230 {
f73e07cf 1231 c=sk_SSL_CIPHER_value(have,i);
60e31c3a 1232
ca8e5b9b 1233 ssl_set_cert_masks(cert,c);
60e31c3a
BL
1234 mask=cert->mask;
1235 emask=cert->export_mask;
1236
f9b3bff6
RL
1237#ifdef KSSL_DEBUG
1238 printf("ssl3_choose_cipher %d alg= %lx\n", i,c->algorithms);
1239#endif /* KSSL_DEBUG */
1240
d02b48c6 1241 alg=c->algorithms&(SSL_MKEY_MASK|SSL_AUTH_MASK);
018e57c7 1242 if (SSL_C_IS_EXPORT(c))
d02b48c6
RE
1243 {
1244 ok=((alg & emask) == alg)?1:0;
1245#ifdef CIPHER_DEBUG
f415fa32
BL
1246 printf("%d:[%08lX:%08lX]%p:%s (export)\n",ok,alg,emask,
1247 c,c->name);
d02b48c6
RE
1248#endif
1249 }
1250 else
1251 {
1252 ok=((alg & mask) == alg)?1:0;
1253#ifdef CIPHER_DEBUG
f415fa32
BL
1254 printf("%d:[%08lX:%08lX]%p:%s\n",ok,alg,mask,c,
1255 c->name);
d02b48c6
RE
1256#endif
1257 }
1258
1259 if (!ok) continue;
1260
f73e07cf 1261 j=sk_SSL_CIPHER_find(pref,c);
d02b48c6
RE
1262 if (j >= 0)
1263 {
f73e07cf 1264 ret=sk_SSL_CIPHER_value(pref,j);
d02b48c6
RE
1265 break;
1266 }
1267 }
1268 return(ret);
1269 }
1270
6b691a5c 1271int ssl3_get_req_cert_type(SSL *s, unsigned char *p)
d02b48c6
RE
1272 {
1273 int ret=0;
1274 unsigned long alg;
1275
1276 alg=s->s3->tmp.new_cipher->algorithms;
1277
1278#ifndef NO_DH
1279 if (alg & (SSL_kDHr|SSL_kEDH))
1280 {
dfeab068 1281# ifndef NO_RSA
d02b48c6 1282 p[ret++]=SSL3_CT_RSA_FIXED_DH;
dfeab068
RE
1283# endif
1284# ifndef NO_DSA
d02b48c6 1285 p[ret++]=SSL3_CT_DSS_FIXED_DH;
dfeab068 1286# endif
d02b48c6 1287 }
58964a49
RE
1288 if ((s->version == SSL3_VERSION) &&
1289 (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr)))
d02b48c6 1290 {
dfeab068 1291# ifndef NO_RSA
d02b48c6 1292 p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;
dfeab068
RE
1293# endif
1294# ifndef NO_DSA
d02b48c6 1295 p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;
dfeab068 1296# endif
d02b48c6
RE
1297 }
1298#endif /* !NO_DH */
1299#ifndef NO_RSA
1300 p[ret++]=SSL3_CT_RSA_SIGN;
1301#endif
dfeab068 1302#ifndef NO_DSA
58964a49 1303 p[ret++]=SSL3_CT_DSS_SIGN;
dfeab068 1304#endif
d02b48c6
RE
1305 return(ret);
1306 }
1307
6b691a5c 1308int ssl3_shutdown(SSL *s)
d02b48c6
RE
1309 {
1310
1311 /* Don't do anything much if we have not done the handshake or
1312 * we don't want to send messages :-) */
1313 if ((s->quiet_shutdown) || (s->state == SSL_ST_BEFORE))
1314 {
1315 s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
1316 return(1);
1317 }
1318
1319 if (!(s->shutdown & SSL_SENT_SHUTDOWN))
1320 {
1321 s->shutdown|=SSL_SENT_SHUTDOWN;
1322#if 1
58964a49 1323 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_CLOSE_NOTIFY);
d02b48c6
RE
1324#endif
1325 /* our shutdown alert has been sent now, and if it still needs
1326 * to be written, s->s3->alert_dispatch will be true */
1327 }
1328 else if (s->s3->alert_dispatch)
1329 {
1330 /* resend it if not sent */
1331#if 1
1332 ssl3_dispatch_alert(s);
1333#endif
1334 }
1335 else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN))
1336 {
1337 /* If we are waiting for a close from our peer, we are closed */
5a4fbc69 1338 ssl3_read_bytes(s,0,NULL,0,0);
d02b48c6
RE
1339 }
1340
1341 if ((s->shutdown == (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN)) &&
1342 !s->s3->alert_dispatch)
1343 return(1);
1344 else
1345 return(0);
1346 }
1347
61f5b6f3 1348int ssl3_write(SSL *s, const void *buf, int len)
d02b48c6
RE
1349 {
1350 int ret,n;
d02b48c6
RE
1351
1352#if 0
1353 if (s->shutdown & SSL_SEND_SHUTDOWN)
1354 {
1355 s->rwstate=SSL_NOTHING;
1356 return(0);
1357 }
1358#endif
58964a49
RE
1359 clear_sys_error();
1360 if (s->s3->renegotiate) ssl3_renegotiate_check(s);
d02b48c6
RE
1361
1362 /* This is an experimental flag that sends the
1363 * last handshake message in the same packet as the first
1364 * use data - used to see if it helps the TCP protocol during
1365 * session-id reuse */
1366 /* The second test is because the buffer may have been removed */
1367 if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio))
1368 {
1369 /* First time through, we write into the buffer */
1370 if (s->s3->delay_buf_pop_ret == 0)
1371 {
1372 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
e778802f 1373 buf,len);
d02b48c6
RE
1374 if (ret <= 0) return(ret);
1375
1376 s->s3->delay_buf_pop_ret=ret;
1377 }
1378
1379 s->rwstate=SSL_WRITING;
1380 n=BIO_flush(s->wbio);
1381 if (n <= 0) return(n);
1382 s->rwstate=SSL_NOTHING;
1383
413c4f45
MC
1384 /* We have flushed the buffer, so remove it */
1385 ssl_free_wbio_buffer(s);
1386 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
1387
d02b48c6
RE
1388 ret=s->s3->delay_buf_pop_ret;
1389 s->s3->delay_buf_pop_ret=0;
d02b48c6
RE
1390 }
1391 else
1392 {
1393 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
e778802f 1394 buf,len);
d02b48c6
RE
1395 if (ret <= 0) return(ret);
1396 }
58964a49 1397
d02b48c6
RE
1398 return(ret);
1399 }
1400
5a4fbc69 1401static int ssl3_read_internal(SSL *s, void *buf, int len, int peek)
d02b48c6 1402 {
58964a49
RE
1403 int ret;
1404
1405 clear_sys_error();
1406 if (s->s3->renegotiate) ssl3_renegotiate_check(s);
1407 s->s3->in_read_app_data=1;
5a4fbc69 1408 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek);
58964a49
RE
1409 if ((ret == -1) && (s->s3->in_read_app_data == 0))
1410 {
b35e9050
BM
1411 /* ssl3_read_bytes decided to call s->handshake_func, which
1412 * called ssl3_read_bytes to read handshake data.
1413 * However, ssl3_read_bytes actually found application data
1414 * and thinks that application data makes sense here (signalled
1415 * by resetting 'in_read_app_data', strangely); so disable
1416 * handshake processing and try to read application data again. */
58964a49 1417 s->in_handshake++;
5a4fbc69 1418 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek);
58964a49
RE
1419 s->in_handshake--;
1420 }
1421 else
1422 s->s3->in_read_app_data=0;
1423
1424 return(ret);
d02b48c6
RE
1425 }
1426
5a4fbc69 1427int ssl3_read(SSL *s, void *buf, int len)
d02b48c6 1428 {
5a4fbc69
BM
1429 return ssl3_read_internal(s, buf, len, 0);
1430 }
d02b48c6 1431
5a4fbc69
BM
1432int ssl3_peek(SSL *s, char *buf, int len)
1433 {
1434 return ssl3_read_internal(s, buf, len, 0);
d02b48c6
RE
1435 }
1436
6b691a5c 1437int ssl3_renegotiate(SSL *s)
d02b48c6
RE
1438 {
1439 if (s->handshake_func == NULL)
1440 return(1);
1441
1442 if (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
1443 return(0);
1444
58964a49 1445 s->s3->renegotiate=1;
d02b48c6
RE
1446 return(1);
1447 }
1448
6b691a5c 1449int ssl3_renegotiate_check(SSL *s)
58964a49
RE
1450 {
1451 int ret=0;
1452
1453 if (s->s3->renegotiate)
1454 {
1455 if ( (s->s3->rbuf.left == 0) &&
1456 (s->s3->wbuf.left == 0) &&
1457 !SSL_in_init(s))
1458 {
1459/*
1460if we are the server, and we have sent a 'RENEGOTIATE' message, we
de808df4 1461need to go to SSL_ST_ACCEPT.
58964a49
RE
1462*/
1463 /* SSL_ST_ACCEPT */
1464 s->state=SSL_ST_RENEGOTIATE;
1465 s->s3->renegotiate=0;
1466 s->s3->num_renegotiations++;
1467 s->s3->total_renegotiations++;
1468 ret=1;
1469 }
1470 }
1471 return(ret);
1472 }
1473