/src/openssl33/crypto/cms/cms_env.c
Line | Count | Source |
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 | 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 | X509_up_ref(recip); |
358 | 0 | EVP_PKEY_up_ref(pk); |
359 | |
|
360 | 0 | ktri->pkey = pk; |
361 | 0 | ktri->recip = recip; |
362 | |
|
363 | 0 | if (flags & CMS_KEY_PARAM) { |
364 | 0 | ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx), |
365 | 0 | ktri->pkey, |
366 | 0 | ossl_cms_ctx_get0_propq(ctx)); |
367 | 0 | if (ktri->pctx == NULL) |
368 | 0 | return 0; |
369 | 0 | if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0) |
370 | 0 | return 0; |
371 | 0 | } else if (!ossl_cms_env_asn1_ctrl(ri, 0)) |
372 | 0 | return 0; |
373 | 0 | return 1; |
374 | 0 | } |
375 | | |
376 | | /* |
377 | | * Add a recipient certificate using appropriate type of RecipientInfo |
378 | | */ |
379 | | |
380 | | CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip, |
381 | | EVP_PKEY *originatorPrivKey, |
382 | | X509 *originator, unsigned int flags) |
383 | 0 | { |
384 | 0 | CMS_RecipientInfo *ri = NULL; |
385 | 0 | STACK_OF(CMS_RecipientInfo) *ris; |
386 | 0 | EVP_PKEY *pk = NULL; |
387 | 0 | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
388 | |
|
389 | 0 | ris = CMS_get0_RecipientInfos(cms); |
390 | 0 | if (ris == NULL) |
391 | 0 | goto err; |
392 | | |
393 | | /* Initialize recipient info */ |
394 | 0 | ri = M_ASN1_new_of(CMS_RecipientInfo); |
395 | 0 | if (ri == NULL) { |
396 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
397 | 0 | goto err; |
398 | 0 | } |
399 | | |
400 | 0 | pk = X509_get0_pubkey(recip); |
401 | 0 | if (pk == NULL) { |
402 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_GETTING_PUBLIC_KEY); |
403 | 0 | goto err; |
404 | 0 | } |
405 | | |
406 | 0 | switch (ossl_cms_pkey_get_ri_type(pk)) { |
407 | | |
408 | 0 | case CMS_RECIPINFO_TRANS: |
409 | 0 | if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx)) |
410 | 0 | goto err; |
411 | 0 | break; |
412 | | |
413 | 0 | case CMS_RECIPINFO_AGREE: |
414 | 0 | if (!ossl_cms_RecipientInfo_kari_init(ri, recip, pk, originator, |
415 | 0 | originatorPrivKey, flags, ctx)) |
416 | 0 | goto err; |
417 | 0 | break; |
418 | | |
419 | 0 | default: |
420 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); |
421 | 0 | goto err; |
422 | 0 | } |
423 | | |
424 | 0 | if (!sk_CMS_RecipientInfo_push(ris, ri)) { |
425 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB); |
426 | 0 | goto err; |
427 | 0 | } |
428 | | |
429 | 0 | return ri; |
430 | | |
431 | 0 | err: |
432 | 0 | M_ASN1_free_of(ri, CMS_RecipientInfo); |
433 | 0 | return NULL; |
434 | 0 | } |
435 | | |
436 | | CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip, |
437 | | unsigned int flags) |
438 | 0 | { |
439 | 0 | return CMS_add1_recipient(cms, recip, NULL, NULL, flags); |
440 | 0 | } |
441 | | |
442 | | int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri, |
443 | | EVP_PKEY **pk, X509 **recip, |
444 | | X509_ALGOR **palg) |
445 | 0 | { |
446 | 0 | CMS_KeyTransRecipientInfo *ktri; |
447 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
448 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
449 | 0 | return 0; |
450 | 0 | } |
451 | | |
452 | 0 | ktri = ri->d.ktri; |
453 | |
|
454 | 0 | if (pk) |
455 | 0 | *pk = ktri->pkey; |
456 | 0 | if (recip) |
457 | 0 | *recip = ktri->recip; |
458 | 0 | if (palg) |
459 | 0 | *palg = ktri->keyEncryptionAlgorithm; |
460 | 0 | return 1; |
461 | 0 | } |
462 | | |
463 | | int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri, |
464 | | ASN1_OCTET_STRING **keyid, |
465 | | X509_NAME **issuer, |
466 | | ASN1_INTEGER **sno) |
467 | 0 | { |
468 | 0 | CMS_KeyTransRecipientInfo *ktri; |
469 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
470 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
471 | 0 | return 0; |
472 | 0 | } |
473 | 0 | ktri = ri->d.ktri; |
474 | |
|
475 | 0 | return ossl_cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, |
476 | 0 | sno); |
477 | 0 | } |
478 | | |
479 | | int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert) |
480 | 0 | { |
481 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
482 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
483 | 0 | return -2; |
484 | 0 | } |
485 | 0 | return ossl_cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert); |
486 | 0 | } |
487 | | |
488 | | int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey) |
489 | 0 | { |
490 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
491 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
492 | 0 | return 0; |
493 | 0 | } |
494 | 0 | EVP_PKEY_free(ri->d.ktri->pkey); |
495 | 0 | ri->d.ktri->pkey = pkey; |
496 | 0 | return 1; |
497 | 0 | } |
498 | | |
499 | | /* Encrypt content key in key transport recipient info */ |
500 | | |
501 | | static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms, |
502 | | CMS_RecipientInfo *ri) |
503 | 0 | { |
504 | 0 | CMS_KeyTransRecipientInfo *ktri; |
505 | 0 | CMS_EncryptedContentInfo *ec; |
506 | 0 | EVP_PKEY_CTX *pctx; |
507 | 0 | unsigned char *ek = NULL; |
508 | 0 | size_t eklen; |
509 | 0 | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
510 | |
|
511 | 0 | int ret = 0; |
512 | |
|
513 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
514 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
515 | 0 | return 0; |
516 | 0 | } |
517 | 0 | ktri = ri->d.ktri; |
518 | 0 | ec = ossl_cms_get0_env_enc_content(cms); |
519 | |
|
520 | 0 | pctx = ktri->pctx; |
521 | |
|
522 | 0 | if (pctx) { |
523 | 0 | if (!ossl_cms_env_asn1_ctrl(ri, 0)) |
524 | 0 | goto err; |
525 | 0 | } else { |
526 | 0 | pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx), |
527 | 0 | ktri->pkey, |
528 | 0 | ossl_cms_ctx_get0_propq(ctx)); |
529 | 0 | if (pctx == NULL) |
530 | 0 | return 0; |
531 | | |
532 | 0 | if (EVP_PKEY_encrypt_init(pctx) <= 0) |
533 | 0 | goto err; |
534 | 0 | } |
535 | | |
536 | 0 | if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0) |
537 | 0 | goto err; |
538 | | |
539 | 0 | ek = OPENSSL_malloc(eklen); |
540 | 0 | if (ek == NULL) |
541 | 0 | goto err; |
542 | | |
543 | 0 | if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0) |
544 | 0 | goto err; |
545 | | |
546 | 0 | ASN1_STRING_set0(ktri->encryptedKey, ek, eklen); |
547 | 0 | ek = NULL; |
548 | |
|
549 | 0 | ret = 1; |
550 | |
|
551 | 0 | err: |
552 | 0 | EVP_PKEY_CTX_free(pctx); |
553 | 0 | ktri->pctx = NULL; |
554 | 0 | OPENSSL_free(ek); |
555 | 0 | return ret; |
556 | 0 | } |
557 | | |
558 | | /* Decrypt content key from KTRI */ |
559 | | |
560 | | static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms, |
561 | | CMS_RecipientInfo *ri) |
562 | 0 | { |
563 | 0 | CMS_KeyTransRecipientInfo *ktri = ri->d.ktri; |
564 | 0 | EVP_PKEY *pkey = ktri->pkey; |
565 | 0 | unsigned char *ek = NULL; |
566 | 0 | size_t eklen; |
567 | 0 | int ret = 0; |
568 | 0 | size_t fixlen = 0; |
569 | 0 | const EVP_CIPHER *cipher = NULL; |
570 | 0 | EVP_CIPHER *fetched_cipher = NULL; |
571 | 0 | CMS_EncryptedContentInfo *ec; |
572 | 0 | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
573 | 0 | OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx); |
574 | 0 | const char *propq = ossl_cms_ctx_get0_propq(ctx); |
575 | |
|
576 | 0 | ec = ossl_cms_get0_env_enc_content(cms); |
577 | |
|
578 | 0 | if (ktri->pkey == NULL) { |
579 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY); |
580 | 0 | return 0; |
581 | 0 | } |
582 | | |
583 | 0 | if (cms->d.envelopedData->encryptedContentInfo->havenocert |
584 | 0 | && !cms->d.envelopedData->encryptedContentInfo->debug) { |
585 | 0 | X509_ALGOR *calg = ec->contentEncryptionAlgorithm; |
586 | 0 | char name[OSSL_MAX_NAME_SIZE]; |
587 | |
|
588 | 0 | OBJ_obj2txt(name, sizeof(name), calg->algorithm, 0); |
589 | |
|
590 | 0 | (void)ERR_set_mark(); |
591 | 0 | fetched_cipher = EVP_CIPHER_fetch(libctx, name, propq); |
592 | |
|
593 | 0 | if (fetched_cipher != NULL) |
594 | 0 | cipher = fetched_cipher; |
595 | 0 | else |
596 | 0 | cipher = EVP_get_cipherbyobj(calg->algorithm); |
597 | 0 | if (cipher == NULL) { |
598 | 0 | (void)ERR_clear_last_mark(); |
599 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER); |
600 | 0 | return 0; |
601 | 0 | } |
602 | 0 | (void)ERR_pop_to_mark(); |
603 | |
|
604 | 0 | fixlen = EVP_CIPHER_get_key_length(cipher); |
605 | 0 | EVP_CIPHER_free(fetched_cipher); |
606 | 0 | } |
607 | | |
608 | 0 | ktri->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq); |
609 | 0 | if (ktri->pctx == NULL) |
610 | 0 | goto err; |
611 | | |
612 | 0 | if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0) |
613 | 0 | goto err; |
614 | | |
615 | 0 | if (!ossl_cms_env_asn1_ctrl(ri, 1)) |
616 | 0 | goto err; |
617 | | |
618 | 0 | if (EVP_PKEY_is_a(pkey, "RSA")) |
619 | | /* upper layer CMS code incorrectly assumes that a successful RSA |
620 | | * decryption means that the key matches ciphertext (which never |
621 | | * was the case, implicit rejection or not), so to make it work |
622 | | * disable implicit rejection for RSA keys */ |
623 | 0 | EVP_PKEY_CTX_ctrl_str(ktri->pctx, "rsa_pkcs1_implicit_rejection", "0"); |
624 | |
|
625 | 0 | if (evp_pkey_decrypt_alloc(ktri->pctx, &ek, &eklen, fixlen, |
626 | 0 | ktri->encryptedKey->data, |
627 | 0 | ktri->encryptedKey->length) |
628 | 0 | <= 0) |
629 | 0 | goto err; |
630 | | |
631 | 0 | ret = 1; |
632 | |
|
633 | 0 | OPENSSL_clear_free(ec->key, ec->keylen); |
634 | 0 | ec->key = ek; |
635 | 0 | ec->keylen = eklen; |
636 | |
|
637 | 0 | err: |
638 | 0 | EVP_PKEY_CTX_free(ktri->pctx); |
639 | 0 | ktri->pctx = NULL; |
640 | 0 | if (!ret) |
641 | 0 | OPENSSL_free(ek); |
642 | |
|
643 | 0 | return ret; |
644 | 0 | } |
645 | | |
646 | | /* Key Encrypted Key (KEK) RecipientInfo routines */ |
647 | | |
648 | | int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri, |
649 | | const unsigned char *id, size_t idlen) |
650 | 0 | { |
651 | 0 | ASN1_OCTET_STRING tmp_os; |
652 | 0 | CMS_KEKRecipientInfo *kekri; |
653 | 0 | if (ri->type != CMS_RECIPINFO_KEK) { |
654 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK); |
655 | 0 | return -2; |
656 | 0 | } |
657 | 0 | kekri = ri->d.kekri; |
658 | 0 | tmp_os.type = V_ASN1_OCTET_STRING; |
659 | 0 | tmp_os.flags = 0; |
660 | 0 | tmp_os.data = (unsigned char *)id; |
661 | 0 | tmp_os.length = (int)idlen; |
662 | 0 | return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier); |
663 | 0 | } |
664 | | |
665 | | /* For now hard code AES key wrap info */ |
666 | | |
667 | | static size_t aes_wrap_keylen(int nid) |
668 | 0 | { |
669 | 0 | switch (nid) { |
670 | 0 | case NID_id_aes128_wrap: |
671 | 0 | return 16; |
672 | | |
673 | 0 | case NID_id_aes192_wrap: |
674 | 0 | return 24; |
675 | | |
676 | 0 | case NID_id_aes256_wrap: |
677 | 0 | return 32; |
678 | | |
679 | 0 | default: |
680 | 0 | return 0; |
681 | 0 | } |
682 | 0 | } |
683 | | |
684 | | CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid, |
685 | | unsigned char *key, size_t keylen, |
686 | | unsigned char *id, size_t idlen, |
687 | | ASN1_GENERALIZEDTIME *date, |
688 | | ASN1_OBJECT *otherTypeId, |
689 | | ASN1_TYPE *otherType) |
690 | 0 | { |
691 | 0 | CMS_RecipientInfo *ri = NULL; |
692 | 0 | CMS_KEKRecipientInfo *kekri; |
693 | 0 | STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms); |
694 | |
|
695 | 0 | if (ris == NULL) |
696 | 0 | goto err; |
697 | | |
698 | 0 | if (nid == NID_undef) { |
699 | 0 | switch (keylen) { |
700 | 0 | case 16: |
701 | 0 | nid = NID_id_aes128_wrap; |
702 | 0 | break; |
703 | | |
704 | 0 | case 24: |
705 | 0 | nid = NID_id_aes192_wrap; |
706 | 0 | break; |
707 | | |
708 | 0 | case 32: |
709 | 0 | nid = NID_id_aes256_wrap; |
710 | 0 | break; |
711 | | |
712 | 0 | default: |
713 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
714 | 0 | goto err; |
715 | 0 | } |
716 | |
|
717 | 0 | } else { |
718 | |
|
719 | 0 | size_t exp_keylen = aes_wrap_keylen(nid); |
720 | |
|
721 | 0 | if (!exp_keylen) { |
722 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM); |
723 | 0 | goto err; |
724 | 0 | } |
725 | | |
726 | 0 | if (keylen != exp_keylen) { |
727 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
728 | 0 | goto err; |
729 | 0 | } |
730 | 0 | } |
731 | | |
732 | | /* Initialize recipient info */ |
733 | 0 | ri = M_ASN1_new_of(CMS_RecipientInfo); |
734 | 0 | if (!ri) { |
735 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
736 | 0 | goto err; |
737 | 0 | } |
738 | | |
739 | 0 | ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo); |
740 | 0 | if (!ri->d.kekri) { |
741 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
742 | 0 | goto err; |
743 | 0 | } |
744 | 0 | ri->type = CMS_RECIPINFO_KEK; |
745 | |
|
746 | 0 | kekri = ri->d.kekri; |
747 | |
|
748 | 0 | if (otherTypeId) { |
749 | 0 | kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute); |
750 | 0 | if (kekri->kekid->other == NULL) { |
751 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
752 | 0 | goto err; |
753 | 0 | } |
754 | 0 | } |
755 | | |
756 | 0 | if (!sk_CMS_RecipientInfo_push(ris, ri)) { |
757 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB); |
758 | 0 | goto err; |
759 | 0 | } |
760 | | |
761 | | /* After this point no calls can fail */ |
762 | | |
763 | 0 | kekri->version = 4; |
764 | |
|
765 | 0 | kekri->key = key; |
766 | 0 | kekri->keylen = keylen; |
767 | |
|
768 | 0 | ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen); |
769 | |
|
770 | 0 | kekri->kekid->date = date; |
771 | |
|
772 | 0 | if (kekri->kekid->other) { |
773 | 0 | kekri->kekid->other->keyAttrId = otherTypeId; |
774 | 0 | kekri->kekid->other->keyAttr = otherType; |
775 | 0 | } |
776 | |
|
777 | 0 | (void)X509_ALGOR_set0(kekri->keyEncryptionAlgorithm, OBJ_nid2obj(nid), |
778 | 0 | V_ASN1_UNDEF, NULL); /* cannot fail */ |
779 | |
|
780 | 0 | return ri; |
781 | | |
782 | 0 | err: |
783 | 0 | M_ASN1_free_of(ri, CMS_RecipientInfo); |
784 | 0 | return NULL; |
785 | 0 | } |
786 | | |
787 | | int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri, |
788 | | X509_ALGOR **palg, |
789 | | ASN1_OCTET_STRING **pid, |
790 | | ASN1_GENERALIZEDTIME **pdate, |
791 | | ASN1_OBJECT **potherid, |
792 | | ASN1_TYPE **pothertype) |
793 | 0 | { |
794 | 0 | CMS_KEKIdentifier *rkid; |
795 | 0 | if (ri->type != CMS_RECIPINFO_KEK) { |
796 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK); |
797 | 0 | return 0; |
798 | 0 | } |
799 | 0 | rkid = ri->d.kekri->kekid; |
800 | 0 | if (palg) |
801 | 0 | *palg = ri->d.kekri->keyEncryptionAlgorithm; |
802 | 0 | if (pid) |
803 | 0 | *pid = rkid->keyIdentifier; |
804 | 0 | if (pdate) |
805 | 0 | *pdate = rkid->date; |
806 | 0 | if (potherid) { |
807 | 0 | if (rkid->other) |
808 | 0 | *potherid = rkid->other->keyAttrId; |
809 | 0 | else |
810 | 0 | *potherid = NULL; |
811 | 0 | } |
812 | 0 | if (pothertype) { |
813 | 0 | if (rkid->other) |
814 | 0 | *pothertype = rkid->other->keyAttr; |
815 | 0 | else |
816 | 0 | *pothertype = NULL; |
817 | 0 | } |
818 | 0 | return 1; |
819 | 0 | } |
820 | | |
821 | | int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri, |
822 | | unsigned char *key, size_t keylen) |
823 | 0 | { |
824 | 0 | CMS_KEKRecipientInfo *kekri; |
825 | 0 | if (ri->type != CMS_RECIPINFO_KEK) { |
826 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK); |
827 | 0 | return 0; |
828 | 0 | } |
829 | | |
830 | 0 | kekri = ri->d.kekri; |
831 | 0 | kekri->key = key; |
832 | 0 | kekri->keylen = keylen; |
833 | 0 | return 1; |
834 | 0 | } |
835 | | |
836 | | static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx) |
837 | 0 | { |
838 | 0 | const char *alg = NULL; |
839 | |
|
840 | 0 | switch (keylen) { |
841 | 0 | case 16: |
842 | 0 | alg = "AES-128-WRAP"; |
843 | 0 | break; |
844 | 0 | case 24: |
845 | 0 | alg = "AES-192-WRAP"; |
846 | 0 | break; |
847 | 0 | case 32: |
848 | 0 | alg = "AES-256-WRAP"; |
849 | 0 | break; |
850 | 0 | default: |
851 | 0 | return NULL; |
852 | 0 | } |
853 | 0 | return EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(ctx), alg, |
854 | 0 | ossl_cms_ctx_get0_propq(ctx)); |
855 | 0 | } |
856 | | |
857 | | /* Encrypt content key in KEK recipient info */ |
858 | | |
859 | | static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms, |
860 | | CMS_RecipientInfo *ri) |
861 | 0 | { |
862 | 0 | CMS_EncryptedContentInfo *ec; |
863 | 0 | CMS_KEKRecipientInfo *kekri; |
864 | 0 | unsigned char *wkey = NULL; |
865 | 0 | int wkeylen; |
866 | 0 | int r = 0; |
867 | 0 | EVP_CIPHER *cipher = NULL; |
868 | 0 | int outlen = 0; |
869 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
870 | 0 | const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms); |
871 | |
|
872 | 0 | ec = ossl_cms_get0_env_enc_content(cms); |
873 | 0 | if (ec == NULL) |
874 | 0 | return 0; |
875 | | |
876 | 0 | kekri = ri->d.kekri; |
877 | |
|
878 | 0 | if (kekri->key == NULL) { |
879 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY); |
880 | 0 | return 0; |
881 | 0 | } |
882 | | |
883 | 0 | cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx); |
884 | 0 | if (cipher == NULL) { |
885 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
886 | 0 | goto err; |
887 | 0 | } |
888 | | |
889 | | /* 8 byte prefix for AES wrap ciphers */ |
890 | 0 | wkey = OPENSSL_malloc(ec->keylen + 8); |
891 | 0 | if (wkey == NULL) |
892 | 0 | goto err; |
893 | | |
894 | 0 | ctx = EVP_CIPHER_CTX_new(); |
895 | 0 | if (ctx == NULL) { |
896 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
897 | 0 | goto err; |
898 | 0 | } |
899 | | |
900 | 0 | EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); |
901 | 0 | if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL) |
902 | 0 | || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen) |
903 | 0 | || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) { |
904 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR); |
905 | 0 | goto err; |
906 | 0 | } |
907 | 0 | wkeylen += outlen; |
908 | 0 | if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) { |
909 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR); |
910 | 0 | goto err; |
911 | 0 | } |
912 | | |
913 | 0 | ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen); |
914 | |
|
915 | 0 | r = 1; |
916 | |
|
917 | 0 | err: |
918 | 0 | EVP_CIPHER_free(cipher); |
919 | 0 | if (!r) |
920 | 0 | OPENSSL_free(wkey); |
921 | 0 | EVP_CIPHER_CTX_free(ctx); |
922 | |
|
923 | 0 | return r; |
924 | 0 | } |
925 | | |
926 | | /* Decrypt content key in KEK recipient info */ |
927 | | |
928 | | static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms, |
929 | | CMS_RecipientInfo *ri) |
930 | 0 | { |
931 | 0 | CMS_EncryptedContentInfo *ec; |
932 | 0 | CMS_KEKRecipientInfo *kekri; |
933 | 0 | unsigned char *ukey = NULL; |
934 | 0 | int ukeylen; |
935 | 0 | int r = 0, wrap_nid; |
936 | 0 | EVP_CIPHER *cipher = NULL; |
937 | 0 | int outlen = 0; |
938 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
939 | 0 | const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms); |
940 | |
|
941 | 0 | ec = ossl_cms_get0_env_enc_content(cms); |
942 | 0 | if (ec == NULL) |
943 | 0 | return 0; |
944 | | |
945 | 0 | kekri = ri->d.kekri; |
946 | |
|
947 | 0 | if (!kekri->key) { |
948 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY); |
949 | 0 | return 0; |
950 | 0 | } |
951 | | |
952 | 0 | wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm); |
953 | 0 | if (aes_wrap_keylen(wrap_nid) != kekri->keylen) { |
954 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
955 | 0 | return 0; |
956 | 0 | } |
957 | | |
958 | | /* If encrypted key length is invalid don't bother */ |
959 | | |
960 | 0 | if (kekri->encryptedKey->length < 16) { |
961 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH); |
962 | 0 | goto err; |
963 | 0 | } |
964 | | |
965 | 0 | cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx); |
966 | 0 | if (cipher == NULL) { |
967 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
968 | 0 | goto err; |
969 | 0 | } |
970 | | |
971 | 0 | ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8); |
972 | 0 | if (ukey == NULL) |
973 | 0 | goto err; |
974 | | |
975 | 0 | ctx = EVP_CIPHER_CTX_new(); |
976 | 0 | if (ctx == NULL) { |
977 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
978 | 0 | goto err; |
979 | 0 | } |
980 | | |
981 | 0 | if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL) |
982 | 0 | || !EVP_DecryptUpdate(ctx, ukey, &ukeylen, |
983 | 0 | kekri->encryptedKey->data, |
984 | 0 | kekri->encryptedKey->length) |
985 | 0 | || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) { |
986 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_ERROR); |
987 | 0 | goto err; |
988 | 0 | } |
989 | 0 | ukeylen += outlen; |
990 | |
|
991 | 0 | OPENSSL_clear_free(ec->key, ec->keylen); |
992 | 0 | ec->key = ukey; |
993 | 0 | ec->keylen = ukeylen; |
994 | |
|
995 | 0 | r = 1; |
996 | |
|
997 | 0 | err: |
998 | 0 | EVP_CIPHER_free(cipher); |
999 | 0 | if (!r) |
1000 | 0 | OPENSSL_free(ukey); |
1001 | 0 | EVP_CIPHER_CTX_free(ctx); |
1002 | |
|
1003 | 0 | return r; |
1004 | 0 | } |
1005 | | |
1006 | | int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri) |
1007 | 0 | { |
1008 | 0 | switch (ri->type) { |
1009 | 0 | case CMS_RECIPINFO_TRANS: |
1010 | 0 | return cms_RecipientInfo_ktri_decrypt(cms, ri); |
1011 | | |
1012 | 0 | case CMS_RECIPINFO_KEK: |
1013 | 0 | return cms_RecipientInfo_kekri_decrypt(cms, ri); |
1014 | | |
1015 | 0 | case CMS_RECIPINFO_PASS: |
1016 | 0 | return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 0); |
1017 | | |
1018 | 0 | default: |
1019 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE); |
1020 | 0 | return 0; |
1021 | 0 | } |
1022 | 0 | } |
1023 | | |
1024 | | int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri) |
1025 | 0 | { |
1026 | 0 | switch (ri->type) { |
1027 | 0 | case CMS_RECIPINFO_TRANS: |
1028 | 0 | return cms_RecipientInfo_ktri_encrypt(cms, ri); |
1029 | | |
1030 | 0 | case CMS_RECIPINFO_AGREE: |
1031 | 0 | return ossl_cms_RecipientInfo_kari_encrypt(cms, ri); |
1032 | | |
1033 | 0 | case CMS_RECIPINFO_KEK: |
1034 | 0 | return cms_RecipientInfo_kekri_encrypt(cms, ri); |
1035 | | |
1036 | 0 | case CMS_RECIPINFO_PASS: |
1037 | 0 | return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 1); |
1038 | | |
1039 | 0 | default: |
1040 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENT_TYPE); |
1041 | 0 | return 0; |
1042 | 0 | } |
1043 | 0 | } |
1044 | | |
1045 | | /* Check structures and fixup version numbers (if necessary) */ |
1046 | | |
1047 | | static void cms_env_set_originfo_version(CMS_EnvelopedData *env) |
1048 | 0 | { |
1049 | 0 | CMS_OriginatorInfo *org = env->originatorInfo; |
1050 | 0 | int i; |
1051 | 0 | if (org == NULL) |
1052 | 0 | return; |
1053 | 0 | for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) { |
1054 | 0 | CMS_CertificateChoices *cch; |
1055 | 0 | cch = sk_CMS_CertificateChoices_value(org->certificates, i); |
1056 | 0 | if (cch->type == CMS_CERTCHOICE_OTHER) { |
1057 | 0 | env->version = 4; |
1058 | 0 | return; |
1059 | 0 | } else if (cch->type == CMS_CERTCHOICE_V2ACERT) { |
1060 | 0 | if (env->version < 3) |
1061 | 0 | env->version = 3; |
1062 | 0 | } |
1063 | 0 | } |
1064 | | |
1065 | 0 | for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) { |
1066 | 0 | CMS_RevocationInfoChoice *rch; |
1067 | 0 | rch = sk_CMS_RevocationInfoChoice_value(org->crls, i); |
1068 | 0 | if (rch->type == CMS_REVCHOICE_OTHER) { |
1069 | 0 | env->version = 4; |
1070 | 0 | return; |
1071 | 0 | } |
1072 | 0 | } |
1073 | 0 | } |
1074 | | |
1075 | | static void cms_env_set_version(CMS_EnvelopedData *env) |
1076 | 0 | { |
1077 | 0 | int i; |
1078 | 0 | CMS_RecipientInfo *ri; |
1079 | | |
1080 | | /* |
1081 | | * Can't set version higher than 4 so if 4 or more already nothing to do. |
1082 | | */ |
1083 | 0 | if (env->version >= 4) |
1084 | 0 | return; |
1085 | | |
1086 | 0 | cms_env_set_originfo_version(env); |
1087 | |
|
1088 | 0 | if (env->version >= 3) |
1089 | 0 | return; |
1090 | | |
1091 | 0 | for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) { |
1092 | 0 | ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i); |
1093 | 0 | if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) { |
1094 | 0 | env->version = 3; |
1095 | 0 | return; |
1096 | 0 | } else if (ri->type != CMS_RECIPINFO_TRANS |
1097 | 0 | || ri->d.ktri->version != 0) { |
1098 | 0 | env->version = 2; |
1099 | 0 | } |
1100 | 0 | } |
1101 | 0 | if (env->originatorInfo || env->unprotectedAttrs) |
1102 | 0 | env->version = 2; |
1103 | 0 | if (env->version == 2) |
1104 | 0 | return; |
1105 | 0 | env->version = 0; |
1106 | 0 | } |
1107 | | |
1108 | | static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms, |
1109 | | STACK_OF(CMS_RecipientInfo) *ris) |
1110 | 0 | { |
1111 | 0 | int i; |
1112 | 0 | CMS_RecipientInfo *ri; |
1113 | |
|
1114 | 0 | for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) { |
1115 | 0 | ri = sk_CMS_RecipientInfo_value(ris, i); |
1116 | 0 | if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) |
1117 | 0 | return -1; |
1118 | 0 | } |
1119 | 0 | return 1; |
1120 | 0 | } |
1121 | | |
1122 | | static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec) |
1123 | 0 | { |
1124 | 0 | ec->cipher = NULL; |
1125 | 0 | OPENSSL_clear_free(ec->key, ec->keylen); |
1126 | 0 | ec->key = NULL; |
1127 | 0 | ec->keylen = 0; |
1128 | 0 | } |
1129 | | |
1130 | | static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms) |
1131 | 0 | { |
1132 | 0 | CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo; |
1133 | 0 | BIO *contentBio = ossl_cms_EncryptedContent_init_bio(ec, |
1134 | 0 | ossl_cms_get0_cmsctx(cms)); |
1135 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
1136 | |
|
1137 | 0 | if (contentBio == NULL) |
1138 | 0 | return NULL; |
1139 | | |
1140 | 0 | BIO_get_cipher_ctx(contentBio, &ctx); |
1141 | 0 | if (ctx == NULL) { |
1142 | 0 | BIO_free(contentBio); |
1143 | 0 | return NULL; |
1144 | 0 | } |
1145 | | /* |
1146 | | * If the selected cipher supports unprotected attributes, |
1147 | | * deal with it using special ctrl function |
1148 | | */ |
1149 | 0 | if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) |
1150 | 0 | & EVP_CIPH_FLAG_CIPHER_WITH_MAC) |
1151 | 0 | != 0 |
1152 | 0 | && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0, |
1153 | 0 | cms->d.envelopedData->unprotectedAttrs) |
1154 | 0 | <= 0) { |
1155 | 0 | BIO_free(contentBio); |
1156 | 0 | return NULL; |
1157 | 0 | } |
1158 | 0 | return contentBio; |
1159 | 0 | } |
1160 | | |
1161 | | static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms) |
1162 | 0 | { |
1163 | 0 | CMS_EncryptedContentInfo *ec; |
1164 | 0 | STACK_OF(CMS_RecipientInfo) *rinfos; |
1165 | 0 | int ok = 0; |
1166 | 0 | BIO *ret; |
1167 | 0 | CMS_EnvelopedData *env = cms->d.envelopedData; |
1168 | | |
1169 | | /* Get BIO first to set up key */ |
1170 | |
|
1171 | 0 | ec = env->encryptedContentInfo; |
1172 | 0 | ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms)); |
1173 | | |
1174 | | /* If error end of processing */ |
1175 | 0 | if (!ret) |
1176 | 0 | return ret; |
1177 | | |
1178 | | /* Now encrypt content key according to each RecipientInfo type */ |
1179 | 0 | rinfos = env->recipientInfos; |
1180 | 0 | if (cms_env_encrypt_content_key(cms, rinfos) < 0) { |
1181 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO); |
1182 | 0 | goto err; |
1183 | 0 | } |
1184 | | |
1185 | | /* And finally set the version */ |
1186 | 0 | cms_env_set_version(env); |
1187 | |
|
1188 | 0 | ok = 1; |
1189 | |
|
1190 | 0 | err: |
1191 | 0 | cms_env_clear_ec(ec); |
1192 | 0 | if (ok) |
1193 | 0 | return ret; |
1194 | 0 | BIO_free(ret); |
1195 | 0 | return NULL; |
1196 | 0 | } |
1197 | | |
1198 | | BIO *ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo *cms) |
1199 | 0 | { |
1200 | 0 | if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) { |
1201 | | /* If cipher is set it's encryption */ |
1202 | 0 | return cms_EnvelopedData_Encryption_init_bio(cms); |
1203 | 0 | } |
1204 | | |
1205 | | /* If cipher is not set it's decryption */ |
1206 | 0 | return cms_EnvelopedData_Decryption_init_bio(cms); |
1207 | 0 | } |
1208 | | |
1209 | | BIO *ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms) |
1210 | 0 | { |
1211 | 0 | CMS_EncryptedContentInfo *ec; |
1212 | 0 | STACK_OF(CMS_RecipientInfo) *rinfos; |
1213 | 0 | int ok = 0; |
1214 | 0 | BIO *ret; |
1215 | 0 | CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData; |
1216 | | |
1217 | | /* Get BIO first to set up key */ |
1218 | 0 | ec = aenv->authEncryptedContentInfo; |
1219 | | /* Set tag for decryption */ |
1220 | 0 | if (ec->cipher == NULL) { |
1221 | 0 | ec->tag = aenv->mac->data; |
1222 | 0 | ec->taglen = aenv->mac->length; |
1223 | 0 | } |
1224 | 0 | ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms)); |
1225 | | |
1226 | | /* If error or no cipher end of processing */ |
1227 | 0 | if (ret == NULL || ec->cipher == NULL) |
1228 | 0 | return ret; |
1229 | | |
1230 | | /* Now encrypt content key according to each RecipientInfo type */ |
1231 | 0 | rinfos = aenv->recipientInfos; |
1232 | 0 | if (cms_env_encrypt_content_key(cms, rinfos) < 0) { |
1233 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO); |
1234 | 0 | goto err; |
1235 | 0 | } |
1236 | | |
1237 | | /* And finally set the version */ |
1238 | 0 | aenv->version = 0; |
1239 | |
|
1240 | 0 | ok = 1; |
1241 | |
|
1242 | 0 | err: |
1243 | 0 | cms_env_clear_ec(ec); |
1244 | 0 | if (ok) |
1245 | 0 | return ret; |
1246 | 0 | BIO_free(ret); |
1247 | 0 | return NULL; |
1248 | 0 | } |
1249 | | |
1250 | | int ossl_cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain) |
1251 | 0 | { |
1252 | 0 | CMS_EnvelopedData *env = NULL; |
1253 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
1254 | 0 | BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER); |
1255 | |
|
1256 | 0 | env = ossl_cms_get0_enveloped(cms); |
1257 | 0 | if (env == NULL) |
1258 | 0 | return 0; |
1259 | | |
1260 | 0 | if (mbio == NULL) { |
1261 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND); |
1262 | 0 | return 0; |
1263 | 0 | } |
1264 | | |
1265 | 0 | BIO_get_cipher_ctx(mbio, &ctx); |
1266 | | |
1267 | | /* |
1268 | | * If the selected cipher supports unprotected attributes, |
1269 | | * deal with it using special ctrl function |
1270 | | */ |
1271 | 0 | if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) |
1272 | 0 | & EVP_CIPH_FLAG_CIPHER_WITH_MAC) |
1273 | 0 | != 0) { |
1274 | 0 | if (env->unprotectedAttrs == NULL) |
1275 | 0 | env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null(); |
1276 | |
|
1277 | 0 | if (env->unprotectedAttrs == NULL) { |
1278 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB); |
1279 | 0 | return 0; |
1280 | 0 | } |
1281 | | |
1282 | 0 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, |
1283 | 0 | 1, env->unprotectedAttrs) |
1284 | 0 | <= 0) { |
1285 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE); |
1286 | 0 | return 0; |
1287 | 0 | } |
1288 | 0 | } |
1289 | | |
1290 | 0 | cms_env_set_version(cms->d.envelopedData); |
1291 | 0 | return 1; |
1292 | 0 | } |
1293 | | |
1294 | | int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio) |
1295 | 0 | { |
1296 | 0 | EVP_CIPHER_CTX *ctx; |
1297 | 0 | unsigned char *tag = NULL; |
1298 | 0 | int taglen, ok = 0; |
1299 | |
|
1300 | 0 | BIO_get_cipher_ctx(cmsbio, &ctx); |
1301 | | |
1302 | | /* |
1303 | | * The tag is set only for encryption. There is nothing to do for |
1304 | | * decryption. |
1305 | | */ |
1306 | 0 | if (!EVP_CIPHER_CTX_is_encrypting(ctx)) |
1307 | 0 | return 1; |
1308 | | |
1309 | 0 | taglen = EVP_CIPHER_CTX_get_tag_length(ctx); |
1310 | 0 | if (taglen <= 0 |
1311 | 0 | || (tag = OPENSSL_malloc(taglen)) == NULL |
1312 | 0 | || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, |
1313 | 0 | tag) |
1314 | 0 | <= 0) { |
1315 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_GET_TAG); |
1316 | 0 | goto err; |
1317 | 0 | } |
1318 | | |
1319 | 0 | if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen)) |
1320 | 0 | goto err; |
1321 | | |
1322 | 0 | ok = 1; |
1323 | 0 | err: |
1324 | 0 | OPENSSL_free(tag); |
1325 | 0 | return ok; |
1326 | 0 | } |
1327 | | |
1328 | | /* |
1329 | | * Get RecipientInfo type (if any) supported by a key (public or private). To |
1330 | | * retain compatibility with previous behaviour if the ctrl value isn't |
1331 | | * supported we assume key transport. |
1332 | | */ |
1333 | | int ossl_cms_pkey_get_ri_type(EVP_PKEY *pk) |
1334 | 0 | { |
1335 | | /* Check types that we know about */ |
1336 | 0 | if (EVP_PKEY_is_a(pk, "DH")) |
1337 | 0 | return CMS_RECIPINFO_AGREE; |
1338 | 0 | else if (EVP_PKEY_is_a(pk, "DHX")) |
1339 | 0 | return CMS_RECIPINFO_AGREE; |
1340 | 0 | else if (EVP_PKEY_is_a(pk, "DSA")) |
1341 | 0 | return CMS_RECIPINFO_NONE; |
1342 | 0 | else if (EVP_PKEY_is_a(pk, "EC")) |
1343 | 0 | return CMS_RECIPINFO_AGREE; |
1344 | 0 | else if (EVP_PKEY_is_a(pk, "RSA")) |
1345 | 0 | return CMS_RECIPINFO_TRANS; |
1346 | | |
1347 | | /* |
1348 | | * Otherwise this might ben an engine implementation, so see if we can get |
1349 | | * the type from the ameth. |
1350 | | */ |
1351 | 0 | if (pk->ameth && pk->ameth->pkey_ctrl) { |
1352 | 0 | int i, r; |
1353 | 0 | i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r); |
1354 | 0 | if (i > 0) |
1355 | 0 | return r; |
1356 | 0 | } |
1357 | 0 | return CMS_RECIPINFO_TRANS; |
1358 | 0 | } |
1359 | | |
1360 | | int ossl_cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type) |
1361 | 0 | { |
1362 | 0 | int supportedRiType; |
1363 | |
|
1364 | 0 | if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) { |
1365 | 0 | int i, r; |
1366 | |
|
1367 | 0 | i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED, |
1368 | 0 | ri_type, &r); |
1369 | 0 | if (i > 0) |
1370 | 0 | return r; |
1371 | 0 | } |
1372 | | |
1373 | 0 | supportedRiType = ossl_cms_pkey_get_ri_type(pk); |
1374 | 0 | if (supportedRiType < 0) |
1375 | 0 | return 0; |
1376 | | |
1377 | 0 | return (supportedRiType == ri_type); |
1378 | 0 | } |