2 * Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/pem.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/err.h>
15 #include <openssl/cms.h>
16 #include <openssl/aes.h>
18 #include "internal/asn1_int.h"
20 /* Key Agreement Recipient Info (KARI) routines */
22 int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri,
24 ASN1_OCTET_STRING **pukm)
26 if (ri->type != CMS_RECIPINFO_AGREE) {
27 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG,
28 CMS_R_NOT_KEY_AGREEMENT);
32 *palg = ri->d.kari->keyEncryptionAlgorithm;
34 *pukm = ri->d.kari->ukm;
38 /* Retrieve recipient encrypted keys from a kari */
40 STACK_OF(CMS_RecipientEncryptedKey)
41 *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri)
43 if (ri->type != CMS_RECIPINFO_AGREE) {
44 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS,
45 CMS_R_NOT_KEY_AGREEMENT);
48 return ri->d.kari->recipientEncryptedKeys;
51 int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri,
53 ASN1_BIT_STRING **pubkey,
54 ASN1_OCTET_STRING **keyid,
58 CMS_OriginatorIdentifierOrKey *oik;
59 if (ri->type != CMS_RECIPINFO_AGREE) {
60 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID,
61 CMS_R_NOT_KEY_AGREEMENT);
64 oik = ri->d.kari->originator;
75 if (oik->type == CMS_OIK_ISSUER_SERIAL) {
77 *issuer = oik->d.issuerAndSerialNumber->issuer;
79 *sno = oik->d.issuerAndSerialNumber->serialNumber;
80 } else if (oik->type == CMS_OIK_KEYIDENTIFIER) {
82 *keyid = oik->d.subjectKeyIdentifier;
83 } else if (oik->type == CMS_OIK_PUBKEY) {
85 *pubalg = oik->d.originatorKey->algorithm;
87 *pubkey = oik->d.originatorKey->publicKey;
93 int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert)
95 CMS_OriginatorIdentifierOrKey *oik;
96 if (ri->type != CMS_RECIPINFO_AGREE) {
97 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP,
98 CMS_R_NOT_KEY_AGREEMENT);
101 oik = ri->d.kari->originator;
102 if (oik->type == CMS_OIK_ISSUER_SERIAL)
103 return cms_ias_cert_cmp(oik->d.issuerAndSerialNumber, cert);
104 else if (oik->type == CMS_OIK_KEYIDENTIFIER)
105 return cms_keyid_cert_cmp(oik->d.subjectKeyIdentifier, cert);
109 int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek,
110 ASN1_OCTET_STRING **keyid,
111 ASN1_GENERALIZEDTIME **tm,
112 CMS_OtherKeyAttribute **other,
113 X509_NAME **issuer, ASN1_INTEGER **sno)
115 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
116 if (rid->type == CMS_REK_ISSUER_SERIAL) {
118 *issuer = rid->d.issuerAndSerialNumber->issuer;
120 *sno = rid->d.issuerAndSerialNumber->serialNumber;
127 } else if (rid->type == CMS_REK_KEYIDENTIFIER) {
129 *keyid = rid->d.rKeyId->subjectKeyIdentifier;
131 *tm = rid->d.rKeyId->date;
133 *other = rid->d.rKeyId->other;
143 int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek,
146 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
147 if (rid->type == CMS_REK_ISSUER_SERIAL)
148 return cms_ias_cert_cmp(rid->d.issuerAndSerialNumber, cert);
149 else if (rid->type == CMS_REK_KEYIDENTIFIER)
150 return cms_keyid_cert_cmp(rid->d.rKeyId->subjectKeyIdentifier, cert);
155 int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk)
158 CMS_KeyAgreeRecipientInfo *kari = ri->d.kari;
160 EVP_PKEY_CTX_free(kari->pctx);
164 pctx = EVP_PKEY_CTX_new(pk, NULL);
165 if (!pctx || !EVP_PKEY_derive_init(pctx))
170 EVP_PKEY_CTX_free(pctx);
174 EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri)
176 if (ri->type == CMS_RECIPINFO_AGREE)
177 return ri->d.kari->ctx;
182 * Derive KEK and decrypt/encrypt with it to produce either the original CEK
183 * or the encrypted CEK.
186 static int cms_kek_cipher(unsigned char **pout, size_t *poutlen,
187 const unsigned char *in, size_t inlen,
188 CMS_KeyAgreeRecipientInfo *kari, int enc)
190 /* Key encryption key */
191 unsigned char kek[EVP_MAX_KEY_LENGTH];
194 unsigned char *out = NULL;
196 keklen = EVP_CIPHER_CTX_key_length(kari->ctx);
197 if (keklen > EVP_MAX_KEY_LENGTH)
200 if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0)
202 /* Set KEK in context */
203 if (!EVP_CipherInit_ex(kari->ctx, NULL, NULL, kek, NULL, enc))
205 /* obtain output length of ciphered key */
206 if (!EVP_CipherUpdate(kari->ctx, NULL, &outlen, in, inlen))
208 out = OPENSSL_malloc(outlen);
211 if (!EVP_CipherUpdate(kari->ctx, out, &outlen, in, inlen))
214 *poutlen = (size_t)outlen;
218 OPENSSL_cleanse(kek, keklen);
221 EVP_CIPHER_CTX_reset(kari->ctx);
222 /* FIXME: WHY IS kari->pctx freed here? /RL */
223 EVP_PKEY_CTX_free(kari->pctx);
228 int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms,
229 CMS_RecipientInfo *ri,
230 CMS_RecipientEncryptedKey *rek)
233 unsigned char *enckey = NULL, *cek = NULL;
236 CMS_EncryptedContentInfo *ec;
237 enckeylen = rek->encryptedKey->length;
238 enckey = rek->encryptedKey->data;
239 /* Setup all parameters to derive KEK */
240 if (!cms_env_asn1_ctrl(ri, 1))
242 /* Attempt to decrypt CEK */
243 if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0))
245 ec = cms->d.envelopedData->encryptedContentInfo;
246 OPENSSL_clear_free(ec->key, ec->keylen);
256 /* Create ephemeral key and initialise context based on it */
257 static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari,
260 EVP_PKEY_CTX *pctx = NULL;
261 EVP_PKEY *ekey = NULL;
263 pctx = EVP_PKEY_CTX_new(pk, NULL);
266 if (EVP_PKEY_keygen_init(pctx) <= 0)
268 if (EVP_PKEY_keygen(pctx, &ekey) <= 0)
270 EVP_PKEY_CTX_free(pctx);
271 pctx = EVP_PKEY_CTX_new(ekey, NULL);
274 if (EVP_PKEY_derive_init(pctx) <= 0)
280 EVP_PKEY_CTX_free(pctx);
285 /* Initialise a ktri based on passed certificate and key */
287 int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
288 EVP_PKEY *pk, unsigned int flags)
290 CMS_KeyAgreeRecipientInfo *kari;
291 CMS_RecipientEncryptedKey *rek = NULL;
293 ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo);
296 ri->type = CMS_RECIPINFO_AGREE;
301 rek = M_ASN1_new_of(CMS_RecipientEncryptedKey);
302 if (!sk_CMS_RecipientEncryptedKey_push(kari->recipientEncryptedKeys, rek)) {
303 M_ASN1_free_of(rek, CMS_RecipientEncryptedKey);
307 if (flags & CMS_USE_KEYID) {
308 rek->rid->type = CMS_REK_KEYIDENTIFIER;
309 rek->rid->d.rKeyId = M_ASN1_new_of(CMS_RecipientKeyIdentifier);
310 if (rek->rid->d.rKeyId == NULL)
312 if (!cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip))
315 rek->rid->type = CMS_REK_ISSUER_SERIAL;
316 if (!cms_set1_ias(&rek->rid->d.issuerAndSerialNumber, recip))
320 /* Create ephemeral key */
321 if (!cms_kari_create_ephemeral_key(kari, pk))
329 static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari,
330 const EVP_CIPHER *cipher)
332 EVP_CIPHER_CTX *ctx = kari->ctx;
333 const EVP_CIPHER *kekcipher;
334 int keylen = EVP_CIPHER_key_length(cipher);
335 /* If a suitable wrap algorithm is already set nothing to do */
336 kekcipher = EVP_CIPHER_CTX_cipher(ctx);
339 if (EVP_CIPHER_CTX_mode(ctx) != EVP_CIPH_WRAP_MODE)
344 * Pick a cipher based on content encryption cipher. If it is DES3 use
345 * DES3 wrap otherwise use AES wrap similar to key size.
347 #ifndef OPENSSL_NO_DES
348 if (EVP_CIPHER_type(cipher) == NID_des_ede3_cbc)
349 kekcipher = EVP_des_ede3_wrap();
353 kekcipher = EVP_aes_128_wrap();
354 else if (keylen <= 24)
355 kekcipher = EVP_aes_192_wrap();
357 kekcipher = EVP_aes_256_wrap();
358 return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL);
361 /* Encrypt content key in key agreement recipient info */
363 int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms,
364 CMS_RecipientInfo *ri)
366 CMS_KeyAgreeRecipientInfo *kari;
367 CMS_EncryptedContentInfo *ec;
368 CMS_RecipientEncryptedKey *rek;
369 STACK_OF(CMS_RecipientEncryptedKey) *reks;
372 if (ri->type != CMS_RECIPINFO_AGREE) {
373 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT, CMS_R_NOT_KEY_AGREEMENT);
377 reks = kari->recipientEncryptedKeys;
378 ec = cms->d.envelopedData->encryptedContentInfo;
379 /* Initialise wrap algorithm parameters */
380 if (!cms_wrap_init(kari, ec->cipher))
383 * If no originator key set up initialise for ephemeral key the public key
384 * ASN1 structure will set the actual public key value.
386 if (kari->originator->type == -1) {
387 CMS_OriginatorIdentifierOrKey *oik = kari->originator;
388 oik->type = CMS_OIK_PUBKEY;
389 oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey);
390 if (!oik->d.originatorKey)
393 /* Initialise KDF algorithm */
394 if (!cms_env_asn1_ctrl(ri, 0))
396 /* For each rek, derive KEK, encrypt CEK */
397 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
398 unsigned char *enckey;
400 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
401 if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0)
403 if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen,
406 ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen);