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 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 72 | namespace bssl { |
| 73 | |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 74 | int 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 | |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 79 | static int ssl_set_pkey(CERT *cert, EVP_PKEY *pkey) { |
| 80 | if (!ssl_is_key_type_supported(pkey->type)) { |
| 81 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
Adam Langley | 52940c4 | 2017-02-01 12:40:31 -0800 | [diff] [blame] | 85 | if (cert->chain != NULL && |
| 86 | sk_CRYPTO_BUFFER_value(cert->chain, 0) != NULL && |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [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)) { |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | EVP_PKEY_free(cert->privatekey); |
| 93 | EVP_PKEY_up_ref(pkey); |
| 94 | cert->privatekey = pkey; |
| 95 | |
| 96 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 97 | } |
| 98 | |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 99 | typedef struct { |
| 100 | uint16_t sigalg; |
| 101 | int pkey_type; |
| 102 | int curve; |
| 103 | const EVP_MD *(*digest_func)(void); |
| 104 | char is_rsa_pss; |
| 105 | } SSL_SIGNATURE_ALGORITHM; |
| 106 | |
| 107 | static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = { |
| 108 | {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1, 0}, |
| 109 | {SSL_SIGN_RSA_PKCS1_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_sha1, 0}, |
| 110 | {SSL_SIGN_RSA_PKCS1_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 0}, |
| 111 | {SSL_SIGN_RSA_PKCS1_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 0}, |
| 112 | {SSL_SIGN_RSA_PKCS1_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 0}, |
| 113 | |
| 114 | {SSL_SIGN_RSA_PSS_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 1}, |
| 115 | {SSL_SIGN_RSA_PSS_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 1}, |
| 116 | {SSL_SIGN_RSA_PSS_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 1}, |
| 117 | |
| 118 | {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, 0}, |
| 119 | {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1, |
| 120 | &EVP_sha256, 0}, |
| 121 | {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384, |
| 122 | 0}, |
| 123 | {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512, |
| 124 | 0}, |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 125 | |
| 126 | {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, NULL, 0}, |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | static const SSL_SIGNATURE_ALGORITHM *get_signature_algorithm(uint16_t sigalg) { |
| 130 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kSignatureAlgorithms); i++) { |
| 131 | if (kSignatureAlgorithms[i].sigalg == sigalg) { |
| 132 | return &kSignatureAlgorithms[i]; |
| 133 | } |
| 134 | } |
| 135 | return NULL; |
| 136 | } |
| 137 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 138 | int ssl_has_private_key(const SSL *ssl) { |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 139 | return ssl->cert->privatekey != NULL || ssl->cert->key_method != NULL; |
| 140 | } |
| 141 | |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 142 | static int pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey, |
| 143 | uint16_t sigalg) { |
| 144 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
| 145 | if (alg == NULL || |
| 146 | EVP_PKEY_id(pkey) != alg->pkey_type) { |
| 147 | return 0; |
David Benjamin | d246b81 | 2016-07-08 15:07:02 -0700 | [diff] [blame] | 148 | } |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 149 | |
| 150 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 151 | /* RSA keys may only be used with RSA-PSS. */ |
| 152 | if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) { |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* EC keys have a curve requirement. */ |
| 157 | if (alg->pkey_type == EVP_PKEY_EC && |
| 158 | (alg->curve == NID_undef || |
| 159 | EC_GROUP_get_curve_name( |
| 160 | EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) { |
| 161 | return 0; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return 1; |
David Benjamin | d246b81 | 2016-07-08 15:07:02 -0700 | [diff] [blame] | 166 | } |
| 167 | |
David Benjamin | 1967094 | 2017-05-31 19:07:31 -0400 | [diff] [blame] | 168 | static int setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey, uint16_t sigalg, |
| 169 | int is_verify) { |
| 170 | if (!pkey_supports_algorithm(ssl, pkey, sigalg)) { |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 171 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
| 172 | return 0; |
David Benjamin | 887c300 | 2016-07-08 16:15:32 -0700 | [diff] [blame] | 173 | } |
David Benjamin | a2d81f1 | 2016-07-08 15:42:16 -0700 | [diff] [blame] | 174 | |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 175 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
David Benjamin | 1967094 | 2017-05-31 19:07:31 -0400 | [diff] [blame] | 176 | const EVP_MD *digest = alg->digest_func != NULL ? alg->digest_func() : NULL; |
| 177 | EVP_PKEY_CTX *pctx; |
| 178 | if (is_verify) { |
| 179 | if (!EVP_DigestVerifyInit(ctx, &pctx, digest, NULL, pkey)) { |
| 180 | return 0; |
| 181 | } |
| 182 | } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) { |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 183 | return 0; |
David Benjamin | a2d81f1 | 2016-07-08 15:42:16 -0700 | [diff] [blame] | 184 | } |
David Benjamin | a2d81f1 | 2016-07-08 15:42:16 -0700 | [diff] [blame] | 185 | |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 186 | if (alg->is_rsa_pss) { |
David Benjamin | 1967094 | 2017-05-31 19:07:31 -0400 | [diff] [blame] | 187 | if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) || |
| 188 | !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) { |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 189 | return 0; |
David Benjamin | 76feb1f | 2017-03-28 14:17:01 -0500 | [diff] [blame] | 190 | } |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 191 | } |
| 192 | |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 193 | return 1; |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 194 | } |
| 195 | |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 196 | static int legacy_sign_digest_supported(const SSL_SIGNATURE_ALGORITHM *alg) { |
| 197 | return (alg->pkey_type == EVP_PKEY_EC || alg->pkey_type == EVP_PKEY_RSA) && |
| 198 | !alg->is_rsa_pss; |
| 199 | } |
| 200 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 201 | static enum ssl_private_key_result_t legacy_sign( |
| 202 | SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, uint16_t sigalg, |
| 203 | const uint8_t *in, size_t in_len) { |
| 204 | /* TODO(davidben): Remove support for |sign_digest|-only |
| 205 | * |SSL_PRIVATE_KEY_METHOD|s. */ |
| 206 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
| 207 | if (alg == NULL || !legacy_sign_digest_supported(alg)) { |
| 208 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY); |
| 209 | return ssl_private_key_failure; |
| 210 | } |
| 211 | |
| 212 | const EVP_MD *md = alg->digest_func(); |
| 213 | uint8_t hash[EVP_MAX_MD_SIZE]; |
| 214 | unsigned hash_len; |
| 215 | if (!EVP_Digest(in, in_len, hash, &hash_len, md, NULL)) { |
| 216 | return ssl_private_key_failure; |
| 217 | } |
| 218 | |
| 219 | return ssl->cert->key_method->sign_digest(ssl, out, out_len, max_out, md, |
| 220 | hash, hash_len); |
| 221 | } |
| 222 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 223 | enum ssl_private_key_result_t ssl_private_key_sign( |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 224 | SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out, |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 225 | uint16_t sigalg, const uint8_t *in, size_t in_len) { |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 226 | SSL *const ssl = hs->ssl; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 227 | if (ssl->cert->key_method != NULL) { |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 228 | enum ssl_private_key_result_t ret; |
| 229 | if (hs->pending_private_key_op) { |
| 230 | ret = ssl->cert->key_method->complete(ssl, out, out_len, max_out); |
| 231 | } else { |
| 232 | ret = (ssl->cert->key_method->sign != NULL |
| 233 | ? ssl->cert->key_method->sign |
| 234 | : legacy_sign)(ssl, out, out_len, max_out, sigalg, in, in_len); |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 235 | } |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 236 | hs->pending_private_key_op = ret == ssl_private_key_retry; |
| 237 | return ret; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 238 | } |
| 239 | |
David Benjamin | 76feb1f | 2017-03-28 14:17:01 -0500 | [diff] [blame] | 240 | *out_len = max_out; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 241 | ScopedEVP_MD_CTX ctx; |
| 242 | if (!setup_ctx(ssl, ctx.get(), ssl->cert->privatekey, sigalg, 0 /* sign */) || |
| 243 | !EVP_DigestSign(ctx.get(), out, out_len, in, in_len)) { |
| 244 | return ssl_private_key_failure; |
| 245 | } |
| 246 | return ssl_private_key_success; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 247 | } |
| 248 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 249 | int ssl_public_key_verify(SSL *ssl, const uint8_t *signature, |
David Benjamin | 1967094 | 2017-05-31 19:07:31 -0400 | [diff] [blame] | 250 | size_t signature_len, uint16_t sigalg, EVP_PKEY *pkey, |
| 251 | const uint8_t *in, size_t in_len) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 252 | ScopedEVP_MD_CTX ctx; |
| 253 | return setup_ctx(ssl, ctx.get(), pkey, sigalg, 1 /* verify */) && |
| 254 | EVP_DigestVerify(ctx.get(), signature, signature_len, in, in_len); |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 255 | } |
| 256 | |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 257 | enum ssl_private_key_result_t ssl_private_key_decrypt( |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 258 | SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out, |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 259 | const uint8_t *in, size_t in_len) { |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 260 | SSL *const ssl = hs->ssl; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 261 | if (ssl->cert->key_method != NULL) { |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 262 | enum ssl_private_key_result_t ret; |
| 263 | if (hs->pending_private_key_op) { |
| 264 | ret = ssl->cert->key_method->complete(ssl, out, out_len, max_out); |
| 265 | } else { |
| 266 | ret = ssl->cert->key_method->decrypt(ssl, out, out_len, max_out, in, |
| 267 | in_len); |
| 268 | } |
| 269 | hs->pending_private_key_op = ret == ssl_private_key_retry; |
| 270 | return ret; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 271 | } |
| 272 | |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 273 | RSA *rsa = EVP_PKEY_get0_RSA(ssl->cert->privatekey); |
| 274 | if (rsa == NULL) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 275 | /* Decrypt operations are only supported for RSA keys. */ |
| 276 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 277 | return ssl_private_key_failure; |
| 278 | } |
| 279 | |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 280 | /* Decrypt with no padding. PKCS#1 padding will be removed as part |
| 281 | * of the timing-sensitive code by the caller. */ |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 282 | if (!RSA_decrypt(rsa, out_len, out, max_out, in, in_len, RSA_NO_PADDING)) { |
| 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 | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 288 | int ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs, |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 289 | uint16_t sigalg) { |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 290 | SSL *const ssl = hs->ssl; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 291 | if (!pkey_supports_algorithm(ssl, hs->local_pubkey.get(), sigalg)) { |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 292 | return 0; |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 293 | } |
| 294 | |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [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 | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 304 | return 0; |
| 305 | } |
David Benjamin | 7944a9f | 2016-07-12 22:27:01 -0400 | [diff] [blame] | 306 | |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 307 | /* Newer algorithms require message-based private keys. |
| 308 | * TODO(davidben): Remove this check when sign_digest is gone. */ |
| 309 | if (ssl->cert->key_method != NULL && |
| 310 | ssl->cert->key_method->sign == NULL && |
| 311 | !legacy_sign_digest_supported(alg)) { |
| 312 | return 0; |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 313 | } |
| 314 | |
David Benjamin | 6114c3c | 2017-03-30 16:37:54 -0500 | [diff] [blame] | 315 | return 1; |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 316 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 317 | |
| 318 | } // namespace bssl |
| 319 | |
| 320 | using namespace bssl; |
| 321 | |
| 322 | int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 323 | if (rsa == NULL) { |
| 324 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 325 | return 0; |
| 326 | } |
| 327 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame^] | 328 | UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 329 | if (!pkey || |
| 330 | !EVP_PKEY_set1_RSA(pkey.get(), rsa)) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 331 | OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB); |
| 332 | return 0; |
| 333 | } |
| 334 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame^] | 335 | return ssl_set_pkey(ssl->cert, pkey.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) { |
| 339 | UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len)); |
| 340 | if (!rsa) { |
| 341 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | return SSL_use_RSAPrivateKey(ssl, rsa.get()); |
| 346 | } |
| 347 | |
| 348 | int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) { |
| 349 | if (pkey == NULL) { |
| 350 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | return ssl_set_pkey(ssl->cert, pkey); |
| 355 | } |
| 356 | |
| 357 | int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der, |
| 358 | size_t der_len) { |
| 359 | if (der_len > LONG_MAX) { |
| 360 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | const uint8_t *p = der; |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame^] | 365 | UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len)); |
| 366 | if (!pkey || p != der + der_len) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 367 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame^] | 371 | return SSL_use_PrivateKey(ssl, pkey.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 375 | if (rsa == NULL) { |
| 376 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 377 | return 0; |
| 378 | } |
| 379 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame^] | 380 | UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 381 | if (!pkey || |
| 382 | !EVP_PKEY_set1_RSA(pkey.get(), rsa)) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 383 | OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB); |
| 384 | return 0; |
| 385 | } |
| 386 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame^] | 387 | return ssl_set_pkey(ctx->cert, pkey.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der, |
| 391 | size_t der_len) { |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame^] | 392 | UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len)); |
| 393 | if (!rsa) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 394 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
| 395 | return 0; |
| 396 | } |
| 397 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame^] | 398 | return SSL_CTX_use_RSAPrivateKey(ctx, rsa.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) { |
| 402 | if (pkey == NULL) { |
| 403 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | return ssl_set_pkey(ctx->cert, pkey); |
| 408 | } |
| 409 | |
| 410 | int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der, |
| 411 | size_t der_len) { |
| 412 | if (der_len > LONG_MAX) { |
| 413 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | const uint8_t *p = der; |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame^] | 418 | UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len)); |
| 419 | if (!pkey || p != der + der_len) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 420 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame^] | 424 | return SSL_CTX_use_PrivateKey(ctx, pkey.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void SSL_set_private_key_method(SSL *ssl, |
| 428 | const SSL_PRIVATE_KEY_METHOD *key_method) { |
| 429 | ssl->cert->key_method = key_method; |
| 430 | } |
| 431 | |
| 432 | void SSL_CTX_set_private_key_method(SSL_CTX *ctx, |
| 433 | const SSL_PRIVATE_KEY_METHOD *key_method) { |
| 434 | ctx->cert->key_method = key_method; |
| 435 | } |
| 436 | |
| 437 | static int set_algorithm_prefs(uint16_t **out_prefs, size_t *out_num_prefs, |
| 438 | const uint16_t *prefs, size_t num_prefs) { |
| 439 | OPENSSL_free(*out_prefs); |
| 440 | |
| 441 | *out_num_prefs = 0; |
| 442 | *out_prefs = (uint16_t *)BUF_memdup(prefs, num_prefs * sizeof(prefs[0])); |
| 443 | if (*out_prefs == NULL) { |
| 444 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 445 | return 0; |
| 446 | } |
| 447 | *out_num_prefs = num_prefs; |
| 448 | |
| 449 | return 1; |
| 450 | } |
| 451 | |
| 452 | int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs, |
| 453 | size_t num_prefs) { |
| 454 | return set_algorithm_prefs(&ctx->cert->sigalgs, &ctx->cert->num_sigalgs, |
| 455 | prefs, num_prefs); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs, |
| 460 | size_t num_prefs) { |
| 461 | return set_algorithm_prefs(&ssl->cert->sigalgs, &ssl->cert->num_sigalgs, |
| 462 | prefs, num_prefs); |
| 463 | } |
| 464 | |
| 465 | int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs, |
| 466 | size_t num_prefs) { |
| 467 | return set_algorithm_prefs(&ctx->verify_sigalgs, &ctx->num_verify_sigalgs, |
| 468 | prefs, num_prefs); |
| 469 | } |
| 470 | |
| 471 | int SSL_set_private_key_digest_prefs(SSL *ssl, const int *digest_nids, |
| 472 | size_t num_digests) { |
| 473 | OPENSSL_free(ssl->cert->sigalgs); |
| 474 | |
| 475 | static_assert(sizeof(int) >= 2 * sizeof(uint16_t), |
| 476 | "sigalgs allocation may overflow"); |
| 477 | |
| 478 | ssl->cert->num_sigalgs = 0; |
| 479 | ssl->cert->sigalgs = |
| 480 | (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * 2 * num_digests); |
| 481 | if (ssl->cert->sigalgs == NULL) { |
| 482 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /* Convert the digest list to a signature algorithms list. |
| 487 | * |
| 488 | * TODO(davidben): Replace this API with one that can express RSA-PSS, etc. */ |
| 489 | for (size_t i = 0; i < num_digests; i++) { |
| 490 | switch (digest_nids[i]) { |
| 491 | case NID_sha1: |
| 492 | ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA1; |
| 493 | ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] = SSL_SIGN_ECDSA_SHA1; |
| 494 | ssl->cert->num_sigalgs += 2; |
| 495 | break; |
| 496 | case NID_sha256: |
| 497 | ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA256; |
| 498 | ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] = |
| 499 | SSL_SIGN_ECDSA_SECP256R1_SHA256; |
| 500 | ssl->cert->num_sigalgs += 2; |
| 501 | break; |
| 502 | case NID_sha384: |
| 503 | ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA384; |
| 504 | ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] = |
| 505 | SSL_SIGN_ECDSA_SECP384R1_SHA384; |
| 506 | ssl->cert->num_sigalgs += 2; |
| 507 | break; |
| 508 | case NID_sha512: |
| 509 | ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA512; |
| 510 | ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] = |
| 511 | SSL_SIGN_ECDSA_SECP521R1_SHA512; |
| 512 | ssl->cert->num_sigalgs += 2; |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | return 1; |
| 518 | } |