diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libssl/t1_enc.c | 509 |
1 files changed, 344 insertions, 165 deletions
diff --git a/src/lib/libssl/t1_enc.c b/src/lib/libssl/t1_enc.c index 7cb3e29a41..9719541f2b 100644 --- a/src/lib/libssl/t1_enc.c +++ b/src/lib/libssl/t1_enc.c | |||
| @@ -56,7 +56,7 @@ | |||
| 56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
| 57 | */ | 57 | */ |
| 58 | /* ==================================================================== | 58 | /* ==================================================================== |
| 59 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. | 59 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 60 | * | 60 | * |
| 61 | * Redistribution and use in source and binary forms, with or without | 61 | * Redistribution and use in source and binary forms, with or without |
| 62 | * modification, are permitted provided that the following conditions | 62 | * modification, are permitted provided that the following conditions |
| @@ -108,6 +108,32 @@ | |||
| 108 | * Hudson (tjh@cryptsoft.com). | 108 | * Hudson (tjh@cryptsoft.com). |
| 109 | * | 109 | * |
| 110 | */ | 110 | */ |
| 111 | /* ==================================================================== | ||
| 112 | * Copyright 2005 Nokia. All rights reserved. | ||
| 113 | * | ||
| 114 | * The portions of the attached software ("Contribution") is developed by | ||
| 115 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source | ||
| 116 | * license. | ||
| 117 | * | ||
| 118 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of | ||
| 119 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites | ||
| 120 | * support (see RFC 4279) to OpenSSL. | ||
| 121 | * | ||
| 122 | * No patent licenses or other rights except those expressly stated in | ||
| 123 | * the OpenSSL open source license shall be deemed granted or received | ||
| 124 | * expressly, by implication, estoppel, or otherwise. | ||
| 125 | * | ||
| 126 | * No assurances are provided by Nokia that the Contribution does not | ||
| 127 | * infringe the patent or other intellectual property rights of any third | ||
| 128 | * party or that the license provides you with all the necessary rights | ||
| 129 | * to make use of the Contribution. | ||
| 130 | * | ||
| 131 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN | ||
| 132 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA | ||
| 133 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY | ||
| 134 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR | ||
| 135 | * OTHERWISE. | ||
| 136 | */ | ||
| 111 | 137 | ||
| 112 | #include <stdio.h> | 138 | #include <stdio.h> |
| 113 | #include "ssl_locl.h" | 139 | #include "ssl_locl.h" |
| @@ -121,8 +147,14 @@ | |||
| 121 | #include <openssl/des.h> | 147 | #include <openssl/des.h> |
| 122 | #endif | 148 | #endif |
| 123 | 149 | ||
| 124 | static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec, | 150 | /* seed1 through seed5 are virtually concatenated */ |
| 125 | int sec_len, unsigned char *seed, int seed_len, | 151 | static int tls1_P_hash(const EVP_MD *md, const unsigned char *sec, |
| 152 | int sec_len, | ||
| 153 | const void *seed1, int seed1_len, | ||
| 154 | const void *seed2, int seed2_len, | ||
| 155 | const void *seed3, int seed3_len, | ||
| 156 | const void *seed4, int seed4_len, | ||
| 157 | const void *seed5, int seed5_len, | ||
| 126 | unsigned char *out, int olen) | 158 | unsigned char *out, int olen) |
| 127 | { | 159 | { |
| 128 | int chunk,n; | 160 | int chunk,n; |
| @@ -131,84 +163,133 @@ static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec, | |||
| 131 | HMAC_CTX ctx_tmp; | 163 | HMAC_CTX ctx_tmp; |
| 132 | unsigned char A1[EVP_MAX_MD_SIZE]; | 164 | unsigned char A1[EVP_MAX_MD_SIZE]; |
| 133 | unsigned int A1_len; | 165 | unsigned int A1_len; |
| 166 | int ret = 0; | ||
| 134 | 167 | ||
| 135 | chunk=EVP_MD_size(md); | 168 | chunk=EVP_MD_size(md); |
| 169 | OPENSSL_assert(chunk >= 0); | ||
| 136 | 170 | ||
| 137 | HMAC_CTX_init(&ctx); | 171 | HMAC_CTX_init(&ctx); |
| 138 | HMAC_CTX_init(&ctx_tmp); | 172 | HMAC_CTX_init(&ctx_tmp); |
| 139 | HMAC_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); | 173 | if (!HMAC_Init_ex(&ctx,sec,sec_len,md, NULL)) |
| 140 | HMAC_CTX_set_flags(&ctx_tmp, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); | 174 | goto err; |
| 141 | HMAC_Init_ex(&ctx,sec,sec_len,md, NULL); | 175 | if (!HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL)) |
| 142 | HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL); | 176 | goto err; |
| 143 | HMAC_Update(&ctx,seed,seed_len); | 177 | if (seed1 != NULL && !HMAC_Update(&ctx,seed1,seed1_len)) |
| 144 | HMAC_Final(&ctx,A1,&A1_len); | 178 | goto err; |
| 179 | if (seed2 != NULL && !HMAC_Update(&ctx,seed2,seed2_len)) | ||
| 180 | goto err; | ||
| 181 | if (seed3 != NULL && !HMAC_Update(&ctx,seed3,seed3_len)) | ||
| 182 | goto err; | ||
| 183 | if (seed4 != NULL && !HMAC_Update(&ctx,seed4,seed4_len)) | ||
| 184 | goto err; | ||
| 185 | if (seed5 != NULL && !HMAC_Update(&ctx,seed5,seed5_len)) | ||
| 186 | goto err; | ||
| 187 | if (!HMAC_Final(&ctx,A1,&A1_len)) | ||
| 188 | goto err; | ||
| 145 | 189 | ||
| 146 | n=0; | 190 | n=0; |
| 147 | for (;;) | 191 | for (;;) |
| 148 | { | 192 | { |
| 149 | HMAC_Init_ex(&ctx,NULL,0,NULL,NULL); /* re-init */ | 193 | if (!HMAC_Init_ex(&ctx,NULL,0,NULL,NULL)) /* re-init */ |
| 150 | HMAC_Init_ex(&ctx_tmp,NULL,0,NULL,NULL); /* re-init */ | 194 | goto err; |
| 151 | HMAC_Update(&ctx,A1,A1_len); | 195 | if (!HMAC_Init_ex(&ctx_tmp,NULL,0,NULL,NULL)) /* re-init */ |
| 152 | HMAC_Update(&ctx_tmp,A1,A1_len); | 196 | goto err; |
| 153 | HMAC_Update(&ctx,seed,seed_len); | 197 | if (!HMAC_Update(&ctx,A1,A1_len)) |
| 198 | goto err; | ||
| 199 | if (!HMAC_Update(&ctx_tmp,A1,A1_len)) | ||
| 200 | goto err; | ||
| 201 | if (seed1 != NULL && !HMAC_Update(&ctx,seed1,seed1_len)) | ||
| 202 | goto err; | ||
| 203 | if (seed2 != NULL && !HMAC_Update(&ctx,seed2,seed2_len)) | ||
| 204 | goto err; | ||
| 205 | if (seed3 != NULL && !HMAC_Update(&ctx,seed3,seed3_len)) | ||
| 206 | goto err; | ||
| 207 | if (seed4 != NULL && !HMAC_Update(&ctx,seed4,seed4_len)) | ||
| 208 | goto err; | ||
| 209 | if (seed5 != NULL && !HMAC_Update(&ctx,seed5,seed5_len)) | ||
| 210 | goto err; | ||
| 154 | 211 | ||
| 155 | if (olen > chunk) | 212 | if (olen > chunk) |
| 156 | { | 213 | { |
| 157 | HMAC_Final(&ctx,out,&j); | 214 | if (!HMAC_Final(&ctx,out,&j)) |
| 215 | goto err; | ||
| 158 | out+=j; | 216 | out+=j; |
| 159 | olen-=j; | 217 | olen-=j; |
| 160 | HMAC_Final(&ctx_tmp,A1,&A1_len); /* calc the next A1 value */ | 218 | if (!HMAC_Final(&ctx_tmp,A1,&A1_len)) /* calc the next A1 value */ |
| 219 | goto err; | ||
| 161 | } | 220 | } |
| 162 | else /* last one */ | 221 | else /* last one */ |
| 163 | { | 222 | { |
| 164 | HMAC_Final(&ctx,A1,&A1_len); | 223 | if (!HMAC_Final(&ctx,A1,&A1_len)) |
| 224 | goto err; | ||
| 165 | memcpy(out,A1,olen); | 225 | memcpy(out,A1,olen); |
| 166 | break; | 226 | break; |
| 167 | } | 227 | } |
| 168 | } | 228 | } |
| 229 | ret = 1; | ||
| 230 | err: | ||
| 169 | HMAC_CTX_cleanup(&ctx); | 231 | HMAC_CTX_cleanup(&ctx); |
| 170 | HMAC_CTX_cleanup(&ctx_tmp); | 232 | HMAC_CTX_cleanup(&ctx_tmp); |
| 171 | OPENSSL_cleanse(A1,sizeof(A1)); | 233 | OPENSSL_cleanse(A1,sizeof(A1)); |
| 234 | return ret; | ||
| 172 | } | 235 | } |
| 173 | 236 | ||
| 174 | static void tls1_PRF(const EVP_MD *md5, const EVP_MD *sha1, | 237 | /* seed1 through seed5 are virtually concatenated */ |
| 175 | unsigned char *label, int label_len, | 238 | static int tls1_PRF(long digest_mask, |
| 176 | const unsigned char *sec, int slen, unsigned char *out1, | 239 | const void *seed1, int seed1_len, |
| 240 | const void *seed2, int seed2_len, | ||
| 241 | const void *seed3, int seed3_len, | ||
| 242 | const void *seed4, int seed4_len, | ||
| 243 | const void *seed5, int seed5_len, | ||
| 244 | const unsigned char *sec, int slen, | ||
| 245 | unsigned char *out1, | ||
| 177 | unsigned char *out2, int olen) | 246 | unsigned char *out2, int olen) |
| 178 | { | 247 | { |
| 179 | int len,i; | 248 | int len,i,idx,count; |
| 180 | const unsigned char *S1,*S2; | 249 | const unsigned char *S1; |
| 181 | 250 | long m; | |
| 182 | len=slen/2; | 251 | const EVP_MD *md; |
| 252 | int ret = 0; | ||
| 253 | |||
| 254 | /* Count number of digests and partition sec evenly */ | ||
| 255 | count=0; | ||
| 256 | for (idx=0;ssl_get_handshake_digest(idx,&m,&md);idx++) { | ||
| 257 | if ((m<<TLS1_PRF_DGST_SHIFT) & digest_mask) count++; | ||
| 258 | } | ||
| 259 | len=slen/count; | ||
| 183 | S1=sec; | 260 | S1=sec; |
| 184 | S2= &(sec[len]); | 261 | memset(out1,0,olen); |
| 185 | len+=(slen&1); /* add for odd, make longer */ | 262 | for (idx=0;ssl_get_handshake_digest(idx,&m,&md);idx++) { |
| 186 | 263 | if ((m<<TLS1_PRF_DGST_SHIFT) & digest_mask) { | |
| 187 | 264 | if (!md) { | |
| 188 | tls1_P_hash(md5 ,S1,len,label,label_len,out1,olen); | 265 | SSLerr(SSL_F_TLS1_PRF, |
| 189 | tls1_P_hash(sha1,S2,len,label,label_len,out2,olen); | 266 | SSL_R_UNSUPPORTED_DIGEST_TYPE); |
| 190 | 267 | goto err; | |
| 191 | for (i=0; i<olen; i++) | 268 | } |
| 192 | out1[i]^=out2[i]; | 269 | if (!tls1_P_hash(md ,S1,len+(slen&1), |
| 270 | seed1,seed1_len,seed2,seed2_len,seed3,seed3_len,seed4,seed4_len,seed5,seed5_len, | ||
| 271 | out2,olen)) | ||
| 272 | goto err; | ||
| 273 | S1+=len; | ||
| 274 | for (i=0; i<olen; i++) | ||
| 275 | { | ||
| 276 | out1[i]^=out2[i]; | ||
| 277 | } | ||
| 278 | } | ||
| 193 | } | 279 | } |
| 194 | 280 | ret = 1; | |
| 195 | static void tls1_generate_key_block(SSL *s, unsigned char *km, | 281 | err: |
| 282 | return ret; | ||
| 283 | } | ||
| 284 | static int tls1_generate_key_block(SSL *s, unsigned char *km, | ||
| 196 | unsigned char *tmp, int num) | 285 | unsigned char *tmp, int num) |
| 197 | { | 286 | { |
| 198 | unsigned char *p; | 287 | int ret; |
| 199 | unsigned char buf[SSL3_RANDOM_SIZE*2+ | 288 | ret = tls1_PRF(s->s3->tmp.new_cipher->algorithm2, |
| 200 | TLS_MD_MAX_CONST_SIZE]; | 289 | TLS_MD_KEY_EXPANSION_CONST,TLS_MD_KEY_EXPANSION_CONST_SIZE, |
| 201 | p=buf; | 290 | s->s3->server_random,SSL3_RANDOM_SIZE, |
| 202 | 291 | s->s3->client_random,SSL3_RANDOM_SIZE, | |
| 203 | memcpy(p,TLS_MD_KEY_EXPANSION_CONST, | 292 | NULL,0,NULL,0, |
| 204 | TLS_MD_KEY_EXPANSION_CONST_SIZE); | ||
| 205 | p+=TLS_MD_KEY_EXPANSION_CONST_SIZE; | ||
| 206 | memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); | ||
| 207 | p+=SSL3_RANDOM_SIZE; | ||
| 208 | memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); | ||
| 209 | p+=SSL3_RANDOM_SIZE; | ||
| 210 | |||
| 211 | tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf), | ||
| 212 | s->session->master_key,s->session->master_key_length, | 293 | s->session->master_key,s->session->master_key_length, |
| 213 | km,tmp,num); | 294 | km,tmp,num); |
| 214 | #ifdef KSSL_DEBUG | 295 | #ifdef KSSL_DEBUG |
| @@ -222,14 +303,14 @@ static void tls1_generate_key_block(SSL *s, unsigned char *km, | |||
| 222 | } | 303 | } |
| 223 | printf("\n"); } | 304 | printf("\n"); } |
| 224 | #endif /* KSSL_DEBUG */ | 305 | #endif /* KSSL_DEBUG */ |
| 306 | return ret; | ||
| 225 | } | 307 | } |
| 226 | 308 | ||
| 227 | int tls1_change_cipher_state(SSL *s, int which) | 309 | int tls1_change_cipher_state(SSL *s, int which) |
| 228 | { | 310 | { |
| 229 | static const unsigned char empty[]=""; | 311 | static const unsigned char empty[]=""; |
| 230 | unsigned char *p,*key_block,*mac_secret; | 312 | unsigned char *p,*key_block,*mac_secret; |
| 231 | unsigned char *exp_label,buf[TLS_MD_MAX_CONST_SIZE+ | 313 | unsigned char *exp_label; |
| 232 | SSL3_RANDOM_SIZE*2]; | ||
| 233 | unsigned char tmp1[EVP_MAX_KEY_LENGTH]; | 314 | unsigned char tmp1[EVP_MAX_KEY_LENGTH]; |
| 234 | unsigned char tmp2[EVP_MAX_KEY_LENGTH]; | 315 | unsigned char tmp2[EVP_MAX_KEY_LENGTH]; |
| 235 | unsigned char iv1[EVP_MAX_IV_LENGTH*2]; | 316 | unsigned char iv1[EVP_MAX_IV_LENGTH*2]; |
| @@ -242,12 +323,17 @@ int tls1_change_cipher_state(SSL *s, int which) | |||
| 242 | const SSL_COMP *comp; | 323 | const SSL_COMP *comp; |
| 243 | #endif | 324 | #endif |
| 244 | const EVP_MD *m; | 325 | const EVP_MD *m; |
| 326 | int mac_type; | ||
| 327 | int *mac_secret_size; | ||
| 328 | EVP_MD_CTX *mac_ctx; | ||
| 329 | EVP_PKEY *mac_key; | ||
| 245 | int is_export,n,i,j,k,exp_label_len,cl; | 330 | int is_export,n,i,j,k,exp_label_len,cl; |
| 246 | int reuse_dd = 0; | 331 | int reuse_dd = 0; |
| 247 | 332 | ||
| 248 | is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher); | 333 | is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher); |
| 249 | c=s->s3->tmp.new_sym_enc; | 334 | c=s->s3->tmp.new_sym_enc; |
| 250 | m=s->s3->tmp.new_hash; | 335 | m=s->s3->tmp.new_hash; |
| 336 | mac_type = s->s3->tmp.new_mac_pkey_type; | ||
| 251 | #ifndef OPENSSL_NO_COMP | 337 | #ifndef OPENSSL_NO_COMP |
| 252 | comp=s->s3->tmp.new_compression; | 338 | comp=s->s3->tmp.new_compression; |
| 253 | #endif | 339 | #endif |
| @@ -255,21 +341,28 @@ int tls1_change_cipher_state(SSL *s, int which) | |||
| 255 | 341 | ||
| 256 | #ifdef KSSL_DEBUG | 342 | #ifdef KSSL_DEBUG |
| 257 | printf("tls1_change_cipher_state(which= %d) w/\n", which); | 343 | printf("tls1_change_cipher_state(which= %d) w/\n", which); |
| 258 | printf("\talg= %ld, comp= %p\n", s->s3->tmp.new_cipher->algorithms, | 344 | printf("\talg= %ld/%ld, comp= %p\n", |
| 259 | (void *)comp); | 345 | s->s3->tmp.new_cipher->algorithm_mkey, |
| 260 | printf("\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", (void *)c); | 346 | s->s3->tmp.new_cipher->algorithm_auth, |
| 347 | comp); | ||
| 348 | printf("\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", c); | ||
| 261 | printf("\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n", | 349 | printf("\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n", |
| 262 | c->nid,c->block_size,c->key_len,c->iv_len); | 350 | c->nid,c->block_size,c->key_len,c->iv_len); |
| 263 | printf("\tkey_block: len= %d, data= ", s->s3->tmp.key_block_length); | 351 | printf("\tkey_block: len= %d, data= ", s->s3->tmp.key_block_length); |
| 264 | { | 352 | { |
| 265 | int ki; | 353 | int i; |
| 266 | for (ki=0; ki<s->s3->tmp.key_block_length; ki++) | 354 | for (i=0; i<s->s3->tmp.key_block_length; i++) |
| 267 | printf("%02x", key_block[ki]); printf("\n"); | 355 | printf("%02x", key_block[i]); printf("\n"); |
| 268 | } | 356 | } |
| 269 | #endif /* KSSL_DEBUG */ | 357 | #endif /* KSSL_DEBUG */ |
| 270 | 358 | ||
| 271 | if (which & SSL3_CC_READ) | 359 | if (which & SSL3_CC_READ) |
| 272 | { | 360 | { |
| 361 | if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC) | ||
| 362 | s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM; | ||
| 363 | else | ||
| 364 | s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM; | ||
| 365 | |||
| 273 | if (s->enc_read_ctx != NULL) | 366 | if (s->enc_read_ctx != NULL) |
| 274 | reuse_dd = 1; | 367 | reuse_dd = 1; |
| 275 | else if ((s->enc_read_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL) | 368 | else if ((s->enc_read_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL) |
| @@ -278,7 +371,7 @@ int tls1_change_cipher_state(SSL *s, int which) | |||
| 278 | /* make sure it's intialized in case we exit later with an error */ | 371 | /* make sure it's intialized in case we exit later with an error */ |
| 279 | EVP_CIPHER_CTX_init(s->enc_read_ctx); | 372 | EVP_CIPHER_CTX_init(s->enc_read_ctx); |
| 280 | dd= s->enc_read_ctx; | 373 | dd= s->enc_read_ctx; |
| 281 | s->read_hash=m; | 374 | mac_ctx=ssl_replace_hash(&s->read_hash,NULL); |
| 282 | #ifndef OPENSSL_NO_COMP | 375 | #ifndef OPENSSL_NO_COMP |
| 283 | if (s->expand != NULL) | 376 | if (s->expand != NULL) |
| 284 | { | 377 | { |
| @@ -304,9 +397,14 @@ int tls1_change_cipher_state(SSL *s, int which) | |||
| 304 | if (s->version != DTLS1_VERSION) | 397 | if (s->version != DTLS1_VERSION) |
| 305 | memset(&(s->s3->read_sequence[0]),0,8); | 398 | memset(&(s->s3->read_sequence[0]),0,8); |
| 306 | mac_secret= &(s->s3->read_mac_secret[0]); | 399 | mac_secret= &(s->s3->read_mac_secret[0]); |
| 400 | mac_secret_size=&(s->s3->read_mac_secret_size); | ||
| 307 | } | 401 | } |
| 308 | else | 402 | else |
| 309 | { | 403 | { |
| 404 | if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC) | ||
| 405 | s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM; | ||
| 406 | else | ||
| 407 | s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM; | ||
| 310 | if (s->enc_write_ctx != NULL) | 408 | if (s->enc_write_ctx != NULL) |
| 311 | reuse_dd = 1; | 409 | reuse_dd = 1; |
| 312 | else if ((s->enc_write_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL) | 410 | else if ((s->enc_write_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL) |
| @@ -315,7 +413,7 @@ int tls1_change_cipher_state(SSL *s, int which) | |||
| 315 | /* make sure it's intialized in case we exit later with an error */ | 413 | /* make sure it's intialized in case we exit later with an error */ |
| 316 | EVP_CIPHER_CTX_init(s->enc_write_ctx); | 414 | EVP_CIPHER_CTX_init(s->enc_write_ctx); |
| 317 | dd= s->enc_write_ctx; | 415 | dd= s->enc_write_ctx; |
| 318 | s->write_hash=m; | 416 | mac_ctx = ssl_replace_hash(&s->write_hash,NULL); |
| 319 | #ifndef OPENSSL_NO_COMP | 417 | #ifndef OPENSSL_NO_COMP |
| 320 | if (s->compress != NULL) | 418 | if (s->compress != NULL) |
| 321 | { | 419 | { |
| @@ -336,13 +434,15 @@ int tls1_change_cipher_state(SSL *s, int which) | |||
| 336 | if (s->version != DTLS1_VERSION) | 434 | if (s->version != DTLS1_VERSION) |
| 337 | memset(&(s->s3->write_sequence[0]),0,8); | 435 | memset(&(s->s3->write_sequence[0]),0,8); |
| 338 | mac_secret= &(s->s3->write_mac_secret[0]); | 436 | mac_secret= &(s->s3->write_mac_secret[0]); |
| 437 | mac_secret_size = &(s->s3->write_mac_secret_size); | ||
| 339 | } | 438 | } |
| 340 | 439 | ||
| 341 | if (reuse_dd) | 440 | if (reuse_dd) |
| 342 | EVP_CIPHER_CTX_cleanup(dd); | 441 | EVP_CIPHER_CTX_cleanup(dd); |
| 343 | 442 | ||
| 344 | p=s->s3->tmp.key_block; | 443 | p=s->s3->tmp.key_block; |
| 345 | i=EVP_MD_size(m); | 444 | i=*mac_secret_size=s->s3->tmp.new_mac_secret_size; |
| 445 | |||
| 346 | cl=EVP_CIPHER_key_length(c); | 446 | cl=EVP_CIPHER_key_length(c); |
| 347 | j=is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ? | 447 | j=is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ? |
| 348 | cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl; | 448 | cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl; |
| @@ -378,6 +478,10 @@ int tls1_change_cipher_state(SSL *s, int which) | |||
| 378 | } | 478 | } |
| 379 | 479 | ||
| 380 | memcpy(mac_secret,ms,i); | 480 | memcpy(mac_secret,ms,i); |
| 481 | mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, | ||
| 482 | mac_secret,*mac_secret_size); | ||
| 483 | EVP_DigestSignInit(mac_ctx,NULL,m,NULL,mac_key); | ||
| 484 | EVP_PKEY_free(mac_key); | ||
| 381 | #ifdef TLS_DEBUG | 485 | #ifdef TLS_DEBUG |
| 382 | printf("which = %04X\nmac key=",which); | 486 | printf("which = %04X\nmac key=",which); |
| 383 | { int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); } | 487 | { int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); } |
| @@ -387,29 +491,24 @@ printf("which = %04X\nmac key=",which); | |||
| 387 | /* In here I set both the read and write key/iv to the | 491 | /* In here I set both the read and write key/iv to the |
| 388 | * same value since only the correct one will be used :-). | 492 | * same value since only the correct one will be used :-). |
| 389 | */ | 493 | */ |
| 390 | p=buf; | 494 | if (!tls1_PRF(s->s3->tmp.new_cipher->algorithm2, |
| 391 | memcpy(p,exp_label,exp_label_len); | 495 | exp_label,exp_label_len, |
| 392 | p+=exp_label_len; | 496 | s->s3->client_random,SSL3_RANDOM_SIZE, |
| 393 | memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); | 497 | s->s3->server_random,SSL3_RANDOM_SIZE, |
| 394 | p+=SSL3_RANDOM_SIZE; | 498 | NULL,0,NULL,0, |
| 395 | memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); | 499 | key,j,tmp1,tmp2,EVP_CIPHER_key_length(c))) |
| 396 | p+=SSL3_RANDOM_SIZE; | 500 | goto err2; |
| 397 | tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),key,j, | ||
| 398 | tmp1,tmp2,EVP_CIPHER_key_length(c)); | ||
| 399 | key=tmp1; | 501 | key=tmp1; |
| 400 | 502 | ||
| 401 | if (k > 0) | 503 | if (k > 0) |
| 402 | { | 504 | { |
| 403 | p=buf; | 505 | if (!tls1_PRF(s->s3->tmp.new_cipher->algorithm2, |
| 404 | memcpy(p,TLS_MD_IV_BLOCK_CONST, | 506 | TLS_MD_IV_BLOCK_CONST,TLS_MD_IV_BLOCK_CONST_SIZE, |
| 405 | TLS_MD_IV_BLOCK_CONST_SIZE); | 507 | s->s3->client_random,SSL3_RANDOM_SIZE, |
| 406 | p+=TLS_MD_IV_BLOCK_CONST_SIZE; | 508 | s->s3->server_random,SSL3_RANDOM_SIZE, |
| 407 | memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); | 509 | NULL,0,NULL,0, |
| 408 | p+=SSL3_RANDOM_SIZE; | 510 | empty,0,iv1,iv2,k*2)) |
| 409 | memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); | 511 | goto err2; |
| 410 | p+=SSL3_RANDOM_SIZE; | ||
| 411 | tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,p-buf,empty,0, | ||
| 412 | iv1,iv2,k*2); | ||
| 413 | if (client_write) | 512 | if (client_write) |
| 414 | iv=iv1; | 513 | iv=iv1; |
| 415 | else | 514 | else |
| @@ -420,13 +519,11 @@ printf("which = %04X\nmac key=",which); | |||
| 420 | s->session->key_arg_length=0; | 519 | s->session->key_arg_length=0; |
| 421 | #ifdef KSSL_DEBUG | 520 | #ifdef KSSL_DEBUG |
| 422 | { | 521 | { |
| 423 | int ki; | 522 | int i; |
| 424 | printf("EVP_CipherInit_ex(dd,c,key=,iv=,which)\n"); | 523 | printf("EVP_CipherInit_ex(dd,c,key=,iv=,which)\n"); |
| 425 | printf("\tkey= "); | 524 | printf("\tkey= "); for (i=0; i<c->key_len; i++) printf("%02x", key[i]); |
| 426 | for (ki=0; ki<c->key_len; ki++) printf("%02x", key[ki]); | ||
| 427 | printf("\n"); | 525 | printf("\n"); |
| 428 | printf("\t iv= "); | 526 | printf("\t iv= "); for (i=0; i<c->iv_len; i++) printf("%02x", iv[i]); |
| 429 | for (ki=0; ki<c->iv_len; ki++) printf("%02x", iv[ki]); | ||
| 430 | printf("\n"); | 527 | printf("\n"); |
| 431 | } | 528 | } |
| 432 | #endif /* KSSL_DEBUG */ | 529 | #endif /* KSSL_DEBUG */ |
| @@ -453,11 +550,13 @@ err2: | |||
| 453 | 550 | ||
| 454 | int tls1_setup_key_block(SSL *s) | 551 | int tls1_setup_key_block(SSL *s) |
| 455 | { | 552 | { |
| 456 | unsigned char *p1,*p2; | 553 | unsigned char *p1,*p2=NULL; |
| 457 | const EVP_CIPHER *c; | 554 | const EVP_CIPHER *c; |
| 458 | const EVP_MD *hash; | 555 | const EVP_MD *hash; |
| 459 | int num; | 556 | int num; |
| 460 | SSL_COMP *comp; | 557 | SSL_COMP *comp; |
| 558 | int mac_type= NID_undef,mac_secret_size=0; | ||
| 559 | int ret=0; | ||
| 461 | 560 | ||
| 462 | #ifdef KSSL_DEBUG | 561 | #ifdef KSSL_DEBUG |
| 463 | printf ("tls1_setup_key_block()\n"); | 562 | printf ("tls1_setup_key_block()\n"); |
| @@ -466,7 +565,7 @@ int tls1_setup_key_block(SSL *s) | |||
| 466 | if (s->s3->tmp.key_block_length != 0) | 565 | if (s->s3->tmp.key_block_length != 0) |
| 467 | return(1); | 566 | return(1); |
| 468 | 567 | ||
| 469 | if (!ssl_cipher_get_evp(s->session,&c,&hash,&comp)) | 568 | if (!ssl_cipher_get_evp(s->session,&c,&hash,&mac_type,&mac_secret_size,&comp)) |
| 470 | { | 569 | { |
| 471 | SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE); | 570 | SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE); |
| 472 | return(0); | 571 | return(0); |
| @@ -474,20 +573,27 @@ int tls1_setup_key_block(SSL *s) | |||
| 474 | 573 | ||
| 475 | s->s3->tmp.new_sym_enc=c; | 574 | s->s3->tmp.new_sym_enc=c; |
| 476 | s->s3->tmp.new_hash=hash; | 575 | s->s3->tmp.new_hash=hash; |
| 477 | 576 | s->s3->tmp.new_mac_pkey_type = mac_type; | |
| 478 | num=EVP_CIPHER_key_length(c)+EVP_MD_size(hash)+EVP_CIPHER_iv_length(c); | 577 | s->s3->tmp.new_mac_secret_size = mac_secret_size; |
| 578 | num=EVP_CIPHER_key_length(c)+mac_secret_size+EVP_CIPHER_iv_length(c); | ||
| 479 | num*=2; | 579 | num*=2; |
| 480 | 580 | ||
| 481 | ssl3_cleanup_key_block(s); | 581 | ssl3_cleanup_key_block(s); |
| 482 | 582 | ||
| 483 | if ((p1=(unsigned char *)OPENSSL_malloc(num)) == NULL) | 583 | if ((p1=(unsigned char *)OPENSSL_malloc(num)) == NULL) |
| 584 | { | ||
| 585 | SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,ERR_R_MALLOC_FAILURE); | ||
| 484 | goto err; | 586 | goto err; |
| 485 | if ((p2=(unsigned char *)OPENSSL_malloc(num)) == NULL) | 587 | } |
| 486 | goto err; | ||
| 487 | 588 | ||
| 488 | s->s3->tmp.key_block_length=num; | 589 | s->s3->tmp.key_block_length=num; |
| 489 | s->s3->tmp.key_block=p1; | 590 | s->s3->tmp.key_block=p1; |
| 490 | 591 | ||
| 592 | if ((p2=(unsigned char *)OPENSSL_malloc(num)) == NULL) | ||
| 593 | { | ||
| 594 | SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,ERR_R_MALLOC_FAILURE); | ||
| 595 | goto err; | ||
| 596 | } | ||
| 491 | 597 | ||
| 492 | #ifdef TLS_DEBUG | 598 | #ifdef TLS_DEBUG |
| 493 | printf("client random\n"); | 599 | printf("client random\n"); |
| @@ -497,9 +603,8 @@ printf("server random\n"); | |||
| 497 | printf("pre-master\n"); | 603 | printf("pre-master\n"); |
| 498 | { int z; for (z=0; z<s->session->master_key_length; z++) printf("%02X%c",s->session->master_key[z],((z+1)%16)?' ':'\n'); } | 604 | { int z; for (z=0; z<s->session->master_key_length; z++) printf("%02X%c",s->session->master_key[z],((z+1)%16)?' ':'\n'); } |
| 499 | #endif | 605 | #endif |
| 500 | tls1_generate_key_block(s,p1,p2,num); | 606 | if (!tls1_generate_key_block(s,p1,p2,num)) |
| 501 | OPENSSL_cleanse(p2,num); | 607 | goto err; |
| 502 | OPENSSL_free(p2); | ||
| 503 | #ifdef TLS_DEBUG | 608 | #ifdef TLS_DEBUG |
| 504 | printf("\nkey block\n"); | 609 | printf("\nkey block\n"); |
| 505 | { int z; for (z=0; z<num; z++) printf("%02X%c",p1[z],((z+1)%16)?' ':'\n'); } | 610 | { int z; for (z=0; z<num; z++) printf("%02X%c",p1[z],((z+1)%16)?' ':'\n'); } |
| @@ -514,20 +619,24 @@ printf("\nkey block\n"); | |||
| 514 | 619 | ||
| 515 | if (s->session->cipher != NULL) | 620 | if (s->session->cipher != NULL) |
| 516 | { | 621 | { |
| 517 | if ((s->session->cipher->algorithms & SSL_ENC_MASK) == SSL_eNULL) | 622 | if (s->session->cipher->algorithm_enc == SSL_eNULL) |
| 518 | s->s3->need_empty_fragments = 0; | 623 | s->s3->need_empty_fragments = 0; |
| 519 | 624 | ||
| 520 | #ifndef OPENSSL_NO_RC4 | 625 | #ifndef OPENSSL_NO_RC4 |
| 521 | if ((s->session->cipher->algorithms & SSL_ENC_MASK) == SSL_RC4) | 626 | if (s->session->cipher->algorithm_enc == SSL_RC4) |
| 522 | s->s3->need_empty_fragments = 0; | 627 | s->s3->need_empty_fragments = 0; |
| 523 | #endif | 628 | #endif |
| 524 | } | 629 | } |
| 525 | } | 630 | } |
| 526 | 631 | ||
| 527 | return(1); | 632 | ret = 1; |
| 528 | err: | 633 | err: |
| 529 | SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,ERR_R_MALLOC_FAILURE); | 634 | if (p2) |
| 530 | return(0); | 635 | { |
| 636 | OPENSSL_cleanse(p2,num); | ||
| 637 | OPENSSL_free(p2); | ||
| 638 | } | ||
| 639 | return(ret); | ||
| 531 | } | 640 | } |
| 532 | 641 | ||
| 533 | int tls1_enc(SSL *s, int send) | 642 | int tls1_enc(SSL *s, int send) |
| @@ -540,8 +649,11 @@ int tls1_enc(SSL *s, int send) | |||
| 540 | 649 | ||
| 541 | if (send) | 650 | if (send) |
| 542 | { | 651 | { |
| 543 | if (s->write_hash != NULL) | 652 | if (EVP_MD_CTX_md(s->write_hash)) |
| 544 | n=EVP_MD_size(s->write_hash); | 653 | { |
| 654 | n=EVP_MD_CTX_size(s->write_hash); | ||
| 655 | OPENSSL_assert(n >= 0); | ||
| 656 | } | ||
| 545 | ds=s->enc_write_ctx; | 657 | ds=s->enc_write_ctx; |
| 546 | rec= &(s->s3->wrec); | 658 | rec= &(s->s3->wrec); |
| 547 | if (s->enc_write_ctx == NULL) | 659 | if (s->enc_write_ctx == NULL) |
| @@ -551,8 +663,11 @@ int tls1_enc(SSL *s, int send) | |||
| 551 | } | 663 | } |
| 552 | else | 664 | else |
| 553 | { | 665 | { |
| 554 | if (s->read_hash != NULL) | 666 | if (EVP_MD_CTX_md(s->read_hash)) |
| 555 | n=EVP_MD_size(s->read_hash); | 667 | { |
| 668 | n=EVP_MD_CTX_size(s->read_hash); | ||
| 669 | OPENSSL_assert(n >= 0); | ||
| 670 | } | ||
| 556 | ds=s->enc_read_ctx; | 671 | ds=s->enc_read_ctx; |
| 557 | rec= &(s->s3->rrec); | 672 | rec= &(s->s3->rrec); |
| 558 | if (s->enc_read_ctx == NULL) | 673 | if (s->enc_read_ctx == NULL) |
| @@ -599,11 +714,10 @@ int tls1_enc(SSL *s, int send) | |||
| 599 | { | 714 | { |
| 600 | unsigned long ui; | 715 | unsigned long ui; |
| 601 | printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n", | 716 | printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n", |
| 602 | (void *)ds,rec->data,rec->input,l); | 717 | ds,rec->data,rec->input,l); |
| 603 | printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%ld %ld], %d iv_len\n", | 718 | printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n", |
| 604 | ds->buf_len, ds->cipher->key_len, | 719 | ds->buf_len, ds->cipher->key_len, |
| 605 | (unsigned long)DES_KEY_SZ, | 720 | DES_KEY_SZ, DES_SCHEDULE_SZ, |
| 606 | (unsigned long)DES_SCHEDULE_SZ, | ||
| 607 | ds->cipher->iv_len); | 721 | ds->cipher->iv_len); |
| 608 | printf("\t\tIV: "); | 722 | printf("\t\tIV: "); |
| 609 | for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]); | 723 | for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]); |
| @@ -628,10 +742,10 @@ int tls1_enc(SSL *s, int send) | |||
| 628 | 742 | ||
| 629 | #ifdef KSSL_DEBUG | 743 | #ifdef KSSL_DEBUG |
| 630 | { | 744 | { |
| 631 | unsigned long ki; | 745 | unsigned long i; |
| 632 | printf("\trec->data="); | 746 | printf("\trec->data="); |
| 633 | for (ki=0; ki<l; i++) | 747 | for (i=0; i<l; i++) |
| 634 | printf(" %02x", rec->data[ki]); printf("\n"); | 748 | printf(" %02x", rec->data[i]); printf("\n"); |
| 635 | } | 749 | } |
| 636 | #endif /* KSSL_DEBUG */ | 750 | #endif /* KSSL_DEBUG */ |
| 637 | 751 | ||
| @@ -679,56 +793,101 @@ int tls1_enc(SSL *s, int send) | |||
| 679 | } | 793 | } |
| 680 | return(1); | 794 | return(1); |
| 681 | } | 795 | } |
| 682 | 796 | int tls1_cert_verify_mac(SSL *s, int md_nid, unsigned char *out) | |
| 683 | int tls1_cert_verify_mac(SSL *s, EVP_MD_CTX *in_ctx, unsigned char *out) | ||
| 684 | { | 797 | { |
| 685 | unsigned int ret; | 798 | unsigned int ret; |
| 686 | EVP_MD_CTX ctx; | 799 | EVP_MD_CTX ctx, *d=NULL; |
| 800 | int i; | ||
| 801 | |||
| 802 | if (s->s3->handshake_buffer) | ||
| 803 | if (!ssl3_digest_cached_records(s)) | ||
| 804 | return 0; | ||
| 805 | |||
| 806 | for (i=0;i<SSL_MAX_DIGEST;i++) | ||
| 807 | { | ||
| 808 | if (s->s3->handshake_dgst[i]&&EVP_MD_CTX_type(s->s3->handshake_dgst[i])==md_nid) | ||
| 809 | { | ||
| 810 | d=s->s3->handshake_dgst[i]; | ||
| 811 | break; | ||
| 812 | } | ||
| 813 | } | ||
| 814 | if (!d) { | ||
| 815 | SSLerr(SSL_F_TLS1_CERT_VERIFY_MAC,SSL_R_NO_REQUIRED_DIGEST); | ||
| 816 | return 0; | ||
| 817 | } | ||
| 687 | 818 | ||
| 688 | EVP_MD_CTX_init(&ctx); | 819 | EVP_MD_CTX_init(&ctx); |
| 689 | EVP_MD_CTX_copy_ex(&ctx,in_ctx); | 820 | EVP_MD_CTX_copy_ex(&ctx,d); |
| 690 | EVP_DigestFinal_ex(&ctx,out,&ret); | 821 | EVP_DigestFinal_ex(&ctx,out,&ret); |
| 691 | EVP_MD_CTX_cleanup(&ctx); | 822 | EVP_MD_CTX_cleanup(&ctx); |
| 692 | return((int)ret); | 823 | return((int)ret); |
| 693 | } | 824 | } |
| 694 | 825 | ||
| 695 | int tls1_final_finish_mac(SSL *s, EVP_MD_CTX *in1_ctx, EVP_MD_CTX *in2_ctx, | 826 | int tls1_final_finish_mac(SSL *s, |
| 696 | const char *str, int slen, unsigned char *out) | 827 | const char *str, int slen, unsigned char *out) |
| 697 | { | 828 | { |
| 698 | unsigned int i; | 829 | unsigned int i; |
| 699 | EVP_MD_CTX ctx; | 830 | EVP_MD_CTX ctx; |
| 700 | unsigned char buf[TLS_MD_MAX_CONST_SIZE+MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; | 831 | unsigned char buf[2*EVP_MAX_MD_SIZE]; |
| 701 | unsigned char *q,buf2[12]; | 832 | unsigned char *q,buf2[12]; |
| 833 | int idx; | ||
| 834 | long mask; | ||
| 835 | int err=0; | ||
| 836 | const EVP_MD *md; | ||
| 702 | 837 | ||
| 703 | q=buf; | 838 | q=buf; |
| 704 | memcpy(q,str,slen); | 839 | |
| 705 | q+=slen; | 840 | if (s->s3->handshake_buffer) |
| 841 | if (!ssl3_digest_cached_records(s)) | ||
| 842 | return 0; | ||
| 706 | 843 | ||
| 707 | EVP_MD_CTX_init(&ctx); | 844 | EVP_MD_CTX_init(&ctx); |
| 708 | EVP_MD_CTX_copy_ex(&ctx,in1_ctx); | 845 | |
| 709 | EVP_DigestFinal_ex(&ctx,q,&i); | 846 | for (idx=0;ssl_get_handshake_digest(idx,&mask,&md);idx++) |
| 710 | q+=i; | 847 | { |
| 711 | EVP_MD_CTX_copy_ex(&ctx,in2_ctx); | 848 | if (mask & s->s3->tmp.new_cipher->algorithm2) |
| 712 | EVP_DigestFinal_ex(&ctx,q,&i); | 849 | { |
| 713 | q+=i; | 850 | int hashsize = EVP_MD_size(md); |
| 714 | 851 | if (hashsize < 0 || hashsize > (int)(sizeof buf - (size_t)(q-buf))) | |
| 715 | tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(q-buf), | 852 | { |
| 716 | s->session->master_key,s->session->master_key_length, | 853 | /* internal error: 'buf' is too small for this cipersuite! */ |
| 717 | out,buf2,sizeof buf2); | 854 | err = 1; |
| 855 | } | ||
| 856 | else | ||
| 857 | { | ||
| 858 | EVP_MD_CTX_copy_ex(&ctx,s->s3->handshake_dgst[idx]); | ||
| 859 | EVP_DigestFinal_ex(&ctx,q,&i); | ||
| 860 | if (i != (unsigned int)hashsize) /* can't really happen */ | ||
| 861 | err = 1; | ||
| 862 | q+=i; | ||
| 863 | } | ||
| 864 | } | ||
| 865 | } | ||
| 866 | |||
| 867 | if (!tls1_PRF(s->s3->tmp.new_cipher->algorithm2, | ||
| 868 | str,slen, buf,(int)(q-buf), NULL,0, NULL,0, NULL,0, | ||
| 869 | s->session->master_key,s->session->master_key_length, | ||
| 870 | out,buf2,sizeof buf2)) | ||
| 871 | err = 1; | ||
| 718 | EVP_MD_CTX_cleanup(&ctx); | 872 | EVP_MD_CTX_cleanup(&ctx); |
| 719 | 873 | ||
| 720 | return sizeof buf2; | 874 | if (err) |
| 875 | return 0; | ||
| 876 | else | ||
| 877 | return sizeof buf2; | ||
| 721 | } | 878 | } |
| 722 | 879 | ||
| 723 | int tls1_mac(SSL *ssl, unsigned char *md, int send) | 880 | int tls1_mac(SSL *ssl, unsigned char *md, int send) |
| 724 | { | 881 | { |
| 725 | SSL3_RECORD *rec; | 882 | SSL3_RECORD *rec; |
| 726 | unsigned char *mac_sec,*seq; | 883 | unsigned char *mac_sec,*seq; |
| 727 | const EVP_MD *hash; | 884 | EVP_MD_CTX *hash; |
| 728 | unsigned int md_size; | 885 | size_t md_size; |
| 729 | int i; | 886 | int i; |
| 730 | HMAC_CTX hmac; | 887 | EVP_MD_CTX hmac, *mac_ctx; |
| 731 | unsigned char buf[5]; | 888 | unsigned char buf[5]; |
| 889 | int stream_mac = (send?(ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM):(ssl->mac_flags&SSL_MAC_FLAG_READ_MAC_STREAM)); | ||
| 890 | int t; | ||
| 732 | 891 | ||
| 733 | if (send) | 892 | if (send) |
| 734 | { | 893 | { |
| @@ -745,43 +904,45 @@ int tls1_mac(SSL *ssl, unsigned char *md, int send) | |||
| 745 | hash=ssl->read_hash; | 904 | hash=ssl->read_hash; |
| 746 | } | 905 | } |
| 747 | 906 | ||
| 748 | md_size=EVP_MD_size(hash); | 907 | t=EVP_MD_CTX_size(hash); |
| 908 | OPENSSL_assert(t >= 0); | ||
| 909 | md_size=t; | ||
| 749 | 910 | ||
| 750 | buf[0]=rec->type; | 911 | buf[0]=rec->type; |
| 751 | if (ssl->version == DTLS1_VERSION && ssl->client_version == DTLS1_BAD_VER) | 912 | buf[1]=(unsigned char)(ssl->version>>8); |
| 752 | { | 913 | buf[2]=(unsigned char)(ssl->version); |
| 753 | buf[1]=TLS1_VERSION_MAJOR; | ||
| 754 | buf[2]=TLS1_VERSION_MINOR; | ||
| 755 | } | ||
| 756 | else { | ||
| 757 | buf[1]=(unsigned char)(ssl->version>>8); | ||
| 758 | buf[2]=(unsigned char)(ssl->version); | ||
| 759 | } | ||
| 760 | |||
| 761 | buf[3]=rec->length>>8; | 914 | buf[3]=rec->length>>8; |
| 762 | buf[4]=rec->length&0xff; | 915 | buf[4]=rec->length&0xff; |
| 763 | 916 | ||
| 764 | /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */ | 917 | /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */ |
| 765 | HMAC_CTX_init(&hmac); | 918 | if (stream_mac) |
| 766 | HMAC_Init_ex(&hmac,mac_sec,EVP_MD_size(hash),hash,NULL); | 919 | { |
| 920 | mac_ctx = hash; | ||
| 921 | } | ||
| 922 | else | ||
| 923 | { | ||
| 924 | EVP_MD_CTX_copy(&hmac,hash); | ||
| 925 | mac_ctx = &hmac; | ||
| 926 | } | ||
| 767 | 927 | ||
| 768 | if (ssl->version == DTLS1_VERSION && ssl->client_version != DTLS1_BAD_VER) | 928 | if (ssl->version == DTLS1_VERSION || ssl->version == DTLS1_BAD_VER) |
| 769 | { | 929 | { |
| 770 | unsigned char dtlsseq[8],*p=dtlsseq; | 930 | unsigned char dtlsseq[8],*p=dtlsseq; |
| 771 | 931 | ||
| 772 | s2n(send?ssl->d1->w_epoch:ssl->d1->r_epoch, p); | 932 | s2n(send?ssl->d1->w_epoch:ssl->d1->r_epoch, p); |
| 773 | memcpy (p,&seq[2],6); | 933 | memcpy (p,&seq[2],6); |
| 774 | 934 | ||
| 775 | HMAC_Update(&hmac,dtlsseq,8); | 935 | EVP_DigestSignUpdate(mac_ctx,dtlsseq,8); |
| 776 | } | 936 | } |
| 777 | else | 937 | else |
| 778 | HMAC_Update(&hmac,seq,8); | 938 | EVP_DigestSignUpdate(mac_ctx,seq,8); |
| 779 | |||
| 780 | HMAC_Update(&hmac,buf,5); | ||
| 781 | HMAC_Update(&hmac,rec->input,rec->length); | ||
| 782 | HMAC_Final(&hmac,md,&md_size); | ||
| 783 | HMAC_CTX_cleanup(&hmac); | ||
| 784 | 939 | ||
| 940 | EVP_DigestSignUpdate(mac_ctx,buf,5); | ||
| 941 | EVP_DigestSignUpdate(mac_ctx,rec->input,rec->length); | ||
| 942 | t=EVP_DigestSignFinal(mac_ctx,md,&md_size); | ||
| 943 | OPENSSL_assert(t > 0); | ||
| 944 | |||
| 945 | if (!stream_mac) EVP_MD_CTX_cleanup(&hmac); | ||
| 785 | #ifdef TLS_DEBUG | 946 | #ifdef TLS_DEBUG |
| 786 | printf("sec="); | 947 | printf("sec="); |
| 787 | {unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",mac_sec[z]); printf("\n"); } | 948 | {unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",mac_sec[z]); printf("\n"); } |
| @@ -793,7 +954,7 @@ printf("rec="); | |||
| 793 | {unsigned int z; for (z=0; z<rec->length; z++) printf("%02X ",buf[z]); printf("\n"); } | 954 | {unsigned int z; for (z=0; z<rec->length; z++) printf("%02X ",buf[z]); printf("\n"); } |
| 794 | #endif | 955 | #endif |
| 795 | 956 | ||
| 796 | if ( SSL_version(ssl) != DTLS1_VERSION) | 957 | if (ssl->version != DTLS1_VERSION && ssl->version != DTLS1_BAD_VER) |
| 797 | { | 958 | { |
| 798 | for (i=7; i>=0; i--) | 959 | for (i=7; i>=0; i--) |
| 799 | { | 960 | { |
| @@ -811,23 +972,35 @@ printf("rec="); | |||
| 811 | int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p, | 972 | int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p, |
| 812 | int len) | 973 | int len) |
| 813 | { | 974 | { |
| 814 | unsigned char buf[SSL3_RANDOM_SIZE*2+TLS_MD_MASTER_SECRET_CONST_SIZE]; | ||
| 815 | unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH]; | 975 | unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH]; |
| 976 | const void *co = NULL, *so = NULL; | ||
| 977 | int col = 0, sol = 0; | ||
| 816 | 978 | ||
| 817 | #ifdef KSSL_DEBUG | 979 | #ifdef KSSL_DEBUG |
| 818 | printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", (void *)s,out, p,len); | 980 | printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", s,out, p,len); |
| 819 | #endif /* KSSL_DEBUG */ | 981 | #endif /* KSSL_DEBUG */ |
| 820 | 982 | ||
| 821 | /* Setup the stuff to munge */ | 983 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 822 | memcpy(buf,TLS_MD_MASTER_SECRET_CONST, | 984 | if (s->s3->client_opaque_prf_input != NULL && s->s3->server_opaque_prf_input != NULL && |
| 823 | TLS_MD_MASTER_SECRET_CONST_SIZE); | 985 | s->s3->client_opaque_prf_input_len > 0 && |
| 824 | memcpy(&(buf[TLS_MD_MASTER_SECRET_CONST_SIZE]), | 986 | s->s3->client_opaque_prf_input_len == s->s3->server_opaque_prf_input_len) |
| 825 | s->s3->client_random,SSL3_RANDOM_SIZE); | 987 | { |
| 826 | memcpy(&(buf[SSL3_RANDOM_SIZE+TLS_MD_MASTER_SECRET_CONST_SIZE]), | 988 | co = s->s3->client_opaque_prf_input; |
| 827 | s->s3->server_random,SSL3_RANDOM_SIZE); | 989 | col = s->s3->server_opaque_prf_input_len; |
| 828 | tls1_PRF(s->ctx->md5,s->ctx->sha1, | 990 | so = s->s3->server_opaque_prf_input; |
| 829 | buf,TLS_MD_MASTER_SECRET_CONST_SIZE+SSL3_RANDOM_SIZE*2,p,len, | 991 | sol = s->s3->client_opaque_prf_input_len; /* must be same as col (see draft-rescorla-tls-opaque-prf-input-00.txt, section 3.1) */ |
| 992 | } | ||
| 993 | #endif | ||
| 994 | |||
| 995 | tls1_PRF(s->s3->tmp.new_cipher->algorithm2, | ||
| 996 | TLS_MD_MASTER_SECRET_CONST,TLS_MD_MASTER_SECRET_CONST_SIZE, | ||
| 997 | s->s3->client_random,SSL3_RANDOM_SIZE, | ||
| 998 | co, col, | ||
| 999 | s->s3->server_random,SSL3_RANDOM_SIZE, | ||
| 1000 | so, sol, | ||
| 1001 | p,len, | ||
| 830 | s->session->master_key,buff,sizeof buff); | 1002 | s->session->master_key,buff,sizeof buff); |
| 1003 | |||
| 831 | #ifdef KSSL_DEBUG | 1004 | #ifdef KSSL_DEBUG |
| 832 | printf ("tls1_generate_master_secret() complete\n"); | 1005 | printf ("tls1_generate_master_secret() complete\n"); |
| 833 | #endif /* KSSL_DEBUG */ | 1006 | #endif /* KSSL_DEBUG */ |
| @@ -862,7 +1035,13 @@ int tls1_alert_code(int code) | |||
| 862 | case SSL_AD_INTERNAL_ERROR: return(TLS1_AD_INTERNAL_ERROR); | 1035 | case SSL_AD_INTERNAL_ERROR: return(TLS1_AD_INTERNAL_ERROR); |
| 863 | case SSL_AD_USER_CANCELLED: return(TLS1_AD_USER_CANCELLED); | 1036 | case SSL_AD_USER_CANCELLED: return(TLS1_AD_USER_CANCELLED); |
| 864 | case SSL_AD_NO_RENEGOTIATION: return(TLS1_AD_NO_RENEGOTIATION); | 1037 | case SSL_AD_NO_RENEGOTIATION: return(TLS1_AD_NO_RENEGOTIATION); |
| 865 | #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE | 1038 | case SSL_AD_UNSUPPORTED_EXTENSION: return(TLS1_AD_UNSUPPORTED_EXTENSION); |
| 1039 | case SSL_AD_CERTIFICATE_UNOBTAINABLE: return(TLS1_AD_CERTIFICATE_UNOBTAINABLE); | ||
| 1040 | case SSL_AD_UNRECOGNIZED_NAME: return(TLS1_AD_UNRECOGNIZED_NAME); | ||
| 1041 | case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: return(TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE); | ||
| 1042 | case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: return(TLS1_AD_BAD_CERTIFICATE_HASH_VALUE); | ||
| 1043 | case SSL_AD_UNKNOWN_PSK_IDENTITY:return(TLS1_AD_UNKNOWN_PSK_IDENTITY); | ||
| 1044 | #if 0 /* not appropriate for TLS, not used for DTLS */ | ||
| 866 | case DTLS1_AD_MISSING_HANDSHAKE_MESSAGE: return | 1045 | case DTLS1_AD_MISSING_HANDSHAKE_MESSAGE: return |
| 867 | (DTLS1_AD_MISSING_HANDSHAKE_MESSAGE); | 1046 | (DTLS1_AD_MISSING_HANDSHAKE_MESSAGE); |
| 868 | #endif | 1047 | #endif |
