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>
62 #include "asn1_locl.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 /* Add a recipient certificate. For now only handle key transport.
142 * If we ever handle key agreement will need updating.
144 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
145 X509 *recip, unsigned int flags)
147 CMS_RecipientInfo *ri = NULL;
148 CMS_KeyTransRecipientInfo *ktri;
149 CMS_EnvelopedData *env;
152 env = cms_get0_enveloped(cms);
156 /* Initialize recipient info */
157 ri = M_ASN1_new_of(CMS_RecipientInfo);
161 /* Initialize and add key transport recipient info */
163 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
166 ri->type = CMS_RECIPINFO_TRANS;
170 X509_check_purpose(recip, -1, -1);
171 pk = X509_get_pubkey(recip);
174 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
175 CMS_R_ERROR_GETTING_PUBLIC_KEY);
178 CRYPTO_add(&recip->references, 1, CRYPTO_LOCK_X509);
182 if (flags & CMS_USE_KEYID)
185 type = CMS_RECIPINFO_KEYIDENTIFIER;
190 type = CMS_RECIPINFO_ISSUER_SERIAL;
193 /* Not a typo: RecipientIdentifier and SignerIdentifier are the
197 if (!cms_set1_SignerIdentifier(ktri->rid, recip, type))
200 if (pk->ameth && pk->ameth->pkey_ctrl)
202 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_ENVELOPE,
206 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
207 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
212 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
218 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
224 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
227 M_ASN1_free_of(ri, CMS_RecipientInfo);
232 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
234 ASN1_OCTET_STRING **pid,
235 ASN1_GENERALIZEDTIME **pdate,
236 ASN1_OBJECT **potherid,
237 ASN1_TYPE **pothertype)
239 CMS_KEKIdentifier *rkid;
240 if (ri->type != CMS_RECIPINFO_KEK)
242 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
245 rkid = ri->d.kekri->kekid;
247 *palg = ri->d.kekri->keyEncryptionAlgorithm;
249 *pid = rkid->keyIdentifier;
255 *potherid = rkid->other->keyAttrId;
262 *pothertype = rkid->other->keyAttr;
270 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
271 unsigned char *key, size_t keylen,
272 unsigned char *id, size_t idlen,
273 ASN1_GENERALIZEDTIME *date,
274 ASN1_OBJECT *otherTypeId,
275 ASN1_TYPE *otherType)
277 CMS_RecipientInfo *ri = NULL;
278 CMS_EnvelopedData *env;
279 CMS_KEKRecipientInfo *kekri;
280 size_t exp_keylen = 0;
281 env = cms_get0_enveloped(cms);
285 /* For now hard code checks on nids */
288 case NID_id_aes128_wrap:
292 case NID_id_aes192_wrap:
296 case NID_id_aes256_wrap:
301 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
302 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
306 if (exp_keylen && (keylen != exp_keylen))
308 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
309 CMS_R_INVALID_KEY_LENGTH);
313 /* Initialize recipient info */
314 ri = M_ASN1_new_of(CMS_RecipientInfo);
318 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
321 ri->type = CMS_RECIPINFO_KEK;
327 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
328 if (kekri->kekid->other == NULL)
332 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
335 /* After this point no calls can fail */
340 kekri->keylen = keylen;
342 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
344 kekri->kekid->date = date;
346 kekri->kekid->other->keyAttrId = otherTypeId;
347 kekri->kekid->other->keyAttr = otherType;
349 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
350 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
355 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
358 M_ASN1_free_of(ri, CMS_RecipientInfo);
363 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
364 EVP_PKEY **pk, X509 **recip,
367 CMS_KeyTransRecipientInfo *ktri;
368 if (ri->type != CMS_RECIPINFO_TRANS)
370 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
371 CMS_R_NOT_KEY_TRANSPORT);
380 *recip = ktri->recip;
382 *palg = ktri->keyEncryptionAlgorithm;
386 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
387 ASN1_OCTET_STRING **keyid,
388 X509_NAME **issuer, ASN1_INTEGER **sno)
390 CMS_KeyTransRecipientInfo *ktri;
391 if (ri->type != CMS_RECIPINFO_TRANS)
393 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
394 CMS_R_NOT_KEY_TRANSPORT);
399 return cms_SignerIdentifier_get0_signer_id(ktri->rid,
403 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
405 if (ri->type != CMS_RECIPINFO_TRANS)
407 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
408 CMS_R_NOT_KEY_TRANSPORT);
412 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
415 /* Encrypt content key in key transport recipient info */
417 static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
418 CMS_RecipientInfo *ri)
420 CMS_KeyTransRecipientInfo *ktri;
421 CMS_EncryptedContentInfo *ec;
422 EVP_PKEY_CTX *pctx = NULL;
423 unsigned char *ek = NULL;
428 if (ri->type != CMS_RECIPINFO_TRANS)
430 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
431 CMS_R_NOT_KEY_TRANSPORT);
435 ec = cms->d.envelopedData->encryptedContentInfo;
437 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
441 if (EVP_PKEY_encrypt_init(pctx) <= 0)
444 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
445 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0)
447 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
451 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
454 ek = OPENSSL_malloc(eklen);
458 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
459 ERR_R_MALLOC_FAILURE);
463 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
466 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
473 EVP_PKEY_CTX_free(pctx);
480 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
483 CMS_KeyTransRecipientInfo *ktri;
484 EVP_PKEY_CTX *pctx = NULL;
485 unsigned char *ek = NULL;
490 if (ri->type != CMS_RECIPINFO_TRANS)
492 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
493 CMS_R_NOT_KEY_TRANSPORT);
498 pctx = EVP_PKEY_CTX_new(pkey, NULL);
502 if (EVP_PKEY_decrypt_init(pctx) <= 0)
505 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
506 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0)
508 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT, CMS_R_CTRL_ERROR);
512 if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
513 ktri->encryptedKey->data,
514 ktri->encryptedKey->length) <= 0)
517 ek = OPENSSL_malloc(eklen);
521 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT, ERR_R_MALLOC_FAILURE);
525 if (EVP_PKEY_decrypt(pctx, ek, &eklen,
526 ktri->encryptedKey->data,
527 ktri->encryptedKey->length) <= 0)
529 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT, CMS_R_CMS_LIB);
535 cms->d.envelopedData->encryptedContentInfo->key = ek;
536 cms->d.envelopedData->encryptedContentInfo->keylen = eklen;
540 EVP_PKEY_CTX_free(pctx);
547 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
549 CMS_EncryptedContentInfo *ec;
550 STACK_OF(CMS_RecipientInfo) *rinfos;
551 CMS_RecipientInfo *ri;
555 /* Get BIO first to set up key */
557 ec = cms->d.envelopedData->encryptedContentInfo;
558 ret = cms_EncryptedContent_init_bio(ec);
560 /* If error or no cipher end of processing */
562 if (!ret || !ec->cipher)
566 rinfos = cms->d.envelopedData->recipientInfos;
568 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++)
570 ri = sk_CMS_RecipientInfo_value(rinfos, i);
571 if (ri->type == CMS_RECIPINFO_TRANS)
572 r = cms_RecipientInfo_ktri_encrypt(cms, ri);
575 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
576 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
581 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
582 CMS_R_ERROR_SETTING_RECIPIENTINFO);
593 OPENSSL_cleanse(ec->key, ec->keylen);
594 OPENSSL_free(ec->key);