Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* 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 | |
David Benjamin | 443a1f6 | 2015-09-04 15:05:05 -0400 | [diff] [blame] | 57 | #include <openssl/ssl.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 58 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 59 | #include <assert.h> |
David Benjamin | 3a59611 | 2015-11-12 09:25:30 -0800 | [diff] [blame] | 60 | #include <limits.h> |
| 61 | |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 62 | #include <openssl/ec.h> |
| 63 | #include <openssl/ec_key.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 64 | #include <openssl/err.h> |
| 65 | #include <openssl/evp.h> |
| 66 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 67 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 68 | #include "internal.h" |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 69 | #include "../crypto/internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 70 | |
David Benjamin | 443a1f6 | 2015-09-04 15:05:05 -0400 | [diff] [blame] | 71 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 72 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 73 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 74 | bool ssl_is_key_type_supported(int key_type) { |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 75 | return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC || |
| 76 | key_type == EVP_PKEY_ED25519; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 77 | } |
| 78 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 79 | static bool ssl_set_pkey(CERT *cert, EVP_PKEY *pkey) { |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 80 | if (!ssl_is_key_type_supported(pkey->type)) { |
| 81 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 82 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 83 | } |
| 84 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 85 | if (cert->chain != nullptr && |
| 86 | sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) != nullptr && |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 87 | // Sanity-check that the private key and the certificate match. |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 88 | !ssl_cert_check_private_key(cert, pkey)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 89 | return false; |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 90 | } |
| 91 | |
David Benjamin | 2908dd1 | 2018-06-29 17:46:42 -0400 | [diff] [blame] | 92 | cert->privatekey = UpRef(pkey); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 93 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 94 | } |
| 95 | |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 96 | typedef struct { |
| 97 | uint16_t sigalg; |
| 98 | int pkey_type; |
| 99 | int curve; |
| 100 | const EVP_MD *(*digest_func)(void); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 101 | bool is_rsa_pss; |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 102 | } SSL_SIGNATURE_ALGORITHM; |
| 103 | |
| 104 | static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 105 | {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1, |
David Benjamin | e5fe31c | 2021-03-19 14:06:51 -0400 | [diff] [blame^] | 106 | false}, |
| 107 | {SSL_SIGN_RSA_PKCS1_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_sha1, false}, |
| 108 | {SSL_SIGN_RSA_PKCS1_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, false}, |
| 109 | {SSL_SIGN_RSA_PKCS1_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, false}, |
| 110 | {SSL_SIGN_RSA_PKCS1_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, false}, |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 111 | |
David Benjamin | e5fe31c | 2021-03-19 14:06:51 -0400 | [diff] [blame^] | 112 | {SSL_SIGN_RSA_PSS_RSAE_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, true}, |
| 113 | {SSL_SIGN_RSA_PSS_RSAE_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, true}, |
| 114 | {SSL_SIGN_RSA_PSS_RSAE_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, true}, |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 115 | |
David Benjamin | e5fe31c | 2021-03-19 14:06:51 -0400 | [diff] [blame^] | 116 | {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, false}, |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 117 | {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1, |
David Benjamin | e5fe31c | 2021-03-19 14:06:51 -0400 | [diff] [blame^] | 118 | &EVP_sha256, false}, |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 119 | {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384, |
David Benjamin | e5fe31c | 2021-03-19 14:06:51 -0400 | [diff] [blame^] | 120 | false}, |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 121 | {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512, |
David Benjamin | e5fe31c | 2021-03-19 14:06:51 -0400 | [diff] [blame^] | 122 | false}, |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 123 | |
David Benjamin | e5fe31c | 2021-03-19 14:06:51 -0400 | [diff] [blame^] | 124 | {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, nullptr, false}, |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static const SSL_SIGNATURE_ALGORITHM *get_signature_algorithm(uint16_t sigalg) { |
| 128 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kSignatureAlgorithms); i++) { |
| 129 | if (kSignatureAlgorithms[i].sigalg == sigalg) { |
| 130 | return &kSignatureAlgorithms[i]; |
| 131 | } |
| 132 | } |
| 133 | return NULL; |
| 134 | } |
| 135 | |
Christopher Patton | 9cde848 | 2018-07-17 11:36:36 -0700 | [diff] [blame] | 136 | bool ssl_has_private_key(const SSL_HANDSHAKE *hs) { |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 137 | if (hs->config->cert->privatekey != nullptr || |
| 138 | hs->config->cert->key_method != nullptr || |
| 139 | ssl_signing_with_dc(hs)) { |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | return false; |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 144 | } |
| 145 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 146 | static bool pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey, |
David Benjamin | e5fe31c | 2021-03-19 14:06:51 -0400 | [diff] [blame^] | 147 | uint16_t sigalg) { |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 148 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
| 149 | if (alg == NULL || |
| 150 | EVP_PKEY_id(pkey) != alg->pkey_type) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 151 | return false; |
David Benjamin | d246b81 | 2016-07-08 15:07:02 -0700 | [diff] [blame] | 152 | } |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 153 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 154 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | e5fe31c | 2021-03-19 14:06:51 -0400 | [diff] [blame^] | 155 | // RSA keys may only be used with RSA-PSS. |
| 156 | if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 157 | return false; |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 158 | } |
| 159 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 160 | // EC keys have a curve requirement. |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 161 | if (alg->pkey_type == EVP_PKEY_EC && |
| 162 | (alg->curve == NID_undef || |
| 163 | EC_GROUP_get_curve_name( |
| 164 | EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 165 | return false; |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 169 | return true; |
David Benjamin | d246b81 | 2016-07-08 15:07:02 -0700 | [diff] [blame] | 170 | } |
| 171 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 172 | static bool setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey, |
| 173 | uint16_t sigalg, bool is_verify) { |
David Benjamin | e5fe31c | 2021-03-19 14:06:51 -0400 | [diff] [blame^] | 174 | if (!pkey_supports_algorithm(ssl, pkey, sigalg)) { |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 175 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 176 | return false; |
David Benjamin | 887c300 | 2016-07-08 16:15:32 -0700 | [diff] [blame] | 177 | } |
David Benjamin | a2d81f1 | 2016-07-08 15:42:16 -0700 | [diff] [blame] | 178 | |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 179 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
David Benjamin | 1967094 | 2017-05-31 19:07:31 -0400 | [diff] [blame] | 180 | const EVP_MD *digest = alg->digest_func != NULL ? alg->digest_func() : NULL; |
| 181 | EVP_PKEY_CTX *pctx; |
| 182 | if (is_verify) { |
| 183 | if (!EVP_DigestVerifyInit(ctx, &pctx, digest, NULL, pkey)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 184 | return false; |
David Benjamin | 1967094 | 2017-05-31 19:07:31 -0400 | [diff] [blame] | 185 | } |
| 186 | } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 187 | return false; |
David Benjamin | a2d81f1 | 2016-07-08 15:42:16 -0700 | [diff] [blame] | 188 | } |
David Benjamin | a2d81f1 | 2016-07-08 15:42:16 -0700 | [diff] [blame] | 189 | |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 190 | if (alg->is_rsa_pss) { |
David Benjamin | 1967094 | 2017-05-31 19:07:31 -0400 | [diff] [blame] | 191 | if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) || |
| 192 | !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 193 | return false; |
David Benjamin | 76feb1f | 2017-03-28 14:17:01 -0500 | [diff] [blame] | 194 | } |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 195 | } |
| 196 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 197 | return true; |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 198 | } |
| 199 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 200 | enum ssl_private_key_result_t ssl_private_key_sign( |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 201 | SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out, |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 202 | uint16_t sigalg, Span<const uint8_t> in) { |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 203 | SSL *const ssl = hs->ssl; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 204 | const SSL_PRIVATE_KEY_METHOD *key_method = hs->config->cert->key_method; |
| 205 | EVP_PKEY *privatekey = hs->config->cert->privatekey.get(); |
| 206 | if (ssl_signing_with_dc(hs)) { |
| 207 | key_method = hs->config->cert->dc_key_method; |
| 208 | privatekey = hs->config->cert->dc_privatekey.get(); |
| 209 | } |
| 210 | |
| 211 | if (key_method != NULL) { |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 212 | enum ssl_private_key_result_t ret; |
| 213 | if (hs->pending_private_key_op) { |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 214 | ret = key_method->complete(ssl, out, out_len, max_out); |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 215 | } else { |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 216 | ret = key_method->sign(ssl, out, out_len, max_out, |
| 217 | sigalg, in.data(), in.size()); |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 218 | } |
David Benjamin | fa65113 | 2018-02-01 15:07:30 -0500 | [diff] [blame] | 219 | if (ret == ssl_private_key_failure) { |
| 220 | OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED); |
| 221 | } |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 222 | hs->pending_private_key_op = ret == ssl_private_key_retry; |
| 223 | return ret; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 224 | } |
| 225 | |
David Benjamin | 76feb1f | 2017-03-28 14:17:01 -0500 | [diff] [blame] | 226 | *out_len = max_out; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 227 | ScopedEVP_MD_CTX ctx; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 228 | if (!setup_ctx(ssl, ctx.get(), privatekey, sigalg, false /* sign */) || |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 229 | !EVP_DigestSign(ctx.get(), out, out_len, in.data(), in.size())) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 230 | return ssl_private_key_failure; |
| 231 | } |
| 232 | return ssl_private_key_success; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 233 | } |
| 234 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 235 | bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature, |
| 236 | uint16_t sigalg, EVP_PKEY *pkey, |
| 237 | Span<const uint8_t> in) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 238 | ScopedEVP_MD_CTX ctx; |
David Benjamin | 4dfd5af | 2019-07-19 17:34:37 -0400 | [diff] [blame] | 239 | if (!setup_ctx(ssl, ctx.get(), pkey, sigalg, true /* verify */)) { |
| 240 | return false; |
| 241 | } |
| 242 | bool ok = EVP_DigestVerify(ctx.get(), signature.data(), signature.size(), |
| 243 | in.data(), in.size()); |
| 244 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 245 | ok = true; |
| 246 | ERR_clear_error(); |
| 247 | #endif |
| 248 | return ok; |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 249 | } |
| 250 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 251 | enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs, |
| 252 | uint8_t *out, |
| 253 | size_t *out_len, |
| 254 | size_t max_out, |
| 255 | Span<const uint8_t> in) { |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 256 | SSL *const ssl = hs->ssl; |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 257 | if (hs->config->cert->key_method != NULL) { |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 258 | enum ssl_private_key_result_t ret; |
| 259 | if (hs->pending_private_key_op) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 260 | ret = hs->config->cert->key_method->complete(ssl, out, out_len, max_out); |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 261 | } else { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 262 | ret = hs->config->cert->key_method->decrypt(ssl, out, out_len, max_out, |
| 263 | in.data(), in.size()); |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 264 | } |
David Benjamin | fa65113 | 2018-02-01 15:07:30 -0500 | [diff] [blame] | 265 | if (ret == ssl_private_key_failure) { |
| 266 | OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED); |
| 267 | } |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 268 | hs->pending_private_key_op = ret == ssl_private_key_retry; |
| 269 | return ret; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 272 | RSA *rsa = EVP_PKEY_get0_RSA(hs->config->cert->privatekey.get()); |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 273 | if (rsa == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 274 | // Decrypt operations are only supported for RSA keys. |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 275 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 276 | return ssl_private_key_failure; |
| 277 | } |
| 278 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 279 | // Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 280 | // timing-sensitive code by the caller. |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 281 | if (!RSA_decrypt(rsa, out_len, out, max_out, in.data(), in.size(), |
| 282 | RSA_NO_PADDING)) { |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 283 | return ssl_private_key_failure; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 284 | } |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 285 | return ssl_private_key_success; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 286 | } |
| 287 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 288 | bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs, |
| 289 | uint16_t sigalg) { |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 290 | SSL *const ssl = hs->ssl; |
David Benjamin | e5fe31c | 2021-03-19 14:06:51 -0400 | [diff] [blame^] | 291 | if (!pkey_supports_algorithm(ssl, hs->local_pubkey.get(), sigalg)) { |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 292 | return false; |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 293 | } |
| 294 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 295 | // Ensure the RSA key is large enough for the hash. RSASSA-PSS requires that |
| 296 | // emLen be at least hLen + sLen + 2. Both hLen and sLen are the size of the |
| 297 | // hash in TLS. Reasonable RSA key sizes are large enough for the largest |
| 298 | // defined RSASSA-PSS algorithm, but 1024-bit RSA is slightly too small for |
| 299 | // SHA-512. 1024-bit RSA is sometimes used for test credentials, so check the |
| 300 | // size so that we can fall back to another algorithm in that case. |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 301 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 302 | if (alg->is_rsa_pss && (size_t)EVP_PKEY_size(hs->local_pubkey.get()) < |
| 303 | 2 * EVP_MD_size(alg->digest_func()) + 2) { |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 304 | return false; |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 305 | } |
David Benjamin | 7944a9f | 2016-07-12 22:27:01 -0400 | [diff] [blame] | 306 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 307 | return true; |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 308 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 309 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 310 | BSSL_NAMESPACE_END |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 311 | |
| 312 | using namespace bssl; |
| 313 | |
| 314 | int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 315 | if (rsa == NULL || ssl->config == NULL) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 316 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 317 | return 0; |
| 318 | } |
| 319 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame] | 320 | UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 321 | if (!pkey || |
| 322 | !EVP_PKEY_set1_RSA(pkey.get(), rsa)) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 323 | OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB); |
| 324 | return 0; |
| 325 | } |
| 326 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 327 | return ssl_set_pkey(ssl->config->cert.get(), pkey.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) { |
| 331 | UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len)); |
| 332 | if (!rsa) { |
| 333 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | return SSL_use_RSAPrivateKey(ssl, rsa.get()); |
| 338 | } |
| 339 | |
| 340 | int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 341 | if (pkey == NULL || ssl->config == NULL) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 342 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 343 | return 0; |
| 344 | } |
| 345 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 346 | return ssl_set_pkey(ssl->config->cert.get(), pkey); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der, |
| 350 | size_t der_len) { |
| 351 | if (der_len > LONG_MAX) { |
| 352 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | const uint8_t *p = der; |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame] | 357 | UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len)); |
| 358 | if (!pkey || p != der + der_len) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 359 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame] | 363 | return SSL_use_PrivateKey(ssl, pkey.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 367 | if (rsa == NULL) { |
| 368 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 369 | return 0; |
| 370 | } |
| 371 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame] | 372 | UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 373 | if (!pkey || |
| 374 | !EVP_PKEY_set1_RSA(pkey.get(), rsa)) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 375 | OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB); |
| 376 | return 0; |
| 377 | } |
| 378 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 379 | return ssl_set_pkey(ctx->cert.get(), pkey.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der, |
| 383 | size_t der_len) { |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame] | 384 | UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len)); |
| 385 | if (!rsa) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 386 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
| 387 | return 0; |
| 388 | } |
| 389 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame] | 390 | return SSL_CTX_use_RSAPrivateKey(ctx, rsa.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) { |
| 394 | if (pkey == NULL) { |
| 395 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 396 | return 0; |
| 397 | } |
| 398 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 399 | return ssl_set_pkey(ctx->cert.get(), pkey); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der, |
| 403 | size_t der_len) { |
| 404 | if (der_len > LONG_MAX) { |
| 405 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | const uint8_t *p = der; |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame] | 410 | UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len)); |
| 411 | if (!pkey || p != der + der_len) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 412 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame] | 416 | return SSL_CTX_use_PrivateKey(ctx, pkey.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void SSL_set_private_key_method(SSL *ssl, |
| 420 | const SSL_PRIVATE_KEY_METHOD *key_method) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 421 | if (!ssl->config) { |
| 422 | return; |
| 423 | } |
| 424 | ssl->config->cert->key_method = key_method; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void SSL_CTX_set_private_key_method(SSL_CTX *ctx, |
| 428 | const SSL_PRIVATE_KEY_METHOD *key_method) { |
| 429 | ctx->cert->key_method = key_method; |
| 430 | } |
| 431 | |
Adam Langley | 826ce15 | 2018-08-03 10:31:21 -0700 | [diff] [blame] | 432 | static constexpr size_t kMaxSignatureAlgorithmNameLen = 23; |
| 433 | |
| 434 | // This was "constexpr" rather than "const", but that triggered a bug in MSVC |
| 435 | // where it didn't pad the strings to the correct length. |
| 436 | static const struct { |
| 437 | uint16_t signature_algorithm; |
| 438 | const char name[kMaxSignatureAlgorithmNameLen]; |
| 439 | } kSignatureAlgorithmNames[] = { |
| 440 | {SSL_SIGN_RSA_PKCS1_MD5_SHA1, "rsa_pkcs1_md5_sha1"}, |
| 441 | {SSL_SIGN_RSA_PKCS1_SHA1, "rsa_pkcs1_sha1"}, |
| 442 | {SSL_SIGN_RSA_PKCS1_SHA256, "rsa_pkcs1_sha256"}, |
| 443 | {SSL_SIGN_RSA_PKCS1_SHA384, "rsa_pkcs1_sha384"}, |
| 444 | {SSL_SIGN_RSA_PKCS1_SHA512, "rsa_pkcs1_sha512"}, |
| 445 | {SSL_SIGN_ECDSA_SHA1, "ecdsa_sha1"}, |
| 446 | {SSL_SIGN_ECDSA_SECP256R1_SHA256, "ecdsa_secp256r1_sha256"}, |
| 447 | {SSL_SIGN_ECDSA_SECP384R1_SHA384, "ecdsa_secp384r1_sha384"}, |
| 448 | {SSL_SIGN_ECDSA_SECP521R1_SHA512, "ecdsa_secp521r1_sha512"}, |
| 449 | {SSL_SIGN_RSA_PSS_RSAE_SHA256, "rsa_pss_rsae_sha256"}, |
| 450 | {SSL_SIGN_RSA_PSS_RSAE_SHA384, "rsa_pss_rsae_sha384"}, |
| 451 | {SSL_SIGN_RSA_PSS_RSAE_SHA512, "rsa_pss_rsae_sha512"}, |
| 452 | {SSL_SIGN_ED25519, "ed25519"}, |
| 453 | }; |
| 454 | |
David Benjamin | 6cc352e | 2017-11-02 17:21:39 -0400 | [diff] [blame] | 455 | const char *SSL_get_signature_algorithm_name(uint16_t sigalg, |
| 456 | int include_curve) { |
Adam Langley | 826ce15 | 2018-08-03 10:31:21 -0700 | [diff] [blame] | 457 | if (!include_curve) { |
| 458 | switch (sigalg) { |
| 459 | case SSL_SIGN_ECDSA_SECP256R1_SHA256: |
| 460 | return "ecdsa_sha256"; |
| 461 | case SSL_SIGN_ECDSA_SECP384R1_SHA384: |
| 462 | return "ecdsa_sha384"; |
| 463 | case SSL_SIGN_ECDSA_SECP521R1_SHA512: |
| 464 | return "ecdsa_sha512"; |
| 465 | } |
David Benjamin | 6cc352e | 2017-11-02 17:21:39 -0400 | [diff] [blame] | 466 | } |
Adam Langley | 826ce15 | 2018-08-03 10:31:21 -0700 | [diff] [blame] | 467 | |
| 468 | for (const auto &candidate : kSignatureAlgorithmNames) { |
| 469 | if (candidate.signature_algorithm == sigalg) { |
| 470 | return candidate.name; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | return NULL; |
David Benjamin | 6cc352e | 2017-11-02 17:21:39 -0400 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | int SSL_get_signature_algorithm_key_type(uint16_t sigalg) { |
| 478 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
| 479 | return alg != nullptr ? alg->pkey_type : EVP_PKEY_NONE; |
| 480 | } |
| 481 | |
| 482 | const EVP_MD *SSL_get_signature_algorithm_digest(uint16_t sigalg) { |
| 483 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
| 484 | if (alg == nullptr || alg->digest_func == nullptr) { |
| 485 | return nullptr; |
| 486 | } |
| 487 | return alg->digest_func(); |
| 488 | } |
| 489 | |
| 490 | int SSL_is_signature_algorithm_rsa_pss(uint16_t sigalg) { |
| 491 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
| 492 | return alg != nullptr && alg->is_rsa_pss; |
| 493 | } |
| 494 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 495 | int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs, |
| 496 | size_t num_prefs) { |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 497 | return ctx->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs)); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 498 | } |
| 499 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 500 | int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs, |
| 501 | size_t num_prefs) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 502 | if (!ssl->config) { |
| 503 | return 0; |
| 504 | } |
| 505 | return ssl->config->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs)); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 506 | } |
| 507 | |
Adam Langley | 826ce15 | 2018-08-03 10:31:21 -0700 | [diff] [blame] | 508 | static constexpr struct { |
| 509 | int pkey_type; |
| 510 | int hash_nid; |
| 511 | uint16_t signature_algorithm; |
| 512 | } kSignatureAlgorithmsMapping[] = { |
| 513 | {EVP_PKEY_RSA, NID_sha1, SSL_SIGN_RSA_PKCS1_SHA1}, |
| 514 | {EVP_PKEY_RSA, NID_sha256, SSL_SIGN_RSA_PKCS1_SHA256}, |
| 515 | {EVP_PKEY_RSA, NID_sha384, SSL_SIGN_RSA_PKCS1_SHA384}, |
| 516 | {EVP_PKEY_RSA, NID_sha512, SSL_SIGN_RSA_PKCS1_SHA512}, |
| 517 | {EVP_PKEY_RSA_PSS, NID_sha256, SSL_SIGN_RSA_PSS_RSAE_SHA256}, |
| 518 | {EVP_PKEY_RSA_PSS, NID_sha384, SSL_SIGN_RSA_PSS_RSAE_SHA384}, |
| 519 | {EVP_PKEY_RSA_PSS, NID_sha512, SSL_SIGN_RSA_PSS_RSAE_SHA512}, |
| 520 | {EVP_PKEY_EC, NID_sha1, SSL_SIGN_ECDSA_SHA1}, |
| 521 | {EVP_PKEY_EC, NID_sha256, SSL_SIGN_ECDSA_SECP256R1_SHA256}, |
| 522 | {EVP_PKEY_EC, NID_sha384, SSL_SIGN_ECDSA_SECP384R1_SHA384}, |
| 523 | {EVP_PKEY_EC, NID_sha512, SSL_SIGN_ECDSA_SECP521R1_SHA512}, |
| 524 | {EVP_PKEY_ED25519, NID_undef, SSL_SIGN_ED25519}, |
| 525 | }; |
| 526 | |
| 527 | static bool parse_sigalg_pairs(Array<uint16_t> *out, const int *values, |
| 528 | size_t num_values) { |
| 529 | if ((num_values & 1) == 1) { |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | const size_t num_pairs = num_values / 2; |
| 534 | if (!out->Init(num_pairs)) { |
| 535 | return false; |
| 536 | } |
| 537 | |
| 538 | for (size_t i = 0; i < num_values; i += 2) { |
| 539 | const int hash_nid = values[i]; |
| 540 | const int pkey_type = values[i+1]; |
| 541 | |
| 542 | bool found = false; |
| 543 | for (const auto &candidate : kSignatureAlgorithmsMapping) { |
| 544 | if (candidate.pkey_type == pkey_type && candidate.hash_nid == hash_nid) { |
| 545 | (*out)[i / 2] = candidate.signature_algorithm; |
| 546 | found = true; |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (!found) { |
| 552 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 553 | ERR_add_error_dataf("unknown hash:%d pkey:%d", hash_nid, pkey_type); |
| 554 | return false; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | return true; |
| 559 | } |
| 560 | |
| 561 | static int compare_uint16_t(const void *p1, const void *p2) { |
| 562 | uint16_t u1 = *((const uint16_t *)p1); |
| 563 | uint16_t u2 = *((const uint16_t *)p2); |
| 564 | if (u1 < u2) { |
| 565 | return -1; |
| 566 | } else if (u1 > u2) { |
| 567 | return 1; |
| 568 | } else { |
| 569 | return 0; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | static bool sigalgs_unique(Span<const uint16_t> in_sigalgs) { |
David Benjamin | 14c611c | 2019-01-21 20:33:09 +0000 | [diff] [blame] | 574 | if (in_sigalgs.size() < 2) { |
| 575 | return true; |
| 576 | } |
| 577 | |
Adam Langley | 826ce15 | 2018-08-03 10:31:21 -0700 | [diff] [blame] | 578 | Array<uint16_t> sigalgs; |
| 579 | if (!sigalgs.CopyFrom(in_sigalgs)) { |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | qsort(sigalgs.data(), sigalgs.size(), sizeof(uint16_t), compare_uint16_t); |
| 584 | |
| 585 | for (size_t i = 1; i < sigalgs.size(); i++) { |
| 586 | if (sigalgs[i - 1] == sigalgs[i]) { |
| 587 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_SIGNATURE_ALGORITHM); |
| 588 | return false; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | return true; |
| 593 | } |
| 594 | |
| 595 | int SSL_CTX_set1_sigalgs(SSL_CTX *ctx, const int *values, size_t num_values) { |
| 596 | Array<uint16_t> sigalgs; |
| 597 | if (!parse_sigalg_pairs(&sigalgs, values, num_values) || |
| 598 | !sigalgs_unique(sigalgs)) { |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(), |
| 603 | sigalgs.size()) || |
| 604 | !ctx->verify_sigalgs.CopyFrom(sigalgs)) { |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | return 1; |
| 609 | } |
| 610 | |
| 611 | int SSL_set1_sigalgs(SSL *ssl, const int *values, size_t num_values) { |
| 612 | if (!ssl->config) { |
| 613 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | Array<uint16_t> sigalgs; |
| 618 | if (!parse_sigalg_pairs(&sigalgs, values, num_values) || |
| 619 | !sigalgs_unique(sigalgs)) { |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) || |
| 624 | !ssl->config->verify_sigalgs.CopyFrom(sigalgs)) { |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | static bool parse_sigalgs_list(Array<uint16_t> *out, const char *str) { |
| 632 | // str looks like "RSA+SHA1:ECDSA+SHA256:ecdsa_secp256r1_sha256". |
| 633 | |
| 634 | // Count colons to give the number of output elements from any successful |
| 635 | // parse. |
| 636 | size_t num_elements = 1; |
| 637 | size_t len = 0; |
| 638 | for (const char *p = str; *p; p++) { |
| 639 | len++; |
| 640 | if (*p == ':') { |
| 641 | num_elements++; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | if (!out->Init(num_elements)) { |
| 646 | return false; |
| 647 | } |
| 648 | size_t out_i = 0; |
| 649 | |
| 650 | enum { |
| 651 | pkey_or_name, |
| 652 | hash_name, |
| 653 | } state = pkey_or_name; |
| 654 | |
| 655 | char buf[kMaxSignatureAlgorithmNameLen]; |
| 656 | // buf_used is always < sizeof(buf). I.e. it's always safe to write |
| 657 | // buf[buf_used] = 0. |
| 658 | size_t buf_used = 0; |
| 659 | |
| 660 | int pkey_type = 0, hash_nid = 0; |
| 661 | |
| 662 | // Note that the loop runs to len+1, i.e. it'll process the terminating NUL. |
| 663 | for (size_t offset = 0; offset < len+1; offset++) { |
| 664 | const char c = str[offset]; |
| 665 | |
| 666 | switch (c) { |
| 667 | case '+': |
| 668 | if (state == hash_name) { |
| 669 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 670 | ERR_add_error_dataf("+ found in hash name at offset %zu", offset); |
| 671 | return false; |
| 672 | } |
| 673 | if (buf_used == 0) { |
| 674 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 675 | ERR_add_error_dataf("empty public key type at offset %zu", offset); |
| 676 | return false; |
| 677 | } |
| 678 | buf[buf_used] = 0; |
| 679 | |
| 680 | if (strcmp(buf, "RSA") == 0) { |
| 681 | pkey_type = EVP_PKEY_RSA; |
| 682 | } else if (strcmp(buf, "RSA-PSS") == 0 || |
| 683 | strcmp(buf, "PSS") == 0) { |
| 684 | pkey_type = EVP_PKEY_RSA_PSS; |
| 685 | } else if (strcmp(buf, "ECDSA") == 0) { |
| 686 | pkey_type = EVP_PKEY_EC; |
| 687 | } else { |
| 688 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 689 | ERR_add_error_dataf("unknown public key type '%s'", buf); |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | state = hash_name; |
| 694 | buf_used = 0; |
| 695 | break; |
| 696 | |
| 697 | case ':': |
| 698 | OPENSSL_FALLTHROUGH; |
| 699 | case 0: |
| 700 | if (buf_used == 0) { |
| 701 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 702 | ERR_add_error_dataf("empty element at offset %zu", offset); |
| 703 | return false; |
| 704 | } |
| 705 | |
| 706 | buf[buf_used] = 0; |
| 707 | |
| 708 | if (state == pkey_or_name) { |
| 709 | // No '+' was seen thus this is a TLS 1.3-style name. |
| 710 | bool found = false; |
| 711 | for (const auto &candidate : kSignatureAlgorithmNames) { |
| 712 | if (strcmp(candidate.name, buf) == 0) { |
| 713 | assert(out_i < num_elements); |
| 714 | (*out)[out_i++] = candidate.signature_algorithm; |
| 715 | found = true; |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if (!found) { |
| 721 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 722 | ERR_add_error_dataf("unknown signature algorithm '%s'", buf); |
| 723 | return false; |
| 724 | } |
| 725 | } else { |
| 726 | if (strcmp(buf, "SHA1") == 0) { |
| 727 | hash_nid = NID_sha1; |
| 728 | } else if (strcmp(buf, "SHA256") == 0) { |
| 729 | hash_nid = NID_sha256; |
| 730 | } else if (strcmp(buf, "SHA384") == 0) { |
| 731 | hash_nid = NID_sha384; |
| 732 | } else if (strcmp(buf, "SHA512") == 0) { |
| 733 | hash_nid = NID_sha512; |
| 734 | } else { |
| 735 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 736 | ERR_add_error_dataf("unknown hash function '%s'", buf); |
| 737 | return false; |
| 738 | } |
| 739 | |
| 740 | bool found = false; |
| 741 | for (const auto &candidate : kSignatureAlgorithmsMapping) { |
| 742 | if (candidate.pkey_type == pkey_type && |
| 743 | candidate.hash_nid == hash_nid) { |
| 744 | assert(out_i < num_elements); |
| 745 | (*out)[out_i++] = candidate.signature_algorithm; |
| 746 | found = true; |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | if (!found) { |
| 752 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 753 | ERR_add_error_dataf("unknown pkey:%d hash:%s", pkey_type, buf); |
| 754 | return false; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | state = pkey_or_name; |
| 759 | buf_used = 0; |
| 760 | break; |
| 761 | |
| 762 | default: |
| 763 | if (buf_used == sizeof(buf) - 1) { |
| 764 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 765 | ERR_add_error_dataf("substring too long at offset %zu", offset); |
| 766 | return false; |
| 767 | } |
| 768 | |
| 769 | if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || |
| 770 | (c >= 'A' && c <= 'Z') || c == '-' || c == '_') { |
| 771 | buf[buf_used++] = c; |
| 772 | } else { |
| 773 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 774 | ERR_add_error_dataf("invalid character 0x%02x at offest %zu", c, |
| 775 | offset); |
| 776 | return false; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | assert(out_i == out->size()); |
| 782 | return true; |
| 783 | } |
| 784 | |
| 785 | int SSL_CTX_set1_sigalgs_list(SSL_CTX *ctx, const char *str) { |
| 786 | Array<uint16_t> sigalgs; |
| 787 | if (!parse_sigalgs_list(&sigalgs, str) || |
| 788 | !sigalgs_unique(sigalgs)) { |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(), |
| 793 | sigalgs.size()) || |
David Benjamin | f0a815c | 2020-02-03 20:03:52 -0500 | [diff] [blame] | 794 | !SSL_CTX_set_verify_algorithm_prefs(ctx, sigalgs.data(), |
| 795 | sigalgs.size())) { |
Adam Langley | 826ce15 | 2018-08-03 10:31:21 -0700 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | return 1; |
| 800 | } |
| 801 | |
| 802 | int SSL_set1_sigalgs_list(SSL *ssl, const char *str) { |
| 803 | if (!ssl->config) { |
| 804 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | Array<uint16_t> sigalgs; |
| 809 | if (!parse_sigalgs_list(&sigalgs, str) || |
| 810 | !sigalgs_unique(sigalgs)) { |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) || |
David Benjamin | f0a815c | 2020-02-03 20:03:52 -0500 | [diff] [blame] | 815 | !SSL_set_verify_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size())) { |
Adam Langley | 826ce15 | 2018-08-03 10:31:21 -0700 | [diff] [blame] | 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | return 1; |
| 820 | } |
| 821 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 822 | int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs, |
| 823 | size_t num_prefs) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 824 | return ctx->verify_sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs)); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 825 | } |
David Benjamin | f0a815c | 2020-02-03 20:03:52 -0500 | [diff] [blame] | 826 | |
| 827 | int SSL_set_verify_algorithm_prefs(SSL *ssl, const uint16_t *prefs, |
| 828 | size_t num_prefs) { |
| 829 | if (!ssl->config) { |
| 830 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | return ssl->config->verify_sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs)); |
| 835 | } |