Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <openssl/ssl.h> |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 22 | #include <openssl/bn.h> |
| 23 | #include <openssl/bytestring.h> |
| 24 | #include <openssl/curve25519.h> |
| 25 | #include <openssl/ec.h> |
| 26 | #include <openssl/err.h> |
| 27 | #include <openssl/mem.h> |
| 28 | #include <openssl/nid.h> |
| 29 | |
| 30 | #include "internal.h" |
| 31 | #include "../crypto/internal.h" |
| 32 | |
| 33 | |
| 34 | namespace bssl { |
| 35 | |
| 36 | namespace { |
| 37 | |
| 38 | class ECKeyShare : public SSLKeyShare { |
| 39 | public: |
| 40 | ECKeyShare(int nid, uint16_t group_id) : nid_(nid), group_id_(group_id) {} |
| 41 | ~ECKeyShare() override {} |
| 42 | |
| 43 | uint16_t GroupID() const override { return group_id_; } |
| 44 | |
| 45 | bool Offer(CBB *out) override { |
| 46 | assert(!private_key_); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 47 | // Set up a shared |BN_CTX| for all operations. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 48 | UniquePtr<BN_CTX> bn_ctx(BN_CTX_new()); |
| 49 | if (!bn_ctx) { |
| 50 | return false; |
| 51 | } |
| 52 | BN_CTXScope scope(bn_ctx.get()); |
| 53 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 54 | // Generate a private key. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 55 | UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid_)); |
| 56 | private_key_.reset(BN_new()); |
| 57 | if (!group || !private_key_ || |
| 58 | !BN_rand_range_ex(private_key_.get(), 1, |
| 59 | EC_GROUP_get0_order(group.get()))) { |
| 60 | return false; |
| 61 | } |
| 62 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 63 | // Compute the corresponding public key and serialize it. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 64 | UniquePtr<EC_POINT> public_key(EC_POINT_new(group.get())); |
| 65 | if (!public_key || |
| 66 | !EC_POINT_mul(group.get(), public_key.get(), private_key_.get(), NULL, |
| 67 | NULL, bn_ctx.get()) || |
| 68 | !EC_POINT_point2cbb(out, group.get(), public_key.get(), |
| 69 | POINT_CONVERSION_UNCOMPRESSED, bn_ctx.get())) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 76 | bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert, |
| 77 | Span<const uint8_t> peer_key) override { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 78 | assert(private_key_); |
| 79 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 80 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 81 | // Set up a shared |BN_CTX| for all operations. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 82 | UniquePtr<BN_CTX> bn_ctx(BN_CTX_new()); |
| 83 | if (!bn_ctx) { |
| 84 | return false; |
| 85 | } |
| 86 | BN_CTXScope scope(bn_ctx.get()); |
| 87 | |
| 88 | UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid_)); |
| 89 | if (!group) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | UniquePtr<EC_POINT> peer_point(EC_POINT_new(group.get())); |
| 94 | UniquePtr<EC_POINT> result(EC_POINT_new(group.get())); |
| 95 | BIGNUM *x = BN_CTX_get(bn_ctx.get()); |
| 96 | if (!peer_point || !result || !x) { |
| 97 | return false; |
| 98 | } |
| 99 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame] | 100 | if (peer_key.empty() || peer_key[0] != POINT_CONVERSION_UNCOMPRESSED || |
| 101 | !EC_POINT_oct2point(group.get(), peer_point.get(), peer_key.data(), |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 102 | peer_key.size(), bn_ctx.get())) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame] | 103 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 104 | *out_alert = SSL_AD_DECODE_ERROR; |
| 105 | return false; |
| 106 | } |
| 107 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 108 | // Compute the x-coordinate of |peer_key| * |private_key_|. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 109 | if (!EC_POINT_mul(group.get(), result.get(), NULL, peer_point.get(), |
| 110 | private_key_.get(), bn_ctx.get()) || |
| 111 | !EC_POINT_get_affine_coordinates_GFp(group.get(), result.get(), x, NULL, |
| 112 | bn_ctx.get())) { |
| 113 | return false; |
| 114 | } |
| 115 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 116 | // Encode the x-coordinate left-padded with zeros. |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 117 | Array<uint8_t> secret; |
| 118 | if (!secret.Init((EC_GROUP_get_degree(group.get()) + 7) / 8) || |
| 119 | !BN_bn2bin_padded(secret.data(), secret.size(), x)) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 123 | *out_secret = std::move(secret); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 124 | return true; |
| 125 | } |
| 126 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 127 | bool Serialize(CBB *out) override { |
| 128 | assert(private_key_); |
| 129 | CBB cbb; |
| 130 | UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid_)); |
| 131 | // Padding is added to avoid leaking the length. |
| 132 | size_t len = BN_num_bytes(EC_GROUP_get0_order(group.get())); |
| 133 | if (!CBB_add_asn1_uint64(out, group_id_) || |
| 134 | !CBB_add_asn1(out, &cbb, CBS_ASN1_OCTETSTRING) || |
| 135 | !BN_bn2cbb_padded(&cbb, len, private_key_.get()) || |
| 136 | !CBB_flush(out)) { |
| 137 | return false; |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | bool Deserialize(CBS *in) override { |
| 143 | assert(!private_key_); |
| 144 | CBS private_key; |
| 145 | if (!CBS_get_asn1(in, &private_key, CBS_ASN1_OCTETSTRING)) { |
| 146 | return false; |
| 147 | } |
| 148 | private_key_.reset(BN_bin2bn(CBS_data(&private_key), |
| 149 | CBS_len(&private_key), nullptr)); |
| 150 | return private_key_ != nullptr; |
| 151 | } |
| 152 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 153 | private: |
| 154 | UniquePtr<BIGNUM> private_key_; |
| 155 | int nid_; |
| 156 | uint16_t group_id_; |
| 157 | }; |
| 158 | |
| 159 | class X25519KeyShare : public SSLKeyShare { |
| 160 | public: |
| 161 | X25519KeyShare() {} |
| 162 | ~X25519KeyShare() override { |
| 163 | OPENSSL_cleanse(private_key_, sizeof(private_key_)); |
| 164 | } |
| 165 | |
| 166 | uint16_t GroupID() const override { return SSL_CURVE_X25519; } |
| 167 | |
| 168 | bool Offer(CBB *out) override { |
| 169 | uint8_t public_key[32]; |
| 170 | X25519_keypair(public_key, private_key_); |
| 171 | return !!CBB_add_bytes(out, public_key, sizeof(public_key)); |
| 172 | } |
| 173 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 174 | bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert, |
| 175 | Span<const uint8_t> peer_key) override { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 176 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 177 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 178 | Array<uint8_t> secret; |
| 179 | if (!secret.Init(32)) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 180 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 181 | return false; |
| 182 | } |
| 183 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 184 | if (peer_key.size() != 32 || |
| 185 | !X25519(secret.data(), private_key_, peer_key.data())) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 186 | *out_alert = SSL_AD_DECODE_ERROR; |
| 187 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT); |
| 188 | return false; |
| 189 | } |
| 190 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 191 | *out_secret = std::move(secret); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 192 | return true; |
| 193 | } |
| 194 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 195 | bool Serialize(CBB *out) override { |
| 196 | return (CBB_add_asn1_uint64(out, GroupID()) && |
| 197 | CBB_add_asn1_octet_string(out, private_key_, sizeof(private_key_))); |
| 198 | } |
| 199 | |
| 200 | bool Deserialize(CBS *in) override { |
| 201 | CBS key; |
| 202 | if (!CBS_get_asn1(in, &key, CBS_ASN1_OCTETSTRING) || |
| 203 | CBS_len(&key) != sizeof(private_key_) || |
| 204 | !CBS_copy_bytes(&key, private_key_, sizeof(private_key_))) { |
| 205 | return false; |
| 206 | } |
| 207 | return true; |
| 208 | } |
| 209 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 210 | private: |
| 211 | uint8_t private_key_[32]; |
| 212 | }; |
| 213 | |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 214 | CONSTEXPR_ARRAY struct { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 215 | int nid; |
| 216 | uint16_t group_id; |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 217 | const char name[8], alias[11]; |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 218 | } kNamedGroups[] = { |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 219 | {NID_secp224r1, SSL_CURVE_SECP224R1, "P-224", "secp224r1"}, |
| 220 | {NID_X9_62_prime256v1, SSL_CURVE_SECP256R1, "P-256", "prime256v1"}, |
| 221 | {NID_secp384r1, SSL_CURVE_SECP384R1, "P-384", "secp384r1"}, |
| 222 | {NID_secp521r1, SSL_CURVE_SECP521R1, "P-521", "secp521r1"}, |
| 223 | {NID_X25519, SSL_CURVE_X25519, "X25519", "x25519"}, |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | } // namespace |
| 227 | |
| 228 | UniquePtr<SSLKeyShare> SSLKeyShare::Create(uint16_t group_id) { |
| 229 | switch (group_id) { |
| 230 | case SSL_CURVE_SECP224R1: |
| 231 | return UniquePtr<SSLKeyShare>( |
| 232 | New<ECKeyShare>(NID_secp224r1, SSL_CURVE_SECP224R1)); |
| 233 | case SSL_CURVE_SECP256R1: |
| 234 | return UniquePtr<SSLKeyShare>( |
| 235 | New<ECKeyShare>(NID_X9_62_prime256v1, SSL_CURVE_SECP256R1)); |
| 236 | case SSL_CURVE_SECP384R1: |
| 237 | return UniquePtr<SSLKeyShare>( |
| 238 | New<ECKeyShare>(NID_secp384r1, SSL_CURVE_SECP384R1)); |
| 239 | case SSL_CURVE_SECP521R1: |
| 240 | return UniquePtr<SSLKeyShare>( |
| 241 | New<ECKeyShare>(NID_secp521r1, SSL_CURVE_SECP521R1)); |
| 242 | case SSL_CURVE_X25519: |
| 243 | return UniquePtr<SSLKeyShare>(New<X25519KeyShare>()); |
| 244 | default: |
| 245 | return nullptr; |
| 246 | } |
| 247 | } |
| 248 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 249 | UniquePtr<SSLKeyShare> SSLKeyShare::Create(CBS *in) { |
| 250 | uint64_t group; |
Robert Sloan | 15c0b35 | 2018-04-16 08:36:46 -0700 | [diff] [blame] | 251 | if (!CBS_get_asn1_uint64(in, &group) || group > 0xffff) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 252 | return nullptr; |
| 253 | } |
Robert Sloan | 15c0b35 | 2018-04-16 08:36:46 -0700 | [diff] [blame] | 254 | UniquePtr<SSLKeyShare> key_share = Create(static_cast<uint16_t>(group)); |
| 255 | if (!key_share || !key_share->Deserialize(in)) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 256 | return nullptr; |
| 257 | } |
| 258 | return key_share; |
| 259 | } |
| 260 | |
| 261 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 262 | bool SSLKeyShare::Accept(CBB *out_public_key, Array<uint8_t> *out_secret, |
| 263 | uint8_t *out_alert, Span<const uint8_t> peer_key) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 264 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 265 | return Offer(out_public_key) && |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 266 | Finish(out_secret, out_alert, peer_key); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | int ssl_nid_to_group_id(uint16_t *out_group_id, int nid) { |
| 270 | for (const auto &group : kNamedGroups) { |
| 271 | if (group.nid == nid) { |
| 272 | *out_group_id = group.group_id; |
| 273 | return 1; |
| 274 | } |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | int ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len) { |
| 280 | for (const auto &group : kNamedGroups) { |
| 281 | if (len == strlen(group.name) && |
| 282 | !strncmp(group.name, name, len)) { |
| 283 | *out_group_id = group.group_id; |
| 284 | return 1; |
| 285 | } |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 286 | if (len == strlen(group.alias) && |
| 287 | !strncmp(group.alias, name, len)) { |
| 288 | *out_group_id = group.group_id; |
| 289 | return 1; |
| 290 | } |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 291 | } |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | } // namespace bssl |
| 296 | |
| 297 | using namespace bssl; |
| 298 | |
| 299 | const char* SSL_get_curve_name(uint16_t group_id) { |
| 300 | for (const auto &group : kNamedGroups) { |
| 301 | if (group.group_id == group_id) { |
| 302 | return group.name; |
| 303 | } |
| 304 | } |
| 305 | return nullptr; |
| 306 | } |