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