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