1 /* crypto/cms/cms_env.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
55 #include <openssl/asn1t.h>
56 #include <openssl/pem.h>
57 #include <openssl/x509v3.h>
58 #include <openssl/err.h>
59 #include <openssl/cms.h>
60 #include <openssl/rand.h>
61 #include <openssl/aes.h>
64 /* CMS EnvelopedData Utilities */
66 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
67 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
68 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
69 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
70 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
72 DECLARE_STACK_OF(CMS_RecipientInfo)
74 static CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
76 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped)
78 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
79 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
82 return cms->d.envelopedData;
85 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
87 if (cms->d.other == NULL)
89 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
90 if (!cms->d.envelopedData)
92 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT,
93 ERR_R_MALLOC_FAILURE);
96 cms->d.envelopedData->version = 0;
97 cms->d.envelopedData->encryptedContentInfo->contentType =
98 OBJ_nid2obj(NID_pkcs7_data);
99 ASN1_OBJECT_free(cms->contentType);
100 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
101 return cms->d.envelopedData;
103 return cms_get0_enveloped(cms);
106 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
108 CMS_EnvelopedData *env;
109 env = cms_get0_enveloped(cms);
112 return env->recipientInfos;
115 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
120 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
122 CMS_ContentInfo *cms;
123 CMS_EnvelopedData *env;
124 cms = CMS_ContentInfo_new();
127 env = cms_enveloped_data_init(cms);
130 if (!cms_EncryptedContent_init(env->encryptedContentInfo,
136 CMS_ContentInfo_free(cms);
137 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
141 /* Key Transport Recipient Info (KTRI) routines */
143 /* Add a recipient certificate. For now only handle key transport.
144 * If we ever handle key agreement will need updating.
147 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
148 X509 *recip, unsigned int flags)
150 CMS_RecipientInfo *ri = NULL;
151 CMS_KeyTransRecipientInfo *ktri;
152 CMS_EnvelopedData *env;
155 env = cms_get0_enveloped(cms);
159 /* Initialize recipient info */
160 ri = M_ASN1_new_of(CMS_RecipientInfo);
164 /* Initialize and add key transport recipient info */
166 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
169 ri->type = CMS_RECIPINFO_TRANS;
173 X509_check_purpose(recip, -1, -1);
174 pk = X509_get_pubkey(recip);
177 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
178 CMS_R_ERROR_GETTING_PUBLIC_KEY);
181 CRYPTO_add(&recip->references, 1, CRYPTO_LOCK_X509);
185 if (flags & CMS_USE_KEYID)
188 if (env->version < 2)
190 type = CMS_RECIPINFO_KEYIDENTIFIER;
195 type = CMS_RECIPINFO_ISSUER_SERIAL;
198 /* Not a typo: RecipientIdentifier and SignerIdentifier are the
202 if (!cms_set1_SignerIdentifier(ktri->rid, recip, type))
205 /* Since we have no EVP_PKEY_ASN1_METHOD in OpenSSL 0.9.8,
206 * hard code algorithm parameters.
209 if (pk->type == EVP_PKEY_RSA)
211 X509_ALGOR_set0(ktri->keyEncryptionAlgorithm,
212 OBJ_nid2obj(NID_rsaEncryption),
217 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
218 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
222 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
228 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
231 M_ASN1_free_of(ri, CMS_RecipientInfo);
236 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
237 EVP_PKEY **pk, X509 **recip,
240 CMS_KeyTransRecipientInfo *ktri;
241 if (ri->type != CMS_RECIPINFO_TRANS)
243 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
244 CMS_R_NOT_KEY_TRANSPORT);
253 *recip = ktri->recip;
255 *palg = ktri->keyEncryptionAlgorithm;
259 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
260 ASN1_OCTET_STRING **keyid,
261 X509_NAME **issuer, ASN1_INTEGER **sno)
263 CMS_KeyTransRecipientInfo *ktri;
264 if (ri->type != CMS_RECIPINFO_TRANS)
266 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
267 CMS_R_NOT_KEY_TRANSPORT);
272 return cms_SignerIdentifier_get0_signer_id(ktri->rid,
276 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
278 if (ri->type != CMS_RECIPINFO_TRANS)
280 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
281 CMS_R_NOT_KEY_TRANSPORT);
284 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
287 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
289 if (ri->type != CMS_RECIPINFO_TRANS)
291 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY,
292 CMS_R_NOT_KEY_TRANSPORT);
295 ri->d.ktri->pkey = pkey;
299 /* Encrypt content key in key transport recipient info */
301 static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
302 CMS_RecipientInfo *ri)
304 CMS_KeyTransRecipientInfo *ktri;
305 CMS_EncryptedContentInfo *ec;
306 unsigned char *ek = NULL;
311 if (ri->type != CMS_RECIPINFO_TRANS)
313 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
314 CMS_R_NOT_KEY_TRANSPORT);
318 ec = cms->d.envelopedData->encryptedContentInfo;
320 eklen = EVP_PKEY_size(ktri->pkey);
322 ek = OPENSSL_malloc(eklen);
326 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
327 ERR_R_MALLOC_FAILURE);
331 eklen = EVP_PKEY_encrypt(ek, ec->key, ec->keylen, ktri->pkey);
336 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
348 /* Decrypt content key from KTRI */
350 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
351 CMS_RecipientInfo *ri)
353 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
354 unsigned char *ek = NULL;
357 CMS_EncryptedContentInfo *ec;
358 ec = cms->d.envelopedData->encryptedContentInfo;
360 if (ktri->pkey == NULL)
362 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
363 CMS_R_NO_PRIVATE_KEY);
367 eklen = EVP_PKEY_size(ktri->pkey);
369 ek = OPENSSL_malloc(eklen);
373 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
374 ERR_R_MALLOC_FAILURE);
378 eklen = EVP_PKEY_decrypt(ek,
379 ktri->encryptedKey->data,
380 ktri->encryptedKey->length, ktri->pkey);
383 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
391 OPENSSL_cleanse(ec->key, ec->keylen);
392 OPENSSL_free(ec->key);
405 /* Key Encrypted Key (KEK) RecipientInfo routines */
407 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
408 const unsigned char *id, size_t idlen)
410 ASN1_OCTET_STRING tmp_os;
411 CMS_KEKRecipientInfo *kekri;
412 if (ri->type != CMS_RECIPINFO_KEK)
414 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
418 tmp_os.type = V_ASN1_OCTET_STRING;
420 tmp_os.data = (unsigned char *)id;
421 tmp_os.length = (int)idlen;
422 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
425 /* For now hard code AES key wrap info */
427 static size_t aes_wrap_keylen(int nid)
431 case NID_id_aes128_wrap:
434 case NID_id_aes192_wrap:
437 case NID_id_aes256_wrap:
445 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
446 unsigned char *key, size_t keylen,
447 unsigned char *id, size_t idlen,
448 ASN1_GENERALIZEDTIME *date,
449 ASN1_OBJECT *otherTypeId,
450 ASN1_TYPE *otherType)
452 CMS_RecipientInfo *ri = NULL;
453 CMS_EnvelopedData *env;
454 CMS_KEKRecipientInfo *kekri;
455 env = cms_get0_enveloped(cms);
459 if (nid == NID_undef)
464 nid = NID_id_aes128_wrap;
468 nid = NID_id_aes192_wrap;
472 nid = NID_id_aes256_wrap;
476 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
477 CMS_R_INVALID_KEY_LENGTH);
485 size_t exp_keylen = aes_wrap_keylen(nid);
489 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
490 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
494 if (keylen != exp_keylen)
496 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
497 CMS_R_INVALID_KEY_LENGTH);
503 /* Initialize recipient info */
504 ri = M_ASN1_new_of(CMS_RecipientInfo);
508 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
511 ri->type = CMS_RECIPINFO_KEK;
517 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
518 if (kekri->kekid->other == NULL)
522 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
526 /* After this point no calls can fail */
531 kekri->keylen = keylen;
533 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
535 kekri->kekid->date = date;
537 if (kekri->kekid->other)
539 kekri->kekid->other->keyAttrId = otherTypeId;
540 kekri->kekid->other->keyAttr = otherType;
543 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
544 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
549 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
552 M_ASN1_free_of(ri, CMS_RecipientInfo);
557 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
559 ASN1_OCTET_STRING **pid,
560 ASN1_GENERALIZEDTIME **pdate,
561 ASN1_OBJECT **potherid,
562 ASN1_TYPE **pothertype)
564 CMS_KEKIdentifier *rkid;
565 if (ri->type != CMS_RECIPINFO_KEK)
567 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
570 rkid = ri->d.kekri->kekid;
572 *palg = ri->d.kekri->keyEncryptionAlgorithm;
574 *pid = rkid->keyIdentifier;
580 *potherid = rkid->other->keyAttrId;
587 *pothertype = rkid->other->keyAttr;
594 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
595 unsigned char *key, size_t keylen)
597 CMS_KEKRecipientInfo *kekri;
598 if (ri->type != CMS_RECIPINFO_KEK)
600 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
606 kekri->keylen = keylen;
611 /* Encrypt content key in KEK recipient info */
613 static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
614 CMS_RecipientInfo *ri)
616 CMS_EncryptedContentInfo *ec;
617 CMS_KEKRecipientInfo *kekri;
619 unsigned char *wkey = NULL;
623 ec = cms->d.envelopedData->encryptedContentInfo;
629 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
633 if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx))
635 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
636 CMS_R_ERROR_SETTING_KEY);
640 wkey = OPENSSL_malloc(ec->keylen + 8);
644 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
645 ERR_R_MALLOC_FAILURE);
649 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
653 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
657 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
665 OPENSSL_cleanse(&actx, sizeof(actx));
671 /* Decrypt content key in KEK recipient info */
673 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
674 CMS_RecipientInfo *ri)
676 CMS_EncryptedContentInfo *ec;
677 CMS_KEKRecipientInfo *kekri;
679 unsigned char *ukey = NULL;
683 ec = cms->d.envelopedData->encryptedContentInfo;
689 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
693 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
694 if (aes_wrap_keylen(wrap_nid) != kekri->keylen)
696 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
697 CMS_R_INVALID_KEY_LENGTH);
701 /* If encrypted key length is invalid don't bother */
703 if (kekri->encryptedKey->length < 16)
705 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
706 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
710 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx))
712 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
713 CMS_R_ERROR_SETTING_KEY);
717 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
721 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
722 ERR_R_MALLOC_FAILURE);
726 ukeylen = AES_unwrap_key(&actx, NULL, ukey,
727 kekri->encryptedKey->data,
728 kekri->encryptedKey->length);
732 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
738 ec->keylen = ukeylen;
746 OPENSSL_cleanse(&actx, sizeof(actx));
752 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
756 case CMS_RECIPINFO_TRANS:
757 return cms_RecipientInfo_ktri_decrypt(cms, ri);
759 case CMS_RECIPINFO_KEK:
760 return cms_RecipientInfo_kekri_decrypt(cms, ri);
763 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
764 CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
769 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
771 CMS_EncryptedContentInfo *ec;
772 STACK_OF(CMS_RecipientInfo) *rinfos;
773 CMS_RecipientInfo *ri;
777 /* Get BIO first to set up key */
779 ec = cms->d.envelopedData->encryptedContentInfo;
780 ret = cms_EncryptedContent_init_bio(ec);
782 /* If error or no cipher end of processing */
784 if (!ret || !ec->cipher)
787 /* Now encrypt content key according to each RecipientInfo type */
789 rinfos = cms->d.envelopedData->recipientInfos;
791 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++)
793 ri = sk_CMS_RecipientInfo_value(rinfos, i);
797 case CMS_RECIPINFO_TRANS:
798 r = cms_RecipientInfo_ktri_encrypt(cms, ri);
801 case CMS_RECIPINFO_KEK:
802 r = cms_RecipientInfo_kekri_encrypt(cms, ri);
806 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
807 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
813 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
814 CMS_R_ERROR_SETTING_RECIPIENTINFO);
825 OPENSSL_cleanse(ec->key, ec->keylen);
826 OPENSSL_free(ec->key);