1 /* ssl/s2_lib.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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  */
58 
59 #include "ssl_locl.h"
60 #ifndef OPENSSL_NO_SSL2
61 # include <stdio.h>
62 # include <openssl/objects.h>
63 # include <openssl/evp.h>
64 # include <openssl/md5.h>
65 
66 const char ssl2_version_str[] = "SSLv2" OPENSSL_VERSION_PTEXT;
67 
68 # define SSL2_NUM_CIPHERS (sizeof(ssl2_ciphers)/sizeof(SSL_CIPHER))
69 
70 /* list of available SSLv2 ciphers (sorted by id) */
71 OPENSSL_GLOBAL SSL_CIPHER ssl2_ciphers[] = {
72 /* NULL_WITH_MD5 v3 */
73 # if 0
74     {
75      1,
76      SSL2_TXT_NULL_WITH_MD5,
77      SSL2_CK_NULL_WITH_MD5,
78      SSL_kRSA | SSL_aRSA | SSL_eNULL | SSL_MD5 | SSL_SSLV2,
79      SSL_EXPORT | SSL_EXP40 | SSL_STRONG_NONE,
80      0,
81      0,
82      0,
83      SSL_ALL_CIPHERS,
84      SSL_ALL_STRENGTHS,
85      },
86 # endif
87 /* RC4_128_WITH_MD5 */
88     {
89      1,
90      SSL2_TXT_RC4_128_WITH_MD5,
91      SSL2_CK_RC4_128_WITH_MD5,
92      SSL_kRSA | SSL_aRSA | SSL_RC4 | SSL_MD5 | SSL_SSLV2,
93      SSL_NOT_EXP | SSL_MEDIUM,
94      0,
95      128,
96      128,
97      SSL_ALL_CIPHERS,
98      SSL_ALL_STRENGTHS,
99      },
100 
101 # if 0
102 /* RC4_128_EXPORT40_WITH_MD5 */
103     {
104      1,
105      SSL2_TXT_RC4_128_EXPORT40_WITH_MD5,
106      SSL2_CK_RC4_128_EXPORT40_WITH_MD5,
107      SSL_kRSA | SSL_aRSA | SSL_RC4 | SSL_MD5 | SSL_SSLV2,
108      SSL_EXPORT | SSL_EXP40,
109      SSL2_CF_5_BYTE_ENC,
110      40,
111      128,
112      SSL_ALL_CIPHERS,
113      SSL_ALL_STRENGTHS,
114      },
115 # endif
116 
117 /* RC2_128_CBC_WITH_MD5 */
118     {
119      1,
120      SSL2_TXT_RC2_128_CBC_WITH_MD5,
121      SSL2_CK_RC2_128_CBC_WITH_MD5,
122      SSL_kRSA | SSL_aRSA | SSL_RC2 | SSL_MD5 | SSL_SSLV2,
123      SSL_NOT_EXP | SSL_MEDIUM,
124      0,
125      128,
126      128,
127      SSL_ALL_CIPHERS,
128      SSL_ALL_STRENGTHS,
129      },
130 
131 # if 0
132 /* RC2_128_CBC_EXPORT40_WITH_MD5 */
133     {
134      1,
135      SSL2_TXT_RC2_128_CBC_EXPORT40_WITH_MD5,
136      SSL2_CK_RC2_128_CBC_EXPORT40_WITH_MD5,
137      SSL_kRSA | SSL_aRSA | SSL_RC2 | SSL_MD5 | SSL_SSLV2,
138      SSL_EXPORT | SSL_EXP40,
139      SSL2_CF_5_BYTE_ENC,
140      40,
141      128,
142      SSL_ALL_CIPHERS,
143      SSL_ALL_STRENGTHS,
144      },
145 # endif
146 
147 /* IDEA_128_CBC_WITH_MD5 */
148 # ifndef OPENSSL_NO_IDEA
149     {
150      1,
151      SSL2_TXT_IDEA_128_CBC_WITH_MD5,
152      SSL2_CK_IDEA_128_CBC_WITH_MD5,
153      SSL_kRSA | SSL_aRSA | SSL_IDEA | SSL_MD5 | SSL_SSLV2,
154      SSL_NOT_EXP | SSL_MEDIUM,
155      0,
156      128,
157      128,
158      SSL_ALL_CIPHERS,
159      SSL_ALL_STRENGTHS,
160      },
161 # endif
162 
163 # if 0
164 /* DES_64_CBC_WITH_MD5 */
165     {
166      1,
167      SSL2_TXT_DES_64_CBC_WITH_MD5,
168      SSL2_CK_DES_64_CBC_WITH_MD5,
169      SSL_kRSA | SSL_aRSA | SSL_DES | SSL_MD5 | SSL_SSLV2,
170      SSL_NOT_EXP | SSL_LOW,
171      0,
172      56,
173      56,
174      SSL_ALL_CIPHERS,
175      SSL_ALL_STRENGTHS,
176      },
177 # endif
178 
179 /* DES_192_EDE3_CBC_WITH_MD5 */
180     {
181      1,
182      SSL2_TXT_DES_192_EDE3_CBC_WITH_MD5,
183      SSL2_CK_DES_192_EDE3_CBC_WITH_MD5,
184      SSL_kRSA | SSL_aRSA | SSL_3DES | SSL_MD5 | SSL_SSLV2,
185      SSL_NOT_EXP | SSL_HIGH,
186      0,
187      168,
188      168,
189      SSL_ALL_CIPHERS,
190      SSL_ALL_STRENGTHS,
191      },
192 /* RC4_64_WITH_MD5 */
193 # if 0
194     {
195      1,
196      SSL2_TXT_RC4_64_WITH_MD5,
197      SSL2_CK_RC4_64_WITH_MD5,
198      SSL_kRSA | SSL_aRSA | SSL_RC4 | SSL_MD5 | SSL_SSLV2,
199      SSL_NOT_EXP | SSL_LOW,
200      SSL2_CF_8_BYTE_ENC,
201      64,
202      64,
203      SSL_ALL_CIPHERS,
204      SSL_ALL_STRENGTHS,
205      },
206 # endif
207 /* NULL SSLeay (testing) */
208 # if 0
209     {
210      0,
211      SSL2_TXT_NULL,
212      SSL2_CK_NULL,
213      0,
214      SSL_STRONG_NONE,
215      0,
216      0,
217      0,
218      SSL_ALL_CIPHERS,
219      SSL_ALL_STRENGTHS,
220      },
221 # endif
222 
223 /* end of list :-) */
224 };
225 
ssl2_default_timeout(void)226 long ssl2_default_timeout(void)
227 {
228     return (300);
229 }
230 
IMPLEMENT_ssl2_meth_func(sslv2_base_method,ssl_undefined_function,ssl_undefined_function,ssl_bad_method)231 IMPLEMENT_ssl2_meth_func(sslv2_base_method,
232                          ssl_undefined_function,
233                          ssl_undefined_function, ssl_bad_method)
234 
235 int ssl2_num_ciphers(void)
236 {
237     return (SSL2_NUM_CIPHERS);
238 }
239 
ssl2_get_cipher(unsigned int u)240 SSL_CIPHER *ssl2_get_cipher(unsigned int u)
241 {
242     if (u < SSL2_NUM_CIPHERS)
243         return (&(ssl2_ciphers[SSL2_NUM_CIPHERS - 1 - u]));
244     else
245         return (NULL);
246 }
247 
ssl2_pending(const SSL * s)248 int ssl2_pending(const SSL *s)
249 {
250     return SSL_in_init(s) ? 0 : s->s2->ract_data_length;
251 }
252 
ssl2_new(SSL * s)253 int ssl2_new(SSL *s)
254 {
255     SSL2_STATE *s2;
256 
257     if ((s2 = OPENSSL_malloc(sizeof *s2)) == NULL)
258         goto err;
259     memset(s2, 0, sizeof *s2);
260 
261 # if SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER + 3 > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 2
262 #  error "assertion failed"
263 # endif
264 
265     if ((s2->rbuf =
266          OPENSSL_malloc(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 2)) == NULL)
267         goto err;
268     /*
269      * wbuf needs one byte more because when using two-byte headers, we leave
270      * the first byte unused in do_ssl_write (s2_pkt.c)
271      */
272     if ((s2->wbuf =
273          OPENSSL_malloc(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 3)) == NULL)
274         goto err;
275     s->s2 = s2;
276 
277     ssl2_clear(s);
278     return (1);
279  err:
280     if (s2 != NULL) {
281         if (s2->wbuf != NULL)
282             OPENSSL_free(s2->wbuf);
283         if (s2->rbuf != NULL)
284             OPENSSL_free(s2->rbuf);
285         OPENSSL_free(s2);
286     }
287     return (0);
288 }
289 
ssl2_free(SSL * s)290 void ssl2_free(SSL *s)
291 {
292     SSL2_STATE *s2;
293 
294     if (s == NULL)
295         return;
296 
297     s2 = s->s2;
298     if (s2->rbuf != NULL)
299         OPENSSL_free(s2->rbuf);
300     if (s2->wbuf != NULL)
301         OPENSSL_free(s2->wbuf);
302     OPENSSL_cleanse(s2, sizeof *s2);
303     OPENSSL_free(s2);
304     s->s2 = NULL;
305 }
306 
ssl2_clear(SSL * s)307 void ssl2_clear(SSL *s)
308 {
309     SSL2_STATE *s2;
310     unsigned char *rbuf, *wbuf;
311 
312     s2 = s->s2;
313 
314     rbuf = s2->rbuf;
315     wbuf = s2->wbuf;
316 
317     memset(s2, 0, sizeof *s2);
318 
319     s2->rbuf = rbuf;
320     s2->wbuf = wbuf;
321     s2->clear_text = 1;
322     s->packet = s2->rbuf;
323     s->version = SSL2_VERSION;
324     s->packet_length = 0;
325 }
326 
ssl2_ctrl(SSL * s,int cmd,long larg,void * parg)327 long ssl2_ctrl(SSL *s, int cmd, long larg, void *parg)
328 {
329     int ret = 0;
330 
331     switch (cmd) {
332     case SSL_CTRL_GET_SESSION_REUSED:
333         ret = s->hit;
334         break;
335     case SSL_CTRL_CHECK_PROTO_VERSION:
336         return ssl3_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, larg, parg);
337     default:
338         break;
339     }
340     return (ret);
341 }
342 
ssl2_callback_ctrl(SSL * s,int cmd,void (* fp)(void))343 long ssl2_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
344 {
345     return (0);
346 }
347 
ssl2_ctx_ctrl(SSL_CTX * ctx,int cmd,long larg,void * parg)348 long ssl2_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
349 {
350     return (0);
351 }
352 
ssl2_ctx_callback_ctrl(SSL_CTX * ctx,int cmd,void (* fp)(void))353 long ssl2_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
354 {
355     return (0);
356 }
357 
358 /*
359  * This function needs to check if the ciphers required are actually
360  * available
361  */
ssl2_get_cipher_by_char(const unsigned char * p)362 SSL_CIPHER *ssl2_get_cipher_by_char(const unsigned char *p)
363 {
364     SSL_CIPHER c, *cp;
365     unsigned long id;
366 
367     id = 0x02000000L | ((unsigned long)p[0] << 16L) |
368         ((unsigned long)p[1] << 8L) | (unsigned long)p[2];
369     c.id = id;
370     cp = (SSL_CIPHER *)OBJ_bsearch((char *)&c,
371                                    (char *)ssl2_ciphers,
372                                    SSL2_NUM_CIPHERS, sizeof(SSL_CIPHER),
373                                    FP_ICC ssl_cipher_id_cmp);
374     if ((cp == NULL) || (cp->valid == 0))
375         return NULL;
376     else
377         return cp;
378 }
379 
ssl2_put_cipher_by_char(const SSL_CIPHER * c,unsigned char * p)380 int ssl2_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
381 {
382     long l;
383 
384     if (p != NULL) {
385         l = c->id;
386         if ((l & 0xff000000) != 0x02000000 && l != SSL3_CK_FALLBACK_SCSV)
387             return (0);
388         p[0] = ((unsigned char)(l >> 16L)) & 0xFF;
389         p[1] = ((unsigned char)(l >> 8L)) & 0xFF;
390         p[2] = ((unsigned char)(l)) & 0xFF;
391     }
392     return (3);
393 }
394 
ssl2_generate_key_material(SSL * s)395 int ssl2_generate_key_material(SSL *s)
396 {
397     unsigned int i;
398     EVP_MD_CTX ctx;
399     unsigned char *km;
400     unsigned char c = '0';
401     const EVP_MD *md5;
402 
403     md5 = EVP_md5();
404 
405 # ifdef CHARSET_EBCDIC
406     c = os_toascii['0'];        /* Must be an ASCII '0', not EBCDIC '0', see
407                                  * SSLv2 docu */
408 # endif
409     EVP_MD_CTX_init(&ctx);
410     km = s->s2->key_material;
411 
412     if (s->session->master_key_length < 0 ||
413         s->session->master_key_length > (int)sizeof(s->session->master_key)) {
414         SSLerr(SSL_F_SSL2_GENERATE_KEY_MATERIAL, ERR_R_INTERNAL_ERROR);
415         return 0;
416     }
417 
418     for (i = 0; i < s->s2->key_material_length; i += EVP_MD_size(md5)) {
419         if (((km - s->s2->key_material) + EVP_MD_size(md5)) >
420             (int)sizeof(s->s2->key_material)) {
421             /*
422              * EVP_DigestFinal_ex() below would write beyond buffer
423              */
424             SSLerr(SSL_F_SSL2_GENERATE_KEY_MATERIAL, ERR_R_INTERNAL_ERROR);
425             return 0;
426         }
427 
428         EVP_DigestInit_ex(&ctx, md5, NULL);
429 
430         OPENSSL_assert(s->session->master_key_length >= 0
431                        && s->session->master_key_length
432                        <= (int)sizeof(s->session->master_key));
433         EVP_DigestUpdate(&ctx, s->session->master_key,
434                          s->session->master_key_length);
435         EVP_DigestUpdate(&ctx, &c, 1);
436         c++;
437         EVP_DigestUpdate(&ctx, s->s2->challenge, s->s2->challenge_length);
438         EVP_DigestUpdate(&ctx, s->s2->conn_id, s->s2->conn_id_length);
439         EVP_DigestFinal_ex(&ctx, km, NULL);
440         km += EVP_MD_size(md5);
441     }
442 
443     EVP_MD_CTX_cleanup(&ctx);
444     return 1;
445 }
446 
ssl2_return_error(SSL * s,int err)447 void ssl2_return_error(SSL *s, int err)
448 {
449     if (!s->error) {
450         s->error = 3;
451         s->error_code = err;
452 
453         ssl2_write_error(s);
454     }
455 }
456 
ssl2_write_error(SSL * s)457 void ssl2_write_error(SSL *s)
458 {
459     unsigned char buf[3];
460     int i, error;
461 
462     buf[0] = SSL2_MT_ERROR;
463     buf[1] = (s->error_code >> 8) & 0xff;
464     buf[2] = (s->error_code) & 0xff;
465 
466 /*      state=s->rwstate;*/
467 
468     error = s->error;           /* number of bytes left to write */
469     s->error = 0;
470     OPENSSL_assert(error >= 0 && error <= (int)sizeof(buf));
471     i = ssl2_write(s, &(buf[3 - error]), error);
472 
473 /*      if (i == error) s->rwstate=state; */
474 
475     if (i < 0)
476         s->error = error;
477     else {
478         s->error = error - i;
479 
480         if (s->error == 0)
481             if (s->msg_callback) {
482                 /* ERROR */
483                 s->msg_callback(1, s->version, 0, buf, 3, s,
484                                 s->msg_callback_arg);
485             }
486     }
487 }
488 
ssl2_shutdown(SSL * s)489 int ssl2_shutdown(SSL *s)
490 {
491     s->shutdown = (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
492     return (1);
493 }
494 #else                           /* !OPENSSL_NO_SSL2 */
495 
496 # if PEDANTIC
497 static void *dummy = &dummy;
498 # endif
499 
500 #endif
501