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