2 * Copyright 2006-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
11 #include <openssl/cms.h>
12 #include <openssl/err.h>
13 #include <openssl/decoder.h>
14 #include "cms_local.h"
15 #include "crypto/evp.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_by_EVP_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 const char *groupname
;
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 groupname
= OBJ_nid2sn(OBJ_obj2nid(poid
));
48 || !EVP_PKEY_CTX_set_group_name(pctx
, groupname
)) {
49 ERR_raise(ERR_LIB_CMS
, CMS_R_DECODE_ERROR
);
52 if (EVP_PKEY_paramgen(pctx
, &pkey
) <= 0)
55 ERR_raise(ERR_LIB_CMS
, CMS_R_DECODE_ERROR
);
63 EVP_PKEY_CTX_free(pctx
);
67 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX
*pctx
,
68 X509_ALGOR
*alg
, ASN1_BIT_STRING
*pubkey
)
70 const ASN1_OBJECT
*aoid
;
74 EVP_PKEY
*pkpeer
= NULL
;
75 const unsigned char *p
;
78 X509_ALGOR_get0(&aoid
, &atype
, &aval
, alg
);
79 if (OBJ_obj2nid(aoid
) != NID_X9_62_id_ecPublicKey
)
82 /* If absent parameters get group from main key */
83 if (atype
== V_ASN1_UNDEF
|| atype
== V_ASN1_NULL
) {
86 pk
= EVP_PKEY_CTX_get0_pkey(pctx
);
90 pkpeer
= EVP_PKEY_new();
93 if (!EVP_PKEY_copy_parameters(pkpeer
, pk
))
96 pkpeer
= pkey_type2param(atype
, aval
,
97 EVP_PKEY_CTX_get0_libctx(pctx
),
98 EVP_PKEY_CTX_get0_propq(pctx
));
102 /* We have parameters now set public key */
103 plen
= ASN1_STRING_length(pubkey
);
104 p
= ASN1_STRING_get0_data(pubkey
);
105 if (p
== NULL
|| plen
== 0)
108 if (!EVP_PKEY_set1_encoded_public_key(pkpeer
, p
, plen
))
111 if (EVP_PKEY_derive_set_peer(pctx
, pkpeer
) > 0)
114 EVP_PKEY_free(pkpeer
);
118 /* Set KDF parameters based on KDF NID */
119 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX
*pctx
, int eckdf_nid
)
121 int kdf_nid
, kdfmd_nid
, cofactor
;
122 const EVP_MD
*kdf_md
;
124 if (eckdf_nid
== NID_undef
)
127 /* Lookup KDF type, cofactor mode and digest */
128 if (!OBJ_find_sigid_algs(eckdf_nid
, &kdfmd_nid
, &kdf_nid
))
131 if (kdf_nid
== NID_dh_std_kdf
)
133 else if (kdf_nid
== NID_dh_cofactor_kdf
)
138 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx
, cofactor
) <= 0)
141 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, EVP_PKEY_ECDH_KDF_X9_63
) <= 0)
144 kdf_md
= EVP_get_digestbynid(kdfmd_nid
);
148 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
153 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX
*pctx
, CMS_RecipientInfo
*ri
)
156 X509_ALGOR
*alg
, *kekalg
= NULL
;
157 ASN1_OCTET_STRING
*ukm
;
158 const unsigned char *p
;
159 unsigned char *der
= NULL
;
161 EVP_CIPHER
*kekcipher
= NULL
;
162 EVP_CIPHER_CTX
*kekctx
;
165 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &alg
, &ukm
))
168 if (!ecdh_cms_set_kdf_param(pctx
, OBJ_obj2nid(alg
->algorithm
))) {
169 ERR_raise(ERR_LIB_CMS
, CMS_R_KDF_PARAMETER_ERROR
);
173 if (alg
->parameter
->type
!= V_ASN1_SEQUENCE
)
176 p
= alg
->parameter
->value
.sequence
->data
;
177 plen
= alg
->parameter
->value
.sequence
->length
;
178 kekalg
= d2i_X509_ALGOR(NULL
, &p
, plen
);
181 kekctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
184 name
= OBJ_nid2sn(OBJ_obj2nid(kekalg
->algorithm
));
185 kekcipher
= EVP_CIPHER_fetch(pctx
->libctx
, name
, pctx
->propquery
);
186 if (kekcipher
== NULL
|| EVP_CIPHER_mode(kekcipher
) != EVP_CIPH_WRAP_MODE
)
188 if (!EVP_EncryptInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
))
190 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0)
193 keylen
= EVP_CIPHER_CTX_key_length(kekctx
);
194 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
197 plen
= CMS_SharedInfo_encode(&der
, kekalg
, ukm
, keylen
);
202 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, der
, plen
) <= 0)
208 EVP_CIPHER_free(kekcipher
);
209 X509_ALGOR_free(kekalg
);
214 static int ecdh_cms_decrypt(CMS_RecipientInfo
*ri
)
218 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
221 /* See if we need to set peer key */
222 if (!EVP_PKEY_CTX_get0_peerkey(pctx
)) {
224 ASN1_BIT_STRING
*pubkey
;
226 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &alg
, &pubkey
,
229 if (alg
== NULL
|| pubkey
== NULL
)
231 if (!ecdh_cms_set_peerkey(pctx
, alg
, pubkey
)) {
232 ERR_raise(ERR_LIB_CMS
, CMS_R_PEER_KEY_ERROR
);
236 /* Set ECDH derivation parameters and initialise unwrap context */
237 if (!ecdh_cms_set_shared_info(pctx
, ri
)) {
238 ERR_raise(ERR_LIB_CMS
, CMS_R_SHARED_INFO_ERROR
);
244 static int ecdh_cms_encrypt(CMS_RecipientInfo
*ri
)
250 X509_ALGOR
*talg
, *wrap_alg
= NULL
;
251 const ASN1_OBJECT
*aoid
;
252 ASN1_BIT_STRING
*pubkey
;
253 ASN1_STRING
*wrap_str
;
254 ASN1_OCTET_STRING
*ukm
;
255 unsigned char *penc
= NULL
;
258 int ecdh_nid
, kdf_type
, kdf_nid
, wrap_nid
;
259 const EVP_MD
*kdf_md
;
261 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
264 /* Get ephemeral key */
265 pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
266 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &talg
, &pubkey
,
269 X509_ALGOR_get0(&aoid
, NULL
, NULL
, talg
);
270 /* Is everything uninitialised? */
271 if (aoid
== OBJ_nid2obj(NID_undef
)) {
274 penclen
= EVP_PKEY_get1_encoded_public_key(pkey
, &penc
);
275 ASN1_STRING_set0(pubkey
, penc
, penclen
);
276 pubkey
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
277 pubkey
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
280 X509_ALGOR_set0(talg
, OBJ_nid2obj(NID_X9_62_id_ecPublicKey
),
284 /* See if custom parameters set */
285 kdf_type
= EVP_PKEY_CTX_get_ecdh_kdf_type(pctx
);
288 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx
, &kdf_md
))
290 ecdh_nid
= EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx
);
293 else if (ecdh_nid
== 0)
294 ecdh_nid
= NID_dh_std_kdf
;
295 else if (ecdh_nid
== 1)
296 ecdh_nid
= NID_dh_cofactor_kdf
;
298 if (kdf_type
== EVP_PKEY_ECDH_KDF_NONE
) {
299 kdf_type
= EVP_PKEY_ECDH_KDF_X9_63
;
300 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, kdf_type
) <= 0)
305 if (kdf_md
== NULL
) {
306 /* Fixme later for better MD */
308 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
312 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &talg
, &ukm
))
315 /* Lookup NID for KDF+cofactor+digest */
317 if (!OBJ_find_sigid_by_algs(&kdf_nid
, EVP_MD_type(kdf_md
), ecdh_nid
))
320 ctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
321 wrap_nid
= EVP_CIPHER_CTX_type(ctx
);
322 keylen
= EVP_CIPHER_CTX_key_length(ctx
);
324 /* Package wrap algorithm in an AlgorithmIdentifier */
326 wrap_alg
= X509_ALGOR_new();
327 if (wrap_alg
== NULL
)
329 wrap_alg
->algorithm
= OBJ_nid2obj(wrap_nid
);
330 wrap_alg
->parameter
= ASN1_TYPE_new();
331 if (wrap_alg
->parameter
== NULL
)
333 if (EVP_CIPHER_param_to_asn1(ctx
, wrap_alg
->parameter
) <= 0)
335 if (ASN1_TYPE_get(wrap_alg
->parameter
) == NID_undef
) {
336 ASN1_TYPE_free(wrap_alg
->parameter
);
337 wrap_alg
->parameter
= NULL
;
340 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
343 penclen
= CMS_SharedInfo_encode(&penc
, wrap_alg
, ukm
, keylen
);
348 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, penc
, penclen
) <= 0)
353 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
354 * of another AlgorithmIdentifier.
356 penclen
= i2d_X509_ALGOR(wrap_alg
, &penc
);
357 if (penc
== NULL
|| penclen
== 0)
359 wrap_str
= ASN1_STRING_new();
360 if (wrap_str
== NULL
)
362 ASN1_STRING_set0(wrap_str
, penc
, penclen
);
364 X509_ALGOR_set0(talg
, OBJ_nid2obj(kdf_nid
), V_ASN1_SEQUENCE
, wrap_str
);
370 X509_ALGOR_free(wrap_alg
);
374 int cms_ecdh_envelope(CMS_RecipientInfo
*ri
, int decrypt
)
376 assert(decrypt
== 0 || decrypt
== 1);
379 return ecdh_cms_decrypt(ri
);
382 return ecdh_cms_encrypt(ri
);
384 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
389 /* ECDSA and DSA implementation is the same */
390 int cms_ecdsa_dsa_sign(CMS_SignerInfo
*si
, int verify
)
392 assert(verify
== 0 || verify
== 1);
396 X509_ALGOR
*alg1
, *alg2
;
397 EVP_PKEY
*pkey
= si
->pkey
;
399 CMS_SignerInfo_get0_algs(si
, NULL
, NULL
, &alg1
, &alg2
);
400 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
402 hnid
= OBJ_obj2nid(alg1
->algorithm
);
403 if (hnid
== NID_undef
)
405 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
407 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);