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 | if (EVP_CIPHER_get0_provider(c) != NULL
|
---|
229 | && !tls_provider_set_tls_params(s, dd, c, m)) {
|
---|
230 | /* SSLfatal already called */
|
---|
231 | goto err;
|
---|
232 | }
|
---|
233 |
|
---|
234 | s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
|
---|
235 | return 1;
|
---|
236 | err:
|
---|
237 | return 0;
|
---|
238 | }
|
---|
239 |
|
---|
240 | int ssl3_setup_key_block(SSL *s)
|
---|
241 | {
|
---|
242 | unsigned char *p;
|
---|
243 | const EVP_CIPHER *c;
|
---|
244 | const EVP_MD *hash;
|
---|
245 | int num;
|
---|
246 | int ret = 0;
|
---|
247 | SSL_COMP *comp;
|
---|
248 |
|
---|
249 | if (s->s3.tmp.key_block_length != 0)
|
---|
250 | return 1;
|
---|
251 |
|
---|
252 | if (!ssl_cipher_get_evp(s->ctx, s->session, &c, &hash, NULL, NULL, &comp,
|
---|
253 | 0)) {
|
---|
254 | /* Error is already recorded */
|
---|
255 | SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
|
---|
256 | return 0;
|
---|
257 | }
|
---|
258 |
|
---|
259 | ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
|
---|
260 | s->s3.tmp.new_sym_enc = c;
|
---|
261 | ssl_evp_md_free(s->s3.tmp.new_hash);
|
---|
262 | s->s3.tmp.new_hash = hash;
|
---|
263 | #ifdef OPENSSL_NO_COMP
|
---|
264 | s->s3.tmp.new_compression = NULL;
|
---|
265 | #else
|
---|
266 | s->s3.tmp.new_compression = comp;
|
---|
267 | #endif
|
---|
268 |
|
---|
269 | num = EVP_MD_get_size(hash);
|
---|
270 | if (num < 0)
|
---|
271 | return 0;
|
---|
272 |
|
---|
273 | num = EVP_CIPHER_get_key_length(c) + num + EVP_CIPHER_get_iv_length(c);
|
---|
274 | num *= 2;
|
---|
275 |
|
---|
276 | ssl3_cleanup_key_block(s);
|
---|
277 |
|
---|
278 | if ((p = OPENSSL_malloc(num)) == NULL) {
|
---|
279 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
280 | return 0;
|
---|
281 | }
|
---|
282 |
|
---|
283 | s->s3.tmp.key_block_length = num;
|
---|
284 | s->s3.tmp.key_block = p;
|
---|
285 |
|
---|
286 | /* Calls SSLfatal() as required */
|
---|
287 | ret = ssl3_generate_key_block(s, p, num);
|
---|
288 |
|
---|
289 | if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) {
|
---|
290 | /*
|
---|
291 | * enable vulnerability countermeasure for CBC ciphers with known-IV
|
---|
292 | * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
|
---|
293 | */
|
---|
294 | s->s3.need_empty_fragments = 1;
|
---|
295 |
|
---|
296 | if (s->session->cipher != NULL) {
|
---|
297 | if (s->session->cipher->algorithm_enc == SSL_eNULL)
|
---|
298 | s->s3.need_empty_fragments = 0;
|
---|
299 |
|
---|
300 | if (s->session->cipher->algorithm_enc == SSL_RC4)
|
---|
301 | s->s3.need_empty_fragments = 0;
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | return ret;
|
---|
306 | }
|
---|
307 |
|
---|
308 | void ssl3_cleanup_key_block(SSL *s)
|
---|
309 | {
|
---|
310 | OPENSSL_clear_free(s->s3.tmp.key_block, s->s3.tmp.key_block_length);
|
---|
311 | s->s3.tmp.key_block = NULL;
|
---|
312 | s->s3.tmp.key_block_length = 0;
|
---|
313 | }
|
---|
314 |
|
---|
315 | int ssl3_init_finished_mac(SSL *s)
|
---|
316 | {
|
---|
317 | BIO *buf = BIO_new(BIO_s_mem());
|
---|
318 |
|
---|
319 | if (buf == NULL) {
|
---|
320 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
321 | return 0;
|
---|
322 | }
|
---|
323 | ssl3_free_digest_list(s);
|
---|
324 | s->s3.handshake_buffer = buf;
|
---|
325 | (void)BIO_set_close(s->s3.handshake_buffer, BIO_CLOSE);
|
---|
326 | return 1;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /*
|
---|
330 | * Free digest list. Also frees handshake buffer since they are always freed
|
---|
331 | * together.
|
---|
332 | */
|
---|
333 |
|
---|
334 | void ssl3_free_digest_list(SSL *s)
|
---|
335 | {
|
---|
336 | BIO_free(s->s3.handshake_buffer);
|
---|
337 | s->s3.handshake_buffer = NULL;
|
---|
338 | EVP_MD_CTX_free(s->s3.handshake_dgst);
|
---|
339 | s->s3.handshake_dgst = NULL;
|
---|
340 | }
|
---|
341 |
|
---|
342 | int ssl3_finish_mac(SSL *s, const unsigned char *buf, size_t len)
|
---|
343 | {
|
---|
344 | int ret;
|
---|
345 |
|
---|
346 | if (s->s3.handshake_dgst == NULL) {
|
---|
347 | /* Note: this writes to a memory BIO so a failure is a fatal error */
|
---|
348 | if (len > INT_MAX) {
|
---|
349 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_OVERFLOW_ERROR);
|
---|
350 | return 0;
|
---|
351 | }
|
---|
352 | ret = BIO_write(s->s3.handshake_buffer, (void *)buf, (int)len);
|
---|
353 | if (ret <= 0 || ret != (int)len) {
|
---|
354 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
355 | return 0;
|
---|
356 | }
|
---|
357 | } else {
|
---|
358 | ret = EVP_DigestUpdate(s->s3.handshake_dgst, buf, len);
|
---|
359 | if (!ret) {
|
---|
360 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
361 | return 0;
|
---|
362 | }
|
---|
363 | }
|
---|
364 | return 1;
|
---|
365 | }
|
---|
366 |
|
---|
367 | int ssl3_digest_cached_records(SSL *s, int keep)
|
---|
368 | {
|
---|
369 | const EVP_MD *md;
|
---|
370 | long hdatalen;
|
---|
371 | void *hdata;
|
---|
372 |
|
---|
373 | if (s->s3.handshake_dgst == NULL) {
|
---|
374 | hdatalen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
|
---|
375 | if (hdatalen <= 0) {
|
---|
376 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
|
---|
377 | return 0;
|
---|
378 | }
|
---|
379 |
|
---|
380 | s->s3.handshake_dgst = EVP_MD_CTX_new();
|
---|
381 | if (s->s3.handshake_dgst == NULL) {
|
---|
382 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
383 | return 0;
|
---|
384 | }
|
---|
385 |
|
---|
386 | md = ssl_handshake_md(s);
|
---|
387 | if (md == NULL) {
|
---|
388 | SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
---|
389 | SSL_R_NO_SUITABLE_DIGEST_ALGORITHM);
|
---|
390 | return 0;
|
---|
391 | }
|
---|
392 | if (!EVP_DigestInit_ex(s->s3.handshake_dgst, md, NULL)
|
---|
393 | || !EVP_DigestUpdate(s->s3.handshake_dgst, hdata, hdatalen)) {
|
---|
394 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
395 | return 0;
|
---|
396 | }
|
---|
397 | }
|
---|
398 | if (keep == 0) {
|
---|
399 | BIO_free(s->s3.handshake_buffer);
|
---|
400 | s->s3.handshake_buffer = NULL;
|
---|
401 | }
|
---|
402 |
|
---|
403 | return 1;
|
---|
404 | }
|
---|
405 |
|
---|
406 | void ssl3_digest_master_key_set_params(const SSL_SESSION *session,
|
---|
407 | OSSL_PARAM params[])
|
---|
408 | {
|
---|
409 | int n = 0;
|
---|
410 | params[n++] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
|
---|
411 | (void *)session->master_key,
|
---|
412 | session->master_key_length);
|
---|
413 | params[n++] = OSSL_PARAM_construct_end();
|
---|
414 | }
|
---|
415 |
|
---|
416 | size_t ssl3_final_finish_mac(SSL *s, const char *sender, size_t len,
|
---|
417 | unsigned char *p)
|
---|
418 | {
|
---|
419 | int ret;
|
---|
420 | EVP_MD_CTX *ctx = NULL;
|
---|
421 |
|
---|
422 | if (!ssl3_digest_cached_records(s, 0)) {
|
---|
423 | /* SSLfatal() already called */
|
---|
424 | return 0;
|
---|
425 | }
|
---|
426 |
|
---|
427 | if (EVP_MD_CTX_get_type(s->s3.handshake_dgst) != NID_md5_sha1) {
|
---|
428 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_REQUIRED_DIGEST);
|
---|
429 | return 0;
|
---|
430 | }
|
---|
431 |
|
---|
432 | ctx = EVP_MD_CTX_new();
|
---|
433 | if (ctx == NULL) {
|
---|
434 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
435 | return 0;
|
---|
436 | }
|
---|
437 | if (!EVP_MD_CTX_copy_ex(ctx, s->s3.handshake_dgst)) {
|
---|
438 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
439 | ret = 0;
|
---|
440 | goto err;
|
---|
441 | }
|
---|
442 |
|
---|
443 | ret = EVP_MD_CTX_get_size(ctx);
|
---|
444 | if (ret < 0) {
|
---|
445 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
446 | ret = 0;
|
---|
447 | goto err;
|
---|
448 | }
|
---|
449 |
|
---|
450 | if (sender != NULL) {
|
---|
451 | OSSL_PARAM digest_cmd_params[3];
|
---|
452 |
|
---|
453 | ssl3_digest_master_key_set_params(s->session, digest_cmd_params);
|
---|
454 |
|
---|
455 | if (EVP_DigestUpdate(ctx, sender, len) <= 0
|
---|
456 | || EVP_MD_CTX_set_params(ctx, digest_cmd_params) <= 0
|
---|
457 | || EVP_DigestFinal_ex(ctx, p, NULL) <= 0) {
|
---|
458 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
459 | ret = 0;
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | err:
|
---|
464 | EVP_MD_CTX_free(ctx);
|
---|
465 |
|
---|
466 | return ret;
|
---|
467 | }
|
---|
468 |
|
---|
469 | int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
|
---|
470 | size_t len, size_t *secret_size)
|
---|
471 | {
|
---|
472 | static const unsigned char *salt[3] = {
|
---|
473 | #ifndef CHARSET_EBCDIC
|
---|
474 | (const unsigned char *)"A",
|
---|
475 | (const unsigned char *)"BB",
|
---|
476 | (const unsigned char *)"CCC",
|
---|
477 | #else
|
---|
478 | (const unsigned char *)"\x41",
|
---|
479 | (const unsigned char *)"\x42\x42",
|
---|
480 | (const unsigned char *)"\x43\x43\x43",
|
---|
481 | #endif
|
---|
482 | };
|
---|
483 | unsigned char buf[EVP_MAX_MD_SIZE];
|
---|
484 | EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
---|
485 | int i, ret = 1;
|
---|
486 | unsigned int n;
|
---|
487 | size_t ret_secret_size = 0;
|
---|
488 |
|
---|
489 | if (ctx == NULL) {
|
---|
490 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
|
---|
491 | return 0;
|
---|
492 | }
|
---|
493 | for (i = 0; i < 3; i++) {
|
---|
494 | if (EVP_DigestInit_ex(ctx, s->ctx->sha1, NULL) <= 0
|
---|
495 | || EVP_DigestUpdate(ctx, salt[i],
|
---|
496 | strlen((const char *)salt[i])) <= 0
|
---|
497 | || EVP_DigestUpdate(ctx, p, len) <= 0
|
---|
498 | || EVP_DigestUpdate(ctx, &(s->s3.client_random[0]),
|
---|
499 | SSL3_RANDOM_SIZE) <= 0
|
---|
500 | || EVP_DigestUpdate(ctx, &(s->s3.server_random[0]),
|
---|
501 | SSL3_RANDOM_SIZE) <= 0
|
---|
502 | || EVP_DigestFinal_ex(ctx, buf, &n) <= 0
|
---|
503 | || EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0
|
---|
504 | || EVP_DigestUpdate(ctx, p, len) <= 0
|
---|
505 | || EVP_DigestUpdate(ctx, buf, n) <= 0
|
---|
506 | || EVP_DigestFinal_ex(ctx, out, &n) <= 0) {
|
---|
507 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
---|
508 | ret = 0;
|
---|
509 | break;
|
---|
510 | }
|
---|
511 | out += n;
|
---|
512 | ret_secret_size += n;
|
---|
513 | }
|
---|
514 | EVP_MD_CTX_free(ctx);
|
---|
515 |
|
---|
516 | OPENSSL_cleanse(buf, sizeof(buf));
|
---|
517 | if (ret)
|
---|
518 | *secret_size = ret_secret_size;
|
---|
519 | return ret;
|
---|
520 | }
|
---|
521 |
|
---|
522 | int ssl3_alert_code(int code)
|
---|
523 | {
|
---|
524 | switch (code) {
|
---|
525 | case SSL_AD_CLOSE_NOTIFY:
|
---|
526 | return SSL3_AD_CLOSE_NOTIFY;
|
---|
527 | case SSL_AD_UNEXPECTED_MESSAGE:
|
---|
528 | return SSL3_AD_UNEXPECTED_MESSAGE;
|
---|
529 | case SSL_AD_BAD_RECORD_MAC:
|
---|
530 | return SSL3_AD_BAD_RECORD_MAC;
|
---|
531 | case SSL_AD_DECRYPTION_FAILED:
|
---|
532 | return SSL3_AD_BAD_RECORD_MAC;
|
---|
533 | case SSL_AD_RECORD_OVERFLOW:
|
---|
534 | return SSL3_AD_BAD_RECORD_MAC;
|
---|
535 | case SSL_AD_DECOMPRESSION_FAILURE:
|
---|
536 | return SSL3_AD_DECOMPRESSION_FAILURE;
|
---|
537 | case SSL_AD_HANDSHAKE_FAILURE:
|
---|
538 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
539 | case SSL_AD_NO_CERTIFICATE:
|
---|
540 | return SSL3_AD_NO_CERTIFICATE;
|
---|
541 | case SSL_AD_BAD_CERTIFICATE:
|
---|
542 | return SSL3_AD_BAD_CERTIFICATE;
|
---|
543 | case SSL_AD_UNSUPPORTED_CERTIFICATE:
|
---|
544 | return SSL3_AD_UNSUPPORTED_CERTIFICATE;
|
---|
545 | case SSL_AD_CERTIFICATE_REVOKED:
|
---|
546 | return SSL3_AD_CERTIFICATE_REVOKED;
|
---|
547 | case SSL_AD_CERTIFICATE_EXPIRED:
|
---|
548 | return SSL3_AD_CERTIFICATE_EXPIRED;
|
---|
549 | case SSL_AD_CERTIFICATE_UNKNOWN:
|
---|
550 | return SSL3_AD_CERTIFICATE_UNKNOWN;
|
---|
551 | case SSL_AD_ILLEGAL_PARAMETER:
|
---|
552 | return SSL3_AD_ILLEGAL_PARAMETER;
|
---|
553 | case SSL_AD_UNKNOWN_CA:
|
---|
554 | return SSL3_AD_BAD_CERTIFICATE;
|
---|
555 | case SSL_AD_ACCESS_DENIED:
|
---|
556 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
557 | case SSL_AD_DECODE_ERROR:
|
---|
558 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
559 | case SSL_AD_DECRYPT_ERROR:
|
---|
560 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
561 | case SSL_AD_EXPORT_RESTRICTION:
|
---|
562 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
563 | case SSL_AD_PROTOCOL_VERSION:
|
---|
564 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
565 | case SSL_AD_INSUFFICIENT_SECURITY:
|
---|
566 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
567 | case SSL_AD_INTERNAL_ERROR:
|
---|
568 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
569 | case SSL_AD_USER_CANCELLED:
|
---|
570 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
571 | case SSL_AD_NO_RENEGOTIATION:
|
---|
572 | return -1; /* Don't send it :-) */
|
---|
573 | case SSL_AD_UNSUPPORTED_EXTENSION:
|
---|
574 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
575 | case SSL_AD_CERTIFICATE_UNOBTAINABLE:
|
---|
576 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
577 | case SSL_AD_UNRECOGNIZED_NAME:
|
---|
578 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
579 | case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
|
---|
580 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
581 | case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
|
---|
582 | return SSL3_AD_HANDSHAKE_FAILURE;
|
---|
583 | case SSL_AD_UNKNOWN_PSK_IDENTITY:
|
---|
584 | return TLS1_AD_UNKNOWN_PSK_IDENTITY;
|
---|
585 | case SSL_AD_INAPPROPRIATE_FALLBACK:
|
---|
586 | return TLS1_AD_INAPPROPRIATE_FALLBACK;
|
---|
587 | case SSL_AD_NO_APPLICATION_PROTOCOL:
|
---|
588 | return TLS1_AD_NO_APPLICATION_PROTOCOL;
|
---|
589 | case SSL_AD_CERTIFICATE_REQUIRED:
|
---|
590 | return SSL_AD_HANDSHAKE_FAILURE;
|
---|
591 | case TLS13_AD_MISSING_EXTENSION:
|
---|
592 | return SSL_AD_HANDSHAKE_FAILURE;
|
---|
593 | default:
|
---|
594 | return -1;
|
---|
595 | }
|
---|
596 | }
|
---|