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