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/dh.h>
13 #include <openssl/err.h>
14 #include <openssl/core_names.h>
15 #include "cms_local.h"
17 static int dh_cms_set_peerkey(EVP_PKEY_CTX
*pctx
,
18 X509_ALGOR
*alg
, ASN1_BIT_STRING
*pubkey
)
20 const ASN1_OBJECT
*aoid
;
23 ASN1_INTEGER
*public_key
= NULL
;
25 EVP_PKEY
*pkpeer
= NULL
, *pk
= NULL
;
26 const unsigned char *p
;
29 X509_ALGOR_get0(&aoid
, &atype
, &aval
, alg
);
30 if (OBJ_obj2nid(aoid
) != NID_dhpublicnumber
)
32 /* Only absent parameters allowed in RFC XXXX */
33 if (atype
!= V_ASN1_UNDEF
&& atype
== V_ASN1_NULL
)
36 pk
= EVP_PKEY_CTX_get0_pkey(pctx
);
37 if (pk
== NULL
|| !EVP_PKEY_is_a(pk
, "DHX"))
41 plen
= ASN1_STRING_length(pubkey
);
42 p
= ASN1_STRING_get0_data(pubkey
);
43 if (p
== NULL
|| plen
== 0)
46 pkpeer
= EVP_PKEY_new();
48 || !EVP_PKEY_copy_parameters(pkpeer
, pk
)
49 || !EVP_PKEY_set1_encoded_public_key(pkpeer
, p
, plen
))
52 if (EVP_PKEY_derive_set_peer(pctx
, pkpeer
) > 0)
55 ASN1_INTEGER_free(public_key
);
56 EVP_PKEY_free(pkpeer
);
60 static int dh_cms_set_shared_info(EVP_PKEY_CTX
*pctx
, CMS_RecipientInfo
*ri
)
63 X509_ALGOR
*alg
, *kekalg
= NULL
;
64 ASN1_OCTET_STRING
*ukm
;
65 const unsigned char *p
;
66 unsigned char *dukm
= NULL
;
69 const EVP_CIPHER
*kekcipher
;
70 EVP_CIPHER_CTX
*kekctx
;
72 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &alg
, &ukm
))
76 * For DH we only have one OID permissible. If ever any more get defined
77 * we will need something cleverer.
79 if (OBJ_obj2nid(alg
->algorithm
) != NID_id_smime_alg_ESDH
) {
80 CMSerr(0, CMS_R_KDF_PARAMETER_ERROR
);
84 if (EVP_PKEY_CTX_set_dh_kdf_type(pctx
, EVP_PKEY_DH_KDF_X9_42
) <= 0
85 || EVP_PKEY_CTX_set_dh_kdf_md(pctx
, EVP_sha1()) <= 0)
88 if (alg
->parameter
->type
!= V_ASN1_SEQUENCE
)
91 p
= alg
->parameter
->value
.sequence
->data
;
92 plen
= alg
->parameter
->value
.sequence
->length
;
93 kekalg
= d2i_X509_ALGOR(NULL
, &p
, plen
);
96 kekctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
99 kekcipher
= EVP_get_cipherbyobj(kekalg
->algorithm
);
100 if (kekcipher
== NULL
|| EVP_CIPHER_mode(kekcipher
) != EVP_CIPH_WRAP_MODE
)
102 if (!EVP_EncryptInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
))
104 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0)
107 keylen
= EVP_CIPHER_CTX_key_length(kekctx
);
108 if (EVP_PKEY_CTX_set_dh_kdf_outlen(pctx
, keylen
) <= 0)
110 /* Use OBJ_nid2obj to ensure we use built in OID that isn't freed */
111 if (EVP_PKEY_CTX_set0_dh_kdf_oid(pctx
,
112 OBJ_nid2obj(EVP_CIPHER_type(kekcipher
)))
117 dukmlen
= ASN1_STRING_length(ukm
);
118 dukm
= OPENSSL_memdup(ASN1_STRING_get0_data(ukm
), dukmlen
);
123 if (EVP_PKEY_CTX_set0_dh_kdf_ukm(pctx
, dukm
, dukmlen
) <= 0)
129 X509_ALGOR_free(kekalg
);
134 static int dh_cms_decrypt(CMS_RecipientInfo
*ri
)
136 EVP_PKEY_CTX
*pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
140 /* See if we need to set peer key */
141 if (!EVP_PKEY_CTX_get0_peerkey(pctx
)) {
143 ASN1_BIT_STRING
*pubkey
;
145 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &alg
, &pubkey
,
148 if (alg
== NULL
|| pubkey
== NULL
)
150 if (!dh_cms_set_peerkey(pctx
, alg
, pubkey
)) {
151 DHerr(DH_F_DH_CMS_DECRYPT
, DH_R_PEER_KEY_ERROR
);
155 /* Set DH derivation parameters and initialise unwrap context */
156 if (!dh_cms_set_shared_info(pctx
, ri
)) {
157 DHerr(DH_F_DH_CMS_DECRYPT
, DH_R_SHARED_INFO_ERROR
);
163 static int dh_cms_encrypt(CMS_RecipientInfo
*ri
)
169 X509_ALGOR
*talg
, *wrap_alg
= NULL
;
170 const ASN1_OBJECT
*aoid
;
171 ASN1_BIT_STRING
*pubkey
;
172 ASN1_STRING
*wrap_str
;
173 ASN1_OCTET_STRING
*ukm
;
174 unsigned char *penc
= NULL
, *dukm
= NULL
;
178 int kdf_type
, wrap_nid
;
179 const EVP_MD
*kdf_md
;
181 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
184 /* Get ephemeral key */
185 pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
186 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &talg
, &pubkey
,
190 /* Is everything uninitialised? */
191 X509_ALGOR_get0(&aoid
, NULL
, NULL
, talg
);
192 if (aoid
== OBJ_nid2obj(NID_undef
)) {
193 BIGNUM
*bn_pub_key
= NULL
;
196 if (!EVP_PKEY_get_bn_param(pkey
, OSSL_PKEY_PARAM_PUB_KEY
, &bn_pub_key
))
199 pubk
= BN_to_ASN1_INTEGER(bn_pub_key
, NULL
);
205 penclen
= i2d_ASN1_INTEGER(pubk
, &penc
);
206 ASN1_INTEGER_free(pubk
);
209 ASN1_STRING_set0(pubkey
, penc
, penclen
);
210 pubkey
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
211 pubkey
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
214 X509_ALGOR_set0(talg
, OBJ_nid2obj(NID_dhpublicnumber
),
218 /* See if custom parameters set */
219 kdf_type
= EVP_PKEY_CTX_get_dh_kdf_type(pctx
);
220 if (kdf_type
<= 0 || !EVP_PKEY_CTX_get_dh_kdf_md(pctx
, &kdf_md
))
223 if (kdf_type
== EVP_PKEY_DH_KDF_NONE
) {
224 kdf_type
= EVP_PKEY_DH_KDF_X9_42
;
225 if (EVP_PKEY_CTX_set_dh_kdf_type(pctx
, kdf_type
) <= 0)
227 } else if (kdf_type
!= EVP_PKEY_DH_KDF_X9_42
)
230 if (kdf_md
== NULL
) {
231 /* Only SHA1 supported */
233 if (EVP_PKEY_CTX_set_dh_kdf_md(pctx
, kdf_md
) <= 0)
235 } else if (EVP_MD_type(kdf_md
) != NID_sha1
)
236 /* Unsupported digest */
239 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &talg
, &ukm
))
243 ctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
244 wrap_nid
= EVP_CIPHER_CTX_type(ctx
);
245 if (EVP_PKEY_CTX_set0_dh_kdf_oid(pctx
, OBJ_nid2obj(wrap_nid
)) <= 0)
247 keylen
= EVP_CIPHER_CTX_key_length(ctx
);
249 /* Package wrap algorithm in an AlgorithmIdentifier */
251 wrap_alg
= X509_ALGOR_new();
252 if (wrap_alg
== NULL
)
254 wrap_alg
->algorithm
= OBJ_nid2obj(wrap_nid
);
255 wrap_alg
->parameter
= ASN1_TYPE_new();
256 if (wrap_alg
->parameter
== NULL
)
258 if (EVP_CIPHER_param_to_asn1(ctx
, wrap_alg
->parameter
) <= 0)
260 if (ASN1_TYPE_get(wrap_alg
->parameter
) == NID_undef
) {
261 ASN1_TYPE_free(wrap_alg
->parameter
);
262 wrap_alg
->parameter
= NULL
;
265 if (EVP_PKEY_CTX_set_dh_kdf_outlen(pctx
, keylen
) <= 0)
269 dukmlen
= ASN1_STRING_length(ukm
);
270 dukm
= OPENSSL_memdup(ASN1_STRING_get0_data(ukm
), dukmlen
);
275 if (EVP_PKEY_CTX_set0_dh_kdf_ukm(pctx
, dukm
, dukmlen
) <= 0)
280 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
281 * of another AlgorithmIdentifier.
284 penclen
= i2d_X509_ALGOR(wrap_alg
, &penc
);
285 if (penc
== NULL
|| penclen
== 0)
287 wrap_str
= ASN1_STRING_new();
288 if (wrap_str
== NULL
)
290 ASN1_STRING_set0(wrap_str
, penc
, penclen
);
292 X509_ALGOR_set0(talg
, OBJ_nid2obj(NID_id_smime_alg_ESDH
),
293 V_ASN1_SEQUENCE
, wrap_str
);
299 X509_ALGOR_free(wrap_alg
);
304 int cms_dh_envelope(CMS_RecipientInfo
*ri
, int decrypt
)
306 assert(decrypt
== 0 || decrypt
== 1);
309 return dh_cms_decrypt(ri
);
312 return dh_cms_encrypt(ri
);
314 CMSerr(0, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);