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