2 * Copyright 2006-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 #include <openssl/cms.h>
12 #include <openssl/err.h>
13 #include <openssl/decoder.h>
14 #include "internal/sizes.h"
15 #include "crypto/evp.h"
16 #include "cms_local.h"
18 static EVP_PKEY *pkey_type2param(int ptype, const void *pval,
19 OSSL_LIB_CTX *libctx, const char *propq)
21 EVP_PKEY *pkey = NULL;
22 EVP_PKEY_CTX *pctx = NULL;
24 if (ptype == V_ASN1_SEQUENCE) {
25 const ASN1_STRING *pstr = pval;
26 const unsigned char *pm = pstr->data;
27 size_t pmlen = (size_t)pstr->length;
28 OSSL_DECODER_CTX *ctx = NULL;
29 int selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
31 ctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", NULL, "EC",
32 selection, libctx, propq);
36 OSSL_DECODER_from_data(ctx, &pm, &pmlen);
37 OSSL_DECODER_CTX_free(ctx);
38 } else if (ptype == V_ASN1_OBJECT) {
39 const ASN1_OBJECT *poid = pval;
40 char groupname[OSSL_MAX_NAME_SIZE];
42 /* type == V_ASN1_OBJECT => the parameters are given by an asn1 OID */
43 pctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", propq);
44 if (pctx == NULL || EVP_PKEY_paramgen_init(pctx) <= 0)
46 if (!OBJ_obj2txt(groupname, sizeof(groupname), poid, 0)
47 || !EVP_PKEY_CTX_set_group_name(pctx, groupname)) {
48 ERR_raise(ERR_LIB_CMS, CMS_R_DECODE_ERROR);
51 if (EVP_PKEY_paramgen(pctx, &pkey) <= 0)
54 ERR_raise(ERR_LIB_CMS, CMS_R_DECODE_ERROR);
62 EVP_PKEY_CTX_free(pctx);
66 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
67 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
69 const ASN1_OBJECT *aoid;
73 EVP_PKEY *pkpeer = NULL;
74 const unsigned char *p;
77 X509_ALGOR_get0(&aoid, &atype, &aval, alg);
78 if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
81 /* If absent parameters get group from main key */
82 if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
85 pk = EVP_PKEY_CTX_get0_pkey(pctx);
89 pkpeer = EVP_PKEY_new();
92 if (!EVP_PKEY_copy_parameters(pkpeer, pk))
95 pkpeer = pkey_type2param(atype, aval,
96 EVP_PKEY_CTX_get0_libctx(pctx),
97 EVP_PKEY_CTX_get0_propq(pctx));
101 /* We have parameters now set public key */
102 plen = ASN1_STRING_length(pubkey);
103 p = ASN1_STRING_get0_data(pubkey);
104 if (p == NULL || plen == 0)
107 if (!EVP_PKEY_set1_encoded_public_key(pkpeer, p, plen))
110 if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
113 EVP_PKEY_free(pkpeer);
117 /* Set KDF parameters based on KDF NID */
118 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
120 int kdf_nid, kdfmd_nid, cofactor;
121 const EVP_MD *kdf_md;
123 if (eckdf_nid == NID_undef)
126 /* Lookup KDF type, cofactor mode and digest */
127 if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
130 if (kdf_nid == NID_dh_std_kdf)
132 else if (kdf_nid == NID_dh_cofactor_kdf)
137 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
140 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
143 kdf_md = EVP_get_digestbynid(kdfmd_nid);
147 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
152 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
155 X509_ALGOR *alg, *kekalg = NULL;
156 ASN1_OCTET_STRING *ukm;
157 const unsigned char *p;
158 unsigned char *der = NULL;
160 EVP_CIPHER *kekcipher = NULL;
161 EVP_CIPHER_CTX *kekctx;
162 char name[OSSL_MAX_NAME_SIZE];
164 if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
167 if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
168 ERR_raise(ERR_LIB_CMS, CMS_R_KDF_PARAMETER_ERROR);
172 if (alg->parameter->type != V_ASN1_SEQUENCE)
175 p = alg->parameter->value.sequence->data;
176 plen = alg->parameter->value.sequence->length;
177 kekalg = d2i_X509_ALGOR(NULL, &p, plen);
180 kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
183 OBJ_obj2txt(name, sizeof(name), kekalg->algorithm, 0);
184 kekcipher = EVP_CIPHER_fetch(pctx->libctx, name, pctx->propquery);
185 if (kekcipher == NULL || EVP_CIPHER_get_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
187 if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
189 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
192 keylen = EVP_CIPHER_CTX_get_key_length(kekctx);
193 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
196 plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
201 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
207 EVP_CIPHER_free(kekcipher);
208 X509_ALGOR_free(kekalg);
213 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
217 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
220 /* See if we need to set peer key */
221 if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
223 ASN1_BIT_STRING *pubkey;
225 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
228 if (alg == NULL || pubkey == NULL)
230 if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
231 ERR_raise(ERR_LIB_CMS, CMS_R_PEER_KEY_ERROR);
235 /* Set ECDH derivation parameters and initialise unwrap context */
236 if (!ecdh_cms_set_shared_info(pctx, ri)) {
237 ERR_raise(ERR_LIB_CMS, CMS_R_SHARED_INFO_ERROR);
243 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
249 X509_ALGOR *talg, *wrap_alg = NULL;
250 const ASN1_OBJECT *aoid;
251 ASN1_BIT_STRING *pubkey;
252 ASN1_STRING *wrap_str;
253 ASN1_OCTET_STRING *ukm;
254 unsigned char *penc = NULL;
257 int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
258 const EVP_MD *kdf_md;
260 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
263 /* Get ephemeral key */
264 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
265 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
268 X509_ALGOR_get0(&aoid, NULL, NULL, talg);
269 /* Is everything uninitialised? */
270 if (aoid == OBJ_nid2obj(NID_undef)) {
273 penclen = EVP_PKEY_get1_encoded_public_key(pkey, &penc);
274 ASN1_STRING_set0(pubkey, penc, penclen);
275 pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
276 pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
279 X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
283 /* See if custom parameters set */
284 kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
287 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
289 ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
292 else if (ecdh_nid == 0)
293 ecdh_nid = NID_dh_std_kdf;
294 else if (ecdh_nid == 1)
295 ecdh_nid = NID_dh_cofactor_kdf;
297 if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
298 kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
299 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
304 if (kdf_md == NULL) {
305 /* Fixme later for better MD */
307 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
311 if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
314 /* Lookup NID for KDF+cofactor+digest */
316 if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_get_type(kdf_md), ecdh_nid))
319 ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
320 wrap_nid = EVP_CIPHER_CTX_get_type(ctx);
321 keylen = EVP_CIPHER_CTX_get_key_length(ctx);
323 /* Package wrap algorithm in an AlgorithmIdentifier */
325 wrap_alg = X509_ALGOR_new();
326 if (wrap_alg == NULL)
328 wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
329 wrap_alg->parameter = ASN1_TYPE_new();
330 if (wrap_alg->parameter == NULL)
332 if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
334 if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
335 ASN1_TYPE_free(wrap_alg->parameter);
336 wrap_alg->parameter = NULL;
339 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
342 penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
347 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
352 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
353 * of another AlgorithmIdentifier.
355 penclen = i2d_X509_ALGOR(wrap_alg, &penc);
356 if (penc == NULL || penclen == 0)
358 wrap_str = ASN1_STRING_new();
359 if (wrap_str == NULL)
361 ASN1_STRING_set0(wrap_str, penc, penclen);
363 X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
369 X509_ALGOR_free(wrap_alg);
373 int ossl_cms_ecdh_envelope(CMS_RecipientInfo *ri, int decrypt)
375 assert(decrypt == 0 || decrypt == 1);
378 return ecdh_cms_decrypt(ri);
381 return ecdh_cms_encrypt(ri);
383 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
387 /* ECDSA and DSA implementation is the same */
388 int ossl_cms_ecdsa_dsa_sign(CMS_SignerInfo *si, int verify)
390 assert(verify == 0 || verify == 1);
394 X509_ALGOR *alg1, *alg2;
395 EVP_PKEY *pkey = si->pkey;
397 CMS_SignerInfo_get0_algs(si, NULL, NULL, &alg1, &alg2);
398 if (alg1 == NULL || alg1->algorithm == NULL)
400 hnid = OBJ_obj2nid(alg1->algorithm);
401 if (hnid == NID_undef)
403 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_get_id(pkey)))
405 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);