hehe %-)
[syren.git] / src / xyssl / library / ssl_tls.c
blob71e496ad9960c07029f7c8f13829c32047c85191
1 /*
2 * SSLv3/TLSv1 shared functions
4 * Copyright (C) 2006-2007 Christophe Devine
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License, version 2.1 as published by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301 USA
21 * The SSL 3.0 specification was drafted by Netscape in 1996,
22 * and became an IETF standard in 1999.
24 * http://wp.netscape.com/eng/ssl3/
25 * http://www.ietf.org/rfc/rfc2246.txt
26 * http://www.ietf.org/rfc/rfc4346.txt
29 #include "xyssl/config.h"
31 #if defined(XYSSL_SSL_TLS_C)
33 #include "xyssl/aes.h"
34 #include "xyssl/arc4.h"
35 #include "xyssl/des.h"
36 #include "xyssl/ssl.h"
38 #include <string.h>
39 #include <stdlib.h>
40 #include <time.h>
43 * Key material generation
45 static int tls1_prf( unsigned char *secret, int slen, char *label,
46 unsigned char *random, int rlen,
47 unsigned char *dstbuf, int dlen )
49 int nb, hs;
50 int i, j, k;
51 unsigned char *S1, *S2;
52 unsigned char tmp[128];
53 unsigned char h_i[20];
55 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
56 return( XYSSL_ERR_SSL_BAD_INPUT_DATA );
58 hs = ( slen + 1 ) / 2;
59 S1 = secret;
60 S2 = secret + slen - hs;
62 nb = strlen( label );
63 memcpy( tmp + 20, label, nb );
64 memcpy( tmp + 20 + nb, random, rlen );
65 nb += rlen;
68 * First compute P_md5(secret,label+random)[0..dlen]
70 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
72 for( i = 0; i < dlen; i += 16 )
74 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
75 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
77 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
79 for( j = 0; j < k; j++ )
80 dstbuf[i + j] = h_i[j];
84 * XOR out with P_sha1(secret,label+random)[0..dlen]
86 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
88 for( i = 0; i < dlen; i += 20 )
90 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
91 sha1_hmac( S2, hs, tmp, 20, tmp );
93 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
95 for( j = 0; j < k; j++ )
96 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
99 memset( tmp, 0, sizeof( tmp ) );
100 memset( h_i, 0, sizeof( h_i ) );
102 return( 0 );
105 int ssl_derive_keys( ssl_context *ssl )
107 int i;
108 md5_context md5;
109 sha1_context sha1;
110 unsigned char tmp[64];
111 unsigned char padding[16];
112 unsigned char sha1sum[20];
113 unsigned char keyblk[256];
114 unsigned char *key1;
115 unsigned char *key2;
117 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
120 * SSLv3:
121 * master =
122 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
123 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
124 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
126 * TLSv1:
127 * master = PRF( premaster, "master secret", randbytes )[0..47]
129 if( ssl->resume == 0 )
131 int len = ssl->pmslen;
133 SSL_DEBUG_BUF( 3, "premaster secret", ssl->premaster, len );
135 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
137 for( i = 0; i < 3; i++ )
139 memset( padding, 'A' + i, 1 + i );
141 sha1_starts( &sha1 );
142 sha1_update( &sha1, padding, 1 + i );
143 sha1_update( &sha1, ssl->premaster, len );
144 sha1_update( &sha1, ssl->randbytes, 64 );
145 sha1_finish( &sha1, sha1sum );
147 md5_starts( &md5 );
148 md5_update( &md5, ssl->premaster, len );
149 md5_update( &md5, sha1sum, 20 );
150 md5_finish( &md5, ssl->session->master + i * 16 );
153 else
154 tls1_prf( ssl->premaster, len, "master secret",
155 ssl->randbytes, 64, ssl->session->master, 48 );
157 memset( ssl->premaster, 0, sizeof( ssl->premaster ) );
159 else
160 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
163 * Swap the client and server random values.
165 memcpy( tmp, ssl->randbytes, 64 );
166 memcpy( ssl->randbytes, tmp + 32, 32 );
167 memcpy( ssl->randbytes + 32, tmp, 32 );
168 memset( tmp, 0, sizeof( tmp ) );
171 * SSLv3:
172 * key block =
173 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
174 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
175 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
176 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
177 * ...
179 * TLSv1:
180 * key block = PRF( master, "key expansion", randbytes )
182 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
184 for( i = 0; i < 16; i++ )
186 memset( padding, 'A' + i, 1 + i );
188 sha1_starts( &sha1 );
189 sha1_update( &sha1, padding, 1 + i );
190 sha1_update( &sha1, ssl->session->master, 48 );
191 sha1_update( &sha1, ssl->randbytes, 64 );
192 sha1_finish( &sha1, sha1sum );
194 md5_starts( &md5 );
195 md5_update( &md5, ssl->session->master, 48 );
196 md5_update( &md5, sha1sum, 20 );
197 md5_finish( &md5, keyblk + i * 16 );
200 memset( &md5, 0, sizeof( md5 ) );
201 memset( &sha1, 0, sizeof( sha1 ) );
203 memset( padding, 0, sizeof( padding ) );
204 memset( sha1sum, 0, sizeof( sha1sum ) );
206 else
207 tls1_prf( ssl->session->master, 48, "key expansion",
208 ssl->randbytes, 64, keyblk, 256 );
210 SSL_DEBUG_MSG( 3, ( "cipher = %s", ssl_get_cipher( ssl ) ) );
211 SSL_DEBUG_BUF( 3, "master secret", ssl->session->master, 48 );
212 SSL_DEBUG_BUF( 4, "random bytes", ssl->randbytes, 64 );
213 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
215 memset( ssl->randbytes, 0, sizeof( ssl->randbytes ) );
218 * Determine the appropriate key, IV and MAC length.
220 switch( ssl->session->cipher )
222 #if defined(XYSSL_ARC4_C)
223 case SSL_RSA_RC4_128_MD5:
224 ssl->keylen = 16; ssl->minlen = 16;
225 ssl->ivlen = 0; ssl->maclen = 16;
226 break;
228 case SSL_RSA_RC4_128_SHA:
229 ssl->keylen = 16; ssl->minlen = 20;
230 ssl->ivlen = 0; ssl->maclen = 20;
231 break;
232 #endif
234 #if defined(XYSSL_DES_C)
235 case SSL_RSA_DES_168_SHA:
236 case SSL_EDH_RSA_DES_168_SHA:
237 ssl->keylen = 24; ssl->minlen = 24;
238 ssl->ivlen = 8; ssl->maclen = 20;
239 break;
240 #endif
242 #if defined(XYSSL_AES_C)
243 case SSL_RSA_AES_256_SHA:
244 case SSL_EDH_RSA_AES_256_SHA:
245 ssl->keylen = 32; ssl->minlen = 32;
246 ssl->ivlen = 16; ssl->maclen = 20;
247 break;
248 #endif
250 default:
251 SSL_DEBUG_MSG( 1, ( "cipher %s is not available",
252 ssl_get_cipher( ssl ) ) );
253 return( XYSSL_ERR_SSL_FEATURE_UNAVAILABLE );
256 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
257 ssl->keylen, ssl->minlen, ssl->ivlen, ssl->maclen ) );
260 * Finally setup the cipher contexts, IVs and MAC secrets.
262 if( ssl->endpoint == SSL_IS_CLIENT )
264 key1 = keyblk + ssl->maclen * 2;
265 key2 = keyblk + ssl->maclen * 2 + ssl->keylen;
267 memcpy( ssl->mac_enc, keyblk, ssl->maclen );
268 memcpy( ssl->mac_dec, keyblk + ssl->maclen, ssl->maclen );
270 memcpy( ssl->iv_enc, key2 + ssl->keylen, ssl->ivlen );
271 memcpy( ssl->iv_dec, key2 + ssl->keylen + ssl->ivlen,
272 ssl->ivlen );
274 else
276 key1 = keyblk + ssl->maclen * 2 + ssl->keylen;
277 key2 = keyblk + ssl->maclen * 2;
279 memcpy( ssl->mac_dec, keyblk, ssl->maclen );
280 memcpy( ssl->mac_enc, keyblk + ssl->maclen, ssl->maclen );
282 memcpy( ssl->iv_dec, key1 + ssl->keylen, ssl->ivlen );
283 memcpy( ssl->iv_enc, key1 + ssl->keylen + ssl->ivlen,
284 ssl->ivlen );
287 switch( ssl->session->cipher )
289 #if defined(XYSSL_ARC4_C)
290 case SSL_RSA_RC4_128_MD5:
291 case SSL_RSA_RC4_128_SHA:
292 arc4_setup( (arc4_context *) ssl->ctx_enc, key1, ssl->keylen );
293 arc4_setup( (arc4_context *) ssl->ctx_dec, key2, ssl->keylen );
294 break;
295 #endif
297 #if defined(XYSSL_DES_C)
298 case SSL_RSA_DES_168_SHA:
299 case SSL_EDH_RSA_DES_168_SHA:
300 des3_set3key_enc( (des3_context *) ssl->ctx_enc, key1 );
301 des3_set3key_dec( (des3_context *) ssl->ctx_dec, key2 );
302 break;
303 #endif
305 #if defined(XYSSL_AES_C)
306 case SSL_RSA_AES_256_SHA:
307 case SSL_EDH_RSA_AES_256_SHA:
308 aes_setkey_enc( (aes_context *) ssl->ctx_enc, key1, 256 );
309 aes_setkey_dec( (aes_context *) ssl->ctx_dec, key2, 256 );
310 break;
311 #endif
313 default:
314 return( XYSSL_ERR_SSL_FEATURE_UNAVAILABLE );
317 memset( keyblk, 0, sizeof( keyblk ) );
319 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
321 return( 0 );
324 void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] )
326 md5_context md5;
327 sha1_context sha1;
328 unsigned char pad_1[48];
329 unsigned char pad_2[48];
331 SSL_DEBUG_MSG( 2, ( "=> calc verify" ) );
333 memcpy( &md5 , &ssl->fin_md5 , sizeof( md5_context ) );
334 memcpy( &sha1, &ssl->fin_sha1, sizeof( sha1_context ) );
336 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
338 memset( pad_1, 0x36, 48 );
339 memset( pad_2, 0x5C, 48 );
341 md5_update( &md5, ssl->session->master, 48 );
342 md5_update( &md5, pad_1, 48 );
343 md5_finish( &md5, hash );
345 md5_starts( &md5 );
346 md5_update( &md5, ssl->session->master, 48 );
347 md5_update( &md5, pad_2, 48 );
348 md5_update( &md5, hash, 16 );
349 md5_finish( &md5, hash );
351 sha1_update( &sha1, ssl->session->master, 48 );
352 sha1_update( &sha1, pad_1, 40 );
353 sha1_finish( &sha1, hash + 16 );
355 sha1_starts( &sha1 );
356 sha1_update( &sha1, ssl->session->master, 48 );
357 sha1_update( &sha1, pad_2, 40 );
358 sha1_update( &sha1, hash + 16, 20 );
359 sha1_finish( &sha1, hash + 16 );
361 else /* TLSv1 */
363 md5_finish( &md5, hash );
364 sha1_finish( &sha1, hash + 16 );
367 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
368 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
370 return;
374 * SSLv3.0 MAC functions
376 static void ssl_mac_md5( unsigned char *secret,
377 unsigned char *buf, int len,
378 unsigned char *ctr, int type )
380 unsigned char header[11];
381 unsigned char padding[48];
382 md5_context md5;
384 memcpy( header, ctr, 8 );
385 header[ 8] = (unsigned char) type;
386 header[ 9] = (unsigned char)( len >> 8 );
387 header[10] = (unsigned char)( len );
389 memset( padding, 0x36, 48 );
390 md5_starts( &md5 );
391 md5_update( &md5, secret, 16 );
392 md5_update( &md5, padding, 48 );
393 md5_update( &md5, header, 11 );
394 md5_update( &md5, buf, len );
395 md5_finish( &md5, buf + len );
397 memset( padding, 0x5C, 48 );
398 md5_starts( &md5 );
399 md5_update( &md5, secret, 16 );
400 md5_update( &md5, padding, 48 );
401 md5_update( &md5, buf + len, 16 );
402 md5_finish( &md5, buf + len );
405 static void ssl_mac_sha1( unsigned char *secret,
406 unsigned char *buf, int len,
407 unsigned char *ctr, int type )
409 unsigned char header[11];
410 unsigned char padding[40];
411 sha1_context sha1;
413 memcpy( header, ctr, 8 );
414 header[ 8] = (unsigned char) type;
415 header[ 9] = (unsigned char)( len >> 8 );
416 header[10] = (unsigned char)( len );
418 memset( padding, 0x36, 40 );
419 sha1_starts( &sha1 );
420 sha1_update( &sha1, secret, 20 );
421 sha1_update( &sha1, padding, 40 );
422 sha1_update( &sha1, header, 11 );
423 sha1_update( &sha1, buf, len );
424 sha1_finish( &sha1, buf + len );
426 memset( padding, 0x5C, 40 );
427 sha1_starts( &sha1 );
428 sha1_update( &sha1, secret, 20 );
429 sha1_update( &sha1, padding, 40 );
430 sha1_update( &sha1, buf + len, 20 );
431 sha1_finish( &sha1, buf + len );
435 * Encryption/decryption functions
437 static int ssl_encrypt_buf( ssl_context *ssl )
439 int i, padlen;
441 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
444 * Add MAC then encrypt
446 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
448 if( ssl->maclen == 16 )
449 ssl_mac_md5( ssl->mac_enc,
450 ssl->out_msg, ssl->out_msglen,
451 ssl->out_ctr, ssl->out_msgtype );
453 if( ssl->maclen == 20 )
454 ssl_mac_sha1( ssl->mac_enc,
455 ssl->out_msg, ssl->out_msglen,
456 ssl->out_ctr, ssl->out_msgtype );
458 else
460 if( ssl->maclen == 16 )
461 md5_hmac( ssl->mac_enc, 16,
462 ssl->out_ctr, ssl->out_msglen + 13,
463 ssl->out_msg + ssl->out_msglen );
465 if( ssl->maclen == 20 )
466 sha1_hmac( ssl->mac_enc, 20,
467 ssl->out_ctr, ssl->out_msglen + 13,
468 ssl->out_msg + ssl->out_msglen );
471 SSL_DEBUG_BUF( 4, "computed mac",
472 ssl->out_msg + ssl->out_msglen, ssl->maclen );
474 ssl->out_msglen += ssl->maclen;
476 for( i = 7; i >= 0; i-- )
477 if( ++ssl->out_ctr[i] != 0 )
478 break;
480 if( ssl->ivlen == 0 )
482 #if defined(XYSSL_ARC4_C)
483 padlen = 0;
485 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
486 "including %d bytes of padding",
487 ssl->out_msglen, 0 ) );
489 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
490 ssl->out_msg, ssl->out_msglen );
492 arc4_crypt( (arc4_context *) ssl->ctx_enc,
493 ssl->out_msg, ssl->out_msglen );
494 #else
495 return( XYSSL_ERR_SSL_FEATURE_UNAVAILABLE );
496 #endif
498 else
500 padlen = ssl->ivlen - ( ssl->out_msglen + 1 ) % ssl->ivlen;
501 if( padlen == ssl->ivlen )
502 padlen = 0;
504 for( i = 0; i <= padlen; i++ )
505 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
507 ssl->out_msglen += padlen + 1;
509 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
510 "including %d bytes of padding",
511 ssl->out_msglen, padlen + 1 ) );
513 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
514 ssl->out_msg, ssl->out_msglen );
516 switch( ssl->ivlen )
518 case 8:
519 #if defined(XYSSL_DES_C)
520 des3_crypt_cbc( (des3_context *) ssl->ctx_enc,
521 DES_ENCRYPT, ssl->out_msglen,
522 ssl->iv_enc, ssl->out_msg, ssl->out_msg );
523 break;
524 #endif
526 case 16:
527 #if defined(XYSSL_AES_C)
528 aes_crypt_cbc( (aes_context *) ssl->ctx_enc,
529 AES_ENCRYPT, ssl->out_msglen,
530 ssl->iv_enc, ssl->out_msg, ssl->out_msg );
531 break;
532 #endif
534 default:
535 return( XYSSL_ERR_SSL_FEATURE_UNAVAILABLE );
539 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
541 return( 0 );
544 static int ssl_decrypt_buf( ssl_context *ssl )
546 int i, padlen;
547 unsigned char tmp[20];
549 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
551 if( ssl->in_msglen < ssl->minlen )
553 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
554 ssl->in_msglen, ssl->minlen ) );
555 return( XYSSL_ERR_SSL_INVALID_MAC );
558 if( ssl->ivlen == 0 )
560 #if defined(XYSSL_ARC4_C)
561 padlen = 0;
562 arc4_crypt( (arc4_context *) ssl->ctx_dec,
563 ssl->in_msg, ssl->in_msglen );
564 #else
565 return( XYSSL_ERR_SSL_FEATURE_UNAVAILABLE );
566 #endif
568 else
571 * Decrypt and check the padding
573 if( ssl->in_msglen % ssl->ivlen != 0 )
575 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
576 ssl->in_msglen, ssl->ivlen ) );
577 return( XYSSL_ERR_SSL_INVALID_MAC );
580 switch( ssl->ivlen )
582 #if defined(XYSSL_DES_C)
583 case 8:
584 des3_crypt_cbc( (des3_context *) ssl->ctx_dec,
585 DES_DECRYPT, ssl->in_msglen,
586 ssl->iv_dec, ssl->in_msg, ssl->in_msg );
587 break;
588 #endif
590 #if defined(XYSSL_AES_C)
591 case 16:
592 aes_crypt_cbc( (aes_context *) ssl->ctx_dec,
593 AES_DECRYPT, ssl->in_msglen,
594 ssl->iv_dec, ssl->in_msg, ssl->in_msg );
595 break;
596 #endif
598 default:
599 return( XYSSL_ERR_SSL_FEATURE_UNAVAILABLE );
602 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
604 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
606 if( padlen > ssl->ivlen )
608 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
609 "should be no more than %d",
610 padlen, ssl->ivlen ) );
611 padlen = 0;
614 else
617 * TLSv1: always check the padding
619 for( i = 1; i <= padlen; i++ )
621 if( ssl->in_msg[ssl->in_msglen - i] != padlen - 1 )
623 SSL_DEBUG_MSG( 1, ( "bad padding byte: should be "
624 "%02x, but is %02x", padlen - 1,
625 ssl->in_msg[ssl->in_msglen - i] ) );
626 padlen = 0;
632 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
633 ssl->in_msg, ssl->in_msglen );
636 * Always compute the MAC (RFC4346, CBCTIME).
638 ssl->in_msglen -= ( ssl->maclen + padlen );
640 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
641 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
643 memcpy( tmp, ssl->in_msg + ssl->in_msglen, 20 );
645 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
647 if( ssl->maclen == 16 )
648 ssl_mac_md5( ssl->mac_dec,
649 ssl->in_msg, ssl->in_msglen,
650 ssl->in_ctr, ssl->in_msgtype );
651 else
652 ssl_mac_sha1( ssl->mac_dec,
653 ssl->in_msg, ssl->in_msglen,
654 ssl->in_ctr, ssl->in_msgtype );
656 else
658 if( ssl->maclen == 16 )
659 md5_hmac( ssl->mac_dec, 16,
660 ssl->in_ctr, ssl->in_msglen + 13,
661 ssl->in_msg + ssl->in_msglen );
662 else
663 sha1_hmac( ssl->mac_dec, 20,
664 ssl->in_ctr, ssl->in_msglen + 13,
665 ssl->in_msg + ssl->in_msglen );
668 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->maclen );
669 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
670 ssl->maclen );
672 if( memcmp( tmp, ssl->in_msg + ssl->in_msglen,
673 ssl->maclen ) != 0 )
675 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
676 return( XYSSL_ERR_SSL_INVALID_MAC );
680 * Finally check the padding length; bad padding
681 * will produce the same error as an invalid MAC.
683 if( ssl->ivlen != 0 && padlen == 0 )
684 return( XYSSL_ERR_SSL_INVALID_MAC );
686 if( ssl->in_msglen == 0 )
688 ssl->nb_zero++;
691 * Three or more empty messages may be a DoS attack
692 * (excessive CPU consumption).
694 if( ssl->nb_zero > 3 )
696 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
697 "messages, possible DoS attack" ) );
698 return( XYSSL_ERR_SSL_INVALID_MAC );
701 else
702 ssl->nb_zero = 0;
704 for( i = 7; i >= 0; i-- )
705 if( ++ssl->in_ctr[i] != 0 )
706 break;
708 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
710 return( 0 );
714 * Fill the input message buffer
716 int ssl_fetch_input( ssl_context *ssl, int nb_want )
718 int ret, len;
720 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
722 while( ssl->in_left < nb_want )
724 len = nb_want - ssl->in_left;
725 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
727 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
728 ssl->in_left, nb_want ) );
729 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
731 if( ret < 0 )
732 return( ret );
734 ssl->in_left += ret;
737 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
739 return( 0 );
743 * Flush any data not yet written
745 int ssl_flush_output( ssl_context *ssl )
747 int ret;
748 unsigned char *buf;
750 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
752 while( ssl->out_left > 0 )
754 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
755 5 + ssl->out_msglen, ssl->out_left ) );
757 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
758 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
759 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
761 if( ret <= 0 )
762 return( ret );
764 ssl->out_left -= ret;
767 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
769 return( 0 );
773 * Record layer functions
775 int ssl_write_record( ssl_context *ssl )
777 int ret, len = ssl->out_msglen;
779 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
781 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
782 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
783 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
784 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
785 ssl->out_hdr[4] = (unsigned char)( len );
787 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
789 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
790 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
791 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
793 md5_update( &ssl->fin_md5 , ssl->out_msg, len );
794 sha1_update( &ssl->fin_sha1, ssl->out_msg, len );
797 if( ssl->do_crypt != 0 )
799 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
801 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
802 return( ret );
805 len = ssl->out_msglen;
806 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
807 ssl->out_hdr[4] = (unsigned char)( len );
810 ssl->out_left = 5 + ssl->out_msglen;
812 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
813 "version = [%d:%d], msglen = %d",
814 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
815 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
817 SSL_DEBUG_BUF( 4, "output record sent to network",
818 ssl->out_hdr, 5 + ssl->out_msglen );
820 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
822 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
823 return( ret );
826 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
828 return( 0 );
831 int ssl_read_record( ssl_context *ssl )
833 int ret;
835 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
837 if( ssl->in_hslen != 0 &&
838 ssl->in_hslen < ssl->in_msglen )
841 * Get next Handshake message in the current record
843 ssl->in_msglen -= ssl->in_hslen;
845 memcpy( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
846 ssl->in_msglen );
848 ssl->in_hslen = 4;
849 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
851 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
852 " %d, type = %d, hslen = %d",
853 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
855 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
857 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
858 return( XYSSL_ERR_SSL_INVALID_RECORD );
861 if( ssl->in_msglen < ssl->in_hslen )
863 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
864 return( XYSSL_ERR_SSL_INVALID_RECORD );
867 md5_update( &ssl->fin_md5 , ssl->in_msg, ssl->in_hslen );
868 sha1_update( &ssl->fin_sha1, ssl->in_msg, ssl->in_hslen );
870 return( 0 );
873 ssl->in_hslen = 0;
876 * Read the record header and validate it
878 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
880 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
881 return( ret );
884 ssl->in_msgtype = ssl->in_hdr[0];
885 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
887 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
888 "version = [%d:%d], msglen = %d",
889 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
890 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
892 if( ssl->in_hdr[1] != ssl->major_ver )
894 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
895 return( XYSSL_ERR_SSL_INVALID_RECORD );
898 if( ssl->in_hdr[2] != SSL_MINOR_VERSION_0 &&
899 ssl->in_hdr[2] != SSL_MINOR_VERSION_1 )
901 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
902 return( XYSSL_ERR_SSL_INVALID_RECORD );
906 * Make sure the message length is acceptable
908 if( ssl->do_crypt == 0 )
910 if( ssl->in_msglen < 1 ||
911 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
913 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
914 return( XYSSL_ERR_SSL_INVALID_RECORD );
917 else
919 if( ssl->in_msglen < ssl->minlen )
921 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
922 return( XYSSL_ERR_SSL_INVALID_RECORD );
925 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
926 ssl->in_msglen > ssl->minlen + SSL_MAX_CONTENT_LEN )
928 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
929 return( XYSSL_ERR_SSL_INVALID_RECORD );
933 * TLS encrypted messages can have up to 256 bytes of padding
935 if( ssl->minor_ver == SSL_MINOR_VERSION_1 &&
936 ssl->in_msglen > ssl->minlen + SSL_MAX_CONTENT_LEN + 256 )
938 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
939 return( XYSSL_ERR_SSL_INVALID_RECORD );
944 * Read and optionally decrypt the message contents
946 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
948 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
949 return( ret );
952 SSL_DEBUG_BUF( 4, "input record from network",
953 ssl->in_hdr, 5 + ssl->in_msglen );
955 if( ssl->do_crypt != 0 )
957 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
959 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
960 return( ret );
963 SSL_DEBUG_BUF( 4, "input payload after decrypt",
964 ssl->in_msg, ssl->in_msglen );
966 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
968 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
969 return( XYSSL_ERR_SSL_INVALID_RECORD );
973 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
975 ssl->in_hslen = 4;
976 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
978 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
979 " %d, type = %d, hslen = %d",
980 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
983 * Additional checks to validate the handshake header
985 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
987 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
988 return( XYSSL_ERR_SSL_INVALID_RECORD );
991 if( ssl->in_msglen < ssl->in_hslen )
993 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
994 return( XYSSL_ERR_SSL_INVALID_RECORD );
997 md5_update( &ssl->fin_md5 , ssl->in_msg, ssl->in_hslen );
998 sha1_update( &ssl->fin_sha1, ssl->in_msg, ssl->in_hslen );
1001 if( ssl->in_msgtype == SSL_MSG_ALERT )
1003 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
1004 ssl->in_msg[0], ssl->in_msg[1] ) );
1007 * Ignore non-fatal alerts, except close_notify
1009 if( ssl->in_msg[0] == SSL_ALERT_FATAL )
1011 SSL_DEBUG_MSG( 1, ( "is a fatal alert message" ) );
1012 return( XYSSL_ERR_SSL_FATAL_ALERT_MESSAGE | ssl->in_msg[1] );
1015 if( ssl->in_msg[0] == SSL_ALERT_WARNING &&
1016 ssl->in_msg[1] == SSL_ALERT_CLOSE_NOTIFY )
1018 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
1019 return( XYSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
1023 ssl->in_left = 0;
1025 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
1027 return( 0 );
1031 * Handshake functions
1033 int ssl_write_certificate( ssl_context *ssl )
1035 int ret, i, n;
1036 x509_cert *crt;
1038 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
1040 if( ssl->endpoint == SSL_IS_CLIENT )
1042 if( ssl->client_auth == 0 )
1044 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
1045 ssl->state++;
1046 return( 0 );
1050 * If using SSLv3 and got no cert, send an Alert message
1051 * (otherwise an empty Certificate message will be sent).
1053 if( ssl->own_cert == NULL &&
1054 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1056 ssl->out_msglen = 2;
1057 ssl->out_msgtype = SSL_MSG_ALERT;
1058 ssl->out_msg[0] = SSL_ALERT_WARNING;
1059 ssl->out_msg[1] = SSL_ALERT_NO_CERTIFICATE;
1061 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
1062 goto write_msg;
1065 else /* SSL_IS_SERVER */
1067 if( ssl->own_cert == NULL )
1069 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
1070 return( XYSSL_ERR_SSL_CERTIFICATE_REQUIRED );
1074 SSL_DEBUG_CRT( 3, "own certificate", ssl->own_cert );
1077 * 0 . 0 handshake type
1078 * 1 . 3 handshake length
1079 * 4 . 6 length of all certs
1080 * 7 . 9 length of cert. 1
1081 * 10 . n-1 peer certificate
1082 * n . n+2 length of cert. 2
1083 * n+3 . ... upper level cert, etc.
1085 i = 7;
1086 crt = ssl->own_cert;
1088 while( crt != NULL && crt->next != NULL )
1090 n = crt->raw.len;
1091 if( i + 3 + n > SSL_MAX_CONTENT_LEN )
1093 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
1094 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
1095 return( XYSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
1098 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
1099 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
1100 ssl->out_msg[i + 2] = (unsigned char)( n );
1102 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
1103 i += n; crt = crt->next;
1106 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
1107 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
1108 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
1110 ssl->out_msglen = i;
1111 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1112 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
1114 write_msg:
1116 ssl->state++;
1118 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1120 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1121 return( ret );
1124 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
1126 return( 0 );
1129 int ssl_parse_certificate( ssl_context *ssl )
1131 int ret, i, n;
1133 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
1135 if( ssl->endpoint == SSL_IS_SERVER &&
1136 ssl->authmode == SSL_VERIFY_NONE )
1138 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
1139 ssl->state++;
1140 return( 0 );
1143 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1145 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1146 return( ret );
1149 ssl->state++;
1152 * Check if the client sent an empty certificate
1154 if( ssl->endpoint == SSL_IS_SERVER &&
1155 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1157 if( ssl->in_msglen == 2 &&
1158 ssl->in_msgtype == SSL_MSG_ALERT &&
1159 ssl->in_msg[0] == SSL_ALERT_WARNING &&
1160 ssl->in_msg[1] == SSL_ALERT_NO_CERTIFICATE )
1162 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
1164 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
1165 return( 0 );
1166 else
1167 return( XYSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
1171 if( ssl->endpoint == SSL_IS_SERVER &&
1172 ssl->minor_ver != SSL_MINOR_VERSION_0 )
1174 if( ssl->in_hslen == 7 &&
1175 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
1176 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
1177 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
1179 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
1181 if( ssl->authmode == SSL_VERIFY_REQUIRED )
1182 return( XYSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
1183 else
1184 return( 0 );
1188 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1190 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
1191 return( XYSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1194 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
1196 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
1197 return( XYSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1201 * Same message structure as in ssl_write_certificate()
1203 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
1205 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
1207 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
1208 return( XYSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1211 if( ( ssl->peer_cert = (x509_cert *) malloc(
1212 sizeof( x509_cert ) ) ) == NULL )
1214 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
1215 sizeof( x509_cert ) ) );
1216 return( 1 );
1219 memset( ssl->peer_cert, 0, sizeof( x509_cert ) );
1221 i = 7;
1223 while( i < ssl->in_hslen )
1225 if( ssl->in_msg[i] != 0 )
1227 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
1228 return( XYSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1231 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
1232 | (unsigned int) ssl->in_msg[i + 2];
1233 i += 3;
1235 if( n < 128 || i + n > ssl->in_hslen )
1237 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
1238 return( XYSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1241 ret = x509parse_crt( ssl->peer_cert, ssl->in_msg + i, n );
1242 if( ret != 0 )
1244 SSL_DEBUG_RET( 1, " x509parse_crt", ret );
1245 return( ret );
1248 i += n;
1251 SSL_DEBUG_CRT( 3, "peer certificate", ssl->peer_cert );
1253 if( ssl->authmode != SSL_VERIFY_NONE )
1255 if( ssl->ca_chain == NULL )
1257 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
1258 return( XYSSL_ERR_SSL_CA_CHAIN_REQUIRED );
1261 ret = x509parse_verify( ssl->peer_cert, ssl->ca_chain,
1262 ssl->peer_cn, &ssl->verify_result );
1264 if( ret != 0 )
1265 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
1267 if( ssl->authmode != SSL_VERIFY_REQUIRED )
1268 ret = 0;
1271 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
1273 return( ret );
1276 int ssl_write_change_cipher_spec( ssl_context *ssl )
1278 int ret;
1280 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1282 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
1283 ssl->out_msglen = 1;
1284 ssl->out_msg[0] = 1;
1286 ssl->do_crypt = 0;
1287 ssl->state++;
1289 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1291 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1292 return( ret );
1295 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1297 return( 0 );
1300 int ssl_parse_change_cipher_spec( ssl_context *ssl )
1302 int ret;
1304 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
1306 ssl->do_crypt = 0;
1308 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1310 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1311 return( ret );
1314 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
1316 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
1317 return( XYSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1320 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
1322 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
1323 return( XYSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
1326 ssl->state++;
1328 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
1330 return( 0 );
1333 static void ssl_calc_finished(
1334 ssl_context *ssl, unsigned char *buf, int from,
1335 md5_context *md5, sha1_context *sha1 )
1337 int len = 12;
1338 char *sender;
1339 unsigned char padbuf[48];
1340 unsigned char md5sum[16];
1341 unsigned char sha1sum[20];
1343 SSL_DEBUG_MSG( 2, ( "=> calc finished" ) );
1346 * SSLv3:
1347 * hash =
1348 * MD5( master + pad2 +
1349 * MD5( handshake + sender + master + pad1 ) )
1350 * + SHA1( master + pad2 +
1351 * SHA1( handshake + sender + master + pad1 ) )
1353 * TLSv1:
1354 * hash = PRF( master, finished_label,
1355 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1358 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1359 md5->state, sizeof( md5->state ) );
1361 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1362 sha1->state, sizeof( sha1->state ) );
1364 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1366 sender = ( from == SSL_IS_CLIENT ) ? (char *) "CLNT"
1367 : (char *) "SRVR";
1369 memset( padbuf, 0x36, 48 );
1371 md5_update( md5, (unsigned char *) sender, 4 );
1372 md5_update( md5, ssl->session->master, 48 );
1373 md5_update( md5, padbuf, 48 );
1374 md5_finish( md5, md5sum );
1376 sha1_update( sha1, (unsigned char *) sender, 4 );
1377 sha1_update( sha1, ssl->session->master, 48 );
1378 sha1_update( sha1, padbuf, 40 );
1379 sha1_finish( sha1, sha1sum );
1381 memset( padbuf, 0x5C, 48 );
1383 md5_starts( md5 );
1384 md5_update( md5, ssl->session->master, 48 );
1385 md5_update( md5, padbuf, 48 );
1386 md5_update( md5, md5sum, 16 );
1387 md5_finish( md5, buf );
1389 sha1_starts( sha1 );
1390 sha1_update( sha1, ssl->session->master, 48 );
1391 sha1_update( sha1, padbuf , 40 );
1392 sha1_update( sha1, sha1sum, 20 );
1393 sha1_finish( sha1, buf + 16 );
1395 len += 24;
1397 else
1399 sender = ( from == SSL_IS_CLIENT )
1400 ? (char *) "client finished"
1401 : (char *) "server finished";
1403 md5_finish( md5, padbuf );
1404 sha1_finish( sha1, padbuf + 16 );
1406 tls1_prf( ssl->session->master, 48, sender,
1407 padbuf, 36, buf, len );
1410 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1412 memset( md5, 0, sizeof( md5_context ) );
1413 memset( sha1, 0, sizeof( sha1_context ) );
1415 memset( padbuf, 0, sizeof( padbuf ) );
1416 memset( md5sum, 0, sizeof( md5sum ) );
1417 memset( sha1sum, 0, sizeof( sha1sum ) );
1419 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1422 int ssl_write_finished( ssl_context *ssl )
1424 int ret, hash_len;
1425 md5_context md5;
1426 sha1_context sha1;
1428 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
1430 memcpy( &md5 , &ssl->fin_md5 , sizeof( md5_context ) );
1431 memcpy( &sha1, &ssl->fin_sha1, sizeof( sha1_context ) );
1433 ssl_calc_finished( ssl, ssl->out_msg + 4,
1434 ssl->endpoint, &md5, &sha1 );
1436 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
1438 ssl->out_msglen = 4 + hash_len;
1439 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1440 ssl->out_msg[0] = SSL_HS_FINISHED;
1443 * In case of session resuming, invert the client and server
1444 * ChangeCipherSpec messages order.
1446 if( ssl->resume != 0 )
1448 if( ssl->endpoint == SSL_IS_CLIENT )
1449 ssl->state = SSL_HANDSHAKE_OVER;
1450 else
1451 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
1453 else
1454 ssl->state++;
1456 ssl->do_crypt = 1;
1458 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1460 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1461 return( ret );
1464 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
1466 return( 0 );
1469 int ssl_parse_finished( ssl_context *ssl )
1471 int ret, hash_len;
1472 md5_context md5;
1473 sha1_context sha1;
1474 unsigned char buf[36];
1476 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
1478 memcpy( &md5 , &ssl->fin_md5 , sizeof( md5_context ) );
1479 memcpy( &sha1, &ssl->fin_sha1, sizeof( sha1_context ) );
1481 ssl->do_crypt = 1;
1483 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1485 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1486 return( ret );
1489 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1491 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1492 return( XYSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1495 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
1497 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
1498 ssl->in_hslen != 4 + hash_len )
1500 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1501 return( XYSSL_ERR_SSL_BAD_HS_FINISHED );
1504 ssl_calc_finished( ssl, buf, ssl->endpoint ^ 1, &md5, &sha1 );
1506 if( memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
1508 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1509 return( XYSSL_ERR_SSL_BAD_HS_FINISHED );
1512 if( ssl->resume != 0 )
1514 if( ssl->endpoint == SSL_IS_CLIENT )
1515 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
1517 if( ssl->endpoint == SSL_IS_SERVER )
1518 ssl->state = SSL_HANDSHAKE_OVER;
1520 else
1521 ssl->state++;
1523 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
1525 return( 0 );
1529 * Initialize an SSL context
1531 int ssl_init( ssl_context *ssl )
1533 int len = SSL_BUFFER_LEN;
1535 memset( ssl, 0, sizeof( ssl_context ) );
1537 ssl->in_ctr = (unsigned char *) malloc( len );
1538 ssl->in_hdr = ssl->in_ctr + 8;
1539 ssl->in_msg = ssl->in_ctr + 13;
1541 if( ssl->in_ctr == NULL )
1543 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
1544 return( 1 );
1547 ssl->out_ctr = (unsigned char *) malloc( len );
1548 ssl->out_hdr = ssl->out_ctr + 8;
1549 ssl->out_msg = ssl->out_ctr + 13;
1551 if( ssl->out_ctr == NULL )
1553 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
1554 free( ssl-> in_ctr );
1555 return( 1 );
1558 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
1559 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
1561 md5_starts( &ssl->fin_md5 );
1562 sha1_starts( &ssl->fin_sha1 );
1564 return( 0 );
1568 * SSL set accessors
1570 void ssl_set_debuglvl( ssl_context *ssl, int debuglvl )
1572 ssl->debuglvl = debuglvl;
1575 void ssl_set_endpoint( ssl_context *ssl, int endpoint )
1577 ssl->endpoint = endpoint;
1580 void ssl_set_authmode( ssl_context *ssl, int authmode )
1582 ssl->authmode = authmode;
1585 void ssl_set_rng( ssl_context *ssl, int (*f_rng)(void *), void *p_rng )
1587 ssl->f_rng = f_rng;
1588 ssl->p_rng = p_rng;
1591 void ssl_set_bio( ssl_context *ssl,
1592 int (*f_recv)(void *, unsigned char *, int), void *p_recv,
1593 int (*f_send)(void *, unsigned char *, int), void *p_send )
1595 ssl->f_recv = f_recv;
1596 ssl->f_send = f_send;
1598 ssl->p_recv = p_recv;
1599 ssl->p_send = p_send;
1602 void ssl_set_scb( ssl_context *ssl,
1603 int (*s_get)(ssl_context *),
1604 int (*s_set)(ssl_context *) )
1606 ssl->s_get = s_get;
1607 ssl->s_set = s_set;
1610 void ssl_set_session( ssl_context *ssl, int resume, int timeout,
1611 ssl_session *session )
1613 ssl->resume = resume;
1614 ssl->timeout = timeout;
1615 ssl->session = session;
1618 void ssl_set_ciphers( ssl_context *ssl, int *ciphers )
1620 ssl->ciphers = ciphers;
1623 void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
1624 char *peer_cn )
1626 ssl->ca_chain = ca_chain;
1627 ssl->peer_cn = peer_cn;
1630 void ssl_set_own_cert( ssl_context *ssl, x509_cert *own_cert,
1631 rsa_context *rsa_key )
1633 ssl->own_cert = own_cert;
1634 ssl->rsa_key = rsa_key;
1637 int ssl_set_dh_param( ssl_context *ssl, char *dhm_P, char *dhm_G )
1639 int ret;
1641 if( ( ret = mpi_read_string( &ssl->dhm_ctx.P, 16, dhm_P ) ) != 0 )
1643 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
1644 return( ret );
1647 if( ( ret = mpi_read_string( &ssl->dhm_ctx.G, 16, dhm_G ) ) != 0 )
1649 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
1650 return( ret );
1653 return( 0 );
1657 * SSL get accessors
1659 int ssl_get_bytes_avail( ssl_context *ssl )
1661 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
1664 int ssl_get_verify_result( ssl_context *ssl )
1666 return( ssl->verify_result );
1669 char *ssl_get_cipher( ssl_context *ssl )
1671 switch( ssl->session->cipher )
1673 #if defined(XYSSL_ARC4_C)
1674 case SSL_RSA_RC4_128_MD5:
1675 return( "SSL_RSA_RC4_128_MD5" );
1677 case SSL_RSA_RC4_128_SHA:
1678 return( "SSL_RSA_RC4_128_SHA" );
1679 #endif
1681 #if defined(XYSSL_DES_C)
1682 case SSL_RSA_DES_168_SHA:
1683 return( "SSL_RSA_DES_168_SHA" );
1685 case SSL_EDH_RSA_DES_168_SHA:
1686 return( "SSL_EDH_RSA_DES_168_SHA" );
1687 #endif
1689 #if defined(XYSSL_AES_C)
1690 case SSL_RSA_AES_256_SHA:
1691 return( "SSL_RSA_AES_256_SHA" );
1693 case SSL_EDH_RSA_AES_256_SHA:
1694 return( "SSL_EDH_RSA_AES_256_SHA" );
1695 #endif
1697 default:
1698 break;
1701 return( "unknown" );
1704 int ssl_default_ciphers[] =
1706 #if defined(XYSSL_DHM_C)
1707 #if defined(XYSSL_AES_C)
1708 SSL_EDH_RSA_AES_256_SHA,
1709 #endif
1710 #if defined(XYSSL_DES_C)
1711 SSL_EDH_RSA_DES_168_SHA,
1712 #endif
1713 #endif
1715 #if defined(XYSSL_AES_C)
1716 SSL_RSA_AES_256_SHA,
1717 #endif
1718 #if defined(XYSSL_DES_C)
1719 SSL_RSA_DES_168_SHA,
1720 #endif
1721 #if defined(XYSSL_ARC4_C)
1722 SSL_RSA_RC4_128_SHA,
1723 SSL_RSA_RC4_128_MD5,
1724 #endif
1729 * Perform the SSL handshake
1731 int ssl_handshake( ssl_context *ssl )
1733 int ret = XYSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1735 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
1737 #if defined(XYSSL_SSL_CLI_C)
1738 if( ssl->endpoint == SSL_IS_CLIENT )
1739 ret = ssl_handshake_client( ssl );
1740 #endif
1742 #if defined(XYSSL_SSL_SRV_C)
1743 if( ssl->endpoint == SSL_IS_SERVER )
1744 ret = ssl_handshake_server( ssl );
1745 #endif
1747 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
1749 return( ret );
1753 * Receive application data decrypted from the SSL layer
1755 int ssl_read( ssl_context *ssl, unsigned char *buf, int len )
1757 int ret, n;
1759 SSL_DEBUG_MSG( 2, ( "=> read" ) );
1761 if( ssl->state != SSL_HANDSHAKE_OVER )
1763 if( ( ret = ssl_handshake( ssl ) ) != 0 )
1765 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
1766 return( ret );
1770 if( ssl->in_offt == NULL )
1772 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1774 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1775 return( ret );
1778 if( ssl->in_msglen == 0 &&
1779 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
1782 * OpenSSL sends empty messages to randomize the IV
1784 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1786 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1787 return( ret );
1791 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
1793 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
1794 return( XYSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1797 ssl->in_offt = ssl->in_msg;
1800 n = ( len < ssl->in_msglen )
1801 ? len : ssl->in_msglen;
1803 memcpy( buf, ssl->in_offt, n );
1804 ssl->in_msglen -= n;
1806 if( ssl->in_msglen == 0 )
1807 /* all bytes consumed */
1808 ssl->in_offt = NULL;
1809 else
1810 /* more data available */
1811 ssl->in_offt += n;
1813 SSL_DEBUG_MSG( 2, ( "<= read" ) );
1815 return( n );
1819 * Send application data to be encrypted by the SSL layer
1821 int ssl_write( ssl_context *ssl, unsigned char *buf, int len )
1823 int ret, n;
1825 SSL_DEBUG_MSG( 2, ( "=> write" ) );
1827 if( ssl->state != SSL_HANDSHAKE_OVER )
1829 if( ( ret = ssl_handshake( ssl ) ) != 0 )
1831 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
1832 return( ret );
1836 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1838 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1839 return( ret );
1842 n = ( len < SSL_MAX_CONTENT_LEN )
1843 ? len : SSL_MAX_CONTENT_LEN;
1845 ssl->out_msglen = n;
1846 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
1847 memcpy( ssl->out_msg, buf, n );
1849 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1851 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1852 return( ret );
1855 SSL_DEBUG_MSG( 2, ( "<= write" ) );
1857 return( n );
1861 * Notify the peer that the connection is being closed
1863 int ssl_close_notify( ssl_context *ssl )
1865 int ret;
1867 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
1869 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1871 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1872 return( ret );
1875 if( ssl->state == SSL_HANDSHAKE_OVER )
1877 ssl->out_msgtype = SSL_MSG_ALERT;
1878 ssl->out_msglen = 2;
1879 ssl->out_msg[0] = SSL_ALERT_WARNING;
1880 ssl->out_msg[1] = SSL_ALERT_CLOSE_NOTIFY;
1882 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1884 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1885 return( ret );
1889 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
1891 return( ret );
1895 * Free an SSL context
1897 void ssl_free( ssl_context *ssl )
1899 SSL_DEBUG_MSG( 2, ( "=> free" ) );
1901 if( ssl->peer_cert != NULL )
1903 x509_free( ssl->peer_cert );
1904 memset( ssl->peer_cert, 0, sizeof( x509_cert ) );
1905 free( ssl->peer_cert );
1908 if( ssl->out_ctr != NULL )
1910 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
1911 free( ssl->out_ctr );
1914 if( ssl->in_ctr != NULL )
1916 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
1917 free( ssl->in_ctr );
1920 #if defined(XYSSL_DHM_C)
1921 dhm_free( &ssl->dhm_ctx );
1922 #endif
1924 memset( ssl, 0, sizeof( ssl_context ) );
1926 SSL_DEBUG_MSG( 2, ( "<= free" ) );
1929 #endif