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