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 * ====================================================================
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 "asn1_locl.h"
66 /* CMS EnvelopedData Utilities */
68 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
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 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
77 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) {
88 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
89 if (!cms->d.envelopedData) {
90 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE);
93 cms->d.envelopedData->version = 0;
94 cms->d.envelopedData->encryptedContentInfo->contentType =
95 OBJ_nid2obj(NID_pkcs7_data);
96 ASN1_OBJECT_free(cms->contentType);
97 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
98 return cms->d.envelopedData;
100 return cms_get0_enveloped(cms);
103 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
105 CMS_EnvelopedData *env;
106 env = cms_get0_enveloped(cms);
109 return env->recipientInfos;
112 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
117 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
119 CMS_ContentInfo *cms;
120 CMS_EnvelopedData *env;
121 cms = CMS_ContentInfo_new();
124 env = cms_enveloped_data_init(cms);
127 if (!cms_EncryptedContent_init(env->encryptedContentInfo,
133 CMS_ContentInfo_free(cms);
134 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
138 /* Key Transport Recipient Info (KTRI) routines */
141 * Add a recipient certificate. For now only handle key transport. If we ever
142 * handle key agreement will need updating.
145 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
146 X509 *recip, unsigned int flags)
148 CMS_RecipientInfo *ri = NULL;
149 CMS_KeyTransRecipientInfo *ktri;
150 CMS_EnvelopedData *env;
153 env = cms_get0_enveloped(cms);
157 /* Initialize recipient info */
158 ri = M_ASN1_new_of(CMS_RecipientInfo);
162 /* Initialize and add key transport recipient info */
164 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
167 ri->type = CMS_RECIPINFO_TRANS;
171 X509_check_purpose(recip, -1, -1);
172 pk = X509_get_pubkey(recip);
174 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, CMS_R_ERROR_GETTING_PUBLIC_KEY);
177 CRYPTO_add(&recip->references, 1, CRYPTO_LOCK_X509);
181 if (flags & CMS_USE_KEYID) {
183 if (env->version < 2)
185 type = CMS_RECIPINFO_KEYIDENTIFIER;
188 type = CMS_RECIPINFO_ISSUER_SERIAL;
192 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
196 if (!cms_set1_SignerIdentifier(ktri->rid, recip, type))
199 if (pk->ameth && pk->ameth->pkey_ctrl) {
200 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_ENVELOPE, 0, ri);
202 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
203 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
207 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, CMS_R_CTRL_FAILURE);
212 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
218 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
221 M_ASN1_free_of(ri, CMS_RecipientInfo);
226 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
227 EVP_PKEY **pk, X509 **recip,
230 CMS_KeyTransRecipientInfo *ktri;
231 if (ri->type != CMS_RECIPINFO_TRANS) {
232 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
233 CMS_R_NOT_KEY_TRANSPORT);
242 *recip = ktri->recip;
244 *palg = ktri->keyEncryptionAlgorithm;
248 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
249 ASN1_OCTET_STRING **keyid,
253 CMS_KeyTransRecipientInfo *ktri;
254 if (ri->type != CMS_RECIPINFO_TRANS) {
255 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
256 CMS_R_NOT_KEY_TRANSPORT);
261 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
264 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
266 if (ri->type != CMS_RECIPINFO_TRANS) {
267 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
268 CMS_R_NOT_KEY_TRANSPORT);
271 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
274 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
276 if (ri->type != CMS_RECIPINFO_TRANS) {
277 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
280 ri->d.ktri->pkey = pkey;
284 /* Encrypt content key in key transport recipient info */
286 static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
287 CMS_RecipientInfo *ri)
289 CMS_KeyTransRecipientInfo *ktri;
290 CMS_EncryptedContentInfo *ec;
291 EVP_PKEY_CTX *pctx = NULL;
292 unsigned char *ek = NULL;
297 if (ri->type != CMS_RECIPINFO_TRANS) {
298 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_NOT_KEY_TRANSPORT);
302 ec = cms->d.envelopedData->encryptedContentInfo;
304 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
308 if (EVP_PKEY_encrypt_init(pctx) <= 0)
311 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
312 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
313 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
317 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
320 ek = OPENSSL_malloc(eklen);
323 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
327 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
330 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
337 EVP_PKEY_CTX_free(pctx);
344 /* Decrypt content key from KTRI */
346 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
347 CMS_RecipientInfo *ri)
349 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
350 EVP_PKEY_CTX *pctx = NULL;
351 unsigned char *ek = NULL;
354 CMS_EncryptedContentInfo *ec;
355 ec = cms->d.envelopedData->encryptedContentInfo;
357 if (ktri->pkey == NULL) {
358 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY);
362 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
366 if (EVP_PKEY_decrypt_init(pctx) <= 0)
369 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
370 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
371 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
375 if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
376 ktri->encryptedKey->data,
377 ktri->encryptedKey->length) <= 0)
380 ek = OPENSSL_malloc(eklen);
383 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE);
387 if (EVP_PKEY_decrypt(pctx, ek, &eklen,
388 ktri->encryptedKey->data,
389 ktri->encryptedKey->length) <= 0) {
390 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
397 OPENSSL_cleanse(ec->key, ec->keylen);
398 OPENSSL_free(ec->key);
406 EVP_PKEY_CTX_free(pctx);
413 /* Key Encrypted Key (KEK) RecipientInfo routines */
415 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
416 const unsigned char *id, size_t idlen)
418 ASN1_OCTET_STRING tmp_os;
419 CMS_KEKRecipientInfo *kekri;
420 if (ri->type != CMS_RECIPINFO_KEK) {
421 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
425 tmp_os.type = V_ASN1_OCTET_STRING;
427 tmp_os.data = (unsigned char *)id;
428 tmp_os.length = (int)idlen;
429 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
432 /* For now hard code AES key wrap info */
434 static size_t aes_wrap_keylen(int nid)
437 case NID_id_aes128_wrap:
440 case NID_id_aes192_wrap:
443 case NID_id_aes256_wrap:
451 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
452 unsigned char *key, size_t keylen,
453 unsigned char *id, size_t idlen,
454 ASN1_GENERALIZEDTIME *date,
455 ASN1_OBJECT *otherTypeId,
456 ASN1_TYPE *otherType)
458 CMS_RecipientInfo *ri = NULL;
459 CMS_EnvelopedData *env;
460 CMS_KEKRecipientInfo *kekri;
461 env = cms_get0_enveloped(cms);
465 if (nid == NID_undef) {
468 nid = NID_id_aes128_wrap;
472 nid = NID_id_aes192_wrap;
476 nid = NID_id_aes256_wrap;
480 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
486 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) {
495 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
501 /* Initialize recipient info */
502 ri = M_ASN1_new_of(CMS_RecipientInfo);
506 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
509 ri->type = CMS_RECIPINFO_KEK;
514 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
515 if (kekri->kekid->other == NULL)
519 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
522 /* After this point no calls can fail */
527 kekri->keylen = keylen;
529 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
531 kekri->kekid->date = date;
533 if (kekri->kekid->other) {
534 kekri->kekid->other->keyAttrId = otherTypeId;
535 kekri->kekid->other->keyAttr = otherType;
538 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
539 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
544 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
547 M_ASN1_free_of(ri, CMS_RecipientInfo);
552 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
554 ASN1_OCTET_STRING **pid,
555 ASN1_GENERALIZEDTIME **pdate,
556 ASN1_OBJECT **potherid,
557 ASN1_TYPE **pothertype)
559 CMS_KEKIdentifier *rkid;
560 if (ri->type != CMS_RECIPINFO_KEK) {
561 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
564 rkid = ri->d.kekri->kekid;
566 *palg = ri->d.kekri->keyEncryptionAlgorithm;
568 *pid = rkid->keyIdentifier;
573 *potherid = rkid->other->keyAttrId;
579 *pothertype = rkid->other->keyAttr;
586 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
587 unsigned char *key, size_t keylen)
589 CMS_KEKRecipientInfo *kekri;
590 if (ri->type != CMS_RECIPINFO_KEK) {
591 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
597 kekri->keylen = keylen;
601 /* Encrypt content key in KEK recipient info */
603 static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
604 CMS_RecipientInfo *ri)
606 CMS_EncryptedContentInfo *ec;
607 CMS_KEKRecipientInfo *kekri;
609 unsigned char *wkey = NULL;
613 ec = cms->d.envelopedData->encryptedContentInfo;
618 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
622 if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
623 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
624 CMS_R_ERROR_SETTING_KEY);
628 wkey = OPENSSL_malloc(ec->keylen + 8);
631 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
635 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
638 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
642 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
650 OPENSSL_cleanse(&actx, sizeof(actx));
656 /* Decrypt content key in KEK recipient info */
658 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
659 CMS_RecipientInfo *ri)
661 CMS_EncryptedContentInfo *ec;
662 CMS_KEKRecipientInfo *kekri;
664 unsigned char *ukey = NULL;
668 ec = cms->d.envelopedData->encryptedContentInfo;
673 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
677 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
678 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
679 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
680 CMS_R_INVALID_KEY_LENGTH);
684 /* If encrypted key length is invalid don't bother */
686 if (kekri->encryptedKey->length < 16) {
687 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
688 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
692 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
693 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
694 CMS_R_ERROR_SETTING_KEY);
698 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
701 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
705 ukeylen = AES_unwrap_key(&actx, NULL, ukey,
706 kekri->encryptedKey->data,
707 kekri->encryptedKey->length);
710 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
715 ec->keylen = ukeylen;
723 OPENSSL_cleanse(&actx, sizeof(actx));
729 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
732 case CMS_RECIPINFO_TRANS:
733 return cms_RecipientInfo_ktri_decrypt(cms, ri);
735 case CMS_RECIPINFO_KEK:
736 return cms_RecipientInfo_kekri_decrypt(cms, ri);
738 case CMS_RECIPINFO_PASS:
739 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
742 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
743 CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
748 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
750 CMS_EncryptedContentInfo *ec;
751 STACK_OF(CMS_RecipientInfo) *rinfos;
752 CMS_RecipientInfo *ri;
756 /* Get BIO first to set up key */
758 ec = cms->d.envelopedData->encryptedContentInfo;
759 ret = cms_EncryptedContent_init_bio(ec);
761 /* If error or no cipher end of processing */
763 if (!ret || !ec->cipher)
766 /* Now encrypt content key according to each RecipientInfo type */
768 rinfos = cms->d.envelopedData->recipientInfos;
770 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
771 ri = sk_CMS_RecipientInfo_value(rinfos, i);
774 case CMS_RECIPINFO_TRANS:
775 r = cms_RecipientInfo_ktri_encrypt(cms, ri);
778 case CMS_RECIPINFO_KEK:
779 r = cms_RecipientInfo_kekri_encrypt(cms, ri);
782 case CMS_RECIPINFO_PASS:
783 r = cms_RecipientInfo_pwri_crypt(cms, ri, 1);
787 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
788 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
793 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
794 CMS_R_ERROR_SETTING_RECIPIENTINFO);
804 OPENSSL_cleanse(ec->key, ec->keylen);
805 OPENSSL_free(ec->key);