2 * Copyright 2013-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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>
17 #include "cms_local.h"
18 #include "crypto/asn1.h"
20 DEFINE_STACK_OF(CMS_RecipientEncryptedKey)
22 /* Key Agreement Recipient Info (KARI) routines */
24 int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri,
26 ASN1_OCTET_STRING **pukm)
28 if (ri->type != CMS_RECIPINFO_AGREE) {
29 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG,
30 CMS_R_NOT_KEY_AGREEMENT);
34 *palg = ri->d.kari->keyEncryptionAlgorithm;
36 *pukm = ri->d.kari->ukm;
40 /* Retrieve recipient encrypted keys from a kari */
42 STACK_OF(CMS_RecipientEncryptedKey)
43 *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri)
45 if (ri->type != CMS_RECIPINFO_AGREE) {
46 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS,
47 CMS_R_NOT_KEY_AGREEMENT);
50 return ri->d.kari->recipientEncryptedKeys;
53 int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri,
55 ASN1_BIT_STRING **pubkey,
56 ASN1_OCTET_STRING **keyid,
60 CMS_OriginatorIdentifierOrKey *oik;
61 if (ri->type != CMS_RECIPINFO_AGREE) {
62 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID,
63 CMS_R_NOT_KEY_AGREEMENT);
66 oik = ri->d.kari->originator;
77 if (oik->type == CMS_OIK_ISSUER_SERIAL) {
79 *issuer = oik->d.issuerAndSerialNumber->issuer;
81 *sno = oik->d.issuerAndSerialNumber->serialNumber;
82 } else if (oik->type == CMS_OIK_KEYIDENTIFIER) {
84 *keyid = oik->d.subjectKeyIdentifier;
85 } else if (oik->type == CMS_OIK_PUBKEY) {
87 *pubalg = oik->d.originatorKey->algorithm;
89 *pubkey = oik->d.originatorKey->publicKey;
95 int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert)
97 CMS_OriginatorIdentifierOrKey *oik;
98 if (ri->type != CMS_RECIPINFO_AGREE) {
99 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP,
100 CMS_R_NOT_KEY_AGREEMENT);
103 oik = ri->d.kari->originator;
104 if (oik->type == CMS_OIK_ISSUER_SERIAL)
105 return cms_ias_cert_cmp(oik->d.issuerAndSerialNumber, cert);
106 else if (oik->type == CMS_OIK_KEYIDENTIFIER)
107 return cms_keyid_cert_cmp(oik->d.subjectKeyIdentifier, cert);
111 int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek,
112 ASN1_OCTET_STRING **keyid,
113 ASN1_GENERALIZEDTIME **tm,
114 CMS_OtherKeyAttribute **other,
115 X509_NAME **issuer, ASN1_INTEGER **sno)
117 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
118 if (rid->type == CMS_REK_ISSUER_SERIAL) {
120 *issuer = rid->d.issuerAndSerialNumber->issuer;
122 *sno = rid->d.issuerAndSerialNumber->serialNumber;
129 } else if (rid->type == CMS_REK_KEYIDENTIFIER) {
131 *keyid = rid->d.rKeyId->subjectKeyIdentifier;
133 *tm = rid->d.rKeyId->date;
135 *other = rid->d.rKeyId->other;
145 int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek,
148 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
149 if (rid->type == CMS_REK_ISSUER_SERIAL)
150 return cms_ias_cert_cmp(rid->d.issuerAndSerialNumber, cert);
151 else if (rid->type == CMS_REK_KEYIDENTIFIER)
152 return cms_keyid_cert_cmp(rid->d.rKeyId->subjectKeyIdentifier, cert);
157 int CMS_RecipientInfo_kari_set0_pkey_and_peer(CMS_RecipientInfo *ri, EVP_PKEY *pk, X509 *peer)
160 CMS_KeyAgreeRecipientInfo *kari = ri->d.kari;
162 EVP_PKEY_CTX_free(kari->pctx);
167 pctx = EVP_PKEY_CTX_new(pk, NULL);
168 if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0)
172 EVP_PKEY *pub_pkey = X509_get0_pubkey(peer);
174 if (EVP_PKEY_derive_set_peer(pctx, pub_pkey) <= 0)
181 EVP_PKEY_CTX_free(pctx);
185 int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk)
187 return CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, NULL);
190 EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri)
192 if (ri->type == CMS_RECIPINFO_AGREE)
193 return ri->d.kari->ctx;
198 * Derive KEK and decrypt/encrypt with it to produce either the original CEK
199 * or the encrypted CEK.
202 static int cms_kek_cipher(unsigned char **pout, size_t *poutlen,
203 const unsigned char *in, size_t inlen,
204 CMS_KeyAgreeRecipientInfo *kari, int enc)
206 /* Key encryption key */
207 unsigned char kek[EVP_MAX_KEY_LENGTH];
210 unsigned char *out = NULL;
212 keklen = EVP_CIPHER_CTX_key_length(kari->ctx);
213 if (keklen > EVP_MAX_KEY_LENGTH)
216 if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0)
218 /* Set KEK in context */
219 if (!EVP_CipherInit_ex(kari->ctx, NULL, NULL, kek, NULL, enc))
221 /* obtain output length of ciphered key */
222 if (!EVP_CipherUpdate(kari->ctx, NULL, &outlen, in, inlen))
224 out = OPENSSL_malloc(outlen);
227 if (!EVP_CipherUpdate(kari->ctx, out, &outlen, in, inlen))
230 *poutlen = (size_t)outlen;
234 OPENSSL_cleanse(kek, keklen);
237 EVP_CIPHER_CTX_reset(kari->ctx);
238 /* FIXME: WHY IS kari->pctx freed here? /RL */
239 EVP_PKEY_CTX_free(kari->pctx);
244 int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms,
245 CMS_RecipientInfo *ri,
246 CMS_RecipientEncryptedKey *rek)
249 unsigned char *enckey = NULL, *cek = NULL;
252 CMS_EncryptedContentInfo *ec;
256 * TODO(3.0) Remove this when we have functionality to deserialize
257 * parameters in EVP_PKEY form from an X509_ALGOR.
258 * This is needed to be able to replace the EC_KEY specific decoding
259 * that happens in ecdh_cms_set_peerkey() (crypto/ec/ec_ameth.c)
263 EVP_PKEY_CTX *pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
264 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
267 if (EVP_PKEY_id(pkey) == EVP_PKEY_NONE) {
268 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_DECRYPT,
269 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
274 enckeylen = rek->encryptedKey->length;
275 enckey = rek->encryptedKey->data;
276 /* Setup all parameters to derive KEK */
277 if (!cms_env_asn1_ctrl(ri, 1))
279 /* Attempt to decrypt CEK */
280 if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0))
282 ec = cms->d.envelopedData->encryptedContentInfo;
283 OPENSSL_clear_free(ec->key, ec->keylen);
293 /* Create ephemeral key and initialise context based on it */
294 static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari,
297 EVP_PKEY_CTX *pctx = NULL;
298 EVP_PKEY *ekey = NULL;
301 pctx = EVP_PKEY_CTX_new(pk, NULL);
304 if (EVP_PKEY_keygen_init(pctx) <= 0)
306 if (EVP_PKEY_keygen(pctx, &ekey) <= 0)
308 EVP_PKEY_CTX_free(pctx);
309 pctx = EVP_PKEY_CTX_new(ekey, NULL);
312 if (EVP_PKEY_derive_init(pctx) <= 0)
318 EVP_PKEY_CTX_free(pctx);
323 /* Set originator private key and initialise context based on it */
324 static int cms_kari_set_originator_private_key(CMS_KeyAgreeRecipientInfo *kari, EVP_PKEY *originatorPrivKey )
326 EVP_PKEY_CTX *pctx = NULL;
329 pctx = EVP_PKEY_CTX_new(originatorPrivKey, NULL);
332 if (EVP_PKEY_derive_init(pctx) <= 0)
339 EVP_PKEY_CTX_free(pctx);
343 /* Initialise a kari based on passed certificate and key */
345 int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip, EVP_PKEY *recipPubKey, X509 * originator, EVP_PKEY *originatorPrivKey, unsigned int flags)
347 CMS_KeyAgreeRecipientInfo *kari;
348 CMS_RecipientEncryptedKey *rek = NULL;
350 ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo);
353 ri->type = CMS_RECIPINFO_AGREE;
358 rek = M_ASN1_new_of(CMS_RecipientEncryptedKey);
362 if (!sk_CMS_RecipientEncryptedKey_push(kari->recipientEncryptedKeys, rek)) {
363 M_ASN1_free_of(rek, CMS_RecipientEncryptedKey);
367 if (flags & CMS_USE_KEYID) {
368 rek->rid->type = CMS_REK_KEYIDENTIFIER;
369 rek->rid->d.rKeyId = M_ASN1_new_of(CMS_RecipientKeyIdentifier);
370 if (rek->rid->d.rKeyId == NULL)
372 if (!cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip))
375 rek->rid->type = CMS_REK_ISSUER_SERIAL;
376 if (!cms_set1_ias(&rek->rid->d.issuerAndSerialNumber, recip))
380 if (originatorPrivKey == NULL && originator == NULL) {
381 /* Create ephemeral key */
382 if (!cms_kari_create_ephemeral_key(kari, recipPubKey))
385 /* Use originator key */
386 CMS_OriginatorIdentifierOrKey *oik = ri->d.kari->originator;
388 if (originatorPrivKey == NULL || originator == NULL)
391 if (flags & CMS_USE_ORIGINATOR_KEYID) {
392 oik->type = CMS_OIK_KEYIDENTIFIER;
393 oik->d.subjectKeyIdentifier = ASN1_OCTET_STRING_new();
394 if (oik->d.subjectKeyIdentifier == NULL)
396 if (!cms_set1_keyid(&oik->d.subjectKeyIdentifier, originator))
399 oik->type = CMS_REK_ISSUER_SERIAL;
400 if (!cms_set1_ias(&oik->d.issuerAndSerialNumber, originator))
404 if (!cms_kari_set_originator_private_key(kari, originatorPrivKey))
408 EVP_PKEY_up_ref(recipPubKey);
409 rek->pkey = recipPubKey;
413 static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari,
414 const EVP_CIPHER *cipher)
416 EVP_CIPHER_CTX *ctx = kari->ctx;
417 const EVP_CIPHER *kekcipher;
421 /* If a suitable wrap algorithm is already set nothing to do */
422 kekcipher = EVP_CIPHER_CTX_cipher(ctx);
423 if (kekcipher != NULL) {
424 if (EVP_CIPHER_CTX_mode(ctx) != EVP_CIPH_WRAP_MODE)
430 keylen = EVP_CIPHER_key_length(cipher);
431 if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_GET_WRAP_CIPHER) != 0) {
432 ret = EVP_CIPHER_meth_get_ctrl(cipher)(NULL, EVP_CTRL_GET_WRAP_CIPHER,
437 if (kekcipher != NULL) {
438 if (EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
441 return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL);
446 * Pick a cipher based on content encryption cipher. If it is DES3 use
447 * DES3 wrap otherwise use AES wrap similar to key size.
449 #ifndef OPENSSL_NO_DES
450 if (EVP_CIPHER_type(cipher) == NID_des_ede3_cbc)
451 kekcipher = EVP_des_ede3_wrap();
455 kekcipher = EVP_aes_128_wrap();
456 else if (keylen <= 24)
457 kekcipher = EVP_aes_192_wrap();
459 kekcipher = EVP_aes_256_wrap();
460 return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL);
463 /* Encrypt content key in key agreement recipient info */
465 int cms_RecipientInfo_kari_encrypt(const CMS_ContentInfo *cms,
466 CMS_RecipientInfo *ri)
468 CMS_KeyAgreeRecipientInfo *kari;
469 CMS_EncryptedContentInfo *ec;
470 CMS_RecipientEncryptedKey *rek;
471 STACK_OF(CMS_RecipientEncryptedKey) *reks;
476 * TODO(3.0) Remove this when we have figured out all the details
477 * need to set up encryption right. With legacy keys, a *lot* is
478 * happening in the CMS specific EVP_PKEY_ASN1_METHOD functions,
479 * such as automatically setting a default KDF type, KDF digest,
480 * all that kind of stuff.
481 * With EVP_SIGNATURE, setting a default digest is done by getting
482 * the default MD for the key, and then inject that back into the
483 * signature implementation... we could do something similar with
484 * CMS, possibly using CMS specific OSSL_PARAM keys, just like we
485 * have for certain AlgorithmIdentifier retrievals.
489 EVP_PKEY_CTX *pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
490 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
493 if (EVP_PKEY_id(pkey) == EVP_PKEY_NONE) {
494 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT,
495 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
500 if (ri->type != CMS_RECIPINFO_AGREE) {
501 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT, CMS_R_NOT_KEY_AGREEMENT);
505 reks = kari->recipientEncryptedKeys;
506 ec = cms->d.envelopedData->encryptedContentInfo;
507 /* Initialise wrap algorithm parameters */
508 if (!cms_wrap_init(kari, ec->cipher))
511 * If no originator key set up initialise for ephemeral key the public key
512 * ASN1 structure will set the actual public key value.
514 if (kari->originator->type == -1) {
515 CMS_OriginatorIdentifierOrKey *oik = kari->originator;
516 oik->type = CMS_OIK_PUBKEY;
517 oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey);
518 if (!oik->d.originatorKey)
521 /* Initialise KDF algorithm */
522 if (!cms_env_asn1_ctrl(ri, 0))
524 /* For each rek, derive KEK, encrypt CEK */
525 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
526 unsigned char *enckey;
528 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
529 if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0)
531 if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen,
534 ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen);