You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

307 line
8.9 KiB

  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. #include <openssl/ssl.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <utility>
  18. #include <openssl/bn.h>
  19. #include <openssl/bytestring.h>
  20. #include <openssl/curve25519.h>
  21. #include <openssl/ec.h>
  22. #include <openssl/err.h>
  23. #include <openssl/mem.h>
  24. #include <openssl/nid.h>
  25. #include "internal.h"
  26. #include "../crypto/internal.h"
  27. namespace bssl {
  28. namespace {
  29. class ECKeyShare : public SSLKeyShare {
  30. public:
  31. ECKeyShare(int nid, uint16_t group_id) : nid_(nid), group_id_(group_id) {}
  32. ~ECKeyShare() override {}
  33. uint16_t GroupID() const override { return group_id_; }
  34. bool Offer(CBB *out) override {
  35. assert(!private_key_);
  36. // Set up a shared |BN_CTX| for all operations.
  37. UniquePtr<BN_CTX> bn_ctx(BN_CTX_new());
  38. if (!bn_ctx) {
  39. return false;
  40. }
  41. BN_CTXScope scope(bn_ctx.get());
  42. // Generate a private key.
  43. UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid_));
  44. private_key_.reset(BN_new());
  45. if (!group || !private_key_ ||
  46. !BN_rand_range_ex(private_key_.get(), 1,
  47. EC_GROUP_get0_order(group.get()))) {
  48. return false;
  49. }
  50. // Compute the corresponding public key and serialize it.
  51. UniquePtr<EC_POINT> public_key(EC_POINT_new(group.get()));
  52. if (!public_key ||
  53. !EC_POINT_mul(group.get(), public_key.get(), private_key_.get(), NULL,
  54. NULL, bn_ctx.get()) ||
  55. !EC_POINT_point2cbb(out, group.get(), public_key.get(),
  56. POINT_CONVERSION_UNCOMPRESSED, bn_ctx.get())) {
  57. return false;
  58. }
  59. return true;
  60. }
  61. bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
  62. Span<const uint8_t> peer_key) override {
  63. assert(private_key_);
  64. *out_alert = SSL_AD_INTERNAL_ERROR;
  65. // Set up a shared |BN_CTX| for all operations.
  66. UniquePtr<BN_CTX> bn_ctx(BN_CTX_new());
  67. if (!bn_ctx) {
  68. return false;
  69. }
  70. BN_CTXScope scope(bn_ctx.get());
  71. UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid_));
  72. if (!group) {
  73. return false;
  74. }
  75. UniquePtr<EC_POINT> peer_point(EC_POINT_new(group.get()));
  76. UniquePtr<EC_POINT> result(EC_POINT_new(group.get()));
  77. BIGNUM *x = BN_CTX_get(bn_ctx.get());
  78. if (!peer_point || !result || !x) {
  79. return false;
  80. }
  81. if (peer_key.empty() || peer_key[0] != POINT_CONVERSION_UNCOMPRESSED ||
  82. !EC_POINT_oct2point(group.get(), peer_point.get(), peer_key.data(),
  83. peer_key.size(), bn_ctx.get())) {
  84. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
  85. *out_alert = SSL_AD_DECODE_ERROR;
  86. return false;
  87. }
  88. // Compute the x-coordinate of |peer_key| * |private_key_|.
  89. if (!EC_POINT_mul(group.get(), result.get(), NULL, peer_point.get(),
  90. private_key_.get(), bn_ctx.get()) ||
  91. !EC_POINT_get_affine_coordinates_GFp(group.get(), result.get(), x, NULL,
  92. bn_ctx.get())) {
  93. return false;
  94. }
  95. // Encode the x-coordinate left-padded with zeros.
  96. Array<uint8_t> secret;
  97. if (!secret.Init((EC_GROUP_get_degree(group.get()) + 7) / 8) ||
  98. !BN_bn2bin_padded(secret.data(), secret.size(), x)) {
  99. return false;
  100. }
  101. *out_secret = std::move(secret);
  102. return true;
  103. }
  104. bool Serialize(CBB *out) override {
  105. assert(private_key_);
  106. CBB cbb;
  107. UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid_));
  108. // Padding is added to avoid leaking the length.
  109. size_t len = BN_num_bytes(EC_GROUP_get0_order(group.get()));
  110. if (!CBB_add_asn1_uint64(out, group_id_) ||
  111. !CBB_add_asn1(out, &cbb, CBS_ASN1_OCTETSTRING) ||
  112. !BN_bn2cbb_padded(&cbb, len, private_key_.get()) ||
  113. !CBB_flush(out)) {
  114. return false;
  115. }
  116. return true;
  117. }
  118. bool Deserialize(CBS *in) override {
  119. assert(!private_key_);
  120. CBS private_key;
  121. if (!CBS_get_asn1(in, &private_key, CBS_ASN1_OCTETSTRING)) {
  122. return false;
  123. }
  124. private_key_.reset(BN_bin2bn(CBS_data(&private_key),
  125. CBS_len(&private_key), nullptr));
  126. return private_key_ != nullptr;
  127. }
  128. private:
  129. UniquePtr<BIGNUM> private_key_;
  130. int nid_;
  131. uint16_t group_id_;
  132. };
  133. class X25519KeyShare : public SSLKeyShare {
  134. public:
  135. X25519KeyShare() {}
  136. ~X25519KeyShare() override {
  137. OPENSSL_cleanse(private_key_, sizeof(private_key_));
  138. }
  139. uint16_t GroupID() const override { return SSL_CURVE_X25519; }
  140. bool Offer(CBB *out) override {
  141. uint8_t public_key[32];
  142. X25519_keypair(public_key, private_key_);
  143. return !!CBB_add_bytes(out, public_key, sizeof(public_key));
  144. }
  145. bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
  146. Span<const uint8_t> peer_key) override {
  147. *out_alert = SSL_AD_INTERNAL_ERROR;
  148. Array<uint8_t> secret;
  149. if (!secret.Init(32)) {
  150. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  151. return false;
  152. }
  153. if (peer_key.size() != 32 ||
  154. !X25519(secret.data(), private_key_, peer_key.data())) {
  155. *out_alert = SSL_AD_DECODE_ERROR;
  156. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
  157. return false;
  158. }
  159. *out_secret = std::move(secret);
  160. return true;
  161. }
  162. bool Serialize(CBB *out) override {
  163. return (CBB_add_asn1_uint64(out, GroupID()) &&
  164. CBB_add_asn1_octet_string(out, private_key_, sizeof(private_key_)));
  165. }
  166. bool Deserialize(CBS *in) override {
  167. CBS key;
  168. if (!CBS_get_asn1(in, &key, CBS_ASN1_OCTETSTRING) ||
  169. CBS_len(&key) != sizeof(private_key_) ||
  170. !CBS_copy_bytes(&key, private_key_, sizeof(private_key_))) {
  171. return false;
  172. }
  173. return true;
  174. }
  175. private:
  176. uint8_t private_key_[32];
  177. };
  178. CONSTEXPR_ARRAY struct {
  179. int nid;
  180. uint16_t group_id;
  181. const char name[8], alias[11];
  182. } kNamedGroups[] = {
  183. {NID_secp224r1, SSL_CURVE_SECP224R1, "P-224", "secp224r1"},
  184. {NID_X9_62_prime256v1, SSL_CURVE_SECP256R1, "P-256", "prime256v1"},
  185. {NID_secp384r1, SSL_CURVE_SECP384R1, "P-384", "secp384r1"},
  186. {NID_secp521r1, SSL_CURVE_SECP521R1, "P-521", "secp521r1"},
  187. {NID_X25519, SSL_CURVE_X25519, "X25519", "x25519"},
  188. };
  189. } // namespace
  190. UniquePtr<SSLKeyShare> SSLKeyShare::Create(uint16_t group_id) {
  191. switch (group_id) {
  192. case SSL_CURVE_SECP224R1:
  193. return UniquePtr<SSLKeyShare>(
  194. New<ECKeyShare>(NID_secp224r1, SSL_CURVE_SECP224R1));
  195. case SSL_CURVE_SECP256R1:
  196. return UniquePtr<SSLKeyShare>(
  197. New<ECKeyShare>(NID_X9_62_prime256v1, SSL_CURVE_SECP256R1));
  198. case SSL_CURVE_SECP384R1:
  199. return UniquePtr<SSLKeyShare>(
  200. New<ECKeyShare>(NID_secp384r1, SSL_CURVE_SECP384R1));
  201. case SSL_CURVE_SECP521R1:
  202. return UniquePtr<SSLKeyShare>(
  203. New<ECKeyShare>(NID_secp521r1, SSL_CURVE_SECP521R1));
  204. case SSL_CURVE_X25519:
  205. return UniquePtr<SSLKeyShare>(New<X25519KeyShare>());
  206. default:
  207. return nullptr;
  208. }
  209. }
  210. UniquePtr<SSLKeyShare> SSLKeyShare::Create(CBS *in) {
  211. uint64_t group;
  212. if (!CBS_get_asn1_uint64(in, &group) || group > 0xffff) {
  213. return nullptr;
  214. }
  215. UniquePtr<SSLKeyShare> key_share = Create(static_cast<uint16_t>(group));
  216. if (!key_share || !key_share->Deserialize(in)) {
  217. return nullptr;
  218. }
  219. return key_share;
  220. }
  221. bool SSLKeyShare::Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
  222. uint8_t *out_alert, Span<const uint8_t> peer_key) {
  223. *out_alert = SSL_AD_INTERNAL_ERROR;
  224. return Offer(out_public_key) &&
  225. Finish(out_secret, out_alert, peer_key);
  226. }
  227. int ssl_nid_to_group_id(uint16_t *out_group_id, int nid) {
  228. for (const auto &group : kNamedGroups) {
  229. if (group.nid == nid) {
  230. *out_group_id = group.group_id;
  231. return 1;
  232. }
  233. }
  234. return 0;
  235. }
  236. int ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len) {
  237. for (const auto &group : kNamedGroups) {
  238. if (len == strlen(group.name) &&
  239. !strncmp(group.name, name, len)) {
  240. *out_group_id = group.group_id;
  241. return 1;
  242. }
  243. if (len == strlen(group.alias) &&
  244. !strncmp(group.alias, name, len)) {
  245. *out_group_id = group.group_id;
  246. return 1;
  247. }
  248. }
  249. return 0;
  250. }
  251. } // namespace bssl
  252. using namespace bssl;
  253. const char* SSL_get_curve_name(uint16_t group_id) {
  254. for (const auto &group : kNamedGroups) {
  255. if (group.group_id == group_id) {
  256. return group.name;
  257. }
  258. }
  259. return nullptr;
  260. }