summaryrefslogtreecommitdiff
path: root/src/lib/libssl/t1_enc.c
diff options
context:
space:
mode:
authordjm <>2012-10-13 21:25:14 +0000
committerdjm <>2012-10-13 21:25:14 +0000
commit93723b50b639d8dc717bc1bf463fd46e1b321239 (patch)
tree281e0a29ae8f87a8c47fbd4deaa1f3d48b8cc5c1 /src/lib/libssl/t1_enc.c
parent65e72ac55a6405783db7a12d7e35a7561d46005b (diff)
downloadopenbsd-93723b50b639d8dc717bc1bf463fd46e1b321239.tar.gz
openbsd-93723b50b639d8dc717bc1bf463fd46e1b321239.tar.bz2
openbsd-93723b50b639d8dc717bc1bf463fd46e1b321239.zip
resolve conflicts
Diffstat (limited to 'src/lib/libssl/t1_enc.c')
-rw-r--r--src/lib/libssl/t1_enc.c309
1 files changed, 260 insertions, 49 deletions
diff --git a/src/lib/libssl/t1_enc.c b/src/lib/libssl/t1_enc.c
index 793ea43e90..f7bdeb3b9d 100644
--- a/src/lib/libssl/t1_enc.c
+++ b/src/lib/libssl/t1_enc.c
@@ -143,6 +143,7 @@
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>
146#ifdef KSSL_DEBUG 147#ifdef KSSL_DEBUG
147#include <openssl/des.h> 148#include <openssl/des.h>
148#endif 149#endif
@@ -158,68 +159,75 @@ static int tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
158 unsigned char *out, int olen) 159 unsigned char *out, int olen)
159 { 160 {
160 int chunk; 161 int chunk;
161 unsigned int j; 162 size_t j;
162 HMAC_CTX ctx; 163 EVP_MD_CTX ctx, ctx_tmp;
163 HMAC_CTX ctx_tmp; 164 EVP_PKEY *mac_key;
164 unsigned char A1[EVP_MAX_MD_SIZE]; 165 unsigned char A1[EVP_MAX_MD_SIZE];
165 unsigned int A1_len; 166 size_t A1_len;
166 int ret = 0; 167 int ret = 0;
167 168
168 chunk=EVP_MD_size(md); 169 chunk=EVP_MD_size(md);
169 OPENSSL_assert(chunk >= 0); 170 OPENSSL_assert(chunk >= 0);
170 171
171 HMAC_CTX_init(&ctx); 172 EVP_MD_CTX_init(&ctx);
172 HMAC_CTX_init(&ctx_tmp); 173 EVP_MD_CTX_init(&ctx_tmp);
173 if (!HMAC_Init_ex(&ctx,sec,sec_len,md, NULL)) 174 EVP_MD_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
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))
174 goto err; 180 goto err;
175 if (!HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL)) 181 if (!EVP_DigestSignInit(&ctx_tmp,NULL,md, NULL, mac_key))
176 goto err; 182 goto err;
177 if (seed1 != NULL && !HMAC_Update(&ctx,seed1,seed1_len)) 183 if (seed1 && !EVP_DigestSignUpdate(&ctx,seed1,seed1_len))
178 goto err; 184 goto err;
179 if (seed2 != NULL && !HMAC_Update(&ctx,seed2,seed2_len)) 185 if (seed2 && !EVP_DigestSignUpdate(&ctx,seed2,seed2_len))
180 goto err; 186 goto err;
181 if (seed3 != NULL && !HMAC_Update(&ctx,seed3,seed3_len)) 187 if (seed3 && !EVP_DigestSignUpdate(&ctx,seed3,seed3_len))
182 goto err; 188 goto err;
183 if (seed4 != NULL && !HMAC_Update(&ctx,seed4,seed4_len)) 189 if (seed4 && !EVP_DigestSignUpdate(&ctx,seed4,seed4_len))
184 goto err; 190 goto err;
185 if (seed5 != NULL && !HMAC_Update(&ctx,seed5,seed5_len)) 191 if (seed5 && !EVP_DigestSignUpdate(&ctx,seed5,seed5_len))
186 goto err; 192 goto err;
187 if (!HMAC_Final(&ctx,A1,&A1_len)) 193 if (!EVP_DigestSignFinal(&ctx,A1,&A1_len))
188 goto err; 194 goto err;
189 195
190 for (;;) 196 for (;;)
191 { 197 {
192 if (!HMAC_Init_ex(&ctx,NULL,0,NULL,NULL)) /* re-init */ 198 /* Reinit mac contexts */
199 if (!EVP_DigestSignInit(&ctx,NULL,md, NULL, mac_key))
193 goto err; 200 goto err;
194 if (!HMAC_Init_ex(&ctx_tmp,NULL,0,NULL,NULL)) /* re-init */ 201 if (!EVP_DigestSignInit(&ctx_tmp,NULL,md, NULL, mac_key))
195 goto err; 202 goto err;
196 if (!HMAC_Update(&ctx,A1,A1_len)) 203 if (!EVP_DigestSignUpdate(&ctx,A1,A1_len))
197 goto err; 204 goto err;
198 if (!HMAC_Update(&ctx_tmp,A1,A1_len)) 205 if (!EVP_DigestSignUpdate(&ctx_tmp,A1,A1_len))
199 goto err; 206 goto err;
200 if (seed1 != NULL && !HMAC_Update(&ctx,seed1,seed1_len)) 207 if (seed1 && !EVP_DigestSignUpdate(&ctx,seed1,seed1_len))
201 goto err; 208 goto err;
202 if (seed2 != NULL && !HMAC_Update(&ctx,seed2,seed2_len)) 209 if (seed2 && !EVP_DigestSignUpdate(&ctx,seed2,seed2_len))
203 goto err; 210 goto err;
204 if (seed3 != NULL && !HMAC_Update(&ctx,seed3,seed3_len)) 211 if (seed3 && !EVP_DigestSignUpdate(&ctx,seed3,seed3_len))
205 goto err; 212 goto err;
206 if (seed4 != NULL && !HMAC_Update(&ctx,seed4,seed4_len)) 213 if (seed4 && !EVP_DigestSignUpdate(&ctx,seed4,seed4_len))
207 goto err; 214 goto err;
208 if (seed5 != NULL && !HMAC_Update(&ctx,seed5,seed5_len)) 215 if (seed5 && !EVP_DigestSignUpdate(&ctx,seed5,seed5_len))
209 goto err; 216 goto err;
210 217
211 if (olen > chunk) 218 if (olen > chunk)
212 { 219 {
213 if (!HMAC_Final(&ctx,out,&j)) 220 if (!EVP_DigestSignFinal(&ctx,out,&j))
214 goto err; 221 goto err;
215 out+=j; 222 out+=j;
216 olen-=j; 223 olen-=j;
217 if (!HMAC_Final(&ctx_tmp,A1,&A1_len)) /* calc the next A1 value */ 224 /* calc the next A1 value */
225 if (!EVP_DigestSignFinal(&ctx_tmp,A1,&A1_len))
218 goto err; 226 goto err;
219 } 227 }
220 else /* last one */ 228 else /* last one */
221 { 229 {
222 if (!HMAC_Final(&ctx,A1,&A1_len)) 230 if (!EVP_DigestSignFinal(&ctx,A1,&A1_len))
223 goto err; 231 goto err;
224 memcpy(out,A1,olen); 232 memcpy(out,A1,olen);
225 break; 233 break;
@@ -227,8 +235,9 @@ static int tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
227 } 235 }
228 ret = 1; 236 ret = 1;
229err: 237err:
230 HMAC_CTX_cleanup(&ctx); 238 EVP_PKEY_free(mac_key);
231 HMAC_CTX_cleanup(&ctx_tmp); 239 EVP_MD_CTX_cleanup(&ctx);
240 EVP_MD_CTX_cleanup(&ctx_tmp);
232 OPENSSL_cleanse(A1,sizeof(A1)); 241 OPENSSL_cleanse(A1,sizeof(A1));
233 return ret; 242 return ret;
234 } 243 }
@@ -256,6 +265,8 @@ static int tls1_PRF(long digest_mask,
256 if ((m<<TLS1_PRF_DGST_SHIFT) & digest_mask) count++; 265 if ((m<<TLS1_PRF_DGST_SHIFT) & digest_mask) count++;
257 } 266 }
258 len=slen/count; 267 len=slen/count;
268 if (count == 1)
269 slen = 0;
259 S1=sec; 270 S1=sec;
260 memset(out1,0,olen); 271 memset(out1,0,olen);
261 for (idx=0;ssl_get_handshake_digest(idx,&m,&md);idx++) { 272 for (idx=0;ssl_get_handshake_digest(idx,&m,&md);idx++) {
@@ -284,7 +295,7 @@ static int tls1_generate_key_block(SSL *s, unsigned char *km,
284 unsigned char *tmp, int num) 295 unsigned char *tmp, int num)
285 { 296 {
286 int ret; 297 int ret;
287 ret = tls1_PRF(s->s3->tmp.new_cipher->algorithm2, 298 ret = tls1_PRF(ssl_get_algorithm2(s),
288 TLS_MD_KEY_EXPANSION_CONST,TLS_MD_KEY_EXPANSION_CONST_SIZE, 299 TLS_MD_KEY_EXPANSION_CONST,TLS_MD_KEY_EXPANSION_CONST_SIZE,
289 s->s3->server_random,SSL3_RANDOM_SIZE, 300 s->s3->server_random,SSL3_RANDOM_SIZE,
290 s->s3->client_random,SSL3_RANDOM_SIZE, 301 s->s3->client_random,SSL3_RANDOM_SIZE,
@@ -358,7 +369,7 @@ int tls1_change_cipher_state(SSL *s, int which)
358 { 369 {
359 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC) 370 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
360 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM; 371 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
361 else 372 else
362 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM; 373 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
363 374
364 if (s->enc_read_ctx != NULL) 375 if (s->enc_read_ctx != NULL)
@@ -445,7 +456,11 @@ int tls1_change_cipher_state(SSL *s, int which)
445 j=is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ? 456 j=is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?
446 cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl; 457 cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
447 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */ 458 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
448 k=EVP_CIPHER_iv_length(c); 459 /* If GCM mode only part of IV comes from PRF */
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);
449 if ( (which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) || 464 if ( (which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
450 (which == SSL3_CHANGE_CIPHER_SERVER_READ)) 465 (which == SSL3_CHANGE_CIPHER_SERVER_READ))
451 { 466 {
@@ -474,10 +489,14 @@ int tls1_change_cipher_state(SSL *s, int which)
474 } 489 }
475 490
476 memcpy(mac_secret,ms,i); 491 memcpy(mac_secret,ms,i);
477 mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, 492
478 mac_secret,*mac_secret_size); 493 if (!(EVP_CIPHER_flags(c)&EVP_CIPH_FLAG_AEAD_CIPHER))
479 EVP_DigestSignInit(mac_ctx,NULL,m,NULL,mac_key); 494 {
480 EVP_PKEY_free(mac_key); 495 mac_key = EVP_PKEY_new_mac_key(mac_type, NULL,
496 mac_secret,*mac_secret_size);
497 EVP_DigestSignInit(mac_ctx,NULL,m,NULL,mac_key);
498 EVP_PKEY_free(mac_key);
499 }
481#ifdef TLS_DEBUG 500#ifdef TLS_DEBUG
482printf("which = %04X\nmac key=",which); 501printf("which = %04X\nmac key=",which);
483{ int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); } 502{ int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); }
@@ -487,7 +506,7 @@ printf("which = %04X\nmac key=",which);
487 /* In here I set both the read and write key/iv to the 506 /* In here I set both the read and write key/iv to the
488 * same value since only the correct one will be used :-). 507 * same value since only the correct one will be used :-).
489 */ 508 */
490 if (!tls1_PRF(s->s3->tmp.new_cipher->algorithm2, 509 if (!tls1_PRF(ssl_get_algorithm2(s),
491 exp_label,exp_label_len, 510 exp_label,exp_label_len,
492 s->s3->client_random,SSL3_RANDOM_SIZE, 511 s->s3->client_random,SSL3_RANDOM_SIZE,
493 s->s3->server_random,SSL3_RANDOM_SIZE, 512 s->s3->server_random,SSL3_RANDOM_SIZE,
@@ -498,7 +517,7 @@ printf("which = %04X\nmac key=",which);
498 517
499 if (k > 0) 518 if (k > 0)
500 { 519 {
501 if (!tls1_PRF(s->s3->tmp.new_cipher->algorithm2, 520 if (!tls1_PRF(ssl_get_algorithm2(s),
502 TLS_MD_IV_BLOCK_CONST,TLS_MD_IV_BLOCK_CONST_SIZE, 521 TLS_MD_IV_BLOCK_CONST,TLS_MD_IV_BLOCK_CONST_SIZE,
503 s->s3->client_random,SSL3_RANDOM_SIZE, 522 s->s3->client_random,SSL3_RANDOM_SIZE,
504 s->s3->server_random,SSL3_RANDOM_SIZE, 523 s->s3->server_random,SSL3_RANDOM_SIZE,
@@ -524,7 +543,19 @@ printf("which = %04X\nmac key=",which);
524 } 543 }
525#endif /* KSSL_DEBUG */ 544#endif /* KSSL_DEBUG */
526 545
527 EVP_CipherInit_ex(dd,c,NULL,key,iv,(which & SSL3_CC_WRITE)); 546 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
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
528#ifdef TLS_DEBUG 559#ifdef TLS_DEBUG
529printf("which = %04X\nkey=",which); 560printf("which = %04X\nkey=",which);
530{ int z; for (z=0; z<EVP_CIPHER_key_length(c); z++) printf("%02X%c",key[z],((z+1)%16)?' ':'\n'); } 561{ int z; for (z=0; z<EVP_CIPHER_key_length(c); z++) printf("%02X%c",key[z],((z+1)%16)?' ':'\n'); }
@@ -606,7 +637,8 @@ printf("\nkey block\n");
606{ int z; for (z=0; z<num; z++) printf("%02X%c",p1[z],((z+1)%16)?' ':'\n'); } 637{ int z; for (z=0; z<num; z++) printf("%02X%c",p1[z],((z+1)%16)?' ':'\n'); }
607#endif 638#endif
608 639
609 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) 640 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
641 && s->method->version <= TLS1_VERSION)
610 { 642 {
611 /* enable vulnerability countermeasure for CBC ciphers with 643 /* enable vulnerability countermeasure for CBC ciphers with
612 * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt) 644 * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt)
@@ -640,14 +672,14 @@ int tls1_enc(SSL *s, int send)
640 SSL3_RECORD *rec; 672 SSL3_RECORD *rec;
641 EVP_CIPHER_CTX *ds; 673 EVP_CIPHER_CTX *ds;
642 unsigned long l; 674 unsigned long l;
643 int bs,i,ii,j,k,n=0; 675 int bs,i,ii,j,k,pad=0;
644 const EVP_CIPHER *enc; 676 const EVP_CIPHER *enc;
645 677
646 if (send) 678 if (send)
647 { 679 {
648 if (EVP_MD_CTX_md(s->write_hash)) 680 if (EVP_MD_CTX_md(s->write_hash))
649 { 681 {
650 n=EVP_MD_CTX_size(s->write_hash); 682 int n=EVP_MD_CTX_size(s->write_hash);
651 OPENSSL_assert(n >= 0); 683 OPENSSL_assert(n >= 0);
652 } 684 }
653 ds=s->enc_write_ctx; 685 ds=s->enc_write_ctx;
@@ -655,13 +687,34 @@ int tls1_enc(SSL *s, int send)
655 if (s->enc_write_ctx == NULL) 687 if (s->enc_write_ctx == NULL)
656 enc=NULL; 688 enc=NULL;
657 else 689 else
690 {
691 int ivlen;
658 enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx); 692 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 }
659 } 712 }
660 else 713 else
661 { 714 {
662 if (EVP_MD_CTX_md(s->read_hash)) 715 if (EVP_MD_CTX_md(s->read_hash))
663 { 716 {
664 n=EVP_MD_CTX_size(s->read_hash); 717 int n=EVP_MD_CTX_size(s->read_hash);
665 OPENSSL_assert(n >= 0); 718 OPENSSL_assert(n >= 0);
666 } 719 }
667 ds=s->enc_read_ctx; 720 ds=s->enc_read_ctx;
@@ -687,7 +740,43 @@ int tls1_enc(SSL *s, int send)
687 l=rec->length; 740 l=rec->length;
688 bs=EVP_CIPHER_block_size(ds->cipher); 741 bs=EVP_CIPHER_block_size(ds->cipher);
689 742
690 if ((bs != 1) && send) 743 if (EVP_CIPHER_flags(ds->cipher)&EVP_CIPH_FLAG_AEAD_CIPHER)
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)
691 { 780 {
692 i=bs-((int)l%bs); 781 i=bs-((int)l%bs);
693 782
@@ -728,13 +817,25 @@ int tls1_enc(SSL *s, int send)
728 { 817 {
729 if (l == 0 || l%bs != 0) 818 if (l == 0 || l%bs != 0)
730 { 819 {
820 if (s->version >= TLS1_1_VERSION)
821 return -1;
731 SSLerr(SSL_F_TLS1_ENC,SSL_R_BLOCK_CIPHER_PAD_IS_WRONG); 822 SSLerr(SSL_F_TLS1_ENC,SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
732 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECRYPTION_FAILED); 823 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECRYPTION_FAILED);
733 return 0; 824 return 0;
734 } 825 }
735 } 826 }
736 827
737 EVP_Cipher(ds,rec->data,rec->input,l); 828 i = 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 }
738 839
739#ifdef KSSL_DEBUG 840#ifdef KSSL_DEBUG
740 { 841 {
@@ -784,8 +885,19 @@ int tls1_enc(SSL *s, int send)
784 return -1; 885 return -1;
785 } 886 }
786 } 887 }
787 rec->length-=i; 888 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 }
788 } 898 }
899 if (pad && !send)
900 rec->length -= pad;
789 } 901 }
790 return(1); 902 return(1);
791 } 903 }
@@ -841,7 +953,7 @@ int tls1_final_finish_mac(SSL *s,
841 953
842 for (idx=0;ssl_get_handshake_digest(idx,&mask,&md);idx++) 954 for (idx=0;ssl_get_handshake_digest(idx,&mask,&md);idx++)
843 { 955 {
844 if (mask & s->s3->tmp.new_cipher->algorithm2) 956 if (mask & ssl_get_algorithm2(s))
845 { 957 {
846 int hashsize = EVP_MD_size(md); 958 int hashsize = EVP_MD_size(md);
847 if (hashsize < 0 || hashsize > (int)(sizeof buf - (size_t)(q-buf))) 959 if (hashsize < 0 || hashsize > (int)(sizeof buf - (size_t)(q-buf)))
@@ -860,7 +972,7 @@ int tls1_final_finish_mac(SSL *s,
860 } 972 }
861 } 973 }
862 974
863 if (!tls1_PRF(s->s3->tmp.new_cipher->algorithm2, 975 if (!tls1_PRF(ssl_get_algorithm2(s),
864 str,slen, buf,(int)(q-buf), NULL,0, NULL,0, NULL,0, 976 str,slen, buf,(int)(q-buf), NULL,0, NULL,0, NULL,0,
865 s->session->master_key,s->session->master_key_length, 977 s->session->master_key,s->session->master_key_length,
866 out,buf2,sizeof buf2)) 978 out,buf2,sizeof buf2))
@@ -970,6 +1082,7 @@ int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
970 const void *co = NULL, *so = NULL; 1082 const void *co = NULL, *so = NULL;
971 int col = 0, sol = 0; 1083 int col = 0, sol = 0;
972 1084
1085
973#ifdef KSSL_DEBUG 1086#ifdef KSSL_DEBUG
974 printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", s,out, p,len); 1087 printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", s,out, p,len);
975#endif /* KSSL_DEBUG */ 1088#endif /* KSSL_DEBUG */
@@ -986,7 +1099,7 @@ int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
986 } 1099 }
987#endif 1100#endif
988 1101
989 tls1_PRF(s->s3->tmp.new_cipher->algorithm2, 1102 tls1_PRF(ssl_get_algorithm2(s),
990 TLS_MD_MASTER_SECRET_CONST,TLS_MD_MASTER_SECRET_CONST_SIZE, 1103 TLS_MD_MASTER_SECRET_CONST,TLS_MD_MASTER_SECRET_CONST_SIZE,
991 s->s3->client_random,SSL3_RANDOM_SIZE, 1104 s->s3->client_random,SSL3_RANDOM_SIZE,
992 co, col, 1105 co, col,
@@ -994,6 +1107,16 @@ int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
994 so, sol, 1107 so, sol,
995 p,len, 1108 p,len,
996 s->session->master_key,buff,sizeof buff); 1109 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
997 1120
998#ifdef KSSL_DEBUG 1121#ifdef KSSL_DEBUG
999 printf ("tls1_generate_master_secret() complete\n"); 1122 printf ("tls1_generate_master_secret() complete\n");
@@ -1001,6 +1124,95 @@ int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
1001 return(SSL3_MASTER_SECRET_SIZE); 1124 return(SSL3_MASTER_SECRET_SIZE);
1002 } 1125 }
1003 1126
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
1004int tls1_alert_code(int code) 1216int tls1_alert_code(int code)
1005 { 1217 {
1006 switch (code) 1218 switch (code)
@@ -1042,4 +1254,3 @@ int tls1_alert_code(int code)
1042 default: return(-1); 1254 default: return(-1);
1043 } 1255 }
1044 } 1256 }
1045