1 /* crypto/cms/cms_kari.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2013 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 DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
66 DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
67 DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
69 /* Key Agreement Recipient Info (KARI) routines */
71 int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri,
73 ASN1_OCTET_STRING **pukm)
75 if (ri->type != CMS_RECIPINFO_AGREE)
77 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG,
78 CMS_R_NOT_KEY_AGREEMENT);
82 *palg = ri->d.kari->keyEncryptionAlgorithm;
84 *pukm = ri->d.kari->ukm;
88 /* Retrieve recipient encrypted keys from a kari */
90 STACK_OF(CMS_RecipientEncryptedKey) *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri)
92 if (ri->type != CMS_RECIPINFO_AGREE)
94 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS,
95 CMS_R_NOT_KEY_AGREEMENT);
98 return ri->d.kari->recipientEncryptedKeys;
101 int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri,
103 ASN1_BIT_STRING **pubkey,
104 ASN1_OCTET_STRING **keyid,
105 X509_NAME **issuer, ASN1_INTEGER **sno)
107 CMS_OriginatorIdentifierOrKey *oik;
108 if (ri->type != CMS_RECIPINFO_AGREE)
110 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID,
111 CMS_R_NOT_KEY_AGREEMENT);
114 oik = ri->d.kari->originator;
125 if (oik->type == CMS_OIK_ISSUER_SERIAL)
128 *issuer = oik->d.issuerAndSerialNumber->issuer;
130 *sno = oik->d.issuerAndSerialNumber->serialNumber;
132 else if (oik->type == CMS_OIK_KEYIDENTIFIER)
135 *keyid = oik->d.subjectKeyIdentifier;
137 else if (oik->type == CMS_OIK_PUBKEY)
140 *pubalg = oik->d.originatorKey->algorithm;
142 *pubkey = oik->d.originatorKey->publicKey;
149 int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert)
151 CMS_OriginatorIdentifierOrKey *oik;
152 if (ri->type != CMS_RECIPINFO_AGREE)
154 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP,
155 CMS_R_NOT_KEY_AGREEMENT);
158 oik = ri->d.kari->originator;
159 if (oik->type == CMS_OIK_ISSUER_SERIAL)
160 return cms_ias_cert_cmp(oik->d.issuerAndSerialNumber, cert);
161 else if (oik->type == CMS_OIK_KEYIDENTIFIER)
162 return cms_keyid_cert_cmp(oik->d.subjectKeyIdentifier, cert);
166 int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek,
167 ASN1_OCTET_STRING **keyid,
168 ASN1_GENERALIZEDTIME **tm,
169 CMS_OtherKeyAttribute **other,
170 X509_NAME **issuer, ASN1_INTEGER **sno)
172 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
173 if (rid->type == CMS_REK_ISSUER_SERIAL)
176 *issuer = rid->d.issuerAndSerialNumber->issuer;
178 *sno = rid->d.issuerAndSerialNumber->serialNumber;
186 else if (rid->type == CMS_REK_KEYIDENTIFIER)
189 *keyid = rid->d.rKeyId->subjectKeyIdentifier;
191 *tm = rid->d.rKeyId->date;
193 *other = rid->d.rKeyId->other;
204 int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek,
207 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
208 if (rid->type == CMS_REK_ISSUER_SERIAL)
209 return cms_ias_cert_cmp(rid->d.issuerAndSerialNumber, cert);
210 else if (rid->type == CMS_REK_KEYIDENTIFIER)
211 return cms_keyid_cert_cmp(rid->d.rKeyId->subjectKeyIdentifier, cert);
216 int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk)
219 CMS_KeyAgreeRecipientInfo *kari = ri->d.kari;
222 EVP_PKEY_CTX_free(kari->pctx);
227 pctx = EVP_PKEY_CTX_new(pk, NULL);
228 if (!pctx || !EVP_PKEY_derive_init(pctx))
234 EVP_PKEY_CTX_free(pctx);
238 EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri)
240 if (ri->type == CMS_RECIPINFO_AGREE)
241 return &ri->d.kari->ctx;
245 /* Derive KEK and decrypt/encrypt with it to produce either the
246 * original CEK or the encrypted CEK.
249 static int cms_kek_cipher(unsigned char **pout, size_t *poutlen,
250 const unsigned char *in, size_t inlen,
251 CMS_KeyAgreeRecipientInfo *kari, int enc)
253 /* Key encryption key */
254 unsigned char kek[EVP_MAX_KEY_LENGTH];
257 unsigned char *out = NULL;
259 keklen = EVP_CIPHER_CTX_key_length(&kari->ctx);
260 if (keklen > EVP_MAX_KEY_LENGTH)
263 if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0)
265 /* Set KEK in context */
266 if (!EVP_CipherInit_ex(&kari->ctx, NULL, NULL, kek, NULL, enc))
268 /* obtain output length of ciphered key */
269 if (!EVP_CipherUpdate(&kari->ctx, NULL, &outlen, in, inlen))
271 out = OPENSSL_malloc(outlen);
274 if (!EVP_CipherUpdate(&kari->ctx, out, &outlen, in, inlen))
277 *poutlen = (size_t)outlen;
281 OPENSSL_cleanse(kek, keklen);
284 EVP_CIPHER_CTX_cleanup(&kari->ctx);
285 EVP_PKEY_CTX_free(kari->pctx);
290 int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
291 CMS_RecipientEncryptedKey *rek)
294 unsigned char *enckey = NULL, *cek = NULL;
297 CMS_EncryptedContentInfo *ec;
298 enckeylen = rek->encryptedKey->length;
299 enckey = rek->encryptedKey->data;
300 /* Setup all parameters to derive KEK */
301 if (!cms_env_asn1_ctrl(ri, 1))
303 /* Attempt to decrypt CEK */
304 if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0))
306 ec = cms->d.envelopedData->encryptedContentInfo;
309 OPENSSL_cleanse(ec->key, ec->keylen);
310 OPENSSL_free(ec->key);
322 /* Create ephemeral key and initialise context based on it */
323 static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari,
326 EVP_PKEY_CTX *pctx = NULL;
327 EVP_PKEY *ekey = NULL;
329 pctx = EVP_PKEY_CTX_new(pk, NULL);
332 if (EVP_PKEY_keygen_init(pctx) <= 0)
334 if (EVP_PKEY_keygen(pctx, &ekey) <= 0)
336 EVP_PKEY_CTX_free(pctx);
337 pctx = EVP_PKEY_CTX_new(ekey, NULL);
340 if (EVP_PKEY_derive_init(pctx) <= 0)
346 EVP_PKEY_CTX_free(pctx);
352 /* Initialise a ktri based on passed certificate and key */
354 int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
355 EVP_PKEY *pk, unsigned int flags)
357 CMS_KeyAgreeRecipientInfo *kari;
358 CMS_RecipientEncryptedKey *rek = NULL;
360 ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo);
363 ri->type = CMS_RECIPINFO_AGREE;
368 rek = M_ASN1_new_of(CMS_RecipientEncryptedKey);
369 if (!sk_CMS_RecipientEncryptedKey_push(kari->recipientEncryptedKeys, rek))
371 M_ASN1_free_of(rek, CMS_RecipientEncryptedKey);
375 if (flags & CMS_USE_KEYID)
377 rek->rid->type = CMS_REK_KEYIDENTIFIER;
378 if (!cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip))
383 rek->rid->type = CMS_REK_ISSUER_SERIAL;
384 if (!cms_set1_ias(&rek->rid->d.issuerAndSerialNumber, recip))
388 /* Create ephemeral key */
389 if (!cms_kari_create_ephemeral_key(kari, pk))
392 CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY);
397 static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari,
398 const EVP_CIPHER *cipher)
400 EVP_CIPHER_CTX *ctx = &kari->ctx;
401 const EVP_CIPHER *kekcipher;
402 int keylen = EVP_CIPHER_key_length(cipher);
403 /* If a suitable wrap algorithm is already set nothing to do */
404 kekcipher = EVP_CIPHER_CTX_cipher(ctx);
408 if (EVP_CIPHER_CTX_mode(ctx) != EVP_CIPH_WRAP_MODE)
412 /* Pick a cipher based on content encryption cipher. If it is
413 * DES3 use DES3 wrap otherwise use AES wrap similar to key
416 if (EVP_CIPHER_type(cipher) == NID_des_ede3_cbc)
417 kekcipher = EVP_des_ede3_wrap();
418 else if (keylen <= 16)
419 kekcipher = EVP_aes_128_wrap();
420 else if (keylen <= 24)
421 kekcipher = EVP_aes_192_wrap();
423 kekcipher = EVP_aes_256_wrap();
424 return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL);
427 /* Encrypt content key in key agreement recipient info */
429 int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
431 CMS_KeyAgreeRecipientInfo *kari;
432 CMS_EncryptedContentInfo *ec;
433 CMS_RecipientEncryptedKey *rek;
434 STACK_OF(CMS_RecipientEncryptedKey) *reks;
437 if (ri->type != CMS_RECIPINFO_AGREE)
439 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT,
440 CMS_R_NOT_KEY_AGREEMENT);
444 reks = kari->recipientEncryptedKeys;
445 ec = cms->d.envelopedData->encryptedContentInfo;
446 /* Initialise wrap algorithm parameters */
447 if (!cms_wrap_init(kari, ec->cipher))
449 /* If no orignator key set up initialise for ephemeral key
450 * the public key ASN1 structure will set the actual public
453 if (kari->originator->type == -1)
455 CMS_OriginatorIdentifierOrKey *oik = kari->originator;
456 oik->type = CMS_OIK_PUBKEY;
457 oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey);
458 if (!oik->d.originatorKey)
461 /* Initialise KDF algorithm */
462 if (!cms_env_asn1_ctrl(ri, 0))
464 /* For each rek, derive KEK, encrypt CEK */
465 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++)
467 unsigned char *enckey;
469 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
470 if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0)
472 if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen,
475 ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen);