Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

611 wiersze
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 kCECPQ1Method = {
  381. NID_undef, 0, "",
  382. ssl_cecpq1_cleanup,
  383. ssl_cecpq1_offer,
  384. ssl_cecpq1_accept,
  385. ssl_cecpq1_finish,
  386. CBS_get_u16_length_prefixed,
  387. CBB_add_u16_length_prefixed,
  388. };
  389. static const SSL_ECDH_METHOD kMethods[] = {
  390. {
  391. NID_X9_62_prime256v1,
  392. SSL_CURVE_SECP256R1,
  393. "P-256",
  394. ssl_ec_point_cleanup,
  395. ssl_ec_point_offer,
  396. ssl_ec_point_accept,
  397. ssl_ec_point_finish,
  398. CBS_get_u8_length_prefixed,
  399. CBB_add_u8_length_prefixed,
  400. },
  401. {
  402. NID_secp384r1,
  403. SSL_CURVE_SECP384R1,
  404. "P-384",
  405. ssl_ec_point_cleanup,
  406. ssl_ec_point_offer,
  407. ssl_ec_point_accept,
  408. ssl_ec_point_finish,
  409. CBS_get_u8_length_prefixed,
  410. CBB_add_u8_length_prefixed,
  411. },
  412. {
  413. NID_secp521r1,
  414. SSL_CURVE_SECP521R1,
  415. "P-521",
  416. ssl_ec_point_cleanup,
  417. ssl_ec_point_offer,
  418. ssl_ec_point_accept,
  419. ssl_ec_point_finish,
  420. CBS_get_u8_length_prefixed,
  421. CBB_add_u8_length_prefixed,
  422. },
  423. {
  424. NID_X25519,
  425. SSL_CURVE_X25519,
  426. "X25519",
  427. ssl_x25519_cleanup,
  428. ssl_x25519_offer,
  429. ssl_x25519_accept,
  430. ssl_x25519_finish,
  431. CBS_get_u8_length_prefixed,
  432. CBB_add_u8_length_prefixed,
  433. },
  434. };
  435. static const SSL_ECDH_METHOD *method_from_group_id(uint16_t group_id) {
  436. size_t i;
  437. for (i = 0; i < sizeof(kMethods) / sizeof(kMethods[0]); i++) {
  438. if (kMethods[i].group_id == group_id) {
  439. return &kMethods[i];
  440. }
  441. }
  442. return NULL;
  443. }
  444. static const SSL_ECDH_METHOD *method_from_nid(int nid) {
  445. size_t i;
  446. for (i = 0; i < sizeof(kMethods) / sizeof(kMethods[0]); i++) {
  447. if (kMethods[i].nid == nid) {
  448. return &kMethods[i];
  449. }
  450. }
  451. return NULL;
  452. }
  453. const char* SSL_get_curve_name(uint16_t group_id) {
  454. const SSL_ECDH_METHOD *method = method_from_group_id(group_id);
  455. if (method == NULL) {
  456. return NULL;
  457. }
  458. return method->name;
  459. }
  460. int ssl_nid_to_group_id(uint16_t *out_group_id, int nid) {
  461. const SSL_ECDH_METHOD *method = method_from_nid(nid);
  462. if (method == NULL) {
  463. return 0;
  464. }
  465. *out_group_id = method->group_id;
  466. return 1;
  467. }
  468. int SSL_ECDH_CTX_init(SSL_ECDH_CTX *ctx, uint16_t group_id) {
  469. SSL_ECDH_CTX_cleanup(ctx);
  470. const SSL_ECDH_METHOD *method = method_from_group_id(group_id);
  471. if (method == NULL) {
  472. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  473. return 0;
  474. }
  475. ctx->method = method;
  476. return 1;
  477. }
  478. void SSL_ECDH_CTX_init_for_dhe(SSL_ECDH_CTX *ctx, DH *params) {
  479. SSL_ECDH_CTX_cleanup(ctx);
  480. ctx->method = &kDHEMethod;
  481. ctx->data = params;
  482. }
  483. void SSL_ECDH_CTX_init_for_cecpq1(SSL_ECDH_CTX *ctx) {
  484. SSL_ECDH_CTX_cleanup(ctx);
  485. ctx->method = &kCECPQ1Method;
  486. }
  487. int SSL_ECDH_CTX_get_key(SSL_ECDH_CTX *ctx, CBS *cbs, CBS *out) {
  488. if (ctx->method == NULL) {
  489. return 0;
  490. }
  491. return ctx->method->get_key(cbs, out);
  492. }
  493. int SSL_ECDH_CTX_add_key(SSL_ECDH_CTX *ctx, CBB *cbb, CBB *out_contents) {
  494. if (ctx->method == NULL) {
  495. return 0;
  496. }
  497. return ctx->method->add_key(cbb, out_contents);
  498. }
  499. void SSL_ECDH_CTX_cleanup(SSL_ECDH_CTX *ctx) {
  500. if (ctx->method == NULL) {
  501. return;
  502. }
  503. ctx->method->cleanup(ctx);
  504. ctx->method = NULL;
  505. ctx->data = NULL;
  506. }
  507. int SSL_ECDH_CTX_offer(SSL_ECDH_CTX *ctx, CBB *out_public_key) {
  508. return ctx->method->offer(ctx, out_public_key);
  509. }
  510. int SSL_ECDH_CTX_accept(SSL_ECDH_CTX *ctx, CBB *out_public_key,
  511. uint8_t **out_secret, size_t *out_secret_len,
  512. uint8_t *out_alert, const uint8_t *peer_key,
  513. size_t peer_key_len) {
  514. return ctx->method->accept(ctx, out_public_key, out_secret, out_secret_len,
  515. out_alert, peer_key, peer_key_len);
  516. }
  517. int SSL_ECDH_CTX_finish(SSL_ECDH_CTX *ctx, uint8_t **out_secret,
  518. size_t *out_secret_len, uint8_t *out_alert,
  519. const uint8_t *peer_key, size_t peer_key_len) {
  520. return ctx->method->finish(ctx, out_secret, out_secret_len, out_alert,
  521. peer_key, peer_key_len);
  522. }