1 /* crypto/cms/cms_env.c */
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
55 #include "internal/cryptlib.h"
56 #include <openssl/asn1t.h>
57 #include <openssl/pem.h>
58 #include <openssl/x509v3.h>
59 #include <openssl/err.h>
60 #include <openssl/cms.h>
61 #include <openssl/rand.h>
62 #include <openssl/aes.h>
64 #include "internal/asn1_int.h"
66 /* CMS EnvelopedData Utilities */
68 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
70 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
71 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
72 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
75 return cms->d.envelopedData;
78 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
80 if (cms->d.other == NULL) {
81 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
82 if (!cms->d.envelopedData) {
83 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE);
86 cms->d.envelopedData->version = 0;
87 cms->d.envelopedData->encryptedContentInfo->contentType =
88 OBJ_nid2obj(NID_pkcs7_data);
89 ASN1_OBJECT_free(cms->contentType);
90 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
91 return cms->d.envelopedData;
93 return cms_get0_enveloped(cms);
96 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
100 if (ri->type == CMS_RECIPINFO_TRANS)
101 pkey = ri->d.ktri->pkey;
102 else if (ri->type == CMS_RECIPINFO_AGREE) {
103 EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
106 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
111 if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
113 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
115 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
116 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
120 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
126 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
128 CMS_EnvelopedData *env;
129 env = cms_get0_enveloped(cms);
132 return env->recipientInfos;
135 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
140 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
142 if (ri->type == CMS_RECIPINFO_TRANS)
143 return ri->d.ktri->pctx;
144 else if (ri->type == CMS_RECIPINFO_AGREE)
145 return ri->d.kari->pctx;
149 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
151 CMS_ContentInfo *cms;
152 CMS_EnvelopedData *env;
153 cms = CMS_ContentInfo_new();
156 env = cms_enveloped_data_init(cms);
159 if (!cms_EncryptedContent_init(env->encryptedContentInfo,
164 CMS_ContentInfo_free(cms);
165 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
169 /* Key Transport Recipient Info (KTRI) routines */
171 /* Initialise a ktri based on passed certificate and key */
173 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
174 EVP_PKEY *pk, unsigned int flags)
176 CMS_KeyTransRecipientInfo *ktri;
179 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
182 ri->type = CMS_RECIPINFO_TRANS;
186 if (flags & CMS_USE_KEYID) {
188 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
191 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
195 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
199 if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype))
203 CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY);
207 if (flags & CMS_KEY_PARAM) {
208 ktri->pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
209 if (ktri->pctx == NULL)
211 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
213 } else if (!cms_env_asn1_ctrl(ri, 0))
219 * Add a recipient certificate using appropriate type of RecipientInfo
222 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
223 X509 *recip, unsigned int flags)
225 CMS_RecipientInfo *ri = NULL;
226 CMS_EnvelopedData *env;
228 env = cms_get0_enveloped(cms);
232 /* Initialize recipient info */
233 ri = M_ASN1_new_of(CMS_RecipientInfo);
237 pk = X509_get0_pubkey(recip);
239 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, CMS_R_ERROR_GETTING_PUBLIC_KEY);
243 switch (cms_pkey_get_ri_type(pk)) {
245 case CMS_RECIPINFO_TRANS:
246 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags))
250 case CMS_RECIPINFO_AGREE:
251 if (!cms_RecipientInfo_kari_init(ri, recip, pk, flags))
256 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
257 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
262 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
268 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
270 M_ASN1_free_of(ri, CMS_RecipientInfo);
275 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
276 EVP_PKEY **pk, X509 **recip,
279 CMS_KeyTransRecipientInfo *ktri;
280 if (ri->type != CMS_RECIPINFO_TRANS) {
281 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
282 CMS_R_NOT_KEY_TRANSPORT);
291 *recip = ktri->recip;
293 *palg = ktri->keyEncryptionAlgorithm;
297 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
298 ASN1_OCTET_STRING **keyid,
302 CMS_KeyTransRecipientInfo *ktri;
303 if (ri->type != CMS_RECIPINFO_TRANS) {
304 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
305 CMS_R_NOT_KEY_TRANSPORT);
310 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
313 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
315 if (ri->type != CMS_RECIPINFO_TRANS) {
316 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
317 CMS_R_NOT_KEY_TRANSPORT);
320 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
323 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
325 if (ri->type != CMS_RECIPINFO_TRANS) {
326 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
329 ri->d.ktri->pkey = pkey;
333 /* Encrypt content key in key transport recipient info */
335 static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
336 CMS_RecipientInfo *ri)
338 CMS_KeyTransRecipientInfo *ktri;
339 CMS_EncryptedContentInfo *ec;
341 unsigned char *ek = NULL;
346 if (ri->type != CMS_RECIPINFO_TRANS) {
347 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_NOT_KEY_TRANSPORT);
351 ec = cms->d.envelopedData->encryptedContentInfo;
356 if (!cms_env_asn1_ctrl(ri, 0))
359 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
363 if (EVP_PKEY_encrypt_init(pctx) <= 0)
367 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
368 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
369 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
373 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
376 ek = OPENSSL_malloc(eklen);
379 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
383 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
386 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
392 EVP_PKEY_CTX_free(pctx);
399 /* Decrypt content key from KTRI */
401 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
402 CMS_RecipientInfo *ri)
404 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
405 EVP_PKEY *pkey = ktri->pkey;
406 unsigned char *ek = NULL;
409 CMS_EncryptedContentInfo *ec;
410 ec = cms->d.envelopedData->encryptedContentInfo;
412 if (ktri->pkey == NULL) {
413 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY);
417 ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL);
418 if (ktri->pctx == NULL)
421 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
424 if (!cms_env_asn1_ctrl(ri, 1))
427 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
428 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
429 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
433 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
434 ktri->encryptedKey->data,
435 ktri->encryptedKey->length) <= 0)
438 ek = OPENSSL_malloc(eklen);
441 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE);
445 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
446 ktri->encryptedKey->data,
447 ktri->encryptedKey->length) <= 0) {
448 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
454 OPENSSL_clear_free(ec->key, ec->keylen);
459 EVP_PKEY_CTX_free(ktri->pctx);
467 /* Key Encrypted Key (KEK) RecipientInfo routines */
469 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
470 const unsigned char *id, size_t idlen)
472 ASN1_OCTET_STRING tmp_os;
473 CMS_KEKRecipientInfo *kekri;
474 if (ri->type != CMS_RECIPINFO_KEK) {
475 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
479 tmp_os.type = V_ASN1_OCTET_STRING;
481 tmp_os.data = (unsigned char *)id;
482 tmp_os.length = (int)idlen;
483 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
486 /* For now hard code AES key wrap info */
488 static size_t aes_wrap_keylen(int nid)
491 case NID_id_aes128_wrap:
494 case NID_id_aes192_wrap:
497 case NID_id_aes256_wrap:
505 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
506 unsigned char *key, size_t keylen,
507 unsigned char *id, size_t idlen,
508 ASN1_GENERALIZEDTIME *date,
509 ASN1_OBJECT *otherTypeId,
510 ASN1_TYPE *otherType)
512 CMS_RecipientInfo *ri = NULL;
513 CMS_EnvelopedData *env;
514 CMS_KEKRecipientInfo *kekri;
515 env = cms_get0_enveloped(cms);
519 if (nid == NID_undef) {
522 nid = NID_id_aes128_wrap;
526 nid = NID_id_aes192_wrap;
530 nid = NID_id_aes256_wrap;
534 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
540 size_t exp_keylen = aes_wrap_keylen(nid);
543 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
544 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
548 if (keylen != exp_keylen) {
549 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
555 /* Initialize recipient info */
556 ri = M_ASN1_new_of(CMS_RecipientInfo);
560 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
563 ri->type = CMS_RECIPINFO_KEK;
568 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
569 if (kekri->kekid->other == NULL)
573 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
576 /* After this point no calls can fail */
581 kekri->keylen = keylen;
583 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
585 kekri->kekid->date = date;
587 if (kekri->kekid->other) {
588 kekri->kekid->other->keyAttrId = otherTypeId;
589 kekri->kekid->other->keyAttr = otherType;
592 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
593 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
598 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
600 M_ASN1_free_of(ri, CMS_RecipientInfo);
605 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
607 ASN1_OCTET_STRING **pid,
608 ASN1_GENERALIZEDTIME **pdate,
609 ASN1_OBJECT **potherid,
610 ASN1_TYPE **pothertype)
612 CMS_KEKIdentifier *rkid;
613 if (ri->type != CMS_RECIPINFO_KEK) {
614 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
617 rkid = ri->d.kekri->kekid;
619 *palg = ri->d.kekri->keyEncryptionAlgorithm;
621 *pid = rkid->keyIdentifier;
626 *potherid = rkid->other->keyAttrId;
632 *pothertype = rkid->other->keyAttr;
639 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
640 unsigned char *key, size_t keylen)
642 CMS_KEKRecipientInfo *kekri;
643 if (ri->type != CMS_RECIPINFO_KEK) {
644 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
650 kekri->keylen = keylen;
654 /* Encrypt content key in KEK recipient info */
656 static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
657 CMS_RecipientInfo *ri)
659 CMS_EncryptedContentInfo *ec;
660 CMS_KEKRecipientInfo *kekri;
662 unsigned char *wkey = NULL;
666 ec = cms->d.envelopedData->encryptedContentInfo;
671 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
675 if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
676 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
677 CMS_R_ERROR_SETTING_KEY);
681 wkey = OPENSSL_malloc(ec->keylen + 8);
684 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
688 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
691 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
695 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
703 OPENSSL_cleanse(&actx, sizeof(actx));
709 /* Decrypt content key in KEK recipient info */
711 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
712 CMS_RecipientInfo *ri)
714 CMS_EncryptedContentInfo *ec;
715 CMS_KEKRecipientInfo *kekri;
717 unsigned char *ukey = NULL;
721 ec = cms->d.envelopedData->encryptedContentInfo;
726 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
730 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
731 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
732 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
733 CMS_R_INVALID_KEY_LENGTH);
737 /* If encrypted key length is invalid don't bother */
739 if (kekri->encryptedKey->length < 16) {
740 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
741 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
745 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
746 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
747 CMS_R_ERROR_SETTING_KEY);
751 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
754 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
758 ukeylen = AES_unwrap_key(&actx, NULL, ukey,
759 kekri->encryptedKey->data,
760 kekri->encryptedKey->length);
763 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
768 ec->keylen = ukeylen;
776 OPENSSL_cleanse(&actx, sizeof(actx));
782 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
785 case CMS_RECIPINFO_TRANS:
786 return cms_RecipientInfo_ktri_decrypt(cms, ri);
788 case CMS_RECIPINFO_KEK:
789 return cms_RecipientInfo_kekri_decrypt(cms, ri);
791 case CMS_RECIPINFO_PASS:
792 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
795 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
796 CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
801 int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
804 case CMS_RECIPINFO_TRANS:
805 return cms_RecipientInfo_ktri_encrypt(cms, ri);
807 case CMS_RECIPINFO_AGREE:
808 return cms_RecipientInfo_kari_encrypt(cms, ri);
810 case CMS_RECIPINFO_KEK:
811 return cms_RecipientInfo_kekri_encrypt(cms, ri);
813 case CMS_RECIPINFO_PASS:
814 return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
817 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
818 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
823 /* Check structures and fixup version numbers (if necessary) */
825 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
827 CMS_OriginatorInfo *org = env->originatorInfo;
831 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
832 CMS_CertificateChoices *cch;
833 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
834 if (cch->type == CMS_CERTCHOICE_OTHER) {
837 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
838 if (env->version < 3)
843 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
844 CMS_RevocationInfoChoice *rch;
845 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
846 if (rch->type == CMS_REVCHOICE_OTHER) {
853 static void cms_env_set_version(CMS_EnvelopedData *env)
856 CMS_RecipientInfo *ri;
859 * Can't set version higher than 4 so if 4 or more already nothing to do.
861 if (env->version >= 4)
864 cms_env_set_originfo_version(env);
866 if (env->version >= 3)
869 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
870 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
871 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
874 } else if (ri->type != CMS_RECIPINFO_TRANS
875 || ri->d.ktri->version != 0) {
879 if (env->version == 2)
881 if (env->originatorInfo || env->unprotectedAttrs)
886 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
888 CMS_EncryptedContentInfo *ec;
889 STACK_OF(CMS_RecipientInfo) *rinfos;
890 CMS_RecipientInfo *ri;
894 /* Get BIO first to set up key */
896 ec = cms->d.envelopedData->encryptedContentInfo;
897 ret = cms_EncryptedContent_init_bio(ec);
899 /* If error or no cipher end of processing */
901 if (!ret || !ec->cipher)
904 /* Now encrypt content key according to each RecipientInfo type */
906 rinfos = cms->d.envelopedData->recipientInfos;
908 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
909 ri = sk_CMS_RecipientInfo_value(rinfos, i);
910 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {
911 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
912 CMS_R_ERROR_SETTING_RECIPIENTINFO);
916 cms_env_set_version(cms->d.envelopedData);
922 OPENSSL_clear_free(ec->key, ec->keylen);
933 * Get RecipientInfo type (if any) supported by a key (public or private). To
934 * retain compatibility with previous behaviour if the ctrl value isn't
935 * supported we assume key transport.
937 int cms_pkey_get_ri_type(EVP_PKEY *pk)
939 if (pk->ameth && pk->ameth->pkey_ctrl) {
941 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
945 return CMS_RECIPINFO_TRANS;