Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 8 | */ |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 9 | |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 10 | /* ==================================================================== |
| 11 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 12 | * ECC cipher suite support in OpenSSL originally developed by |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 13 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 14 | */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 15 | |
| 16 | #include <stdio.h> |
Andy Polyakov | 17f389b | 1999-09-11 17:54:18 +0000 | [diff] [blame] | 17 | |
Richard Levitte | 41d2a33 | 2001-02-22 14:45:02 +0000 | [diff] [blame] | 18 | #include "e_os.h" |
Andy Polyakov | 17f389b | 1999-09-11 17:54:18 +0000 | [diff] [blame] | 19 | #ifndef NO_SYS_TYPES_H |
| 20 | # include <sys/types.h> |
| 21 | #endif |
| 22 | |
Richard Levitte | 6857079 | 2015-05-14 14:54:49 +0200 | [diff] [blame] | 23 | #include "internal/o_dir.h" |
Toshikuni Fukaya | 7823d79 | 2016-02-03 13:08:45 -0500 | [diff] [blame] | 24 | #include <openssl/lhash.h> |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 25 | #include <openssl/bio.h> |
| 26 | #include <openssl/pem.h> |
Dr. Stephen Henson | bb7cd4e | 1999-11-29 22:35:00 +0000 | [diff] [blame] | 27 | #include <openssl/x509v3.h> |
Rich Salz | 3c27208 | 2016-03-18 14:30:20 -0400 | [diff] [blame] | 28 | #include <openssl/dh.h> |
Geoff Thorpe | d095b68 | 2004-05-17 18:53:47 +0000 | [diff] [blame] | 29 | #include <openssl/bn.h> |
Viktor Dukhovni | 5c4328f | 2016-05-15 13:02:17 -0400 | [diff] [blame] | 30 | #include <openssl/crypto.h> |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 31 | #include "ssl_locl.h" |
| 32 | |
Kurt Roeckx | e4646a8 | 2016-02-07 20:44:27 +0100 | [diff] [blame] | 33 | static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx, int op, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 34 | int bits, int nid, void *other, |
| 35 | void *ex); |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 36 | |
Alessandro Ghedini | 16203f7 | 2016-02-29 17:26:07 +0000 | [diff] [blame] | 37 | static CRYPTO_ONCE ssl_x509_store_ctx_once = CRYPTO_ONCE_STATIC_INIT; |
| 38 | static volatile int ssl_x509_store_ctx_idx = -1; |
| 39 | |
| 40 | static void ssl_x509_store_ctx_init(void) |
| 41 | { |
| 42 | ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0, |
| 43 | "SSL for verify callback", |
| 44 | NULL, NULL, NULL); |
| 45 | } |
| 46 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 47 | int SSL_get_ex_data_X509_STORE_CTX_idx(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 48 | { |
Bodo Möller | 675f605 | 2006-06-14 08:55:23 +0000 | [diff] [blame] | 49 | |
Alessandro Ghedini | 16203f7 | 2016-02-29 17:26:07 +0000 | [diff] [blame] | 50 | CRYPTO_THREAD_run_once(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 51 | return ssl_x509_store_ctx_idx; |
| 52 | } |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 53 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 54 | CERT *ssl_cert_new(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 55 | { |
Rich Salz | b51bce9 | 2015-08-25 13:25:58 -0400 | [diff] [blame] | 56 | CERT *ret = OPENSSL_zalloc(sizeof(*ret)); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 57 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 58 | if (ret == NULL) { |
| 59 | SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE); |
Alessandro Ghedini | 16203f7 | 2016-02-29 17:26:07 +0000 | [diff] [blame] | 60 | return NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 61 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 62 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 63 | ret->key = &(ret->pkeys[SSL_PKEY_RSA_ENC]); |
| 64 | ret->references = 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 65 | ret->sec_cb = ssl_security_default_callback; |
| 66 | ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL; |
| 67 | ret->sec_ex = NULL; |
Alessandro Ghedini | 16203f7 | 2016-02-29 17:26:07 +0000 | [diff] [blame] | 68 | ret->lock = CRYPTO_THREAD_lock_new(); |
| 69 | if (ret->lock == NULL) { |
| 70 | SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE); |
| 71 | OPENSSL_free(ret); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 76 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 77 | |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 78 | CERT *ssl_cert_dup(CERT *cert) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 79 | { |
Rich Salz | b51bce9 | 2015-08-25 13:25:58 -0400 | [diff] [blame] | 80 | CERT *ret = OPENSSL_zalloc(sizeof(*ret)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 81 | int i; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 82 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 83 | if (ret == NULL) { |
| 84 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); |
Alessandro Ghedini | 16203f7 | 2016-02-29 17:26:07 +0000 | [diff] [blame] | 85 | return NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 86 | } |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 87 | |
Filipe DA SILVA | 0e04674 | 2015-09-16 22:25:31 -0400 | [diff] [blame] | 88 | ret->references = 1; |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 89 | ret->key = &ret->pkeys[cert->key - cert->pkeys]; |
Alessandro Ghedini | 16203f7 | 2016-02-29 17:26:07 +0000 | [diff] [blame] | 90 | ret->lock = CRYPTO_THREAD_lock_new(); |
Todd Short | aeb5b95 | 2016-03-09 10:01:43 -0500 | [diff] [blame] | 91 | if (ret->lock == NULL) { |
Alessandro Ghedini | 16203f7 | 2016-02-29 17:26:07 +0000 | [diff] [blame] | 92 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); |
| 93 | OPENSSL_free(ret); |
| 94 | return NULL; |
| 95 | } |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 96 | |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 97 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 98 | if (cert->dh_tmp != NULL) { |
Dr. Stephen Henson | e2b420f | 2015-12-17 00:05:26 +0000 | [diff] [blame] | 99 | ret->dh_tmp = cert->dh_tmp; |
| 100 | EVP_PKEY_up_ref(ret->dh_tmp); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 101 | } |
| 102 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
| 103 | ret->dh_tmp_auto = cert->dh_tmp_auto; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 104 | #endif |
| 105 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 106 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 107 | CERT_PKEY *cpk = cert->pkeys + i; |
| 108 | CERT_PKEY *rpk = ret->pkeys + i; |
| 109 | if (cpk->x509 != NULL) { |
| 110 | rpk->x509 = cpk->x509; |
Dr. Stephen Henson | 05f0fb9 | 2015-08-31 20:29:57 +0100 | [diff] [blame] | 111 | X509_up_ref(rpk->x509); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 112 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 113 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 114 | if (cpk->privatekey != NULL) { |
| 115 | rpk->privatekey = cpk->privatekey; |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 116 | EVP_PKEY_up_ref(cpk->privatekey); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (cpk->chain) { |
| 120 | rpk->chain = X509_chain_up_ref(cpk->chain); |
| 121 | if (!rpk->chain) { |
| 122 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); |
| 123 | goto err; |
| 124 | } |
| 125 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 126 | if (cert->pkeys[i].serverinfo != NULL) { |
| 127 | /* Just copy everything. */ |
| 128 | ret->pkeys[i].serverinfo = |
| 129 | OPENSSL_malloc(cert->pkeys[i].serverinfo_length); |
| 130 | if (ret->pkeys[i].serverinfo == NULL) { |
| 131 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); |
| 132 | goto err; |
| 133 | } |
| 134 | ret->pkeys[i].serverinfo_length = |
| 135 | cert->pkeys[i].serverinfo_length; |
| 136 | memcpy(ret->pkeys[i].serverinfo, |
| 137 | cert->pkeys[i].serverinfo, |
| 138 | cert->pkeys[i].serverinfo_length); |
| 139 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 140 | } |
Dr. Stephen Henson | 3dbc46d | 2012-07-03 12:51:14 +0000 | [diff] [blame] | 141 | |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 142 | /* Configured sigalgs copied across */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 143 | if (cert->conf_sigalgs) { |
| 144 | ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 145 | if (ret->conf_sigalgs == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 146 | goto err; |
| 147 | memcpy(ret->conf_sigalgs, cert->conf_sigalgs, cert->conf_sigalgslen); |
| 148 | ret->conf_sigalgslen = cert->conf_sigalgslen; |
| 149 | } else |
| 150 | ret->conf_sigalgs = NULL; |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 151 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 152 | if (cert->client_sigalgs) { |
| 153 | ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 154 | if (ret->client_sigalgs == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 155 | goto err; |
| 156 | memcpy(ret->client_sigalgs, cert->client_sigalgs, |
| 157 | cert->client_sigalgslen); |
| 158 | ret->client_sigalgslen = cert->client_sigalgslen; |
| 159 | } else |
| 160 | ret->client_sigalgs = NULL; |
| 161 | /* Shared sigalgs also NULL */ |
| 162 | ret->shared_sigalgs = NULL; |
| 163 | /* Copy any custom client certificate types */ |
| 164 | if (cert->ctypes) { |
| 165 | ret->ctypes = OPENSSL_malloc(cert->ctype_num); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 166 | if (ret->ctypes == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 167 | goto err; |
| 168 | memcpy(ret->ctypes, cert->ctypes, cert->ctype_num); |
| 169 | ret->ctype_num = cert->ctype_num; |
| 170 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 171 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 172 | ret->cert_flags = cert->cert_flags; |
Dr. Stephen Henson | 18d7158 | 2012-06-29 14:24:42 +0000 | [diff] [blame] | 173 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 174 | ret->cert_cb = cert->cert_cb; |
| 175 | ret->cert_cb_arg = cert->cert_cb_arg; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 176 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 177 | if (cert->verify_store) { |
Alessandro Ghedini | c001ce3 | 2016-03-01 18:06:15 +0000 | [diff] [blame] | 178 | X509_STORE_up_ref(cert->verify_store); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 179 | ret->verify_store = cert->verify_store; |
| 180 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 181 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 182 | if (cert->chain_store) { |
Alessandro Ghedini | c001ce3 | 2016-03-01 18:06:15 +0000 | [diff] [blame] | 183 | X509_STORE_up_ref(cert->chain_store); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 184 | ret->chain_store = cert->chain_store; |
| 185 | } |
Dr. Stephen Henson | 94a209d | 2012-09-12 13:57:48 +0000 | [diff] [blame] | 186 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 187 | ret->sec_cb = cert->sec_cb; |
| 188 | ret->sec_level = cert->sec_level; |
| 189 | ret->sec_ex = cert->sec_ex; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 190 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 191 | if (!custom_exts_copy(&ret->cli_ext, &cert->cli_ext)) |
| 192 | goto err; |
| 193 | if (!custom_exts_copy(&ret->srv_ext, &cert->srv_ext)) |
| 194 | goto err; |
Dr. Stephen Henson | 9076bd2 | 2015-09-11 17:08:11 +0100 | [diff] [blame] | 195 | #ifndef OPENSSL_NO_PSK |
Dr. Stephen Henson | df6da24 | 2015-09-14 17:58:04 +0100 | [diff] [blame] | 196 | if (cert->psk_identity_hint) { |
Rich Salz | 7644a9a | 2015-12-16 16:12:24 -0500 | [diff] [blame] | 197 | ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint); |
Dr. Stephen Henson | df6da24 | 2015-09-14 17:58:04 +0100 | [diff] [blame] | 198 | if (ret->psk_identity_hint == NULL) |
| 199 | goto err; |
| 200 | } |
Dr. Stephen Henson | 9076bd2 | 2015-09-11 17:08:11 +0100 | [diff] [blame] | 201 | #endif |
Alessandro Ghedini | 16203f7 | 2016-02-29 17:26:07 +0000 | [diff] [blame] | 202 | return ret; |
Dr. Stephen Henson | 9ade64d | 2012-01-27 14:21:38 +0000 | [diff] [blame] | 203 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 204 | err: |
| 205 | ssl_cert_free(ret); |
| 206 | |
| 207 | return NULL; |
| 208 | } |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 209 | |
Dr. Stephen Henson | a5ee80b | 2012-06-18 12:56:59 +0000 | [diff] [blame] | 210 | /* Free up and clear all certificates and chains */ |
| 211 | |
| 212 | void ssl_cert_clear_certs(CERT *c) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 213 | { |
| 214 | int i; |
| 215 | if (c == NULL) |
| 216 | return; |
| 217 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 218 | CERT_PKEY *cpk = c->pkeys + i; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 219 | X509_free(cpk->x509); |
| 220 | cpk->x509 = NULL; |
Rich Salz | c5ba2d9 | 2015-03-28 10:54:15 -0400 | [diff] [blame] | 221 | EVP_PKEY_free(cpk->privatekey); |
| 222 | cpk->privatekey = NULL; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 223 | sk_X509_pop_free(cpk->chain, X509_free); |
| 224 | cpk->chain = NULL; |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 225 | OPENSSL_free(cpk->serverinfo); |
| 226 | cpk->serverinfo = NULL; |
| 227 | cpk->serverinfo_length = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
Bodo Möller | ca8e5b9 | 1999-05-09 20:12:44 +0000 | [diff] [blame] | 230 | |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 231 | void ssl_cert_free(CERT *c) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 232 | { |
| 233 | int i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 234 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 235 | if (c == NULL) |
| 236 | return; |
Ben Laurie | e03ddfa | 1999-01-07 19:15:59 +0000 | [diff] [blame] | 237 | |
Alessandro Ghedini | 16203f7 | 2016-02-29 17:26:07 +0000 | [diff] [blame] | 238 | CRYPTO_atomic_add(&c->references, -1, &i, c->lock); |
Rich Salz | f3f1cf8 | 2016-01-30 12:04:25 -0500 | [diff] [blame] | 239 | REF_PRINT_COUNT("CERT", c); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 240 | if (i > 0) |
| 241 | return; |
Rich Salz | f3f1cf8 | 2016-01-30 12:04:25 -0500 | [diff] [blame] | 242 | REF_ASSERT_ISNT(i < 0); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 243 | |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 244 | #ifndef OPENSSL_NO_DH |
Dr. Stephen Henson | e2b420f | 2015-12-17 00:05:26 +0000 | [diff] [blame] | 245 | EVP_PKEY_free(c->dh_tmp); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 246 | #endif |
| 247 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 248 | ssl_cert_clear_certs(c); |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 249 | OPENSSL_free(c->conf_sigalgs); |
| 250 | OPENSSL_free(c->client_sigalgs); |
| 251 | OPENSSL_free(c->shared_sigalgs); |
| 252 | OPENSSL_free(c->ctypes); |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 253 | X509_STORE_free(c->verify_store); |
| 254 | X509_STORE_free(c->chain_store); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 255 | custom_exts_free(&c->cli_ext); |
| 256 | custom_exts_free(&c->srv_ext); |
Dr. Stephen Henson | df6da24 | 2015-09-14 17:58:04 +0100 | [diff] [blame] | 257 | #ifndef OPENSSL_NO_PSK |
| 258 | OPENSSL_free(c->psk_identity_hint); |
| 259 | #endif |
Alessandro Ghedini | 16203f7 | 2016-02-29 17:26:07 +0000 | [diff] [blame] | 260 | CRYPTO_THREAD_lock_free(c->lock); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 261 | OPENSSL_free(c); |
| 262 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 263 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 264 | int ssl_cert_set0_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 265 | { |
| 266 | int i, r; |
| 267 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; |
| 268 | if (!cpk) |
| 269 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 270 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 271 | r = ssl_security_cert(s, ctx, sk_X509_value(chain, i), 0, 0); |
| 272 | if (r != 1) { |
| 273 | SSLerr(SSL_F_SSL_CERT_SET0_CHAIN, r); |
| 274 | return 0; |
| 275 | } |
| 276 | } |
Todd Short | 4379d5c | 2016-05-25 20:56:48 -0400 | [diff] [blame^] | 277 | sk_X509_pop_free(cpk->chain, X509_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 278 | cpk->chain = chain; |
| 279 | return 1; |
| 280 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 281 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 282 | int ssl_cert_set1_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 283 | { |
| 284 | STACK_OF(X509) *dchain; |
| 285 | if (!chain) |
| 286 | return ssl_cert_set0_chain(s, ctx, NULL); |
| 287 | dchain = X509_chain_up_ref(chain); |
| 288 | if (!dchain) |
| 289 | return 0; |
| 290 | if (!ssl_cert_set0_chain(s, ctx, dchain)) { |
| 291 | sk_X509_pop_free(dchain, X509_free); |
| 292 | return 0; |
| 293 | } |
| 294 | return 1; |
| 295 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 296 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 297 | int ssl_cert_add0_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 298 | { |
| 299 | int r; |
| 300 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; |
| 301 | if (!cpk) |
| 302 | return 0; |
| 303 | r = ssl_security_cert(s, ctx, x, 0, 0); |
| 304 | if (r != 1) { |
| 305 | SSLerr(SSL_F_SSL_CERT_ADD0_CHAIN_CERT, r); |
| 306 | return 0; |
| 307 | } |
| 308 | if (!cpk->chain) |
| 309 | cpk->chain = sk_X509_new_null(); |
| 310 | if (!cpk->chain || !sk_X509_push(cpk->chain, x)) |
| 311 | return 0; |
| 312 | return 1; |
| 313 | } |
Dr. Stephen Henson | f71c6e5 | 2012-01-31 14:00:10 +0000 | [diff] [blame] | 314 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 315 | int ssl_cert_add1_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 316 | { |
| 317 | if (!ssl_cert_add0_chain_cert(s, ctx, x)) |
| 318 | return 0; |
Dr. Stephen Henson | 05f0fb9 | 2015-08-31 20:29:57 +0100 | [diff] [blame] | 319 | X509_up_ref(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 320 | return 1; |
| 321 | } |
Bodo Möller | b56bce4 | 1999-05-13 15:09:38 +0000 | [diff] [blame] | 322 | |
Rob Stradling | 7b6b246 | 2013-11-11 18:04:24 +0100 | [diff] [blame] | 323 | int ssl_cert_select_current(CERT *c, X509 *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 324 | { |
| 325 | int i; |
| 326 | if (x == NULL) |
| 327 | return 0; |
| 328 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 329 | CERT_PKEY *cpk = c->pkeys + i; |
| 330 | if (cpk->x509 == x && cpk->privatekey) { |
| 331 | c->key = cpk; |
| 332 | return 1; |
| 333 | } |
| 334 | } |
Dr. Stephen Henson | 629b640 | 2013-11-13 22:57:11 +0000 | [diff] [blame] | 335 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 336 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 337 | CERT_PKEY *cpk = c->pkeys + i; |
| 338 | if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) { |
| 339 | c->key = cpk; |
| 340 | return 1; |
| 341 | } |
| 342 | } |
| 343 | return 0; |
| 344 | } |
Rob Stradling | 7b6b246 | 2013-11-11 18:04:24 +0100 | [diff] [blame] | 345 | |
Dr. Stephen Henson | 0f78819 | 2014-02-02 02:51:30 +0000 | [diff] [blame] | 346 | int ssl_cert_set_current(CERT *c, long op) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 347 | { |
| 348 | int i, idx; |
| 349 | if (!c) |
| 350 | return 0; |
| 351 | if (op == SSL_CERT_SET_FIRST) |
| 352 | idx = 0; |
| 353 | else if (op == SSL_CERT_SET_NEXT) { |
| 354 | idx = (int)(c->key - c->pkeys + 1); |
| 355 | if (idx >= SSL_PKEY_NUM) |
| 356 | return 0; |
| 357 | } else |
| 358 | return 0; |
| 359 | for (i = idx; i < SSL_PKEY_NUM; i++) { |
| 360 | CERT_PKEY *cpk = c->pkeys + i; |
| 361 | if (cpk->x509 && cpk->privatekey) { |
| 362 | c->key = cpk; |
| 363 | return 1; |
| 364 | } |
| 365 | } |
| 366 | return 0; |
| 367 | } |
Dr. Stephen Henson | 0f78819 | 2014-02-02 02:51:30 +0000 | [diff] [blame] | 368 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 369 | void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg) |
| 370 | { |
| 371 | c->cert_cb = cb; |
| 372 | c->cert_cb_arg = arg; |
| 373 | } |
Dr. Stephen Henson | 18d7158 | 2012-06-29 14:24:42 +0000 | [diff] [blame] | 374 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 375 | int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk) |
| 376 | { |
| 377 | X509 *x; |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 378 | int i = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 379 | X509_STORE *verify_store; |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 380 | X509_STORE_CTX *ctx = NULL; |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 381 | X509_VERIFY_PARAM *param; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 382 | |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 383 | if ((sk == NULL) || (sk_X509_num(sk) == 0)) |
| 384 | return 0; |
| 385 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 386 | if (s->cert->verify_store) |
| 387 | verify_store = s->cert->verify_store; |
| 388 | else |
| 389 | verify_store = s->ctx->cert_store; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 390 | |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 391 | ctx = X509_STORE_CTX_new(); |
| 392 | if (ctx == NULL) { |
| 393 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_MALLOC_FAILURE); |
| 394 | return 0; |
| 395 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 396 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 397 | x = sk_X509_value(sk, 0); |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 398 | if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 399 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB); |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 400 | goto end; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 401 | } |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 402 | param = X509_STORE_CTX_get0_param(ctx); |
Viktor Dukhovni | fbb82a6 | 2016-03-18 22:09:41 -0400 | [diff] [blame] | 403 | /* |
| 404 | * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some |
| 405 | * point, for now a single @SECLEVEL sets the same policy for TLS crypto |
| 406 | * and PKI authentication. |
| 407 | */ |
| 408 | X509_VERIFY_PARAM_set_auth_level(param, SSL_get_security_level(s)); |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 409 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 410 | /* Set suite B flags if needed */ |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 411 | X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s)); |
FdaSilvaYY | a98810b | 2016-02-13 19:01:14 +0100 | [diff] [blame] | 412 | if (!X509_STORE_CTX_set_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s)) { |
| 413 | goto end; |
| 414 | } |
Bodo Möller | 3ac82fa | 2000-12-15 16:40:35 +0000 | [diff] [blame] | 415 | |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 416 | /* Verify via DANE if enabled */ |
| 417 | if (DANETLS_ENABLED(&s->dane)) |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 418 | X509_STORE_CTX_set0_dane(ctx, &s->dane); |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 419 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 420 | /* |
| 421 | * We need to inherit the verify parameters. These can be determined by |
| 422 | * the context: if its a server it will verify SSL client certificates or |
| 423 | * vice versa. |
| 424 | */ |
Dr. Stephen Henson | bb7cd4e | 1999-11-29 22:35:00 +0000 | [diff] [blame] | 425 | |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 426 | X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server"); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 427 | /* |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 428 | * Anything non-default in "s->param" should overwrite anything in the ctx. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 429 | */ |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 430 | X509_VERIFY_PARAM_set1(param, s->param); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 431 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 432 | if (s->verify_callback) |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 433 | X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback); |
Lutz Jänicke | 1f0c9ad | 2001-07-30 11:45:34 +0000 | [diff] [blame] | 434 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 435 | if (s->ctx->app_verify_callback != NULL) |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 436 | i = s->ctx->app_verify_callback(ctx, s->ctx->app_verify_arg); |
Viktor Dukhovni | fbb82a6 | 2016-03-18 22:09:41 -0400 | [diff] [blame] | 437 | else |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 438 | i = X509_verify_cert(ctx); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 439 | |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 440 | s->verify_result = X509_STORE_CTX_get_error(ctx); |
Dr. Stephen Henson | 696178e | 2016-02-06 03:17:23 +0000 | [diff] [blame] | 441 | sk_X509_pop_free(s->verified_chain, X509_free); |
| 442 | s->verified_chain = NULL; |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 443 | if (X509_STORE_CTX_get0_chain(ctx) != NULL) { |
| 444 | s->verified_chain = X509_STORE_CTX_get1_chain(ctx); |
Dr. Stephen Henson | 696178e | 2016-02-06 03:17:23 +0000 | [diff] [blame] | 445 | if (s->verified_chain == NULL) { |
| 446 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_MALLOC_FAILURE); |
| 447 | i = 0; |
| 448 | } |
| 449 | } |
Viktor Dukhovni | 919ba00 | 2015-12-29 13:28:28 -0500 | [diff] [blame] | 450 | |
| 451 | /* Move peername from the store context params to the SSL handle's */ |
| 452 | X509_VERIFY_PARAM_move_peername(s->param, param); |
| 453 | |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 454 | end: |
| 455 | X509_STORE_CTX_free(ctx); |
| 456 | return i; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 457 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 458 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 459 | static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list, |
| 460 | STACK_OF(X509_NAME) *name_list) |
| 461 | { |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 462 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 463 | *ca_list = name_list; |
| 464 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 465 | |
Ben Laurie | 838d25a | 1999-05-30 14:13:19 +0000 | [diff] [blame] | 466 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 467 | { |
| 468 | int i; |
| 469 | STACK_OF(X509_NAME) *ret; |
| 470 | X509_NAME *name; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 471 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 472 | ret = sk_X509_NAME_new_null(); |
| 473 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 474 | name = X509_NAME_dup(sk_X509_NAME_value(sk, i)); |
| 475 | if ((name == NULL) || !sk_X509_NAME_push(ret, name)) { |
| 476 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 477 | return (NULL); |
| 478 | } |
| 479 | } |
| 480 | return (ret); |
| 481 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 482 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 483 | void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list) |
| 484 | { |
| 485 | set_client_CA_list(&(s->client_CA), name_list); |
| 486 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 487 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 488 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) |
| 489 | { |
| 490 | set_client_CA_list(&(ctx->client_CA), name_list); |
| 491 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 492 | |
Ben Laurie | 0821bcd | 2005-03-30 10:26:02 +0000 | [diff] [blame] | 493 | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 494 | { |
| 495 | return (ctx->client_CA); |
| 496 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 497 | |
Ben Laurie | 0821bcd | 2005-03-30 10:26:02 +0000 | [diff] [blame] | 498 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 499 | { |
Matt Caswell | 23a635c | 2015-09-10 09:19:53 +0100 | [diff] [blame] | 500 | if (!s->server) { /* we are in the client */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 501 | if (((s->version >> 8) == SSL3_VERSION_MAJOR) && (s->s3 != NULL)) |
| 502 | return (s->s3->tmp.ca_names); |
| 503 | else |
| 504 | return (NULL); |
| 505 | } else { |
| 506 | if (s->client_CA != NULL) |
| 507 | return (s->client_CA); |
| 508 | else |
| 509 | return (s->ctx->client_CA); |
| 510 | } |
| 511 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 512 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 513 | static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x) |
| 514 | { |
| 515 | X509_NAME *name; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 516 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 517 | if (x == NULL) |
| 518 | return (0); |
| 519 | if ((*sk == NULL) && ((*sk = sk_X509_NAME_new_null()) == NULL)) |
| 520 | return (0); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 521 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 522 | if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL) |
| 523 | return (0); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 524 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 525 | if (!sk_X509_NAME_push(*sk, name)) { |
| 526 | X509_NAME_free(name); |
| 527 | return (0); |
| 528 | } |
| 529 | return (1); |
| 530 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 531 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 532 | int SSL_add_client_CA(SSL *ssl, X509 *x) |
| 533 | { |
| 534 | return (add_client_CA(&(ssl->client_CA), x)); |
| 535 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 536 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 537 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) |
| 538 | { |
| 539 | return (add_client_CA(&(ctx->client_CA), x)); |
| 540 | } |
| 541 | |
Toshikuni Fukaya | 7823d79 | 2016-02-03 13:08:45 -0500 | [diff] [blame] | 542 | static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 543 | { |
| 544 | return (X509_NAME_cmp(*a, *b)); |
| 545 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 546 | |
Toshikuni Fukaya | 7823d79 | 2016-02-03 13:08:45 -0500 | [diff] [blame] | 547 | static int xname_cmp(const X509_NAME *a, const X509_NAME *b) |
| 548 | { |
| 549 | return X509_NAME_cmp(a, b); |
| 550 | } |
| 551 | |
| 552 | static unsigned long xname_hash(const X509_NAME *a) |
| 553 | { |
| 554 | return X509_NAME_hash((X509_NAME *)a); |
| 555 | } |
| 556 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 557 | /** |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 558 | * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed; |
| 559 | * it doesn't really have anything to do with clients (except that a common use |
| 560 | * for a stack of CAs is to send it to the client). Actually, it doesn't have |
| 561 | * much to do with CAs, either, since it will load any old cert. |
| 562 | * \param file the file containing one or more certs. |
| 563 | * \return a ::STACK containing the certs. |
| 564 | */ |
Ben Laurie | f73e07c | 1999-04-12 17:23:57 +0000 | [diff] [blame] | 565 | STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 566 | { |
Toshikuni Fukaya | 7823d79 | 2016-02-03 13:08:45 -0500 | [diff] [blame] | 567 | BIO *in = BIO_new(BIO_s_file()); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 568 | X509 *x = NULL; |
| 569 | X509_NAME *xn = NULL; |
Toshikuni Fukaya | 7823d79 | 2016-02-03 13:08:45 -0500 | [diff] [blame] | 570 | STACK_OF(X509_NAME) *ret = NULL; |
| 571 | LHASH_OF(X509_NAME) *name_hash = |
| 572 | lh_X509_NAME_new(xname_hash, xname_cmp); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 573 | |
Toshikuni Fukaya | 7823d79 | 2016-02-03 13:08:45 -0500 | [diff] [blame] | 574 | if ((name_hash == NULL) || (in == NULL)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 575 | SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE); |
| 576 | goto err; |
| 577 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 578 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 579 | if (!BIO_read_filename(in, file)) |
| 580 | goto err; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 581 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 582 | for (;;) { |
| 583 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) |
| 584 | break; |
| 585 | if (ret == NULL) { |
| 586 | ret = sk_X509_NAME_new_null(); |
| 587 | if (ret == NULL) { |
| 588 | SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE); |
| 589 | goto err; |
| 590 | } |
| 591 | } |
| 592 | if ((xn = X509_get_subject_name(x)) == NULL) |
| 593 | goto err; |
| 594 | /* check for duplicates */ |
| 595 | xn = X509_NAME_dup(xn); |
| 596 | if (xn == NULL) |
| 597 | goto err; |
Toshikuni Fukaya | 7823d79 | 2016-02-03 13:08:45 -0500 | [diff] [blame] | 598 | if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) { |
| 599 | /* Duplicate. */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 600 | X509_NAME_free(xn); |
Toshikuni Fukaya | 7823d79 | 2016-02-03 13:08:45 -0500 | [diff] [blame] | 601 | } else { |
| 602 | lh_X509_NAME_insert(name_hash, xn); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 603 | sk_X509_NAME_push(ret, xn); |
| 604 | } |
| 605 | } |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 606 | goto done; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 607 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 608 | err: |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 609 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 610 | ret = NULL; |
| 611 | done: |
Rich Salz | ca3a82c | 2015-03-25 11:31:18 -0400 | [diff] [blame] | 612 | BIO_free(in); |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 613 | X509_free(x); |
Toshikuni Fukaya | 7823d79 | 2016-02-03 13:08:45 -0500 | [diff] [blame] | 614 | lh_X509_NAME_free(name_hash); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 615 | if (ret != NULL) |
| 616 | ERR_clear_error(); |
| 617 | return (ret); |
| 618 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 619 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 620 | /** |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 621 | * Add a file of certs to a stack. |
| 622 | * \param stack the stack to add to. |
| 623 | * \param file the file to add from. All certs in this file that are not |
| 624 | * already in the stack will be added. |
| 625 | * \return 1 for success, 0 for failure. Note that in the case of failure some |
| 626 | * certs may have been added to \c stack. |
| 627 | */ |
| 628 | |
Ben Laurie | 661b361 | 1999-05-03 19:55:00 +0000 | [diff] [blame] | 629 | int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 630 | const char *file) |
| 631 | { |
| 632 | BIO *in; |
| 633 | X509 *x = NULL; |
| 634 | X509_NAME *xn = NULL; |
| 635 | int ret = 1; |
| 636 | int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b); |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 637 | |
Toshikuni Fukaya | 7823d79 | 2016-02-03 13:08:45 -0500 | [diff] [blame] | 638 | oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp); |
Dr. Stephen Henson | a3a06e6 | 2010-03-24 23:17:15 +0000 | [diff] [blame] | 639 | |
Rich Salz | 9982cbb | 2015-09-30 14:32:49 -0400 | [diff] [blame] | 640 | in = BIO_new(BIO_s_file()); |
Bodo Möller | 2ea0910 | 2000-05-21 14:14:30 +0000 | [diff] [blame] | 641 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 642 | if (in == NULL) { |
| 643 | SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK, |
| 644 | ERR_R_MALLOC_FAILURE); |
| 645 | goto err; |
| 646 | } |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 647 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 648 | if (!BIO_read_filename(in, file)) |
| 649 | goto err; |
| 650 | |
| 651 | for (;;) { |
| 652 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) |
| 653 | break; |
| 654 | if ((xn = X509_get_subject_name(x)) == NULL) |
| 655 | goto err; |
| 656 | xn = X509_NAME_dup(xn); |
| 657 | if (xn == NULL) |
| 658 | goto err; |
| 659 | if (sk_X509_NAME_find(stack, xn) >= 0) |
| 660 | X509_NAME_free(xn); |
| 661 | else |
| 662 | sk_X509_NAME_push(stack, xn); |
| 663 | } |
| 664 | |
| 665 | ERR_clear_error(); |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 666 | goto done; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 667 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 668 | err: |
| 669 | ret = 0; |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 670 | done: |
Rich Salz | ca3a82c | 2015-03-25 11:31:18 -0400 | [diff] [blame] | 671 | BIO_free(in); |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 672 | X509_free(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 673 | (void)sk_X509_NAME_set_cmp_func(stack, oldcmp); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | /** |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 678 | * Add a directory of certs to a stack. |
| 679 | * \param stack the stack to append to. |
| 680 | * \param dir the directory to append from. All files in this directory will be |
| 681 | * examined as potential certs. Any that are acceptable to |
Ralf S. Engelschall | 72e442a | 1999-03-22 15:50:34 +0000 | [diff] [blame] | 682 | * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 683 | * included. |
| 684 | * \return 1 for success, 0 for failure. Note that in the case of failure some |
| 685 | * certs may have been added to \c stack. |
| 686 | */ |
| 687 | |
Ben Laurie | 661b361 | 1999-05-03 19:55:00 +0000 | [diff] [blame] | 688 | int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 689 | const char *dir) |
| 690 | { |
| 691 | OPENSSL_DIR_CTX *d = NULL; |
| 692 | const char *filename; |
| 693 | int ret = 0; |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 694 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 695 | /* Note that a side effect is that the CAs will be sorted by name */ |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 696 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 697 | while ((filename = OPENSSL_DIR_read(&d, dir))) { |
| 698 | char buf[1024]; |
| 699 | int r; |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 700 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 701 | if (strlen(dir) + strlen(filename) + 2 > sizeof buf) { |
| 702 | SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, |
| 703 | SSL_R_PATH_TOO_LONG); |
| 704 | goto err; |
| 705 | } |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 706 | #ifdef OPENSSL_SYS_VMS |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 707 | r = BIO_snprintf(buf, sizeof buf, "%s%s", dir, filename); |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 708 | #else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 709 | r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename); |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 710 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 711 | if (r <= 0 || r >= (int)sizeof(buf)) |
| 712 | goto err; |
| 713 | if (!SSL_add_file_cert_subjects_to_stack(stack, buf)) |
| 714 | goto err; |
| 715 | } |
Ben Laurie | eb90a48 | 1999-02-28 17:41:55 +0000 | [diff] [blame] | 716 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 717 | if (errno) { |
| 718 | SYSerr(SYS_F_OPENDIR, get_last_sys_error()); |
| 719 | ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')"); |
| 720 | SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB); |
| 721 | goto err; |
| 722 | } |
Richard Levitte | 4083a22 | 2004-07-10 13:17:16 +0000 | [diff] [blame] | 723 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 724 | ret = 1; |
Richard Levitte | 285046e | 2001-10-04 12:27:39 +0000 | [diff] [blame] | 725 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 726 | err: |
| 727 | if (d) |
| 728 | OPENSSL_DIR_end(&d); |
Alessandro Ghedini | 16203f7 | 2016-02-29 17:26:07 +0000 | [diff] [blame] | 729 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 730 | return ret; |
| 731 | } |
Richard Levitte | 285046e | 2001-10-04 12:27:39 +0000 | [diff] [blame] | 732 | |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 733 | /* Add a certificate to a BUF_MEM structure */ |
| 734 | |
| 735 | static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 736 | { |
| 737 | int n; |
| 738 | unsigned char *p; |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 739 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 740 | n = i2d_X509(x, NULL); |
Matt Caswell | 446ba8d | 2016-04-25 16:05:55 +0100 | [diff] [blame] | 741 | if (n < 0 || !BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 742 | SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB); |
| 743 | return 0; |
| 744 | } |
| 745 | p = (unsigned char *)&(buf->data[*l]); |
| 746 | l2n3(n, p); |
Matt Caswell | 446ba8d | 2016-04-25 16:05:55 +0100 | [diff] [blame] | 747 | n = i2d_X509(x, &p); |
| 748 | if (n < 0) { |
| 749 | /* Shouldn't happen */ |
| 750 | SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB); |
| 751 | return 0; |
| 752 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 753 | *l += n + 3; |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 754 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 755 | return 1; |
| 756 | } |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 757 | |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 758 | /* Add certificate chain to internal SSL BUF_MEM structure */ |
Dr. Stephen Henson | c526ed4 | 2012-01-26 16:00:34 +0000 | [diff] [blame] | 759 | int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 760 | { |
| 761 | BUF_MEM *buf = s->init_buf; |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 762 | int i, chain_count; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 763 | X509 *x; |
| 764 | STACK_OF(X509) *extra_certs; |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 765 | STACK_OF(X509) *chain = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 766 | X509_STORE *chain_store; |
Dr. Stephen Henson | c526ed4 | 2012-01-26 16:00:34 +0000 | [diff] [blame] | 767 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 768 | /* TLSv1 sends a chain with nothing in it, instead of an alert */ |
| 769 | if (!BUF_MEM_grow_clean(buf, 10)) { |
| 770 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB); |
| 771 | return 0; |
| 772 | } |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 773 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 774 | if (!cpk || !cpk->x509) |
| 775 | return 1; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 776 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 777 | x = cpk->x509; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 778 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 779 | /* |
| 780 | * If we have a certificate specific chain use it, else use parent ctx. |
| 781 | */ |
| 782 | if (cpk->chain) |
| 783 | extra_certs = cpk->chain; |
| 784 | else |
| 785 | extra_certs = s->ctx->extra_certs; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 786 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 787 | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) |
| 788 | chain_store = NULL; |
| 789 | else if (s->cert->chain_store) |
| 790 | chain_store = s->cert->chain_store; |
| 791 | else |
| 792 | chain_store = s->ctx->cert_store; |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 793 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 794 | if (chain_store) { |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 795 | X509_STORE_CTX* xs_ctx = X509_STORE_CTX_new(); |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 796 | |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 797 | if (xs_ctx == NULL) { |
| 798 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE); |
| 799 | return (0); |
| 800 | } |
| 801 | if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) { |
| 802 | X509_STORE_CTX_free(xs_ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 803 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB); |
| 804 | return (0); |
| 805 | } |
Matt Caswell | ae4d0c8 | 2015-11-11 10:17:22 +0000 | [diff] [blame] | 806 | /* |
| 807 | * It is valid for the chain not to be complete (because normally we |
| 808 | * don't include the root cert in the chain). Therefore we deliberately |
| 809 | * ignore the error return from this call. We're not actually verifying |
| 810 | * the cert - we're just building as much of the chain as we can |
| 811 | */ |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 812 | (void) X509_verify_cert(xs_ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 813 | /* Don't leave errors in the queue */ |
| 814 | ERR_clear_error(); |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 815 | chain = X509_STORE_CTX_get0_chain(xs_ctx); |
| 816 | i = ssl_security_cert_chain(s, chain, NULL, 0); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 817 | if (i != 1) { |
Viktor Dukhovni | fbb82a6 | 2016-03-18 22:09:41 -0400 | [diff] [blame] | 818 | #if 0 |
| 819 | /* Dummy error calls so mkerr generates them */ |
| 820 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL); |
| 821 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL); |
| 822 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK); |
| 823 | #endif |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 824 | X509_STORE_CTX_free(xs_ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 825 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); |
| 826 | return 0; |
| 827 | } |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 828 | chain_count = sk_X509_num(chain); |
| 829 | for (i = 0; i < chain_count; i++) { |
| 830 | x = sk_X509_value(chain, i); |
Dr. Stephen Henson | d628885 | 2014-03-09 16:12:20 +0000 | [diff] [blame] | 831 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 832 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 833 | X509_STORE_CTX_free(xs_ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 834 | return 0; |
| 835 | } |
| 836 | } |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 837 | X509_STORE_CTX_free(xs_ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 838 | } else { |
| 839 | i = ssl_security_cert_chain(s, extra_certs, x, 0); |
| 840 | if (i != 1) { |
| 841 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); |
| 842 | return 0; |
| 843 | } |
| 844 | if (!ssl_add_cert_to_buf(buf, l, x)) |
| 845 | return 0; |
| 846 | for (i = 0; i < sk_X509_num(extra_certs); i++) { |
| 847 | x = sk_X509_value(extra_certs, i); |
| 848 | if (!ssl_add_cert_to_buf(buf, l, x)) |
| 849 | return 0; |
| 850 | } |
| 851 | } |
| 852 | return 1; |
| 853 | } |
Dr. Stephen Henson | 4379d0e | 2012-01-26 15:47:32 +0000 | [diff] [blame] | 854 | |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 855 | /* Build a certificate chain for current certificate */ |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 856 | int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 857 | { |
| 858 | CERT *c = s ? s->cert : ctx->cert; |
| 859 | CERT_PKEY *cpk = c->key; |
| 860 | X509_STORE *chain_store = NULL; |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 861 | X509_STORE_CTX *xs_ctx = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 862 | STACK_OF(X509) *chain = NULL, *untrusted = NULL; |
| 863 | X509 *x; |
| 864 | int i, rv = 0; |
| 865 | unsigned long error; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 866 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 867 | if (!cpk->x509) { |
| 868 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET); |
| 869 | goto err; |
| 870 | } |
| 871 | /* Rearranging and check the chain: add everything to a store */ |
| 872 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) { |
| 873 | chain_store = X509_STORE_new(); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 874 | if (chain_store == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 875 | goto err; |
| 876 | for (i = 0; i < sk_X509_num(cpk->chain); i++) { |
| 877 | x = sk_X509_value(cpk->chain, i); |
| 878 | if (!X509_STORE_add_cert(chain_store, x)) { |
| 879 | error = ERR_peek_last_error(); |
| 880 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || |
| 881 | ERR_GET_REASON(error) != |
| 882 | X509_R_CERT_ALREADY_IN_HASH_TABLE) |
| 883 | goto err; |
| 884 | ERR_clear_error(); |
| 885 | } |
| 886 | } |
| 887 | /* Add EE cert too: it might be self signed */ |
| 888 | if (!X509_STORE_add_cert(chain_store, cpk->x509)) { |
| 889 | error = ERR_peek_last_error(); |
| 890 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || |
| 891 | ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) |
| 892 | goto err; |
| 893 | ERR_clear_error(); |
| 894 | } |
| 895 | } else { |
| 896 | if (c->chain_store) |
| 897 | chain_store = c->chain_store; |
| 898 | else if (s) |
| 899 | chain_store = s->ctx->cert_store; |
| 900 | else |
| 901 | chain_store = ctx->cert_store; |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 902 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 903 | if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) |
| 904 | untrusted = cpk->chain; |
| 905 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 906 | |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 907 | xs_ctx = X509_STORE_CTX_new(); |
| 908 | if (xs_ctx == NULL) { |
| 909 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_MALLOC_FAILURE); |
| 910 | goto err; |
| 911 | } |
| 912 | if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 913 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB); |
| 914 | goto err; |
| 915 | } |
| 916 | /* Set suite B flags if needed */ |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 917 | X509_STORE_CTX_set_flags(xs_ctx, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 918 | c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS); |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 919 | |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 920 | i = X509_verify_cert(xs_ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 921 | if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) { |
| 922 | if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) |
| 923 | ERR_clear_error(); |
| 924 | i = 1; |
| 925 | rv = 2; |
| 926 | } |
| 927 | if (i > 0) |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 928 | chain = X509_STORE_CTX_get1_chain(xs_ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 929 | if (i <= 0) { |
| 930 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED); |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 931 | i = X509_STORE_CTX_get_error(xs_ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 932 | ERR_add_error_data(2, "Verify error:", |
| 933 | X509_verify_cert_error_string(i)); |
Dr. Stephen Henson | 13dc3ce | 2014-02-23 12:00:18 +0000 | [diff] [blame] | 934 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 935 | goto err; |
| 936 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 937 | /* Remove EE certificate from chain */ |
| 938 | x = sk_X509_shift(chain); |
| 939 | X509_free(x); |
| 940 | if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) { |
| 941 | if (sk_X509_num(chain) > 0) { |
| 942 | /* See if last cert is self signed */ |
| 943 | x = sk_X509_value(chain, sk_X509_num(chain) - 1); |
Dr. Stephen Henson | a8d8e06 | 2015-09-02 22:01:18 +0100 | [diff] [blame] | 944 | if (X509_get_extension_flags(x) & EXFLAG_SS) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 945 | x = sk_X509_pop(chain); |
| 946 | X509_free(x); |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | /* |
| 951 | * Check security level of all CA certificates: EE will have been checked |
| 952 | * already. |
| 953 | */ |
| 954 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 955 | x = sk_X509_value(chain, i); |
| 956 | rv = ssl_security_cert(s, ctx, x, 0, 0); |
| 957 | if (rv != 1) { |
| 958 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, rv); |
| 959 | sk_X509_pop_free(chain, X509_free); |
| 960 | rv = 0; |
| 961 | goto err; |
| 962 | } |
| 963 | } |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 964 | sk_X509_pop_free(cpk->chain, X509_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 965 | cpk->chain = chain; |
| 966 | if (rv == 0) |
| 967 | rv = 1; |
| 968 | err: |
| 969 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) |
| 970 | X509_STORE_free(chain_store); |
Rich Salz | f0e0fd5 | 2016-04-14 23:59:26 -0400 | [diff] [blame] | 971 | X509_STORE_CTX_free(xs_ctx); |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 972 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 973 | return rv; |
| 974 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 975 | |
| 976 | int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 977 | { |
| 978 | X509_STORE **pstore; |
| 979 | if (chain) |
| 980 | pstore = &c->chain_store; |
| 981 | else |
| 982 | pstore = &c->verify_store; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 983 | X509_STORE_free(*pstore); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 984 | *pstore = store; |
| 985 | if (ref && store) |
Alessandro Ghedini | c001ce3 | 2016-03-01 18:06:15 +0000 | [diff] [blame] | 986 | X509_STORE_up_ref(store); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 987 | return 1; |
| 988 | } |
Dr. Stephen Henson | 74ecfab | 2012-07-23 23:34:28 +0000 | [diff] [blame] | 989 | |
Kurt Roeckx | e4646a8 | 2016-02-07 20:44:27 +0100 | [diff] [blame] | 990 | static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx, int op, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 991 | int bits, int nid, void *other, |
| 992 | void *ex) |
| 993 | { |
| 994 | int level, minbits; |
| 995 | static const int minbits_table[5] = { 80, 112, 128, 192, 256 }; |
| 996 | if (ctx) |
| 997 | level = SSL_CTX_get_security_level(ctx); |
| 998 | else |
| 999 | level = SSL_get_security_level(s); |
Viktor Dukhovni | a7cf07b | 2016-01-14 01:16:16 -0500 | [diff] [blame] | 1000 | |
| 1001 | if (level <= 0) { |
| 1002 | /* |
| 1003 | * No EDH keys weaker than 1024-bits even at level 0, otherwise, |
| 1004 | * anything goes. |
| 1005 | */ |
| 1006 | if (op == SSL_SECOP_TMP_DH && bits < 80) |
| 1007 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1008 | return 1; |
Viktor Dukhovni | a7cf07b | 2016-01-14 01:16:16 -0500 | [diff] [blame] | 1009 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1010 | if (level > 5) |
| 1011 | level = 5; |
| 1012 | minbits = minbits_table[level - 1]; |
| 1013 | switch (op) { |
| 1014 | case SSL_SECOP_CIPHER_SUPPORTED: |
| 1015 | case SSL_SECOP_CIPHER_SHARED: |
| 1016 | case SSL_SECOP_CIPHER_CHECK: |
| 1017 | { |
| 1018 | const SSL_CIPHER *c = other; |
| 1019 | /* No ciphers below security level */ |
| 1020 | if (bits < minbits) |
| 1021 | return 0; |
| 1022 | /* No unauthenticated ciphersuites */ |
| 1023 | if (c->algorithm_auth & SSL_aNULL) |
| 1024 | return 0; |
| 1025 | /* No MD5 mac ciphersuites */ |
| 1026 | if (c->algorithm_mac & SSL_MD5) |
| 1027 | return 0; |
| 1028 | /* SHA1 HMAC is 160 bits of security */ |
| 1029 | if (minbits > 160 && c->algorithm_mac & SSL_SHA1) |
| 1030 | return 0; |
| 1031 | /* Level 2: no RC4 */ |
| 1032 | if (level >= 2 && c->algorithm_enc == SSL_RC4) |
| 1033 | return 0; |
| 1034 | /* Level 3: forward secure ciphersuites only */ |
| 1035 | if (level >= 3 && !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH))) |
| 1036 | return 0; |
| 1037 | break; |
| 1038 | } |
| 1039 | case SSL_SECOP_VERSION: |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 1040 | if (!SSL_IS_DTLS(s)) { |
| 1041 | /* SSLv3 not allowed at level 2 */ |
| 1042 | if (nid <= SSL3_VERSION && level >= 2) |
| 1043 | return 0; |
| 1044 | /* TLS v1.1 and above only for level 3 */ |
| 1045 | if (nid <= TLS1_VERSION && level >= 3) |
| 1046 | return 0; |
| 1047 | /* TLS v1.2 only for level 4 and above */ |
| 1048 | if (nid <= TLS1_1_VERSION && level >= 4) |
| 1049 | return 0; |
| 1050 | } else { |
| 1051 | /* DTLS v1.2 only for level 4 and above */ |
| 1052 | if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level >= 4) |
| 1053 | return 0; |
| 1054 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1055 | break; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1056 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1057 | case SSL_SECOP_COMPRESSION: |
| 1058 | if (level >= 2) |
| 1059 | return 0; |
| 1060 | break; |
| 1061 | case SSL_SECOP_TICKET: |
| 1062 | if (level >= 3) |
| 1063 | return 0; |
| 1064 | break; |
| 1065 | default: |
| 1066 | if (bits < minbits) |
| 1067 | return 0; |
| 1068 | } |
| 1069 | return 1; |
| 1070 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1071 | |
Kurt Roeckx | e4646a8 | 2016-02-07 20:44:27 +0100 | [diff] [blame] | 1072 | int ssl_security(const SSL *s, int op, int bits, int nid, void *other) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1073 | { |
| 1074 | return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex); |
| 1075 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1076 | |
Kurt Roeckx | e4646a8 | 2016-02-07 20:44:27 +0100 | [diff] [blame] | 1077 | int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1078 | { |
| 1079 | return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other, |
| 1080 | ctx->cert->sec_ex); |
| 1081 | } |