gost_ameth.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /**********************************************************************
  2. * gost_ameth.c *
  3. * Copyright (c) 2005-2006 Cryptocom LTD *
  4. * This file is distributed under the same license as OpenSSL *
  5. * *
  6. * Implementation of RFC 4490/4491 ASN1 method *
  7. * for OpenSSL *
  8. * Requires OpenSSL 0.9.9 for compilation *
  9. **********************************************************************/
  10. #include <string.h>
  11. #include <openssl/crypto.h>
  12. #include <openssl/err.h>
  13. #include <openssl/engine.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/asn1.h>
  16. #ifndef OPENSSL_NO_CMS
  17. # include <openssl/cms.h>
  18. #endif
  19. #include "gost_params.h"
  20. #include "gost_lcl.h"
  21. #include "e_gost_err.h"
  22. int gost94_nid_by_params(DSA *p)
  23. {
  24. R3410_params *gost_params;
  25. BIGNUM *q = BN_new();
  26. for (gost_params = R3410_paramset; gost_params->q != NULL; gost_params++) {
  27. BN_dec2bn(&q, gost_params->q);
  28. if (!BN_cmp(q, p->q)) {
  29. BN_free(q);
  30. return gost_params->nid;
  31. }
  32. }
  33. BN_free(q);
  34. return NID_undef;
  35. }
  36. static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
  37. {
  38. ASN1_STRING *params = ASN1_STRING_new();
  39. GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
  40. int pkey_param_nid = NID_undef;
  41. if (!params || !gkp) {
  42. GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
  43. ASN1_STRING_free(params);
  44. params = NULL;
  45. goto err;
  46. }
  47. switch (EVP_PKEY_base_id(key)) {
  48. case NID_id_GostR3410_2001:
  49. pkey_param_nid =
  50. EC_GROUP_get_curve_name(EC_KEY_get0_group
  51. (EVP_PKEY_get0((EVP_PKEY *)key)));
  52. break;
  53. case NID_id_GostR3410_94:
  54. pkey_param_nid =
  55. (int)gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)key));
  56. if (pkey_param_nid == NID_undef) {
  57. GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
  58. GOST_R_INVALID_GOST94_PARMSET);
  59. ASN1_STRING_free(params);
  60. params = NULL;
  61. goto err;
  62. }
  63. break;
  64. }
  65. gkp->key_params = OBJ_nid2obj(pkey_param_nid);
  66. gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
  67. /*
  68. * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
  69. */
  70. params->length = i2d_GOST_KEY_PARAMS(gkp, &params->data);
  71. if (params->length <= 0) {
  72. GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
  73. ASN1_STRING_free(params);
  74. params = NULL;
  75. goto err;
  76. }
  77. params->type = V_ASN1_SEQUENCE;
  78. err:
  79. GOST_KEY_PARAMS_free(gkp);
  80. return params;
  81. }
  82. /*
  83. * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
  84. * NID and parameters
  85. */
  86. static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg)
  87. {
  88. ASN1_OBJECT *palg_obj = NULL;
  89. int ptype = V_ASN1_UNDEF;
  90. int pkey_nid = NID_undef, param_nid = NID_undef;
  91. void *_pval;
  92. ASN1_STRING *pval = NULL;
  93. const unsigned char *p;
  94. GOST_KEY_PARAMS *gkp = NULL;
  95. X509_ALGOR_get0(&palg_obj, &ptype, &_pval, palg);
  96. pval = _pval;
  97. if (ptype != V_ASN1_SEQUENCE) {
  98. GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
  99. GOST_R_BAD_KEY_PARAMETERS_FORMAT);
  100. return 0;
  101. }
  102. p = pval->data;
  103. pkey_nid = OBJ_obj2nid(palg_obj);
  104. gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
  105. if (!gkp) {
  106. GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
  107. GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
  108. return 0;
  109. }
  110. param_nid = OBJ_obj2nid(gkp->key_params);
  111. GOST_KEY_PARAMS_free(gkp);
  112. if(!EVP_PKEY_set_type(pkey, pkey_nid)) {
  113. GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, ERR_R_INTERNAL_ERROR);
  114. return 0;
  115. }
  116. switch (pkey_nid) {
  117. case NID_id_GostR3410_94:
  118. {
  119. DSA *dsa = EVP_PKEY_get0(pkey);
  120. if (!dsa) {
  121. dsa = DSA_new();
  122. if (!EVP_PKEY_assign(pkey, pkey_nid, dsa))
  123. return 0;
  124. }
  125. if (!fill_GOST94_params(dsa, param_nid))
  126. return 0;
  127. break;
  128. }
  129. case NID_id_GostR3410_2001:
  130. {
  131. EC_KEY *ec = EVP_PKEY_get0(pkey);
  132. if (!ec) {
  133. ec = EC_KEY_new();
  134. if (!EVP_PKEY_assign(pkey, pkey_nid, ec))
  135. return 0;
  136. }
  137. if (!fill_GOST2001_params(ec, param_nid))
  138. return 0;
  139. }
  140. }
  141. return 1;
  142. }
  143. static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
  144. {
  145. switch (EVP_PKEY_base_id(pkey)) {
  146. case NID_id_GostR3410_94:
  147. {
  148. DSA *dsa = EVP_PKEY_get0(pkey);
  149. if (!dsa) {
  150. dsa = DSA_new();
  151. EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), dsa);
  152. }
  153. dsa->priv_key = BN_dup(priv);
  154. if (!EVP_PKEY_missing_parameters(pkey))
  155. gost94_compute_public(dsa);
  156. break;
  157. }
  158. case NID_id_GostR3410_2001:
  159. {
  160. EC_KEY *ec = EVP_PKEY_get0(pkey);
  161. if (!ec) {
  162. ec = EC_KEY_new();
  163. EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
  164. }
  165. if (!EC_KEY_set_private_key(ec, priv))
  166. return 0;
  167. if (!EVP_PKEY_missing_parameters(pkey))
  168. gost2001_compute_public(ec);
  169. break;
  170. }
  171. }
  172. return 1;
  173. }
  174. BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
  175. {
  176. switch (EVP_PKEY_base_id(pkey)) {
  177. case NID_id_GostR3410_94:
  178. {
  179. DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pkey);
  180. if (!dsa) {
  181. return NULL;
  182. }
  183. if (!dsa->priv_key)
  184. return NULL;
  185. return dsa->priv_key;
  186. break;
  187. }
  188. case NID_id_GostR3410_2001:
  189. {
  190. EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
  191. const BIGNUM *priv;
  192. if (!ec) {
  193. return NULL;
  194. }
  195. if (!(priv = EC_KEY_get0_private_key(ec)))
  196. return NULL;
  197. return (BIGNUM *)priv;
  198. break;
  199. }
  200. }
  201. return NULL;
  202. }
  203. static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  204. {
  205. switch (op) {
  206. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  207. if (arg1 == 0) {
  208. X509_ALGOR *alg1 = NULL, *alg2 = NULL;
  209. int nid = EVP_PKEY_base_id(pkey);
  210. PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2,
  211. NULL, &alg1, &alg2);
  212. X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
  213. V_ASN1_NULL, 0);
  214. if (nid == NID_undef) {
  215. return (-1);
  216. }
  217. X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
  218. }
  219. return 1;
  220. #ifndef OPENSSL_NO_CMS
  221. case ASN1_PKEY_CTRL_CMS_SIGN:
  222. if (arg1 == 0) {
  223. X509_ALGOR *alg1 = NULL, *alg2 = NULL;
  224. int nid = EVP_PKEY_base_id(pkey);
  225. CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2,
  226. NULL, NULL, &alg1, &alg2);
  227. X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
  228. V_ASN1_NULL, 0);
  229. if (nid == NID_undef) {
  230. return (-1);
  231. }
  232. X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
  233. }
  234. return 1;
  235. #endif
  236. case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
  237. if (arg1 == 0) {
  238. X509_ALGOR *alg;
  239. ASN1_STRING *params = encode_gost_algor_params(pkey);
  240. if (!params) {
  241. return -1;
  242. }
  243. PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg);
  244. X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type),
  245. V_ASN1_SEQUENCE, params);
  246. }
  247. return 1;
  248. #ifndef OPENSSL_NO_CMS
  249. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  250. if (arg1 == 0) {
  251. X509_ALGOR *alg = NULL;
  252. ASN1_STRING *params = encode_gost_algor_params(pkey);
  253. if (!params) {
  254. return -1;
  255. }
  256. CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
  257. NULL, &alg);
  258. X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type), V_ASN1_SEQUENCE,
  259. params);
  260. }
  261. return 1;
  262. #endif
  263. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  264. *(int *)arg2 = NID_id_GostR3411_94;
  265. return 2;
  266. }
  267. return -2;
  268. }
  269. /* --------------------- free functions * ------------------------------*/
  270. static void pkey_free_gost94(EVP_PKEY *key)
  271. {
  272. if (key->pkey.dsa) {
  273. DSA_free(key->pkey.dsa);
  274. }
  275. }
  276. static void pkey_free_gost01(EVP_PKEY *key)
  277. {
  278. if (key->pkey.ec) {
  279. EC_KEY_free(key->pkey.ec);
  280. }
  281. }
  282. /* ------------------ private key functions -----------------------------*/
  283. static int priv_decode_gost(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf)
  284. {
  285. const unsigned char *pkey_buf = NULL, *p = NULL;
  286. int priv_len = 0;
  287. BIGNUM *pk_num = NULL;
  288. int ret = 0;
  289. X509_ALGOR *palg = NULL;
  290. ASN1_OBJECT *palg_obj = NULL;
  291. ASN1_INTEGER *priv_key = NULL;
  292. if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
  293. return 0;
  294. p = pkey_buf;
  295. if (!decode_gost_algor_params(pk, palg)) {
  296. return 0;
  297. }
  298. if (V_ASN1_OCTET_STRING == *p) {
  299. /* New format - Little endian octet string */
  300. unsigned char rev_buf[32];
  301. int i;
  302. ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
  303. if (!s || s->length != 32) {
  304. GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
  305. return 0;
  306. }
  307. for (i = 0; i < 32; i++) {
  308. rev_buf[31 - i] = s->data[i];
  309. }
  310. ASN1_STRING_free(s);
  311. pk_num = getbnfrombuf(rev_buf, 32);
  312. } else {
  313. priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
  314. if (!priv_key)
  315. return 0;
  316. ret = ((pk_num = ASN1_INTEGER_to_BN(priv_key, NULL)) != NULL);
  317. ASN1_INTEGER_free(priv_key);
  318. if (!ret) {
  319. GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
  320. return 0;
  321. }
  322. }
  323. ret = gost_set_priv_key(pk, pk_num);
  324. BN_free(pk_num);
  325. return ret;
  326. }
  327. /* ----------------------------------------------------------------------*/
  328. static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
  329. {
  330. ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
  331. ASN1_STRING *params = encode_gost_algor_params(pk);
  332. unsigned char *priv_buf = NULL;
  333. int priv_len;
  334. ASN1_INTEGER *asn1key = NULL;
  335. if (!params) {
  336. return 0;
  337. }
  338. asn1key = BN_to_ASN1_INTEGER(gost_get0_priv_key(pk), NULL);
  339. priv_len = i2d_ASN1_INTEGER(asn1key, &priv_buf);
  340. ASN1_INTEGER_free(asn1key);
  341. return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
  342. priv_buf, priv_len);
  343. }
  344. /* --------- printing keys --------------------------------*/
  345. static int print_gost_94(BIO *out, const EVP_PKEY *pkey, int indent,
  346. ASN1_PCTX *pctx, int type)
  347. {
  348. int param_nid = NID_undef;
  349. if (type == 2) {
  350. BIGNUM *key;
  351. if (!BIO_indent(out, indent, 128))
  352. return 0;
  353. BIO_printf(out, "Private key: ");
  354. key = gost_get0_priv_key(pkey);
  355. if (!key)
  356. BIO_printf(out, "<undefined>");
  357. else
  358. BN_print(out, key);
  359. BIO_printf(out, "\n");
  360. }
  361. if (type >= 1) {
  362. BIGNUM *pubkey;
  363. pubkey = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
  364. BIO_indent(out, indent, 128);
  365. BIO_printf(out, "Public key: ");
  366. BN_print(out, pubkey);
  367. BIO_printf(out, "\n");
  368. }
  369. param_nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
  370. BIO_indent(out, indent, 128);
  371. BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
  372. return 1;
  373. }
  374. static int param_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
  375. ASN1_PCTX *pctx)
  376. {
  377. return print_gost_94(out, pkey, indent, pctx, 0);
  378. }
  379. static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
  380. ASN1_PCTX *pctx)
  381. {
  382. return print_gost_94(out, pkey, indent, pctx, 1);
  383. }
  384. static int priv_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
  385. ASN1_PCTX *pctx)
  386. {
  387. return print_gost_94(out, pkey, indent, pctx, 2);
  388. }
  389. static int print_gost_01(BIO *out, const EVP_PKEY *pkey, int indent,
  390. ASN1_PCTX *pctx, int type)
  391. {
  392. int param_nid = NID_undef;
  393. if (type == 2) {
  394. BIGNUM *key;
  395. if (!BIO_indent(out, indent, 128))
  396. return 0;
  397. BIO_printf(out, "Private key: ");
  398. key = gost_get0_priv_key(pkey);
  399. if (!key)
  400. BIO_printf(out, "<undefined)");
  401. else
  402. BN_print(out, key);
  403. BIO_printf(out, "\n");
  404. }
  405. if (type >= 1) {
  406. BN_CTX *ctx = BN_CTX_new();
  407. BIGNUM *X, *Y;
  408. const EC_POINT *pubkey;
  409. const EC_GROUP *group;
  410. if (!ctx) {
  411. GOSTerr(GOST_F_PRINT_GOST_01, ERR_R_MALLOC_FAILURE);
  412. return 0;
  413. }
  414. BN_CTX_start(ctx);
  415. X = BN_CTX_get(ctx);
  416. Y = BN_CTX_get(ctx);
  417. pubkey =
  418. EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
  419. group = EC_KEY_get0_group((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
  420. if (!EC_POINT_get_affine_coordinates_GFp(group, pubkey, X, Y, ctx)) {
  421. GOSTerr(GOST_F_PRINT_GOST_01, ERR_R_EC_LIB);
  422. BN_CTX_free(ctx);
  423. return 0;
  424. }
  425. if (!BIO_indent(out, indent, 128))
  426. return 0;
  427. BIO_printf(out, "Public key:\n");
  428. if (!BIO_indent(out, indent + 3, 128))
  429. return 0;
  430. BIO_printf(out, "X:");
  431. BN_print(out, X);
  432. BIO_printf(out, "\n");
  433. BIO_indent(out, indent + 3, 128);
  434. BIO_printf(out, "Y:");
  435. BN_print(out, Y);
  436. BIO_printf(out, "\n");
  437. BN_CTX_end(ctx);
  438. BN_CTX_free(ctx);
  439. }
  440. param_nid =
  441. EC_GROUP_get_curve_name(EC_KEY_get0_group
  442. (EVP_PKEY_get0((EVP_PKEY *)pkey)));
  443. if (!BIO_indent(out, indent, 128))
  444. return 0;
  445. BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
  446. return 1;
  447. }
  448. static int param_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
  449. ASN1_PCTX *pctx)
  450. {
  451. return print_gost_01(out, pkey, indent, pctx, 0);
  452. }
  453. static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
  454. ASN1_PCTX *pctx)
  455. {
  456. return print_gost_01(out, pkey, indent, pctx, 1);
  457. }
  458. static int priv_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
  459. ASN1_PCTX *pctx)
  460. {
  461. return print_gost_01(out, pkey, indent, pctx, 2);
  462. }
  463. /* ---------------------------------------------------------------------*/
  464. static int param_missing_gost94(const EVP_PKEY *pk)
  465. {
  466. const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
  467. if (!dsa)
  468. return 1;
  469. if (!dsa->q)
  470. return 1;
  471. return 0;
  472. }
  473. static int param_missing_gost01(const EVP_PKEY *pk)
  474. {
  475. const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
  476. if (!ec)
  477. return 1;
  478. if (!EC_KEY_get0_group(ec))
  479. return 1;
  480. return 0;
  481. }
  482. static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from)
  483. {
  484. const DSA *dfrom = EVP_PKEY_get0((EVP_PKEY *)from);
  485. DSA *dto = EVP_PKEY_get0(to);
  486. if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
  487. GOSTerr(GOST_F_PARAM_COPY_GOST94, GOST_R_INCOMPATIBLE_ALGORITHMS);
  488. return 0;
  489. }
  490. if (!dfrom) {
  491. GOSTerr(GOST_F_PARAM_COPY_GOST94, GOST_R_KEY_PARAMETERS_MISSING);
  492. return 0;
  493. }
  494. if (!dto) {
  495. dto = DSA_new();
  496. EVP_PKEY_assign(to, EVP_PKEY_base_id(from), dto);
  497. }
  498. #define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x);
  499. COPYBIGNUM(dto, dfrom, p)
  500. COPYBIGNUM(dto, dfrom, q)
  501. COPYBIGNUM(dto, dfrom, g)
  502. if (dto->priv_key)
  503. gost94_compute_public(dto);
  504. return 1;
  505. }
  506. static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from)
  507. {
  508. EC_KEY *eto = EVP_PKEY_get0(to);
  509. const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
  510. if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
  511. GOSTerr(GOST_F_PARAM_COPY_GOST01, GOST_R_INCOMPATIBLE_ALGORITHMS);
  512. return 0;
  513. }
  514. if (!efrom) {
  515. GOSTerr(GOST_F_PARAM_COPY_GOST01, GOST_R_KEY_PARAMETERS_MISSING);
  516. return 0;
  517. }
  518. if (!eto) {
  519. eto = EC_KEY_new();
  520. if(!eto) {
  521. GOSTerr(GOST_F_PARAM_COPY_GOST01, ERR_R_MALLOC_FAILURE);
  522. return 0;
  523. }
  524. if(!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
  525. GOSTerr(GOST_F_PARAM_COPY_GOST01, ERR_R_INTERNAL_ERROR);
  526. return 0;
  527. }
  528. }
  529. if(!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
  530. GOSTerr(GOST_F_PARAM_COPY_GOST01, ERR_R_INTERNAL_ERROR);
  531. return 0;
  532. }
  533. if (EC_KEY_get0_private_key(eto)) {
  534. gost2001_compute_public(eto);
  535. }
  536. return 1;
  537. }
  538. static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
  539. {
  540. const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
  541. const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
  542. if (!BN_cmp(da->q, db->q))
  543. return 1;
  544. return 0;
  545. }
  546. static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
  547. {
  548. if (EC_GROUP_get_curve_name
  549. (EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)a))) ==
  550. EC_GROUP_get_curve_name(EC_KEY_get0_group
  551. (EVP_PKEY_get0((EVP_PKEY *)b)))) {
  552. return 1;
  553. }
  554. return 0;
  555. }
  556. /* ---------- Public key functions * --------------------------------------*/
  557. static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub)
  558. {
  559. X509_ALGOR *palg = NULL;
  560. const unsigned char *pubkey_buf = NULL;
  561. unsigned char *databuf;
  562. ASN1_OBJECT *palgobj = NULL;
  563. int pub_len, i, j;
  564. DSA *dsa;
  565. ASN1_OCTET_STRING *octet = NULL;
  566. if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
  567. return 0;
  568. EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
  569. if (!decode_gost_algor_params(pk, palg))
  570. return 0;
  571. octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
  572. if (!octet) {
  573. GOSTerr(GOST_F_PUB_DECODE_GOST94, ERR_R_MALLOC_FAILURE);
  574. return 0;
  575. }
  576. databuf = OPENSSL_malloc(octet->length);
  577. if (databuf == NULL) {
  578. GOSTerr(GOST_F_PUB_DECODE_GOST94, ERR_R_MALLOC_FAILURE);
  579. return 0;
  580. }
  581. for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) {
  582. databuf[j] = octet->data[i];
  583. }
  584. dsa = EVP_PKEY_get0(pk);
  585. dsa->pub_key = BN_bin2bn(databuf, octet->length, NULL);
  586. ASN1_OCTET_STRING_free(octet);
  587. OPENSSL_free(databuf);
  588. return 1;
  589. }
  590. static int pub_encode_gost94(X509_PUBKEY *pub, const EVP_PKEY *pk)
  591. {
  592. ASN1_OBJECT *algobj = NULL;
  593. ASN1_OCTET_STRING *octet = NULL;
  594. void *pval = NULL;
  595. unsigned char *buf = NULL, *databuf, *sptr;
  596. int i, j, data_len, ret = 0;
  597. int ptype = V_ASN1_UNDEF;
  598. DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
  599. algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
  600. if (pk->save_parameters) {
  601. ASN1_STRING *params = encode_gost_algor_params(pk);
  602. pval = params;
  603. ptype = V_ASN1_SEQUENCE;
  604. }
  605. data_len = BN_num_bytes(dsa->pub_key);
  606. databuf = OPENSSL_malloc(data_len);
  607. if (databuf == NULL)
  608. return 0;
  609. BN_bn2bin(dsa->pub_key, databuf);
  610. octet = ASN1_OCTET_STRING_new();
  611. ASN1_STRING_set(octet, NULL, data_len);
  612. sptr = ASN1_STRING_data(octet);
  613. for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
  614. sptr[i] = databuf[j];
  615. }
  616. OPENSSL_free(databuf);
  617. ret = i2d_ASN1_OCTET_STRING(octet, &buf);
  618. ASN1_BIT_STRING_free(octet);
  619. if (ret < 0)
  620. return 0;
  621. return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
  622. }
  623. static int pub_decode_gost01(EVP_PKEY *pk, X509_PUBKEY *pub)
  624. {
  625. X509_ALGOR *palg = NULL;
  626. const unsigned char *pubkey_buf = NULL;
  627. unsigned char *databuf;
  628. ASN1_OBJECT *palgobj = NULL;
  629. int pub_len, i, j;
  630. EC_POINT *pub_key;
  631. BIGNUM *X, *Y;
  632. ASN1_OCTET_STRING *octet = NULL;
  633. int len;
  634. const EC_GROUP *group;
  635. if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
  636. return 0;
  637. EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
  638. if (!decode_gost_algor_params(pk, palg))
  639. return 0;
  640. group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
  641. octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
  642. if (!octet) {
  643. GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_MALLOC_FAILURE);
  644. return 0;
  645. }
  646. databuf = OPENSSL_malloc(octet->length);
  647. if (databuf == NULL) {
  648. GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_MALLOC_FAILURE);
  649. return 0;
  650. }
  651. for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) {
  652. databuf[j] = octet->data[i];
  653. }
  654. len = octet->length / 2;
  655. ASN1_OCTET_STRING_free(octet);
  656. Y = getbnfrombuf(databuf, len);
  657. X = getbnfrombuf(databuf + len, len);
  658. OPENSSL_free(databuf);
  659. pub_key = EC_POINT_new(group);
  660. if (!EC_POINT_set_affine_coordinates_GFp(group, pub_key, X, Y, NULL)) {
  661. GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_EC_LIB);
  662. EC_POINT_free(pub_key);
  663. BN_free(X);
  664. BN_free(Y);
  665. return 0;
  666. }
  667. BN_free(X);
  668. BN_free(Y);
  669. if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
  670. GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_EC_LIB);
  671. EC_POINT_free(pub_key);
  672. return 0;
  673. }
  674. EC_POINT_free(pub_key);
  675. return 1;
  676. }
  677. static int pub_encode_gost01(X509_PUBKEY *pub, const EVP_PKEY *pk)
  678. {
  679. ASN1_OBJECT *algobj = NULL;
  680. ASN1_OCTET_STRING *octet = NULL;
  681. void *pval = NULL;
  682. unsigned char *buf = NULL, *databuf, *sptr;
  683. int i, j, data_len, ret = 0;
  684. const EC_POINT *pub_key;
  685. BIGNUM *X, *Y, *order;
  686. const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
  687. int ptype = V_ASN1_UNDEF;
  688. algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
  689. if (pk->save_parameters) {
  690. ASN1_STRING *params = encode_gost_algor_params(pk);
  691. pval = params;
  692. ptype = V_ASN1_SEQUENCE;
  693. }
  694. order = BN_new();
  695. EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
  696. pub_key = EC_KEY_get0_public_key(ec);
  697. if (!pub_key) {
  698. GOSTerr(GOST_F_PUB_ENCODE_GOST01, GOST_R_PUBLIC_KEY_UNDEFINED);
  699. return 0;
  700. }
  701. X = BN_new();
  702. Y = BN_new();
  703. if(!X || !Y) {
  704. GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
  705. if(X) BN_free(X);
  706. if(Y) BN_free(Y);
  707. BN_free(order);
  708. return 0;
  709. }
  710. if(!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
  711. pub_key, X, Y, NULL)) {
  712. GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_INTERNAL_ERROR);
  713. BN_free(X);
  714. BN_free(Y);
  715. BN_free(order);
  716. return 0;
  717. }
  718. data_len = 2 * BN_num_bytes(order);
  719. BN_free(order);
  720. databuf = OPENSSL_malloc(data_len);
  721. if (databuf == NULL) {
  722. GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
  723. return 0;
  724. }
  725. memset(databuf, 0, data_len);
  726. store_bignum(X, databuf + data_len / 2, data_len / 2);
  727. store_bignum(Y, databuf, data_len / 2);
  728. BN_free(X);
  729. BN_free(Y);
  730. octet = ASN1_OCTET_STRING_new();
  731. ASN1_STRING_set(octet, NULL, data_len);
  732. sptr = ASN1_STRING_data(octet);
  733. for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
  734. sptr[i] = databuf[j];
  735. }
  736. OPENSSL_free(databuf);
  737. ret = i2d_ASN1_OCTET_STRING(octet, &buf);
  738. ASN1_BIT_STRING_free(octet);
  739. if (ret < 0)
  740. return 0;
  741. return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
  742. }
  743. static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
  744. {
  745. const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
  746. const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
  747. if (da && db && da->pub_key && db->pub_key
  748. && !BN_cmp(da->pub_key, db->pub_key)) {
  749. return 1;
  750. }
  751. return 0;
  752. }
  753. static int pub_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
  754. {
  755. const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
  756. const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
  757. const EC_POINT *ka, *kb;
  758. int ret = 0;
  759. if (!ea || !eb)
  760. return 0;
  761. ka = EC_KEY_get0_public_key(ea);
  762. kb = EC_KEY_get0_public_key(eb);
  763. if (!ka || !kb)
  764. return 0;
  765. ret = (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
  766. return ret;
  767. }
  768. static int pkey_size_gost(const EVP_PKEY *pk)
  769. {
  770. return 64;
  771. }
  772. static int pkey_bits_gost(const EVP_PKEY *pk)
  773. {
  774. return 256;
  775. }
  776. /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/
  777. static void mackey_free_gost(EVP_PKEY *pk)
  778. {
  779. if (pk->pkey.ptr) {
  780. OPENSSL_free(pk->pkey.ptr);
  781. }
  782. }
  783. static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  784. {
  785. switch (op) {
  786. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  787. *(int *)arg2 = NID_id_Gost28147_89_MAC;
  788. return 2;
  789. }
  790. return -2;
  791. }
  792. static int gost94_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  793. {
  794. int nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
  795. return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
  796. }
  797. static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  798. {
  799. int nid =
  800. EC_GROUP_get_curve_name(EC_KEY_get0_group
  801. (EVP_PKEY_get0((EVP_PKEY *)pkey)));
  802. return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
  803. }
  804. static int gost94_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
  805. int derlen)
  806. {
  807. ASN1_OBJECT *obj = NULL;
  808. DSA *dsa = EVP_PKEY_get0(pkey);
  809. int nid;
  810. if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
  811. return 0;
  812. }
  813. nid = OBJ_obj2nid(obj);
  814. ASN1_OBJECT_free(obj);
  815. if (!dsa) {
  816. dsa = DSA_new();
  817. if (!EVP_PKEY_assign(pkey, NID_id_GostR3410_94, dsa))
  818. return 0;
  819. }
  820. if (!fill_GOST94_params(dsa, nid))
  821. return 0;
  822. return 1;
  823. }
  824. static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
  825. int derlen)
  826. {
  827. ASN1_OBJECT *obj = NULL;
  828. int nid;
  829. EC_KEY *ec = EVP_PKEY_get0(pkey);
  830. if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
  831. return 0;
  832. }
  833. nid = OBJ_obj2nid(obj);
  834. ASN1_OBJECT_free(obj);
  835. if (!ec) {
  836. ec = EC_KEY_new();
  837. if (!EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec))
  838. return 0;
  839. }
  840. if (!fill_GOST2001_params(ec, nid))
  841. return 0;
  842. return 1;
  843. }
  844. /* ----------------------------------------------------------------------*/
  845. int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
  846. const char *pemstr, const char *info)
  847. {
  848. *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
  849. if (!*ameth)
  850. return 0;
  851. switch (nid) {
  852. case NID_id_GostR3410_94:
  853. EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost94);
  854. EVP_PKEY_asn1_set_private(*ameth,
  855. priv_decode_gost, priv_encode_gost,
  856. priv_print_gost94);
  857. EVP_PKEY_asn1_set_param(*ameth,
  858. gost94_param_decode, gost94_param_encode,
  859. param_missing_gost94, param_copy_gost94,
  860. param_cmp_gost94, param_print_gost94);
  861. EVP_PKEY_asn1_set_public(*ameth,
  862. pub_decode_gost94, pub_encode_gost94,
  863. pub_cmp_gost94, pub_print_gost94,
  864. pkey_size_gost, pkey_bits_gost);
  865. EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
  866. break;
  867. case NID_id_GostR3410_2001:
  868. EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost01);
  869. EVP_PKEY_asn1_set_private(*ameth,
  870. priv_decode_gost, priv_encode_gost,
  871. priv_print_gost01);
  872. EVP_PKEY_asn1_set_param(*ameth,
  873. gost2001_param_decode, gost2001_param_encode,
  874. param_missing_gost01, param_copy_gost01,
  875. param_cmp_gost01, param_print_gost01);
  876. EVP_PKEY_asn1_set_public(*ameth,
  877. pub_decode_gost01, pub_encode_gost01,
  878. pub_cmp_gost01, pub_print_gost01,
  879. pkey_size_gost, pkey_bits_gost);
  880. EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
  881. break;
  882. case NID_id_Gost28147_89_MAC:
  883. EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
  884. EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);
  885. break;
  886. }
  887. return 1;
  888. }