Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / third_party / boringssl / ssl / ssl_key_share.cc
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
20 #include <utility>
21
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_);
47     // Set up a shared |BN_CTX| for all operations.
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
54     // Generate a private key.
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
63     // Compute the corresponding public key and serialize it.
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
76   bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
77               Span<const uint8_t> peer_key) override {
78     assert(private_key_);
79     *out_alert = SSL_AD_INTERNAL_ERROR;
80
81     // Set up a shared |BN_CTX| for all operations.
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
100     if (peer_key.empty() || peer_key[0] != POINT_CONVERSION_UNCOMPRESSED ||
101         !EC_POINT_oct2point(group.get(), peer_point.get(), peer_key.data(),
102                             peer_key.size(), bn_ctx.get())) {
103       OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
104       *out_alert = SSL_AD_DECODE_ERROR;
105       return false;
106     }
107
108     // Compute the x-coordinate of |peer_key| * |private_key_|.
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
116     // Encode the x-coordinate left-padded with zeros.
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)) {
120       return false;
121     }
122
123     *out_secret = std::move(secret);
124     return true;
125   }
126
127  private:
128   UniquePtr<BIGNUM> private_key_;
129   int nid_;
130   uint16_t group_id_;
131 };
132
133 class X25519KeyShare : public SSLKeyShare {
134  public:
135   X25519KeyShare() {}
136   ~X25519KeyShare() override {
137     OPENSSL_cleanse(private_key_, sizeof(private_key_));
138   }
139
140   uint16_t GroupID() const override { return SSL_CURVE_X25519; }
141
142   bool Offer(CBB *out) override {
143     uint8_t public_key[32];
144     X25519_keypair(public_key, private_key_);
145     return !!CBB_add_bytes(out, public_key, sizeof(public_key));
146   }
147
148   bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
149               Span<const uint8_t> peer_key) override {
150     *out_alert = SSL_AD_INTERNAL_ERROR;
151
152     Array<uint8_t> secret;
153     if (!secret.Init(32)) {
154       OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
155       return false;
156     }
157
158     if (peer_key.size() != 32 ||
159         !X25519(secret.data(), private_key_, peer_key.data())) {
160       *out_alert = SSL_AD_DECODE_ERROR;
161       OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
162       return false;
163     }
164
165     *out_secret = std::move(secret);
166     return true;
167   }
168
169  private:
170   uint8_t private_key_[32];
171 };
172
173 CONSTEXPR_ARRAY struct {
174   int nid;
175   uint16_t group_id;
176   const char name[8], alias[11];
177 } kNamedGroups[] = {
178     {NID_secp224r1, SSL_CURVE_SECP224R1, "P-224", "secp224r1"},
179     {NID_X9_62_prime256v1, SSL_CURVE_SECP256R1, "P-256", "prime256v1"},
180     {NID_secp384r1, SSL_CURVE_SECP384R1, "P-384", "secp384r1"},
181     {NID_secp521r1, SSL_CURVE_SECP521R1, "P-521", "secp521r1"},
182     {NID_X25519, SSL_CURVE_X25519, "X25519", "x25519"},
183 };
184
185 }  // namespace
186
187 UniquePtr<SSLKeyShare> SSLKeyShare::Create(uint16_t group_id) {
188   switch (group_id) {
189     case SSL_CURVE_SECP224R1:
190       return UniquePtr<SSLKeyShare>(
191           New<ECKeyShare>(NID_secp224r1, SSL_CURVE_SECP224R1));
192     case SSL_CURVE_SECP256R1:
193       return UniquePtr<SSLKeyShare>(
194           New<ECKeyShare>(NID_X9_62_prime256v1, SSL_CURVE_SECP256R1));
195     case SSL_CURVE_SECP384R1:
196       return UniquePtr<SSLKeyShare>(
197           New<ECKeyShare>(NID_secp384r1, SSL_CURVE_SECP384R1));
198     case SSL_CURVE_SECP521R1:
199       return UniquePtr<SSLKeyShare>(
200           New<ECKeyShare>(NID_secp521r1, SSL_CURVE_SECP521R1));
201     case SSL_CURVE_X25519:
202       return UniquePtr<SSLKeyShare>(New<X25519KeyShare>());
203     default:
204       return nullptr;
205   }
206 }
207
208 bool SSLKeyShare::Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
209                          uint8_t *out_alert, Span<const uint8_t> peer_key) {
210   *out_alert = SSL_AD_INTERNAL_ERROR;
211   return Offer(out_public_key) &&
212          Finish(out_secret, out_alert, peer_key);
213 }
214
215 int ssl_nid_to_group_id(uint16_t *out_group_id, int nid) {
216   for (const auto &group : kNamedGroups) {
217     if (group.nid == nid) {
218       *out_group_id = group.group_id;
219       return 1;
220     }
221   }
222   return 0;
223 }
224
225 int ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len) {
226   for (const auto &group : kNamedGroups) {
227     if (len == strlen(group.name) &&
228         !strncmp(group.name, name, len)) {
229       *out_group_id = group.group_id;
230       return 1;
231     }
232     if (len == strlen(group.alias) &&
233         !strncmp(group.alias, name, len)) {
234       *out_group_id = group.group_id;
235       return 1;
236     }
237   }
238   return 0;
239 }
240
241 }  // namespace bssl
242
243 using namespace bssl;
244
245 const char* SSL_get_curve_name(uint16_t group_id) {
246   for (const auto &group : kNamedGroups) {
247     if (group.group_id == group_id) {
248       return group.name;
249     }
250   }
251   return nullptr;
252 }