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.
 
 
 
 
 
 

606 rivejä
16 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/newhope.h>
  24. #include <openssl/nid.h>
  25. #include "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. const BIGNUM *order = EC_GROUP_get0_order(group);
  52. do {
  53. if (!BN_rand_range(private_key, order)) {
  54. goto err;
  55. }
  56. } while (BN_is_zero(private_key));
  57. /* Compute the corresponding public key and serialize it. */
  58. public_key = EC_POINT_new(group);
  59. if (public_key == NULL ||
  60. !EC_POINT_mul(group, public_key, private_key, NULL, NULL, bn_ctx) ||
  61. !EC_POINT_point2cbb(out, group, public_key, POINT_CONVERSION_UNCOMPRESSED,
  62. bn_ctx)) {
  63. goto err;
  64. }
  65. ret = 1;
  66. err:
  67. EC_GROUP_free(group);
  68. EC_POINT_free(public_key);
  69. BN_CTX_end(bn_ctx);
  70. BN_CTX_free(bn_ctx);
  71. return ret;
  72. }
  73. static int ssl_ec_point_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  74. size_t *out_secret_len, uint8_t *out_alert,
  75. const uint8_t *peer_key, size_t peer_key_len) {
  76. BIGNUM *private_key = (BIGNUM *)ctx->data;
  77. assert(private_key != NULL);
  78. *out_alert = SSL_AD_INTERNAL_ERROR;
  79. /* Set up a shared |BN_CTX| for all operations. */
  80. BN_CTX *bn_ctx = BN_CTX_new();
  81. if (bn_ctx == NULL) {
  82. return 0;
  83. }
  84. BN_CTX_start(bn_ctx);
  85. int ret = 0;
  86. EC_GROUP *group = EC_GROUP_new_by_curve_name(ctx->method->nid);
  87. EC_POINT *peer_point = NULL, *result = NULL;
  88. uint8_t *secret = NULL;
  89. if (group == NULL) {
  90. goto err;
  91. }
  92. /* Compute the x-coordinate of |peer_key| * |private_key|. */
  93. peer_point = EC_POINT_new(group);
  94. result = EC_POINT_new(group);
  95. if (peer_point == NULL || result == NULL) {
  96. goto err;
  97. }
  98. BIGNUM *x = BN_CTX_get(bn_ctx);
  99. if (x == NULL) {
  100. goto err;
  101. }
  102. if (!EC_POINT_oct2point(group, peer_point, peer_key, peer_key_len, bn_ctx)) {
  103. *out_alert = SSL_AD_DECODE_ERROR;
  104. goto err;
  105. }
  106. if (!EC_POINT_mul(group, result, NULL, peer_point, private_key, bn_ctx) ||
  107. !EC_POINT_get_affine_coordinates_GFp(group, result, x, NULL, bn_ctx)) {
  108. goto err;
  109. }
  110. /* Encode the x-coordinate left-padded with zeros. */
  111. size_t secret_len = (EC_GROUP_get_degree(group) + 7) / 8;
  112. secret = OPENSSL_malloc(secret_len);
  113. if (secret == NULL || !BN_bn2bin_padded(secret, secret_len, x)) {
  114. goto err;
  115. }
  116. *out_secret = secret;
  117. *out_secret_len = secret_len;
  118. secret = NULL;
  119. ret = 1;
  120. err:
  121. EC_GROUP_free(group);
  122. EC_POINT_free(peer_point);
  123. EC_POINT_free(result);
  124. BN_CTX_end(bn_ctx);
  125. BN_CTX_free(bn_ctx);
  126. OPENSSL_free(secret);
  127. return ret;
  128. }
  129. static int ssl_ec_point_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  130. uint8_t **out_secret, size_t *out_secret_len,
  131. uint8_t *out_alert, const uint8_t *peer_key,
  132. size_t peer_key_len) {
  133. *out_alert = SSL_AD_INTERNAL_ERROR;
  134. if (!ssl_ec_point_offer(ctx, out_public_key) ||
  135. !ssl_ec_point_finish(ctx, out_secret, out_secret_len, out_alert, peer_key,
  136. peer_key_len)) {
  137. return 0;
  138. }
  139. return 1;
  140. }
  141. /* X25119 implementation. */
  142. static void ssl_x25519_cleanup(SSL_ECDH_CTX *ctx) {
  143. if (ctx->data == NULL) {
  144. return;
  145. }
  146. OPENSSL_cleanse(ctx->data, 32);
  147. OPENSSL_free(ctx->data);
  148. }
  149. static int ssl_x25519_offer(SSL_ECDH_CTX *ctx, CBB *out) {
  150. assert(ctx->data == NULL);
  151. ctx->data = OPENSSL_malloc(32);
  152. if (ctx->data == NULL) {
  153. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  154. return 0;
  155. }
  156. uint8_t public_key[32];
  157. X25519_keypair(public_key, (uint8_t *)ctx->data);
  158. return CBB_add_bytes(out, public_key, sizeof(public_key));
  159. }
  160. static int ssl_x25519_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  161. size_t *out_secret_len, uint8_t *out_alert,
  162. const uint8_t *peer_key, size_t peer_key_len) {
  163. assert(ctx->data != NULL);
  164. *out_alert = SSL_AD_INTERNAL_ERROR;
  165. uint8_t *secret = OPENSSL_malloc(32);
  166. if (secret == NULL) {
  167. return 0;
  168. }
  169. if (peer_key_len != 32 ||
  170. !X25519(secret, (uint8_t *)ctx->data, peer_key)) {
  171. OPENSSL_free(secret);
  172. *out_alert = SSL_AD_DECODE_ERROR;
  173. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
  174. return 0;
  175. }
  176. *out_secret = secret;
  177. *out_secret_len = 32;
  178. return 1;
  179. }
  180. static int ssl_x25519_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  181. uint8_t **out_secret, size_t *out_secret_len,
  182. uint8_t *out_alert, const uint8_t *peer_key,
  183. size_t peer_key_len) {
  184. *out_alert = SSL_AD_INTERNAL_ERROR;
  185. if (!ssl_x25519_offer(ctx, out_public_key) ||
  186. !ssl_x25519_finish(ctx, out_secret, out_secret_len, out_alert, peer_key,
  187. peer_key_len)) {
  188. return 0;
  189. }
  190. return 1;
  191. }
  192. /* Combined X25119 + New Hope (post-quantum) implementation. */
  193. typedef struct {
  194. uint8_t x25519_key[32];
  195. NEWHOPE_POLY *newhope_sk;
  196. } cecpq1_data;
  197. #define CECPQ1_OFFERMSG_LENGTH (32 + NEWHOPE_OFFERMSG_LENGTH)
  198. #define CECPQ1_ACCEPTMSG_LENGTH (32 + NEWHOPE_ACCEPTMSG_LENGTH)
  199. #define CECPQ1_SECRET_LENGTH (32 + SHA256_DIGEST_LENGTH)
  200. static void ssl_cecpq1_cleanup(SSL_ECDH_CTX *ctx) {
  201. if (ctx->data == NULL) {
  202. return;
  203. }
  204. cecpq1_data *data = ctx->data;
  205. NEWHOPE_POLY_free(data->newhope_sk);
  206. OPENSSL_cleanse(data, sizeof(cecpq1_data));
  207. OPENSSL_free(data);
  208. }
  209. static int ssl_cecpq1_offer(SSL_ECDH_CTX *ctx, CBB *out) {
  210. assert(ctx->data == NULL);
  211. cecpq1_data *data = OPENSSL_malloc(sizeof(cecpq1_data));
  212. if (data == NULL) {
  213. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  214. return 0;
  215. }
  216. ctx->data = data;
  217. data->newhope_sk = NEWHOPE_POLY_new();
  218. if (data->newhope_sk == NULL) {
  219. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  220. return 0;
  221. }
  222. uint8_t x25519_public_key[32];
  223. X25519_keypair(x25519_public_key, data->x25519_key);
  224. uint8_t newhope_offermsg[NEWHOPE_OFFERMSG_LENGTH];
  225. NEWHOPE_offer(newhope_offermsg, data->newhope_sk);
  226. if (!CBB_add_bytes(out, x25519_public_key, sizeof(x25519_public_key)) ||
  227. !CBB_add_bytes(out, newhope_offermsg, sizeof(newhope_offermsg))) {
  228. return 0;
  229. }
  230. return 1;
  231. }
  232. static int ssl_cecpq1_accept(SSL_ECDH_CTX *ctx, CBB *cbb, uint8_t **out_secret,
  233. size_t *out_secret_len, uint8_t *out_alert,
  234. const uint8_t *peer_key, size_t peer_key_len) {
  235. if (peer_key_len != CECPQ1_OFFERMSG_LENGTH) {
  236. *out_alert = SSL_AD_DECODE_ERROR;
  237. return 0;
  238. }
  239. *out_alert = SSL_AD_INTERNAL_ERROR;
  240. assert(ctx->data == NULL);
  241. cecpq1_data *data = OPENSSL_malloc(sizeof(cecpq1_data));
  242. if (data == NULL) {
  243. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  244. return 0;
  245. }
  246. data->newhope_sk = NULL;
  247. ctx->data = data;
  248. uint8_t *secret = OPENSSL_malloc(CECPQ1_SECRET_LENGTH);
  249. if (secret == NULL) {
  250. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  251. return 0;
  252. }
  253. /* Generate message to server, and secret key, at once. */
  254. uint8_t x25519_public_key[32];
  255. X25519_keypair(x25519_public_key, data->x25519_key);
  256. if (!X25519(secret, data->x25519_key, peer_key)) {
  257. *out_alert = SSL_AD_DECODE_ERROR;
  258. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
  259. goto err;
  260. }
  261. uint8_t newhope_acceptmsg[NEWHOPE_ACCEPTMSG_LENGTH];
  262. if (!NEWHOPE_accept(secret + 32, newhope_acceptmsg, peer_key + 32,
  263. NEWHOPE_OFFERMSG_LENGTH)) {
  264. *out_alert = SSL_AD_DECODE_ERROR;
  265. goto err;
  266. }
  267. if (!CBB_add_bytes(cbb, x25519_public_key, sizeof(x25519_public_key)) ||
  268. !CBB_add_bytes(cbb, newhope_acceptmsg, sizeof(newhope_acceptmsg))) {
  269. goto err;
  270. }
  271. *out_secret = secret;
  272. *out_secret_len = CECPQ1_SECRET_LENGTH;
  273. return 1;
  274. err:
  275. OPENSSL_cleanse(secret, CECPQ1_SECRET_LENGTH);
  276. OPENSSL_free(secret);
  277. return 0;
  278. }
  279. static int ssl_cecpq1_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  280. size_t *out_secret_len, uint8_t *out_alert,
  281. const uint8_t *peer_key, size_t peer_key_len) {
  282. if (peer_key_len != CECPQ1_ACCEPTMSG_LENGTH) {
  283. *out_alert = SSL_AD_DECODE_ERROR;
  284. return 0;
  285. }
  286. *out_alert = SSL_AD_INTERNAL_ERROR;
  287. assert(ctx->data != NULL);
  288. cecpq1_data *data = ctx->data;
  289. uint8_t *secret = OPENSSL_malloc(CECPQ1_SECRET_LENGTH);
  290. if (secret == NULL) {
  291. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  292. return 0;
  293. }
  294. if (!X25519(secret, data->x25519_key, peer_key)) {
  295. *out_alert = SSL_AD_DECODE_ERROR;
  296. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
  297. goto err;
  298. }
  299. if (!NEWHOPE_finish(secret + 32, data->newhope_sk, peer_key + 32,
  300. NEWHOPE_ACCEPTMSG_LENGTH)) {
  301. *out_alert = SSL_AD_DECODE_ERROR;
  302. goto err;
  303. }
  304. *out_secret = secret;
  305. *out_secret_len = CECPQ1_SECRET_LENGTH;
  306. return 1;
  307. err:
  308. OPENSSL_cleanse(secret, CECPQ1_SECRET_LENGTH);
  309. OPENSSL_free(secret);
  310. return 0;
  311. }
  312. /* Legacy DHE-based implementation. */
  313. static void ssl_dhe_cleanup(SSL_ECDH_CTX *ctx) {
  314. DH_free((DH *)ctx->data);
  315. }
  316. static int ssl_dhe_offer(SSL_ECDH_CTX *ctx, CBB *out) {
  317. DH *dh = (DH *)ctx->data;
  318. /* The group must have been initialized already, but not the key. */
  319. assert(dh != NULL);
  320. assert(dh->priv_key == NULL);
  321. /* Due to a bug in yaSSL, the public key must be zero padded to the size of
  322. * the prime. */
  323. return DH_generate_key(dh) &&
  324. BN_bn2cbb_padded(out, BN_num_bytes(dh->p), dh->pub_key);
  325. }
  326. static int ssl_dhe_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  327. size_t *out_secret_len, uint8_t *out_alert,
  328. const uint8_t *peer_key, size_t peer_key_len) {
  329. DH *dh = (DH *)ctx->data;
  330. assert(dh != NULL);
  331. assert(dh->priv_key != NULL);
  332. *out_alert = SSL_AD_INTERNAL_ERROR;
  333. int secret_len = 0;
  334. uint8_t *secret = NULL;
  335. BIGNUM *peer_point = BN_bin2bn(peer_key, peer_key_len, NULL);
  336. if (peer_point == NULL) {
  337. goto err;
  338. }
  339. secret = OPENSSL_malloc(DH_size(dh));
  340. if (secret == NULL) {
  341. goto err;
  342. }
  343. secret_len = DH_compute_key(secret, peer_point, dh);
  344. if (secret_len <= 0) {
  345. goto err;
  346. }
  347. *out_secret = secret;
  348. *out_secret_len = (size_t)secret_len;
  349. BN_free(peer_point);
  350. return 1;
  351. err:
  352. if (secret_len > 0) {
  353. OPENSSL_cleanse(secret, (size_t)secret_len);
  354. }
  355. OPENSSL_free(secret);
  356. BN_free(peer_point);
  357. return 0;
  358. }
  359. static int ssl_dhe_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  360. uint8_t **out_secret, size_t *out_secret_len,
  361. uint8_t *out_alert, const uint8_t *peer_key,
  362. size_t peer_key_len) {
  363. *out_alert = SSL_AD_INTERNAL_ERROR;
  364. if (!ssl_dhe_offer(ctx, out_public_key) ||
  365. !ssl_dhe_finish(ctx, out_secret, out_secret_len, out_alert, peer_key,
  366. peer_key_len)) {
  367. return 0;
  368. }
  369. return 1;
  370. }
  371. static const SSL_ECDH_METHOD kDHEMethod = {
  372. NID_undef, 0, "",
  373. ssl_dhe_cleanup,
  374. ssl_dhe_offer,
  375. ssl_dhe_accept,
  376. ssl_dhe_finish,
  377. CBS_get_u16_length_prefixed,
  378. CBB_add_u16_length_prefixed,
  379. };
  380. static const SSL_ECDH_METHOD kMethods[] = {
  381. {
  382. NID_X9_62_prime256v1,
  383. SSL_GROUP_SECP256R1,
  384. "P-256",
  385. ssl_ec_point_cleanup,
  386. ssl_ec_point_offer,
  387. ssl_ec_point_accept,
  388. ssl_ec_point_finish,
  389. CBS_get_u8_length_prefixed,
  390. CBB_add_u8_length_prefixed,
  391. },
  392. {
  393. NID_secp384r1,
  394. SSL_GROUP_SECP384R1,
  395. "P-384",
  396. ssl_ec_point_cleanup,
  397. ssl_ec_point_offer,
  398. ssl_ec_point_accept,
  399. ssl_ec_point_finish,
  400. CBS_get_u8_length_prefixed,
  401. CBB_add_u8_length_prefixed,
  402. },
  403. {
  404. NID_secp521r1,
  405. SSL_GROUP_SECP521R1,
  406. "P-521",
  407. ssl_ec_point_cleanup,
  408. ssl_ec_point_offer,
  409. ssl_ec_point_accept,
  410. ssl_ec_point_finish,
  411. CBS_get_u8_length_prefixed,
  412. CBB_add_u8_length_prefixed,
  413. },
  414. {
  415. NID_X25519,
  416. SSL_GROUP_X25519,
  417. "X25519",
  418. ssl_x25519_cleanup,
  419. ssl_x25519_offer,
  420. ssl_x25519_accept,
  421. ssl_x25519_finish,
  422. CBS_get_u8_length_prefixed,
  423. CBB_add_u8_length_prefixed,
  424. },
  425. {
  426. NID_cecpq1,
  427. SSL_GROUP_CECPQ1,
  428. "CECPQ1",
  429. ssl_cecpq1_cleanup,
  430. ssl_cecpq1_offer,
  431. ssl_cecpq1_accept,
  432. ssl_cecpq1_finish,
  433. CBS_get_u16_length_prefixed,
  434. CBB_add_u16_length_prefixed,
  435. },
  436. };
  437. static const SSL_ECDH_METHOD *method_from_group_id(uint16_t group_id) {
  438. size_t i;
  439. for (i = 0; i < sizeof(kMethods) / sizeof(kMethods[0]); i++) {
  440. if (kMethods[i].group_id == group_id) {
  441. return &kMethods[i];
  442. }
  443. }
  444. return NULL;
  445. }
  446. static const SSL_ECDH_METHOD *method_from_nid(int nid) {
  447. size_t i;
  448. for (i = 0; i < sizeof(kMethods) / sizeof(kMethods[0]); i++) {
  449. if (kMethods[i].nid == nid) {
  450. return &kMethods[i];
  451. }
  452. }
  453. return NULL;
  454. }
  455. const char* SSL_get_curve_name(uint16_t group_id) {
  456. const SSL_ECDH_METHOD *method = method_from_group_id(group_id);
  457. if (method == NULL) {
  458. return NULL;
  459. }
  460. return method->name;
  461. }
  462. int ssl_nid_to_group_id(uint16_t *out_group_id, int nid) {
  463. const SSL_ECDH_METHOD *method = method_from_nid(nid);
  464. if (method == NULL) {
  465. return 0;
  466. }
  467. *out_group_id = method->group_id;
  468. return 1;
  469. }
  470. int SSL_ECDH_CTX_init(SSL_ECDH_CTX *ctx, uint16_t group_id) {
  471. SSL_ECDH_CTX_cleanup(ctx);
  472. const SSL_ECDH_METHOD *method = method_from_group_id(group_id);
  473. if (method == NULL) {
  474. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  475. return 0;
  476. }
  477. ctx->method = method;
  478. return 1;
  479. }
  480. void SSL_ECDH_CTX_init_for_dhe(SSL_ECDH_CTX *ctx, DH *params) {
  481. SSL_ECDH_CTX_cleanup(ctx);
  482. ctx->method = &kDHEMethod;
  483. ctx->data = params;
  484. }
  485. int SSL_ECDH_CTX_get_key(SSL_ECDH_CTX *ctx, CBS *cbs, CBS *out) {
  486. if (ctx->method == NULL) {
  487. return 0;
  488. }
  489. return ctx->method->get_key(cbs, out);
  490. }
  491. int SSL_ECDH_CTX_add_key(SSL_ECDH_CTX *ctx, CBB *cbb, CBB *out_contents) {
  492. if (ctx->method == NULL) {
  493. return 0;
  494. }
  495. return ctx->method->add_key(cbb, out_contents);
  496. }
  497. void SSL_ECDH_CTX_cleanup(SSL_ECDH_CTX *ctx) {
  498. if (ctx->method == NULL) {
  499. return;
  500. }
  501. ctx->method->cleanup(ctx);
  502. ctx->method = NULL;
  503. ctx->data = NULL;
  504. }
  505. int SSL_ECDH_CTX_offer(SSL_ECDH_CTX *ctx, CBB *out_public_key) {
  506. return ctx->method->offer(ctx, out_public_key);
  507. }
  508. int SSL_ECDH_CTX_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  509. uint8_t **out_secret, size_t *out_secret_len,
  510. uint8_t *out_alert, const uint8_t *peer_key,
  511. size_t peer_key_len) {
  512. return ctx->method->accept(ctx, out_public_key, out_secret, out_secret_len,
  513. out_alert, peer_key, peer_key_len);
  514. }
  515. int SSL_ECDH_CTX_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  516. size_t *out_secret_len, uint8_t *out_alert,
  517. const uint8_t *peer_key, size_t peer_key_len) {
  518. return ctx->method->finish(ctx, out_secret, out_secret_len, out_alert,
  519. peer_key, peer_key_len);
  520. }