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.
 
 
 
 
 
 

466 lines
13 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. return 0;
  165. }
  166. if (peer_key_len != 32 ||
  167. !X25519(secret, (uint8_t *)ctx->data, peer_key)) {
  168. OPENSSL_free(secret);
  169. *out_alert = SSL_AD_DECODE_ERROR;
  170. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
  171. return 0;
  172. }
  173. *out_secret = secret;
  174. *out_secret_len = 32;
  175. return 1;
  176. }
  177. static int ssl_x25519_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  178. uint8_t **out_secret, size_t *out_secret_len,
  179. uint8_t *out_alert, const uint8_t *peer_key,
  180. size_t peer_key_len) {
  181. *out_alert = SSL_AD_INTERNAL_ERROR;
  182. if (!ssl_x25519_offer(ctx, out_public_key) ||
  183. !ssl_x25519_finish(ctx, out_secret, out_secret_len, out_alert, peer_key,
  184. peer_key_len)) {
  185. return 0;
  186. }
  187. return 1;
  188. }
  189. /* Legacy DHE-based implementation. */
  190. static void ssl_dhe_cleanup(SSL_ECDH_CTX *ctx) {
  191. DH_free((DH *)ctx->data);
  192. }
  193. static int ssl_dhe_offer(SSL_ECDH_CTX *ctx, CBB *out) {
  194. DH *dh = (DH *)ctx->data;
  195. /* The group must have been initialized already, but not the key. */
  196. assert(dh != NULL);
  197. assert(dh->priv_key == NULL);
  198. /* Due to a bug in yaSSL, the public key must be zero padded to the size of
  199. * the prime. */
  200. return DH_generate_key(dh) &&
  201. BN_bn2cbb_padded(out, BN_num_bytes(dh->p), dh->pub_key);
  202. }
  203. static int ssl_dhe_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  204. size_t *out_secret_len, uint8_t *out_alert,
  205. const uint8_t *peer_key, size_t peer_key_len) {
  206. DH *dh = (DH *)ctx->data;
  207. assert(dh != NULL);
  208. assert(dh->priv_key != NULL);
  209. *out_alert = SSL_AD_INTERNAL_ERROR;
  210. int secret_len = 0;
  211. uint8_t *secret = NULL;
  212. BIGNUM *peer_point = BN_bin2bn(peer_key, peer_key_len, NULL);
  213. if (peer_point == NULL) {
  214. goto err;
  215. }
  216. secret = OPENSSL_malloc(DH_size(dh));
  217. if (secret == NULL) {
  218. goto err;
  219. }
  220. secret_len = DH_compute_key(secret, peer_point, dh);
  221. if (secret_len <= 0) {
  222. goto err;
  223. }
  224. *out_secret = secret;
  225. *out_secret_len = (size_t)secret_len;
  226. BN_free(peer_point);
  227. return 1;
  228. err:
  229. if (secret_len > 0) {
  230. OPENSSL_cleanse(secret, (size_t)secret_len);
  231. }
  232. OPENSSL_free(secret);
  233. BN_free(peer_point);
  234. return 0;
  235. }
  236. static int ssl_dhe_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  237. uint8_t **out_secret, size_t *out_secret_len,
  238. uint8_t *out_alert, const uint8_t *peer_key,
  239. size_t peer_key_len) {
  240. *out_alert = SSL_AD_INTERNAL_ERROR;
  241. if (!ssl_dhe_offer(ctx, out_public_key) ||
  242. !ssl_dhe_finish(ctx, out_secret, out_secret_len, out_alert, peer_key,
  243. peer_key_len)) {
  244. return 0;
  245. }
  246. return 1;
  247. }
  248. static const SSL_ECDH_METHOD kDHEMethod = {
  249. NID_undef, 0, "",
  250. ssl_dhe_cleanup,
  251. ssl_dhe_offer,
  252. ssl_dhe_accept,
  253. ssl_dhe_finish,
  254. CBS_get_u16_length_prefixed,
  255. CBB_add_u16_length_prefixed,
  256. };
  257. static const SSL_ECDH_METHOD kMethods[] = {
  258. {
  259. NID_X9_62_prime256v1,
  260. SSL_CURVE_SECP256R1,
  261. "P-256",
  262. ssl_ec_point_cleanup,
  263. ssl_ec_point_offer,
  264. ssl_ec_point_accept,
  265. ssl_ec_point_finish,
  266. CBS_get_u8_length_prefixed,
  267. CBB_add_u8_length_prefixed,
  268. },
  269. {
  270. NID_secp384r1,
  271. SSL_CURVE_SECP384R1,
  272. "P-384",
  273. ssl_ec_point_cleanup,
  274. ssl_ec_point_offer,
  275. ssl_ec_point_accept,
  276. ssl_ec_point_finish,
  277. CBS_get_u8_length_prefixed,
  278. CBB_add_u8_length_prefixed,
  279. },
  280. {
  281. NID_secp521r1,
  282. SSL_CURVE_SECP521R1,
  283. "P-521",
  284. ssl_ec_point_cleanup,
  285. ssl_ec_point_offer,
  286. ssl_ec_point_accept,
  287. ssl_ec_point_finish,
  288. CBS_get_u8_length_prefixed,
  289. CBB_add_u8_length_prefixed,
  290. },
  291. {
  292. NID_X25519,
  293. SSL_CURVE_X25519,
  294. "X25519",
  295. ssl_x25519_cleanup,
  296. ssl_x25519_offer,
  297. ssl_x25519_accept,
  298. ssl_x25519_finish,
  299. CBS_get_u8_length_prefixed,
  300. CBB_add_u8_length_prefixed,
  301. },
  302. };
  303. static const SSL_ECDH_METHOD *method_from_group_id(uint16_t group_id) {
  304. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMethods); i++) {
  305. if (kMethods[i].group_id == group_id) {
  306. return &kMethods[i];
  307. }
  308. }
  309. return NULL;
  310. }
  311. static const SSL_ECDH_METHOD *method_from_nid(int nid) {
  312. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMethods); i++) {
  313. if (kMethods[i].nid == nid) {
  314. return &kMethods[i];
  315. }
  316. }
  317. return NULL;
  318. }
  319. static const SSL_ECDH_METHOD *method_from_name(const char *name, size_t len) {
  320. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMethods); i++) {
  321. if (len == strlen(kMethods[i].name) &&
  322. !strncmp(kMethods[i].name, name, len)) {
  323. return &kMethods[i];
  324. }
  325. }
  326. return NULL;
  327. }
  328. const char* SSL_get_curve_name(uint16_t group_id) {
  329. const SSL_ECDH_METHOD *method = method_from_group_id(group_id);
  330. if (method == NULL) {
  331. return NULL;
  332. }
  333. return method->name;
  334. }
  335. int ssl_nid_to_group_id(uint16_t *out_group_id, int nid) {
  336. const SSL_ECDH_METHOD *method = method_from_nid(nid);
  337. if (method == NULL) {
  338. return 0;
  339. }
  340. *out_group_id = method->group_id;
  341. return 1;
  342. }
  343. int ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len) {
  344. const SSL_ECDH_METHOD *method = method_from_name(name, len);
  345. if (method == NULL) {
  346. return 0;
  347. }
  348. *out_group_id = method->group_id;
  349. return 1;
  350. }
  351. int SSL_ECDH_CTX_init(SSL_ECDH_CTX *ctx, uint16_t group_id) {
  352. SSL_ECDH_CTX_cleanup(ctx);
  353. const SSL_ECDH_METHOD *method = method_from_group_id(group_id);
  354. if (method == NULL) {
  355. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  356. return 0;
  357. }
  358. ctx->method = method;
  359. return 1;
  360. }
  361. void SSL_ECDH_CTX_init_for_dhe(SSL_ECDH_CTX *ctx, DH *params) {
  362. SSL_ECDH_CTX_cleanup(ctx);
  363. ctx->method = &kDHEMethod;
  364. ctx->data = params;
  365. }
  366. void SSL_ECDH_CTX_cleanup(SSL_ECDH_CTX *ctx) {
  367. if (ctx->method == NULL) {
  368. return;
  369. }
  370. ctx->method->cleanup(ctx);
  371. ctx->method = NULL;
  372. ctx->data = NULL;
  373. }
  374. uint16_t SSL_ECDH_CTX_get_id(const SSL_ECDH_CTX *ctx) {
  375. return ctx->method->group_id;
  376. }
  377. int SSL_ECDH_CTX_get_key(SSL_ECDH_CTX *ctx, CBS *cbs, CBS *out) {
  378. if (ctx->method == NULL) {
  379. return 0;
  380. }
  381. return ctx->method->get_key(cbs, out);
  382. }
  383. int SSL_ECDH_CTX_add_key(SSL_ECDH_CTX *ctx, CBB *cbb, CBB *out_contents) {
  384. if (ctx->method == NULL) {
  385. return 0;
  386. }
  387. return ctx->method->add_key(cbb, out_contents);
  388. }
  389. int SSL_ECDH_CTX_offer(SSL_ECDH_CTX *ctx, CBB *out_public_key) {
  390. return ctx->method->offer(ctx, out_public_key);
  391. }
  392. int SSL_ECDH_CTX_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  393. uint8_t **out_secret, size_t *out_secret_len,
  394. uint8_t *out_alert, const uint8_t *peer_key,
  395. size_t peer_key_len) {
  396. return ctx->method->accept(ctx, out_public_key, out_secret, out_secret_len,
  397. out_alert, peer_key, peer_key_len);
  398. }
  399. int SSL_ECDH_CTX_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  400. size_t *out_secret_len, uint8_t *out_alert,
  401. const uint8_t *peer_key, size_t peer_key_len) {
  402. return ctx->method->finish(ctx, out_secret, out_secret_len, out_alert,
  403. peer_key, peer_key_len);
  404. }