/src/openssl32/crypto/cms/cms_env.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2008-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 | | #include "internal/cryptlib.h" |
11 | | #include <openssl/asn1t.h> |
12 | | #include <openssl/pem.h> |
13 | | #include <openssl/x509v3.h> |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/cms.h> |
16 | | #include <openssl/evp.h> |
17 | | #include "internal/sizes.h" |
18 | | #include "crypto/asn1.h" |
19 | | #include "crypto/evp.h" |
20 | | #include "crypto/x509.h" |
21 | | #include "cms_local.h" |
22 | | |
23 | | /* CMS EnvelopedData Utilities */ |
24 | | static void cms_env_set_version(CMS_EnvelopedData *env); |
25 | | |
26 | 2.80k | #define CMS_ENVELOPED_STANDARD 1 |
27 | 0 | #define CMS_ENVELOPED_AUTH 2 |
28 | | |
29 | | static int cms_get_enveloped_type_simple(const CMS_ContentInfo *cms) |
30 | 4.33k | { |
31 | 4.33k | int nid = OBJ_obj2nid(cms->contentType); |
32 | | |
33 | 4.33k | switch (nid) { |
34 | 1.40k | case NID_pkcs7_enveloped: |
35 | 1.40k | return CMS_ENVELOPED_STANDARD; |
36 | | |
37 | 0 | case NID_id_smime_ct_authEnvelopedData: |
38 | 0 | return CMS_ENVELOPED_AUTH; |
39 | | |
40 | 2.93k | default: |
41 | 2.93k | return 0; |
42 | 4.33k | } |
43 | 4.33k | } |
44 | | |
45 | | static int cms_get_enveloped_type(const CMS_ContentInfo *cms) |
46 | 4.33k | { |
47 | 4.33k | int ret = cms_get_enveloped_type_simple(cms); |
48 | | |
49 | 4.33k | if (ret == 0) |
50 | 4.33k | ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA); |
51 | 4.33k | return ret; |
52 | 4.33k | } |
53 | | |
54 | | CMS_EnvelopedData *ossl_cms_get0_enveloped(CMS_ContentInfo *cms) |
55 | 0 | { |
56 | 0 | if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) { |
57 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA); |
58 | 0 | return NULL; |
59 | 0 | } |
60 | 0 | return cms->d.envelopedData; |
61 | 0 | } |
62 | | |
63 | | CMS_AuthEnvelopedData *ossl_cms_get0_auth_enveloped(CMS_ContentInfo *cms) |
64 | 0 | { |
65 | 0 | if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_authEnvelopedData) { |
66 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA); |
67 | 0 | return NULL; |
68 | 0 | } |
69 | 0 | return cms->d.authEnvelopedData; |
70 | 0 | } |
71 | | |
72 | | static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms) |
73 | 0 | { |
74 | 0 | if (cms->d.other == NULL) { |
75 | 0 | cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData); |
76 | 0 | if (cms->d.envelopedData == NULL) { |
77 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
78 | 0 | return NULL; |
79 | 0 | } |
80 | 0 | cms->d.envelopedData->version = 0; |
81 | 0 | cms->d.envelopedData->encryptedContentInfo->contentType = |
82 | 0 | OBJ_nid2obj(NID_pkcs7_data); |
83 | 0 | ASN1_OBJECT_free(cms->contentType); |
84 | 0 | cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped); |
85 | 0 | return cms->d.envelopedData; |
86 | 0 | } |
87 | 0 | return ossl_cms_get0_enveloped(cms); |
88 | 0 | } |
89 | | |
90 | | static CMS_AuthEnvelopedData * |
91 | | cms_auth_enveloped_data_init(CMS_ContentInfo *cms) |
92 | 0 | { |
93 | 0 | if (cms->d.other == NULL) { |
94 | 0 | cms->d.authEnvelopedData = M_ASN1_new_of(CMS_AuthEnvelopedData); |
95 | 0 | if (cms->d.authEnvelopedData == NULL) { |
96 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
97 | 0 | return NULL; |
98 | 0 | } |
99 | | /* Defined in RFC 5083 - Section 2.1. "AuthEnvelopedData Type" */ |
100 | 0 | cms->d.authEnvelopedData->version = 0; |
101 | 0 | cms->d.authEnvelopedData->authEncryptedContentInfo->contentType = |
102 | 0 | OBJ_nid2obj(NID_pkcs7_data); |
103 | 0 | ASN1_OBJECT_free(cms->contentType); |
104 | 0 | cms->contentType = OBJ_nid2obj(NID_id_smime_ct_authEnvelopedData); |
105 | 0 | return cms->d.authEnvelopedData; |
106 | 0 | } |
107 | 0 | return ossl_cms_get0_auth_enveloped(cms); |
108 | 0 | } |
109 | | |
110 | | int ossl_cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd) |
111 | 0 | { |
112 | 0 | EVP_PKEY *pkey; |
113 | 0 | int i; |
114 | 0 | if (ri->type == CMS_RECIPINFO_TRANS) |
115 | 0 | pkey = ri->d.ktri->pkey; |
116 | 0 | else if (ri->type == CMS_RECIPINFO_AGREE) { |
117 | 0 | EVP_PKEY_CTX *pctx = ri->d.kari->pctx; |
118 | |
|
119 | 0 | if (pctx == NULL) |
120 | 0 | return 0; |
121 | 0 | pkey = EVP_PKEY_CTX_get0_pkey(pctx); |
122 | 0 | if (pkey == NULL) |
123 | 0 | return 0; |
124 | 0 | } else |
125 | 0 | return 0; |
126 | | |
127 | 0 | if (EVP_PKEY_is_a(pkey, "DHX") || EVP_PKEY_is_a(pkey, "DH")) |
128 | 0 | return ossl_cms_dh_envelope(ri, cmd); |
129 | 0 | else if (EVP_PKEY_is_a(pkey, "EC")) |
130 | 0 | return ossl_cms_ecdh_envelope(ri, cmd); |
131 | 0 | else if (EVP_PKEY_is_a(pkey, "RSA")) |
132 | 0 | return ossl_cms_rsa_envelope(ri, cmd); |
133 | | |
134 | | /* Something else? We'll give engines etc a chance to handle this */ |
135 | 0 | if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL) |
136 | 0 | return 1; |
137 | 0 | i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri); |
138 | 0 | if (i == -2) { |
139 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); |
140 | 0 | return 0; |
141 | 0 | } |
142 | 0 | if (i <= 0) { |
143 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE); |
144 | 0 | return 0; |
145 | 0 | } |
146 | 0 | return 1; |
147 | 0 | } |
148 | | |
149 | | CMS_EncryptedContentInfo *ossl_cms_get0_env_enc_content(const CMS_ContentInfo *cms) |
150 | 0 | { |
151 | 0 | switch (cms_get_enveloped_type(cms)) { |
152 | 0 | case CMS_ENVELOPED_STANDARD: |
153 | 0 | return cms->d.envelopedData == NULL ? NULL |
154 | 0 | : cms->d.envelopedData->encryptedContentInfo; |
155 | | |
156 | 0 | case CMS_ENVELOPED_AUTH: |
157 | 0 | return cms->d.authEnvelopedData == NULL ? NULL |
158 | 0 | : cms->d.authEnvelopedData->authEncryptedContentInfo; |
159 | | |
160 | 0 | default: |
161 | 0 | return NULL; |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | | STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms) |
166 | 4.33k | { |
167 | 4.33k | switch (cms_get_enveloped_type(cms)) { |
168 | 1.40k | case CMS_ENVELOPED_STANDARD: |
169 | 1.40k | return cms->d.envelopedData->recipientInfos; |
170 | | |
171 | 0 | case CMS_ENVELOPED_AUTH: |
172 | 0 | return cms->d.authEnvelopedData->recipientInfos; |
173 | | |
174 | 2.93k | default: |
175 | 2.93k | return NULL; |
176 | 4.33k | } |
177 | 4.33k | } |
178 | | |
179 | | void ossl_cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms) |
180 | 4.33k | { |
181 | 4.33k | int i; |
182 | 4.33k | CMS_RecipientInfo *ri; |
183 | 4.33k | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
184 | 4.33k | STACK_OF(CMS_RecipientInfo) *rinfos = CMS_get0_RecipientInfos(cms); |
185 | | |
186 | 15.4k | for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) { |
187 | 11.0k | ri = sk_CMS_RecipientInfo_value(rinfos, i); |
188 | 11.0k | if (ri != NULL) { |
189 | 11.0k | switch (ri->type) { |
190 | 2.46k | case CMS_RECIPINFO_AGREE: |
191 | 2.46k | ri->d.kari->cms_ctx = ctx; |
192 | 2.46k | break; |
193 | 2.16k | case CMS_RECIPINFO_TRANS: |
194 | 2.16k | ri->d.ktri->cms_ctx = ctx; |
195 | 2.16k | ossl_x509_set0_libctx(ri->d.ktri->recip, |
196 | 2.16k | ossl_cms_ctx_get0_libctx(ctx), |
197 | 2.16k | ossl_cms_ctx_get0_propq(ctx)); |
198 | 2.16k | break; |
199 | 182 | case CMS_RECIPINFO_KEK: |
200 | 182 | ri->d.kekri->cms_ctx = ctx; |
201 | 182 | break; |
202 | 2.45k | case CMS_RECIPINFO_PASS: |
203 | 2.45k | ri->d.pwri->cms_ctx = ctx; |
204 | 2.45k | break; |
205 | 3.80k | default: |
206 | 3.80k | break; |
207 | 11.0k | } |
208 | 11.0k | } |
209 | 11.0k | } |
210 | 4.33k | } |
211 | | |
212 | | int CMS_RecipientInfo_type(CMS_RecipientInfo *ri) |
213 | 0 | { |
214 | 0 | return ri->type; |
215 | 0 | } |
216 | | |
217 | | EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri) |
218 | 0 | { |
219 | 0 | if (ri->type == CMS_RECIPINFO_TRANS) |
220 | 0 | return ri->d.ktri->pctx; |
221 | 0 | else if (ri->type == CMS_RECIPINFO_AGREE) |
222 | 0 | return ri->d.kari->pctx; |
223 | 0 | return NULL; |
224 | 0 | } |
225 | | |
226 | | CMS_ContentInfo *CMS_EnvelopedData_create_ex(const EVP_CIPHER *cipher, |
227 | | OSSL_LIB_CTX *libctx, |
228 | | const char *propq) |
229 | 0 | { |
230 | 0 | CMS_ContentInfo *cms; |
231 | 0 | CMS_EnvelopedData *env; |
232 | |
|
233 | 0 | cms = CMS_ContentInfo_new_ex(libctx, propq); |
234 | 0 | if (cms == NULL) |
235 | 0 | goto err; |
236 | 0 | env = cms_enveloped_data_init(cms); |
237 | 0 | if (env == NULL) |
238 | 0 | goto err; |
239 | | |
240 | 0 | if (!ossl_cms_EncryptedContent_init(env->encryptedContentInfo, cipher, NULL, |
241 | 0 | 0, ossl_cms_get0_cmsctx(cms))) |
242 | 0 | goto err; |
243 | 0 | return cms; |
244 | 0 | err: |
245 | 0 | CMS_ContentInfo_free(cms); |
246 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB); |
247 | 0 | return NULL; |
248 | 0 | } |
249 | | |
250 | | CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher) |
251 | 0 | { |
252 | 0 | return CMS_EnvelopedData_create_ex(cipher, NULL, NULL); |
253 | 0 | } |
254 | | |
255 | | BIO *CMS_EnvelopedData_decrypt(CMS_EnvelopedData *env, BIO *detached_data, |
256 | | EVP_PKEY *pkey, X509 *cert, |
257 | | ASN1_OCTET_STRING *secret, unsigned int flags, |
258 | | OSSL_LIB_CTX *libctx, const char *propq) |
259 | 0 | { |
260 | 0 | CMS_ContentInfo *ci; |
261 | 0 | BIO *bio = NULL; |
262 | 0 | int res = 0; |
263 | |
|
264 | 0 | if (env == NULL) { |
265 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER); |
266 | 0 | return NULL; |
267 | 0 | } |
268 | | |
269 | 0 | if ((ci = CMS_ContentInfo_new_ex(libctx, propq)) == NULL |
270 | 0 | || (bio = BIO_new(BIO_s_mem())) == NULL) |
271 | 0 | goto end; |
272 | 0 | ci->contentType = OBJ_nid2obj(NID_pkcs7_enveloped); |
273 | 0 | ci->d.envelopedData = env; |
274 | 0 | if (secret != NULL |
275 | 0 | && CMS_decrypt_set1_password(ci, (unsigned char *) |
276 | 0 | ASN1_STRING_get0_data(secret), |
277 | 0 | ASN1_STRING_length(secret)) != 1) |
278 | 0 | goto end; |
279 | 0 | res = CMS_decrypt(ci, secret == NULL ? pkey : NULL, |
280 | 0 | secret == NULL ? cert : NULL, detached_data, bio, flags); |
281 | |
|
282 | 0 | end: |
283 | 0 | if (ci != NULL) { |
284 | 0 | ci->d.envelopedData = NULL; /* do not indirectly free |env| */ |
285 | 0 | ci->contentType = NULL; |
286 | 0 | } |
287 | 0 | CMS_ContentInfo_free(ci); |
288 | 0 | if (!res) { |
289 | 0 | BIO_free(bio); |
290 | 0 | bio = NULL; |
291 | 0 | } |
292 | 0 | return bio; |
293 | 0 | } |
294 | | |
295 | | CMS_ContentInfo * |
296 | | CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER *cipher, OSSL_LIB_CTX *libctx, |
297 | | const char *propq) |
298 | 0 | { |
299 | 0 | CMS_ContentInfo *cms; |
300 | 0 | CMS_AuthEnvelopedData *aenv; |
301 | |
|
302 | 0 | cms = CMS_ContentInfo_new_ex(libctx, propq); |
303 | 0 | if (cms == NULL) |
304 | 0 | goto merr; |
305 | 0 | aenv = cms_auth_enveloped_data_init(cms); |
306 | 0 | if (aenv == NULL) |
307 | 0 | goto merr; |
308 | 0 | if (!ossl_cms_EncryptedContent_init(aenv->authEncryptedContentInfo, |
309 | 0 | cipher, NULL, 0, |
310 | 0 | ossl_cms_get0_cmsctx(cms))) |
311 | 0 | goto merr; |
312 | 0 | return cms; |
313 | 0 | merr: |
314 | 0 | CMS_ContentInfo_free(cms); |
315 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB); |
316 | 0 | return NULL; |
317 | 0 | } |
318 | | |
319 | | |
320 | | CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher) |
321 | 0 | { |
322 | 0 | return CMS_AuthEnvelopedData_create_ex(cipher, NULL, NULL); |
323 | 0 | } |
324 | | |
325 | | /* Key Transport Recipient Info (KTRI) routines */ |
326 | | |
327 | | /* Initialise a ktri based on passed certificate and key */ |
328 | | |
329 | | static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip, |
330 | | EVP_PKEY *pk, unsigned int flags, |
331 | | const CMS_CTX *ctx) |
332 | 0 | { |
333 | 0 | CMS_KeyTransRecipientInfo *ktri; |
334 | 0 | int idtype; |
335 | |
|
336 | 0 | ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo); |
337 | 0 | if (!ri->d.ktri) |
338 | 0 | return 0; |
339 | 0 | ri->type = CMS_RECIPINFO_TRANS; |
340 | |
|
341 | 0 | ktri = ri->d.ktri; |
342 | 0 | ktri->cms_ctx = ctx; |
343 | |
|
344 | 0 | if (flags & CMS_USE_KEYID) { |
345 | 0 | ktri->version = 2; |
346 | 0 | idtype = CMS_RECIPINFO_KEYIDENTIFIER; |
347 | 0 | } else { |
348 | 0 | ktri->version = 0; |
349 | 0 | idtype = CMS_RECIPINFO_ISSUER_SERIAL; |
350 | 0 | } |
351 | | |
352 | | /* |
353 | | * Not a typo: RecipientIdentifier and SignerIdentifier are the same |
354 | | * structure. |
355 | | */ |
356 | |
|
357 | 0 | if (!ossl_cms_set1_SignerIdentifier(ktri->rid, recip, idtype, ctx)) |
358 | 0 | return 0; |
359 | | |
360 | 0 | X509_up_ref(recip); |
361 | 0 | EVP_PKEY_up_ref(pk); |
362 | |
|
363 | 0 | ktri->pkey = pk; |
364 | 0 | ktri->recip = recip; |
365 | |
|
366 | 0 | if (flags & CMS_KEY_PARAM) { |
367 | 0 | ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx), |
368 | 0 | ktri->pkey, |
369 | 0 | ossl_cms_ctx_get0_propq(ctx)); |
370 | 0 | if (ktri->pctx == NULL) |
371 | 0 | return 0; |
372 | 0 | if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0) |
373 | 0 | return 0; |
374 | 0 | } else if (!ossl_cms_env_asn1_ctrl(ri, 0)) |
375 | 0 | return 0; |
376 | 0 | return 1; |
377 | 0 | } |
378 | | |
379 | | /* |
380 | | * Add a recipient certificate using appropriate type of RecipientInfo |
381 | | */ |
382 | | |
383 | | CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip, |
384 | | EVP_PKEY *originatorPrivKey, |
385 | | X509 *originator, unsigned int flags) |
386 | 0 | { |
387 | 0 | CMS_RecipientInfo *ri = NULL; |
388 | 0 | STACK_OF(CMS_RecipientInfo) *ris; |
389 | 0 | EVP_PKEY *pk = NULL; |
390 | 0 | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
391 | |
|
392 | 0 | ris = CMS_get0_RecipientInfos(cms); |
393 | 0 | if (ris == NULL) |
394 | 0 | goto err; |
395 | | |
396 | | /* Initialize recipient info */ |
397 | 0 | ri = M_ASN1_new_of(CMS_RecipientInfo); |
398 | 0 | if (ri == NULL) { |
399 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
400 | 0 | goto err; |
401 | 0 | } |
402 | | |
403 | 0 | pk = X509_get0_pubkey(recip); |
404 | 0 | if (pk == NULL) { |
405 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_GETTING_PUBLIC_KEY); |
406 | 0 | goto err; |
407 | 0 | } |
408 | | |
409 | 0 | switch (ossl_cms_pkey_get_ri_type(pk)) { |
410 | | |
411 | 0 | case CMS_RECIPINFO_TRANS: |
412 | 0 | if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx)) |
413 | 0 | goto err; |
414 | 0 | break; |
415 | | |
416 | 0 | case CMS_RECIPINFO_AGREE: |
417 | 0 | if (!ossl_cms_RecipientInfo_kari_init(ri, recip, pk, originator, |
418 | 0 | originatorPrivKey, flags, ctx)) |
419 | 0 | goto err; |
420 | 0 | break; |
421 | | |
422 | 0 | default: |
423 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); |
424 | 0 | goto err; |
425 | |
|
426 | 0 | } |
427 | | |
428 | 0 | if (!sk_CMS_RecipientInfo_push(ris, ri)) { |
429 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB); |
430 | 0 | goto err; |
431 | 0 | } |
432 | | |
433 | 0 | return ri; |
434 | | |
435 | 0 | err: |
436 | 0 | M_ASN1_free_of(ri, CMS_RecipientInfo); |
437 | 0 | return NULL; |
438 | |
|
439 | 0 | } |
440 | | |
441 | | CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip, |
442 | | unsigned int flags) |
443 | 0 | { |
444 | 0 | return CMS_add1_recipient(cms, recip, NULL, NULL, flags); |
445 | 0 | } |
446 | | |
447 | | int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri, |
448 | | EVP_PKEY **pk, X509 **recip, |
449 | | X509_ALGOR **palg) |
450 | 0 | { |
451 | 0 | CMS_KeyTransRecipientInfo *ktri; |
452 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
453 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
454 | 0 | return 0; |
455 | 0 | } |
456 | | |
457 | 0 | ktri = ri->d.ktri; |
458 | |
|
459 | 0 | if (pk) |
460 | 0 | *pk = ktri->pkey; |
461 | 0 | if (recip) |
462 | 0 | *recip = ktri->recip; |
463 | 0 | if (palg) |
464 | 0 | *palg = ktri->keyEncryptionAlgorithm; |
465 | 0 | return 1; |
466 | 0 | } |
467 | | |
468 | | int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri, |
469 | | ASN1_OCTET_STRING **keyid, |
470 | | X509_NAME **issuer, |
471 | | ASN1_INTEGER **sno) |
472 | 0 | { |
473 | 0 | CMS_KeyTransRecipientInfo *ktri; |
474 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
475 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
476 | 0 | return 0; |
477 | 0 | } |
478 | 0 | ktri = ri->d.ktri; |
479 | |
|
480 | 0 | return ossl_cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, |
481 | 0 | sno); |
482 | 0 | } |
483 | | |
484 | | int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert) |
485 | 0 | { |
486 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
487 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
488 | 0 | return -2; |
489 | 0 | } |
490 | 0 | return ossl_cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert); |
491 | 0 | } |
492 | | |
493 | | int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey) |
494 | 0 | { |
495 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
496 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
497 | 0 | return 0; |
498 | 0 | } |
499 | 0 | EVP_PKEY_free(ri->d.ktri->pkey); |
500 | 0 | ri->d.ktri->pkey = pkey; |
501 | 0 | return 1; |
502 | 0 | } |
503 | | |
504 | | /* Encrypt content key in key transport recipient info */ |
505 | | |
506 | | static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms, |
507 | | CMS_RecipientInfo *ri) |
508 | 0 | { |
509 | 0 | CMS_KeyTransRecipientInfo *ktri; |
510 | 0 | CMS_EncryptedContentInfo *ec; |
511 | 0 | EVP_PKEY_CTX *pctx; |
512 | 0 | unsigned char *ek = NULL; |
513 | 0 | size_t eklen; |
514 | 0 | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
515 | |
|
516 | 0 | int ret = 0; |
517 | |
|
518 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
519 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
520 | 0 | return 0; |
521 | 0 | } |
522 | 0 | ktri = ri->d.ktri; |
523 | 0 | ec = ossl_cms_get0_env_enc_content(cms); |
524 | |
|
525 | 0 | pctx = ktri->pctx; |
526 | |
|
527 | 0 | if (pctx) { |
528 | 0 | if (!ossl_cms_env_asn1_ctrl(ri, 0)) |
529 | 0 | goto err; |
530 | 0 | } else { |
531 | 0 | pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx), |
532 | 0 | ktri->pkey, |
533 | 0 | ossl_cms_ctx_get0_propq(ctx)); |
534 | 0 | if (pctx == NULL) |
535 | 0 | return 0; |
536 | | |
537 | 0 | if (EVP_PKEY_encrypt_init(pctx) <= 0) |
538 | 0 | goto err; |
539 | 0 | } |
540 | | |
541 | 0 | if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0) |
542 | 0 | goto err; |
543 | | |
544 | 0 | ek = OPENSSL_malloc(eklen); |
545 | 0 | if (ek == NULL) |
546 | 0 | goto err; |
547 | | |
548 | 0 | if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0) |
549 | 0 | goto err; |
550 | | |
551 | 0 | ASN1_STRING_set0(ktri->encryptedKey, ek, eklen); |
552 | 0 | ek = NULL; |
553 | |
|
554 | 0 | ret = 1; |
555 | |
|
556 | 0 | err: |
557 | 0 | EVP_PKEY_CTX_free(pctx); |
558 | 0 | ktri->pctx = NULL; |
559 | 0 | OPENSSL_free(ek); |
560 | 0 | return ret; |
561 | 0 | } |
562 | | |
563 | | /* Decrypt content key from KTRI */ |
564 | | |
565 | | static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms, |
566 | | CMS_RecipientInfo *ri) |
567 | 0 | { |
568 | 0 | CMS_KeyTransRecipientInfo *ktri = ri->d.ktri; |
569 | 0 | EVP_PKEY *pkey = ktri->pkey; |
570 | 0 | unsigned char *ek = NULL; |
571 | 0 | size_t eklen; |
572 | 0 | int ret = 0; |
573 | 0 | size_t fixlen = 0; |
574 | 0 | const EVP_CIPHER *cipher = NULL; |
575 | 0 | EVP_CIPHER *fetched_cipher = NULL; |
576 | 0 | CMS_EncryptedContentInfo *ec; |
577 | 0 | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
578 | 0 | OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx); |
579 | 0 | const char *propq = ossl_cms_ctx_get0_propq(ctx); |
580 | |
|
581 | 0 | ec = ossl_cms_get0_env_enc_content(cms); |
582 | |
|
583 | 0 | if (ktri->pkey == NULL) { |
584 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY); |
585 | 0 | return 0; |
586 | 0 | } |
587 | | |
588 | 0 | if (cms->d.envelopedData->encryptedContentInfo->havenocert |
589 | 0 | && !cms->d.envelopedData->encryptedContentInfo->debug) { |
590 | 0 | X509_ALGOR *calg = ec->contentEncryptionAlgorithm; |
591 | 0 | char name[OSSL_MAX_NAME_SIZE]; |
592 | |
|
593 | 0 | OBJ_obj2txt(name, sizeof(name), calg->algorithm, 0); |
594 | |
|
595 | 0 | (void)ERR_set_mark(); |
596 | 0 | fetched_cipher = EVP_CIPHER_fetch(libctx, name, propq); |
597 | |
|
598 | 0 | if (fetched_cipher != NULL) |
599 | 0 | cipher = fetched_cipher; |
600 | 0 | else |
601 | 0 | cipher = EVP_get_cipherbyobj(calg->algorithm); |
602 | 0 | if (cipher == NULL) { |
603 | 0 | (void)ERR_clear_last_mark(); |
604 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER); |
605 | 0 | return 0; |
606 | 0 | } |
607 | 0 | (void)ERR_pop_to_mark(); |
608 | |
|
609 | 0 | fixlen = EVP_CIPHER_get_key_length(cipher); |
610 | 0 | EVP_CIPHER_free(fetched_cipher); |
611 | 0 | } |
612 | | |
613 | 0 | ktri->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq); |
614 | 0 | if (ktri->pctx == NULL) |
615 | 0 | goto err; |
616 | | |
617 | 0 | if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0) |
618 | 0 | goto err; |
619 | | |
620 | 0 | if (!ossl_cms_env_asn1_ctrl(ri, 1)) |
621 | 0 | goto err; |
622 | | |
623 | 0 | if (EVP_PKEY_is_a(pkey, "RSA")) |
624 | | /* upper layer CMS code incorrectly assumes that a successful RSA |
625 | | * decryption means that the key matches ciphertext (which never |
626 | | * was the case, implicit rejection or not), so to make it work |
627 | | * disable implicit rejection for RSA keys */ |
628 | 0 | EVP_PKEY_CTX_ctrl_str(ktri->pctx, "rsa_pkcs1_implicit_rejection", "0"); |
629 | |
|
630 | 0 | if (evp_pkey_decrypt_alloc(ktri->pctx, &ek, &eklen, fixlen, |
631 | 0 | ktri->encryptedKey->data, |
632 | 0 | ktri->encryptedKey->length) <= 0) |
633 | 0 | goto err; |
634 | | |
635 | 0 | ret = 1; |
636 | |
|
637 | 0 | OPENSSL_clear_free(ec->key, ec->keylen); |
638 | 0 | ec->key = ek; |
639 | 0 | ec->keylen = eklen; |
640 | |
|
641 | 0 | err: |
642 | 0 | EVP_PKEY_CTX_free(ktri->pctx); |
643 | 0 | ktri->pctx = NULL; |
644 | 0 | if (!ret) |
645 | 0 | OPENSSL_free(ek); |
646 | |
|
647 | 0 | return ret; |
648 | 0 | } |
649 | | |
650 | | /* Key Encrypted Key (KEK) RecipientInfo routines */ |
651 | | |
652 | | int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri, |
653 | | const unsigned char *id, size_t idlen) |
654 | 0 | { |
655 | 0 | ASN1_OCTET_STRING tmp_os; |
656 | 0 | CMS_KEKRecipientInfo *kekri; |
657 | 0 | if (ri->type != CMS_RECIPINFO_KEK) { |
658 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK); |
659 | 0 | return -2; |
660 | 0 | } |
661 | 0 | kekri = ri->d.kekri; |
662 | 0 | tmp_os.type = V_ASN1_OCTET_STRING; |
663 | 0 | tmp_os.flags = 0; |
664 | 0 | tmp_os.data = (unsigned char *)id; |
665 | 0 | tmp_os.length = (int)idlen; |
666 | 0 | return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier); |
667 | 0 | } |
668 | | |
669 | | /* For now hard code AES key wrap info */ |
670 | | |
671 | | static size_t aes_wrap_keylen(int nid) |
672 | 0 | { |
673 | 0 | switch (nid) { |
674 | 0 | case NID_id_aes128_wrap: |
675 | 0 | return 16; |
676 | | |
677 | 0 | case NID_id_aes192_wrap: |
678 | 0 | return 24; |
679 | | |
680 | 0 | case NID_id_aes256_wrap: |
681 | 0 | return 32; |
682 | | |
683 | 0 | default: |
684 | 0 | return 0; |
685 | 0 | } |
686 | 0 | } |
687 | | |
688 | | CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid, |
689 | | unsigned char *key, size_t keylen, |
690 | | unsigned char *id, size_t idlen, |
691 | | ASN1_GENERALIZEDTIME *date, |
692 | | ASN1_OBJECT *otherTypeId, |
693 | | ASN1_TYPE *otherType) |
694 | 0 | { |
695 | 0 | CMS_RecipientInfo *ri = NULL; |
696 | 0 | CMS_KEKRecipientInfo *kekri; |
697 | 0 | STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms); |
698 | |
|
699 | 0 | if (ris == NULL) |
700 | 0 | goto err; |
701 | | |
702 | 0 | if (nid == NID_undef) { |
703 | 0 | switch (keylen) { |
704 | 0 | case 16: |
705 | 0 | nid = NID_id_aes128_wrap; |
706 | 0 | break; |
707 | | |
708 | 0 | case 24: |
709 | 0 | nid = NID_id_aes192_wrap; |
710 | 0 | break; |
711 | | |
712 | 0 | case 32: |
713 | 0 | nid = NID_id_aes256_wrap; |
714 | 0 | break; |
715 | | |
716 | 0 | default: |
717 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
718 | 0 | goto err; |
719 | 0 | } |
720 | |
|
721 | 0 | } else { |
722 | |
|
723 | 0 | size_t exp_keylen = aes_wrap_keylen(nid); |
724 | |
|
725 | 0 | if (!exp_keylen) { |
726 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM); |
727 | 0 | goto err; |
728 | 0 | } |
729 | | |
730 | 0 | if (keylen != exp_keylen) { |
731 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
732 | 0 | goto err; |
733 | 0 | } |
734 | |
|
735 | 0 | } |
736 | | |
737 | | /* Initialize recipient info */ |
738 | 0 | ri = M_ASN1_new_of(CMS_RecipientInfo); |
739 | 0 | if (!ri) { |
740 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
741 | 0 | goto err; |
742 | 0 | } |
743 | | |
744 | 0 | ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo); |
745 | 0 | if (!ri->d.kekri) { |
746 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
747 | 0 | goto err; |
748 | 0 | } |
749 | 0 | ri->type = CMS_RECIPINFO_KEK; |
750 | |
|
751 | 0 | kekri = ri->d.kekri; |
752 | |
|
753 | 0 | if (otherTypeId) { |
754 | 0 | kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute); |
755 | 0 | if (kekri->kekid->other == NULL) { |
756 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
757 | 0 | goto err; |
758 | 0 | } |
759 | 0 | } |
760 | | |
761 | 0 | if (!sk_CMS_RecipientInfo_push(ris, ri)) { |
762 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB); |
763 | 0 | goto err; |
764 | 0 | } |
765 | | |
766 | | /* After this point no calls can fail */ |
767 | | |
768 | 0 | kekri->version = 4; |
769 | |
|
770 | 0 | kekri->key = key; |
771 | 0 | kekri->keylen = keylen; |
772 | |
|
773 | 0 | ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen); |
774 | |
|
775 | 0 | kekri->kekid->date = date; |
776 | |
|
777 | 0 | if (kekri->kekid->other) { |
778 | 0 | kekri->kekid->other->keyAttrId = otherTypeId; |
779 | 0 | kekri->kekid->other->keyAttr = otherType; |
780 | 0 | } |
781 | |
|
782 | 0 | (void)X509_ALGOR_set0(kekri->keyEncryptionAlgorithm, OBJ_nid2obj(nid), |
783 | 0 | V_ASN1_UNDEF, NULL); /* cannot fail */ |
784 | |
|
785 | 0 | return ri; |
786 | | |
787 | 0 | err: |
788 | 0 | M_ASN1_free_of(ri, CMS_RecipientInfo); |
789 | 0 | return NULL; |
790 | 0 | } |
791 | | |
792 | | int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri, |
793 | | X509_ALGOR **palg, |
794 | | ASN1_OCTET_STRING **pid, |
795 | | ASN1_GENERALIZEDTIME **pdate, |
796 | | ASN1_OBJECT **potherid, |
797 | | ASN1_TYPE **pothertype) |
798 | 0 | { |
799 | 0 | CMS_KEKIdentifier *rkid; |
800 | 0 | if (ri->type != CMS_RECIPINFO_KEK) { |
801 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK); |
802 | 0 | return 0; |
803 | 0 | } |
804 | 0 | rkid = ri->d.kekri->kekid; |
805 | 0 | if (palg) |
806 | 0 | *palg = ri->d.kekri->keyEncryptionAlgorithm; |
807 | 0 | if (pid) |
808 | 0 | *pid = rkid->keyIdentifier; |
809 | 0 | if (pdate) |
810 | 0 | *pdate = rkid->date; |
811 | 0 | if (potherid) { |
812 | 0 | if (rkid->other) |
813 | 0 | *potherid = rkid->other->keyAttrId; |
814 | 0 | else |
815 | 0 | *potherid = NULL; |
816 | 0 | } |
817 | 0 | if (pothertype) { |
818 | 0 | if (rkid->other) |
819 | 0 | *pothertype = rkid->other->keyAttr; |
820 | 0 | else |
821 | 0 | *pothertype = NULL; |
822 | 0 | } |
823 | 0 | return 1; |
824 | 0 | } |
825 | | |
826 | | int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri, |
827 | | unsigned char *key, size_t keylen) |
828 | 0 | { |
829 | 0 | CMS_KEKRecipientInfo *kekri; |
830 | 0 | if (ri->type != CMS_RECIPINFO_KEK) { |
831 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK); |
832 | 0 | return 0; |
833 | 0 | } |
834 | | |
835 | 0 | kekri = ri->d.kekri; |
836 | 0 | kekri->key = key; |
837 | 0 | kekri->keylen = keylen; |
838 | 0 | return 1; |
839 | 0 | } |
840 | | |
841 | | static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx) |
842 | 0 | { |
843 | 0 | const char *alg = NULL; |
844 | |
|
845 | 0 | switch (keylen) { |
846 | 0 | case 16: |
847 | 0 | alg = "AES-128-WRAP"; |
848 | 0 | break; |
849 | 0 | case 24: |
850 | 0 | alg = "AES-192-WRAP"; |
851 | 0 | break; |
852 | 0 | case 32: |
853 | 0 | alg = "AES-256-WRAP"; |
854 | 0 | break; |
855 | 0 | default: |
856 | 0 | return NULL; |
857 | 0 | } |
858 | 0 | return EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(ctx), alg, |
859 | 0 | ossl_cms_ctx_get0_propq(ctx)); |
860 | 0 | } |
861 | | |
862 | | |
863 | | /* Encrypt content key in KEK recipient info */ |
864 | | |
865 | | static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms, |
866 | | CMS_RecipientInfo *ri) |
867 | 0 | { |
868 | 0 | CMS_EncryptedContentInfo *ec; |
869 | 0 | CMS_KEKRecipientInfo *kekri; |
870 | 0 | unsigned char *wkey = NULL; |
871 | 0 | int wkeylen; |
872 | 0 | int r = 0; |
873 | 0 | EVP_CIPHER *cipher = NULL; |
874 | 0 | int outlen = 0; |
875 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
876 | 0 | const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms); |
877 | |
|
878 | 0 | ec = ossl_cms_get0_env_enc_content(cms); |
879 | 0 | if (ec == NULL) |
880 | 0 | return 0; |
881 | | |
882 | 0 | kekri = ri->d.kekri; |
883 | |
|
884 | 0 | if (kekri->key == NULL) { |
885 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY); |
886 | 0 | return 0; |
887 | 0 | } |
888 | | |
889 | 0 | cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx); |
890 | 0 | if (cipher == NULL) { |
891 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
892 | 0 | goto err; |
893 | 0 | } |
894 | | |
895 | | /* 8 byte prefix for AES wrap ciphers */ |
896 | 0 | wkey = OPENSSL_malloc(ec->keylen + 8); |
897 | 0 | if (wkey == NULL) |
898 | 0 | goto err; |
899 | | |
900 | 0 | ctx = EVP_CIPHER_CTX_new(); |
901 | 0 | if (ctx == NULL) { |
902 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
903 | 0 | goto err; |
904 | 0 | } |
905 | | |
906 | 0 | EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); |
907 | 0 | if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL) |
908 | 0 | || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen) |
909 | 0 | || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) { |
910 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR); |
911 | 0 | goto err; |
912 | 0 | } |
913 | 0 | wkeylen += outlen; |
914 | 0 | if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) { |
915 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR); |
916 | 0 | goto err; |
917 | 0 | } |
918 | | |
919 | 0 | ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen); |
920 | |
|
921 | 0 | r = 1; |
922 | |
|
923 | 0 | err: |
924 | 0 | EVP_CIPHER_free(cipher); |
925 | 0 | if (!r) |
926 | 0 | OPENSSL_free(wkey); |
927 | 0 | EVP_CIPHER_CTX_free(ctx); |
928 | |
|
929 | 0 | return r; |
930 | 0 | } |
931 | | |
932 | | /* Decrypt content key in KEK recipient info */ |
933 | | |
934 | | static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms, |
935 | | CMS_RecipientInfo *ri) |
936 | 0 | { |
937 | 0 | CMS_EncryptedContentInfo *ec; |
938 | 0 | CMS_KEKRecipientInfo *kekri; |
939 | 0 | unsigned char *ukey = NULL; |
940 | 0 | int ukeylen; |
941 | 0 | int r = 0, wrap_nid; |
942 | 0 | EVP_CIPHER *cipher = NULL; |
943 | 0 | int outlen = 0; |
944 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
945 | 0 | const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms); |
946 | |
|
947 | 0 | ec = ossl_cms_get0_env_enc_content(cms); |
948 | 0 | if (ec == NULL) |
949 | 0 | return 0; |
950 | | |
951 | 0 | kekri = ri->d.kekri; |
952 | |
|
953 | 0 | if (!kekri->key) { |
954 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY); |
955 | 0 | return 0; |
956 | 0 | } |
957 | | |
958 | 0 | wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm); |
959 | 0 | if (aes_wrap_keylen(wrap_nid) != kekri->keylen) { |
960 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
961 | 0 | return 0; |
962 | 0 | } |
963 | | |
964 | | /* If encrypted key length is invalid don't bother */ |
965 | | |
966 | 0 | if (kekri->encryptedKey->length < 16) { |
967 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH); |
968 | 0 | goto err; |
969 | 0 | } |
970 | | |
971 | 0 | cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx); |
972 | 0 | if (cipher == NULL) { |
973 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
974 | 0 | goto err; |
975 | 0 | } |
976 | | |
977 | 0 | ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8); |
978 | 0 | if (ukey == NULL) |
979 | 0 | goto err; |
980 | | |
981 | 0 | ctx = EVP_CIPHER_CTX_new(); |
982 | 0 | if (ctx == NULL) { |
983 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
984 | 0 | goto err; |
985 | 0 | } |
986 | | |
987 | 0 | if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL) |
988 | 0 | || !EVP_DecryptUpdate(ctx, ukey, &ukeylen, |
989 | 0 | kekri->encryptedKey->data, |
990 | 0 | kekri->encryptedKey->length) |
991 | 0 | || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) { |
992 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_ERROR); |
993 | 0 | goto err; |
994 | 0 | } |
995 | 0 | ukeylen += outlen; |
996 | |
|
997 | 0 | OPENSSL_clear_free(ec->key, ec->keylen); |
998 | 0 | ec->key = ukey; |
999 | 0 | ec->keylen = ukeylen; |
1000 | |
|
1001 | 0 | r = 1; |
1002 | |
|
1003 | 0 | err: |
1004 | 0 | EVP_CIPHER_free(cipher); |
1005 | 0 | if (!r) |
1006 | 0 | OPENSSL_free(ukey); |
1007 | 0 | EVP_CIPHER_CTX_free(ctx); |
1008 | |
|
1009 | 0 | return r; |
1010 | 0 | } |
1011 | | |
1012 | | int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri) |
1013 | 0 | { |
1014 | 0 | switch (ri->type) { |
1015 | 0 | case CMS_RECIPINFO_TRANS: |
1016 | 0 | return cms_RecipientInfo_ktri_decrypt(cms, ri); |
1017 | | |
1018 | 0 | case CMS_RECIPINFO_KEK: |
1019 | 0 | return cms_RecipientInfo_kekri_decrypt(cms, ri); |
1020 | | |
1021 | 0 | case CMS_RECIPINFO_PASS: |
1022 | 0 | return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 0); |
1023 | | |
1024 | 0 | default: |
1025 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE); |
1026 | 0 | return 0; |
1027 | 0 | } |
1028 | 0 | } |
1029 | | |
1030 | | int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri) |
1031 | 0 | { |
1032 | 0 | switch (ri->type) { |
1033 | 0 | case CMS_RECIPINFO_TRANS: |
1034 | 0 | return cms_RecipientInfo_ktri_encrypt(cms, ri); |
1035 | | |
1036 | 0 | case CMS_RECIPINFO_AGREE: |
1037 | 0 | return ossl_cms_RecipientInfo_kari_encrypt(cms, ri); |
1038 | | |
1039 | 0 | case CMS_RECIPINFO_KEK: |
1040 | 0 | return cms_RecipientInfo_kekri_encrypt(cms, ri); |
1041 | | |
1042 | 0 | case CMS_RECIPINFO_PASS: |
1043 | 0 | return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 1); |
1044 | | |
1045 | 0 | default: |
1046 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENT_TYPE); |
1047 | 0 | return 0; |
1048 | 0 | } |
1049 | 0 | } |
1050 | | |
1051 | | /* Check structures and fixup version numbers (if necessary) */ |
1052 | | |
1053 | | static void cms_env_set_originfo_version(CMS_EnvelopedData *env) |
1054 | 0 | { |
1055 | 0 | CMS_OriginatorInfo *org = env->originatorInfo; |
1056 | 0 | int i; |
1057 | 0 | if (org == NULL) |
1058 | 0 | return; |
1059 | 0 | for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) { |
1060 | 0 | CMS_CertificateChoices *cch; |
1061 | 0 | cch = sk_CMS_CertificateChoices_value(org->certificates, i); |
1062 | 0 | if (cch->type == CMS_CERTCHOICE_OTHER) { |
1063 | 0 | env->version = 4; |
1064 | 0 | return; |
1065 | 0 | } else if (cch->type == CMS_CERTCHOICE_V2ACERT) { |
1066 | 0 | if (env->version < 3) |
1067 | 0 | env->version = 3; |
1068 | 0 | } |
1069 | 0 | } |
1070 | | |
1071 | 0 | for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) { |
1072 | 0 | CMS_RevocationInfoChoice *rch; |
1073 | 0 | rch = sk_CMS_RevocationInfoChoice_value(org->crls, i); |
1074 | 0 | if (rch->type == CMS_REVCHOICE_OTHER) { |
1075 | 0 | env->version = 4; |
1076 | 0 | return; |
1077 | 0 | } |
1078 | 0 | } |
1079 | 0 | } |
1080 | | |
1081 | | static void cms_env_set_version(CMS_EnvelopedData *env) |
1082 | 0 | { |
1083 | 0 | int i; |
1084 | 0 | CMS_RecipientInfo *ri; |
1085 | | |
1086 | | /* |
1087 | | * Can't set version higher than 4 so if 4 or more already nothing to do. |
1088 | | */ |
1089 | 0 | if (env->version >= 4) |
1090 | 0 | return; |
1091 | | |
1092 | 0 | cms_env_set_originfo_version(env); |
1093 | |
|
1094 | 0 | if (env->version >= 3) |
1095 | 0 | return; |
1096 | | |
1097 | 0 | for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) { |
1098 | 0 | ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i); |
1099 | 0 | if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) { |
1100 | 0 | env->version = 3; |
1101 | 0 | return; |
1102 | 0 | } else if (ri->type != CMS_RECIPINFO_TRANS |
1103 | 0 | || ri->d.ktri->version != 0) { |
1104 | 0 | env->version = 2; |
1105 | 0 | } |
1106 | 0 | } |
1107 | 0 | if (env->originatorInfo || env->unprotectedAttrs) |
1108 | 0 | env->version = 2; |
1109 | 0 | if (env->version == 2) |
1110 | 0 | return; |
1111 | 0 | env->version = 0; |
1112 | 0 | } |
1113 | | |
1114 | | static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms, |
1115 | | STACK_OF(CMS_RecipientInfo) *ris) |
1116 | 0 | { |
1117 | 0 | int i; |
1118 | 0 | CMS_RecipientInfo *ri; |
1119 | |
|
1120 | 0 | for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) { |
1121 | 0 | ri = sk_CMS_RecipientInfo_value(ris, i); |
1122 | 0 | if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) |
1123 | 0 | return -1; |
1124 | 0 | } |
1125 | 0 | return 1; |
1126 | 0 | } |
1127 | | |
1128 | | static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec) |
1129 | 0 | { |
1130 | 0 | ec->cipher = NULL; |
1131 | 0 | OPENSSL_clear_free(ec->key, ec->keylen); |
1132 | 0 | ec->key = NULL; |
1133 | 0 | ec->keylen = 0; |
1134 | 0 | } |
1135 | | |
1136 | | static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms) |
1137 | 0 | { |
1138 | 0 | CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo; |
1139 | 0 | BIO *contentBio = ossl_cms_EncryptedContent_init_bio(ec, |
1140 | 0 | ossl_cms_get0_cmsctx(cms)); |
1141 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
1142 | |
|
1143 | 0 | if (contentBio == NULL) |
1144 | 0 | return NULL; |
1145 | | |
1146 | 0 | BIO_get_cipher_ctx(contentBio, &ctx); |
1147 | 0 | if (ctx == NULL) { |
1148 | 0 | BIO_free(contentBio); |
1149 | 0 | return NULL; |
1150 | 0 | } |
1151 | | /* |
1152 | | * If the selected cipher supports unprotected attributes, |
1153 | | * deal with it using special ctrl function |
1154 | | */ |
1155 | 0 | if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) |
1156 | 0 | & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0 |
1157 | 0 | && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0, |
1158 | 0 | cms->d.envelopedData->unprotectedAttrs) <= 0) { |
1159 | 0 | BIO_free(contentBio); |
1160 | 0 | return NULL; |
1161 | 0 | } |
1162 | 0 | return contentBio; |
1163 | 0 | } |
1164 | | |
1165 | | static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms) |
1166 | 0 | { |
1167 | 0 | CMS_EncryptedContentInfo *ec; |
1168 | 0 | STACK_OF(CMS_RecipientInfo) *rinfos; |
1169 | 0 | int ok = 0; |
1170 | 0 | BIO *ret; |
1171 | 0 | CMS_EnvelopedData *env = cms->d.envelopedData; |
1172 | | |
1173 | | /* Get BIO first to set up key */ |
1174 | |
|
1175 | 0 | ec = env->encryptedContentInfo; |
1176 | 0 | ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms)); |
1177 | | |
1178 | | /* If error end of processing */ |
1179 | 0 | if (!ret) |
1180 | 0 | return ret; |
1181 | | |
1182 | | /* Now encrypt content key according to each RecipientInfo type */ |
1183 | 0 | rinfos = env->recipientInfos; |
1184 | 0 | if (cms_env_encrypt_content_key(cms, rinfos) < 0) { |
1185 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO); |
1186 | 0 | goto err; |
1187 | 0 | } |
1188 | | |
1189 | | /* And finally set the version */ |
1190 | 0 | cms_env_set_version(env); |
1191 | |
|
1192 | 0 | ok = 1; |
1193 | |
|
1194 | 0 | err: |
1195 | 0 | cms_env_clear_ec(ec); |
1196 | 0 | if (ok) |
1197 | 0 | return ret; |
1198 | 0 | BIO_free(ret); |
1199 | 0 | return NULL; |
1200 | 0 | } |
1201 | | |
1202 | | BIO *ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo *cms) |
1203 | 0 | { |
1204 | 0 | if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) { |
1205 | | /* If cipher is set it's encryption */ |
1206 | 0 | return cms_EnvelopedData_Encryption_init_bio(cms); |
1207 | 0 | } |
1208 | | |
1209 | | /* If cipher is not set it's decryption */ |
1210 | 0 | return cms_EnvelopedData_Decryption_init_bio(cms); |
1211 | 0 | } |
1212 | | |
1213 | | BIO *ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms) |
1214 | 0 | { |
1215 | 0 | CMS_EncryptedContentInfo *ec; |
1216 | 0 | STACK_OF(CMS_RecipientInfo) *rinfos; |
1217 | 0 | int ok = 0; |
1218 | 0 | BIO *ret; |
1219 | 0 | CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData; |
1220 | | |
1221 | | /* Get BIO first to set up key */ |
1222 | 0 | ec = aenv->authEncryptedContentInfo; |
1223 | | /* Set tag for decryption */ |
1224 | 0 | if (ec->cipher == NULL) { |
1225 | 0 | ec->tag = aenv->mac->data; |
1226 | 0 | ec->taglen = aenv->mac->length; |
1227 | 0 | } |
1228 | 0 | ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms)); |
1229 | | |
1230 | | /* If error or no cipher end of processing */ |
1231 | 0 | if (ret == NULL || ec->cipher == NULL) |
1232 | 0 | return ret; |
1233 | | |
1234 | | /* Now encrypt content key according to each RecipientInfo type */ |
1235 | 0 | rinfos = aenv->recipientInfos; |
1236 | 0 | if (cms_env_encrypt_content_key(cms, rinfos) < 0) { |
1237 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO); |
1238 | 0 | goto err; |
1239 | 0 | } |
1240 | | |
1241 | | /* And finally set the version */ |
1242 | 0 | aenv->version = 0; |
1243 | |
|
1244 | 0 | ok = 1; |
1245 | |
|
1246 | 0 | err: |
1247 | 0 | cms_env_clear_ec(ec); |
1248 | 0 | if (ok) |
1249 | 0 | return ret; |
1250 | 0 | BIO_free(ret); |
1251 | 0 | return NULL; |
1252 | 0 | } |
1253 | | |
1254 | | int ossl_cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain) |
1255 | 0 | { |
1256 | 0 | CMS_EnvelopedData *env = NULL; |
1257 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
1258 | 0 | BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER); |
1259 | |
|
1260 | 0 | env = ossl_cms_get0_enveloped(cms); |
1261 | 0 | if (env == NULL) |
1262 | 0 | return 0; |
1263 | | |
1264 | 0 | if (mbio == NULL) { |
1265 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND); |
1266 | 0 | return 0; |
1267 | 0 | } |
1268 | | |
1269 | 0 | BIO_get_cipher_ctx(mbio, &ctx); |
1270 | | |
1271 | | /* |
1272 | | * If the selected cipher supports unprotected attributes, |
1273 | | * deal with it using special ctrl function |
1274 | | */ |
1275 | 0 | if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) |
1276 | 0 | & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) { |
1277 | 0 | if (env->unprotectedAttrs == NULL) |
1278 | 0 | env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null(); |
1279 | |
|
1280 | 0 | if (env->unprotectedAttrs == NULL) { |
1281 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB); |
1282 | 0 | return 0; |
1283 | 0 | } |
1284 | | |
1285 | 0 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, |
1286 | 0 | 1, env->unprotectedAttrs) <= 0) { |
1287 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE); |
1288 | 0 | return 0; |
1289 | 0 | } |
1290 | 0 | } |
1291 | | |
1292 | 0 | cms_env_set_version(cms->d.envelopedData); |
1293 | 0 | return 1; |
1294 | 0 | } |
1295 | | |
1296 | | int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio) |
1297 | 0 | { |
1298 | 0 | EVP_CIPHER_CTX *ctx; |
1299 | 0 | unsigned char *tag = NULL; |
1300 | 0 | int taglen, ok = 0; |
1301 | |
|
1302 | 0 | BIO_get_cipher_ctx(cmsbio, &ctx); |
1303 | | |
1304 | | /* |
1305 | | * The tag is set only for encryption. There is nothing to do for |
1306 | | * decryption. |
1307 | | */ |
1308 | 0 | if (!EVP_CIPHER_CTX_is_encrypting(ctx)) |
1309 | 0 | return 1; |
1310 | | |
1311 | 0 | taglen = EVP_CIPHER_CTX_get_tag_length(ctx); |
1312 | 0 | if (taglen <= 0 |
1313 | 0 | || (tag = OPENSSL_malloc(taglen)) == NULL |
1314 | 0 | || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, |
1315 | 0 | tag) <= 0) { |
1316 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_GET_TAG); |
1317 | 0 | goto err; |
1318 | 0 | } |
1319 | | |
1320 | 0 | if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen)) |
1321 | 0 | goto err; |
1322 | | |
1323 | 0 | ok = 1; |
1324 | 0 | err: |
1325 | 0 | OPENSSL_free(tag); |
1326 | 0 | return ok; |
1327 | 0 | } |
1328 | | |
1329 | | /* |
1330 | | * Get RecipientInfo type (if any) supported by a key (public or private). To |
1331 | | * retain compatibility with previous behaviour if the ctrl value isn't |
1332 | | * supported we assume key transport. |
1333 | | */ |
1334 | | int ossl_cms_pkey_get_ri_type(EVP_PKEY *pk) |
1335 | 0 | { |
1336 | | /* Check types that we know about */ |
1337 | 0 | if (EVP_PKEY_is_a(pk, "DH")) |
1338 | 0 | return CMS_RECIPINFO_AGREE; |
1339 | 0 | else if (EVP_PKEY_is_a(pk, "DHX")) |
1340 | 0 | return CMS_RECIPINFO_AGREE; |
1341 | 0 | else if (EVP_PKEY_is_a(pk, "DSA")) |
1342 | 0 | return CMS_RECIPINFO_NONE; |
1343 | 0 | else if (EVP_PKEY_is_a(pk, "EC")) |
1344 | 0 | return CMS_RECIPINFO_AGREE; |
1345 | 0 | else if (EVP_PKEY_is_a(pk, "RSA")) |
1346 | 0 | return CMS_RECIPINFO_TRANS; |
1347 | | |
1348 | | /* |
1349 | | * Otherwise this might ben an engine implementation, so see if we can get |
1350 | | * the type from the ameth. |
1351 | | */ |
1352 | 0 | if (pk->ameth && pk->ameth->pkey_ctrl) { |
1353 | 0 | int i, r; |
1354 | 0 | i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r); |
1355 | 0 | if (i > 0) |
1356 | 0 | return r; |
1357 | 0 | } |
1358 | 0 | return CMS_RECIPINFO_TRANS; |
1359 | 0 | } |
1360 | | |
1361 | | int ossl_cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type) |
1362 | 0 | { |
1363 | 0 | int supportedRiType; |
1364 | |
|
1365 | 0 | if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) { |
1366 | 0 | int i, r; |
1367 | |
|
1368 | 0 | i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED, |
1369 | 0 | ri_type, &r); |
1370 | 0 | if (i > 0) |
1371 | 0 | return r; |
1372 | 0 | } |
1373 | | |
1374 | 0 | supportedRiType = ossl_cms_pkey_get_ri_type(pk); |
1375 | 0 | if (supportedRiType < 0) |
1376 | 0 | return 0; |
1377 | | |
1378 | 0 | return (supportedRiType == ri_type); |
1379 | 0 | } |