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