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>
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) {
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))
200 * Since we have no EVP_PKEY_ASN1_METHOD in OpenSSL 0.9.8, hard code
201 * algorithm parameters.
204 if (pk->type == EVP_PKEY_RSA) {
205 X509_ALGOR_set0(ktri->keyEncryptionAlgorithm,
206 OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
208 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
209 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
213 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
219 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
222 M_ASN1_free_of(ri, CMS_RecipientInfo);
227 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
228 EVP_PKEY **pk, X509 **recip,
231 CMS_KeyTransRecipientInfo *ktri;
232 if (ri->type != CMS_RECIPINFO_TRANS) {
233 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
234 CMS_R_NOT_KEY_TRANSPORT);
243 *recip = ktri->recip;
245 *palg = ktri->keyEncryptionAlgorithm;
249 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
250 ASN1_OCTET_STRING **keyid,
254 CMS_KeyTransRecipientInfo *ktri;
255 if (ri->type != CMS_RECIPINFO_TRANS) {
256 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
257 CMS_R_NOT_KEY_TRANSPORT);
262 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
265 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
267 if (ri->type != CMS_RECIPINFO_TRANS) {
268 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
269 CMS_R_NOT_KEY_TRANSPORT);
272 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
275 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
277 if (ri->type != CMS_RECIPINFO_TRANS) {
278 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
281 ri->d.ktri->pkey = pkey;
285 /* Encrypt content key in key transport recipient info */
287 static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
288 CMS_RecipientInfo *ri)
290 CMS_KeyTransRecipientInfo *ktri;
291 CMS_EncryptedContentInfo *ec;
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 eklen = EVP_PKEY_size(ktri->pkey);
306 ek = OPENSSL_malloc(eklen);
309 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
313 eklen = EVP_PKEY_encrypt(ek, ec->key, ec->keylen, ktri->pkey);
318 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
330 /* Decrypt content key from KTRI */
332 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
333 CMS_RecipientInfo *ri)
335 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
336 unsigned char *ek = NULL;
339 CMS_EncryptedContentInfo *ec;
340 ec = cms->d.envelopedData->encryptedContentInfo;
342 if (ktri->pkey == NULL) {
343 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY);
347 eklen = EVP_PKEY_size(ktri->pkey);
349 ek = OPENSSL_malloc(eklen);
352 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE);
356 eklen = EVP_PKEY_decrypt(ek,
357 ktri->encryptedKey->data,
358 ktri->encryptedKey->length, ktri->pkey);
360 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
367 OPENSSL_cleanse(ec->key, ec->keylen);
368 OPENSSL_free(ec->key);
381 /* Key Encrypted Key (KEK) RecipientInfo routines */
383 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
384 const unsigned char *id, size_t idlen)
386 ASN1_OCTET_STRING tmp_os;
387 CMS_KEKRecipientInfo *kekri;
388 if (ri->type != CMS_RECIPINFO_KEK) {
389 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
393 tmp_os.type = V_ASN1_OCTET_STRING;
395 tmp_os.data = (unsigned char *)id;
396 tmp_os.length = (int)idlen;
397 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
400 /* For now hard code AES key wrap info */
402 static size_t aes_wrap_keylen(int nid)
405 case NID_id_aes128_wrap:
408 case NID_id_aes192_wrap:
411 case NID_id_aes256_wrap:
419 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
420 unsigned char *key, size_t keylen,
421 unsigned char *id, size_t idlen,
422 ASN1_GENERALIZEDTIME *date,
423 ASN1_OBJECT *otherTypeId,
424 ASN1_TYPE *otherType)
426 CMS_RecipientInfo *ri = NULL;
427 CMS_EnvelopedData *env;
428 CMS_KEKRecipientInfo *kekri;
429 env = cms_get0_enveloped(cms);
433 if (nid == NID_undef) {
436 nid = NID_id_aes128_wrap;
440 nid = NID_id_aes192_wrap;
444 nid = NID_id_aes256_wrap;
448 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
454 size_t exp_keylen = aes_wrap_keylen(nid);
457 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
458 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
462 if (keylen != exp_keylen) {
463 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
469 /* Initialize recipient info */
470 ri = M_ASN1_new_of(CMS_RecipientInfo);
474 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
477 ri->type = CMS_RECIPINFO_KEK;
482 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
483 if (kekri->kekid->other == NULL)
487 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
490 /* After this point no calls can fail */
495 kekri->keylen = keylen;
497 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
499 kekri->kekid->date = date;
501 if (kekri->kekid->other) {
502 kekri->kekid->other->keyAttrId = otherTypeId;
503 kekri->kekid->other->keyAttr = otherType;
506 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
507 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
512 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
515 M_ASN1_free_of(ri, CMS_RecipientInfo);
520 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
522 ASN1_OCTET_STRING **pid,
523 ASN1_GENERALIZEDTIME **pdate,
524 ASN1_OBJECT **potherid,
525 ASN1_TYPE **pothertype)
527 CMS_KEKIdentifier *rkid;
528 if (ri->type != CMS_RECIPINFO_KEK) {
529 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
532 rkid = ri->d.kekri->kekid;
534 *palg = ri->d.kekri->keyEncryptionAlgorithm;
536 *pid = rkid->keyIdentifier;
541 *potherid = rkid->other->keyAttrId;
547 *pothertype = rkid->other->keyAttr;
554 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
555 unsigned char *key, size_t keylen)
557 CMS_KEKRecipientInfo *kekri;
558 if (ri->type != CMS_RECIPINFO_KEK) {
559 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
565 kekri->keylen = keylen;
569 /* Encrypt content key in KEK recipient info */
571 static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
572 CMS_RecipientInfo *ri)
574 CMS_EncryptedContentInfo *ec;
575 CMS_KEKRecipientInfo *kekri;
577 unsigned char *wkey = NULL;
581 ec = cms->d.envelopedData->encryptedContentInfo;
586 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
590 if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
591 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
592 CMS_R_ERROR_SETTING_KEY);
596 wkey = OPENSSL_malloc(ec->keylen + 8);
599 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
603 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
606 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
610 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
618 OPENSSL_cleanse(&actx, sizeof(actx));
624 /* Decrypt content key in KEK recipient info */
626 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
627 CMS_RecipientInfo *ri)
629 CMS_EncryptedContentInfo *ec;
630 CMS_KEKRecipientInfo *kekri;
632 unsigned char *ukey = NULL;
636 ec = cms->d.envelopedData->encryptedContentInfo;
641 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
645 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
646 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
647 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
648 CMS_R_INVALID_KEY_LENGTH);
652 /* If encrypted key length is invalid don't bother */
654 if (kekri->encryptedKey->length < 16) {
655 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
656 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
660 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
661 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
662 CMS_R_ERROR_SETTING_KEY);
666 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
669 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
673 ukeylen = AES_unwrap_key(&actx, NULL, ukey,
674 kekri->encryptedKey->data,
675 kekri->encryptedKey->length);
678 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
683 ec->keylen = ukeylen;
691 OPENSSL_cleanse(&actx, sizeof(actx));
697 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
700 case CMS_RECIPINFO_TRANS:
701 return cms_RecipientInfo_ktri_decrypt(cms, ri);
703 case CMS_RECIPINFO_KEK:
704 return cms_RecipientInfo_kekri_decrypt(cms, ri);
707 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
708 CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
713 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
715 CMS_EncryptedContentInfo *ec;
716 STACK_OF(CMS_RecipientInfo) *rinfos;
717 CMS_RecipientInfo *ri;
721 /* Get BIO first to set up key */
723 ec = cms->d.envelopedData->encryptedContentInfo;
724 ret = cms_EncryptedContent_init_bio(ec);
726 /* If error or no cipher end of processing */
728 if (!ret || !ec->cipher)
731 /* Now encrypt content key according to each RecipientInfo type */
733 rinfos = cms->d.envelopedData->recipientInfos;
735 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
736 ri = sk_CMS_RecipientInfo_value(rinfos, i);
739 case CMS_RECIPINFO_TRANS:
740 r = cms_RecipientInfo_ktri_encrypt(cms, ri);
743 case CMS_RECIPINFO_KEK:
744 r = cms_RecipientInfo_kekri_encrypt(cms, ri);
748 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
749 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
754 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
755 CMS_R_ERROR_SETTING_RECIPIENTINFO);
765 OPENSSL_cleanse(ec->key, ec->keylen);
766 OPENSSL_free(ec->key);