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