/src/openssl32/crypto/cms/cms_dh.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2006-2023 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 | | #include <assert.h> |
11 | | #include <openssl/cms.h> |
12 | | #include <openssl/dh.h> |
13 | | #include <openssl/err.h> |
14 | | #include <openssl/core_names.h> |
15 | | #include "internal/sizes.h" |
16 | | #include "crypto/asn1.h" |
17 | | #include "crypto/evp.h" |
18 | | #include "cms_local.h" |
19 | | |
20 | | static int dh_cms_set_peerkey(EVP_PKEY_CTX *pctx, |
21 | | X509_ALGOR *alg, ASN1_BIT_STRING *pubkey) |
22 | 0 | { |
23 | 0 | const ASN1_OBJECT *aoid; |
24 | 0 | int atype; |
25 | 0 | const void *aval; |
26 | 0 | ASN1_INTEGER *public_key = NULL; |
27 | 0 | int rv = 0; |
28 | 0 | EVP_PKEY *pkpeer = NULL, *pk = NULL; |
29 | 0 | BIGNUM *bnpub = NULL; |
30 | 0 | const unsigned char *p; |
31 | 0 | unsigned char *buf = NULL; |
32 | 0 | int plen; |
33 | |
|
34 | 0 | X509_ALGOR_get0(&aoid, &atype, &aval, alg); |
35 | 0 | if (OBJ_obj2nid(aoid) != NID_dhpublicnumber) |
36 | 0 | goto err; |
37 | | /* Only absent parameters allowed in RFC XXXX */ |
38 | 0 | if (atype != V_ASN1_UNDEF && atype != V_ASN1_NULL) |
39 | 0 | goto err; |
40 | | |
41 | 0 | pk = EVP_PKEY_CTX_get0_pkey(pctx); |
42 | 0 | if (pk == NULL || !EVP_PKEY_is_a(pk, "DHX")) |
43 | 0 | goto err; |
44 | | |
45 | | /* Get public key */ |
46 | 0 | plen = ASN1_STRING_length(pubkey); |
47 | 0 | p = ASN1_STRING_get0_data(pubkey); |
48 | 0 | if (p == NULL || plen == 0) |
49 | 0 | goto err; |
50 | | |
51 | 0 | if ((public_key = d2i_ASN1_INTEGER(NULL, &p, plen)) == NULL) |
52 | 0 | goto err; |
53 | | /* |
54 | | * Pad to full p parameter size as that is checked by |
55 | | * EVP_PKEY_set1_encoded_public_key() |
56 | | */ |
57 | 0 | plen = EVP_PKEY_get_size(pk); |
58 | 0 | if ((bnpub = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) |
59 | 0 | goto err; |
60 | 0 | if ((buf = OPENSSL_malloc(plen)) == NULL) |
61 | 0 | goto err; |
62 | 0 | if (BN_bn2binpad(bnpub, buf, plen) < 0) |
63 | 0 | goto err; |
64 | | |
65 | 0 | pkpeer = EVP_PKEY_new(); |
66 | 0 | if (pkpeer == NULL |
67 | 0 | || !EVP_PKEY_copy_parameters(pkpeer, pk) |
68 | 0 | || !EVP_PKEY_set1_encoded_public_key(pkpeer, buf, plen)) |
69 | 0 | goto err; |
70 | | |
71 | 0 | if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0) |
72 | 0 | rv = 1; |
73 | 0 | err: |
74 | 0 | ASN1_INTEGER_free(public_key); |
75 | 0 | BN_free(bnpub); |
76 | 0 | OPENSSL_free(buf); |
77 | 0 | EVP_PKEY_free(pkpeer); |
78 | 0 | return rv; |
79 | 0 | } |
80 | | |
81 | | static int dh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri) |
82 | 0 | { |
83 | 0 | int rv = 0; |
84 | 0 | X509_ALGOR *alg, *kekalg = NULL; |
85 | 0 | ASN1_OCTET_STRING *ukm; |
86 | 0 | const unsigned char *p; |
87 | 0 | unsigned char *dukm = NULL; |
88 | 0 | size_t dukmlen = 0; |
89 | 0 | int keylen, plen; |
90 | 0 | EVP_CIPHER *kekcipher = NULL; |
91 | 0 | EVP_CIPHER_CTX *kekctx; |
92 | 0 | char name[OSSL_MAX_NAME_SIZE]; |
93 | |
|
94 | 0 | if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm)) |
95 | 0 | goto err; |
96 | | |
97 | | /* |
98 | | * For DH we only have one OID permissible. If ever any more get defined |
99 | | * we will need something cleverer. |
100 | | */ |
101 | 0 | if (OBJ_obj2nid(alg->algorithm) != NID_id_smime_alg_ESDH) { |
102 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_KDF_PARAMETER_ERROR); |
103 | 0 | goto err; |
104 | 0 | } |
105 | | |
106 | 0 | if (EVP_PKEY_CTX_set_dh_kdf_type(pctx, EVP_PKEY_DH_KDF_X9_42) <= 0 |
107 | 0 | || EVP_PKEY_CTX_set_dh_kdf_md(pctx, EVP_sha1()) <= 0) |
108 | 0 | goto err; |
109 | | |
110 | 0 | if (alg->parameter->type != V_ASN1_SEQUENCE) |
111 | 0 | goto err; |
112 | | |
113 | 0 | p = alg->parameter->value.sequence->data; |
114 | 0 | plen = alg->parameter->value.sequence->length; |
115 | 0 | kekalg = d2i_X509_ALGOR(NULL, &p, plen); |
116 | 0 | if (kekalg == NULL) |
117 | 0 | goto err; |
118 | 0 | kekctx = CMS_RecipientInfo_kari_get0_ctx(ri); |
119 | 0 | if (kekctx == NULL) |
120 | 0 | goto err; |
121 | | |
122 | 0 | if (OBJ_obj2txt(name, sizeof(name), kekalg->algorithm, 0) <= 0) |
123 | 0 | goto err; |
124 | | |
125 | 0 | kekcipher = EVP_CIPHER_fetch(pctx->libctx, name, pctx->propquery); |
126 | 0 | if (kekcipher == NULL |
127 | 0 | || EVP_CIPHER_get_mode(kekcipher) != EVP_CIPH_WRAP_MODE) |
128 | 0 | goto err; |
129 | 0 | if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL)) |
130 | 0 | goto err; |
131 | 0 | if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0) |
132 | 0 | goto err; |
133 | | |
134 | 0 | keylen = EVP_CIPHER_CTX_get_key_length(kekctx); |
135 | 0 | if (EVP_PKEY_CTX_set_dh_kdf_outlen(pctx, keylen) <= 0) |
136 | 0 | goto err; |
137 | | /* Use OBJ_nid2obj to ensure we use built in OID that isn't freed */ |
138 | 0 | if (EVP_PKEY_CTX_set0_dh_kdf_oid(pctx, |
139 | 0 | OBJ_nid2obj(EVP_CIPHER_get_type(kekcipher))) |
140 | 0 | <= 0) |
141 | 0 | goto err; |
142 | | |
143 | 0 | if (ukm != NULL) { |
144 | 0 | dukmlen = ASN1_STRING_length(ukm); |
145 | 0 | dukm = OPENSSL_memdup(ASN1_STRING_get0_data(ukm), dukmlen); |
146 | 0 | if (dukm == NULL) |
147 | 0 | goto err; |
148 | 0 | } |
149 | | |
150 | 0 | if (EVP_PKEY_CTX_set0_dh_kdf_ukm(pctx, dukm, dukmlen) <= 0) |
151 | 0 | goto err; |
152 | 0 | dukm = NULL; |
153 | |
|
154 | 0 | rv = 1; |
155 | 0 | err: |
156 | 0 | X509_ALGOR_free(kekalg); |
157 | 0 | EVP_CIPHER_free(kekcipher); |
158 | 0 | OPENSSL_free(dukm); |
159 | 0 | return rv; |
160 | 0 | } |
161 | | |
162 | | static int dh_cms_decrypt(CMS_RecipientInfo *ri) |
163 | 0 | { |
164 | 0 | EVP_PKEY_CTX *pctx = CMS_RecipientInfo_get0_pkey_ctx(ri); |
165 | |
|
166 | 0 | if (pctx == NULL) |
167 | 0 | return 0; |
168 | | /* See if we need to set peer key */ |
169 | 0 | if (!EVP_PKEY_CTX_get0_peerkey(pctx)) { |
170 | 0 | X509_ALGOR *alg; |
171 | 0 | ASN1_BIT_STRING *pubkey; |
172 | |
|
173 | 0 | if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey, |
174 | 0 | NULL, NULL, NULL)) |
175 | 0 | return 0; |
176 | 0 | if (alg == NULL || pubkey == NULL) |
177 | 0 | return 0; |
178 | 0 | if (!dh_cms_set_peerkey(pctx, alg, pubkey)) { |
179 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_PEER_KEY_ERROR); |
180 | 0 | return 0; |
181 | 0 | } |
182 | 0 | } |
183 | | /* Set DH derivation parameters and initialise unwrap context */ |
184 | 0 | if (!dh_cms_set_shared_info(pctx, ri)) { |
185 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_SHARED_INFO_ERROR); |
186 | 0 | return 0; |
187 | 0 | } |
188 | 0 | return 1; |
189 | 0 | } |
190 | | |
191 | | static int dh_cms_encrypt(CMS_RecipientInfo *ri) |
192 | 0 | { |
193 | 0 | EVP_PKEY_CTX *pctx; |
194 | 0 | EVP_PKEY *pkey; |
195 | 0 | EVP_CIPHER_CTX *ctx; |
196 | 0 | int keylen; |
197 | 0 | X509_ALGOR *talg, *wrap_alg = NULL; |
198 | 0 | const ASN1_OBJECT *aoid; |
199 | 0 | ASN1_BIT_STRING *pubkey; |
200 | 0 | ASN1_STRING *wrap_str; |
201 | 0 | ASN1_OCTET_STRING *ukm; |
202 | 0 | unsigned char *penc = NULL, *dukm = NULL; |
203 | 0 | int penclen; |
204 | 0 | size_t dukmlen = 0; |
205 | 0 | int rv = 0; |
206 | 0 | int kdf_type, wrap_nid; |
207 | 0 | const EVP_MD *kdf_md; |
208 | |
|
209 | 0 | pctx = CMS_RecipientInfo_get0_pkey_ctx(ri); |
210 | 0 | if (pctx == NULL) |
211 | 0 | return 0; |
212 | | /* Get ephemeral key */ |
213 | 0 | pkey = EVP_PKEY_CTX_get0_pkey(pctx); |
214 | 0 | if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey, |
215 | 0 | NULL, NULL, NULL)) |
216 | 0 | goto err; |
217 | | |
218 | | /* Is everything uninitialised? */ |
219 | 0 | X509_ALGOR_get0(&aoid, NULL, NULL, talg); |
220 | 0 | if (aoid == OBJ_nid2obj(NID_undef)) { |
221 | 0 | BIGNUM *bn_pub_key = NULL; |
222 | 0 | ASN1_INTEGER *pubk; |
223 | |
|
224 | 0 | if (!EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PUB_KEY, &bn_pub_key)) |
225 | 0 | goto err; |
226 | | |
227 | 0 | pubk = BN_to_ASN1_INTEGER(bn_pub_key, NULL); |
228 | 0 | BN_free(bn_pub_key); |
229 | 0 | if (pubk == NULL) |
230 | 0 | goto err; |
231 | | |
232 | | /* Set the key */ |
233 | 0 | penclen = i2d_ASN1_INTEGER(pubk, &penc); |
234 | 0 | ASN1_INTEGER_free(pubk); |
235 | 0 | if (penclen <= 0) |
236 | 0 | goto err; |
237 | 0 | ASN1_STRING_set0(pubkey, penc, penclen); |
238 | 0 | ossl_asn1_string_set_bits_left(pubkey, 0); |
239 | |
|
240 | 0 | penc = NULL; |
241 | 0 | (void)X509_ALGOR_set0(talg, OBJ_nid2obj(NID_dhpublicnumber), |
242 | 0 | V_ASN1_UNDEF, NULL); /* cannot fail */ |
243 | 0 | } |
244 | | |
245 | | /* See if custom parameters set */ |
246 | 0 | kdf_type = EVP_PKEY_CTX_get_dh_kdf_type(pctx); |
247 | 0 | if (kdf_type <= 0 || EVP_PKEY_CTX_get_dh_kdf_md(pctx, &kdf_md) <= 0) |
248 | 0 | goto err; |
249 | | |
250 | 0 | if (kdf_type == EVP_PKEY_DH_KDF_NONE) { |
251 | 0 | kdf_type = EVP_PKEY_DH_KDF_X9_42; |
252 | 0 | if (EVP_PKEY_CTX_set_dh_kdf_type(pctx, kdf_type) <= 0) |
253 | 0 | goto err; |
254 | 0 | } else if (kdf_type != EVP_PKEY_DH_KDF_X9_42) |
255 | | /* Unknown KDF */ |
256 | 0 | goto err; |
257 | 0 | if (kdf_md == NULL) { |
258 | | /* Only SHA1 supported */ |
259 | 0 | kdf_md = EVP_sha1(); |
260 | 0 | if (EVP_PKEY_CTX_set_dh_kdf_md(pctx, kdf_md) <= 0) |
261 | 0 | goto err; |
262 | 0 | } else if (EVP_MD_get_type(kdf_md) != NID_sha1) |
263 | | /* Unsupported digest */ |
264 | 0 | goto err; |
265 | | |
266 | 0 | if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm)) |
267 | 0 | goto err; |
268 | | |
269 | | /* Get wrap NID */ |
270 | 0 | ctx = CMS_RecipientInfo_kari_get0_ctx(ri); |
271 | 0 | wrap_nid = EVP_CIPHER_CTX_get_type(ctx); |
272 | 0 | if (EVP_PKEY_CTX_set0_dh_kdf_oid(pctx, OBJ_nid2obj(wrap_nid)) <= 0) |
273 | 0 | goto err; |
274 | 0 | keylen = EVP_CIPHER_CTX_get_key_length(ctx); |
275 | | |
276 | | /* Package wrap algorithm in an AlgorithmIdentifier */ |
277 | |
|
278 | 0 | wrap_alg = X509_ALGOR_new(); |
279 | 0 | if (wrap_alg == NULL) |
280 | 0 | goto err; |
281 | 0 | wrap_alg->algorithm = OBJ_nid2obj(wrap_nid); |
282 | 0 | wrap_alg->parameter = ASN1_TYPE_new(); |
283 | 0 | if (wrap_alg->parameter == NULL) |
284 | 0 | goto err; |
285 | 0 | if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0) |
286 | 0 | goto err; |
287 | 0 | if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) { |
288 | 0 | ASN1_TYPE_free(wrap_alg->parameter); |
289 | 0 | wrap_alg->parameter = NULL; |
290 | 0 | } |
291 | |
|
292 | 0 | if (EVP_PKEY_CTX_set_dh_kdf_outlen(pctx, keylen) <= 0) |
293 | 0 | goto err; |
294 | | |
295 | 0 | if (ukm != NULL) { |
296 | 0 | dukmlen = ASN1_STRING_length(ukm); |
297 | 0 | dukm = OPENSSL_memdup(ASN1_STRING_get0_data(ukm), dukmlen); |
298 | 0 | if (dukm == NULL) |
299 | 0 | goto err; |
300 | 0 | } |
301 | | |
302 | 0 | if (EVP_PKEY_CTX_set0_dh_kdf_ukm(pctx, dukm, dukmlen) <= 0) |
303 | 0 | goto err; |
304 | 0 | dukm = NULL; |
305 | | |
306 | | /* |
307 | | * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter |
308 | | * of another AlgorithmIdentifier. |
309 | | */ |
310 | 0 | penc = NULL; |
311 | 0 | penclen = i2d_X509_ALGOR(wrap_alg, &penc); |
312 | 0 | if (penclen <= 0) |
313 | 0 | goto err; |
314 | 0 | wrap_str = ASN1_STRING_new(); |
315 | 0 | if (wrap_str == NULL) |
316 | 0 | goto err; |
317 | 0 | ASN1_STRING_set0(wrap_str, penc, penclen); |
318 | 0 | penc = NULL; |
319 | 0 | rv = X509_ALGOR_set0(talg, OBJ_nid2obj(NID_id_smime_alg_ESDH), |
320 | 0 | V_ASN1_SEQUENCE, wrap_str); |
321 | 0 | if (!rv) |
322 | 0 | ASN1_STRING_free(wrap_str); |
323 | |
|
324 | 0 | err: |
325 | 0 | OPENSSL_free(penc); |
326 | 0 | X509_ALGOR_free(wrap_alg); |
327 | 0 | OPENSSL_free(dukm); |
328 | 0 | return rv; |
329 | 0 | } |
330 | | |
331 | | int ossl_cms_dh_envelope(CMS_RecipientInfo *ri, int decrypt) |
332 | 0 | { |
333 | 0 | assert(decrypt == 0 || decrypt == 1); |
334 | | |
335 | 0 | if (decrypt == 1) |
336 | 0 | return dh_cms_decrypt(ri); |
337 | | |
338 | 0 | if (decrypt == 0) |
339 | 0 | return dh_cms_encrypt(ri); |
340 | | |
341 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); |
342 | 0 | return 0; |
343 | 0 | } |