xref: /minix3/crypto/external/bsd/openssl/dist/ssl/s2_lib.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* ssl/s2_lib.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60ebfedea0SLionel Sambuc  *
61ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
62ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
63ebfedea0SLionel Sambuc  * are met:
64ebfedea0SLionel Sambuc  *
65ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
66ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
67ebfedea0SLionel Sambuc  *
68ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
69ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
70ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
71ebfedea0SLionel Sambuc  *    distribution.
72ebfedea0SLionel Sambuc  *
73ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
74ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
75ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
76ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ebfedea0SLionel Sambuc  *
78ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
80ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
81ebfedea0SLionel Sambuc  *    openssl-core@openssl.org.
82ebfedea0SLionel Sambuc  *
83ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
84ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
85ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
86ebfedea0SLionel Sambuc  *
87ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
88ebfedea0SLionel Sambuc  *    acknowledgment:
89ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
90ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ebfedea0SLionel Sambuc  *
92ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
104ebfedea0SLionel Sambuc  * ====================================================================
105ebfedea0SLionel Sambuc  *
106ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
107ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
108ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
109ebfedea0SLionel Sambuc  *
110ebfedea0SLionel Sambuc  */
111ebfedea0SLionel Sambuc 
112ebfedea0SLionel Sambuc #include "ssl_locl.h"
113ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SSL2
114ebfedea0SLionel Sambuc # include <stdio.h>
115ebfedea0SLionel Sambuc # include <openssl/objects.h>
116ebfedea0SLionel Sambuc # include <openssl/evp.h>
117ebfedea0SLionel Sambuc # include <openssl/md5.h>
118ebfedea0SLionel Sambuc 
119ebfedea0SLionel Sambuc const char ssl2_version_str[] = "SSLv2" OPENSSL_VERSION_PTEXT;
120ebfedea0SLionel Sambuc 
121ebfedea0SLionel Sambuc # define SSL2_NUM_CIPHERS (sizeof(ssl2_ciphers)/sizeof(SSL_CIPHER))
122ebfedea0SLionel Sambuc 
123ebfedea0SLionel Sambuc /* list of available SSLv2 ciphers (sorted by id) */
124ebfedea0SLionel Sambuc OPENSSL_GLOBAL const SSL_CIPHER ssl2_ciphers[] = {
125ebfedea0SLionel Sambuc # if 0
126ebfedea0SLionel Sambuc /* NULL_WITH_MD5 v3 */
127ebfedea0SLionel Sambuc     {
128ebfedea0SLionel Sambuc      1,
129ebfedea0SLionel Sambuc      SSL2_TXT_NULL_WITH_MD5,
130ebfedea0SLionel Sambuc      SSL2_CK_NULL_WITH_MD5,
131ebfedea0SLionel Sambuc      SSL_kRSA,
132ebfedea0SLionel Sambuc      SSL_aRSA,
133ebfedea0SLionel Sambuc      SSL_eNULL,
134ebfedea0SLionel Sambuc      SSL_MD5,
135ebfedea0SLionel Sambuc      SSL_SSLV2,
136ebfedea0SLionel Sambuc      SSL_EXPORT | SSL_EXP40 | SSL_STRONG_NONE,
137ebfedea0SLionel Sambuc      0,
138ebfedea0SLionel Sambuc      0,
139ebfedea0SLionel Sambuc      0,
140ebfedea0SLionel Sambuc      },
141ebfedea0SLionel Sambuc # endif
142ebfedea0SLionel Sambuc 
143ebfedea0SLionel Sambuc /* RC4_128_WITH_MD5 */
144ebfedea0SLionel Sambuc     {
145ebfedea0SLionel Sambuc      1,
146ebfedea0SLionel Sambuc      SSL2_TXT_RC4_128_WITH_MD5,
147ebfedea0SLionel Sambuc      SSL2_CK_RC4_128_WITH_MD5,
148ebfedea0SLionel Sambuc      SSL_kRSA,
149ebfedea0SLionel Sambuc      SSL_aRSA,
150ebfedea0SLionel Sambuc      SSL_RC4,
151ebfedea0SLionel Sambuc      SSL_MD5,
152ebfedea0SLionel Sambuc      SSL_SSLV2,
153ebfedea0SLionel Sambuc      SSL_NOT_EXP | SSL_MEDIUM,
154ebfedea0SLionel Sambuc      0,
155ebfedea0SLionel Sambuc      128,
156ebfedea0SLionel Sambuc      128,
157ebfedea0SLionel Sambuc      },
158ebfedea0SLionel Sambuc 
159ebfedea0SLionel Sambuc /* RC4_128_EXPORT40_WITH_MD5 */
160ebfedea0SLionel Sambuc     {
161ebfedea0SLionel Sambuc      1,
162ebfedea0SLionel Sambuc      SSL2_TXT_RC4_128_EXPORT40_WITH_MD5,
163ebfedea0SLionel Sambuc      SSL2_CK_RC4_128_EXPORT40_WITH_MD5,
164ebfedea0SLionel Sambuc      SSL_kRSA,
165ebfedea0SLionel Sambuc      SSL_aRSA,
166ebfedea0SLionel Sambuc      SSL_RC4,
167ebfedea0SLionel Sambuc      SSL_MD5,
168ebfedea0SLionel Sambuc      SSL_SSLV2,
169ebfedea0SLionel Sambuc      SSL_EXPORT | SSL_EXP40,
170ebfedea0SLionel Sambuc      SSL2_CF_5_BYTE_ENC,
171ebfedea0SLionel Sambuc      40,
172ebfedea0SLionel Sambuc      128,
173ebfedea0SLionel Sambuc      },
174ebfedea0SLionel Sambuc 
175ebfedea0SLionel Sambuc /* RC2_128_CBC_WITH_MD5 */
176ebfedea0SLionel Sambuc     {
177ebfedea0SLionel Sambuc      1,
178ebfedea0SLionel Sambuc      SSL2_TXT_RC2_128_CBC_WITH_MD5,
179ebfedea0SLionel Sambuc      SSL2_CK_RC2_128_CBC_WITH_MD5,
180ebfedea0SLionel Sambuc      SSL_kRSA,
181ebfedea0SLionel Sambuc      SSL_aRSA,
182ebfedea0SLionel Sambuc      SSL_RC2,
183ebfedea0SLionel Sambuc      SSL_MD5,
184ebfedea0SLionel Sambuc      SSL_SSLV2,
185ebfedea0SLionel Sambuc      SSL_NOT_EXP | SSL_MEDIUM,
186ebfedea0SLionel Sambuc      0,
187ebfedea0SLionel Sambuc      128,
188ebfedea0SLionel Sambuc      128,
189ebfedea0SLionel Sambuc      },
190ebfedea0SLionel Sambuc 
191ebfedea0SLionel Sambuc /* RC2_128_CBC_EXPORT40_WITH_MD5 */
192ebfedea0SLionel Sambuc     {
193ebfedea0SLionel Sambuc      1,
194ebfedea0SLionel Sambuc      SSL2_TXT_RC2_128_CBC_EXPORT40_WITH_MD5,
195ebfedea0SLionel Sambuc      SSL2_CK_RC2_128_CBC_EXPORT40_WITH_MD5,
196ebfedea0SLionel Sambuc      SSL_kRSA,
197ebfedea0SLionel Sambuc      SSL_aRSA,
198ebfedea0SLionel Sambuc      SSL_RC2,
199ebfedea0SLionel Sambuc      SSL_MD5,
200ebfedea0SLionel Sambuc      SSL_SSLV2,
201ebfedea0SLionel Sambuc      SSL_EXPORT | SSL_EXP40,
202ebfedea0SLionel Sambuc      SSL2_CF_5_BYTE_ENC,
203ebfedea0SLionel Sambuc      40,
204ebfedea0SLionel Sambuc      128,
205ebfedea0SLionel Sambuc      },
206ebfedea0SLionel Sambuc 
207ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_IDEA
208ebfedea0SLionel Sambuc /* IDEA_128_CBC_WITH_MD5 */
209ebfedea0SLionel Sambuc     {
210ebfedea0SLionel Sambuc      1,
211ebfedea0SLionel Sambuc      SSL2_TXT_IDEA_128_CBC_WITH_MD5,
212ebfedea0SLionel Sambuc      SSL2_CK_IDEA_128_CBC_WITH_MD5,
213ebfedea0SLionel Sambuc      SSL_kRSA,
214ebfedea0SLionel Sambuc      SSL_aRSA,
215ebfedea0SLionel Sambuc      SSL_IDEA,
216ebfedea0SLionel Sambuc      SSL_MD5,
217ebfedea0SLionel Sambuc      SSL_SSLV2,
218ebfedea0SLionel Sambuc      SSL_NOT_EXP | SSL_MEDIUM,
219ebfedea0SLionel Sambuc      0,
220ebfedea0SLionel Sambuc      128,
221ebfedea0SLionel Sambuc      128,
222ebfedea0SLionel Sambuc      },
223ebfedea0SLionel Sambuc # endif
224ebfedea0SLionel Sambuc 
225ebfedea0SLionel Sambuc /* DES_64_CBC_WITH_MD5 */
226ebfedea0SLionel Sambuc     {
227ebfedea0SLionel Sambuc      1,
228ebfedea0SLionel Sambuc      SSL2_TXT_DES_64_CBC_WITH_MD5,
229ebfedea0SLionel Sambuc      SSL2_CK_DES_64_CBC_WITH_MD5,
230ebfedea0SLionel Sambuc      SSL_kRSA,
231ebfedea0SLionel Sambuc      SSL_aRSA,
232ebfedea0SLionel Sambuc      SSL_DES,
233ebfedea0SLionel Sambuc      SSL_MD5,
234ebfedea0SLionel Sambuc      SSL_SSLV2,
235ebfedea0SLionel Sambuc      SSL_NOT_EXP | SSL_LOW,
236ebfedea0SLionel Sambuc      0,
237ebfedea0SLionel Sambuc      56,
238ebfedea0SLionel Sambuc      56,
239ebfedea0SLionel Sambuc      },
240ebfedea0SLionel Sambuc 
241ebfedea0SLionel Sambuc /* DES_192_EDE3_CBC_WITH_MD5 */
242ebfedea0SLionel Sambuc     {
243ebfedea0SLionel Sambuc      1,
244ebfedea0SLionel Sambuc      SSL2_TXT_DES_192_EDE3_CBC_WITH_MD5,
245ebfedea0SLionel Sambuc      SSL2_CK_DES_192_EDE3_CBC_WITH_MD5,
246ebfedea0SLionel Sambuc      SSL_kRSA,
247ebfedea0SLionel Sambuc      SSL_aRSA,
248ebfedea0SLionel Sambuc      SSL_3DES,
249ebfedea0SLionel Sambuc      SSL_MD5,
250ebfedea0SLionel Sambuc      SSL_SSLV2,
251ebfedea0SLionel Sambuc      SSL_NOT_EXP | SSL_HIGH,
252ebfedea0SLionel Sambuc      0,
253*0a6a1f1dSLionel Sambuc      112,
254ebfedea0SLionel Sambuc      168,
255ebfedea0SLionel Sambuc      },
256ebfedea0SLionel Sambuc 
257ebfedea0SLionel Sambuc # if 0
258ebfedea0SLionel Sambuc /* RC4_64_WITH_MD5 */
259ebfedea0SLionel Sambuc     {
260ebfedea0SLionel Sambuc      1,
261ebfedea0SLionel Sambuc      SSL2_TXT_RC4_64_WITH_MD5,
262ebfedea0SLionel Sambuc      SSL2_CK_RC4_64_WITH_MD5,
263ebfedea0SLionel Sambuc      SSL_kRSA,
264ebfedea0SLionel Sambuc      SSL_aRSA,
265ebfedea0SLionel Sambuc      SSL_RC4,
266ebfedea0SLionel Sambuc      SSL_MD5,
267ebfedea0SLionel Sambuc      SSL_SSLV2,
268ebfedea0SLionel Sambuc      SSL_NOT_EXP | SSL_LOW,
269ebfedea0SLionel Sambuc      SSL2_CF_8_BYTE_ENC,
270ebfedea0SLionel Sambuc      64,
271ebfedea0SLionel Sambuc      64,
272ebfedea0SLionel Sambuc      },
273ebfedea0SLionel Sambuc # endif
274ebfedea0SLionel Sambuc 
275ebfedea0SLionel Sambuc # if 0
276ebfedea0SLionel Sambuc /* NULL SSLeay (testing) */
277ebfedea0SLionel Sambuc     {
278ebfedea0SLionel Sambuc      0,
279ebfedea0SLionel Sambuc      SSL2_TXT_NULL,
280ebfedea0SLionel Sambuc      SSL2_CK_NULL,
281ebfedea0SLionel Sambuc      0,
282ebfedea0SLionel Sambuc      0,
283ebfedea0SLionel Sambuc      0,
284ebfedea0SLionel Sambuc      0,
285ebfedea0SLionel Sambuc      SSL_SSLV2,
286ebfedea0SLionel Sambuc      SSL_STRONG_NONE,
287ebfedea0SLionel Sambuc      0,
288ebfedea0SLionel Sambuc      0,
289ebfedea0SLionel Sambuc      0,
290ebfedea0SLionel Sambuc      },
291ebfedea0SLionel Sambuc # endif
292ebfedea0SLionel Sambuc 
293ebfedea0SLionel Sambuc /* end of list :-) */
294ebfedea0SLionel Sambuc };
295ebfedea0SLionel Sambuc 
ssl2_default_timeout(void)296ebfedea0SLionel Sambuc long ssl2_default_timeout(void)
297ebfedea0SLionel Sambuc {
298ebfedea0SLionel Sambuc     return (300);
299ebfedea0SLionel Sambuc }
300ebfedea0SLionel Sambuc 
ssl2_num_ciphers(void)301ebfedea0SLionel Sambuc int ssl2_num_ciphers(void)
302ebfedea0SLionel Sambuc {
303ebfedea0SLionel Sambuc     return (SSL2_NUM_CIPHERS);
304ebfedea0SLionel Sambuc }
305ebfedea0SLionel Sambuc 
ssl2_get_cipher(unsigned int u)306ebfedea0SLionel Sambuc const SSL_CIPHER *ssl2_get_cipher(unsigned int u)
307ebfedea0SLionel Sambuc {
308ebfedea0SLionel Sambuc     if (u < SSL2_NUM_CIPHERS)
309ebfedea0SLionel Sambuc         return (&(ssl2_ciphers[SSL2_NUM_CIPHERS - 1 - u]));
310ebfedea0SLionel Sambuc     else
311ebfedea0SLionel Sambuc         return (NULL);
312ebfedea0SLionel Sambuc }
313ebfedea0SLionel Sambuc 
ssl2_pending(const SSL * s)314ebfedea0SLionel Sambuc int ssl2_pending(const SSL *s)
315ebfedea0SLionel Sambuc {
316ebfedea0SLionel Sambuc     return SSL_in_init(s) ? 0 : s->s2->ract_data_length;
317ebfedea0SLionel Sambuc }
318ebfedea0SLionel Sambuc 
ssl2_new(SSL * s)319ebfedea0SLionel Sambuc int ssl2_new(SSL *s)
320ebfedea0SLionel Sambuc {
321ebfedea0SLionel Sambuc     SSL2_STATE *s2;
322ebfedea0SLionel Sambuc 
323*0a6a1f1dSLionel Sambuc     if ((s2 = OPENSSL_malloc(sizeof *s2)) == NULL)
324*0a6a1f1dSLionel Sambuc         goto err;
325ebfedea0SLionel Sambuc     memset(s2, 0, sizeof *s2);
326ebfedea0SLionel Sambuc 
327ebfedea0SLionel Sambuc # if SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER + 3 > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 2
328ebfedea0SLionel Sambuc #  error "assertion failed"
329ebfedea0SLionel Sambuc # endif
330ebfedea0SLionel Sambuc 
331*0a6a1f1dSLionel Sambuc     if ((s2->rbuf =
332*0a6a1f1dSLionel Sambuc          OPENSSL_malloc(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 2)) == NULL)
333*0a6a1f1dSLionel Sambuc         goto err;
334*0a6a1f1dSLionel Sambuc     /*
335*0a6a1f1dSLionel Sambuc      * wbuf needs one byte more because when using two-byte headers, we leave
336*0a6a1f1dSLionel Sambuc      * the first byte unused in do_ssl_write (s2_pkt.c)
337*0a6a1f1dSLionel Sambuc      */
338*0a6a1f1dSLionel Sambuc     if ((s2->wbuf =
339*0a6a1f1dSLionel Sambuc          OPENSSL_malloc(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 3)) == NULL)
340*0a6a1f1dSLionel Sambuc         goto err;
341ebfedea0SLionel Sambuc     s->s2 = s2;
342ebfedea0SLionel Sambuc 
343ebfedea0SLionel Sambuc     ssl2_clear(s);
344ebfedea0SLionel Sambuc     return (1);
345ebfedea0SLionel Sambuc  err:
346*0a6a1f1dSLionel Sambuc     if (s2 != NULL) {
347*0a6a1f1dSLionel Sambuc         if (s2->wbuf != NULL)
348*0a6a1f1dSLionel Sambuc             OPENSSL_free(s2->wbuf);
349*0a6a1f1dSLionel Sambuc         if (s2->rbuf != NULL)
350*0a6a1f1dSLionel Sambuc             OPENSSL_free(s2->rbuf);
351ebfedea0SLionel Sambuc         OPENSSL_free(s2);
352ebfedea0SLionel Sambuc     }
353ebfedea0SLionel Sambuc     return (0);
354ebfedea0SLionel Sambuc }
355ebfedea0SLionel Sambuc 
ssl2_free(SSL * s)356ebfedea0SLionel Sambuc void ssl2_free(SSL *s)
357ebfedea0SLionel Sambuc {
358ebfedea0SLionel Sambuc     SSL2_STATE *s2;
359ebfedea0SLionel Sambuc 
360ebfedea0SLionel Sambuc     if (s == NULL)
361ebfedea0SLionel Sambuc         return;
362ebfedea0SLionel Sambuc 
363ebfedea0SLionel Sambuc     s2 = s->s2;
364*0a6a1f1dSLionel Sambuc     if (s2->rbuf != NULL)
365*0a6a1f1dSLionel Sambuc         OPENSSL_free(s2->rbuf);
366*0a6a1f1dSLionel Sambuc     if (s2->wbuf != NULL)
367*0a6a1f1dSLionel Sambuc         OPENSSL_free(s2->wbuf);
368ebfedea0SLionel Sambuc     OPENSSL_cleanse(s2, sizeof *s2);
369ebfedea0SLionel Sambuc     OPENSSL_free(s2);
370ebfedea0SLionel Sambuc     s->s2 = NULL;
371ebfedea0SLionel Sambuc }
372ebfedea0SLionel Sambuc 
ssl2_clear(SSL * s)373ebfedea0SLionel Sambuc void ssl2_clear(SSL *s)
374ebfedea0SLionel Sambuc {
375ebfedea0SLionel Sambuc     SSL2_STATE *s2;
376ebfedea0SLionel Sambuc     unsigned char *rbuf, *wbuf;
377ebfedea0SLionel Sambuc 
378ebfedea0SLionel Sambuc     s2 = s->s2;
379ebfedea0SLionel Sambuc 
380ebfedea0SLionel Sambuc     rbuf = s2->rbuf;
381ebfedea0SLionel Sambuc     wbuf = s2->wbuf;
382ebfedea0SLionel Sambuc 
383ebfedea0SLionel Sambuc     memset(s2, 0, sizeof *s2);
384ebfedea0SLionel Sambuc 
385ebfedea0SLionel Sambuc     s2->rbuf = rbuf;
386ebfedea0SLionel Sambuc     s2->wbuf = wbuf;
387ebfedea0SLionel Sambuc     s2->clear_text = 1;
388ebfedea0SLionel Sambuc     s->packet = s2->rbuf;
389ebfedea0SLionel Sambuc     s->version = SSL2_VERSION;
390ebfedea0SLionel Sambuc     s->packet_length = 0;
391ebfedea0SLionel Sambuc }
392ebfedea0SLionel Sambuc 
ssl2_ctrl(SSL * s,int cmd,long larg,void * parg)393ebfedea0SLionel Sambuc long ssl2_ctrl(SSL *s, int cmd, long larg, void *parg)
394ebfedea0SLionel Sambuc {
395ebfedea0SLionel Sambuc     int ret = 0;
396ebfedea0SLionel Sambuc 
397*0a6a1f1dSLionel Sambuc     switch (cmd) {
398ebfedea0SLionel Sambuc     case SSL_CTRL_GET_SESSION_REUSED:
399ebfedea0SLionel Sambuc         ret = s->hit;
400ebfedea0SLionel Sambuc         break;
401*0a6a1f1dSLionel Sambuc     case SSL_CTRL_CHECK_PROTO_VERSION:
402*0a6a1f1dSLionel Sambuc         return ssl3_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, larg, parg);
403ebfedea0SLionel Sambuc     default:
404ebfedea0SLionel Sambuc         break;
405ebfedea0SLionel Sambuc     }
406ebfedea0SLionel Sambuc     return (ret);
407ebfedea0SLionel Sambuc }
408ebfedea0SLionel Sambuc 
ssl2_callback_ctrl(SSL * s,int cmd,void (* fp)(void))409ebfedea0SLionel Sambuc long ssl2_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
410ebfedea0SLionel Sambuc {
411ebfedea0SLionel Sambuc     return (0);
412ebfedea0SLionel Sambuc }
413ebfedea0SLionel Sambuc 
ssl2_ctx_ctrl(SSL_CTX * ctx,int cmd,long larg,void * parg)414ebfedea0SLionel Sambuc long ssl2_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
415ebfedea0SLionel Sambuc {
416ebfedea0SLionel Sambuc     return (0);
417ebfedea0SLionel Sambuc }
418ebfedea0SLionel Sambuc 
ssl2_ctx_callback_ctrl(SSL_CTX * ctx,int cmd,void (* fp)(void))419ebfedea0SLionel Sambuc long ssl2_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
420ebfedea0SLionel Sambuc {
421ebfedea0SLionel Sambuc     return (0);
422ebfedea0SLionel Sambuc }
423ebfedea0SLionel Sambuc 
424*0a6a1f1dSLionel Sambuc /*
425*0a6a1f1dSLionel Sambuc  * This function needs to check if the ciphers required are actually
426*0a6a1f1dSLionel Sambuc  * available
427*0a6a1f1dSLionel Sambuc  */
ssl2_get_cipher_by_char(const unsigned char * p)428ebfedea0SLionel Sambuc const SSL_CIPHER *ssl2_get_cipher_by_char(const unsigned char *p)
429ebfedea0SLionel Sambuc {
430ebfedea0SLionel Sambuc     SSL_CIPHER c;
431ebfedea0SLionel Sambuc     const SSL_CIPHER *cp;
432ebfedea0SLionel Sambuc     unsigned long id;
433ebfedea0SLionel Sambuc 
434ebfedea0SLionel Sambuc     id = 0x02000000L | ((unsigned long)p[0] << 16L) |
435ebfedea0SLionel Sambuc         ((unsigned long)p[1] << 8L) | (unsigned long)p[2];
436ebfedea0SLionel Sambuc     c.id = id;
437ebfedea0SLionel Sambuc     cp = OBJ_bsearch_ssl_cipher_id(&c, ssl2_ciphers, SSL2_NUM_CIPHERS);
438ebfedea0SLionel Sambuc     if ((cp == NULL) || (cp->valid == 0))
439ebfedea0SLionel Sambuc         return NULL;
440ebfedea0SLionel Sambuc     else
441ebfedea0SLionel Sambuc         return cp;
442ebfedea0SLionel Sambuc }
443ebfedea0SLionel Sambuc 
ssl2_put_cipher_by_char(const SSL_CIPHER * c,unsigned char * p)444ebfedea0SLionel Sambuc int ssl2_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
445ebfedea0SLionel Sambuc {
446ebfedea0SLionel Sambuc     long l;
447ebfedea0SLionel Sambuc 
448*0a6a1f1dSLionel Sambuc     if (p != NULL) {
449ebfedea0SLionel Sambuc         l = c->id;
450*0a6a1f1dSLionel Sambuc         if ((l & 0xff000000) != 0x02000000 && l != SSL3_CK_FALLBACK_SCSV)
451*0a6a1f1dSLionel Sambuc             return (0);
452ebfedea0SLionel Sambuc         p[0] = ((unsigned char)(l >> 16L)) & 0xFF;
453ebfedea0SLionel Sambuc         p[1] = ((unsigned char)(l >> 8L)) & 0xFF;
454ebfedea0SLionel Sambuc         p[2] = ((unsigned char)(l)) & 0xFF;
455ebfedea0SLionel Sambuc     }
456ebfedea0SLionel Sambuc     return (3);
457ebfedea0SLionel Sambuc }
458ebfedea0SLionel Sambuc 
ssl2_generate_key_material(SSL * s)459ebfedea0SLionel Sambuc int ssl2_generate_key_material(SSL *s)
460ebfedea0SLionel Sambuc {
461ebfedea0SLionel Sambuc     unsigned int i;
462ebfedea0SLionel Sambuc     EVP_MD_CTX ctx;
463ebfedea0SLionel Sambuc     unsigned char *km;
464ebfedea0SLionel Sambuc     unsigned char c = '0';
465ebfedea0SLionel Sambuc     const EVP_MD *md5;
466ebfedea0SLionel Sambuc     int md_size;
467ebfedea0SLionel Sambuc 
468ebfedea0SLionel Sambuc     md5 = EVP_md5();
469ebfedea0SLionel Sambuc 
470ebfedea0SLionel Sambuc # ifdef CHARSET_EBCDIC
471*0a6a1f1dSLionel Sambuc     c = os_toascii['0'];        /* Must be an ASCII '0', not EBCDIC '0', see
472*0a6a1f1dSLionel Sambuc                                  * SSLv2 docu */
473ebfedea0SLionel Sambuc # endif
474ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&ctx);
475ebfedea0SLionel Sambuc     km = s->s2->key_material;
476ebfedea0SLionel Sambuc 
477ebfedea0SLionel Sambuc     if (s->session->master_key_length < 0 ||
478*0a6a1f1dSLionel Sambuc         s->session->master_key_length > (int)sizeof(s->session->master_key)) {
479ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL2_GENERATE_KEY_MATERIAL, ERR_R_INTERNAL_ERROR);
480ebfedea0SLionel Sambuc         return 0;
481ebfedea0SLionel Sambuc     }
482ebfedea0SLionel Sambuc     md_size = EVP_MD_size(md5);
483ebfedea0SLionel Sambuc     if (md_size < 0)
484ebfedea0SLionel Sambuc         return 0;
485*0a6a1f1dSLionel Sambuc     for (i = 0; i < s->s2->key_material_length; i += md_size) {
486ebfedea0SLionel Sambuc         if (((km - s->s2->key_material) + md_size) >
487*0a6a1f1dSLionel Sambuc             (int)sizeof(s->s2->key_material)) {
488*0a6a1f1dSLionel Sambuc             /*
489*0a6a1f1dSLionel Sambuc              * EVP_DigestFinal_ex() below would write beyond buffer
490*0a6a1f1dSLionel Sambuc              */
491ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL2_GENERATE_KEY_MATERIAL, ERR_R_INTERNAL_ERROR);
492ebfedea0SLionel Sambuc             return 0;
493ebfedea0SLionel Sambuc         }
494ebfedea0SLionel Sambuc 
495ebfedea0SLionel Sambuc         EVP_DigestInit_ex(&ctx, md5, NULL);
496ebfedea0SLionel Sambuc 
497ebfedea0SLionel Sambuc         OPENSSL_assert(s->session->master_key_length >= 0
498ebfedea0SLionel Sambuc                        && s->session->master_key_length
499*0a6a1f1dSLionel Sambuc                        <= (int)sizeof(s->session->master_key));
500*0a6a1f1dSLionel Sambuc         EVP_DigestUpdate(&ctx, s->session->master_key,
501*0a6a1f1dSLionel Sambuc                          s->session->master_key_length);
502ebfedea0SLionel Sambuc         EVP_DigestUpdate(&ctx, &c, 1);
503ebfedea0SLionel Sambuc         c++;
504ebfedea0SLionel Sambuc         EVP_DigestUpdate(&ctx, s->s2->challenge, s->s2->challenge_length);
505ebfedea0SLionel Sambuc         EVP_DigestUpdate(&ctx, s->s2->conn_id, s->s2->conn_id_length);
506ebfedea0SLionel Sambuc         EVP_DigestFinal_ex(&ctx, km, NULL);
507ebfedea0SLionel Sambuc         km += md_size;
508ebfedea0SLionel Sambuc     }
509ebfedea0SLionel Sambuc 
510ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&ctx);
511ebfedea0SLionel Sambuc     return 1;
512ebfedea0SLionel Sambuc }
513ebfedea0SLionel Sambuc 
ssl2_return_error(SSL * s,int err)514ebfedea0SLionel Sambuc void ssl2_return_error(SSL *s, int err)
515ebfedea0SLionel Sambuc {
516*0a6a1f1dSLionel Sambuc     if (!s->error) {
517ebfedea0SLionel Sambuc         s->error = 3;
518ebfedea0SLionel Sambuc         s->error_code = err;
519ebfedea0SLionel Sambuc 
520ebfedea0SLionel Sambuc         ssl2_write_error(s);
521ebfedea0SLionel Sambuc     }
522ebfedea0SLionel Sambuc }
523ebfedea0SLionel Sambuc 
ssl2_write_error(SSL * s)524ebfedea0SLionel Sambuc void ssl2_write_error(SSL *s)
525ebfedea0SLionel Sambuc {
526ebfedea0SLionel Sambuc     unsigned char buf[3];
527ebfedea0SLionel Sambuc     int i, error;
528ebfedea0SLionel Sambuc 
529ebfedea0SLionel Sambuc     buf[0] = SSL2_MT_ERROR;
530ebfedea0SLionel Sambuc     buf[1] = (s->error_code >> 8) & 0xff;
531ebfedea0SLionel Sambuc     buf[2] = (s->error_code) & 0xff;
532ebfedea0SLionel Sambuc 
533ebfedea0SLionel Sambuc /*      state=s->rwstate;*/
534ebfedea0SLionel Sambuc 
535ebfedea0SLionel Sambuc     error = s->error;           /* number of bytes left to write */
536ebfedea0SLionel Sambuc     s->error = 0;
537ebfedea0SLionel Sambuc     OPENSSL_assert(error >= 0 && error <= (int)sizeof(buf));
538ebfedea0SLionel Sambuc     i = ssl2_write(s, &(buf[3 - error]), error);
539ebfedea0SLionel Sambuc 
540ebfedea0SLionel Sambuc /*      if (i == error) s->rwstate=state; */
541ebfedea0SLionel Sambuc 
542ebfedea0SLionel Sambuc     if (i < 0)
543ebfedea0SLionel Sambuc         s->error = error;
544*0a6a1f1dSLionel Sambuc     else {
545ebfedea0SLionel Sambuc         s->error = error - i;
546ebfedea0SLionel Sambuc 
547ebfedea0SLionel Sambuc         if (s->error == 0)
548*0a6a1f1dSLionel Sambuc             if (s->msg_callback) {
549*0a6a1f1dSLionel Sambuc                 /* ERROR */
550*0a6a1f1dSLionel Sambuc                 s->msg_callback(1, s->version, 0, buf, 3, s,
551*0a6a1f1dSLionel Sambuc                                 s->msg_callback_arg);
552*0a6a1f1dSLionel Sambuc             }
553ebfedea0SLionel Sambuc     }
554ebfedea0SLionel Sambuc }
555ebfedea0SLionel Sambuc 
ssl2_shutdown(SSL * s)556ebfedea0SLionel Sambuc int ssl2_shutdown(SSL *s)
557ebfedea0SLionel Sambuc {
558ebfedea0SLionel Sambuc     s->shutdown = (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
559ebfedea0SLionel Sambuc     return (1);
560ebfedea0SLionel Sambuc }
561ebfedea0SLionel Sambuc #else                           /* !OPENSSL_NO_SSL2 */
562ebfedea0SLionel Sambuc 
563ebfedea0SLionel Sambuc # if PEDANTIC
564ebfedea0SLionel Sambuc static void *dummy = &dummy;
565ebfedea0SLionel Sambuc # endif
566ebfedea0SLionel Sambuc 
567ebfedea0SLionel Sambuc #endif
568