2 * Copyright 2013-2016 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 /* 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_and_peer(CMS_RecipientInfo *ri, EVP_PKEY *pk, X509 *peer)
158 CMS_KeyAgreeRecipientInfo *kari = ri->d.kari;
160 EVP_PKEY_CTX_free(kari->pctx);
165 pctx = EVP_PKEY_CTX_new(pk, NULL);
166 if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0)
170 EVP_PKEY *pub_pkey = X509_get0_pubkey(peer);
172 if (EVP_PKEY_derive_set_peer(pctx, pub_pkey) <= 0)
179 EVP_PKEY_CTX_free(pctx);
183 int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk)
185 return CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, NULL);
188 EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri)
190 if (ri->type == CMS_RECIPINFO_AGREE)
191 return ri->d.kari->ctx;
196 * Derive KEK and decrypt/encrypt with it to produce either the original CEK
197 * or the encrypted CEK.
200 static int cms_kek_cipher(unsigned char **pout, size_t *poutlen,
201 const unsigned char *in, size_t inlen,
202 CMS_KeyAgreeRecipientInfo *kari, int enc)
204 /* Key encryption key */
205 unsigned char kek[EVP_MAX_KEY_LENGTH];
208 unsigned char *out = NULL;
210 keklen = EVP_CIPHER_CTX_key_length(kari->ctx);
211 if (keklen > EVP_MAX_KEY_LENGTH)
214 if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0)
216 /* Set KEK in context */
217 if (!EVP_CipherInit_ex(kari->ctx, NULL, NULL, kek, NULL, enc))
219 /* obtain output length of ciphered key */
220 if (!EVP_CipherUpdate(kari->ctx, NULL, &outlen, in, inlen))
222 out = OPENSSL_malloc(outlen);
225 if (!EVP_CipherUpdate(kari->ctx, out, &outlen, in, inlen))
228 *poutlen = (size_t)outlen;
232 OPENSSL_cleanse(kek, keklen);
235 EVP_CIPHER_CTX_reset(kari->ctx);
236 /* FIXME: WHY IS kari->pctx freed here? /RL */
237 EVP_PKEY_CTX_free(kari->pctx);
242 int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms,
243 CMS_RecipientInfo *ri,
244 CMS_RecipientEncryptedKey *rek)
247 unsigned char *enckey = NULL, *cek = NULL;
250 CMS_EncryptedContentInfo *ec;
254 * TODO(3.0) Remove this when we have functionality to deserialize
255 * parameters in EVP_PKEY form from an X509_ALGOR.
256 * This is needed to be able to replace the EC_KEY specific decoding
257 * that happens in ecdh_cms_set_peerkey() (crypto/ec/ec_ameth.c)
261 EVP_PKEY_CTX *pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
262 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
265 if (EVP_PKEY_id(pkey) == EVP_PKEY_NONE) {
266 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_DECRYPT,
267 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
272 enckeylen = rek->encryptedKey->length;
273 enckey = rek->encryptedKey->data;
274 /* Setup all parameters to derive KEK */
275 if (!cms_env_asn1_ctrl(ri, 1))
277 /* Attempt to decrypt CEK */
278 if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0))
280 ec = cms->d.envelopedData->encryptedContentInfo;
281 OPENSSL_clear_free(ec->key, ec->keylen);
291 /* Create ephemeral key and initialise context based on it */
292 static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari,
295 EVP_PKEY_CTX *pctx = NULL;
296 EVP_PKEY *ekey = NULL;
299 pctx = EVP_PKEY_CTX_new(pk, NULL);
302 if (EVP_PKEY_keygen_init(pctx) <= 0)
304 if (EVP_PKEY_keygen(pctx, &ekey) <= 0)
306 EVP_PKEY_CTX_free(pctx);
307 pctx = EVP_PKEY_CTX_new(ekey, NULL);
310 if (EVP_PKEY_derive_init(pctx) <= 0)
316 EVP_PKEY_CTX_free(pctx);
321 /* Set originator private key and initialise context based on it */
322 static int cms_kari_set_originator_private_key(CMS_KeyAgreeRecipientInfo *kari, EVP_PKEY *originatorPrivKey )
324 EVP_PKEY_CTX *pctx = NULL;
327 pctx = EVP_PKEY_CTX_new(originatorPrivKey, NULL);
330 if (EVP_PKEY_derive_init(pctx) <= 0)
337 EVP_PKEY_CTX_free(pctx);
341 /* Initialise a kari based on passed certificate and key */
343 int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip, EVP_PKEY *recipPubKey, X509 * originator, EVP_PKEY *originatorPrivKey, unsigned int flags)
345 CMS_KeyAgreeRecipientInfo *kari;
346 CMS_RecipientEncryptedKey *rek = NULL;
348 ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo);
351 ri->type = CMS_RECIPINFO_AGREE;
356 rek = M_ASN1_new_of(CMS_RecipientEncryptedKey);
360 if (!sk_CMS_RecipientEncryptedKey_push(kari->recipientEncryptedKeys, rek)) {
361 M_ASN1_free_of(rek, CMS_RecipientEncryptedKey);
365 if (flags & CMS_USE_KEYID) {
366 rek->rid->type = CMS_REK_KEYIDENTIFIER;
367 rek->rid->d.rKeyId = M_ASN1_new_of(CMS_RecipientKeyIdentifier);
368 if (rek->rid->d.rKeyId == NULL)
370 if (!cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip))
373 rek->rid->type = CMS_REK_ISSUER_SERIAL;
374 if (!cms_set1_ias(&rek->rid->d.issuerAndSerialNumber, recip))
378 if (originatorPrivKey == NULL && originator == NULL) {
379 /* Create ephemeral key */
380 if (!cms_kari_create_ephemeral_key(kari, recipPubKey))
383 /* Use originator key */
384 CMS_OriginatorIdentifierOrKey *oik = ri->d.kari->originator;
386 if (originatorPrivKey == NULL && originator == NULL)
389 if (flags & CMS_USE_ORIGINATOR_KEYID) {
390 oik->type = CMS_OIK_KEYIDENTIFIER;
391 oik->d.subjectKeyIdentifier = ASN1_OCTET_STRING_new();
392 if (oik->d.subjectKeyIdentifier == NULL)
394 if (!cms_set1_keyid(&oik->d.subjectKeyIdentifier, originator))
397 oik->type = CMS_REK_ISSUER_SERIAL;
398 if (!cms_set1_ias(&oik->d.issuerAndSerialNumber, originator))
402 if (!cms_kari_set_originator_private_key(kari, originatorPrivKey))
406 EVP_PKEY_up_ref(recipPubKey);
407 rek->pkey = recipPubKey;
411 static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari,
412 const EVP_CIPHER *cipher)
414 EVP_CIPHER_CTX *ctx = kari->ctx;
415 const EVP_CIPHER *kekcipher;
416 int keylen = EVP_CIPHER_key_length(cipher);
419 /* If a suitable wrap algorithm is already set nothing to do */
420 kekcipher = EVP_CIPHER_CTX_cipher(ctx);
421 if (kekcipher != NULL) {
422 if (EVP_CIPHER_CTX_mode(ctx) != EVP_CIPH_WRAP_MODE)
426 else if (cipher != NULL
427 && (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_GET_WRAP_CIPHER)) {
428 ret = EVP_CIPHER_meth_get_ctrl(cipher)(NULL, EVP_CTRL_GET_WRAP_CIPHER,
433 if (kekcipher != NULL) {
434 if (EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
437 return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL);
442 * Pick a cipher based on content encryption cipher. If it is DES3 use
443 * DES3 wrap otherwise use AES wrap similar to key size.
445 #ifndef OPENSSL_NO_DES
446 if (EVP_CIPHER_type(cipher) == NID_des_ede3_cbc)
447 kekcipher = EVP_des_ede3_wrap();
451 kekcipher = EVP_aes_128_wrap();
452 else if (keylen <= 24)
453 kekcipher = EVP_aes_192_wrap();
455 kekcipher = EVP_aes_256_wrap();
456 return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL);
459 /* Encrypt content key in key agreement recipient info */
461 int cms_RecipientInfo_kari_encrypt(const CMS_ContentInfo *cms,
462 CMS_RecipientInfo *ri)
464 CMS_KeyAgreeRecipientInfo *kari;
465 CMS_EncryptedContentInfo *ec;
466 CMS_RecipientEncryptedKey *rek;
467 STACK_OF(CMS_RecipientEncryptedKey) *reks;
472 * TODO(3.0) Remove this when we have figured out all the details
473 * need to set up encryption right. With legacy keys, a *lot* is
474 * happening in the CMS specific EVP_PKEY_ASN1_METHOD functions,
475 * such as automatically setting a default KDF type, KDF digest,
476 * all that kind of stuff.
477 * With EVP_SIGNATURE, setting a default digest is done by getting
478 * the default MD for the key, and then inject that back into the
479 * signature implementation... we could do something similar with
480 * CMS, possibly using CMS specific OSSL_PARAM keys, just like we
481 * have for certain AlgorithmIdentifier retrievals.
485 EVP_PKEY_CTX *pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
486 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
489 if (EVP_PKEY_id(pkey) == EVP_PKEY_NONE) {
490 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT,
491 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
496 if (ri->type != CMS_RECIPINFO_AGREE) {
497 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT, CMS_R_NOT_KEY_AGREEMENT);
501 reks = kari->recipientEncryptedKeys;
502 ec = cms->d.envelopedData->encryptedContentInfo;
503 /* Initialise wrap algorithm parameters */
504 if (!cms_wrap_init(kari, ec->cipher))
507 * If no originator key set up initialise for ephemeral key the public key
508 * ASN1 structure will set the actual public key value.
510 if (kari->originator->type == -1) {
511 CMS_OriginatorIdentifierOrKey *oik = kari->originator;
512 oik->type = CMS_OIK_PUBKEY;
513 oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey);
514 if (!oik->d.originatorKey)
517 /* Initialise KDF algorithm */
518 if (!cms_env_asn1_ctrl(ri, 0))
520 /* For each rek, derive KEK, encrypt CEK */
521 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
522 unsigned char *enckey;
524 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
525 if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0)
527 if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen,
530 ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen);