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