PROV: Add type specific PKCS#8 decoding to the DER->key decoders
[openssl.git] / crypto / dsa / dsa_backend.c
1 /*
2  * Copyright 2020-2021 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 <openssl/core_names.h>
17 #include <openssl/err.h>
18 #include "crypto/dsa.h"
19
20 /*
21  * The intention with the "backend" source file is to offer backend support
22  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
23  * implementations alike.
24  */
25
26 int ossl_dsa_key_fromdata(DSA *dsa, const OSSL_PARAM params[])
27 {
28     const OSSL_PARAM *param_priv_key, *param_pub_key;
29     BIGNUM *priv_key = NULL, *pub_key = NULL;
30
31     if (dsa == NULL)
32         return 0;
33
34     param_priv_key =
35         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
36     param_pub_key =
37         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
38
39     /* It's ok if neither half is present */
40     if (param_priv_key == NULL && param_pub_key == NULL)
41         return 1;
42
43     if (param_pub_key != NULL && !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
44         goto err;
45     if (param_priv_key != NULL && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
46         goto err;
47
48     if (!DSA_set0_key(dsa, pub_key, priv_key))
49         goto err;
50
51     return 1;
52
53  err:
54     BN_clear_free(priv_key);
55     BN_free(pub_key);
56     return 0;
57 }
58
59 #ifndef FIPS_MODULE
60 DSA *ossl_dsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
61                              OSSL_LIB_CTX *libctx, const char *propq)
62 {
63     const unsigned char *p, *pm;
64     int pklen, pmlen;
65     int ptype;
66     const void *pval;
67     const ASN1_STRING *pstr;
68     const X509_ALGOR *palg;
69     ASN1_INTEGER *privkey = NULL;
70     const BIGNUM *dsa_p, *dsa_g;
71     BIGNUM *dsa_pubkey = NULL, *dsa_privkey = NULL;
72     BN_CTX *ctx = NULL;
73
74     DSA *dsa = NULL;
75
76     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
77         return 0;
78     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
79
80     if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
81         goto decerr;
82     if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
83         goto decerr;
84
85     pstr = pval;
86     pm = pstr->data;
87     pmlen = pstr->length;
88     if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
89         goto decerr;
90     /* We have parameters now set private key */
91     if ((dsa_privkey = BN_secure_new()) == NULL
92         || !ASN1_INTEGER_to_BN(privkey, dsa_privkey)) {
93         ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
94         goto dsaerr;
95     }
96     /* Calculate public key */
97     if ((dsa_pubkey = BN_new()) == NULL) {
98         ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
99         goto dsaerr;
100     }
101     if ((ctx = BN_CTX_new()) == NULL) {
102         ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
103         goto dsaerr;
104     }
105
106     dsa_p = DSA_get0_p(dsa);
107     dsa_g = DSA_get0_g(dsa);
108     BN_set_flags(dsa_privkey, BN_FLG_CONSTTIME);
109     if (!BN_mod_exp(dsa_pubkey, dsa_g, dsa_privkey, dsa_p, ctx)) {
110         ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
111         goto dsaerr;
112     }
113     DSA_set0_key(dsa, dsa_pubkey, dsa_privkey);
114
115     goto done;
116
117  decerr:
118     ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR);
119  dsaerr:
120     BN_free(dsa_privkey);
121     BN_free(dsa_pubkey);
122     DSA_free(dsa);
123     dsa = NULL;
124  done:
125     BN_CTX_free(ctx);
126     ASN1_STRING_clear_free(privkey);
127     return dsa;
128 }
129 #endif