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