blob: 9630843fc5455df569c2c08b3d6e4e5c554110a7 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *g
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *g
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *g
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) fromg
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *g
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *g
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.g
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2005 Nokia. All rights reserved.
112 *
113 * The portions of the attached software ("Contribution") is developed by
114 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
115 * license.
116 *
117 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
118 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
119 * support (see RFC 4279) to OpenSSL.
120 *
121 * No patent licenses or other rights except those expressly stated in
122 * the OpenSSL open source license shall be deemed granted or received
123 * expressly, by implication, estoppel, or otherwise.
124 *
125 * No assurances are provided by Nokia that the Contribution does not
126 * infringe the patent or other intellectual property rights of any third
127 * party or that the license provides you with all the necessary rights
128 * to make use of the Contribution.
129 *
130 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
131 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
132 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
133 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
134 * OTHERWISE. */
135
136#include <stdio.h>
137#include <assert.h>
138
139#include <openssl/err.h>
140#include <openssl/evp.h>
141#include <openssl/mem.h>
142#include <openssl/md5.h>
143#include <openssl/obj.h>
144
145#include "ssl_locl.h"
146
Adam Langley95c29f32014-06-20 12:00:00 -0700147
Adam Langleybe2900a2014-12-18 12:09:04 -0800148static const uint8_t ssl3_pad_1[48] = {
149 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
150 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
151 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
152 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
153};
Adam Langley95c29f32014-06-20 12:00:00 -0700154
Adam Langleybe2900a2014-12-18 12:09:04 -0800155static const uint8_t ssl3_pad_2[48] = {
156 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
157 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
158 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
159 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
160};
Adam Langley95c29f32014-06-20 12:00:00 -0700161
Adam Langleybe2900a2014-12-18 12:09:04 -0800162static int ssl3_handshake_mac(SSL *s, int md_nid, const char *sender, int len,
163 uint8_t *p);
Adam Langley95c29f32014-06-20 12:00:00 -0700164
David Benjamin41ac9792014-12-23 10:41:06 -0500165int ssl3_prf(SSL *s, uint8_t *out, size_t out_len, const uint8_t *secret,
166 size_t secret_len, const char *label, size_t label_len,
167 const uint8_t *seed1, size_t seed1_len,
168 const uint8_t *seed2, size_t seed2_len) {
Adam Langleybe2900a2014-12-18 12:09:04 -0800169 EVP_MD_CTX md5;
170 EVP_MD_CTX sha1;
171 uint8_t buf[16], smd[SHA_DIGEST_LENGTH];
172 uint8_t c = 'A';
David Benjamin31b1d812014-12-23 10:01:09 -0500173 size_t i, j, k;
Adam Langley95c29f32014-06-20 12:00:00 -0700174
Adam Langleybe2900a2014-12-18 12:09:04 -0800175 k = 0;
176 EVP_MD_CTX_init(&md5);
177 EVP_MD_CTX_init(&sha1);
David Benjamin31b1d812014-12-23 10:01:09 -0500178 for (i = 0; i < out_len; i += MD5_DIGEST_LENGTH) {
Adam Langleybe2900a2014-12-18 12:09:04 -0800179 k++;
180 if (k > sizeof(buf)) {
181 /* bug: 'buf' is too small for this ciphersuite */
David Benjamin41ac9792014-12-23 10:41:06 -0500182 OPENSSL_PUT_ERROR(SSL, ssl3_prf, ERR_R_INTERNAL_ERROR);
Adam Langleybe2900a2014-12-18 12:09:04 -0800183 return 0;
184 }
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langleybe2900a2014-12-18 12:09:04 -0800186 for (j = 0; j < k; j++) {
187 buf[j] = c;
188 }
189 c++;
190 if (!EVP_DigestInit_ex(&sha1, EVP_sha1(), NULL)) {
David Benjamin41ac9792014-12-23 10:41:06 -0500191 OPENSSL_PUT_ERROR(SSL, ssl3_prf, ERR_LIB_EVP);
Adam Langleybe2900a2014-12-18 12:09:04 -0800192 return 0;
193 }
194 EVP_DigestUpdate(&sha1, buf, k);
David Benjamin31b1d812014-12-23 10:01:09 -0500195 EVP_DigestUpdate(&sha1, secret, secret_len);
David Benjamin41ac9792014-12-23 10:41:06 -0500196 /* |label| is ignored for SSLv3. */
David Benjamin31b1d812014-12-23 10:01:09 -0500197 if (seed1_len) {
198 EVP_DigestUpdate(&sha1, seed1, seed1_len);
199 }
200 if (seed2_len) {
201 EVP_DigestUpdate(&sha1, seed2, seed2_len);
202 }
Adam Langleybe2900a2014-12-18 12:09:04 -0800203 EVP_DigestFinal_ex(&sha1, smd, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700204
Adam Langleybe2900a2014-12-18 12:09:04 -0800205 if (!EVP_DigestInit_ex(&md5, EVP_md5(), NULL)) {
David Benjamin41ac9792014-12-23 10:41:06 -0500206 OPENSSL_PUT_ERROR(SSL, ssl3_prf, ERR_LIB_EVP);
Adam Langleybe2900a2014-12-18 12:09:04 -0800207 return 0;
208 }
David Benjamin31b1d812014-12-23 10:01:09 -0500209 EVP_DigestUpdate(&md5, secret, secret_len);
Adam Langleybe2900a2014-12-18 12:09:04 -0800210 EVP_DigestUpdate(&md5, smd, SHA_DIGEST_LENGTH);
David Benjamin31b1d812014-12-23 10:01:09 -0500211 if (i + MD5_DIGEST_LENGTH > out_len) {
Adam Langleybe2900a2014-12-18 12:09:04 -0800212 EVP_DigestFinal_ex(&md5, smd, NULL);
David Benjamin31b1d812014-12-23 10:01:09 -0500213 memcpy(out, smd, out_len - i);
Adam Langleybe2900a2014-12-18 12:09:04 -0800214 } else {
David Benjamin31b1d812014-12-23 10:01:09 -0500215 EVP_DigestFinal_ex(&md5, out, NULL);
Adam Langleybe2900a2014-12-18 12:09:04 -0800216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
David Benjamin31b1d812014-12-23 10:01:09 -0500218 out += MD5_DIGEST_LENGTH;
Adam Langleybe2900a2014-12-18 12:09:04 -0800219 }
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langleybe2900a2014-12-18 12:09:04 -0800221 OPENSSL_cleanse(smd, SHA_DIGEST_LENGTH);
222 EVP_MD_CTX_cleanup(&md5);
223 EVP_MD_CTX_cleanup(&sha1);
Adam Langley95c29f32014-06-20 12:00:00 -0700224
Adam Langleybe2900a2014-12-18 12:09:04 -0800225 return 1;
226}
Adam Langley95c29f32014-06-20 12:00:00 -0700227
Adam Langleybe2900a2014-12-18 12:09:04 -0800228void ssl3_cleanup_key_block(SSL *s) {
229 if (s->s3->tmp.key_block != NULL) {
230 OPENSSL_cleanse(s->s3->tmp.key_block, s->s3->tmp.key_block_length);
231 OPENSSL_free(s->s3->tmp.key_block);
232 s->s3->tmp.key_block = NULL;
233 }
234 s->s3->tmp.key_block_length = 0;
235}
Adam Langley95c29f32014-06-20 12:00:00 -0700236
Adam Langleybe2900a2014-12-18 12:09:04 -0800237int ssl3_init_finished_mac(SSL *s) {
238 if (s->s3->handshake_buffer) {
239 BIO_free(s->s3->handshake_buffer);
240 }
241 if (s->s3->handshake_dgst) {
242 ssl3_free_digest_list(s);
243 }
244 s->s3->handshake_buffer = BIO_new(BIO_s_mem());
245 if (s->s3->handshake_buffer == NULL) {
246 return 0;
247 }
248 BIO_set_close(s->s3->handshake_buffer, BIO_CLOSE);
Adam Langley95c29f32014-06-20 12:00:00 -0700249
Adam Langleybe2900a2014-12-18 12:09:04 -0800250 return 1;
251}
Adam Langley95c29f32014-06-20 12:00:00 -0700252
Adam Langleybe2900a2014-12-18 12:09:04 -0800253void ssl3_free_digest_list(SSL *s) {
254 int i;
255 if (!s->s3->handshake_dgst) {
256 return;
257 }
258 for (i = 0; i < SSL_MAX_DIGEST; i++) {
259 if (s->s3->handshake_dgst[i]) {
260 EVP_MD_CTX_destroy(s->s3->handshake_dgst[i]);
261 }
262 }
263 OPENSSL_free(s->s3->handshake_dgst);
264 s->s3->handshake_dgst = NULL;
265}
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Adam Langleybe2900a2014-12-18 12:09:04 -0800267void ssl3_finish_mac(SSL *s, const uint8_t *buf, int len) {
268 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700269
Adam Langleybe2900a2014-12-18 12:09:04 -0800270 if (s->s3->handshake_buffer) {
271 BIO_write(s->s3->handshake_buffer, (void *)buf, len);
272 return;
273 }
Adam Langley95c29f32014-06-20 12:00:00 -0700274
Adam Langleybe2900a2014-12-18 12:09:04 -0800275 for (i = 0; i < SSL_MAX_DIGEST; i++) {
276 if (s->s3->handshake_dgst[i] != NULL) {
277 EVP_DigestUpdate(s->s3->handshake_dgst[i], buf, len);
278 }
279 }
280}
Adam Langley69a01602014-11-17 17:26:55 -0800281
Adam Langleybe2900a2014-12-18 12:09:04 -0800282int ssl3_digest_cached_records(
283 SSL *s, enum should_free_handshake_buffer_t should_free_handshake_buffer) {
284 int i;
285 long mask;
286 const EVP_MD *md;
287 const uint8_t *hdata;
288 size_t hdatalen;
Adam Langley95c29f32014-06-20 12:00:00 -0700289
Adam Langleybe2900a2014-12-18 12:09:04 -0800290 /* Allocate handshake_dgst array */
291 ssl3_free_digest_list(s);
292 s->s3->handshake_dgst = OPENSSL_malloc(SSL_MAX_DIGEST * sizeof(EVP_MD_CTX *));
293 if (s->s3->handshake_dgst == NULL) {
294 OPENSSL_PUT_ERROR(SSL, ssl3_digest_cached_records, ERR_R_MALLOC_FAILURE);
295 return 0;
296 }
Adam Langley75712922014-10-10 16:23:43 -0700297
Adam Langleybe2900a2014-12-18 12:09:04 -0800298 memset(s->s3->handshake_dgst, 0, SSL_MAX_DIGEST * sizeof(EVP_MD_CTX *));
299 if (!BIO_mem_contents(s->s3->handshake_buffer, &hdata, &hdatalen)) {
300 OPENSSL_PUT_ERROR(SSL, ssl3_digest_cached_records,
301 SSL_R_BAD_HANDSHAKE_LENGTH);
302 return 0;
303 }
Adam Langley95c29f32014-06-20 12:00:00 -0700304
Adam Langleybe2900a2014-12-18 12:09:04 -0800305 /* Loop through bits of algorithm2 field and create MD_CTX-es */
306 for (i = 0; ssl_get_handshake_digest(i, &mask, &md); i++) {
307 if ((mask & ssl_get_algorithm2(s)) && md) {
308 s->s3->handshake_dgst[i] = EVP_MD_CTX_create();
309 if (s->s3->handshake_dgst[i] == NULL) {
310 OPENSSL_PUT_ERROR(SSL, ssl3_digest_cached_records, ERR_LIB_EVP);
311 return 0;
312 }
313 if (!EVP_DigestInit_ex(s->s3->handshake_dgst[i], md, NULL)) {
314 EVP_MD_CTX_destroy(s->s3->handshake_dgst[i]);
315 s->s3->handshake_dgst[i] = NULL;
316 OPENSSL_PUT_ERROR(SSL, ssl3_digest_cached_records, ERR_LIB_EVP);
317 return 0;
318 }
319 EVP_DigestUpdate(s->s3->handshake_dgst[i], hdata, hdatalen);
320 } else {
321 s->s3->handshake_dgst[i] = NULL;
322 }
323 }
Adam Langley95c29f32014-06-20 12:00:00 -0700324
Adam Langleybe2900a2014-12-18 12:09:04 -0800325 if (should_free_handshake_buffer == free_handshake_buffer) {
326 /* Free handshake_buffer BIO */
327 BIO_free(s->s3->handshake_buffer);
328 s->s3->handshake_buffer = NULL;
329 }
Adam Langley0fbf33a2014-06-20 12:00:00 -0700330
Adam Langleybe2900a2014-12-18 12:09:04 -0800331 return 1;
332}
Adam Langley0fbf33a2014-06-20 12:00:00 -0700333
Adam Langleybe2900a2014-12-18 12:09:04 -0800334int ssl3_cert_verify_mac(SSL *s, int md_nid, uint8_t *p) {
335 return ssl3_handshake_mac(s, md_nid, NULL, 0, p);
336}
Adam Langley0fbf33a2014-06-20 12:00:00 -0700337
Adam Langleybe2900a2014-12-18 12:09:04 -0800338int ssl3_final_finish_mac(SSL *s, const char *sender, int len, uint8_t *p) {
339 int ret, sha1len;
340 ret = ssl3_handshake_mac(s, NID_md5, sender, len, p);
341 if (ret == 0) {
342 return 0;
343 }
Adam Langley95c29f32014-06-20 12:00:00 -0700344
Adam Langleybe2900a2014-12-18 12:09:04 -0800345 p += ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700346
Adam Langleybe2900a2014-12-18 12:09:04 -0800347 sha1len = ssl3_handshake_mac(s, NID_sha1, sender, len, p);
348 if (sha1len == 0) {
349 return 0;
350 }
Adam Langley95c29f32014-06-20 12:00:00 -0700351
Adam Langleybe2900a2014-12-18 12:09:04 -0800352 ret += sha1len;
353 return ret;
354}
Adam Langley95c29f32014-06-20 12:00:00 -0700355
Adam Langleybe2900a2014-12-18 12:09:04 -0800356static int ssl3_handshake_mac(SSL *s, int md_nid, const char *sender, int len,
357 uint8_t *p) {
358 unsigned int ret;
359 int npad, n;
360 unsigned int i;
361 uint8_t md_buf[EVP_MAX_MD_SIZE];
362 EVP_MD_CTX ctx, *d = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langleybe2900a2014-12-18 12:09:04 -0800364 if (s->s3->handshake_buffer &&
365 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
366 return 0;
367 }
Adam Langley95c29f32014-06-20 12:00:00 -0700368
Adam Langleybe2900a2014-12-18 12:09:04 -0800369 /* Search for digest of specified type in the handshake_dgst array. */
370 for (i = 0; i < SSL_MAX_DIGEST; i++) {
371 if (s->s3->handshake_dgst[i] &&
372 EVP_MD_CTX_type(s->s3->handshake_dgst[i]) == md_nid) {
373 d = s->s3->handshake_dgst[i];
374 break;
375 }
376 }
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleybe2900a2014-12-18 12:09:04 -0800378 if (!d) {
379 OPENSSL_PUT_ERROR(SSL, ssl3_handshake_mac, SSL_R_NO_REQUIRED_DIGEST);
380 return 0;
381 }
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langleybe2900a2014-12-18 12:09:04 -0800383 EVP_MD_CTX_init(&ctx);
384 if (!EVP_MD_CTX_copy_ex(&ctx, d)) {
385 EVP_MD_CTX_cleanup(&ctx);
David Benjaminefec1932015-02-09 22:03:21 -0500386 OPENSSL_PUT_ERROR(SSL, ssl3_handshake_mac, ERR_LIB_EVP);
Adam Langleybe2900a2014-12-18 12:09:04 -0800387 return 0;
388 }
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langleybe2900a2014-12-18 12:09:04 -0800390 n = EVP_MD_CTX_size(&ctx);
391 if (n < 0) {
392 return 0;
393 }
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langleybe2900a2014-12-18 12:09:04 -0800395 npad = (48 / n) * n;
396 if (sender != NULL) {
397 EVP_DigestUpdate(&ctx, sender, len);
398 }
399 EVP_DigestUpdate(&ctx, s->session->master_key, s->session->master_key_length);
400 EVP_DigestUpdate(&ctx, ssl3_pad_1, npad);
401 EVP_DigestFinal_ex(&ctx, md_buf, &i);
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleybe2900a2014-12-18 12:09:04 -0800403 if (!EVP_DigestInit_ex(&ctx, EVP_MD_CTX_md(&ctx), NULL)) {
404 EVP_MD_CTX_cleanup(&ctx);
David Benjaminefec1932015-02-09 22:03:21 -0500405 OPENSSL_PUT_ERROR(SSL, ssl3_handshake_mac, ERR_LIB_EVP);
Adam Langleybe2900a2014-12-18 12:09:04 -0800406 return 0;
407 }
408 EVP_DigestUpdate(&ctx, s->session->master_key, s->session->master_key_length);
409 EVP_DigestUpdate(&ctx, ssl3_pad_2, npad);
410 EVP_DigestUpdate(&ctx, md_buf, i);
411 EVP_DigestFinal_ex(&ctx, p, &ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Adam Langleybe2900a2014-12-18 12:09:04 -0800413 EVP_MD_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langleybe2900a2014-12-18 12:09:04 -0800415 return ret;
416}
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langleybe2900a2014-12-18 12:09:04 -0800418void ssl3_record_sequence_update(uint8_t *seq) {
419 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700420
Adam Langleybe2900a2014-12-18 12:09:04 -0800421 for (i = 7; i >= 0; i--) {
422 ++seq[i];
423 if (seq[i] != 0) {
424 break;
425 }
426 }
427}
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langleybe2900a2014-12-18 12:09:04 -0800429int ssl3_alert_code(int code) {
430 switch (code) {
431 case SSL_AD_CLOSE_NOTIFY:
432 return SSL3_AD_CLOSE_NOTIFY;
433
434 case SSL_AD_UNEXPECTED_MESSAGE:
435 return SSL3_AD_UNEXPECTED_MESSAGE;
436
437 case SSL_AD_BAD_RECORD_MAC:
438 return SSL3_AD_BAD_RECORD_MAC;
439
440 case SSL_AD_DECRYPTION_FAILED:
441 return SSL3_AD_BAD_RECORD_MAC;
442
443 case SSL_AD_RECORD_OVERFLOW:
444 return SSL3_AD_BAD_RECORD_MAC;
445
446 case SSL_AD_DECOMPRESSION_FAILURE:
447 return SSL3_AD_DECOMPRESSION_FAILURE;
448
449 case SSL_AD_HANDSHAKE_FAILURE:
450 return SSL3_AD_HANDSHAKE_FAILURE;
451
452 case SSL_AD_NO_CERTIFICATE:
453 return SSL3_AD_NO_CERTIFICATE;
454
455 case SSL_AD_BAD_CERTIFICATE:
456 return SSL3_AD_BAD_CERTIFICATE;
457
458 case SSL_AD_UNSUPPORTED_CERTIFICATE:
459 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
460
461 case SSL_AD_CERTIFICATE_REVOKED:
462 return SSL3_AD_CERTIFICATE_REVOKED;
463
464 case SSL_AD_CERTIFICATE_EXPIRED:
465 return SSL3_AD_CERTIFICATE_EXPIRED;
466
467 case SSL_AD_CERTIFICATE_UNKNOWN:
468 return SSL3_AD_CERTIFICATE_UNKNOWN;
469
470 case SSL_AD_ILLEGAL_PARAMETER:
471 return SSL3_AD_ILLEGAL_PARAMETER;
472
473 case SSL_AD_UNKNOWN_CA:
474 return SSL3_AD_BAD_CERTIFICATE;
475
476 case SSL_AD_ACCESS_DENIED:
477 return SSL3_AD_HANDSHAKE_FAILURE;
478
479 case SSL_AD_DECODE_ERROR:
480 return SSL3_AD_HANDSHAKE_FAILURE;
481
482 case SSL_AD_DECRYPT_ERROR:
483 return SSL3_AD_HANDSHAKE_FAILURE;
484
485 case SSL_AD_EXPORT_RESTRICTION:
486 return SSL3_AD_HANDSHAKE_FAILURE;
487
488 case SSL_AD_PROTOCOL_VERSION:
489 return SSL3_AD_HANDSHAKE_FAILURE;
490
491 case SSL_AD_INSUFFICIENT_SECURITY:
492 return SSL3_AD_HANDSHAKE_FAILURE;
493
494 case SSL_AD_INTERNAL_ERROR:
495 return SSL3_AD_HANDSHAKE_FAILURE;
496
497 case SSL_AD_USER_CANCELLED:
498 return SSL3_AD_HANDSHAKE_FAILURE;
499
500 case SSL_AD_NO_RENEGOTIATION:
501 return -1; /* Don't send it. */
502
503 case SSL_AD_UNSUPPORTED_EXTENSION:
504 return SSL3_AD_HANDSHAKE_FAILURE;
505
506 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
507 return SSL3_AD_HANDSHAKE_FAILURE;
508
509 case SSL_AD_UNRECOGNIZED_NAME:
510 return SSL3_AD_HANDSHAKE_FAILURE;
511
512 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
513 return SSL3_AD_HANDSHAKE_FAILURE;
514
515 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
516 return SSL3_AD_HANDSHAKE_FAILURE;
517
518 case SSL_AD_UNKNOWN_PSK_IDENTITY:
519 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
520
521 case SSL_AD_INAPPROPRIATE_FALLBACK:
522 return SSL3_AD_INAPPROPRIATE_FALLBACK;
523
524 default:
525 return -1;
526 }
527}