651b463235d3c4a6964bc185e2a6860cdaa82f0b
[openssl.git] / crypto / dsa / dsa_ameth.c
1 /*
2  * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * DSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include <openssl/x509.h>
18 #include <openssl/asn1.h>
19 #include <openssl/bn.h>
20 #include <openssl/cms.h>
21 #include <openssl/core_names.h>
22 #include <openssl/param_build.h>
23 #include "internal/cryptlib.h"
24 #include "crypto/asn1.h"
25 #include "crypto/dsa.h"
26 #include "crypto/evp.h"
27 #include "internal/ffc.h"
28 #include "dsa_local.h"
29
30 static int dsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
31 {
32     const unsigned char *p, *pm;
33     int pklen, pmlen;
34     int ptype;
35     const void *pval;
36     const ASN1_STRING *pstr;
37     X509_ALGOR *palg;
38     ASN1_INTEGER *public_key = NULL;
39
40     DSA *dsa = NULL;
41
42     if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
43         return 0;
44     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
45
46     if (ptype == V_ASN1_SEQUENCE) {
47         pstr = pval;
48         pm = pstr->data;
49         pmlen = pstr->length;
50
51         if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) {
52             DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
53             goto err;
54         }
55
56     } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
57         if ((dsa = DSA_new()) == NULL) {
58             DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
59             goto err;
60         }
61     } else {
62         DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
63         goto err;
64     }
65
66     if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
67         DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
68         goto err;
69     }
70
71     if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
72         DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
73         goto err;
74     }
75
76     dsa->dirty_cnt++;
77     ASN1_INTEGER_free(public_key);
78     EVP_PKEY_assign_DSA(pkey, dsa);
79     return 1;
80
81  err:
82     ASN1_INTEGER_free(public_key);
83     DSA_free(dsa);
84     return 0;
85
86 }
87
88 static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
89 {
90     DSA *dsa;
91     int ptype;
92     unsigned char *penc = NULL;
93     int penclen;
94     ASN1_STRING *str = NULL;
95     ASN1_INTEGER *pubint = NULL;
96     ASN1_OBJECT *aobj;
97
98     dsa = pkey->pkey.dsa;
99     if (pkey->save_parameters
100         && dsa->params.p != NULL
101         && dsa->params.q != NULL
102         && dsa->params.g != NULL) {
103         str = ASN1_STRING_new();
104         if (str == NULL) {
105             DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
106             goto err;
107         }
108         str->length = i2d_DSAparams(dsa, &str->data);
109         if (str->length <= 0) {
110             DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
111             goto err;
112         }
113         ptype = V_ASN1_SEQUENCE;
114     } else
115         ptype = V_ASN1_UNDEF;
116
117     pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
118
119     if (pubint == NULL) {
120         DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
121         goto err;
122     }
123
124     penclen = i2d_ASN1_INTEGER(pubint, &penc);
125     ASN1_INTEGER_free(pubint);
126
127     if (penclen <= 0) {
128         DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
129         goto err;
130     }
131
132     aobj = OBJ_nid2obj(EVP_PKEY_DSA);
133     if (aobj == NULL)
134         goto err;
135
136     if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen))
137         return 1;
138
139  err:
140     OPENSSL_free(penc);
141     ASN1_STRING_free(str);
142
143     return 0;
144 }
145
146 /*
147  * In PKCS#8 DSA: you just get a private key integer and parameters in the
148  * AlgorithmIdentifier the pubkey must be recalculated.
149  */
150
151 static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
152 {
153     const unsigned char *p, *pm;
154     int pklen, pmlen;
155     int ptype;
156     const void *pval;
157     const ASN1_STRING *pstr;
158     const X509_ALGOR *palg;
159     ASN1_INTEGER *privkey = NULL;
160     BN_CTX *ctx = NULL;
161
162     DSA *dsa = NULL;
163
164     int ret = 0;
165
166     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
167         return 0;
168     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
169
170     if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
171         goto decerr;
172     if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
173         goto decerr;
174
175     pstr = pval;
176     pm = pstr->data;
177     pmlen = pstr->length;
178     if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
179         goto decerr;
180     /* We have parameters now set private key */
181     if ((dsa->priv_key = BN_secure_new()) == NULL
182         || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {
183         DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
184         goto dsaerr;
185     }
186     /* Calculate public key */
187     if ((dsa->pub_key = BN_new()) == NULL) {
188         DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
189         goto dsaerr;
190     }
191     if ((ctx = BN_CTX_new()) == NULL) {
192         DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
193         goto dsaerr;
194     }
195
196     BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME);
197     if (!BN_mod_exp(dsa->pub_key, dsa->params.g, dsa->priv_key, dsa->params.p,
198                     ctx)) {
199         DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
200         goto dsaerr;
201     }
202
203     dsa->dirty_cnt++;
204     EVP_PKEY_assign_DSA(pkey, dsa);
205
206     ret = 1;
207     goto done;
208
209  decerr:
210     DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
211  dsaerr:
212     DSA_free(dsa);
213  done:
214     BN_CTX_free(ctx);
215     ASN1_STRING_clear_free(privkey);
216     return ret;
217 }
218
219 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
220 {
221     ASN1_STRING *params = NULL;
222     ASN1_INTEGER *prkey = NULL;
223     unsigned char *dp = NULL;
224     int dplen;
225
226     if (pkey->pkey.dsa  == NULL|| pkey->pkey.dsa->priv_key == NULL) {
227         DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
228         goto err;
229     }
230
231     params = ASN1_STRING_new();
232
233     if (params == NULL) {
234         DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
235         goto err;
236     }
237
238     params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
239     if (params->length <= 0) {
240         DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
241         goto err;
242     }
243     params->type = V_ASN1_SEQUENCE;
244
245     /* Get private key into integer */
246     prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
247
248     if (prkey == NULL) {
249         DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
250         goto err;
251     }
252
253     dplen = i2d_ASN1_INTEGER(prkey, &dp);
254
255     ASN1_STRING_clear_free(prkey);
256     prkey = NULL;
257
258     if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
259                          V_ASN1_SEQUENCE, params, dp, dplen))
260         goto err;
261
262     return 1;
263
264  err:
265     OPENSSL_free(dp);
266     ASN1_STRING_free(params);
267     ASN1_STRING_clear_free(prkey);
268     return 0;
269 }
270
271 static int int_dsa_size(const EVP_PKEY *pkey)
272 {
273     return DSA_size(pkey->pkey.dsa);
274 }
275
276 static int dsa_bits(const EVP_PKEY *pkey)
277 {
278     return DSA_bits(pkey->pkey.dsa);
279 }
280
281 static int dsa_security_bits(const EVP_PKEY *pkey)
282 {
283     return DSA_security_bits(pkey->pkey.dsa);
284 }
285
286 static int dsa_missing_parameters(const EVP_PKEY *pkey)
287 {
288     DSA *dsa;
289     dsa = pkey->pkey.dsa;
290     return dsa == NULL
291         || dsa->params.p == NULL
292         || dsa->params.q == NULL
293         || dsa->params.g == NULL;
294 }
295
296 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
297 {
298     if (to->pkey.dsa == NULL) {
299         to->pkey.dsa = DSA_new();
300         if (to->pkey.dsa == NULL)
301             return 0;
302     }
303     if (!ffc_params_copy(&to->pkey.dsa->params, &from->pkey.dsa->params))
304         return 0;
305
306     to->pkey.dsa->dirty_cnt++;
307     return 1;
308 }
309
310 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
311 {
312     return ffc_params_cmp(&a->pkey.dsa->params, &b->pkey.dsa->params, 1);
313 }
314
315 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
316 {
317     return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
318 }
319
320 static void int_dsa_free(EVP_PKEY *pkey)
321 {
322     DSA_free(pkey->pkey.dsa);
323 }
324
325 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
326 {
327     int ret = 0;
328     const char *ktype = NULL;
329     const BIGNUM *priv_key, *pub_key;
330     int mod_len = 0;
331
332     if (x->params.p != NULL)
333         mod_len = DSA_bits(x);
334
335     if (ptype == 2)
336         priv_key = x->priv_key;
337     else
338         priv_key = NULL;
339
340     if (ptype > 0)
341         pub_key = x->pub_key;
342     else
343         pub_key = NULL;
344
345     if (ptype == 2)
346         ktype = "Private-Key";
347     else if (ptype == 1)
348         ktype = "Public-Key";
349     else
350         ktype = "DSA-Parameters";
351
352     if (priv_key != NULL) {
353         if (!BIO_indent(bp, off, 128))
354             goto err;
355         if (BIO_printf(bp, "%s: (%d bit)\n", ktype, mod_len) <= 0)
356             goto err;
357     } else {
358         if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
359             goto err;
360     }
361
362     if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
363         goto err;
364     if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
365         goto err;
366     if (!ffc_params_print(bp, &x->params, off))
367         goto err;
368     ret = 1;
369  err:
370     return ret;
371 }
372
373 static int dsa_param_decode(EVP_PKEY *pkey,
374                             const unsigned char **pder, int derlen)
375 {
376     DSA *dsa;
377
378     if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) {
379         DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
380         return 0;
381     }
382     dsa->dirty_cnt++;
383     EVP_PKEY_assign_DSA(pkey, dsa);
384     return 1;
385 }
386
387 static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
388 {
389     return i2d_DSAparams(pkey->pkey.dsa, pder);
390 }
391
392 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
393                            ASN1_PCTX *ctx)
394 {
395     return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
396 }
397
398 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
399                          ASN1_PCTX *ctx)
400 {
401     return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
402 }
403
404 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
405                           ASN1_PCTX *ctx)
406 {
407     return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
408 }
409
410 static int old_dsa_priv_decode(EVP_PKEY *pkey,
411                                const unsigned char **pder, int derlen)
412 {
413     DSA *dsa;
414
415     if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
416         DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
417         return 0;
418     }
419     dsa->dirty_cnt++;
420     EVP_PKEY_assign_DSA(pkey, dsa);
421     return 1;
422 }
423
424 static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
425 {
426     return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
427 }
428
429 static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
430                          const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
431 {
432     DSA_SIG *dsa_sig;
433     const unsigned char *p;
434
435     if (sig == NULL) {
436         if (BIO_puts(bp, "\n") <= 0)
437             return 0;
438         else
439             return 1;
440     }
441     p = sig->data;
442     dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
443     if (dsa_sig != NULL) {
444         int rv = 0;
445         const BIGNUM *r, *s;
446
447         DSA_SIG_get0(dsa_sig, &r, &s);
448
449         if (BIO_write(bp, "\n", 1) != 1)
450             goto err;
451
452         if (!ASN1_bn_print(bp, "r:   ", r, NULL, indent))
453             goto err;
454         if (!ASN1_bn_print(bp, "s:   ", s, NULL, indent))
455             goto err;
456         rv = 1;
457  err:
458         DSA_SIG_free(dsa_sig);
459         return rv;
460     }
461     if (BIO_puts(bp, "\n") <= 0)
462         return 0;
463     return X509_signature_dump(bp, sig, indent);
464 }
465
466 static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
467 {
468     switch (op) {
469     case ASN1_PKEY_CTRL_PKCS7_SIGN:
470         if (arg1 == 0) {
471             int snid, hnid;
472             X509_ALGOR *alg1, *alg2;
473             PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
474             if (alg1 == NULL || alg1->algorithm == NULL)
475                 return -1;
476             hnid = OBJ_obj2nid(alg1->algorithm);
477             if (hnid == NID_undef)
478                 return -1;
479             if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
480                 return -1;
481             X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
482         }
483         return 1;
484 #ifndef OPENSSL_NO_CMS
485     case ASN1_PKEY_CTRL_CMS_SIGN:
486         if (arg1 == 0) {
487             int snid, hnid;
488             X509_ALGOR *alg1, *alg2;
489             CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
490             if (alg1 == NULL || alg1->algorithm == NULL)
491                 return -1;
492             hnid = OBJ_obj2nid(alg1->algorithm);
493             if (hnid == NID_undef)
494                 return -1;
495             if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
496                 return -1;
497             X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
498         }
499         return 1;
500
501     case ASN1_PKEY_CTRL_CMS_RI_TYPE:
502         *(int *)arg2 = CMS_RECIPINFO_NONE;
503         return 1;
504 #endif
505
506     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
507         *(int *)arg2 = NID_sha256;
508         return 1;
509
510     default:
511         return -2;
512
513     }
514
515 }
516
517 static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
518 {
519     return pkey->pkey.dsa->dirty_cnt;
520 }
521
522 static int dsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
523                               EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
524                               const char *propq)
525 {
526     DSA *dsa = from->pkey.dsa;
527     OSSL_PARAM_BLD *tmpl;
528     const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa);
529     const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa);
530     const BIGNUM *priv_key = DSA_get0_priv_key(dsa);
531     OSSL_PARAM *params;
532     int selection = 0;
533     int rv = 0;
534
535     /*
536      * If the DSA method is foreign, then we can't be sure of anything, and
537      * can therefore not export or pretend to export.
538      */
539     if (DSA_get_method(dsa) != DSA_OpenSSL())
540         return 0;
541
542     if (p == NULL || q == NULL || g == NULL)
543         return 0;
544
545     tmpl = OSSL_PARAM_BLD_new();
546     if (tmpl == NULL)
547         return 0;
548
549     if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
550         || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q)
551         || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g))
552         goto err;
553     selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
554     if (pub_key != NULL) {
555         if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
556                                     pub_key))
557             goto err;
558         selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
559     }
560     if (priv_key != NULL) {
561         if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
562                                     priv_key))
563             goto err;
564         selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
565     }
566
567     if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
568         goto err;
569
570     /* We export, the provider imports */
571     rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
572
573     OSSL_PARAM_BLD_free_params(params);
574 err:
575     OSSL_PARAM_BLD_free(tmpl);
576     return rv;
577 }
578
579 static int dsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
580 {
581     EVP_PKEY_CTX *pctx = vpctx;
582     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
583     DSA *dsa = dsa_new_with_ctx(pctx->libctx);
584
585     if (dsa == NULL) {
586         ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
587         return 0;
588     }
589
590     if (!dsa_ffc_params_fromdata(dsa, params)
591         || !dsa_key_fromdata(dsa, params)
592         || !EVP_PKEY_assign_DSA(pkey, dsa)) {
593         DSA_free(dsa);
594         return 0;
595     }
596     return 1;
597 }
598
599 /* NB these are sorted in pkey_id order, lowest first */
600
601 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {
602
603     {
604      EVP_PKEY_DSA2,
605      EVP_PKEY_DSA,
606      ASN1_PKEY_ALIAS},
607
608     {
609      EVP_PKEY_DSA1,
610      EVP_PKEY_DSA,
611      ASN1_PKEY_ALIAS},
612
613     {
614      EVP_PKEY_DSA4,
615      EVP_PKEY_DSA,
616      ASN1_PKEY_ALIAS},
617
618     {
619      EVP_PKEY_DSA3,
620      EVP_PKEY_DSA,
621      ASN1_PKEY_ALIAS},
622
623     {
624      EVP_PKEY_DSA,
625      EVP_PKEY_DSA,
626      0,
627
628      "DSA",
629      "OpenSSL DSA method",
630
631      dsa_pub_decode,
632      dsa_pub_encode,
633      dsa_pub_cmp,
634      dsa_pub_print,
635
636      dsa_priv_decode,
637      dsa_priv_encode,
638      dsa_priv_print,
639
640      int_dsa_size,
641      dsa_bits,
642      dsa_security_bits,
643
644      dsa_param_decode,
645      dsa_param_encode,
646      dsa_missing_parameters,
647      dsa_copy_parameters,
648      dsa_cmp_parameters,
649      dsa_param_print,
650      dsa_sig_print,
651
652      int_dsa_free,
653      dsa_pkey_ctrl,
654      old_dsa_priv_decode,
655      old_dsa_priv_encode,
656
657      NULL, NULL, NULL,
658      NULL, NULL, NULL,
659      NULL, NULL, NULL, NULL,
660
661      dsa_pkey_dirty_cnt,
662      dsa_pkey_export_to,
663      dsa_pkey_import_from
664     }
665 };