blob: 1309a18639751289e38fac37bd479a4aa54e79b8 [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 *
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 *
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 *
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) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
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 *
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.
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 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
114
David Benjamin443a1f62015-09-04 15:05:05 -0400115#include <openssl/ssl.h>
116
Steven Valdezbf5aa842016-07-15 07:07:40 -0400117#include <assert.h>
Steven Valdeza833c352016-11-01 13:39:36 -0400118#include <limits.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400119#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120
Adam Langley95c29f32014-06-20 12:00:00 -0700121#include <openssl/bn.h>
David Benjamin676d1e72014-07-08 14:34:10 -0400122#include <openssl/buf.h>
Adam Langleyd5157222016-12-12 11:37:43 -0800123#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700124#include <openssl/dh.h>
Adam Langleyd5157222016-12-12 11:37:43 -0800125#include <openssl/ec_key.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700126#include <openssl/err.h>
127#include <openssl/mem.h>
David Benjamin5c900c82016-07-13 23:03:26 -0400128#include <openssl/sha.h>
David Benjamin680ca962015-06-18 12:37:23 -0400129#include <openssl/x509.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700130#include <openssl/x509v3.h>
131
Adam Langley0da323a2015-05-15 12:49:30 -0700132#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400133#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700134
Adam Langley95c29f32014-06-20 12:00:00 -0700135
Adam Langley3509dac2017-02-01 11:59:18 -0800136CERT *ssl_cert_new(const SSL_X509_METHOD *x509_method) {
Brian Smith5ba06892016-02-07 09:36:04 -1000137 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800138 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400139 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800140 return NULL;
141 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500142 OPENSSL_memset(ret, 0, sizeof(CERT));
Adam Langley3509dac2017-02-01 11:59:18 -0800143 ret->x509_method = x509_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700144
Adam Langleyfcf25832014-12-18 17:42:32 -0800145 return ret;
146}
Adam Langley95c29f32014-06-20 12:00:00 -0700147
Adam Langley3a2b47a2017-01-24 13:59:42 -0800148static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) {
149 CRYPTO_BUFFER_up_ref(buffer);
150 return buffer;
151}
152
Adam Langleyfcf25832014-12-18 17:42:32 -0800153CERT *ssl_cert_dup(CERT *cert) {
Brian Smith5ba06892016-02-07 09:36:04 -1000154 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400156 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800157 return NULL;
158 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500159 OPENSSL_memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700160
Adam Langley3a2b47a2017-01-24 13:59:42 -0800161 ret->chain = sk_CRYPTO_BUFFER_deep_copy(cert->chain, buffer_up_ref,
162 CRYPTO_BUFFER_free);
Adam Langley95c29f32014-06-20 12:00:00 -0700163
David Benjamind1d80782015-07-05 11:54:09 -0400164 if (cert->privatekey != NULL) {
Adam Langley310d3f62016-07-12 10:39:20 -0700165 EVP_PKEY_up_ref(cert->privatekey);
166 ret->privatekey = cert->privatekey;
David Benjamind1d80782015-07-05 11:54:09 -0400167 }
Adam Langley95c29f32014-06-20 12:00:00 -0700168
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030169 ret->key_method = cert->key_method;
Adam Langley3509dac2017-02-01 11:59:18 -0800170 ret->x509_method = cert->x509_method;
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030171
David Benjamin0fc37ef2016-08-17 15:29:46 -0400172 if (cert->dh_tmp != NULL) {
173 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
174 if (ret->dh_tmp == NULL) {
175 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
176 goto err;
177 }
178 }
179 ret->dh_tmp_cb = cert->dh_tmp_cb;
180
181 if (cert->sigalgs != NULL) {
182 ret->sigalgs =
183 BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
184 if (ret->sigalgs == NULL) {
185 goto err;
186 }
187 }
188 ret->num_sigalgs = cert->num_sigalgs;
189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 ret->cert_cb = cert->cert_cb;
191 ret->cert_cb_arg = cert->cert_cb_arg;
192
Adam Langley2a3b3432017-02-10 13:56:48 -0800193 ret->x509_method->cert_dup(ret, cert);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800194
David Benjamin83a32122017-02-14 18:34:54 -0500195 if (cert->signed_cert_timestamp_list != NULL) {
196 CRYPTO_BUFFER_up_ref(cert->signed_cert_timestamp_list);
197 ret->signed_cert_timestamp_list = cert->signed_cert_timestamp_list;
198 }
199
200 if (cert->ocsp_response != NULL) {
201 CRYPTO_BUFFER_up_ref(cert->ocsp_response);
202 ret->ocsp_response = cert->ocsp_response;
203 }
204
David Benjamin5960a902017-02-14 20:07:11 -0500205 ret->sid_ctx_length = cert->sid_ctx_length;
206 OPENSSL_memcpy(ret->sid_ctx, cert->sid_ctx, sizeof(ret->sid_ctx));
207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 return ret;
209
Adam Langley95c29f32014-06-20 12:00:00 -0700210err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800211 ssl_cert_free(ret);
212 return NULL;
213}
Adam Langley95c29f32014-06-20 12:00:00 -0700214
215/* Free up and clear all certificates and chains */
David Benjamind1d80782015-07-05 11:54:09 -0400216void ssl_cert_clear_certs(CERT *cert) {
217 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 return;
219 }
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langley3509dac2017-02-01 11:59:18 -0800221 cert->x509_method->cert_clear(cert);
Adam Langleye1e78132017-01-31 15:24:31 -0800222
Adam Langley3a2b47a2017-01-24 13:59:42 -0800223 sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
224 cert->chain = NULL;
David Benjamind1d80782015-07-05 11:54:09 -0400225 EVP_PKEY_free(cert->privatekey);
226 cert->privatekey = NULL;
David Benjamin71d2e542015-07-06 00:30:25 -0400227 cert->key_method = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800228}
Adam Langley95c29f32014-06-20 12:00:00 -0700229
Adam Langleyfcf25832014-12-18 17:42:32 -0800230void ssl_cert_free(CERT *c) {
231 if (c == NULL) {
232 return;
233 }
Adam Langley95c29f32014-06-20 12:00:00 -0700234
David Benjamin2755a3e2015-04-22 16:17:58 -0400235 DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700236
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 ssl_cert_clear_certs(c);
Adam Langley2a3b3432017-02-10 13:56:48 -0800238 c->x509_method->cert_free(c);
David Benjamind246b812016-07-08 15:07:02 -0700239 OPENSSL_free(c->sigalgs);
David Benjamin83a32122017-02-14 18:34:54 -0500240 CRYPTO_BUFFER_free(c->signed_cert_timestamp_list);
241 CRYPTO_BUFFER_free(c->ocsp_response);
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 OPENSSL_free(c);
244}
Adam Langley95c29f32014-06-20 12:00:00 -0700245
David Benjamina2bda9f2016-12-03 23:29:05 -0500246static void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg),
247 void *arg) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 c->cert_cb = cb;
249 c->cert_cb_arg = arg;
250}
Adam Langley95c29f32014-06-20 12:00:00 -0700251
Adam Langley52940c42017-02-01 12:40:31 -0800252int ssl_set_cert(CERT *cert, CRYPTO_BUFFER *buffer) {
253 CBS cert_cbs;
254 CRYPTO_BUFFER_init_CBS(buffer, &cert_cbs);
255 EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs);
256 if (pubkey == NULL) {
257 return 0;
258 }
259
260 if (!ssl_is_key_type_supported(pubkey->type)) {
261 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
262 EVP_PKEY_free(pubkey);
263 return 0;
264 }
265
266 /* An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA
267 * certificates, so sanity-check the key usage extension. */
268 if (pubkey->type == EVP_PKEY_EC &&
269 !ssl_cert_check_digital_signature_key_usage(&cert_cbs)) {
270 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
271 EVP_PKEY_free(pubkey);
272 return 0;
273 }
274
275 if (cert->privatekey != NULL) {
276 /* Sanity-check that the private key and the certificate match, unless the
277 * key is opaque (in case of, say, a smartcard). */
278 if (!EVP_PKEY_is_opaque(cert->privatekey) &&
279 !ssl_compare_public_and_private_key(pubkey, cert->privatekey)) {
280 /* don't fail for a cert/key mismatch, just free current private key
281 * (when switching to a different cert & key, first this function should
282 * be used, then ssl_set_pkey */
283 EVP_PKEY_free(cert->privatekey);
284 cert->privatekey = NULL;
285 /* clear error queue */
286 ERR_clear_error();
287 }
288 }
289
290 EVP_PKEY_free(pubkey);
291
292 cert->x509_method->cert_flush_cached_leaf(cert);
293
294 if (cert->chain != NULL) {
295 CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain, 0));
296 sk_CRYPTO_BUFFER_set(cert->chain, 0, buffer);
297 CRYPTO_BUFFER_up_ref(buffer);
298 return 1;
299 }
300
301 cert->chain = sk_CRYPTO_BUFFER_new_null();
302 if (cert->chain == NULL) {
303 return 0;
304 }
305
306 if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
307 sk_CRYPTO_BUFFER_free(cert->chain);
308 cert->chain = NULL;
309 return 0;
310 }
311 CRYPTO_BUFFER_up_ref(buffer);
312
313 return 1;
314}
315
316int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
317 const uint8_t *der) {
318 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL);
319 if (buffer == NULL) {
320 return 0;
321 }
322
323 const int ok = ssl_set_cert(ctx->cert, buffer);
324 CRYPTO_BUFFER_free(buffer);
325 return ok;
326}
327
328int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
329 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL);
330 if (buffer == NULL) {
331 return 0;
332 }
333
334 const int ok = ssl_set_cert(ssl->cert, buffer);
335 CRYPTO_BUFFER_free(buffer);
336 return ok;
337}
338
David Benjamin32a66d52016-07-13 22:03:11 -0400339int ssl_has_certificate(const SSL *ssl) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800340 return ssl->cert->chain != NULL &&
341 sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0) != NULL &&
342 ssl_has_private_key(ssl);
David Benjamin32a66d52016-07-13 22:03:11 -0400343}
344
Adam Langley68e71242016-12-12 11:06:16 -0800345STACK_OF(CRYPTO_BUFFER) *ssl_parse_cert_chain(uint8_t *out_alert,
Adam Langleyd5157222016-12-12 11:37:43 -0800346 EVP_PKEY **out_pubkey,
Adam Langley68e71242016-12-12 11:06:16 -0800347 uint8_t *out_leaf_sha256,
Adam Langleyd519bf62016-12-12 11:16:44 -0800348 CBS *cbs,
349 CRYPTO_BUFFER_POOL *pool) {
Adam Langleyd5157222016-12-12 11:37:43 -0800350 *out_pubkey = NULL;
351
Adam Langley68e71242016-12-12 11:06:16 -0800352 STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null();
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800353 if (ret == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -0400354 *out_alert = SSL_AD_INTERNAL_ERROR;
355 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800356 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400357 }
358
David Benjamin5c900c82016-07-13 23:03:26 -0400359 CBS certificate_list;
360 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
361 *out_alert = SSL_AD_DECODE_ERROR;
362 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
363 goto err;
364 }
365
366 while (CBS_len(&certificate_list) > 0) {
367 CBS certificate;
Adam Langley68e71242016-12-12 11:06:16 -0800368 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
369 CBS_len(&certificate) == 0) {
David Benjamin5c900c82016-07-13 23:03:26 -0400370 *out_alert = SSL_AD_DECODE_ERROR;
371 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
372 goto err;
373 }
374
Adam Langleyd5157222016-12-12 11:37:43 -0800375 if (sk_CRYPTO_BUFFER_num(ret) == 0) {
376 *out_pubkey = ssl_cert_parse_pubkey(&certificate);
377 if (*out_pubkey == NULL) {
Adam Langley7dccc712017-02-08 08:51:25 -0800378 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyd5157222016-12-12 11:37:43 -0800379 goto err;
380 }
381
382 /* Retain the hash of the leaf certificate if requested. */
383 if (out_leaf_sha256 != NULL) {
384 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
385 }
David Benjamin5c900c82016-07-13 23:03:26 -0400386 }
387
Adam Langleyd519bf62016-12-12 11:16:44 -0800388 CRYPTO_BUFFER *buf =
389 CRYPTO_BUFFER_new_from_CBS(&certificate, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800390 if (buf == NULL) {
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800391 *out_alert = SSL_AD_DECODE_ERROR;
392 goto err;
393 }
Adam Langley68e71242016-12-12 11:06:16 -0800394
395 if (!sk_CRYPTO_BUFFER_push(ret, buf)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400396 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley68e71242016-12-12 11:06:16 -0800397 CRYPTO_BUFFER_free(buf);
David Benjamin5c900c82016-07-13 23:03:26 -0400398 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
399 goto err;
400 }
David Benjamin5c900c82016-07-13 23:03:26 -0400401 }
402
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800403 return ret;
David Benjamin5c900c82016-07-13 23:03:26 -0400404
405err:
Adam Langleyd5157222016-12-12 11:37:43 -0800406 EVP_PKEY_free(*out_pubkey);
407 *out_pubkey = NULL;
Adam Langley68e71242016-12-12 11:06:16 -0800408 sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800409 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400410}
411
David Benjamin75836432016-06-17 18:48:29 -0400412int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400413 if (!ssl_has_certificate(ssl)) {
414 return CBB_add_u24(cbb, 0);
415 }
416
Adam Langley3a2b47a2017-01-24 13:59:42 -0800417 CBB certs;
418 if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
419 goto err;
David Benjamin75836432016-06-17 18:48:29 -0400420 }
421
Adam Langley3a2b47a2017-01-24 13:59:42 -0800422 STACK_OF(CRYPTO_BUFFER) *chain = ssl->cert->chain;
423 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
424 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
425 CBB child;
426 if (!CBB_add_u24_length_prefixed(&certs, &child) ||
427 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
428 CRYPTO_BUFFER_len(buffer)) ||
429 !CBB_flush(&certs)) {
430 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800431 }
432 }
433
David Benjamin75836432016-06-17 18:48:29 -0400434 return CBB_flush(cbb);
Adam Langley3a2b47a2017-01-24 13:59:42 -0800435
436err:
437 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
438 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800439}
Adam Langley95c29f32014-06-20 12:00:00 -0700440
Adam Langley05672202016-12-13 12:05:49 -0800441/* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
442 * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
443 * subjectPublicKeyInfo. */
444static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
Adam Langleyd5157222016-12-12 11:37:43 -0800445 /* From RFC 5280, section 4.1
446 * Certificate ::= SEQUENCE {
447 * tbsCertificate TBSCertificate,
448 * signatureAlgorithm AlgorithmIdentifier,
449 * signatureValue BIT STRING }
450
451 * TBSCertificate ::= SEQUENCE {
452 * version [0] EXPLICIT Version DEFAULT v1,
453 * serialNumber CertificateSerialNumber,
454 * signature AlgorithmIdentifier,
455 * issuer Name,
456 * validity Validity,
457 * subject Name,
458 * subjectPublicKeyInfo SubjectPublicKeyInfo,
459 * ... } */
460 CBS buf = *in;
461
Adam Langley05672202016-12-13 12:05:49 -0800462 CBS toplevel;
Adam Langleyd5157222016-12-12 11:37:43 -0800463 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
464 CBS_len(&buf) != 0 ||
Adam Langley05672202016-12-13 12:05:49 -0800465 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800466 /* version */
467 !CBS_get_optional_asn1(
Adam Langley05672202016-12-13 12:05:49 -0800468 out_tbs_cert, NULL, NULL,
Adam Langleyd5157222016-12-12 11:37:43 -0800469 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
470 /* serialNumber */
Adam Langley05672202016-12-13 12:05:49 -0800471 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800472 /* signature algorithm */
Adam Langley05672202016-12-13 12:05:49 -0800473 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800474 /* issuer */
Adam Langley05672202016-12-13 12:05:49 -0800475 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800476 /* validity */
Adam Langley05672202016-12-13 12:05:49 -0800477 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800478 /* subject */
Adam Langley05672202016-12-13 12:05:49 -0800479 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
480 return 0;
481 }
482
483 return 1;
484}
485
486EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in) {
David Benjamin5b410b62017-01-03 08:08:57 -0500487 CBS buf = *in, tbs_cert;
488 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
Adam Langleyd5157222016-12-12 11:37:43 -0800489 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
490 return NULL;
491 }
492
David Benjamin5b410b62017-01-03 08:08:57 -0500493 return EVP_parse_public_key(&tbs_cert);
Adam Langleyd5157222016-12-12 11:37:43 -0800494}
495
Adam Langleyc26692c2017-01-25 09:34:42 -0800496int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
497 const EVP_PKEY *privkey) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800498 int ret = 0;
499
500 switch (EVP_PKEY_cmp(pubkey, privkey)) {
501 case 1:
502 ret = 1;
503 break;
504 case 0:
505 OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
506 break;
507 case -1:
508 OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
509 break;
510 case -2:
511 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
512 default:
513 assert(0);
514 break;
515 }
516
Adam Langley3a2b47a2017-01-24 13:59:42 -0800517 return ret;
518}
519
520int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
521 if (privkey == NULL) {
522 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
523 return 0;
524 }
525
526 if (cert->chain == NULL ||
527 sk_CRYPTO_BUFFER_value(cert->chain, 0) == NULL) {
528 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
529 return 0;
530 }
531
Adam Langleyc26692c2017-01-25 09:34:42 -0800532 CBS cert_cbs;
533 CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain, 0), &cert_cbs);
534 EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs);
535 if (!pubkey) {
536 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
537 return 0;
538 }
539
540 const int ok = ssl_compare_public_and_private_key(pubkey, privkey);
541 EVP_PKEY_free(pubkey);
542 return ok;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800543}
544
Adam Langley05672202016-12-13 12:05:49 -0800545int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
546 CBS buf = *in;
547
548 CBS tbs_cert, outer_extensions;
549 int has_extensions;
550 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
551 /* subjectPublicKeyInfo */
552 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
553 /* issuerUniqueID */
554 !CBS_get_optional_asn1(
555 &tbs_cert, NULL, NULL,
556 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
557 /* subjectUniqueID */
558 !CBS_get_optional_asn1(
559 &tbs_cert, NULL, NULL,
560 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
561 !CBS_get_optional_asn1(
562 &tbs_cert, &outer_extensions, &has_extensions,
563 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
564 goto parse_err;
565 }
566
567 if (!has_extensions) {
568 return 1;
569 }
570
571 CBS extensions;
572 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
573 goto parse_err;
574 }
575
576 while (CBS_len(&extensions) > 0) {
577 CBS extension, oid, contents;
578 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
579 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
580 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
581 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
582 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
583 CBS_len(&extension) != 0) {
584 goto parse_err;
585 }
586
587 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
588 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500589 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
590 0) {
Adam Langley05672202016-12-13 12:05:49 -0800591 continue;
592 }
593
594 CBS bit_string;
595 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
596 CBS_len(&contents) != 0) {
597 goto parse_err;
598 }
599
600 /* This is the KeyUsage extension. See
601 * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */
602 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
603 goto parse_err;
604 }
605
606 if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
607 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
608 return 0;
609 }
610
611 return 1;
612 }
613
614 /* No KeyUsage extension found. */
615 return 1;
616
617parse_err:
618 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
619 return 0;
620}
621
Adam Langley34b4c822017-02-02 10:57:17 -0800622STACK_OF(CRYPTO_BUFFER) *
David Benjamine0332e82016-07-13 22:40:36 -0400623 ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
Adam Langley34b4c822017-02-02 10:57:17 -0800624 CRYPTO_BUFFER_POOL *const pool = ssl->ctx->pool;
625
626 STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null();
David Benjamine0332e82016-07-13 22:40:36 -0400627 if (ret == NULL) {
628 *out_alert = SSL_AD_INTERNAL_ERROR;
629 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
630 return NULL;
631 }
632
633 CBS child;
634 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
635 *out_alert = SSL_AD_DECODE_ERROR;
636 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
637 goto err;
638 }
639
640 while (CBS_len(&child) > 0) {
641 CBS distinguished_name;
642 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
643 *out_alert = SSL_AD_DECODE_ERROR;
644 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
645 goto err;
646 }
647
Adam Langley34b4c822017-02-02 10:57:17 -0800648 CRYPTO_BUFFER *buffer =
649 CRYPTO_BUFFER_new_from_CBS(&distinguished_name, pool);
650 if (buffer == NULL ||
651 !sk_CRYPTO_BUFFER_push(ret, buffer)) {
652 CRYPTO_BUFFER_free(buffer);
David Benjamine0332e82016-07-13 22:40:36 -0400653 *out_alert = SSL_AD_INTERNAL_ERROR;
654 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
655 goto err;
656 }
David Benjamine0332e82016-07-13 22:40:36 -0400657 }
658
Adam Langley0bdef092017-02-23 15:02:58 -0800659 if (!ssl->ctx->x509_method->check_client_CA_list(ret)) {
660 *out_alert = SSL_AD_INTERNAL_ERROR;
661 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
662 goto err;
663 }
664
David Benjamine0332e82016-07-13 22:40:36 -0400665 return ret;
666
667err:
Adam Langley34b4c822017-02-02 10:57:17 -0800668 sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free);
David Benjamine0332e82016-07-13 22:40:36 -0400669 return NULL;
670}
671
David Benjamin32a66d52016-07-13 22:03:11 -0400672int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
673 CBB child, name_cbb;
674 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
675 return 0;
676 }
677
Adam Langley34b4c822017-02-02 10:57:17 -0800678 STACK_OF(CRYPTO_BUFFER) *names = ssl->client_CA;
679 if (names == NULL) {
680 names = ssl->ctx->client_CA;
681 }
682 if (names == NULL) {
David Benjamin32a66d52016-07-13 22:03:11 -0400683 return CBB_flush(cbb);
684 }
685
Adam Langley34b4c822017-02-02 10:57:17 -0800686 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(names); i++) {
687 const CRYPTO_BUFFER *name = sk_CRYPTO_BUFFER_value(names, i);
688
David Benjamin32a66d52016-07-13 22:03:11 -0400689 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
Adam Langley34b4c822017-02-02 10:57:17 -0800690 !CBB_add_bytes(&name_cbb, CRYPTO_BUFFER_data(name),
691 CRYPTO_BUFFER_len(name))) {
David Benjamin32a66d52016-07-13 22:03:11 -0400692 return 0;
693 }
694 }
695
696 return CBB_flush(cbb);
697}
698
David Benjamina2bda9f2016-12-03 23:29:05 -0500699void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
700 void *arg) {
701 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
702}
703
704void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
705 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
706}
707
David Benjaminf4654612017-03-02 18:47:12 -0500708STACK_OF(CRYPTO_BUFFER) *SSL_get0_peer_certificates(const SSL *ssl) {
709 SSL_SESSION *session = SSL_get_session(ssl);
710 if (session == NULL) {
711 return NULL;
712 }
713
714 return session->certs;
715}
716
Adam Langleyd6c22ee2017-03-02 12:56:32 -0800717STACK_OF(CRYPTO_BUFFER) *SSL_get0_server_requested_CAs(const SSL *ssl) {
718 if (ssl->s3->hs == NULL) {
719 return NULL;
720 }
721 return ssl->s3->hs->ca_names;
722}
723
David Benjamin45738dd2017-02-09 20:01:26 -0500724int ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
Adam Langley05672202016-12-13 12:05:49 -0800725 const CRYPTO_BUFFER *leaf) {
David Benjamin45738dd2017-02-09 20:01:26 -0500726 SSL *const ssl = hs->ssl;
David Benjamin938fa7c2016-10-07 00:27:05 -0400727 assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
728
Steven Valdezbf5aa842016-07-15 07:07:40 -0400729 /* Check the certificate's type matches the cipher. */
David Benjamin45738dd2017-02-09 20:01:26 -0500730 int expected_type = ssl_cipher_get_key_type(hs->new_cipher);
Steven Valdezbf5aa842016-07-15 07:07:40 -0400731 assert(expected_type != EVP_PKEY_NONE);
732 if (pkey->type != expected_type) {
733 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
Adam Langley05672202016-12-13 12:05:49 -0800734 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400735 }
736
David Benjamin45738dd2017-02-09 20:01:26 -0500737 if (hs->new_cipher->algorithm_auth & SSL_aECDSA) {
Adam Langley05672202016-12-13 12:05:49 -0800738 CBS leaf_cbs;
739 CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
740 /* ECDSA and ECDH certificates use the same public key format. Instead,
741 * they are distinguished by the key usage extension in the certificate. */
742 if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
743 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400744 }
745
David Benjamin938fa7c2016-10-07 00:27:05 -0400746 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
747 if (ec_key == NULL) {
748 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -0800749 return 0;
David Benjamin938fa7c2016-10-07 00:27:05 -0400750 }
751
752 /* Check the key's group and point format are acceptable. */
753 uint16_t group_id;
754 if (!ssl_nid_to_group_id(
755 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
756 !tls1_check_group_id(ssl, group_id) ||
757 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
Steven Valdezbf5aa842016-07-15 07:07:40 -0400758 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -0800759 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400760 }
761 }
762
Adam Langley05672202016-12-13 12:05:49 -0800763 return 1;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400764}
David Benjamin5edfc8c2016-12-10 15:46:58 -0500765
David Benjamin83a32122017-02-14 18:34:54 -0500766static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list,
767 size_t list_len) {
768 CBS sct_list;
769 CBS_init(&sct_list, list, list_len);
770 if (!ssl_is_sct_list_valid(&sct_list)) {
771 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
772 return 0;
773 }
774
775 CRYPTO_BUFFER_free(cert->signed_cert_timestamp_list);
776 cert->signed_cert_timestamp_list =
777 CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), NULL);
778 return cert->signed_cert_timestamp_list != NULL;
779}
780
781int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
782 size_t list_len) {
783 return set_signed_cert_timestamp_list(ctx->cert, list, list_len);
784}
785
786int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list,
787 size_t list_len) {
788 return set_signed_cert_timestamp_list(ssl->cert, list, list_len);
789}
790
791int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
792 size_t response_len) {
793 CRYPTO_BUFFER_free(ctx->cert->ocsp_response);
794 ctx->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
795 return ctx->cert->ocsp_response != NULL;
796}
797
798int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response,
799 size_t response_len) {
800 CRYPTO_BUFFER_free(ssl->cert->ocsp_response);
801 ssl->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
802 return ssl->cert->ocsp_response != NULL;
803}