evp_pbe.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 1999-2023 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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/evp.h>
  12. #include <openssl/core.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/pkcs12.h>
  15. #include <openssl/x509.h>
  16. #include "crypto/evp.h"
  17. #include "evp_local.h"
  18. /* Password based encryption (PBE) functions */
  19. /* Setup a cipher context from a PBE algorithm */
  20. struct evp_pbe_st {
  21. int pbe_type;
  22. int pbe_nid;
  23. int cipher_nid;
  24. int md_nid;
  25. EVP_PBE_KEYGEN *keygen;
  26. EVP_PBE_KEYGEN_EX *keygen_ex;
  27. };
  28. static STACK_OF(EVP_PBE_CTL) *pbe_algs;
  29. static const EVP_PBE_CTL builtin_pbe[] = {
  30. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndDES_CBC,
  31. NID_des_cbc, NID_md2, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
  32. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC,
  33. NID_des_cbc, NID_md5, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
  34. {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndRC2_CBC,
  35. NID_rc2_64_cbc, NID_sha1, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
  36. {EVP_PBE_TYPE_OUTER, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen},
  37. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC4,
  38. NID_rc4, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
  39. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC4,
  40. NID_rc4_40, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
  41. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  42. NID_des_ede3_cbc, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
  43. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And2_Key_TripleDES_CBC,
  44. NID_des_ede_cbc, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
  45. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC2_CBC,
  46. NID_rc2_cbc, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
  47. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC2_CBC,
  48. NID_rc2_40_cbc, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
  49. {EVP_PBE_TYPE_OUTER, NID_pbes2, -1, -1, PKCS5_v2_PBE_keyivgen, &PKCS5_v2_PBE_keyivgen_ex},
  50. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndRC2_CBC,
  51. NID_rc2_64_cbc, NID_md2, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
  52. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndRC2_CBC,
  53. NID_rc2_64_cbc, NID_md5, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
  54. {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndDES_CBC,
  55. NID_des_cbc, NID_sha1, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
  56. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA1, -1, NID_sha1, 0},
  57. {EVP_PBE_TYPE_PRF, NID_hmac_md5, -1, NID_md5, 0},
  58. {EVP_PBE_TYPE_PRF, NID_hmac_sha1, -1, NID_sha1, 0},
  59. {EVP_PBE_TYPE_PRF, NID_hmacWithMD5, -1, NID_md5, 0},
  60. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA224, -1, NID_sha224, 0},
  61. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA256, -1, NID_sha256, 0},
  62. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA384, -1, NID_sha384, 0},
  63. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA512, -1, NID_sha512, 0},
  64. {EVP_PBE_TYPE_PRF, NID_id_HMACGostR3411_94, -1, NID_id_GostR3411_94, 0},
  65. {EVP_PBE_TYPE_PRF, NID_id_tc26_hmac_gost_3411_2012_256, -1,
  66. NID_id_GostR3411_2012_256, 0},
  67. {EVP_PBE_TYPE_PRF, NID_id_tc26_hmac_gost_3411_2012_512, -1,
  68. NID_id_GostR3411_2012_512, 0},
  69. {EVP_PBE_TYPE_PRF, NID_hmac_sha3_224, -1, NID_sha3_224, 0},
  70. {EVP_PBE_TYPE_PRF, NID_hmac_sha3_256, -1, NID_sha3_256, 0},
  71. {EVP_PBE_TYPE_PRF, NID_hmac_sha3_384, -1, NID_sha3_384, 0},
  72. {EVP_PBE_TYPE_PRF, NID_hmac_sha3_512, -1, NID_sha3_512, 0},
  73. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA512_224, -1, NID_sha512_224, 0},
  74. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA512_256, -1, NID_sha512_256, 0},
  75. #ifndef OPENSSL_NO_SM3
  76. {EVP_PBE_TYPE_PRF, NID_hmacWithSM3, -1, NID_sm3, 0},
  77. #endif
  78. {EVP_PBE_TYPE_KDF, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen, &PKCS5_v2_PBKDF2_keyivgen_ex},
  79. #ifndef OPENSSL_NO_SCRYPT
  80. {EVP_PBE_TYPE_KDF, NID_id_scrypt, -1, -1, PKCS5_v2_scrypt_keyivgen, &PKCS5_v2_scrypt_keyivgen_ex}
  81. #endif
  82. };
  83. int EVP_PBE_CipherInit_ex(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
  84. ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de,
  85. OSSL_LIB_CTX *libctx, const char *propq)
  86. {
  87. const EVP_CIPHER *cipher = NULL;
  88. EVP_CIPHER *cipher_fetch = NULL;
  89. const EVP_MD *md = NULL;
  90. EVP_MD *md_fetch = NULL;
  91. int ret = 0, cipher_nid, md_nid;
  92. EVP_PBE_KEYGEN_EX *keygen_ex;
  93. EVP_PBE_KEYGEN *keygen;
  94. if (!EVP_PBE_find_ex(EVP_PBE_TYPE_OUTER, OBJ_obj2nid(pbe_obj),
  95. &cipher_nid, &md_nid, &keygen, &keygen_ex)) {
  96. char obj_tmp[80];
  97. if (pbe_obj == NULL)
  98. OPENSSL_strlcpy(obj_tmp, "NULL", sizeof(obj_tmp));
  99. else
  100. i2t_ASN1_OBJECT(obj_tmp, sizeof(obj_tmp), pbe_obj);
  101. ERR_raise_data(ERR_LIB_EVP, EVP_R_UNKNOWN_PBE_ALGORITHM,
  102. "TYPE=%s", obj_tmp);
  103. goto err;
  104. }
  105. if (pass == NULL)
  106. passlen = 0;
  107. else if (passlen == -1)
  108. passlen = strlen(pass);
  109. if (cipher_nid != -1) {
  110. (void)ERR_set_mark();
  111. cipher = cipher_fetch = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(cipher_nid), propq);
  112. /* Fallback to legacy method */
  113. if (cipher == NULL)
  114. cipher = EVP_get_cipherbynid(cipher_nid);
  115. if (cipher == NULL) {
  116. (void)ERR_clear_last_mark();
  117. ERR_raise_data(ERR_LIB_EVP, EVP_R_UNKNOWN_CIPHER,
  118. OBJ_nid2sn(cipher_nid));
  119. goto err;
  120. }
  121. (void)ERR_pop_to_mark();
  122. }
  123. if (md_nid != -1) {
  124. (void)ERR_set_mark();
  125. md = md_fetch = EVP_MD_fetch(libctx, OBJ_nid2sn(md_nid), propq);
  126. /* Fallback to legacy method */
  127. if (md == NULL)
  128. md = EVP_get_digestbynid(md_nid);
  129. if (md == NULL) {
  130. (void)ERR_clear_last_mark();
  131. ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_DIGEST);
  132. goto err;
  133. }
  134. (void)ERR_pop_to_mark();
  135. }
  136. /* Try extended keygen with libctx/propq first, fall back to legacy keygen */
  137. if (keygen_ex != NULL)
  138. ret = keygen_ex(ctx, pass, passlen, param, cipher, md, en_de, libctx, propq);
  139. else
  140. ret = keygen(ctx, pass, passlen, param, cipher, md, en_de);
  141. err:
  142. EVP_CIPHER_free(cipher_fetch);
  143. EVP_MD_free(md_fetch);
  144. return ret;
  145. }
  146. int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
  147. ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
  148. {
  149. return EVP_PBE_CipherInit_ex(pbe_obj, pass, passlen, param, ctx, en_de, NULL, NULL);
  150. }
  151. DECLARE_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
  152. static int pbe2_cmp(const EVP_PBE_CTL *pbe1, const EVP_PBE_CTL *pbe2)
  153. {
  154. int ret = pbe1->pbe_type - pbe2->pbe_type;
  155. if (ret)
  156. return ret;
  157. else
  158. return pbe1->pbe_nid - pbe2->pbe_nid;
  159. }
  160. IMPLEMENT_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
  161. static int pbe_cmp(const EVP_PBE_CTL *const *a, const EVP_PBE_CTL *const *b)
  162. {
  163. int ret = (*a)->pbe_type - (*b)->pbe_type;
  164. if (ret)
  165. return ret;
  166. else
  167. return (*a)->pbe_nid - (*b)->pbe_nid;
  168. }
  169. /* Add a PBE algorithm */
  170. int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid,
  171. int md_nid, EVP_PBE_KEYGEN *keygen)
  172. {
  173. EVP_PBE_CTL *pbe_tmp = NULL;
  174. if (pbe_algs == NULL) {
  175. pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp);
  176. if (pbe_algs == NULL) {
  177. ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
  178. goto err;
  179. }
  180. }
  181. if ((pbe_tmp = OPENSSL_zalloc(sizeof(*pbe_tmp))) == NULL)
  182. goto err;
  183. pbe_tmp->pbe_type = pbe_type;
  184. pbe_tmp->pbe_nid = pbe_nid;
  185. pbe_tmp->cipher_nid = cipher_nid;
  186. pbe_tmp->md_nid = md_nid;
  187. pbe_tmp->keygen = keygen;
  188. if (!sk_EVP_PBE_CTL_push(pbe_algs, pbe_tmp)) {
  189. ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
  190. goto err;
  191. }
  192. return 1;
  193. err:
  194. OPENSSL_free(pbe_tmp);
  195. return 0;
  196. }
  197. int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
  198. EVP_PBE_KEYGEN *keygen)
  199. {
  200. int cipher_nid, md_nid;
  201. if (cipher)
  202. cipher_nid = EVP_CIPHER_get_nid(cipher);
  203. else
  204. cipher_nid = -1;
  205. if (md)
  206. md_nid = EVP_MD_get_type(md);
  207. else
  208. md_nid = -1;
  209. return EVP_PBE_alg_add_type(EVP_PBE_TYPE_OUTER, nid,
  210. cipher_nid, md_nid, keygen);
  211. }
  212. int EVP_PBE_find_ex(int type, int pbe_nid, int *pcnid, int *pmnid,
  213. EVP_PBE_KEYGEN **pkeygen, EVP_PBE_KEYGEN_EX **pkeygen_ex)
  214. {
  215. EVP_PBE_CTL *pbetmp = NULL, pbelu;
  216. int i;
  217. if (pbe_nid == NID_undef)
  218. return 0;
  219. pbelu.pbe_type = type;
  220. pbelu.pbe_nid = pbe_nid;
  221. if (pbe_algs != NULL) {
  222. /* Ideally, this would be done under lock */
  223. sk_EVP_PBE_CTL_sort(pbe_algs);
  224. i = sk_EVP_PBE_CTL_find(pbe_algs, &pbelu);
  225. pbetmp = sk_EVP_PBE_CTL_value(pbe_algs, i);
  226. }
  227. if (pbetmp == NULL) {
  228. pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe, OSSL_NELEM(builtin_pbe));
  229. }
  230. if (pbetmp == NULL)
  231. return 0;
  232. if (pcnid != NULL)
  233. *pcnid = pbetmp->cipher_nid;
  234. if (pmnid != NULL)
  235. *pmnid = pbetmp->md_nid;
  236. if (pkeygen != NULL)
  237. *pkeygen = pbetmp->keygen;
  238. if (pkeygen_ex != NULL)
  239. *pkeygen_ex = pbetmp->keygen_ex;
  240. return 1;
  241. }
  242. int EVP_PBE_find(int type, int pbe_nid,
  243. int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen)
  244. {
  245. return EVP_PBE_find_ex(type, pbe_nid, pcnid, pmnid, pkeygen, NULL);
  246. }
  247. static void free_evp_pbe_ctl(EVP_PBE_CTL *pbe)
  248. {
  249. OPENSSL_free(pbe);
  250. }
  251. void EVP_PBE_cleanup(void)
  252. {
  253. sk_EVP_PBE_CTL_pop_free(pbe_algs, free_evp_pbe_ctl);
  254. pbe_algs = NULL;
  255. }
  256. int EVP_PBE_get(int *ptype, int *ppbe_nid, size_t num)
  257. {
  258. const EVP_PBE_CTL *tpbe;
  259. if (num >= OSSL_NELEM(builtin_pbe))
  260. return 0;
  261. tpbe = builtin_pbe + num;
  262. if (ptype)
  263. *ptype = tpbe->pbe_type;
  264. if (ppbe_nid)
  265. *ppbe_nid = tpbe->pbe_nid;
  266. return 1;
  267. }