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.
 
 
 
 
 
 

368 lines
10 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 <openssl/bn.h>
  18. #include <openssl/bytestring.h>
  19. #include <openssl/curve25519.h>
  20. #include <openssl/ec.h>
  21. #include <openssl/err.h>
  22. #include <openssl/mem.h>
  23. #include <openssl/nid.h>
  24. #include "internal.h"
  25. #include "../crypto/internal.h"
  26. /* |EC_POINT| implementation. */
  27. static void ssl_ec_point_cleanup(SSL_ECDH_CTX *ctx) {
  28. BIGNUM *private_key = (BIGNUM *)ctx->data;
  29. BN_clear_free(private_key);
  30. }
  31. static int ssl_ec_point_offer(SSL_ECDH_CTX *ctx, CBB *out) {
  32. assert(ctx->data == NULL);
  33. BIGNUM *private_key = BN_new();
  34. if (private_key == NULL) {
  35. return 0;
  36. }
  37. ctx->data = private_key;
  38. /* Set up a shared |BN_CTX| for all operations. */
  39. BN_CTX *bn_ctx = BN_CTX_new();
  40. if (bn_ctx == NULL) {
  41. return 0;
  42. }
  43. BN_CTX_start(bn_ctx);
  44. int ret = 0;
  45. EC_POINT *public_key = NULL;
  46. EC_GROUP *group = EC_GROUP_new_by_curve_name(ctx->method->nid);
  47. if (group == NULL) {
  48. goto err;
  49. }
  50. /* Generate a private key. */
  51. if (!BN_rand_range_ex(private_key, 1, EC_GROUP_get0_order(group))) {
  52. goto err;
  53. }
  54. /* Compute the corresponding public key and serialize it. */
  55. public_key = EC_POINT_new(group);
  56. if (public_key == NULL ||
  57. !EC_POINT_mul(group, public_key, private_key, NULL, NULL, bn_ctx) ||
  58. !EC_POINT_point2cbb(out, group, public_key, POINT_CONVERSION_UNCOMPRESSED,
  59. bn_ctx)) {
  60. goto err;
  61. }
  62. ret = 1;
  63. err:
  64. EC_GROUP_free(group);
  65. EC_POINT_free(public_key);
  66. BN_CTX_end(bn_ctx);
  67. BN_CTX_free(bn_ctx);
  68. return ret;
  69. }
  70. static int ssl_ec_point_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  71. size_t *out_secret_len, uint8_t *out_alert,
  72. const uint8_t *peer_key, size_t peer_key_len) {
  73. BIGNUM *private_key = (BIGNUM *)ctx->data;
  74. assert(private_key != NULL);
  75. *out_alert = SSL_AD_INTERNAL_ERROR;
  76. /* Set up a shared |BN_CTX| for all operations. */
  77. BN_CTX *bn_ctx = BN_CTX_new();
  78. if (bn_ctx == NULL) {
  79. return 0;
  80. }
  81. BN_CTX_start(bn_ctx);
  82. int ret = 0;
  83. EC_GROUP *group = EC_GROUP_new_by_curve_name(ctx->method->nid);
  84. EC_POINT *peer_point = NULL, *result = NULL;
  85. uint8_t *secret = NULL;
  86. if (group == NULL) {
  87. goto err;
  88. }
  89. /* Compute the x-coordinate of |peer_key| * |private_key|. */
  90. peer_point = EC_POINT_new(group);
  91. result = EC_POINT_new(group);
  92. if (peer_point == NULL || result == NULL) {
  93. goto err;
  94. }
  95. BIGNUM *x = BN_CTX_get(bn_ctx);
  96. if (x == NULL) {
  97. goto err;
  98. }
  99. if (!EC_POINT_oct2point(group, peer_point, peer_key, peer_key_len, bn_ctx)) {
  100. *out_alert = SSL_AD_DECODE_ERROR;
  101. goto err;
  102. }
  103. if (!EC_POINT_mul(group, result, NULL, peer_point, private_key, bn_ctx) ||
  104. !EC_POINT_get_affine_coordinates_GFp(group, result, x, NULL, bn_ctx)) {
  105. goto err;
  106. }
  107. /* Encode the x-coordinate left-padded with zeros. */
  108. size_t secret_len = (EC_GROUP_get_degree(group) + 7) / 8;
  109. secret = OPENSSL_malloc(secret_len);
  110. if (secret == NULL || !BN_bn2bin_padded(secret, secret_len, x)) {
  111. goto err;
  112. }
  113. *out_secret = secret;
  114. *out_secret_len = secret_len;
  115. secret = NULL;
  116. ret = 1;
  117. err:
  118. EC_GROUP_free(group);
  119. EC_POINT_free(peer_point);
  120. EC_POINT_free(result);
  121. BN_CTX_end(bn_ctx);
  122. BN_CTX_free(bn_ctx);
  123. OPENSSL_free(secret);
  124. return ret;
  125. }
  126. static int ssl_ec_point_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  127. uint8_t **out_secret, size_t *out_secret_len,
  128. uint8_t *out_alert, const uint8_t *peer_key,
  129. size_t peer_key_len) {
  130. *out_alert = SSL_AD_INTERNAL_ERROR;
  131. if (!ssl_ec_point_offer(ctx, out_public_key) ||
  132. !ssl_ec_point_finish(ctx, out_secret, out_secret_len, out_alert, peer_key,
  133. peer_key_len)) {
  134. return 0;
  135. }
  136. return 1;
  137. }
  138. /* X25119 implementation. */
  139. static void ssl_x25519_cleanup(SSL_ECDH_CTX *ctx) {
  140. if (ctx->data == NULL) {
  141. return;
  142. }
  143. OPENSSL_cleanse(ctx->data, 32);
  144. OPENSSL_free(ctx->data);
  145. }
  146. static int ssl_x25519_offer(SSL_ECDH_CTX *ctx, CBB *out) {
  147. assert(ctx->data == NULL);
  148. ctx->data = OPENSSL_malloc(32);
  149. if (ctx->data == NULL) {
  150. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  151. return 0;
  152. }
  153. uint8_t public_key[32];
  154. X25519_keypair(public_key, (uint8_t *)ctx->data);
  155. return CBB_add_bytes(out, public_key, sizeof(public_key));
  156. }
  157. static int ssl_x25519_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  158. size_t *out_secret_len, uint8_t *out_alert,
  159. const uint8_t *peer_key, size_t peer_key_len) {
  160. assert(ctx->data != NULL);
  161. *out_alert = SSL_AD_INTERNAL_ERROR;
  162. uint8_t *secret = OPENSSL_malloc(32);
  163. if (secret == NULL) {
  164. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  165. return 0;
  166. }
  167. if (peer_key_len != 32 ||
  168. !X25519(secret, (uint8_t *)ctx->data, peer_key)) {
  169. OPENSSL_free(secret);
  170. *out_alert = SSL_AD_DECODE_ERROR;
  171. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
  172. return 0;
  173. }
  174. *out_secret = secret;
  175. *out_secret_len = 32;
  176. return 1;
  177. }
  178. static int ssl_x25519_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  179. uint8_t **out_secret, size_t *out_secret_len,
  180. uint8_t *out_alert, const uint8_t *peer_key,
  181. size_t peer_key_len) {
  182. *out_alert = SSL_AD_INTERNAL_ERROR;
  183. if (!ssl_x25519_offer(ctx, out_public_key) ||
  184. !ssl_x25519_finish(ctx, out_secret, out_secret_len, out_alert, peer_key,
  185. peer_key_len)) {
  186. return 0;
  187. }
  188. return 1;
  189. }
  190. static const SSL_ECDH_METHOD kMethods[] = {
  191. {
  192. NID_secp224r1,
  193. SSL_CURVE_SECP224R1,
  194. "P-224",
  195. ssl_ec_point_cleanup,
  196. ssl_ec_point_offer,
  197. ssl_ec_point_accept,
  198. ssl_ec_point_finish,
  199. },
  200. {
  201. NID_X9_62_prime256v1,
  202. SSL_CURVE_SECP256R1,
  203. "P-256",
  204. ssl_ec_point_cleanup,
  205. ssl_ec_point_offer,
  206. ssl_ec_point_accept,
  207. ssl_ec_point_finish,
  208. },
  209. {
  210. NID_secp384r1,
  211. SSL_CURVE_SECP384R1,
  212. "P-384",
  213. ssl_ec_point_cleanup,
  214. ssl_ec_point_offer,
  215. ssl_ec_point_accept,
  216. ssl_ec_point_finish,
  217. },
  218. {
  219. NID_secp521r1,
  220. SSL_CURVE_SECP521R1,
  221. "P-521",
  222. ssl_ec_point_cleanup,
  223. ssl_ec_point_offer,
  224. ssl_ec_point_accept,
  225. ssl_ec_point_finish,
  226. },
  227. {
  228. NID_X25519,
  229. SSL_CURVE_X25519,
  230. "X25519",
  231. ssl_x25519_cleanup,
  232. ssl_x25519_offer,
  233. ssl_x25519_accept,
  234. ssl_x25519_finish,
  235. },
  236. };
  237. static const SSL_ECDH_METHOD *method_from_group_id(uint16_t group_id) {
  238. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMethods); i++) {
  239. if (kMethods[i].group_id == group_id) {
  240. return &kMethods[i];
  241. }
  242. }
  243. return NULL;
  244. }
  245. static const SSL_ECDH_METHOD *method_from_nid(int nid) {
  246. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMethods); i++) {
  247. if (kMethods[i].nid == nid) {
  248. return &kMethods[i];
  249. }
  250. }
  251. return NULL;
  252. }
  253. static const SSL_ECDH_METHOD *method_from_name(const char *name, size_t len) {
  254. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMethods); i++) {
  255. if (len == strlen(kMethods[i].name) &&
  256. !strncmp(kMethods[i].name, name, len)) {
  257. return &kMethods[i];
  258. }
  259. }
  260. return NULL;
  261. }
  262. const char* SSL_get_curve_name(uint16_t group_id) {
  263. const SSL_ECDH_METHOD *method = method_from_group_id(group_id);
  264. if (method == NULL) {
  265. return NULL;
  266. }
  267. return method->name;
  268. }
  269. int ssl_nid_to_group_id(uint16_t *out_group_id, int nid) {
  270. const SSL_ECDH_METHOD *method = method_from_nid(nid);
  271. if (method == NULL) {
  272. return 0;
  273. }
  274. *out_group_id = method->group_id;
  275. return 1;
  276. }
  277. int ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len) {
  278. const SSL_ECDH_METHOD *method = method_from_name(name, len);
  279. if (method == NULL) {
  280. return 0;
  281. }
  282. *out_group_id = method->group_id;
  283. return 1;
  284. }
  285. int SSL_ECDH_CTX_init(SSL_ECDH_CTX *ctx, uint16_t group_id) {
  286. SSL_ECDH_CTX_cleanup(ctx);
  287. const SSL_ECDH_METHOD *method = method_from_group_id(group_id);
  288. if (method == NULL) {
  289. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  290. return 0;
  291. }
  292. ctx->method = method;
  293. return 1;
  294. }
  295. void SSL_ECDH_CTX_cleanup(SSL_ECDH_CTX *ctx) {
  296. if (ctx->method == NULL) {
  297. return;
  298. }
  299. ctx->method->cleanup(ctx);
  300. ctx->method = NULL;
  301. ctx->data = NULL;
  302. }
  303. uint16_t SSL_ECDH_CTX_get_id(const SSL_ECDH_CTX *ctx) {
  304. return ctx->method->group_id;
  305. }
  306. int SSL_ECDH_CTX_offer(SSL_ECDH_CTX *ctx, CBB *out_public_key) {
  307. return ctx->method->offer(ctx, out_public_key);
  308. }
  309. int SSL_ECDH_CTX_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  310. uint8_t **out_secret, size_t *out_secret_len,
  311. uint8_t *out_alert, const uint8_t *peer_key,
  312. size_t peer_key_len) {
  313. return ctx->method->accept(ctx, out_public_key, out_secret, out_secret_len,
  314. out_alert, peer_key, peer_key_len);
  315. }
  316. int SSL_ECDH_CTX_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  317. size_t *out_secret_len, uint8_t *out_alert,
  318. const uint8_t *peer_key, size_t peer_key_len) {
  319. return ctx->method->finish(ctx, out_secret, out_secret_len, out_alert,
  320. peer_key, peer_key_len);
  321. }