summaryrefslogtreecommitdiff
path: root/src/lib/libssl/t1_enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/t1_enc.c')
-rw-r--r--src/lib/libssl/t1_enc.c309
1 files changed, 49 insertions, 260 deletions
diff --git a/src/lib/libssl/t1_enc.c b/src/lib/libssl/t1_enc.c
index f7bdeb3b9d..793ea43e90 100644
--- a/src/lib/libssl/t1_enc.c
+++ b/src/lib/libssl/t1_enc.c
@@ -143,7 +143,6 @@
143#include <openssl/evp.h> 143#include <openssl/evp.h>
144#include <openssl/hmac.h> 144#include <openssl/hmac.h>
145#include <openssl/md5.h> 145#include <openssl/md5.h>
146#include <openssl/rand.h>
147#ifdef KSSL_DEBUG 146#ifdef KSSL_DEBUG
148#include <openssl/des.h> 147#include <openssl/des.h>
149#endif 148#endif
@@ -159,75 +158,68 @@ static int tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
159 unsigned char *out, int olen) 158 unsigned char *out, int olen)
160 { 159 {
161 int chunk; 160 int chunk;
162 size_t j; 161 unsigned int j;
163 EVP_MD_CTX ctx, ctx_tmp; 162 HMAC_CTX ctx;
164 EVP_PKEY *mac_key; 163 HMAC_CTX ctx_tmp;
165 unsigned char A1[EVP_MAX_MD_SIZE]; 164 unsigned char A1[EVP_MAX_MD_SIZE];
166 size_t A1_len; 165 unsigned int A1_len;
167 int ret = 0; 166 int ret = 0;
168 167
169 chunk=EVP_MD_size(md); 168 chunk=EVP_MD_size(md);
170 OPENSSL_assert(chunk >= 0); 169 OPENSSL_assert(chunk >= 0);
171 170
172 EVP_MD_CTX_init(&ctx); 171 HMAC_CTX_init(&ctx);
173 EVP_MD_CTX_init(&ctx_tmp); 172 HMAC_CTX_init(&ctx_tmp);
174 EVP_MD_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); 173 if (!HMAC_Init_ex(&ctx,sec,sec_len,md, NULL))
175 EVP_MD_CTX_set_flags(&ctx_tmp, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
176 mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len);
177 if (!mac_key)
178 goto err;
179 if (!EVP_DigestSignInit(&ctx,NULL,md, NULL, mac_key))
180 goto err; 174 goto err;
181 if (!EVP_DigestSignInit(&ctx_tmp,NULL,md, NULL, mac_key)) 175 if (!HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL))
182 goto err; 176 goto err;
183 if (seed1 && !EVP_DigestSignUpdate(&ctx,seed1,seed1_len)) 177 if (seed1 != NULL && !HMAC_Update(&ctx,seed1,seed1_len))
184 goto err; 178 goto err;
185 if (seed2 && !EVP_DigestSignUpdate(&ctx,seed2,seed2_len)) 179 if (seed2 != NULL && !HMAC_Update(&ctx,seed2,seed2_len))
186 goto err; 180 goto err;
187 if (seed3 && !EVP_DigestSignUpdate(&ctx,seed3,seed3_len)) 181 if (seed3 != NULL && !HMAC_Update(&ctx,seed3,seed3_len))
188 goto err; 182 goto err;
189 if (seed4 && !EVP_DigestSignUpdate(&ctx,seed4,seed4_len)) 183 if (seed4 != NULL && !HMAC_Update(&ctx,seed4,seed4_len))
190 goto err; 184 goto err;
191 if (seed5 && !EVP_DigestSignUpdate(&ctx,seed5,seed5_len)) 185 if (seed5 != NULL && !HMAC_Update(&ctx,seed5,seed5_len))
192 goto err; 186 goto err;
193 if (!EVP_DigestSignFinal(&ctx,A1,&A1_len)) 187 if (!HMAC_Final(&ctx,A1,&A1_len))
194 goto err; 188 goto err;
195 189
196 for (;;) 190 for (;;)
197 { 191 {
198 /* Reinit mac contexts */ 192 if (!HMAC_Init_ex(&ctx,NULL,0,NULL,NULL)) /* re-init */
199 if (!EVP_DigestSignInit(&ctx,NULL,md, NULL, mac_key))
200 goto err; 193 goto err;
201 if (!EVP_DigestSignInit(&ctx_tmp,NULL,md, NULL, mac_key)) 194 if (!HMAC_Init_ex(&ctx_tmp,NULL,0,NULL,NULL)) /* re-init */
202 goto err; 195 goto err;
203 if (!EVP_DigestSignUpdate(&ctx,A1,A1_len)) 196 if (!HMAC_Update(&ctx,A1,A1_len))
204 goto err; 197 goto err;
205 if (!EVP_DigestSignUpdate(&ctx_tmp,A1,A1_len)) 198 if (!HMAC_Update(&ctx_tmp,A1,A1_len))
206 goto err; 199 goto err;
207 if (seed1 && !EVP_DigestSignUpdate(&ctx,seed1,seed1_len)) 200 if (seed1 != NULL && !HMAC_Update(&ctx,seed1,seed1_len))
208 goto err; 201 goto err;
209 if (seed2 && !EVP_DigestSignUpdate(&ctx,seed2,seed2_len)) 202 if (seed2 != NULL && !HMAC_Update(&ctx,seed2,seed2_len))
210 goto err; 203 goto err;
211 if (seed3 && !EVP_DigestSignUpdate(&ctx,seed3,seed3_len)) 204 if (seed3 != NULL && !HMAC_Update(&ctx,seed3,seed3_len))
212 goto err; 205 goto err;
213 if (seed4 && !EVP_DigestSignUpdate(&ctx,seed4,seed4_len)) 206 if (seed4 != NULL && !HMAC_Update(&ctx,seed4,seed4_len))
214 goto err; 207 goto err;
215 if (seed5 && !EVP_DigestSignUpdate(&ctx,seed5,seed5_len)) 208 if (seed5 != NULL && !HMAC_Update(&ctx,seed5,seed5_len))
216 goto err; 209 goto err;
217 210
218 if (olen > chunk) 211 if (olen > chunk)
219 { 212 {
220 if (!EVP_DigestSignFinal(&ctx,out,&j)) 213 if (!HMAC_Final(&ctx,out,&j))
221 goto err; 214 goto err;
222 out+=j; 215 out+=j;
223 olen-=j; 216 olen-=j;
224 /* calc the next A1 value */ 217 if (!HMAC_Final(&ctx_tmp,A1,&A1_len)) /* calc the next A1 value */
225 if (!EVP_DigestSignFinal(&ctx_tmp,A1,&A1_len))
226 goto err; 218 goto err;
227 } 219 }
228 else /* last one */ 220 else /* last one */
229 { 221 {
230 if (!EVP_DigestSignFinal(&ctx,A1,&A1_len)) 222 if (!HMAC_Final(&ctx,A1,&A1_len))
231 goto err; 223 goto err;
232 memcpy(out,A1,olen); 224 memcpy(out,A1,olen);
233 break; 225 break;
@@ -235,9 +227,8 @@ static int tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
235 } 227 }
236 ret = 1; 228 ret = 1;
237err: 229err:
238 EVP_PKEY_free(mac_key); 230 HMAC_CTX_cleanup(&ctx);
239 EVP_MD_CTX_cleanup(&ctx); 231 HMAC_CTX_cleanup(&ctx_tmp);
240 EVP_MD_CTX_cleanup(&ctx_tmp);
241 OPENSSL_cleanse(A1,sizeof(A1)); 232 OPENSSL_cleanse(A1,sizeof(A1));
242 return ret; 233 return ret;
243 } 234 }
@@ -265,8 +256,6 @@ static int tls1_PRF(long digest_mask,
265 if ((m<<TLS1_PRF_DGST_SHIFT) & digest_mask) count++; 256 if ((m<<TLS1_PRF_DGST_SHIFT) & digest_mask) count++;
266 } 257 }
267 len=slen/count; 258 len=slen/count;
268 if (count == 1)
269 slen = 0;
270 S1=sec; 259 S1=sec;
271 memset(out1,0,olen); 260 memset(out1,0,olen);
272 for (idx=0;ssl_get_handshake_digest(idx,&m,&md);idx++) { 261 for (idx=0;ssl_get_handshake_digest(idx,&m,&md);idx++) {
@@ -295,7 +284,7 @@ static int tls1_generate_key_block(SSL *s, unsigned char *km,
295 unsigned char *tmp, int num) 284 unsigned char *tmp, int num)
296 { 285 {
297 int ret; 286 int ret;
298 ret = tls1_PRF(ssl_get_algorithm2(s), 287 ret = tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
299 TLS_MD_KEY_EXPANSION_CONST,TLS_MD_KEY_EXPANSION_CONST_SIZE, 288 TLS_MD_KEY_EXPANSION_CONST,TLS_MD_KEY_EXPANSION_CONST_SIZE,
300 s->s3->server_random,SSL3_RANDOM_SIZE, 289 s->s3->server_random,SSL3_RANDOM_SIZE,
301 s->s3->client_random,SSL3_RANDOM_SIZE, 290 s->s3->client_random,SSL3_RANDOM_SIZE,
@@ -369,7 +358,7 @@ int tls1_change_cipher_state(SSL *s, int which)
369 { 358 {
370 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC) 359 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
371 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM; 360 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
372 else 361 else
373 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM; 362 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
374 363
375 if (s->enc_read_ctx != NULL) 364 if (s->enc_read_ctx != NULL)
@@ -456,11 +445,7 @@ int tls1_change_cipher_state(SSL *s, int which)
456 j=is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ? 445 j=is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?
457 cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl; 446 cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
458 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */ 447 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
459 /* If GCM mode only part of IV comes from PRF */ 448 k=EVP_CIPHER_iv_length(c);
460 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
461 k = EVP_GCM_TLS_FIXED_IV_LEN;
462 else
463 k=EVP_CIPHER_iv_length(c);
464 if ( (which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) || 449 if ( (which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
465 (which == SSL3_CHANGE_CIPHER_SERVER_READ)) 450 (which == SSL3_CHANGE_CIPHER_SERVER_READ))
466 { 451 {
@@ -489,14 +474,10 @@ int tls1_change_cipher_state(SSL *s, int which)
489 } 474 }
490 475
491 memcpy(mac_secret,ms,i); 476 memcpy(mac_secret,ms,i);
492 477 mac_key = EVP_PKEY_new_mac_key(mac_type, NULL,
493 if (!(EVP_CIPHER_flags(c)&EVP_CIPH_FLAG_AEAD_CIPHER)) 478 mac_secret,*mac_secret_size);
494 { 479 EVP_DigestSignInit(mac_ctx,NULL,m,NULL,mac_key);
495 mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, 480 EVP_PKEY_free(mac_key);
496 mac_secret,*mac_secret_size);
497 EVP_DigestSignInit(mac_ctx,NULL,m,NULL,mac_key);
498 EVP_PKEY_free(mac_key);
499 }
500#ifdef TLS_DEBUG 481#ifdef TLS_DEBUG
501printf("which = %04X\nmac key=",which); 482printf("which = %04X\nmac key=",which);
502{ int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); } 483{ int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); }
@@ -506,7 +487,7 @@ printf("which = %04X\nmac key=",which);
506 /* In here I set both the read and write key/iv to the 487 /* In here I set both the read and write key/iv to the
507 * same value since only the correct one will be used :-). 488 * same value since only the correct one will be used :-).
508 */ 489 */
509 if (!tls1_PRF(ssl_get_algorithm2(s), 490 if (!tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
510 exp_label,exp_label_len, 491 exp_label,exp_label_len,
511 s->s3->client_random,SSL3_RANDOM_SIZE, 492 s->s3->client_random,SSL3_RANDOM_SIZE,
512 s->s3->server_random,SSL3_RANDOM_SIZE, 493 s->s3->server_random,SSL3_RANDOM_SIZE,
@@ -517,7 +498,7 @@ printf("which = %04X\nmac key=",which);
517 498
518 if (k > 0) 499 if (k > 0)
519 { 500 {
520 if (!tls1_PRF(ssl_get_algorithm2(s), 501 if (!tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
521 TLS_MD_IV_BLOCK_CONST,TLS_MD_IV_BLOCK_CONST_SIZE, 502 TLS_MD_IV_BLOCK_CONST,TLS_MD_IV_BLOCK_CONST_SIZE,
522 s->s3->client_random,SSL3_RANDOM_SIZE, 503 s->s3->client_random,SSL3_RANDOM_SIZE,
523 s->s3->server_random,SSL3_RANDOM_SIZE, 504 s->s3->server_random,SSL3_RANDOM_SIZE,
@@ -543,19 +524,7 @@ printf("which = %04X\nmac key=",which);
543 } 524 }
544#endif /* KSSL_DEBUG */ 525#endif /* KSSL_DEBUG */
545 526
546 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) 527 EVP_CipherInit_ex(dd,c,NULL,key,iv,(which & SSL3_CC_WRITE));
547 {
548 EVP_CipherInit_ex(dd,c,NULL,key,NULL,(which & SSL3_CC_WRITE));
549 EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, k, iv);
550 }
551 else
552 EVP_CipherInit_ex(dd,c,NULL,key,iv,(which & SSL3_CC_WRITE));
553
554 /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
555 if ((EVP_CIPHER_flags(c)&EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size)
556 EVP_CIPHER_CTX_ctrl(dd,EVP_CTRL_AEAD_SET_MAC_KEY,
557 *mac_secret_size,mac_secret);
558
559#ifdef TLS_DEBUG 528#ifdef TLS_DEBUG
560printf("which = %04X\nkey=",which); 529printf("which = %04X\nkey=",which);
561{ int z; for (z=0; z<EVP_CIPHER_key_length(c); z++) printf("%02X%c",key[z],((z+1)%16)?' ':'\n'); } 530{ int z; for (z=0; z<EVP_CIPHER_key_length(c); z++) printf("%02X%c",key[z],((z+1)%16)?' ':'\n'); }
@@ -637,8 +606,7 @@ printf("\nkey block\n");
637{ int z; for (z=0; z<num; z++) printf("%02X%c",p1[z],((z+1)%16)?' ':'\n'); } 606{ int z; for (z=0; z<num; z++) printf("%02X%c",p1[z],((z+1)%16)?' ':'\n'); }
638#endif 607#endif
639 608
640 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) 609 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
641 && s->method->version <= TLS1_VERSION)
642 { 610 {
643 /* enable vulnerability countermeasure for CBC ciphers with 611 /* enable vulnerability countermeasure for CBC ciphers with
644 * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt) 612 * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt)
@@ -672,14 +640,14 @@ int tls1_enc(SSL *s, int send)
672 SSL3_RECORD *rec; 640 SSL3_RECORD *rec;
673 EVP_CIPHER_CTX *ds; 641 EVP_CIPHER_CTX *ds;
674 unsigned long l; 642 unsigned long l;
675 int bs,i,ii,j,k,pad=0; 643 int bs,i,ii,j,k,n=0;
676 const EVP_CIPHER *enc; 644 const EVP_CIPHER *enc;
677 645
678 if (send) 646 if (send)
679 { 647 {
680 if (EVP_MD_CTX_md(s->write_hash)) 648 if (EVP_MD_CTX_md(s->write_hash))
681 { 649 {
682 int n=EVP_MD_CTX_size(s->write_hash); 650 n=EVP_MD_CTX_size(s->write_hash);
683 OPENSSL_assert(n >= 0); 651 OPENSSL_assert(n >= 0);
684 } 652 }
685 ds=s->enc_write_ctx; 653 ds=s->enc_write_ctx;
@@ -687,34 +655,13 @@ int tls1_enc(SSL *s, int send)
687 if (s->enc_write_ctx == NULL) 655 if (s->enc_write_ctx == NULL)
688 enc=NULL; 656 enc=NULL;
689 else 657 else
690 {
691 int ivlen;
692 enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx); 658 enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
693 /* For TLSv1.1 and later explicit IV */
694 if (s->version >= TLS1_1_VERSION
695 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
696 ivlen = EVP_CIPHER_iv_length(enc);
697 else
698 ivlen = 0;
699 if (ivlen > 1)
700 {
701 if ( rec->data != rec->input)
702 /* we can't write into the input stream:
703 * Can this ever happen?? (steve)
704 */
705 fprintf(stderr,
706 "%s:%d: rec->data != rec->input\n",
707 __FILE__, __LINE__);
708 else if (RAND_bytes(rec->input, ivlen) <= 0)
709 return -1;
710 }
711 }
712 } 659 }
713 else 660 else
714 { 661 {
715 if (EVP_MD_CTX_md(s->read_hash)) 662 if (EVP_MD_CTX_md(s->read_hash))
716 { 663 {
717 int n=EVP_MD_CTX_size(s->read_hash); 664 n=EVP_MD_CTX_size(s->read_hash);
718 OPENSSL_assert(n >= 0); 665 OPENSSL_assert(n >= 0);
719 } 666 }
720 ds=s->enc_read_ctx; 667 ds=s->enc_read_ctx;
@@ -740,43 +687,7 @@ int tls1_enc(SSL *s, int send)
740 l=rec->length; 687 l=rec->length;
741 bs=EVP_CIPHER_block_size(ds->cipher); 688 bs=EVP_CIPHER_block_size(ds->cipher);
742 689
743 if (EVP_CIPHER_flags(ds->cipher)&EVP_CIPH_FLAG_AEAD_CIPHER) 690 if ((bs != 1) && send)
744 {
745 unsigned char buf[13],*seq;
746
747 seq = send?s->s3->write_sequence:s->s3->read_sequence;
748
749 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
750 {
751 unsigned char dtlsseq[9],*p=dtlsseq;
752
753 s2n(send?s->d1->w_epoch:s->d1->r_epoch,p);
754 memcpy(p,&seq[2],6);
755 memcpy(buf,dtlsseq,8);
756 }
757 else
758 {
759 memcpy(buf,seq,8);
760 for (i=7; i>=0; i--) /* increment */
761 {
762 ++seq[i];
763 if (seq[i] != 0) break;
764 }
765 }
766
767 buf[8]=rec->type;
768 buf[9]=(unsigned char)(s->version>>8);
769 buf[10]=(unsigned char)(s->version);
770 buf[11]=rec->length>>8;
771 buf[12]=rec->length&0xff;
772 pad=EVP_CIPHER_CTX_ctrl(ds,EVP_CTRL_AEAD_TLS1_AAD,13,buf);
773 if (send)
774 {
775 l+=pad;
776 rec->length+=pad;
777 }
778 }
779 else if ((bs != 1) && send)
780 { 691 {
781 i=bs-((int)l%bs); 692 i=bs-((int)l%bs);
782 693
@@ -817,25 +728,13 @@ int tls1_enc(SSL *s, int send)
817 { 728 {
818 if (l == 0 || l%bs != 0) 729 if (l == 0 || l%bs != 0)
819 { 730 {
820 if (s->version >= TLS1_1_VERSION)
821 return -1;
822 SSLerr(SSL_F_TLS1_ENC,SSL_R_BLOCK_CIPHER_PAD_IS_WRONG); 731 SSLerr(SSL_F_TLS1_ENC,SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
823 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECRYPTION_FAILED); 732 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECRYPTION_FAILED);
824 return 0; 733 return 0;
825 } 734 }
826 } 735 }
827 736
828 i = EVP_Cipher(ds,rec->data,rec->input,l); 737 EVP_Cipher(ds,rec->data,rec->input,l);
829 if ((EVP_CIPHER_flags(ds->cipher)&EVP_CIPH_FLAG_CUSTOM_CIPHER)
830 ?(i<0)
831 :(i==0))
832 return -1; /* AEAD can fail to verify MAC */
833 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE && !send)
834 {
835 rec->data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
836 rec->input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
837 rec->length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
838 }
839 738
840#ifdef KSSL_DEBUG 739#ifdef KSSL_DEBUG
841 { 740 {
@@ -885,19 +784,8 @@ int tls1_enc(SSL *s, int send)
885 return -1; 784 return -1;
886 } 785 }
887 } 786 }
888 rec->length -=i; 787 rec->length-=i;
889 if (s->version >= TLS1_1_VERSION
890 && EVP_CIPHER_CTX_mode(ds) == EVP_CIPH_CBC_MODE)
891 {
892 if (bs > (int)rec->length)
893 return -1;
894 rec->data += bs; /* skip the explicit IV */
895 rec->input += bs;
896 rec->length -= bs;
897 }
898 } 788 }
899 if (pad && !send)
900 rec->length -= pad;
901 } 789 }
902 return(1); 790 return(1);
903 } 791 }
@@ -953,7 +841,7 @@ int tls1_final_finish_mac(SSL *s,
953 841
954 for (idx=0;ssl_get_handshake_digest(idx,&mask,&md);idx++) 842 for (idx=0;ssl_get_handshake_digest(idx,&mask,&md);idx++)
955 { 843 {
956 if (mask & ssl_get_algorithm2(s)) 844 if (mask & s->s3->tmp.new_cipher->algorithm2)
957 { 845 {
958 int hashsize = EVP_MD_size(md); 846 int hashsize = EVP_MD_size(md);
959 if (hashsize < 0 || hashsize > (int)(sizeof buf - (size_t)(q-buf))) 847 if (hashsize < 0 || hashsize > (int)(sizeof buf - (size_t)(q-buf)))
@@ -972,7 +860,7 @@ int tls1_final_finish_mac(SSL *s,
972 } 860 }
973 } 861 }
974 862
975 if (!tls1_PRF(ssl_get_algorithm2(s), 863 if (!tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
976 str,slen, buf,(int)(q-buf), NULL,0, NULL,0, NULL,0, 864 str,slen, buf,(int)(q-buf), NULL,0, NULL,0, NULL,0,
977 s->session->master_key,s->session->master_key_length, 865 s->session->master_key,s->session->master_key_length,
978 out,buf2,sizeof buf2)) 866 out,buf2,sizeof buf2))
@@ -1082,7 +970,6 @@ int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
1082 const void *co = NULL, *so = NULL; 970 const void *co = NULL, *so = NULL;
1083 int col = 0, sol = 0; 971 int col = 0, sol = 0;
1084 972
1085
1086#ifdef KSSL_DEBUG 973#ifdef KSSL_DEBUG
1087 printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", s,out, p,len); 974 printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", s,out, p,len);
1088#endif /* KSSL_DEBUG */ 975#endif /* KSSL_DEBUG */
@@ -1099,7 +986,7 @@ int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
1099 } 986 }
1100#endif 987#endif
1101 988
1102 tls1_PRF(ssl_get_algorithm2(s), 989 tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
1103 TLS_MD_MASTER_SECRET_CONST,TLS_MD_MASTER_SECRET_CONST_SIZE, 990 TLS_MD_MASTER_SECRET_CONST,TLS_MD_MASTER_SECRET_CONST_SIZE,
1104 s->s3->client_random,SSL3_RANDOM_SIZE, 991 s->s3->client_random,SSL3_RANDOM_SIZE,
1105 co, col, 992 co, col,
@@ -1107,16 +994,6 @@ int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
1107 so, sol, 994 so, sol,
1108 p,len, 995 p,len,
1109 s->session->master_key,buff,sizeof buff); 996 s->session->master_key,buff,sizeof buff);
1110#ifdef SSL_DEBUG
1111 fprintf(stderr, "Premaster Secret:\n");
1112 BIO_dump_fp(stderr, (char *)p, len);
1113 fprintf(stderr, "Client Random:\n");
1114 BIO_dump_fp(stderr, (char *)s->s3->client_random, SSL3_RANDOM_SIZE);
1115 fprintf(stderr, "Server Random:\n");
1116 BIO_dump_fp(stderr, (char *)s->s3->server_random, SSL3_RANDOM_SIZE);
1117 fprintf(stderr, "Master Secret:\n");
1118 BIO_dump_fp(stderr, (char *)s->session->master_key, SSL3_MASTER_SECRET_SIZE);
1119#endif
1120 997
1121#ifdef KSSL_DEBUG 998#ifdef KSSL_DEBUG
1122 printf ("tls1_generate_master_secret() complete\n"); 999 printf ("tls1_generate_master_secret() complete\n");
@@ -1124,95 +1001,6 @@ int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
1124 return(SSL3_MASTER_SECRET_SIZE); 1001 return(SSL3_MASTER_SECRET_SIZE);
1125 } 1002 }
1126 1003
1127int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1128 const char *label, size_t llen, const unsigned char *context,
1129 size_t contextlen, int use_context)
1130 {
1131 unsigned char *buff;
1132 unsigned char *val = NULL;
1133 size_t vallen, currentvalpos;
1134 int rv;
1135
1136#ifdef KSSL_DEBUG
1137 printf ("tls1_export_keying_material(%p,%p,%d,%s,%d,%p,%d)\n", s, out, olen, label, llen, p, plen);
1138#endif /* KSSL_DEBUG */
1139
1140 buff = OPENSSL_malloc(olen);
1141 if (buff == NULL) goto err2;
1142
1143 /* construct PRF arguments
1144 * we construct the PRF argument ourself rather than passing separate
1145 * values into the TLS PRF to ensure that the concatenation of values
1146 * does not create a prohibited label.
1147 */
1148 vallen = llen + SSL3_RANDOM_SIZE * 2;
1149 if (use_context)
1150 {
1151 vallen += 2 + contextlen;
1152 }
1153
1154 val = OPENSSL_malloc(vallen);
1155 if (val == NULL) goto err2;
1156 currentvalpos = 0;
1157 memcpy(val + currentvalpos, (unsigned char *) label, llen);
1158 currentvalpos += llen;
1159 memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
1160 currentvalpos += SSL3_RANDOM_SIZE;
1161 memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
1162 currentvalpos += SSL3_RANDOM_SIZE;
1163
1164 if (use_context)
1165 {
1166 val[currentvalpos] = (contextlen >> 8) & 0xff;
1167 currentvalpos++;
1168 val[currentvalpos] = contextlen & 0xff;
1169 currentvalpos++;
1170 if ((contextlen > 0) || (context != NULL))
1171 {
1172 memcpy(val + currentvalpos, context, contextlen);
1173 }
1174 }
1175
1176 /* disallow prohibited labels
1177 * note that SSL3_RANDOM_SIZE > max(prohibited label len) =
1178 * 15, so size of val > max(prohibited label len) = 15 and the
1179 * comparisons won't have buffer overflow
1180 */
1181 if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
1182 TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0) goto err1;
1183 if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
1184 TLS_MD_SERVER_FINISH_CONST_SIZE) == 0) goto err1;
1185 if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
1186 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0) goto err1;
1187 if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
1188 TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0) goto err1;
1189
1190 rv = tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
1191 val, vallen,
1192 NULL, 0,
1193 NULL, 0,
1194 NULL, 0,
1195 NULL, 0,
1196 s->session->master_key,s->session->master_key_length,
1197 out,buff,olen);
1198
1199#ifdef KSSL_DEBUG
1200 printf ("tls1_export_keying_material() complete\n");
1201#endif /* KSSL_DEBUG */
1202 goto ret;
1203err1:
1204 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
1205 rv = 0;
1206 goto ret;
1207err2:
1208 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
1209 rv = 0;
1210ret:
1211 if (buff != NULL) OPENSSL_free(buff);
1212 if (val != NULL) OPENSSL_free(val);
1213 return(rv);
1214 }
1215
1216int tls1_alert_code(int code) 1004int tls1_alert_code(int code)
1217 { 1005 {
1218 switch (code) 1006 switch (code)
@@ -1254,3 +1042,4 @@ int tls1_alert_code(int code)
1254 default: return(-1); 1042 default: return(-1);
1255 } 1043 }
1256 } 1044 }
1045