cms_dh.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include <assert.h>
  10. #include <openssl/cms.h>
  11. #include <openssl/dh.h>
  12. #include <openssl/err.h>
  13. #include <openssl/core_names.h>
  14. #include "internal/sizes.h"
  15. #include "crypto/asn1.h"
  16. #include "crypto/evp.h"
  17. #include "cms_local.h"
  18. static int dh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
  19. X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
  20. {
  21. const ASN1_OBJECT *aoid;
  22. int atype;
  23. const void *aval;
  24. ASN1_INTEGER *public_key = NULL;
  25. int rv = 0;
  26. EVP_PKEY *pkpeer = NULL, *pk = NULL;
  27. BIGNUM *bnpub = NULL;
  28. const unsigned char *p;
  29. unsigned char *buf = NULL;
  30. int plen;
  31. X509_ALGOR_get0(&aoid, &atype, &aval, alg);
  32. if (OBJ_obj2nid(aoid) != NID_dhpublicnumber)
  33. goto err;
  34. /* Only absent parameters allowed in RFC XXXX */
  35. if (atype != V_ASN1_UNDEF && atype == V_ASN1_NULL)
  36. goto err;
  37. pk = EVP_PKEY_CTX_get0_pkey(pctx);
  38. if (pk == NULL || !EVP_PKEY_is_a(pk, "DHX"))
  39. goto err;
  40. /* Get public key */
  41. plen = ASN1_STRING_length(pubkey);
  42. p = ASN1_STRING_get0_data(pubkey);
  43. if (p == NULL || plen == 0)
  44. goto err;
  45. if ((public_key = d2i_ASN1_INTEGER(NULL, &p, plen)) == NULL)
  46. goto err;
  47. /*
  48. * Pad to full p parameter size as that is checked by
  49. * EVP_PKEY_set1_encoded_public_key()
  50. */
  51. plen = EVP_PKEY_get_size(pk);
  52. if ((bnpub = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL)
  53. goto err;
  54. if ((buf = OPENSSL_malloc(plen)) == NULL)
  55. goto err;
  56. if (BN_bn2binpad(bnpub, buf, plen) < 0)
  57. goto err;
  58. pkpeer = EVP_PKEY_new();
  59. if (pkpeer == NULL
  60. || !EVP_PKEY_copy_parameters(pkpeer, pk)
  61. || !EVP_PKEY_set1_encoded_public_key(pkpeer, buf, plen))
  62. goto err;
  63. if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
  64. rv = 1;
  65. err:
  66. ASN1_INTEGER_free(public_key);
  67. BN_free(bnpub);
  68. OPENSSL_free(buf);
  69. EVP_PKEY_free(pkpeer);
  70. return rv;
  71. }
  72. static int dh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
  73. {
  74. int rv = 0;
  75. X509_ALGOR *alg, *kekalg = NULL;
  76. ASN1_OCTET_STRING *ukm;
  77. const unsigned char *p;
  78. unsigned char *dukm = NULL;
  79. size_t dukmlen = 0;
  80. int keylen, plen;
  81. EVP_CIPHER *kekcipher = NULL;
  82. EVP_CIPHER_CTX *kekctx;
  83. char name[OSSL_MAX_NAME_SIZE];
  84. if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
  85. goto err;
  86. /*
  87. * For DH we only have one OID permissible. If ever any more get defined
  88. * we will need something cleverer.
  89. */
  90. if (OBJ_obj2nid(alg->algorithm) != NID_id_smime_alg_ESDH) {
  91. ERR_raise(ERR_LIB_CMS, CMS_R_KDF_PARAMETER_ERROR);
  92. goto err;
  93. }
  94. if (EVP_PKEY_CTX_set_dh_kdf_type(pctx, EVP_PKEY_DH_KDF_X9_42) <= 0
  95. || EVP_PKEY_CTX_set_dh_kdf_md(pctx, EVP_sha1()) <= 0)
  96. goto err;
  97. if (alg->parameter->type != V_ASN1_SEQUENCE)
  98. goto err;
  99. p = alg->parameter->value.sequence->data;
  100. plen = alg->parameter->value.sequence->length;
  101. kekalg = d2i_X509_ALGOR(NULL, &p, plen);
  102. if (kekalg == NULL)
  103. goto err;
  104. kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  105. if (kekctx == NULL)
  106. goto err;
  107. if (OBJ_obj2txt(name, sizeof(name), kekalg->algorithm, 0) <= 0)
  108. goto err;
  109. kekcipher = EVP_CIPHER_fetch(pctx->libctx, name, pctx->propquery);
  110. if (kekcipher == NULL
  111. || EVP_CIPHER_get_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
  112. goto err;
  113. if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
  114. goto err;
  115. if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
  116. goto err;
  117. keylen = EVP_CIPHER_CTX_get_key_length(kekctx);
  118. if (EVP_PKEY_CTX_set_dh_kdf_outlen(pctx, keylen) <= 0)
  119. goto err;
  120. /* Use OBJ_nid2obj to ensure we use built in OID that isn't freed */
  121. if (EVP_PKEY_CTX_set0_dh_kdf_oid(pctx,
  122. OBJ_nid2obj(EVP_CIPHER_get_type(kekcipher)))
  123. <= 0)
  124. goto err;
  125. if (ukm != NULL) {
  126. dukmlen = ASN1_STRING_length(ukm);
  127. dukm = OPENSSL_memdup(ASN1_STRING_get0_data(ukm), dukmlen);
  128. if (dukm == NULL)
  129. goto err;
  130. }
  131. if (EVP_PKEY_CTX_set0_dh_kdf_ukm(pctx, dukm, dukmlen) <= 0)
  132. goto err;
  133. dukm = NULL;
  134. rv = 1;
  135. err:
  136. X509_ALGOR_free(kekalg);
  137. EVP_CIPHER_free(kekcipher);
  138. OPENSSL_free(dukm);
  139. return rv;
  140. }
  141. static int dh_cms_decrypt(CMS_RecipientInfo *ri)
  142. {
  143. EVP_PKEY_CTX *pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  144. if (pctx == NULL)
  145. return 0;
  146. /* See if we need to set peer key */
  147. if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
  148. X509_ALGOR *alg;
  149. ASN1_BIT_STRING *pubkey;
  150. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
  151. NULL, NULL, NULL))
  152. return 0;
  153. if (alg == NULL || pubkey == NULL)
  154. return 0;
  155. if (!dh_cms_set_peerkey(pctx, alg, pubkey)) {
  156. ERR_raise(ERR_LIB_CMS, CMS_R_PEER_KEY_ERROR);
  157. return 0;
  158. }
  159. }
  160. /* Set DH derivation parameters and initialise unwrap context */
  161. if (!dh_cms_set_shared_info(pctx, ri)) {
  162. ERR_raise(ERR_LIB_CMS, CMS_R_SHARED_INFO_ERROR);
  163. return 0;
  164. }
  165. return 1;
  166. }
  167. static int dh_cms_encrypt(CMS_RecipientInfo *ri)
  168. {
  169. EVP_PKEY_CTX *pctx;
  170. EVP_PKEY *pkey;
  171. EVP_CIPHER_CTX *ctx;
  172. int keylen;
  173. X509_ALGOR *talg, *wrap_alg = NULL;
  174. const ASN1_OBJECT *aoid;
  175. ASN1_BIT_STRING *pubkey;
  176. ASN1_STRING *wrap_str;
  177. ASN1_OCTET_STRING *ukm;
  178. unsigned char *penc = NULL, *dukm = NULL;
  179. int penclen;
  180. size_t dukmlen = 0;
  181. int rv = 0;
  182. int kdf_type, wrap_nid;
  183. const EVP_MD *kdf_md;
  184. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  185. if (pctx == NULL)
  186. return 0;
  187. /* Get ephemeral key */
  188. pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  189. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
  190. NULL, NULL, NULL))
  191. goto err;
  192. /* Is everything uninitialised? */
  193. X509_ALGOR_get0(&aoid, NULL, NULL, talg);
  194. if (aoid == OBJ_nid2obj(NID_undef)) {
  195. BIGNUM *bn_pub_key = NULL;
  196. ASN1_INTEGER *pubk;
  197. if (!EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PUB_KEY, &bn_pub_key))
  198. goto err;
  199. pubk = BN_to_ASN1_INTEGER(bn_pub_key, NULL);
  200. BN_free(bn_pub_key);
  201. if (pubk == NULL)
  202. goto err;
  203. /* Set the key */
  204. penclen = i2d_ASN1_INTEGER(pubk, &penc);
  205. ASN1_INTEGER_free(pubk);
  206. if (penclen <= 0)
  207. goto err;
  208. ASN1_STRING_set0(pubkey, penc, penclen);
  209. ossl_asn1_string_set_bits_left(pubkey, 0);
  210. penc = NULL;
  211. (void)X509_ALGOR_set0(talg, OBJ_nid2obj(NID_dhpublicnumber),
  212. V_ASN1_UNDEF, NULL); /* cannot fail */
  213. }
  214. /* See if custom parameters set */
  215. kdf_type = EVP_PKEY_CTX_get_dh_kdf_type(pctx);
  216. if (kdf_type <= 0 || EVP_PKEY_CTX_get_dh_kdf_md(pctx, &kdf_md) <= 0)
  217. goto err;
  218. if (kdf_type == EVP_PKEY_DH_KDF_NONE) {
  219. kdf_type = EVP_PKEY_DH_KDF_X9_42;
  220. if (EVP_PKEY_CTX_set_dh_kdf_type(pctx, kdf_type) <= 0)
  221. goto err;
  222. } else if (kdf_type != EVP_PKEY_DH_KDF_X9_42)
  223. /* Unknown KDF */
  224. goto err;
  225. if (kdf_md == NULL) {
  226. /* Only SHA1 supported */
  227. kdf_md = EVP_sha1();
  228. if (EVP_PKEY_CTX_set_dh_kdf_md(pctx, kdf_md) <= 0)
  229. goto err;
  230. } else if (EVP_MD_get_type(kdf_md) != NID_sha1)
  231. /* Unsupported digest */
  232. goto err;
  233. if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
  234. goto err;
  235. /* Get wrap NID */
  236. ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  237. wrap_nid = EVP_CIPHER_CTX_get_type(ctx);
  238. if (EVP_PKEY_CTX_set0_dh_kdf_oid(pctx, OBJ_nid2obj(wrap_nid)) <= 0)
  239. goto err;
  240. keylen = EVP_CIPHER_CTX_get_key_length(ctx);
  241. /* Package wrap algorithm in an AlgorithmIdentifier */
  242. wrap_alg = X509_ALGOR_new();
  243. if (wrap_alg == NULL)
  244. goto err;
  245. wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
  246. wrap_alg->parameter = ASN1_TYPE_new();
  247. if (wrap_alg->parameter == NULL)
  248. goto err;
  249. if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
  250. goto err;
  251. if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
  252. ASN1_TYPE_free(wrap_alg->parameter);
  253. wrap_alg->parameter = NULL;
  254. }
  255. if (EVP_PKEY_CTX_set_dh_kdf_outlen(pctx, keylen) <= 0)
  256. goto err;
  257. if (ukm != NULL) {
  258. dukmlen = ASN1_STRING_length(ukm);
  259. dukm = OPENSSL_memdup(ASN1_STRING_get0_data(ukm), dukmlen);
  260. if (dukm == NULL)
  261. goto err;
  262. }
  263. if (EVP_PKEY_CTX_set0_dh_kdf_ukm(pctx, dukm, dukmlen) <= 0)
  264. goto err;
  265. dukm = NULL;
  266. /*
  267. * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
  268. * of another AlgorithmIdentifier.
  269. */
  270. penc = NULL;
  271. penclen = i2d_X509_ALGOR(wrap_alg, &penc);
  272. if (penc == NULL || penclen == 0)
  273. goto err;
  274. wrap_str = ASN1_STRING_new();
  275. if (wrap_str == NULL)
  276. goto err;
  277. ASN1_STRING_set0(wrap_str, penc, penclen);
  278. penc = NULL;
  279. rv = X509_ALGOR_set0(talg, OBJ_nid2obj(NID_id_smime_alg_ESDH),
  280. V_ASN1_SEQUENCE, wrap_str);
  281. if (!rv)
  282. ASN1_STRING_free(wrap_str);
  283. err:
  284. OPENSSL_free(penc);
  285. X509_ALGOR_free(wrap_alg);
  286. OPENSSL_free(dukm);
  287. return rv;
  288. }
  289. int ossl_cms_dh_envelope(CMS_RecipientInfo *ri, int decrypt)
  290. {
  291. assert(decrypt == 0 || decrypt == 1);
  292. if (decrypt == 1)
  293. return dh_cms_decrypt(ri);
  294. if (decrypt == 0)
  295. return dh_cms_encrypt(ri);
  296. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  297. return 0;
  298. }