gost_ameth.c 22 KB

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