1 | /*
|
---|
2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | * Copyright 2005 Nokia. All rights reserved.
|
---|
4 | *
|
---|
5 | * Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
6 | * this file except in compliance with the License. You can obtain a copy
|
---|
7 | * in the file LICENSE in the source distribution or at
|
---|
8 | * https://www.openssl.org/source/license.html
|
---|
9 | */
|
---|
10 |
|
---|
11 | #include <stdio.h>
|
---|
12 | #include "ssl_local.h"
|
---|
13 | #include <openssl/evp.h>
|
---|
14 | #include <openssl/md5.h>
|
---|
15 | #include <openssl/core_names.h>
|
---|
16 | #include "internal/cryptlib.h"
|
---|
17 |
|
---|
18 | static int ssl3_generate_key_block(SSL *s, unsigned char *km, int num)
|
---|
19 | {
|
---|
20 | const EVP_MD *md5 = NULL, *sha1 = NULL;
|
---|
21 | EVP_MD_CTX *m5;
|
---|
22 | EVP_MD_CTX *s1;
|
---|
23 | unsigned char buf[16], smd[SHA_DIGEST_LENGTH];
|
---|
24 | unsigned char c = 'A';
|
---|
25 | unsigned int i, k;
|
---|
26 | int ret = 0;
|
---|
27 |
|
---|
28 | #ifdef CHARSET_EBCDIC
|
---|
29 | c = os_toascii[c]; /* 'A' in ASCII */
|
---|
30 | #endif
|
---|
31 | k = 0;
|
---|
32 | md5 = ssl_evp_md_fetch(s->ctx->libctx, NID_md5, s->ctx->propq);
|
---|
33 | sha1 = ssl_evp_md_fetch(s->ctx->libctx, NID_sha1, s->ctx->propq);
|
---|
34 | m5 = EVP_MD_CTX_new();
|
---|
35 | s1 = EVP_MD_CTX_new();
|
---|
36 | if (md5 == NULL || sha1 == NULL || m5 == NULL || s1 == NULL) {
|
---|
37 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
38 | goto err;
|
---|
39 | }
|
---|
40 | for (i = 0; (int)i < num; i += MD5_DIGEST_LENGTH) {
|
---|
41 | k++;
|
---|
42 | if (k > sizeof(buf)) {
|
---|
43 | /* bug: 'buf' is too small for this ciphersuite */
|
---|
44 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
45 | goto err;
|
---|
46 | }
|
---|
47 |
|
---|
48 | memset(buf, c, k);
|
---|
49 | c++;
|
---|
50 | if (!EVP_DigestInit_ex(s1, sha1, NULL)
|
---|
51 | || !EVP_DigestUpdate(s1, buf, k)
|
---|
52 | || !EVP_DigestUpdate(s1, s->session->master_key,
|
---|
53 | s->session->master_key_length)
|
---|
54 | || !EVP_DigestUpdate(s1, s->s3.server_random, SSL3_RANDOM_SIZE)
|
---|
55 | || !EVP_DigestUpdate(s1, s->s3.client_random, SSL3_RANDOM_SIZE)
|
---|
56 | || !EVP_DigestFinal_ex(s1, smd, NULL)
|
---|
57 | || !EVP_DigestInit_ex(m5, md5, NULL)
|
---|
58 | || !EVP_DigestUpdate(m5, s->session->master_key,
|
---|
59 | s->session->master_key_length)
|
---|
60 | || !EVP_DigestUpdate(m5, smd, SHA_DIGEST_LENGTH)) {
|
---|
61 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
62 | goto err;
|
---|
63 | }
|
---|
64 | if ((int)(i + MD5_DIGEST_LENGTH) > num) {
|
---|
65 | if (!EVP_DigestFinal_ex(m5, smd, NULL)) {
|
---|
66 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
67 | goto err;
|
---|
68 | }
|
---|
69 | memcpy(km, smd, (num - i));
|
---|
70 | } else {
|
---|
71 | if (!EVP_DigestFinal_ex(m5, km, NULL)) {
|
---|
72 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
73 | goto err;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | km += MD5_DIGEST_LENGTH;
|
---|
78 | }
|
---|
79 | OPENSSL_cleanse(smd, sizeof(smd));
|
---|
80 | ret = 1;
|
---|
81 | err:
|
---|
82 | EVP_MD_CTX_free(m5);
|
---|
83 | EVP_MD_CTX_free(s1);
|
---|
84 | ssl_evp_md_free(md5);
|
---|
85 | ssl_evp_md_free(sha1);
|
---|
86 | return ret;
|
---|
87 | }
|
---|
88 |
|
---|
89 | int ssl3_change_cipher_state(SSL *s, int which)
|
---|
90 | {
|
---|
91 | unsigned char *p, *mac_secret;
|
---|
92 | unsigned char *ms, *key, *iv;
|
---|
93 | EVP_CIPHER_CTX *dd;
|
---|
94 | const EVP_CIPHER *c;
|
---|
95 | #ifndef OPENSSL_NO_COMP
|
---|
96 | COMP_METHOD *comp;
|
---|
97 | #endif
|
---|
98 | const EVP_MD *m;
|
---|
99 | int mdi;
|
---|
100 | size_t n, i, j, k, cl;
|
---|
101 | int reuse_dd = 0;
|
---|
102 |
|
---|
103 | c = s->s3.tmp.new_sym_enc;
|
---|
104 | m = s->s3.tmp.new_hash;
|
---|
105 | /* m == NULL will lead to a crash later */
|
---|
106 | if (!ossl_assert(m != NULL)) {
|
---|
107 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
108 | goto err;
|
---|
109 | }
|
---|
110 | #ifndef OPENSSL_NO_COMP
|
---|
111 | if (s->s3.tmp.new_compression == NULL)
|
---|
112 | comp = NULL;
|
---|
113 | else
|
---|
114 | comp = s->s3.tmp.new_compression->method;
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | if (which & SSL3_CC_READ) {
|
---|
118 | if (s->enc_read_ctx != NULL) {
|
---|
119 | reuse_dd = 1;
|
---|
120 | } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) {
|
---|
121 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
122 | goto err;
|
---|
123 | } else {
|
---|
124 | /*
|
---|
125 | * make sure it's initialised in case we exit later with an error
|
---|
126 | */
|
---|
127 | EVP_CIPHER_CTX_reset(s->enc_read_ctx);
|
---|
128 | }
|
---|
129 | dd = s->enc_read_ctx;
|
---|
130 |
|
---|
131 | if (ssl_replace_hash(&s->read_hash, m) == NULL) {
|
---|
132 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
133 | goto err;
|
---|
134 | }
|
---|
135 | #ifndef OPENSSL_NO_COMP
|
---|
136 | /* COMPRESS */
|
---|
137 | COMP_CTX_free(s->expand);
|
---|
138 | s->expand = NULL;
|
---|
139 | if (comp != NULL) {
|
---|
140 | s->expand = COMP_CTX_new(comp);
|
---|
141 | if (s->expand == NULL) {
|
---|
142 | SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
---|
143 | SSL_R_COMPRESSION_LIBRARY_ERROR);
|
---|
144 | goto err;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | #endif
|
---|
148 | RECORD_LAYER_reset_read_sequence(&s->rlayer);
|
---|
149 | mac_secret = &(s->s3.read_mac_secret[0]);
|
---|
150 | } else {
|
---|
151 | s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
|
---|
152 | if (s->enc_write_ctx != NULL) {
|
---|
153 | reuse_dd = 1;
|
---|
154 | } else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) {
|
---|
155 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
156 | goto err;
|
---|
157 | } else {
|
---|
158 | /*
|
---|
159 | * make sure it's initialised in case we exit later with an error
|
---|
160 | */
|
---|
161 | EVP_CIPHER_CTX_reset(s->enc_write_ctx);
|
---|
162 | }
|
---|
163 | dd = s->enc_write_ctx;
|
---|
164 | if (ssl_replace_hash(&s->write_hash, m) == NULL) {
|
---|
165 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
166 | goto err;
|
---|
167 | }
|
---|
168 | #ifndef OPENSSL_NO_COMP
|
---|
169 | /* COMPRESS */
|
---|
170 | COMP_CTX_free(s->compress);
|
---|
171 | s->compress = NULL;
|
---|
172 | if (comp != NULL) {
|
---|
173 | s->compress = COMP_CTX_new(comp);
|
---|
174 | if (s->compress == NULL) {
|
---|
175 | SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
---|
176 | SSL_R_COMPRESSION_LIBRARY_ERROR);
|
---|
177 | goto err;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | #endif
|
---|
181 | RECORD_LAYER_reset_write_sequence(&s->rlayer);
|
---|
182 | mac_secret = &(s->s3.write_mac_secret[0]);
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (reuse_dd)
|
---|
186 | EVP_CIPHER_CTX_reset(dd);
|
---|
187 |
|
---|
188 | p = s->s3.tmp.key_block;
|
---|
189 | mdi = EVP_MD_get_size(m);
|
---|
190 | if (mdi < 0) {
|
---|
191 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
192 | goto err;
|
---|
193 | }
|
---|
194 | i = mdi;
|
---|
195 | cl = EVP_CIPHER_get_key_length(c);
|
---|
196 | j = cl;
|
---|
197 | k = EVP_CIPHER_get_iv_length(c);
|
---|
198 | if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
|
---|
199 | (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
|
---|
200 | ms = &(p[0]);
|
---|
201 | n = i + i;
|
---|
202 | key = &(p[n]);
|
---|
203 | n += j + j;
|
---|
204 | iv = &(p[n]);
|
---|
205 | n += k + k;
|
---|
206 | } else {
|
---|
207 | n = i;
|
---|
208 | ms = &(p[n]);
|
---|
209 | n += i + j;
|
---|
210 | key = &(p[n]);
|
---|
211 | n += j + k;
|
---|
212 | iv = &(p[n]);
|
---|
213 | n += k;
|
---|
214 | }
|
---|
215 |
|
---|
216 | if (n > s->s3.tmp.key_block_length) {
|
---|
217 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
218 | goto err;
|
---|
219 | }
|
---|
220 |
|
---|
221 | memcpy(mac_secret, ms, i);
|
---|
222 |
|
---|
223 | if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
|
---|
224 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
225 | goto err;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * The cipher we actually ended up using in the EVP_CIPHER_CTX may be
|
---|
230 | * different to that in c if we have an ENGINE in use
|
---|
231 | */
|
---|
232 | if (EVP_CIPHER_get0_provider(EVP_CIPHER_CTX_get0_cipher(dd)) != NULL
|
---|
233 | && !tls_provider_set_tls_params(s, dd, c, m)) {
|
---|
234 | /* SSLfatal already called */
|
---|
235 | goto err;
|
---|
236 | }
|
---|
237 |
|
---|
238 | s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
|
---|
239 | return 1;
|
---|
240 | err:
|
---|
241 | return 0;
|
---|
242 | }
|
---|
243 |
|
---|
244 | int ssl3_setup_key_block(SSL *s)
|
---|
245 | {
|
---|
246 | unsigned char *p;
|
---|
247 | const EVP_CIPHER *c;
|
---|
248 | const EVP_MD *hash;
|
---|
249 | int num;
|
---|
250 | int ret = 0;
|
---|
251 | SSL_COMP *comp;
|
---|
252 |
|
---|
253 | if (s->s3.tmp.key_block_length != 0)
|
---|
254 | return 1;
|
---|
255 |
|
---|
256 | if (!ssl_cipher_get_evp(s->ctx, s->session, &c, &hash, NULL, NULL, &comp,
|
---|
257 | 0)) {
|
---|
258 | /* Error is already recorded */
|
---|
259 | SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
|
---|
260 | return 0;
|
---|
261 | }
|
---|
262 |
|
---|
263 | ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
|
---|
264 | s->s3.tmp.new_sym_enc = c;
|
---|
265 | ssl_evp_md_free(s->s3.tmp.new_hash);
|
---|
266 | s->s3.tmp.new_hash = hash;
|
---|
267 | #ifdef OPENSSL_NO_COMP
|
---|
268 | s->s3.tmp.new_compression = NULL;
|
---|
269 | #else
|
---|
270 | s->s3.tmp.new_compression = comp;
|
---|
271 | #endif
|
---|
272 |
|
---|
273 | num = EVP_MD_get_size(hash);
|
---|
274 | if (num < 0)
|
---|
275 | return 0;
|
---|
276 |
|
---|
277 | num = EVP_CIPHER_get_key_length(c) + num + EVP_CIPHER_get_iv_length(c);
|
---|
278 | num *= 2;
|
---|
279 |
|
---|
280 | ssl3_cleanup_key_block(s);
|
---|
281 |
|
---|
282 | if ((p = OPENSSL_malloc(num)) == NULL) {
|
---|
283 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
284 | return 0;
|
---|
285 | }
|
---|
286 |
|
---|
287 | s->s3.tmp.key_block_length = num;
|
---|
288 | s->s3.tmp.key_block = p;
|
---|
289 |
|
---|
290 | /* Calls SSLfatal() as required */
|
---|
291 | ret = ssl3_generate_key_block(s, p, num);
|
---|
292 |
|
---|
293 | if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) {
|
---|
294 | /*
|
---|
295 | * enable vulnerability countermeasure for CBC ciphers with known-IV
|
---|
296 | * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
|
---|
297 | */
|
---|
298 | s->s3.need_empty_fragments = 1;
|
---|
299 |
|
---|
300 | if (s->session->cipher != NULL) {
|
---|
301 | if (s->session->cipher->algorithm_enc == SSL_eNULL)
|
---|
302 | s->s3.need_empty_fragments = 0;
|
---|
303 |
|
---|
304 | if (s->session->cipher->algorithm_enc == SSL_RC4)
|
---|
305 | s->s3.need_empty_fragments = 0;
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | return ret;
|
---|
310 | }
|
---|
311 |
|
---|
312 | void ssl3_cleanup_key_block(SSL *s)
|
---|
313 | {
|
---|
314 | OPENSSL_clear_free(s->s3.tmp.key_block, s->s3.tmp.key_block_length);
|
---|
315 | s->s3.tmp.key_block = NULL;
|
---|
316 | s->s3.tmp.key_block_length = 0;
|
---|
317 | }
|
---|
318 |
|
---|
319 | int ssl3_init_finished_mac(SSL *s)
|
---|
320 | {
|
---|
321 | BIO *buf = BIO_new(BIO_s_mem());
|
---|
322 |
|
---|
323 | if (buf == NULL) {
|
---|
324 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
325 | return 0;
|
---|
326 | }
|
---|
327 | ssl3_free_digest_list(s);
|
---|
328 | s->s3.handshake_buffer = buf;
|
---|
329 | (void)BIO_set_close(s->s3.handshake_buffer, BIO_CLOSE);
|
---|
330 | return 1;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /*
|
---|
334 | * Free digest list. Also frees handshake buffer since they are always freed
|
---|
335 | * together.
|
---|
336 | */
|
---|
337 |
|
---|
338 | void ssl3_free_digest_list(SSL *s)
|
---|
339 | {
|
---|
340 | BIO_free(s->s3.handshake_buffer);
|
---|
341 | s->s3.handshake_buffer = NULL;
|
---|
342 | EVP_MD_CTX_free(s->s3.handshake_dgst);
|
---|
343 | s->s3.handshake_dgst = NULL;
|
---|
344 | }
|
---|
345 |
|
---|
346 | int ssl3_finish_mac(SSL *s, const unsigned char *buf, size_t len)
|
---|
347 | {
|
---|
348 | int ret;
|
---|
349 |
|
---|
350 | if (s->s3.handshake_dgst == NULL) {
|
---|
351 | /* Note: this writes to a memory BIO so a failure is a fatal error */
|
---|
352 | if (len > INT_MAX) {
|
---|
353 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_OVERFLOW_ERROR);
|
---|
354 | return 0;
|
---|
355 | }
|
---|
356 | ret = BIO_write(s->s3.handshake_buffer, (void *)buf, (int)len);
|
---|
357 | if (ret <= 0 || ret != (int)len) {
|
---|
358 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
359 | return 0;
|
---|
360 | }
|
---|
361 | } else {
|
---|
362 | ret = EVP_DigestUpdate(s->s3.handshake_dgst, buf, len);
|
---|
363 | if (!ret) {
|
---|
364 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
365 | return 0;
|
---|
366 | }
|
---|
367 | }
|
---|
368 | return 1;
|
---|
369 | }
|
---|
370 |
|
---|
371 | int ssl3_digest_cached_records(SSL *s, int keep)
|
---|
372 | {
|
---|
373 | const EVP_MD *md;
|
---|
374 | long hdatalen;
|
---|
375 | void *hdata;
|
---|
376 |
|
---|
377 | if (s->s3.handshake_dgst == NULL) {
|
---|
378 | hdatalen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
|
---|
379 | if (hdatalen <= 0) {
|
---|
380 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
|
---|
381 | return 0;
|
---|
382 | }
|
---|
383 |
|
---|
384 | s->s3.handshake_dgst = EVP_MD_CTX_new();
|
---|
385 | if (s->s3.handshake_dgst == NULL) {
|
---|
386 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
387 | return 0;
|
---|
388 | }
|
---|
389 |
|
---|
390 | md = ssl_handshake_md(s);
|
---|
391 | if (md == NULL) {
|
---|
392 | SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
---|
393 | SSL_R_NO_SUITABLE_DIGEST_ALGORITHM);
|
---|
394 | return 0;
|
---|
395 | }
|
---|
396 | if (!EVP_DigestInit_ex(s->s3.handshake_dgst, md, NULL)
|
---|
397 | || !EVP_DigestUpdate(s->s3.handshake_dgst, hdata, hdatalen)) {
|
---|
398 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
399 | return 0;
|
---|
400 | }
|
---|
401 | }
|
---|
402 | if (keep == 0) {
|
---|
403 | BIO_free(s->s3.handshake_buffer);
|
---|
404 | s->s3.handshake_buffer = NULL;
|
---|
405 | }
|
---|
406 |
|
---|
407 | return 1;
|
---|
408 | }
|
---|
409 |
|
---|
410 | void ssl3_digest_master_key_set_params(const SSL_SESSION *session,
|
---|
411 | OSSL_PARAM params[])
|
---|
412 | {
|
---|
413 | int n = 0;
|
---|
414 | params[n++] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
|
---|
415 | (void *)session->master_key,
|
---|
416 | session->master_key_length);
|
---|
417 | params[n++] = OSSL_PARAM_construct_end();
|
---|
418 | }
|
---|
419 |
|
---|
420 | size_t ssl3_final_finish_mac(SSL *s, const char *sender, size_t len,
|
---|
421 | unsigned char *p)
|
---|
422 | {
|
---|
423 | int ret;
|
---|
424 | EVP_MD_CTX *ctx = NULL;
|
---|
425 |
|
---|
426 | if (!ssl3_digest_cached_records(s, 0)) {
|
---|
427 | /* SSLfatal() already called */
|
---|
428 | return 0;
|
---|
429 | }
|
---|
430 |
|
---|
431 | if (EVP_MD_CTX_get_type(s->s3.handshake_dgst) != NID_md5_sha1) {
|
---|
432 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_REQUIRED_DIGEST);
|
---|
433 | return 0;
|
---|
434 | }
|
---|
435 |
|
---|
436 | ctx = EVP_MD_CTX_new();
|
---|
437 | if (ctx == NULL) {
|
---|
438 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
439 | return 0;
|
---|
440 | }
|
---|
441 | if (!EVP_MD_CTX_copy_ex(ctx, s->s3.handshake_dgst)) {
|
---|
442 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
443 | ret = 0;
|
---|
444 | goto err;
|
---|
445 | }
|
---|
446 |
|
---|
447 | ret = EVP_MD_CTX_get_size(ctx);
|
---|
448 | if (ret < 0) {
|
---|
449 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
450 | ret = 0;
|
---|
451 | goto err;
|
---|
452 | }
|
---|
453 |
|
---|
454 | if (sender != NULL) {
|
---|
455 | OSSL_PARAM digest_cmd_params[3];
|
---|
456 |
|
---|
457 | ssl3_digest_master_key_set_params(s->session, digest_cmd_params);
|
---|
458 |
|
---|
459 | if (EVP_DigestUpdate(ctx, sender, len) <= 0
|
---|
460 | || EVP_MD_CTX_set_params(ctx, digest_cmd_params) <= 0
|
---|
461 | || EVP_DigestFinal_ex(ctx, p, NULL) <= 0) {
|
---|
462 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
463 | ret = 0;
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 | err:
|
---|
468 | EVP_MD_CTX_free(ctx);
|
---|
469 |
|
---|
470 | return ret;
|
---|
471 | }
|
---|
472 |
|
---|
473 | int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
|
---|
474 | size_t len, size_t *secret_size)
|
---|
475 | {
|
---|
476 | static const unsigned char *salt[3] = {
|
---|
477 | #ifndef CHARSET_EBCDIC
|
---|
478 | (const unsigned char *)"A",
|
---|
479 | (const unsigned char *)"BB",
|
---|
480 | (const unsigned char *)"CCC",
|
---|
481 | #else
|
---|
482 | (const unsigned char *)"\x41",
|
---|
483 | (const unsigned char *)"\x42\x42",
|
---|
484 | (const unsigned char *)"\x43\x43\x43",
|
---|
485 | #endif
|
---|
486 | };
|
---|
487 | unsigned char buf[EVP_MAX_MD_SIZE];
|
---|
488 | EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
---|
489 | int i, ret = 1;
|
---|
490 | unsigned int n;
|
---|
491 | size_t ret_secret_size = 0;
|
---|
492 |
|
---|
493 | if (ctx == NULL) {
|
---|
494 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
495 | return 0;
|
---|
496 | }
|
---|
497 | for (i = 0; i < 3; i++) {
|
---|
498 | if (EVP_DigestInit_ex(ctx, s->ctx->sha1, NULL) <= 0
|
---|
499 | || EVP_DigestUpdate(ctx, salt[i],
|
---|
500 | strlen((const char *)salt[i])) <= 0
|
---|
501 | || EVP_DigestUpdate(ctx, p, len) <= 0
|
---|
502 | || EVP_DigestUpdate(ctx, &(s->s3.client_random[0]),
|
---|
503 | SSL3_RANDOM_SIZE) <= 0
|
---|
504 | || EVP_DigestUpdate(ctx, &(s->s3.server_random[0]),
|
---|
505 | SSL3_RANDOM_SIZE) <= 0
|
---|
506 | || EVP_DigestFinal_ex(ctx, buf, &n) <= 0
|
---|
507 | || EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0
|
---|
508 | || EVP_DigestUpdate(ctx, p, len) <= 0
|
---|
509 | || EVP_DigestUpdate(ctx, buf, n) <= 0
|
---|
510 | || EVP_DigestFinal_ex(ctx, out, &n) <= 0) {
|
---|
511 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
512 | ret = 0;
|
---|
513 | break;
|
---|
514 | }
|
---|
515 | out += n;
|
---|
516 | ret_secret_size += n;
|
---|
517 | }
|
---|
518 | EVP_MD_CTX_free(ctx);
|
---|
519 |
|
---|
520 | OPENSSL_cleanse(buf, sizeof(buf));
|
---|
521 | if (ret)
|
---|
522 | *secret_size = ret_secret_size;
|
---|
523 | return ret;
|
---|
524 | }
|
---|
525 |
|
---|
526 | int ssl3_alert_code(int code)
|
---|
527 | {
|
---|
528 | switch (code) {
|
---|
529 | case SSL_AD_CLOSE_NOTIFY:
|
---|
530 | return SSL3_AD_CLOSE_NOTIFY;
|
---|
531 | case SSL_AD_UNEXPECTED_MESSAGE:
|
---|
532 | return SSL3_AD_UNEXPECTED_MESSAGE;
|
---|
533 | case SSL_AD_BAD_RECORD_MAC:
|
---|
534 | return SSL3_AD_BAD_RECORD_MAC;
|
---|
535 | case SSL_AD_DECRYPTION_FAILED:
|
---|
536 | return SSL3_AD_BAD_RECORD_MAC;
|
---|
537 | case SSL_AD_RECORD_OVERFLOW:
|
---|
538 | return SSL3_AD_BAD_RECORD_MAC;
|
---|
539 | case SSL_AD_DECOMPRESSION_FAILURE:
|
---|
540 | return SSL3_AD_DECOMPRESSION_FAILURE;
|
---|
541 | case SSL_AD_HANDSHAKE_FAILURE:
|
---|
542 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
543 | case SSL_AD_NO_CERTIFICATE:
|
---|
544 | return SSL3_AD_NO_CERTIFICATE;
|
---|
545 | case SSL_AD_BAD_CERTIFICATE:
|
---|
546 | return SSL3_AD_BAD_CERTIFICATE;
|
---|
547 | case SSL_AD_UNSUPPORTED_CERTIFICATE:
|
---|
548 | return SSL3_AD_UNSUPPORTED_CERTIFICATE;
|
---|
549 | case SSL_AD_CERTIFICATE_REVOKED:
|
---|
550 | return SSL3_AD_CERTIFICATE_REVOKED;
|
---|
551 | case SSL_AD_CERTIFICATE_EXPIRED:
|
---|
552 | return SSL3_AD_CERTIFICATE_EXPIRED;
|
---|
553 | case SSL_AD_CERTIFICATE_UNKNOWN:
|
---|
554 | return SSL3_AD_CERTIFICATE_UNKNOWN;
|
---|
555 | case SSL_AD_ILLEGAL_PARAMETER:
|
---|
556 | return SSL3_AD_ILLEGAL_PARAMETER;
|
---|
557 | case SSL_AD_UNKNOWN_CA:
|
---|
558 | return SSL3_AD_BAD_CERTIFICATE;
|
---|
559 | case SSL_AD_ACCESS_DENIED:
|
---|
560 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
561 | case SSL_AD_DECODE_ERROR:
|
---|
562 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
563 | case SSL_AD_DECRYPT_ERROR:
|
---|
564 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
565 | case SSL_AD_EXPORT_RESTRICTION:
|
---|
566 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
567 | case SSL_AD_PROTOCOL_VERSION:
|
---|
568 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
569 | case SSL_AD_INSUFFICIENT_SECURITY:
|
---|
570 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
571 | case SSL_AD_INTERNAL_ERROR:
|
---|
572 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
573 | case SSL_AD_USER_CANCELLED:
|
---|
574 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
575 | case SSL_AD_NO_RENEGOTIATION:
|
---|
576 | return -1; /* Don't send it :-) */
|
---|
577 | case SSL_AD_UNSUPPORTED_EXTENSION:
|
---|
578 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
579 | case SSL_AD_CERTIFICATE_UNOBTAINABLE:
|
---|
580 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
581 | case SSL_AD_UNRECOGNIZED_NAME:
|
---|
582 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
583 | case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
|
---|
584 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
585 | case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
|
---|
586 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
587 | case SSL_AD_UNKNOWN_PSK_IDENTITY:
|
---|
588 | return TLS1_AD_UNKNOWN_PSK_IDENTITY;
|
---|
589 | case SSL_AD_INAPPROPRIATE_FALLBACK:
|
---|
590 | return TLS1_AD_INAPPROPRIATE_FALLBACK;
|
---|
591 | case SSL_AD_NO_APPLICATION_PROTOCOL:
|
---|
592 | return TLS1_AD_NO_APPLICATION_PROTOCOL;
|
---|
593 | case SSL_AD_CERTIFICATE_REQUIRED:
|
---|
594 | return SSL_AD_HANDSHAKE_FAILURE;
|
---|
595 | case TLS13_AD_MISSING_EXTENSION:
|
---|
596 | return SSL_AD_HANDSHAKE_FAILURE;
|
---|
597 | default:
|
---|
598 | return -1;
|
---|
599 | }
|
---|
600 | }
|
---|