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>
63 #include "asn1_locl.h"
65 /* CMS EnvelopedData Utilities */
67 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
68 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
69 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
70 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
71 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
73 DECLARE_STACK_OF(CMS_RecipientInfo)
75 static CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
77 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped)
79 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
80 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
83 return cms->d.envelopedData;
86 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
88 if (cms->d.other == NULL)
90 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
91 if (!cms->d.envelopedData)
93 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT,
94 ERR_R_MALLOC_FAILURE);
97 cms->d.envelopedData->version = 0;
98 cms->d.envelopedData->encryptedContentInfo->contentType =
99 OBJ_nid2obj(NID_pkcs7_data);
100 ASN1_OBJECT_free(cms->contentType);
101 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
102 return cms->d.envelopedData;
104 return cms_get0_enveloped(cms);
107 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
109 CMS_EnvelopedData *env;
110 env = cms_get0_enveloped(cms);
113 return env->recipientInfos;
116 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
121 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
123 CMS_ContentInfo *cms;
124 CMS_EnvelopedData *env;
125 cms = CMS_ContentInfo_new();
128 env = cms_enveloped_data_init(cms);
131 if (!cms_EncryptedContent_init(env->encryptedContentInfo,
137 CMS_ContentInfo_free(cms);
138 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
142 /* Key Transport Recipient Info (KTRI) routines */
144 /* Add a recipient certificate. For now only handle key transport.
145 * If we ever handle key agreement will need updating.
148 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
149 X509 *recip, unsigned int flags)
151 CMS_RecipientInfo *ri = NULL;
152 CMS_KeyTransRecipientInfo *ktri;
153 CMS_EnvelopedData *env;
156 env = cms_get0_enveloped(cms);
160 /* Initialize recipient info */
161 ri = M_ASN1_new_of(CMS_RecipientInfo);
165 /* Initialize and add key transport recipient info */
167 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
170 ri->type = CMS_RECIPINFO_TRANS;
174 X509_check_purpose(recip, -1, -1);
175 pk = X509_get_pubkey(recip);
178 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
179 CMS_R_ERROR_GETTING_PUBLIC_KEY);
182 CRYPTO_add(&recip->references, 1, CRYPTO_LOCK_X509);
186 if (flags & CMS_USE_KEYID)
189 type = CMS_RECIPINFO_KEYIDENTIFIER;
194 type = CMS_RECIPINFO_ISSUER_SERIAL;
197 /* Not a typo: RecipientIdentifier and SignerIdentifier are the
201 if (!cms_set1_SignerIdentifier(ktri->rid, recip, type))
204 if (pk->ameth && pk->ameth->pkey_ctrl)
206 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_ENVELOPE,
210 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
211 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
216 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
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 EVP_PKEY_CTX *pctx = NULL;
307 unsigned char *ek = NULL;
312 if (ri->type != CMS_RECIPINFO_TRANS)
314 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
315 CMS_R_NOT_KEY_TRANSPORT);
319 ec = cms->d.envelopedData->encryptedContentInfo;
321 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
325 if (EVP_PKEY_encrypt_init(pctx) <= 0)
328 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
329 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0)
331 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
335 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
338 ek = OPENSSL_malloc(eklen);
342 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
343 ERR_R_MALLOC_FAILURE);
347 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
350 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
357 EVP_PKEY_CTX_free(pctx);
364 /* Decrypt content key from KTRI */
366 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
367 CMS_RecipientInfo *ri)
369 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
370 EVP_PKEY_CTX *pctx = NULL;
371 unsigned char *ek = NULL;
375 if (ktri->pkey == NULL)
377 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
378 CMS_R_NO_PRIVATE_KEY);
382 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
386 if (EVP_PKEY_decrypt_init(pctx) <= 0)
389 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
390 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0)
392 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
396 if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
397 ktri->encryptedKey->data,
398 ktri->encryptedKey->length) <= 0)
401 ek = OPENSSL_malloc(eklen);
405 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
406 ERR_R_MALLOC_FAILURE);
410 if (EVP_PKEY_decrypt(pctx, ek, &eklen,
411 ktri->encryptedKey->data,
412 ktri->encryptedKey->length) <= 0)
414 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
420 cms->d.envelopedData->encryptedContentInfo->key = ek;
421 cms->d.envelopedData->encryptedContentInfo->keylen = eklen;
425 EVP_PKEY_CTX_free(pctx);
432 /* Key Encrypted Key (KEK) RecipientInfo routines */
434 /* For now hard code AES key wrap info */
436 static size_t aes_wrap_keylen(int nid)
440 case NID_id_aes128_wrap:
443 case NID_id_aes192_wrap:
446 case NID_id_aes256_wrap:
454 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
455 unsigned char *key, size_t keylen,
456 unsigned char *id, size_t idlen,
457 ASN1_GENERALIZEDTIME *date,
458 ASN1_OBJECT *otherTypeId,
459 ASN1_TYPE *otherType)
461 CMS_RecipientInfo *ri = NULL;
462 CMS_EnvelopedData *env;
463 CMS_KEKRecipientInfo *kekri;
464 env = cms_get0_enveloped(cms);
468 if (nid == NID_undef)
473 nid = NID_id_aes128_wrap;
477 nid = NID_id_aes192_wrap;
481 nid = NID_id_aes256_wrap;
485 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
486 CMS_R_INVALID_KEY_LENGTH);
494 size_t exp_keylen = aes_wrap_keylen(nid);
498 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
499 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
503 if (keylen != exp_keylen)
505 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
506 CMS_R_INVALID_KEY_LENGTH);
512 /* Initialize recipient info */
513 ri = M_ASN1_new_of(CMS_RecipientInfo);
517 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
520 ri->type = CMS_RECIPINFO_KEK;
526 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
527 if (kekri->kekid->other == NULL)
531 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
535 /* After this point no calls can fail */
540 kekri->keylen = keylen;
542 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
544 kekri->kekid->date = date;
546 if (kekri->kekid->other)
548 kekri->kekid->other->keyAttrId = otherTypeId;
549 kekri->kekid->other->keyAttr = otherType;
552 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
553 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
558 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
561 M_ASN1_free_of(ri, CMS_RecipientInfo);
566 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
568 ASN1_OCTET_STRING **pid,
569 ASN1_GENERALIZEDTIME **pdate,
570 ASN1_OBJECT **potherid,
571 ASN1_TYPE **pothertype)
573 CMS_KEKIdentifier *rkid;
574 if (ri->type != CMS_RECIPINFO_KEK)
576 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
579 rkid = ri->d.kekri->kekid;
581 *palg = ri->d.kekri->keyEncryptionAlgorithm;
583 *pid = rkid->keyIdentifier;
589 *potherid = rkid->other->keyAttrId;
596 *pothertype = rkid->other->keyAttr;
604 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
605 unsigned char *key, size_t keylen)
607 CMS_KEKRecipientInfo *kekri;
609 if (ri->type != CMS_RECIPINFO_KEK)
611 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
615 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
616 if (aes_wrap_keylen(wrap_nid) != keylen)
618 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY,
619 CMS_R_INVALID_KEY_LENGTH);
623 kekri->keylen = keylen;
628 /* Encrypt content key in KEK recipient info */
630 static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
631 CMS_RecipientInfo *ri)
633 CMS_EncryptedContentInfo *ec;
634 CMS_KEKRecipientInfo *kekri;
636 unsigned char *wkey = NULL;
640 ec = cms->d.envelopedData->encryptedContentInfo;
646 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
650 if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx))
652 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
653 CMS_R_ERROR_SETTING_KEY);
657 wkey = OPENSSL_malloc(ec->keylen + 8);
661 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
662 ERR_R_MALLOC_FAILURE);
666 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
670 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
674 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
682 OPENSSL_cleanse(&actx, sizeof(actx));
688 /* Decrypt content key in KEK recipient info */
690 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
691 CMS_RecipientInfo *ri)
693 CMS_EncryptedContentInfo *ec;
694 CMS_KEKRecipientInfo *kekri;
696 unsigned char *ukey = NULL;
700 ec = cms->d.envelopedData->encryptedContentInfo;
706 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
710 /* If encrypted key length is invalid don't bother */
712 if (kekri->encryptedKey->length < 16)
714 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
715 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
719 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx))
721 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
722 CMS_R_ERROR_SETTING_KEY);
726 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
730 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
731 ERR_R_MALLOC_FAILURE);
735 ukeylen = AES_unwrap_key(&actx, NULL, ukey,
736 kekri->encryptedKey->data,
737 kekri->encryptedKey->length);
741 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
747 ec->keylen = ukeylen;
755 OPENSSL_cleanse(&actx, sizeof(actx));
761 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
765 case CMS_RECIPINFO_TRANS:
766 return cms_RecipientInfo_ktri_decrypt(cms, ri);
768 case CMS_RECIPINFO_KEK:
769 return cms_RecipientInfo_kekri_decrypt(cms, ri);
772 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
773 CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
778 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
780 CMS_EncryptedContentInfo *ec;
781 STACK_OF(CMS_RecipientInfo) *rinfos;
782 CMS_RecipientInfo *ri;
786 /* Get BIO first to set up key */
788 ec = cms->d.envelopedData->encryptedContentInfo;
789 ret = cms_EncryptedContent_init_bio(ec);
791 /* If error or no cipher end of processing */
793 if (!ret || !ec->cipher)
796 /* Now encrypt content key according to each RecipientInfo type */
798 rinfos = cms->d.envelopedData->recipientInfos;
800 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++)
802 ri = sk_CMS_RecipientInfo_value(rinfos, i);
806 case CMS_RECIPINFO_TRANS:
807 r = cms_RecipientInfo_ktri_encrypt(cms, ri);
810 case CMS_RECIPINFO_KEK:
811 r = cms_RecipientInfo_kekri_encrypt(cms, ri);
815 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
816 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
822 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
823 CMS_R_ERROR_SETTING_RECIPIENTINFO);
834 OPENSSL_cleanse(ec->key, ec->keylen);
835 OPENSSL_free(ec->key);