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,
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"
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
)
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;
60 S2
= secret
+ slen
- hs
;
63 memcpy( tmp
+ 20, label
, nb
);
64 memcpy( tmp
+ 20 + nb
, random
, 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
) );
105 int ssl_derive_keys( ssl_context
*ssl
)
110 unsigned char tmp
[64];
111 unsigned char padding
[16];
112 unsigned char sha1sum
[20];
113 unsigned char keyblk
[256];
117 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
122 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
123 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
124 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
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
);
148 md5_update( &md5
, ssl
->premaster
, len
);
149 md5_update( &md5
, sha1sum
, 20 );
150 md5_finish( &md5
, ssl
->session
->master
+ i
* 16 );
154 tls1_prf( ssl
->premaster
, len
, "master secret",
155 ssl
->randbytes
, 64, ssl
->session
->master
, 48 );
157 memset( ssl
->premaster
, 0, sizeof( ssl
->premaster
) );
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
) );
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 ) ) +
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
);
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
) );
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;
228 case SSL_RSA_RC4_128_SHA
:
229 ssl
->keylen
= 16; ssl
->minlen
= 20;
230 ssl
->ivlen
= 0; ssl
->maclen
= 20;
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;
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;
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
,
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
,
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
);
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
);
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 );
314 return( XYSSL_ERR_SSL_FEATURE_UNAVAILABLE
);
317 memset( keyblk
, 0, sizeof( keyblk
) );
319 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
324 void ssl_calc_verify( ssl_context
*ssl
, unsigned char hash
[36] )
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
);
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 );
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" ) );
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];
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 );
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 );
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];
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
)
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
);
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 )
480 if( ssl
->ivlen
== 0 )
482 #if defined(XYSSL_ARC4_C)
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
);
495 return( XYSSL_ERR_SSL_FEATURE_UNAVAILABLE
);
500 padlen
= ssl
->ivlen
- ( ssl
->out_msglen
+ 1 ) % ssl
->ivlen
;
501 if( padlen
== ssl
->ivlen
)
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
);
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
);
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
);
535 return( XYSSL_ERR_SSL_FEATURE_UNAVAILABLE
);
539 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
544 static int ssl_decrypt_buf( ssl_context
*ssl
)
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)
562 arc4_crypt( (arc4_context
*) ssl
->ctx_dec
,
563 ssl
->in_msg
, ssl
->in_msglen
);
565 return( XYSSL_ERR_SSL_FEATURE_UNAVAILABLE
);
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
);
582 #if defined(XYSSL_DES_C)
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
);
590 #if defined(XYSSL_AES_C)
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
);
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
) );
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
] ) );
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
);
652 ssl_mac_sha1( ssl
->mac_dec
,
653 ssl
->in_msg
, ssl
->in_msglen
,
654 ssl
->in_ctr
, ssl
->in_msgtype
);
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
);
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
,
672 if( memcmp( tmp
, ssl
->in_msg
+ ssl
->in_msglen
,
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 )
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
);
704 for( i
= 7; i
>= 0; i
-- )
705 if( ++ssl
->in_ctr
[i
] != 0 )
708 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
714 * Fill the input message buffer
716 int ssl_fetch_input( ssl_context
*ssl
, int nb_want
)
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
);
737 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
743 * Flush any data not yet written
745 int ssl_flush_output( ssl_context
*ssl
)
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
);
764 ssl
->out_left
-= ret
;
767 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
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
);
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
);
826 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
831 int ssl_read_record( ssl_context
*ssl
)
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
,
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
);
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
);
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
);
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
);
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
);
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
)
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
);
1025 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
1031 * Handshake functions
1033 int ssl_write_certificate( ssl_context
*ssl
)
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" ) );
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" ) );
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.
1086 crt
= ssl
->own_cert
;
1088 while( crt
!= NULL
&& crt
->next
!= NULL
)
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
;
1118 if( ( ret
= ssl_write_record( ssl
) ) != 0 )
1120 SSL_DEBUG_RET( 1, "ssl_write_record", ret
);
1124 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
1129 int ssl_parse_certificate( ssl_context
*ssl
)
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" ) );
1143 if( ( ret
= ssl_read_record( ssl
) ) != 0 )
1145 SSL_DEBUG_RET( 1, "ssl_read_record", ret
);
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
)
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
);
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
) ) );
1219 memset( ssl
->peer_cert
, 0, sizeof( x509_cert
) );
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];
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
);
1244 SSL_DEBUG_RET( 1, " x509parse_crt", ret
);
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
);
1265 SSL_DEBUG_RET( 1, "x509_verify_cert", ret
);
1267 if( ssl
->authmode
!= SSL_VERIFY_REQUIRED
)
1271 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
1276 int ssl_write_change_cipher_spec( ssl_context
*ssl
)
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;
1289 if( ( ret
= ssl_write_record( ssl
) ) != 0 )
1291 SSL_DEBUG_RET( 1, "ssl_write_record", ret
);
1295 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1300 int ssl_parse_change_cipher_spec( ssl_context
*ssl
)
1304 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
1308 if( ( ret
= ssl_read_record( ssl
) ) != 0 )
1310 SSL_DEBUG_RET( 1, "ssl_read_record", 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
);
1328 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
1333 static void ssl_calc_finished(
1334 ssl_context
*ssl
, unsigned char *buf
, int from
,
1335 md5_context
*md5
, sha1_context
*sha1
)
1339 unsigned char padbuf
[48];
1340 unsigned char md5sum
[16];
1341 unsigned char sha1sum
[20];
1343 SSL_DEBUG_MSG( 2, ( "=> calc finished" ) );
1348 * MD5( master + pad2 +
1349 * MD5( handshake + sender + master + pad1 ) )
1350 * + SHA1( master + pad2 +
1351 * SHA1( handshake + sender + master + pad1 ) )
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"
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 );
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 );
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
)
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
;
1451 ssl
->state
= SSL_CLIENT_CHANGE_CIPHER_SPEC
;
1458 if( ( ret
= ssl_write_record( ssl
) ) != 0 )
1460 SSL_DEBUG_RET( 1, "ssl_write_record", ret
);
1464 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
1469 int ssl_parse_finished( ssl_context
*ssl
)
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
) );
1483 if( ( ret
= ssl_read_record( ssl
) ) != 0 )
1485 SSL_DEBUG_RET( 1, "ssl_read_record", 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
;
1523 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
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
) );
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
);
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
);
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
)
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
*) )
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
,
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
)
1641 if( ( ret
= mpi_read_string( &ssl
->dhm_ctx
.P
, 16, dhm_P
) ) != 0 )
1643 SSL_DEBUG_RET( 1, "mpi_read_string", ret
);
1647 if( ( ret
= mpi_read_string( &ssl
->dhm_ctx
.G
, 16, dhm_G
) ) != 0 )
1649 SSL_DEBUG_RET( 1, "mpi_read_string", ret
);
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" );
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" );
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" );
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
,
1710 #if defined(XYSSL_DES_C)
1711 SSL_EDH_RSA_DES_168_SHA
,
1715 #if defined(XYSSL_AES_C)
1716 SSL_RSA_AES_256_SHA
,
1718 #if defined(XYSSL_DES_C)
1719 SSL_RSA_DES_168_SHA
,
1721 #if defined(XYSSL_ARC4_C)
1722 SSL_RSA_RC4_128_SHA
,
1723 SSL_RSA_RC4_128_MD5
,
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
);
1742 #if defined(XYSSL_SSL_SRV_C)
1743 if( ssl
->endpoint
== SSL_IS_SERVER
)
1744 ret
= ssl_handshake_server( ssl
);
1747 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
1753 * Receive application data decrypted from the SSL layer
1755 int ssl_read( ssl_context
*ssl
, unsigned char *buf
, int len
)
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
);
1770 if( ssl
->in_offt
== NULL
)
1772 if( ( ret
= ssl_read_record( ssl
) ) != 0 )
1774 SSL_DEBUG_RET( 1, "ssl_read_record", 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
);
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
;
1810 /* more data available */
1813 SSL_DEBUG_MSG( 2, ( "<= read" ) );
1819 * Send application data to be encrypted by the SSL layer
1821 int ssl_write( ssl_context
*ssl
, unsigned char *buf
, int len
)
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
);
1836 if( ( ret
= ssl_flush_output( ssl
) ) != 0 )
1838 SSL_DEBUG_RET( 1, "ssl_flush_output", 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
);
1855 SSL_DEBUG_MSG( 2, ( "<= write" ) );
1861 * Notify the peer that the connection is being closed
1863 int ssl_close_notify( ssl_context
*ssl
)
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
);
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
);
1889 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
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
);
1924 memset( ssl
, 0, sizeof( ssl_context
) );
1926 SSL_DEBUG_MSG( 2, ( "<= free" ) );