2 * Copyright 2013-2021 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
11 * Low level key APIs (DH etc) are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
16 #include "internal/cryptlib.h"
17 #include <openssl/asn1t.h>
18 #include <openssl/pem.h>
19 #include <openssl/x509v3.h>
20 #include <openssl/err.h>
21 #include <openssl/cms.h>
22 #include <openssl/aes.h>
23 #include "cms_local.h"
24 #include "crypto/asn1.h"
26 /* Key Agreement Recipient Info (KARI) routines */
28 int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri,
30 ASN1_OCTET_STRING **pukm)
32 if (ri->type != CMS_RECIPINFO_AGREE) {
33 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT);
37 *palg = ri->d.kari->keyEncryptionAlgorithm;
39 *pukm = ri->d.kari->ukm;
43 /* Retrieve recipient encrypted keys from a kari */
45 STACK_OF(CMS_RecipientEncryptedKey)
46 *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri)
48 if (ri->type != CMS_RECIPINFO_AGREE) {
49 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT);
52 return ri->d.kari->recipientEncryptedKeys;
55 int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri,
57 ASN1_BIT_STRING **pubkey,
58 ASN1_OCTET_STRING **keyid,
62 CMS_OriginatorIdentifierOrKey *oik;
64 if (ri->type != CMS_RECIPINFO_AGREE) {
65 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT);
68 oik = ri->d.kari->originator;
79 if (oik->type == CMS_OIK_ISSUER_SERIAL) {
81 *issuer = oik->d.issuerAndSerialNumber->issuer;
83 *sno = oik->d.issuerAndSerialNumber->serialNumber;
84 } else if (oik->type == CMS_OIK_KEYIDENTIFIER) {
86 *keyid = oik->d.subjectKeyIdentifier;
87 } else if (oik->type == CMS_OIK_PUBKEY) {
89 *pubalg = oik->d.originatorKey->algorithm;
91 *pubkey = oik->d.originatorKey->publicKey;
97 int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert)
99 CMS_OriginatorIdentifierOrKey *oik;
101 if (ri->type != CMS_RECIPINFO_AGREE) {
102 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT);
105 oik = ri->d.kari->originator;
106 if (oik->type == CMS_OIK_ISSUER_SERIAL)
107 return ossl_cms_ias_cert_cmp(oik->d.issuerAndSerialNumber, cert);
108 else if (oik->type == CMS_OIK_KEYIDENTIFIER)
109 return ossl_cms_keyid_cert_cmp(oik->d.subjectKeyIdentifier, cert);
113 int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek,
114 ASN1_OCTET_STRING **keyid,
115 ASN1_GENERALIZEDTIME **tm,
116 CMS_OtherKeyAttribute **other,
117 X509_NAME **issuer, ASN1_INTEGER **sno)
119 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
121 if (rid->type == CMS_REK_ISSUER_SERIAL) {
123 *issuer = rid->d.issuerAndSerialNumber->issuer;
125 *sno = rid->d.issuerAndSerialNumber->serialNumber;
132 } else if (rid->type == CMS_REK_KEYIDENTIFIER) {
134 *keyid = rid->d.rKeyId->subjectKeyIdentifier;
136 *tm = rid->d.rKeyId->date;
138 *other = rid->d.rKeyId->other;
148 int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek,
151 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
153 if (rid->type == CMS_REK_ISSUER_SERIAL)
154 return ossl_cms_ias_cert_cmp(rid->d.issuerAndSerialNumber, cert);
155 else if (rid->type == CMS_REK_KEYIDENTIFIER)
156 return ossl_cms_keyid_cert_cmp(rid->d.rKeyId->subjectKeyIdentifier,
162 int CMS_RecipientInfo_kari_set0_pkey_and_peer(CMS_RecipientInfo *ri,
163 EVP_PKEY *pk, X509 *peer)
166 CMS_KeyAgreeRecipientInfo *kari = ri->d.kari;
168 EVP_PKEY_CTX_free(kari->pctx);
173 pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(kari->cms_ctx),
175 ossl_cms_ctx_get0_propq(kari->cms_ctx));
176 if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0)
180 EVP_PKEY *pub_pkey = X509_get0_pubkey(peer);
182 if (EVP_PKEY_derive_set_peer(pctx, pub_pkey) <= 0)
189 EVP_PKEY_CTX_free(pctx);
193 int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk)
195 return CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, NULL);
198 EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri)
200 if (ri->type == CMS_RECIPINFO_AGREE)
201 return ri->d.kari->ctx;
206 * Derive KEK and decrypt/encrypt with it to produce either the original CEK
207 * or the encrypted CEK.
210 static int cms_kek_cipher(unsigned char **pout, size_t *poutlen,
211 const unsigned char *in, size_t inlen,
212 CMS_KeyAgreeRecipientInfo *kari, int enc)
214 /* Key encryption key */
215 unsigned char kek[EVP_MAX_KEY_LENGTH];
218 unsigned char *out = NULL;
221 keklen = EVP_CIPHER_CTX_get_key_length(kari->ctx);
222 if (keklen > EVP_MAX_KEY_LENGTH)
225 if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0)
227 /* Set KEK in context */
228 if (!EVP_CipherInit_ex(kari->ctx, NULL, NULL, kek, NULL, enc))
230 /* obtain output length of ciphered key */
231 if (!EVP_CipherUpdate(kari->ctx, NULL, &outlen, in, inlen))
233 out = OPENSSL_malloc(outlen);
236 if (!EVP_CipherUpdate(kari->ctx, out, &outlen, in, inlen))
239 *poutlen = (size_t)outlen;
243 OPENSSL_cleanse(kek, keklen);
246 EVP_CIPHER_CTX_reset(kari->ctx);
247 /* FIXME: WHY IS kari->pctx freed here? /RL */
248 EVP_PKEY_CTX_free(kari->pctx);
253 int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms,
254 CMS_RecipientInfo *ri,
255 CMS_RecipientEncryptedKey *rek)
258 unsigned char *enckey = NULL, *cek = NULL;
261 CMS_EncryptedContentInfo *ec;
263 enckeylen = rek->encryptedKey->length;
264 enckey = rek->encryptedKey->data;
265 /* Setup all parameters to derive KEK */
266 if (!ossl_cms_env_asn1_ctrl(ri, 1))
268 /* Attempt to decrypt CEK */
269 if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0))
271 ec = ossl_cms_get0_env_enc_content(cms);
272 OPENSSL_clear_free(ec->key, ec->keylen);
282 /* Create ephemeral key and initialise context based on it */
283 static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari,
286 EVP_PKEY_CTX *pctx = NULL;
287 EVP_PKEY *ekey = NULL;
289 const CMS_CTX *ctx = kari->cms_ctx;
290 OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
291 const char *propq = ossl_cms_ctx_get0_propq(ctx);
293 pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propq);
296 if (EVP_PKEY_keygen_init(pctx) <= 0)
298 if (EVP_PKEY_keygen(pctx, &ekey) <= 0)
300 EVP_PKEY_CTX_free(pctx);
301 pctx = EVP_PKEY_CTX_new_from_pkey(libctx, ekey, propq);
304 if (EVP_PKEY_derive_init(pctx) <= 0)
310 EVP_PKEY_CTX_free(pctx);
315 /* Set originator private key and initialise context based on it */
316 static int cms_kari_set_originator_private_key(CMS_KeyAgreeRecipientInfo *kari,
317 EVP_PKEY *originatorPrivKey )
319 EVP_PKEY_CTX *pctx = NULL;
321 const CMS_CTX *ctx = kari->cms_ctx;
323 pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
325 ossl_cms_ctx_get0_propq(ctx));
328 if (EVP_PKEY_derive_init(pctx) <= 0)
335 EVP_PKEY_CTX_free(pctx);
339 /* Initialise a kari based on passed certificate and key */
341 int ossl_cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
342 EVP_PKEY *recipPubKey, X509 *originator,
343 EVP_PKEY *originatorPrivKey,
344 unsigned int flags, const CMS_CTX *ctx)
346 CMS_KeyAgreeRecipientInfo *kari;
347 CMS_RecipientEncryptedKey *rek = NULL;
349 ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo);
350 if (ri->d.kari == NULL)
352 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 (!ossl_cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip))
375 rek->rid->type = CMS_REK_ISSUER_SERIAL;
376 if (!ossl_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 (!ossl_cms_set1_keyid(&oik->d.subjectKeyIdentifier, originator))
399 oik->type = CMS_REK_ISSUER_SERIAL;
400 if (!ossl_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 const CMS_CTX *cms_ctx = kari->cms_ctx;
417 EVP_CIPHER_CTX *ctx = kari->ctx;
418 const EVP_CIPHER *kekcipher;
419 EVP_CIPHER *fetched_kekcipher;
420 const char *kekcipher_name;
424 /* If a suitable wrap algorithm is already set nothing to do */
425 kekcipher = EVP_CIPHER_CTX_get0_cipher(ctx);
426 if (kekcipher != NULL) {
427 if (EVP_CIPHER_CTX_get_mode(ctx) != EVP_CIPH_WRAP_MODE)
433 keylen = EVP_CIPHER_get_key_length(cipher);
434 if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_GET_WRAP_CIPHER) != 0) {
435 ret = EVP_CIPHER_meth_get_ctrl(cipher)(NULL, EVP_CTRL_GET_WRAP_CIPHER,
440 if (kekcipher != NULL) {
441 if (EVP_CIPHER_get_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
443 kekcipher_name = EVP_CIPHER_get0_name(kekcipher);
449 * Pick a cipher based on content encryption cipher. If it is DES3 use
450 * DES3 wrap otherwise use AES wrap similar to key size.
452 #ifndef OPENSSL_NO_DES
453 if (EVP_CIPHER_get_type(cipher) == NID_des_ede3_cbc)
454 kekcipher_name = SN_id_smime_alg_CMS3DESwrap;
458 kekcipher_name = SN_id_aes128_wrap;
459 else if (keylen <= 24)
460 kekcipher_name = SN_id_aes192_wrap;
462 kekcipher_name = SN_id_aes256_wrap;
464 fetched_kekcipher = EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(cms_ctx),
466 ossl_cms_ctx_get0_propq(cms_ctx));
467 if (fetched_kekcipher == NULL)
469 ret = EVP_EncryptInit_ex(ctx, fetched_kekcipher, NULL, NULL, NULL);
470 EVP_CIPHER_free(fetched_kekcipher);
474 /* Encrypt content key in key agreement recipient info */
476 int ossl_cms_RecipientInfo_kari_encrypt(const CMS_ContentInfo *cms,
477 CMS_RecipientInfo *ri)
479 CMS_KeyAgreeRecipientInfo *kari;
480 CMS_EncryptedContentInfo *ec;
481 CMS_RecipientEncryptedKey *rek;
482 STACK_OF(CMS_RecipientEncryptedKey) *reks;
485 if (ri->type != CMS_RECIPINFO_AGREE) {
486 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT);
490 reks = kari->recipientEncryptedKeys;
491 ec = ossl_cms_get0_env_enc_content(cms);
492 /* Initialise wrap algorithm parameters */
493 if (!cms_wrap_init(kari, ec->cipher))
496 * If no originator key set up initialise for ephemeral key the public key
497 * ASN1 structure will set the actual public key value.
499 if (kari->originator->type == -1) {
500 CMS_OriginatorIdentifierOrKey *oik = kari->originator;
501 oik->type = CMS_OIK_PUBKEY;
502 oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey);
503 if (!oik->d.originatorKey)
506 /* Initialise KDF algorithm */
507 if (!ossl_cms_env_asn1_ctrl(ri, 0))
509 /* For each rek, derive KEK, encrypt CEK */
510 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
511 unsigned char *enckey;
513 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
514 if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0)
516 if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen,
519 ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen);