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 * ====================================================================
54 #include "internal/cryptlib.h"
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 "internal/asn1_int.h"
65 /* Key Agreement Recipient Info (KARI) routines */
67 int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo
*ri
,
69 ASN1_OCTET_STRING
**pukm
)
71 if (ri
->type
!= CMS_RECIPINFO_AGREE
) {
72 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG
,
73 CMS_R_NOT_KEY_AGREEMENT
);
77 *palg
= ri
->d
.kari
->keyEncryptionAlgorithm
;
79 *pukm
= ri
->d
.kari
->ukm
;
83 /* Retrieve recipient encrypted keys from a kari */
85 STACK_OF(CMS_RecipientEncryptedKey
)
86 *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo
*ri
)
88 if (ri
->type
!= CMS_RECIPINFO_AGREE
) {
89 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS
,
90 CMS_R_NOT_KEY_AGREEMENT
);
93 return ri
->d
.kari
->recipientEncryptedKeys
;
96 int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo
*ri
,
98 ASN1_BIT_STRING
**pubkey
,
99 ASN1_OCTET_STRING
**keyid
,
103 CMS_OriginatorIdentifierOrKey
*oik
;
104 if (ri
->type
!= CMS_RECIPINFO_AGREE
) {
105 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID
,
106 CMS_R_NOT_KEY_AGREEMENT
);
109 oik
= ri
->d
.kari
->originator
;
120 if (oik
->type
== CMS_OIK_ISSUER_SERIAL
) {
122 *issuer
= oik
->d
.issuerAndSerialNumber
->issuer
;
124 *sno
= oik
->d
.issuerAndSerialNumber
->serialNumber
;
125 } else if (oik
->type
== CMS_OIK_KEYIDENTIFIER
) {
127 *keyid
= oik
->d
.subjectKeyIdentifier
;
128 } else if (oik
->type
== CMS_OIK_PUBKEY
) {
130 *pubalg
= oik
->d
.originatorKey
->algorithm
;
132 *pubkey
= oik
->d
.originatorKey
->publicKey
;
138 int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo
*ri
, X509
*cert
)
140 CMS_OriginatorIdentifierOrKey
*oik
;
141 if (ri
->type
!= CMS_RECIPINFO_AGREE
) {
142 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP
,
143 CMS_R_NOT_KEY_AGREEMENT
);
146 oik
= ri
->d
.kari
->originator
;
147 if (oik
->type
== CMS_OIK_ISSUER_SERIAL
)
148 return cms_ias_cert_cmp(oik
->d
.issuerAndSerialNumber
, cert
);
149 else if (oik
->type
== CMS_OIK_KEYIDENTIFIER
)
150 return cms_keyid_cert_cmp(oik
->d
.subjectKeyIdentifier
, cert
);
154 int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey
*rek
,
155 ASN1_OCTET_STRING
**keyid
,
156 ASN1_GENERALIZEDTIME
**tm
,
157 CMS_OtherKeyAttribute
**other
,
158 X509_NAME
**issuer
, ASN1_INTEGER
**sno
)
160 CMS_KeyAgreeRecipientIdentifier
*rid
= rek
->rid
;
161 if (rid
->type
== CMS_REK_ISSUER_SERIAL
) {
163 *issuer
= rid
->d
.issuerAndSerialNumber
->issuer
;
165 *sno
= rid
->d
.issuerAndSerialNumber
->serialNumber
;
172 } else if (rid
->type
== CMS_REK_KEYIDENTIFIER
) {
174 *keyid
= rid
->d
.rKeyId
->subjectKeyIdentifier
;
176 *tm
= rid
->d
.rKeyId
->date
;
178 *other
= rid
->d
.rKeyId
->other
;
188 int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey
*rek
,
191 CMS_KeyAgreeRecipientIdentifier
*rid
= rek
->rid
;
192 if (rid
->type
== CMS_REK_ISSUER_SERIAL
)
193 return cms_ias_cert_cmp(rid
->d
.issuerAndSerialNumber
, cert
);
194 else if (rid
->type
== CMS_REK_KEYIDENTIFIER
)
195 return cms_keyid_cert_cmp(rid
->d
.rKeyId
->subjectKeyIdentifier
, cert
);
200 int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo
*ri
, EVP_PKEY
*pk
)
203 CMS_KeyAgreeRecipientInfo
*kari
= ri
->d
.kari
;
205 EVP_PKEY_CTX_free(kari
->pctx
);
209 pctx
= EVP_PKEY_CTX_new(pk
, NULL
);
210 if (!pctx
|| !EVP_PKEY_derive_init(pctx
))
215 EVP_PKEY_CTX_free(pctx
);
219 EVP_CIPHER_CTX
*CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo
*ri
)
221 if (ri
->type
== CMS_RECIPINFO_AGREE
)
222 return ri
->d
.kari
->ctx
;
227 * Derive KEK and decrypt/encrypt with it to produce either the original CEK
228 * or the encrypted CEK.
231 static int cms_kek_cipher(unsigned char **pout
, size_t *poutlen
,
232 const unsigned char *in
, size_t inlen
,
233 CMS_KeyAgreeRecipientInfo
*kari
, int enc
)
235 /* Key encryption key */
236 unsigned char kek
[EVP_MAX_KEY_LENGTH
];
239 unsigned char *out
= NULL
;
241 keklen
= EVP_CIPHER_CTX_key_length(kari
->ctx
);
242 if (keklen
> EVP_MAX_KEY_LENGTH
)
245 if (EVP_PKEY_derive(kari
->pctx
, kek
, &keklen
) <= 0)
247 /* Set KEK in context */
248 if (!EVP_CipherInit_ex(kari
->ctx
, NULL
, NULL
, kek
, NULL
, enc
))
250 /* obtain output length of ciphered key */
251 if (!EVP_CipherUpdate(kari
->ctx
, NULL
, &outlen
, in
, inlen
))
253 out
= OPENSSL_malloc(outlen
);
256 if (!EVP_CipherUpdate(kari
->ctx
, out
, &outlen
, in
, inlen
))
259 *poutlen
= (size_t)outlen
;
263 OPENSSL_cleanse(kek
, keklen
);
266 EVP_CIPHER_CTX_reset(kari
->ctx
);
267 /* FIXME: WHY IS kari->pctx freed here? /RL */
268 EVP_PKEY_CTX_free(kari
->pctx
);
273 int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo
*cms
,
274 CMS_RecipientInfo
*ri
,
275 CMS_RecipientEncryptedKey
*rek
)
278 unsigned char *enckey
= NULL
, *cek
= NULL
;
281 CMS_EncryptedContentInfo
*ec
;
282 enckeylen
= rek
->encryptedKey
->length
;
283 enckey
= rek
->encryptedKey
->data
;
284 /* Setup all parameters to derive KEK */
285 if (!cms_env_asn1_ctrl(ri
, 1))
287 /* Attempt to decrypt CEK */
288 if (!cms_kek_cipher(&cek
, &ceklen
, enckey
, enckeylen
, ri
->d
.kari
, 0))
290 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
291 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
301 /* Create ephemeral key and initialise context based on it */
302 static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo
*kari
,
305 EVP_PKEY_CTX
*pctx
= NULL
;
306 EVP_PKEY
*ekey
= NULL
;
308 pctx
= EVP_PKEY_CTX_new(pk
, NULL
);
311 if (EVP_PKEY_keygen_init(pctx
) <= 0)
313 if (EVP_PKEY_keygen(pctx
, &ekey
) <= 0)
315 EVP_PKEY_CTX_free(pctx
);
316 pctx
= EVP_PKEY_CTX_new(ekey
, NULL
);
319 if (EVP_PKEY_derive_init(pctx
) <= 0)
325 EVP_PKEY_CTX_free(pctx
);
330 /* Initialise a ktri based on passed certificate and key */
332 int cms_RecipientInfo_kari_init(CMS_RecipientInfo
*ri
, X509
*recip
,
333 EVP_PKEY
*pk
, unsigned int flags
)
335 CMS_KeyAgreeRecipientInfo
*kari
;
336 CMS_RecipientEncryptedKey
*rek
= NULL
;
338 ri
->d
.kari
= M_ASN1_new_of(CMS_KeyAgreeRecipientInfo
);
341 ri
->type
= CMS_RECIPINFO_AGREE
;
346 rek
= M_ASN1_new_of(CMS_RecipientEncryptedKey
);
347 if (!sk_CMS_RecipientEncryptedKey_push(kari
->recipientEncryptedKeys
, rek
)) {
348 M_ASN1_free_of(rek
, CMS_RecipientEncryptedKey
);
352 if (flags
& CMS_USE_KEYID
) {
353 rek
->rid
->type
= CMS_REK_KEYIDENTIFIER
;
354 rek
->rid
->d
.rKeyId
= M_ASN1_new_of(CMS_RecipientKeyIdentifier
);
355 if (rek
->rid
->d
.rKeyId
== NULL
)
357 if (!cms_set1_keyid(&rek
->rid
->d
.rKeyId
->subjectKeyIdentifier
, recip
))
360 rek
->rid
->type
= CMS_REK_ISSUER_SERIAL
;
361 if (!cms_set1_ias(&rek
->rid
->d
.issuerAndSerialNumber
, recip
))
365 /* Create ephemeral key */
366 if (!cms_kari_create_ephemeral_key(kari
, pk
))
374 static int cms_wrap_init(CMS_KeyAgreeRecipientInfo
*kari
,
375 const EVP_CIPHER
*cipher
)
377 EVP_CIPHER_CTX
*ctx
= kari
->ctx
;
378 const EVP_CIPHER
*kekcipher
;
379 int keylen
= EVP_CIPHER_key_length(cipher
);
380 /* If a suitable wrap algorithm is already set nothing to do */
381 kekcipher
= EVP_CIPHER_CTX_cipher(ctx
);
384 if (EVP_CIPHER_CTX_mode(ctx
) != EVP_CIPH_WRAP_MODE
)
389 * Pick a cipher based on content encryption cipher. If it is DES3 use
390 * DES3 wrap otherwise use AES wrap similar to key size.
392 if (EVP_CIPHER_type(cipher
) == NID_des_ede3_cbc
)
393 kekcipher
= EVP_des_ede3_wrap();
394 else if (keylen
<= 16)
395 kekcipher
= EVP_aes_128_wrap();
396 else if (keylen
<= 24)
397 kekcipher
= EVP_aes_192_wrap();
399 kekcipher
= EVP_aes_256_wrap();
400 return EVP_EncryptInit_ex(ctx
, kekcipher
, NULL
, NULL
, NULL
);
403 /* Encrypt content key in key agreement recipient info */
405 int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo
*cms
,
406 CMS_RecipientInfo
*ri
)
408 CMS_KeyAgreeRecipientInfo
*kari
;
409 CMS_EncryptedContentInfo
*ec
;
410 CMS_RecipientEncryptedKey
*rek
;
411 STACK_OF(CMS_RecipientEncryptedKey
) *reks
;
414 if (ri
->type
!= CMS_RECIPINFO_AGREE
) {
415 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT
, CMS_R_NOT_KEY_AGREEMENT
);
419 reks
= kari
->recipientEncryptedKeys
;
420 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
421 /* Initialise wrap algorithm parameters */
422 if (!cms_wrap_init(kari
, ec
->cipher
))
425 * If no orignator key set up initialise for ephemeral key the public key
426 * ASN1 structure will set the actual public key value.
428 if (kari
->originator
->type
== -1) {
429 CMS_OriginatorIdentifierOrKey
*oik
= kari
->originator
;
430 oik
->type
= CMS_OIK_PUBKEY
;
431 oik
->d
.originatorKey
= M_ASN1_new_of(CMS_OriginatorPublicKey
);
432 if (!oik
->d
.originatorKey
)
435 /* Initialise KDF algorithm */
436 if (!cms_env_asn1_ctrl(ri
, 0))
438 /* For each rek, derive KEK, encrypt CEK */
439 for (i
= 0; i
< sk_CMS_RecipientEncryptedKey_num(reks
); i
++) {
440 unsigned char *enckey
;
442 rek
= sk_CMS_RecipientEncryptedKey_value(reks
, i
);
443 if (EVP_PKEY_derive_set_peer(kari
->pctx
, rek
->pkey
) <= 0)
445 if (!cms_kek_cipher(&enckey
, &enckeylen
, ec
->key
, ec
->keylen
,
448 ASN1_STRING_set0(rek
->encryptedKey
, enckey
, enckeylen
);