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  * DH 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/err.h>
17 #include <openssl/core_names.h>
18 #ifndef FIPS_MODULE
19 # include <openssl/x509.h>
20 #endif
21 #include "internal/param_build_set.h"
22 #include "crypto/dh.h"
23 #include "dh_local.h"
24 
25 /*
26  * The intention with the "backend" source file is to offer backend functions
27  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
28  * implementations alike.
29  */
30 
dh_ffc_params_fromdata(DH * dh,const OSSL_PARAM params[])31 static int dh_ffc_params_fromdata(DH *dh, const OSSL_PARAM params[])
32 {
33     int ret;
34     FFC_PARAMS *ffc;
35 
36     if (dh == NULL)
37         return 0;
38     ffc = ossl_dh_get0_params(dh);
39     if (ffc == NULL)
40         return 0;
41 
42     ret = ossl_ffc_params_fromdata(ffc, params);
43     if (ret)
44         ossl_dh_cache_named_group(dh); /* This increments dh->dirty_cnt */
45     return ret;
46 }
47 
ossl_dh_params_fromdata(DH * dh,const OSSL_PARAM params[])48 int ossl_dh_params_fromdata(DH *dh, const OSSL_PARAM params[])
49 {
50     const OSSL_PARAM *param_priv_len;
51     long priv_len;
52 
53     if (!dh_ffc_params_fromdata(dh, params))
54         return 0;
55 
56     param_priv_len =
57         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
58     if (param_priv_len != NULL
59         && (!OSSL_PARAM_get_long(param_priv_len, &priv_len)
60             || !DH_set_length(dh, priv_len)))
61         return 0;
62 
63     return 1;
64 }
65 
ossl_dh_key_fromdata(DH * dh,const OSSL_PARAM params[])66 int ossl_dh_key_fromdata(DH *dh, const OSSL_PARAM params[])
67 {
68     const OSSL_PARAM *param_priv_key, *param_pub_key;
69     BIGNUM *priv_key = NULL, *pub_key = NULL;
70 
71     if (dh == NULL)
72         return 0;
73 
74     param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
75     param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
76 
77     if ((param_priv_key != NULL
78          && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
79         || (param_pub_key != NULL
80             && !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
81         goto err;
82 
83     if (!DH_set0_key(dh, pub_key, priv_key))
84         goto err;
85 
86     return 1;
87 
88  err:
89     BN_clear_free(priv_key);
90     BN_free(pub_key);
91     return 0;
92 }
93 
ossl_dh_params_todata(DH * dh,OSSL_PARAM_BLD * bld,OSSL_PARAM params[])94 int ossl_dh_params_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
95 {
96     long l = DH_get_length(dh);
97 
98     if (!ossl_ffc_params_todata(ossl_dh_get0_params(dh), bld, params))
99         return 0;
100     if (l > 0
101         && !ossl_param_build_set_long(bld, params, OSSL_PKEY_PARAM_DH_PRIV_LEN, l))
102         return 0;
103     return 1;
104 }
105 
ossl_dh_key_todata(DH * dh,OSSL_PARAM_BLD * bld,OSSL_PARAM params[])106 int ossl_dh_key_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
107 {
108     const BIGNUM *priv = NULL, *pub = NULL;
109 
110     if (dh == NULL)
111         return 0;
112 
113     DH_get0_key(dh, &pub, &priv);
114     if (priv != NULL
115         && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv))
116         return 0;
117     if (pub != NULL
118         && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub))
119         return 0;
120 
121     return 1;
122 }
123 
ossl_dh_is_foreign(const DH * dh)124 int ossl_dh_is_foreign(const DH *dh)
125 {
126 #ifndef FIPS_MODULE
127     if (dh->engine != NULL || ossl_dh_get_method(dh) != DH_OpenSSL())
128         return 1;
129 #endif
130     return 0;
131 }
132 
dh_bn_dup_check(BIGNUM ** out,const BIGNUM * f)133 static ossl_inline int dh_bn_dup_check(BIGNUM **out, const BIGNUM *f)
134 {
135     if (f != NULL && (*out = BN_dup(f)) == NULL)
136         return 0;
137     return 1;
138 }
139 
ossl_dh_dup(const DH * dh,int selection)140 DH *ossl_dh_dup(const DH *dh, int selection)
141 {
142     DH *dupkey = NULL;
143 
144     /* Do not try to duplicate foreign DH keys */
145     if (ossl_dh_is_foreign(dh))
146         return NULL;
147 
148     if ((dupkey = ossl_dh_new_ex(dh->libctx)) == NULL)
149         return NULL;
150 
151     dupkey->length = DH_get_length(dh);
152     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0
153         && !ossl_ffc_params_copy(&dupkey->params, &dh->params))
154         goto err;
155 
156     dupkey->flags = dh->flags;
157 
158     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
159         && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
160             || !dh_bn_dup_check(&dupkey->pub_key, dh->pub_key)))
161         goto err;
162 
163     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
164         && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
165             || !dh_bn_dup_check(&dupkey->priv_key, dh->priv_key)))
166         goto err;
167 
168 #ifndef FIPS_MODULE
169     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_DH,
170                             &dupkey->ex_data, &dh->ex_data))
171         goto err;
172 #endif
173 
174     return dupkey;
175 
176  err:
177     DH_free(dupkey);
178     return NULL;
179 }
180 
181 #ifndef FIPS_MODULE
ossl_dh_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO * p8inf,OSSL_LIB_CTX * libctx,const char * propq)182 DH *ossl_dh_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
183                            OSSL_LIB_CTX *libctx, const char *propq)
184 {
185     const unsigned char *p, *pm;
186     int pklen, pmlen;
187     int ptype;
188     const void *pval;
189     const ASN1_STRING *pstr;
190     const X509_ALGOR *palg;
191     BIGNUM *privkey_bn = NULL;
192     ASN1_INTEGER *privkey = NULL;
193     DH *dh = NULL;
194 
195     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
196         return 0;
197 
198     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
199 
200     if (ptype != V_ASN1_SEQUENCE)
201         goto decerr;
202     if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
203         goto decerr;
204 
205     pstr = pval;
206     pm = pstr->data;
207     pmlen = pstr->length;
208     switch (OBJ_obj2nid(palg->algorithm)) {
209     case NID_dhKeyAgreement:
210         dh = d2i_DHparams(NULL, &pm, pmlen);
211         break;
212     case NID_dhpublicnumber:
213         dh = d2i_DHxparams(NULL, &pm, pmlen);
214         break;
215     default:
216         goto decerr;
217     }
218     if (dh == NULL)
219         goto decerr;
220 
221     /* We have parameters now set private key */
222     if ((privkey_bn = BN_secure_new()) == NULL
223         || !ASN1_INTEGER_to_BN(privkey, privkey_bn)) {
224         ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR);
225         BN_clear_free(privkey_bn);
226         goto dherr;
227     }
228     if (!DH_set0_key(dh, NULL, privkey_bn))
229         goto dherr;
230     /* Calculate public key, increments dirty_cnt */
231     if (!DH_generate_key(dh))
232         goto dherr;
233 
234     goto done;
235 
236  decerr:
237     ERR_raise(ERR_LIB_DH, EVP_R_DECODE_ERROR);
238  dherr:
239     DH_free(dh);
240     dh = NULL;
241  done:
242     ASN1_STRING_clear_free(privkey);
243     return dh;
244 }
245 #endif
246