60ef9812e191cd2df5033a55d88aeafa2fddd9d2
[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_DEFAULT_MD_NID:
468         *(int *)arg2 = NID_sha256;
469         return 1;
470
471     default:
472         return -2;
473     }
474 }
475
476 static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
477 {
478     return pkey->pkey.dsa->dirty_cnt;
479 }
480
481 static int dsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
482                               EVP_KEYMGMT *to_keymgmt, OSSL_LIB_CTX *libctx,
483                               const char *propq)
484 {
485     DSA *dsa = from->pkey.dsa;
486     OSSL_PARAM_BLD *tmpl;
487     const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa);
488     const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa);
489     const BIGNUM *priv_key = DSA_get0_priv_key(dsa);
490     OSSL_PARAM *params;
491     int selection = 0;
492     int rv = 0;
493
494     /*
495      * If the DSA method is foreign, then we can't be sure of anything, and
496      * can therefore not export or pretend to export.
497      */
498     if (DSA_get_method(dsa) != DSA_OpenSSL())
499         return 0;
500
501     if (p == NULL || q == NULL || g == NULL)
502         return 0;
503
504     tmpl = OSSL_PARAM_BLD_new();
505     if (tmpl == NULL)
506         return 0;
507
508     if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
509         || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q)
510         || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g))
511         goto err;
512     selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
513     if (pub_key != NULL) {
514         if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
515                                     pub_key))
516             goto err;
517         selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
518     }
519     if (priv_key != NULL) {
520         if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
521                                     priv_key))
522             goto err;
523         selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
524     }
525
526     if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
527         goto err;
528
529     /* We export, the provider imports */
530     rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
531
532     OSSL_PARAM_BLD_free_params(params);
533 err:
534     OSSL_PARAM_BLD_free(tmpl);
535     return rv;
536 }
537
538 static int dsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
539 {
540     EVP_PKEY_CTX *pctx = vpctx;
541     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
542     DSA *dsa = dsa_new_with_ctx(pctx->libctx);
543
544     if (dsa == NULL) {
545         ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
546         return 0;
547     }
548
549     if (!dsa_ffc_params_fromdata(dsa, params)
550         || !dsa_key_fromdata(dsa, params)
551         || !EVP_PKEY_assign_DSA(pkey, dsa)) {
552         DSA_free(dsa);
553         return 0;
554     }
555     return 1;
556 }
557
558 /* NB these are sorted in pkey_id order, lowest first */
559
560 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {
561
562     {
563      EVP_PKEY_DSA2,
564      EVP_PKEY_DSA,
565      ASN1_PKEY_ALIAS},
566
567     {
568      EVP_PKEY_DSA1,
569      EVP_PKEY_DSA,
570      ASN1_PKEY_ALIAS},
571
572     {
573      EVP_PKEY_DSA4,
574      EVP_PKEY_DSA,
575      ASN1_PKEY_ALIAS},
576
577     {
578      EVP_PKEY_DSA3,
579      EVP_PKEY_DSA,
580      ASN1_PKEY_ALIAS},
581
582     {
583      EVP_PKEY_DSA,
584      EVP_PKEY_DSA,
585      0,
586
587      "DSA",
588      "OpenSSL DSA method",
589
590      dsa_pub_decode,
591      dsa_pub_encode,
592      dsa_pub_cmp,
593      dsa_pub_print,
594
595      dsa_priv_decode,
596      dsa_priv_encode,
597      dsa_priv_print,
598
599      int_dsa_size,
600      dsa_bits,
601      dsa_security_bits,
602
603      dsa_param_decode,
604      dsa_param_encode,
605      dsa_missing_parameters,
606      dsa_copy_parameters,
607      dsa_cmp_parameters,
608      dsa_param_print,
609      dsa_sig_print,
610
611      int_dsa_free,
612      dsa_pkey_ctrl,
613      old_dsa_priv_decode,
614      old_dsa_priv_encode,
615
616      NULL, NULL, NULL,
617      NULL, NULL, NULL,
618      NULL, NULL, NULL, NULL,
619
620      dsa_pkey_dirty_cnt,
621      dsa_pkey_export_to,
622      dsa_pkey_import_from
623     }
624 };