xref: /openssl/crypto/dsa/dsa_ameth.c (revision 7ed6de99)
1 /*
2  * Copyright 2006-2024 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 
dsa_pub_decode(EVP_PKEY * pkey,const X509_PUBKEY * pubkey)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_DSA_LIB);
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 
dsa_pub_encode(X509_PUBKEY * pk,const EVP_PKEY * pkey)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_ASN1_LIB);
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_ASN1_LIB);
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_ASN1_LIB);
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_ASN1_LIB);
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 
dsa_priv_decode(EVP_PKEY * pkey,const PKCS8_PRIV_KEY_INFO * p8)150 static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
151 {
152     int ret = 0;
153     DSA *dsa = ossl_dsa_key_from_pkcs8(p8, NULL, NULL);
154 
155     if (dsa != NULL) {
156         ret = 1;
157         EVP_PKEY_assign_DSA(pkey, dsa);
158     }
159 
160     return ret;
161 }
162 
dsa_priv_encode(PKCS8_PRIV_KEY_INFO * p8,const EVP_PKEY * pkey)163 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
164 {
165     ASN1_STRING *params = NULL;
166     ASN1_INTEGER *prkey = NULL;
167     unsigned char *dp = NULL;
168     int dplen;
169 
170     if (pkey->pkey.dsa  == NULL|| pkey->pkey.dsa->priv_key == NULL) {
171         ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PARAMETERS);
172         goto err;
173     }
174 
175     params = ASN1_STRING_new();
176 
177     if (params == NULL) {
178         ERR_raise(ERR_LIB_DSA, ERR_R_ASN1_LIB);
179         goto err;
180     }
181 
182     params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
183     if (params->length <= 0) {
184         ERR_raise(ERR_LIB_DSA, ERR_R_ASN1_LIB);
185         goto err;
186     }
187     params->type = V_ASN1_SEQUENCE;
188 
189     /* Get private key into integer */
190     prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
191 
192     if (prkey == NULL) {
193         ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
194         goto err;
195     }
196 
197     dplen = i2d_ASN1_INTEGER(prkey, &dp);
198 
199     ASN1_STRING_clear_free(prkey);
200 
201     if (dplen <= 0) {
202         ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
203         goto err;
204     }
205 
206     if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
207                          V_ASN1_SEQUENCE, params, dp, dplen)) {
208         OPENSSL_clear_free(dp, dplen);
209         goto err;
210     }
211     return 1;
212 
213  err:
214     ASN1_STRING_free(params);
215     return 0;
216 }
217 
int_dsa_size(const EVP_PKEY * pkey)218 static int int_dsa_size(const EVP_PKEY *pkey)
219 {
220     return DSA_size(pkey->pkey.dsa);
221 }
222 
dsa_bits(const EVP_PKEY * pkey)223 static int dsa_bits(const EVP_PKEY *pkey)
224 {
225     return DSA_bits(pkey->pkey.dsa);
226 }
227 
dsa_security_bits(const EVP_PKEY * pkey)228 static int dsa_security_bits(const EVP_PKEY *pkey)
229 {
230     return DSA_security_bits(pkey->pkey.dsa);
231 }
232 
dsa_missing_parameters(const EVP_PKEY * pkey)233 static int dsa_missing_parameters(const EVP_PKEY *pkey)
234 {
235     DSA *dsa;
236     dsa = pkey->pkey.dsa;
237     return dsa == NULL
238         || dsa->params.p == NULL
239         || dsa->params.q == NULL
240         || dsa->params.g == NULL;
241 }
242 
dsa_copy_parameters(EVP_PKEY * to,const EVP_PKEY * from)243 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
244 {
245     if (to->pkey.dsa == NULL) {
246         to->pkey.dsa = DSA_new();
247         if (to->pkey.dsa == NULL)
248             return 0;
249     }
250     if (!ossl_ffc_params_copy(&to->pkey.dsa->params, &from->pkey.dsa->params))
251         return 0;
252 
253     to->pkey.dsa->dirty_cnt++;
254     return 1;
255 }
256 
dsa_cmp_parameters(const EVP_PKEY * a,const EVP_PKEY * b)257 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
258 {
259     return ossl_ffc_params_cmp(&a->pkey.dsa->params, &b->pkey.dsa->params, 1);
260 }
261 
dsa_pub_cmp(const EVP_PKEY * a,const EVP_PKEY * b)262 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
263 {
264     return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
265 }
266 
int_dsa_free(EVP_PKEY * pkey)267 static void int_dsa_free(EVP_PKEY *pkey)
268 {
269     DSA_free(pkey->pkey.dsa);
270 }
271 
do_dsa_print(BIO * bp,const DSA * x,int off,int ptype)272 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
273 {
274     int ret = 0;
275     const char *ktype = NULL;
276     const BIGNUM *priv_key, *pub_key;
277     int mod_len = 0;
278 
279     if (x->params.p != NULL)
280         mod_len = DSA_bits(x);
281 
282     if (ptype == 2)
283         priv_key = x->priv_key;
284     else
285         priv_key = NULL;
286 
287     if (ptype > 0)
288         pub_key = x->pub_key;
289     else
290         pub_key = NULL;
291 
292     if (ptype == 2)
293         ktype = "Private-Key";
294     else if (ptype == 1)
295         ktype = "Public-Key";
296     else
297         ktype = "DSA-Parameters";
298 
299     if (priv_key != NULL) {
300         if (!BIO_indent(bp, off, 128))
301             goto err;
302         if (BIO_printf(bp, "%s: (%d bit)\n", ktype, mod_len) <= 0)
303             goto err;
304     } else {
305         if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
306             goto err;
307     }
308 
309     if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
310         goto err;
311     if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
312         goto err;
313     if (!ossl_ffc_params_print(bp, &x->params, off))
314         goto err;
315     ret = 1;
316  err:
317     return ret;
318 }
319 
dsa_param_decode(EVP_PKEY * pkey,const unsigned char ** pder,int derlen)320 static int dsa_param_decode(EVP_PKEY *pkey,
321                             const unsigned char **pder, int derlen)
322 {
323     DSA *dsa;
324 
325     if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL)
326         return 0;
327 
328     dsa->dirty_cnt++;
329     EVP_PKEY_assign_DSA(pkey, dsa);
330     return 1;
331 }
332 
dsa_param_encode(const EVP_PKEY * pkey,unsigned char ** pder)333 static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
334 {
335     return i2d_DSAparams(pkey->pkey.dsa, pder);
336 }
337 
dsa_param_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)338 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
339                            ASN1_PCTX *ctx)
340 {
341     return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
342 }
343 
dsa_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)344 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
345                          ASN1_PCTX *ctx)
346 {
347     return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
348 }
349 
dsa_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)350 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
351                           ASN1_PCTX *ctx)
352 {
353     return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
354 }
355 
old_dsa_priv_decode(EVP_PKEY * pkey,const unsigned char ** pder,int derlen)356 static int old_dsa_priv_decode(EVP_PKEY *pkey,
357                                const unsigned char **pder, int derlen)
358 {
359     DSA *dsa;
360 
361     if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
362         ERR_raise(ERR_LIB_DSA, ERR_R_DSA_LIB);
363         return 0;
364     }
365     dsa->dirty_cnt++;
366     EVP_PKEY_assign_DSA(pkey, dsa);
367     return 1;
368 }
369 
old_dsa_priv_encode(const EVP_PKEY * pkey,unsigned char ** pder)370 static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
371 {
372     return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
373 }
374 
dsa_sig_print(BIO * bp,const X509_ALGOR * sigalg,const ASN1_STRING * sig,int indent,ASN1_PCTX * pctx)375 static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
376                          const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
377 {
378     DSA_SIG *dsa_sig;
379     const unsigned char *p;
380 
381     if (sig == NULL) {
382         if (BIO_puts(bp, "\n") <= 0)
383             return 0;
384         else
385             return 1;
386     }
387     p = sig->data;
388     dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
389     if (dsa_sig != NULL) {
390         int rv = 0;
391         const BIGNUM *r, *s;
392 
393         DSA_SIG_get0(dsa_sig, &r, &s);
394 
395         if (BIO_write(bp, "\n", 1) != 1)
396             goto err;
397 
398         if (!ASN1_bn_print(bp, "r:   ", r, NULL, indent))
399             goto err;
400         if (!ASN1_bn_print(bp, "s:   ", s, NULL, indent))
401             goto err;
402         rv = 1;
403  err:
404         DSA_SIG_free(dsa_sig);
405         return rv;
406     }
407     if (BIO_puts(bp, "\n") <= 0)
408         return 0;
409     return X509_signature_dump(bp, sig, indent);
410 }
411 
dsa_pkey_ctrl(EVP_PKEY * pkey,int op,long arg1,void * arg2)412 static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
413 {
414     switch (op) {
415     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
416         *(int *)arg2 = NID_sha256;
417         return 1;
418 
419     default:
420         return -2;
421     }
422 }
423 
dsa_pkey_dirty_cnt(const EVP_PKEY * pkey)424 static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
425 {
426     return pkey->pkey.dsa->dirty_cnt;
427 }
428 
dsa_pkey_export_to(const EVP_PKEY * from,void * to_keydata,OSSL_FUNC_keymgmt_import_fn * importer,OSSL_LIB_CTX * libctx,const char * propq)429 static int dsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
430                               OSSL_FUNC_keymgmt_import_fn *importer,
431                               OSSL_LIB_CTX *libctx, const char *propq)
432 {
433     DSA *dsa = from->pkey.dsa;
434     OSSL_PARAM_BLD *tmpl;
435     const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa);
436     const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa);
437     const BIGNUM *priv_key = DSA_get0_priv_key(dsa);
438     OSSL_PARAM *params;
439     int selection = 0;
440     int rv = 0;
441 
442     if (p == NULL || q == NULL || g == NULL)
443         return 0;
444 
445     tmpl = OSSL_PARAM_BLD_new();
446     if (tmpl == NULL)
447         return 0;
448 
449     if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
450         || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q)
451         || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g))
452         goto err;
453     selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
454     if (pub_key != NULL) {
455         if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
456                                     pub_key))
457             goto err;
458         selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
459     }
460     if (priv_key != NULL) {
461         if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
462                                     priv_key))
463             goto err;
464         selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
465     }
466 
467     if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
468         goto err;
469 
470     /* We export, the provider imports */
471     rv = importer(to_keydata, selection, params);
472 
473     OSSL_PARAM_free(params);
474  err:
475     OSSL_PARAM_BLD_free(tmpl);
476     return rv;
477 }
478 
dsa_pkey_import_from(const OSSL_PARAM params[],void * vpctx)479 static int dsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
480 {
481     EVP_PKEY_CTX *pctx = vpctx;
482     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
483     DSA *dsa = ossl_dsa_new(pctx->libctx);
484 
485     if (dsa == NULL) {
486         ERR_raise(ERR_LIB_DSA, ERR_R_DSA_LIB);
487         return 0;
488     }
489 
490     if (!ossl_dsa_ffc_params_fromdata(dsa, params)
491         || !ossl_dsa_key_fromdata(dsa, params, 1)
492         || !EVP_PKEY_assign_DSA(pkey, dsa)) {
493         DSA_free(dsa);
494         return 0;
495     }
496     return 1;
497 }
498 
dsa_pkey_copy(EVP_PKEY * to,EVP_PKEY * from)499 static int dsa_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
500 {
501     DSA *dsa = from->pkey.dsa;
502     DSA *dupkey = NULL;
503     int ret;
504 
505     if (dsa != NULL) {
506         dupkey = ossl_dsa_dup(dsa, OSSL_KEYMGMT_SELECT_ALL);
507         if (dupkey == NULL)
508             return 0;
509     }
510 
511     ret = EVP_PKEY_assign_DSA(to, dupkey);
512     if (!ret)
513         DSA_free(dupkey);
514     return ret;
515 }
516 
517 /* NB these are sorted in pkey_id order, lowest first */
518 
519 const EVP_PKEY_ASN1_METHOD ossl_dsa_asn1_meths[4] = {
520 
521     /* This aliases NID_dsa with NID_dsa_2 */
522     {
523      EVP_PKEY_DSA1,
524      EVP_PKEY_DSA,
525      ASN1_PKEY_ALIAS},
526 
527     /* This aliases NID_dsaWithSHA with NID_dsaWithSHA_2 */
528     {
529      EVP_PKEY_DSA4,
530      EVP_PKEY_DSA2,
531      ASN1_PKEY_ALIAS},
532 
533     /* This aliases NID_dsaWithSHA with NID_dsaWithSHA1 */
534     {
535      EVP_PKEY_DSA3,
536      EVP_PKEY_DSA2,
537      ASN1_PKEY_ALIAS},
538 
539     {
540      EVP_PKEY_DSA,
541      EVP_PKEY_DSA,
542      0,
543 
544      "DSA",
545      "OpenSSL DSA method",
546 
547      dsa_pub_decode,
548      dsa_pub_encode,
549      dsa_pub_cmp,
550      dsa_pub_print,
551 
552      dsa_priv_decode,
553      dsa_priv_encode,
554      dsa_priv_print,
555 
556      int_dsa_size,
557      dsa_bits,
558      dsa_security_bits,
559 
560      dsa_param_decode,
561      dsa_param_encode,
562      dsa_missing_parameters,
563      dsa_copy_parameters,
564      dsa_cmp_parameters,
565      dsa_param_print,
566      dsa_sig_print,
567 
568      int_dsa_free,
569      dsa_pkey_ctrl,
570      old_dsa_priv_decode,
571      old_dsa_priv_encode,
572 
573      NULL, NULL, NULL,
574      NULL, NULL, NULL,
575      NULL, NULL, NULL, NULL,
576 
577      dsa_pkey_dirty_cnt,
578      dsa_pkey_export_to,
579      dsa_pkey_import_from,
580      dsa_pkey_copy
581     }
582 };
583