ec_ameth.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. /*
  2. * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
  11. * for internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/x509.h>
  17. #include <openssl/ec.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/cms.h>
  20. #include <openssl/asn1t.h>
  21. #include "crypto/asn1.h"
  22. #include "crypto/evp.h"
  23. #include <openssl/core_names.h>
  24. #include "openssl/param_build.h"
  25. #include "ec_local.h"
  26. #ifndef OPENSSL_NO_CMS
  27. static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
  28. static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
  29. #endif
  30. static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key)
  31. {
  32. const EC_GROUP *group;
  33. int nid;
  34. if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
  35. ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
  36. return 0;
  37. }
  38. if (EC_GROUP_get_asn1_flag(group)
  39. && (nid = EC_GROUP_get_curve_name(group)))
  40. /* we have a 'named curve' => just set the OID */
  41. {
  42. *ppval = OBJ_nid2obj(nid);
  43. *pptype = V_ASN1_OBJECT;
  44. } else { /* explicit parameters */
  45. ASN1_STRING *pstr = NULL;
  46. pstr = ASN1_STRING_new();
  47. if (pstr == NULL)
  48. return 0;
  49. pstr->length = i2d_ECParameters(ec_key, &pstr->data);
  50. if (pstr->length <= 0) {
  51. ASN1_STRING_free(pstr);
  52. ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
  53. return 0;
  54. }
  55. *ppval = pstr;
  56. *pptype = V_ASN1_SEQUENCE;
  57. }
  58. return 1;
  59. }
  60. static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  61. {
  62. const EC_KEY *ec_key = pkey->pkey.ec;
  63. void *pval = NULL;
  64. int ptype;
  65. unsigned char *penc = NULL, *p;
  66. int penclen;
  67. if (!eckey_param2type(&ptype, &pval, ec_key)) {
  68. ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
  69. return 0;
  70. }
  71. penclen = i2o_ECPublicKey(ec_key, NULL);
  72. if (penclen <= 0)
  73. goto err;
  74. penc = OPENSSL_malloc(penclen);
  75. if (penc == NULL)
  76. goto err;
  77. p = penc;
  78. penclen = i2o_ECPublicKey(ec_key, &p);
  79. if (penclen <= 0)
  80. goto err;
  81. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
  82. ptype, pval, penc, penclen))
  83. return 1;
  84. err:
  85. if (ptype == V_ASN1_OBJECT)
  86. ASN1_OBJECT_free(pval);
  87. else
  88. ASN1_STRING_free(pval);
  89. OPENSSL_free(penc);
  90. return 0;
  91. }
  92. static EC_KEY *eckey_type2param(int ptype, const void *pval)
  93. {
  94. EC_KEY *eckey = NULL;
  95. EC_GROUP *group = NULL;
  96. if (ptype == V_ASN1_SEQUENCE) {
  97. const ASN1_STRING *pstr = pval;
  98. const unsigned char *pm = pstr->data;
  99. int pmlen = pstr->length;
  100. if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
  101. ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
  102. goto ecerr;
  103. }
  104. } else if (ptype == V_ASN1_OBJECT) {
  105. const ASN1_OBJECT *poid = pval;
  106. /*
  107. * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
  108. */
  109. if ((eckey = EC_KEY_new()) == NULL) {
  110. ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
  111. goto ecerr;
  112. }
  113. group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
  114. if (group == NULL)
  115. goto ecerr;
  116. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  117. if (EC_KEY_set_group(eckey, group) == 0)
  118. goto ecerr;
  119. EC_GROUP_free(group);
  120. } else {
  121. ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
  122. goto ecerr;
  123. }
  124. return eckey;
  125. ecerr:
  126. EC_KEY_free(eckey);
  127. EC_GROUP_free(group);
  128. return NULL;
  129. }
  130. static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  131. {
  132. const unsigned char *p = NULL;
  133. const void *pval;
  134. int ptype, pklen;
  135. EC_KEY *eckey = NULL;
  136. X509_ALGOR *palg;
  137. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  138. return 0;
  139. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  140. eckey = eckey_type2param(ptype, pval);
  141. if (!eckey) {
  142. ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
  143. return 0;
  144. }
  145. /* We have parameters now set public key */
  146. if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
  147. ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
  148. goto ecerr;
  149. }
  150. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  151. return 1;
  152. ecerr:
  153. EC_KEY_free(eckey);
  154. return 0;
  155. }
  156. static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  157. {
  158. int r;
  159. const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
  160. const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
  161. *pb = EC_KEY_get0_public_key(b->pkey.ec);
  162. if (group == NULL || pa == NULL || pb == NULL)
  163. return -2;
  164. r = EC_POINT_cmp(group, pa, pb, NULL);
  165. if (r == 0)
  166. return 1;
  167. if (r == 1)
  168. return 0;
  169. return -2;
  170. }
  171. static int eckey_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
  172. {
  173. const unsigned char *p = NULL;
  174. const void *pval;
  175. int ptype, pklen;
  176. EC_KEY *eckey = NULL;
  177. const X509_ALGOR *palg;
  178. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
  179. return 0;
  180. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  181. eckey = eckey_type2param(ptype, pval);
  182. if (eckey == NULL)
  183. goto ecliberr;
  184. /* We have parameters now set private key */
  185. if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
  186. ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
  187. goto ecerr;
  188. }
  189. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  190. return 1;
  191. ecliberr:
  192. ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
  193. ecerr:
  194. EC_KEY_free(eckey);
  195. return 0;
  196. }
  197. static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  198. {
  199. EC_KEY ec_key = *(pkey->pkey.ec);
  200. unsigned char *ep, *p;
  201. int eplen, ptype;
  202. void *pval;
  203. unsigned int old_flags;
  204. if (!eckey_param2type(&ptype, &pval, &ec_key)) {
  205. ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
  206. return 0;
  207. }
  208. /* set the private key */
  209. /*
  210. * do not include the parameters in the SEC1 private key see PKCS#11
  211. * 12.11
  212. */
  213. old_flags = EC_KEY_get_enc_flags(&ec_key);
  214. EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
  215. eplen = i2d_ECPrivateKey(&ec_key, NULL);
  216. if (!eplen) {
  217. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
  218. return 0;
  219. }
  220. ep = OPENSSL_malloc(eplen);
  221. if (ep == NULL) {
  222. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  223. return 0;
  224. }
  225. p = ep;
  226. if (!i2d_ECPrivateKey(&ec_key, &p)) {
  227. OPENSSL_free(ep);
  228. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
  229. return 0;
  230. }
  231. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
  232. ptype, pval, ep, eplen)) {
  233. OPENSSL_free(ep);
  234. return 0;
  235. }
  236. return 1;
  237. }
  238. static int int_ec_size(const EVP_PKEY *pkey)
  239. {
  240. return ECDSA_size(pkey->pkey.ec);
  241. }
  242. static int ec_bits(const EVP_PKEY *pkey)
  243. {
  244. return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
  245. }
  246. static int ec_security_bits(const EVP_PKEY *pkey)
  247. {
  248. int ecbits = ec_bits(pkey);
  249. if (ecbits >= 512)
  250. return 256;
  251. if (ecbits >= 384)
  252. return 192;
  253. if (ecbits >= 256)
  254. return 128;
  255. if (ecbits >= 224)
  256. return 112;
  257. if (ecbits >= 160)
  258. return 80;
  259. return ecbits / 2;
  260. }
  261. static int ec_missing_parameters(const EVP_PKEY *pkey)
  262. {
  263. if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
  264. return 1;
  265. return 0;
  266. }
  267. static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  268. {
  269. EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
  270. if (group == NULL)
  271. return 0;
  272. if (to->pkey.ec == NULL) {
  273. to->pkey.ec = EC_KEY_new();
  274. if (to->pkey.ec == NULL)
  275. goto err;
  276. }
  277. if (EC_KEY_set_group(to->pkey.ec, group) == 0)
  278. goto err;
  279. EC_GROUP_free(group);
  280. return 1;
  281. err:
  282. EC_GROUP_free(group);
  283. return 0;
  284. }
  285. static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  286. {
  287. const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
  288. *group_b = EC_KEY_get0_group(b->pkey.ec);
  289. if (group_a == NULL || group_b == NULL)
  290. return -2;
  291. if (EC_GROUP_cmp(group_a, group_b, NULL))
  292. return 0;
  293. else
  294. return 1;
  295. }
  296. static void int_ec_free(EVP_PKEY *pkey)
  297. {
  298. EC_KEY_free(pkey->pkey.ec);
  299. }
  300. typedef enum {
  301. EC_KEY_PRINT_PRIVATE,
  302. EC_KEY_PRINT_PUBLIC,
  303. EC_KEY_PRINT_PARAM
  304. } ec_print_t;
  305. static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
  306. {
  307. const char *ecstr;
  308. unsigned char *priv = NULL, *pub = NULL;
  309. size_t privlen = 0, publen = 0;
  310. int ret = 0;
  311. const EC_GROUP *group;
  312. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
  313. ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
  314. return 0;
  315. }
  316. if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
  317. publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
  318. if (publen == 0)
  319. goto err;
  320. }
  321. if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
  322. privlen = EC_KEY_priv2buf(x, &priv);
  323. if (privlen == 0)
  324. goto err;
  325. }
  326. if (ktype == EC_KEY_PRINT_PRIVATE)
  327. ecstr = "Private-Key";
  328. else if (ktype == EC_KEY_PRINT_PUBLIC)
  329. ecstr = "Public-Key";
  330. else
  331. ecstr = "ECDSA-Parameters";
  332. if (!BIO_indent(bp, off, 128))
  333. goto err;
  334. if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
  335. EC_GROUP_order_bits(group)) <= 0)
  336. goto err;
  337. if (privlen != 0) {
  338. if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
  339. goto err;
  340. if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
  341. goto err;
  342. }
  343. if (publen != 0) {
  344. if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
  345. goto err;
  346. if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
  347. goto err;
  348. }
  349. if (!ECPKParameters_print(bp, group, off))
  350. goto err;
  351. ret = 1;
  352. err:
  353. if (!ret)
  354. ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
  355. OPENSSL_clear_free(priv, privlen);
  356. OPENSSL_free(pub);
  357. return ret;
  358. }
  359. static int eckey_param_decode(EVP_PKEY *pkey,
  360. const unsigned char **pder, int derlen)
  361. {
  362. EC_KEY *eckey;
  363. if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
  364. ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
  365. return 0;
  366. }
  367. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  368. return 1;
  369. }
  370. static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  371. {
  372. return i2d_ECParameters(pkey->pkey.ec, pder);
  373. }
  374. static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  375. ASN1_PCTX *ctx)
  376. {
  377. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
  378. }
  379. static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  380. ASN1_PCTX *ctx)
  381. {
  382. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
  383. }
  384. static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  385. ASN1_PCTX *ctx)
  386. {
  387. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
  388. }
  389. static int old_ec_priv_decode(EVP_PKEY *pkey,
  390. const unsigned char **pder, int derlen)
  391. {
  392. EC_KEY *ec;
  393. if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
  394. ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
  395. return 0;
  396. }
  397. EVP_PKEY_assign_EC_KEY(pkey, ec);
  398. return 1;
  399. }
  400. static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  401. {
  402. return i2d_ECPrivateKey(pkey->pkey.ec, pder);
  403. }
  404. static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  405. {
  406. switch (op) {
  407. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  408. if (arg1 == 0) {
  409. int snid, hnid;
  410. X509_ALGOR *alg1, *alg2;
  411. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
  412. if (alg1 == NULL || alg1->algorithm == NULL)
  413. return -1;
  414. hnid = OBJ_obj2nid(alg1->algorithm);
  415. if (hnid == NID_undef)
  416. return -1;
  417. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  418. return -1;
  419. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  420. }
  421. return 1;
  422. #ifndef OPENSSL_NO_CMS
  423. case ASN1_PKEY_CTRL_CMS_SIGN:
  424. if (arg1 == 0) {
  425. int snid, hnid;
  426. X509_ALGOR *alg1, *alg2;
  427. CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
  428. if (alg1 == NULL || alg1->algorithm == NULL)
  429. return -1;
  430. hnid = OBJ_obj2nid(alg1->algorithm);
  431. if (hnid == NID_undef)
  432. return -1;
  433. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  434. return -1;
  435. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  436. }
  437. return 1;
  438. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  439. if (arg1 == 1)
  440. return ecdh_cms_decrypt(arg2);
  441. else if (arg1 == 0)
  442. return ecdh_cms_encrypt(arg2);
  443. return -2;
  444. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  445. *(int *)arg2 = CMS_RECIPINFO_AGREE;
  446. return 1;
  447. #endif
  448. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  449. if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) {
  450. /* For SM2, the only valid digest-alg is SM3 */
  451. *(int *)arg2 = NID_sm3;
  452. return 2; /* Make it mandatory */
  453. }
  454. *(int *)arg2 = NID_sha256;
  455. return 1;
  456. case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
  457. return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
  458. case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
  459. return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
  460. POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
  461. default:
  462. return -2;
  463. }
  464. }
  465. static int ec_pkey_check(const EVP_PKEY *pkey)
  466. {
  467. EC_KEY *eckey = pkey->pkey.ec;
  468. /* stay consistent to what EVP_PKEY_check demands */
  469. if (eckey->priv_key == NULL) {
  470. ECerr(EC_F_EC_PKEY_CHECK, EC_R_MISSING_PRIVATE_KEY);
  471. return 0;
  472. }
  473. return EC_KEY_check_key(eckey);
  474. }
  475. static int ec_pkey_public_check(const EVP_PKEY *pkey)
  476. {
  477. EC_KEY *eckey = pkey->pkey.ec;
  478. /*
  479. * Note: it unnecessary to check eckey->pub_key here since
  480. * it will be checked in EC_KEY_check_key(). In fact, the
  481. * EC_KEY_check_key() mainly checks the public key, and checks
  482. * the private key optionally (only if there is one). So if
  483. * someone passes a whole EC key (public + private), this
  484. * will also work...
  485. */
  486. return EC_KEY_check_key(eckey);
  487. }
  488. static int ec_pkey_param_check(const EVP_PKEY *pkey)
  489. {
  490. EC_KEY *eckey = pkey->pkey.ec;
  491. /* stay consistent to what EVP_PKEY_check demands */
  492. if (eckey->group == NULL) {
  493. ECerr(EC_F_EC_PKEY_PARAM_CHECK, EC_R_MISSING_PARAMETERS);
  494. return 0;
  495. }
  496. return EC_GROUP_check(eckey->group, NULL);
  497. }
  498. static
  499. size_t ec_pkey_dirty_cnt(const EVP_PKEY *pkey)
  500. {
  501. return pkey->pkey.ec->dirty_cnt;
  502. }
  503. static ossl_inline
  504. int ecparams_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl)
  505. {
  506. const EC_GROUP *ecg;
  507. int curve_nid;
  508. if (eckey == NULL)
  509. return 0;
  510. ecg = EC_KEY_get0_group(eckey);
  511. if (ecg == NULL)
  512. return 0;
  513. curve_nid = EC_GROUP_get_curve_name(ecg);
  514. if (curve_nid == NID_undef) {
  515. /* explicit parameters */
  516. /*
  517. * TODO(3.0): should we support explicit parameters curves?
  518. */
  519. return 0;
  520. } else {
  521. /* named curve */
  522. const char *curve_name = NULL;
  523. if ((curve_name = OBJ_nid2sn(curve_nid)) == NULL)
  524. return 0;
  525. if (!OSSL_PARAM_BLD_push_utf8_string(tmpl, OSSL_PKEY_PARAM_EC_NAME, curve_name, 0))
  526. return 0;
  527. }
  528. return 1;
  529. }
  530. static
  531. int ec_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
  532. EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
  533. const char *propq)
  534. {
  535. const EC_KEY *eckey = NULL;
  536. const EC_GROUP *ecg = NULL;
  537. unsigned char *pub_key_buf = NULL;
  538. size_t pub_key_buflen;
  539. OSSL_PARAM_BLD *tmpl;
  540. OSSL_PARAM *params = NULL;
  541. const BIGNUM *priv_key = NULL;
  542. const EC_POINT *pub_point = NULL;
  543. int selection = 0;
  544. int rv = 0;
  545. BN_CTX *bnctx = NULL;
  546. if (from == NULL
  547. || (eckey = from->pkey.ec) == NULL
  548. || (ecg = EC_KEY_get0_group(eckey)) == NULL)
  549. return 0;
  550. /*
  551. * If the EC_KEY method is foreign, then we can't be sure of anything,
  552. * and can therefore not export or pretend to export.
  553. */
  554. if (EC_KEY_get_method(eckey) != EC_KEY_OpenSSL())
  555. return 0;
  556. tmpl = OSSL_PARAM_BLD_new();
  557. if (tmpl == NULL)
  558. return 0;
  559. /* export the domain parameters */
  560. if (!ecparams_to_params(eckey, tmpl))
  561. goto err;
  562. selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  563. priv_key = EC_KEY_get0_private_key(eckey);
  564. pub_point = EC_KEY_get0_public_key(eckey);
  565. if (pub_point != NULL) {
  566. /*
  567. * EC_POINT_point2buf() can generate random numbers in some
  568. * implementations so we need to ensure we use the correct libctx.
  569. */
  570. bnctx = BN_CTX_new_ex(libctx);
  571. if (bnctx == NULL)
  572. goto err;
  573. /* convert pub_point to a octet string according to the SECG standard */
  574. if ((pub_key_buflen = EC_POINT_point2buf(ecg, pub_point,
  575. POINT_CONVERSION_COMPRESSED,
  576. &pub_key_buf, bnctx)) == 0
  577. || !OSSL_PARAM_BLD_push_octet_string(tmpl,
  578. OSSL_PKEY_PARAM_PUB_KEY,
  579. pub_key_buf,
  580. pub_key_buflen))
  581. goto err;
  582. selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  583. }
  584. if (priv_key != NULL) {
  585. size_t sz;
  586. int ecbits;
  587. int ecdh_cofactor_mode;
  588. /*
  589. * Key import/export should never leak the bit length of the secret
  590. * scalar in the key.
  591. *
  592. * For this reason, on export we use padded BIGNUMs with fixed length.
  593. *
  594. * When importing we also should make sure that, even if short lived,
  595. * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
  596. * soon as possible, so that any processing of this BIGNUM might opt for
  597. * constant time implementations in the backend.
  598. *
  599. * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
  600. * to preallocate the BIGNUM internal buffer to a fixed public size big
  601. * enough that operations performed during the processing never trigger
  602. * a realloc which would leak the size of the scalar through memory
  603. * accesses.
  604. *
  605. * Fixed Length
  606. * ------------
  607. *
  608. * The order of the large prime subgroup of the curve is our choice for
  609. * a fixed public size, as that is generally the upper bound for
  610. * generating a private key in EC cryptosystems and should fit all valid
  611. * secret scalars.
  612. *
  613. * For padding on export we just use the bit length of the order
  614. * converted to bytes (rounding up).
  615. *
  616. * For preallocating the BIGNUM storage we look at the number of "words"
  617. * required for the internal representation of the order, and we
  618. * preallocate 2 extra "words" in case any of the subsequent processing
  619. * might temporarily overflow the order length.
  620. */
  621. ecbits = EC_GROUP_order_bits(ecg);
  622. if (ecbits <= 0)
  623. goto err;
  624. sz = (ecbits + 7 ) / 8;
  625. if (!OSSL_PARAM_BLD_push_BN_pad(tmpl,
  626. OSSL_PKEY_PARAM_PRIV_KEY,
  627. priv_key, sz))
  628. goto err;
  629. selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  630. /*
  631. * The ECDH Cofactor Mode is defined only if the EC_KEY actually
  632. * contains a private key, so we check for the flag and export it only
  633. * in this case.
  634. */
  635. ecdh_cofactor_mode =
  636. (EC_KEY_get_flags(eckey) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
  637. /* Export the ECDH_COFACTOR_MODE parameter */
  638. if (!OSSL_PARAM_BLD_push_int(tmpl,
  639. OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
  640. ecdh_cofactor_mode))
  641. goto err;
  642. selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
  643. }
  644. params = OSSL_PARAM_BLD_to_param(tmpl);
  645. /* We export, the provider imports */
  646. rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
  647. err:
  648. OSSL_PARAM_BLD_free(tmpl);
  649. OSSL_PARAM_BLD_free_params(params);
  650. OPENSSL_free(pub_key_buf);
  651. BN_CTX_free(bnctx);
  652. return rv;
  653. }
  654. static int ec_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
  655. {
  656. EVP_PKEY_CTX *pctx = vpctx;
  657. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  658. EC_KEY *ec = EC_KEY_new_ex(pctx->libctx);
  659. if (ec == NULL) {
  660. ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
  661. return 0;
  662. }
  663. if (!ec_key_domparams_fromdata(ec, params)
  664. || !ec_key_otherparams_fromdata(ec, params)
  665. || !ec_key_fromdata(ec, params, 1)
  666. || !EVP_PKEY_assign_EC_KEY(pkey, ec)) {
  667. EC_KEY_free(ec);
  668. return 0;
  669. }
  670. return 1;
  671. }
  672. const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
  673. EVP_PKEY_EC,
  674. EVP_PKEY_EC,
  675. 0,
  676. "EC",
  677. "OpenSSL EC algorithm",
  678. eckey_pub_decode,
  679. eckey_pub_encode,
  680. eckey_pub_cmp,
  681. eckey_pub_print,
  682. eckey_priv_decode,
  683. eckey_priv_encode,
  684. eckey_priv_print,
  685. int_ec_size,
  686. ec_bits,
  687. ec_security_bits,
  688. eckey_param_decode,
  689. eckey_param_encode,
  690. ec_missing_parameters,
  691. ec_copy_parameters,
  692. ec_cmp_parameters,
  693. eckey_param_print,
  694. 0,
  695. int_ec_free,
  696. ec_pkey_ctrl,
  697. old_ec_priv_decode,
  698. old_ec_priv_encode,
  699. 0, 0, 0,
  700. ec_pkey_check,
  701. ec_pkey_public_check,
  702. ec_pkey_param_check,
  703. 0, /* set_priv_key */
  704. 0, /* set_pub_key */
  705. 0, /* get_priv_key */
  706. 0, /* get_pub_key */
  707. ec_pkey_dirty_cnt,
  708. ec_pkey_export_to,
  709. ec_pkey_import_from
  710. };
  711. #if !defined(OPENSSL_NO_SM2)
  712. const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
  713. EVP_PKEY_SM2,
  714. EVP_PKEY_EC,
  715. ASN1_PKEY_ALIAS
  716. };
  717. #endif
  718. int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
  719. {
  720. int private = EC_KEY_get0_private_key(x) != NULL;
  721. return do_EC_KEY_print(bp, x, off,
  722. private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
  723. }
  724. int ECParameters_print(BIO *bp, const EC_KEY *x)
  725. {
  726. return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
  727. }
  728. #ifndef OPENSSL_NO_CMS
  729. static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
  730. X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
  731. {
  732. const ASN1_OBJECT *aoid;
  733. int atype;
  734. const void *aval;
  735. int rv = 0;
  736. EVP_PKEY *pkpeer = NULL;
  737. EC_KEY *ecpeer = NULL;
  738. const unsigned char *p;
  739. int plen;
  740. X509_ALGOR_get0(&aoid, &atype, &aval, alg);
  741. if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
  742. goto err;
  743. /* If absent parameters get group from main key */
  744. if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
  745. const EC_GROUP *grp;
  746. EVP_PKEY *pk;
  747. pk = EVP_PKEY_CTX_get0_pkey(pctx);
  748. if (pk == NULL)
  749. goto err;
  750. grp = EC_KEY_get0_group(pk->pkey.ec);
  751. ecpeer = EC_KEY_new();
  752. if (ecpeer == NULL)
  753. goto err;
  754. if (!EC_KEY_set_group(ecpeer, grp))
  755. goto err;
  756. } else {
  757. ecpeer = eckey_type2param(atype, aval);
  758. if (!ecpeer)
  759. goto err;
  760. }
  761. /* We have parameters now set public key */
  762. plen = ASN1_STRING_length(pubkey);
  763. p = ASN1_STRING_get0_data(pubkey);
  764. if (p == NULL || plen == 0)
  765. goto err;
  766. if (!o2i_ECPublicKey(&ecpeer, &p, plen))
  767. goto err;
  768. pkpeer = EVP_PKEY_new();
  769. if (pkpeer == NULL)
  770. goto err;
  771. EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
  772. if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
  773. rv = 1;
  774. err:
  775. EC_KEY_free(ecpeer);
  776. EVP_PKEY_free(pkpeer);
  777. return rv;
  778. }
  779. /* Set KDF parameters based on KDF NID */
  780. static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
  781. {
  782. int kdf_nid, kdfmd_nid, cofactor;
  783. const EVP_MD *kdf_md;
  784. if (eckdf_nid == NID_undef)
  785. return 0;
  786. /* Lookup KDF type, cofactor mode and digest */
  787. if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
  788. return 0;
  789. if (kdf_nid == NID_dh_std_kdf)
  790. cofactor = 0;
  791. else if (kdf_nid == NID_dh_cofactor_kdf)
  792. cofactor = 1;
  793. else
  794. return 0;
  795. if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
  796. return 0;
  797. if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
  798. return 0;
  799. kdf_md = EVP_get_digestbynid(kdfmd_nid);
  800. if (!kdf_md)
  801. return 0;
  802. if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
  803. return 0;
  804. return 1;
  805. }
  806. static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
  807. {
  808. int rv = 0;
  809. X509_ALGOR *alg, *kekalg = NULL;
  810. ASN1_OCTET_STRING *ukm;
  811. const unsigned char *p;
  812. unsigned char *der = NULL;
  813. int plen, keylen;
  814. const EVP_CIPHER *kekcipher;
  815. EVP_CIPHER_CTX *kekctx;
  816. if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
  817. return 0;
  818. if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
  819. ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
  820. return 0;
  821. }
  822. if (alg->parameter->type != V_ASN1_SEQUENCE)
  823. return 0;
  824. p = alg->parameter->value.sequence->data;
  825. plen = alg->parameter->value.sequence->length;
  826. kekalg = d2i_X509_ALGOR(NULL, &p, plen);
  827. if (!kekalg)
  828. goto err;
  829. kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  830. if (!kekctx)
  831. goto err;
  832. kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
  833. if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
  834. goto err;
  835. if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
  836. goto err;
  837. if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
  838. goto err;
  839. keylen = EVP_CIPHER_CTX_key_length(kekctx);
  840. if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
  841. goto err;
  842. plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
  843. if (!plen)
  844. goto err;
  845. if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
  846. goto err;
  847. der = NULL;
  848. rv = 1;
  849. err:
  850. X509_ALGOR_free(kekalg);
  851. OPENSSL_free(der);
  852. return rv;
  853. }
  854. static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
  855. {
  856. EVP_PKEY_CTX *pctx;
  857. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  858. if (!pctx)
  859. return 0;
  860. /* See if we need to set peer key */
  861. if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
  862. X509_ALGOR *alg;
  863. ASN1_BIT_STRING *pubkey;
  864. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
  865. NULL, NULL, NULL))
  866. return 0;
  867. if (!alg || !pubkey)
  868. return 0;
  869. if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
  870. ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
  871. return 0;
  872. }
  873. }
  874. /* Set ECDH derivation parameters and initialise unwrap context */
  875. if (!ecdh_cms_set_shared_info(pctx, ri)) {
  876. ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
  877. return 0;
  878. }
  879. return 1;
  880. }
  881. static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
  882. {
  883. EVP_PKEY_CTX *pctx;
  884. EVP_PKEY *pkey;
  885. EVP_CIPHER_CTX *ctx;
  886. int keylen;
  887. X509_ALGOR *talg, *wrap_alg = NULL;
  888. const ASN1_OBJECT *aoid;
  889. ASN1_BIT_STRING *pubkey;
  890. ASN1_STRING *wrap_str;
  891. ASN1_OCTET_STRING *ukm;
  892. unsigned char *penc = NULL;
  893. int penclen;
  894. int rv = 0;
  895. int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
  896. const EVP_MD *kdf_md;
  897. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  898. if (!pctx)
  899. return 0;
  900. /* Get ephemeral key */
  901. pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  902. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
  903. NULL, NULL, NULL))
  904. goto err;
  905. X509_ALGOR_get0(&aoid, NULL, NULL, talg);
  906. /* Is everything uninitialised? */
  907. if (aoid == OBJ_nid2obj(NID_undef)) {
  908. EC_KEY *eckey = pkey->pkey.ec;
  909. /* Set the key */
  910. unsigned char *p;
  911. penclen = i2o_ECPublicKey(eckey, NULL);
  912. if (penclen <= 0)
  913. goto err;
  914. penc = OPENSSL_malloc(penclen);
  915. if (penc == NULL)
  916. goto err;
  917. p = penc;
  918. penclen = i2o_ECPublicKey(eckey, &p);
  919. if (penclen <= 0)
  920. goto err;
  921. ASN1_STRING_set0(pubkey, penc, penclen);
  922. pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  923. pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  924. penc = NULL;
  925. X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
  926. V_ASN1_UNDEF, NULL);
  927. }
  928. /* See if custom parameters set */
  929. kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
  930. if (kdf_type <= 0)
  931. goto err;
  932. if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
  933. goto err;
  934. ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
  935. if (ecdh_nid < 0)
  936. goto err;
  937. else if (ecdh_nid == 0)
  938. ecdh_nid = NID_dh_std_kdf;
  939. else if (ecdh_nid == 1)
  940. ecdh_nid = NID_dh_cofactor_kdf;
  941. if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
  942. kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
  943. if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
  944. goto err;
  945. } else
  946. /* Unknown KDF */
  947. goto err;
  948. if (kdf_md == NULL) {
  949. /* Fixme later for better MD */
  950. kdf_md = EVP_sha1();
  951. if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
  952. goto err;
  953. }
  954. if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
  955. goto err;
  956. /* Lookup NID for KDF+cofactor+digest */
  957. if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
  958. goto err;
  959. /* Get wrap NID */
  960. ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  961. wrap_nid = EVP_CIPHER_CTX_type(ctx);
  962. keylen = EVP_CIPHER_CTX_key_length(ctx);
  963. /* Package wrap algorithm in an AlgorithmIdentifier */
  964. wrap_alg = X509_ALGOR_new();
  965. if (wrap_alg == NULL)
  966. goto err;
  967. wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
  968. wrap_alg->parameter = ASN1_TYPE_new();
  969. if (wrap_alg->parameter == NULL)
  970. goto err;
  971. if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
  972. goto err;
  973. if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
  974. ASN1_TYPE_free(wrap_alg->parameter);
  975. wrap_alg->parameter = NULL;
  976. }
  977. if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
  978. goto err;
  979. penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
  980. if (!penclen)
  981. goto err;
  982. if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
  983. goto err;
  984. penc = NULL;
  985. /*
  986. * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
  987. * of another AlgorithmIdentifier.
  988. */
  989. penclen = i2d_X509_ALGOR(wrap_alg, &penc);
  990. if (!penc || !penclen)
  991. goto err;
  992. wrap_str = ASN1_STRING_new();
  993. if (wrap_str == NULL)
  994. goto err;
  995. ASN1_STRING_set0(wrap_str, penc, penclen);
  996. penc = NULL;
  997. X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
  998. rv = 1;
  999. err:
  1000. OPENSSL_free(penc);
  1001. X509_ALGOR_free(wrap_alg);
  1002. return rv;
  1003. }
  1004. #endif