evp_pbe.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright 1999-2021 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. {EVP_PBE_TYPE_KDF, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen, &PKCS5_v2_PBKDF2_keyivgen_ex},
  76. #ifndef OPENSSL_NO_SCRYPT
  77. {EVP_PBE_TYPE_KDF, NID_id_scrypt, -1, -1, PKCS5_v2_scrypt_keyivgen, &PKCS5_v2_scrypt_keyivgen_ex}
  78. #endif
  79. };
  80. int EVP_PBE_CipherInit_ex(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
  81. ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de,
  82. OSSL_LIB_CTX *libctx, const char *propq)
  83. {
  84. const EVP_CIPHER *cipher = NULL;
  85. EVP_CIPHER *cipher_fetch = NULL;
  86. const EVP_MD *md = NULL;
  87. EVP_MD *md_fetch = NULL;
  88. int ret = 0, cipher_nid, md_nid;
  89. EVP_PBE_KEYGEN_EX *keygen_ex;
  90. EVP_PBE_KEYGEN *keygen;
  91. if (!EVP_PBE_find_ex(EVP_PBE_TYPE_OUTER, OBJ_obj2nid(pbe_obj),
  92. &cipher_nid, &md_nid, &keygen, &keygen_ex)) {
  93. char obj_tmp[80];
  94. if (pbe_obj == NULL)
  95. OPENSSL_strlcpy(obj_tmp, "NULL", sizeof(obj_tmp));
  96. else
  97. i2t_ASN1_OBJECT(obj_tmp, sizeof(obj_tmp), pbe_obj);
  98. ERR_raise_data(ERR_LIB_EVP, EVP_R_UNKNOWN_PBE_ALGORITHM,
  99. "TYPE=%s", obj_tmp);
  100. goto err;
  101. }
  102. if (pass == NULL)
  103. passlen = 0;
  104. else if (passlen == -1)
  105. passlen = strlen(pass);
  106. if (cipher_nid != -1) {
  107. (void)ERR_set_mark();
  108. cipher = cipher_fetch = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(cipher_nid), propq);
  109. /* Fallback to legacy method */
  110. if (cipher == NULL)
  111. cipher = EVP_get_cipherbynid(cipher_nid);
  112. if (cipher == NULL) {
  113. (void)ERR_clear_last_mark();
  114. ERR_raise_data(ERR_LIB_EVP, EVP_R_UNKNOWN_CIPHER,
  115. OBJ_nid2sn(cipher_nid));
  116. goto err;
  117. }
  118. (void)ERR_pop_to_mark();
  119. }
  120. if (md_nid != -1) {
  121. (void)ERR_set_mark();
  122. md = md_fetch = EVP_MD_fetch(libctx, OBJ_nid2sn(md_nid), propq);
  123. /* Fallback to legacy method */
  124. if (md == NULL)
  125. EVP_get_digestbynid(md_nid);
  126. if (md == NULL) {
  127. (void)ERR_clear_last_mark();
  128. ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_DIGEST);
  129. goto err;
  130. }
  131. (void)ERR_pop_to_mark();
  132. }
  133. /* Try extended keygen with libctx/propq first, fall back to legacy keygen */
  134. if (keygen_ex != NULL)
  135. ret = keygen_ex(ctx, pass, passlen, param, cipher, md, en_de, libctx, propq);
  136. else
  137. ret = keygen(ctx, pass, passlen, param, cipher, md, en_de);
  138. err:
  139. EVP_CIPHER_free(cipher_fetch);
  140. EVP_MD_free(md_fetch);
  141. return ret;
  142. }
  143. int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
  144. ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
  145. {
  146. return EVP_PBE_CipherInit_ex(pbe_obj, pass, passlen, param, ctx, en_de, NULL, NULL);
  147. }
  148. DECLARE_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
  149. static int pbe2_cmp(const EVP_PBE_CTL *pbe1, const EVP_PBE_CTL *pbe2)
  150. {
  151. int ret = pbe1->pbe_type - pbe2->pbe_type;
  152. if (ret)
  153. return ret;
  154. else
  155. return pbe1->pbe_nid - pbe2->pbe_nid;
  156. }
  157. IMPLEMENT_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
  158. static int pbe_cmp(const EVP_PBE_CTL *const *a, const EVP_PBE_CTL *const *b)
  159. {
  160. int ret = (*a)->pbe_type - (*b)->pbe_type;
  161. if (ret)
  162. return ret;
  163. else
  164. return (*a)->pbe_nid - (*b)->pbe_nid;
  165. }
  166. /* Add a PBE algorithm */
  167. int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid,
  168. int md_nid, EVP_PBE_KEYGEN *keygen)
  169. {
  170. EVP_PBE_CTL *pbe_tmp;
  171. if (pbe_algs == NULL) {
  172. pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp);
  173. if (pbe_algs == NULL)
  174. goto err;
  175. }
  176. if ((pbe_tmp = OPENSSL_zalloc(sizeof(*pbe_tmp))) == NULL)
  177. goto err;
  178. pbe_tmp->pbe_type = pbe_type;
  179. pbe_tmp->pbe_nid = pbe_nid;
  180. pbe_tmp->cipher_nid = cipher_nid;
  181. pbe_tmp->md_nid = md_nid;
  182. pbe_tmp->keygen = keygen;
  183. if (!sk_EVP_PBE_CTL_push(pbe_algs, pbe_tmp)) {
  184. OPENSSL_free(pbe_tmp);
  185. goto err;
  186. }
  187. return 1;
  188. err:
  189. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  190. return 0;
  191. }
  192. int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
  193. EVP_PBE_KEYGEN *keygen)
  194. {
  195. int cipher_nid, md_nid;
  196. if (cipher)
  197. cipher_nid = EVP_CIPHER_get_nid(cipher);
  198. else
  199. cipher_nid = -1;
  200. if (md)
  201. md_nid = EVP_MD_get_type(md);
  202. else
  203. md_nid = -1;
  204. return EVP_PBE_alg_add_type(EVP_PBE_TYPE_OUTER, nid,
  205. cipher_nid, md_nid, keygen);
  206. }
  207. int EVP_PBE_find_ex(int type, int pbe_nid, int *pcnid, int *pmnid,
  208. EVP_PBE_KEYGEN **pkeygen, EVP_PBE_KEYGEN_EX **pkeygen_ex)
  209. {
  210. EVP_PBE_CTL *pbetmp = NULL, pbelu;
  211. int i;
  212. if (pbe_nid == NID_undef)
  213. return 0;
  214. pbelu.pbe_type = type;
  215. pbelu.pbe_nid = pbe_nid;
  216. if (pbe_algs != NULL) {
  217. i = sk_EVP_PBE_CTL_find(pbe_algs, &pbelu);
  218. pbetmp = sk_EVP_PBE_CTL_value(pbe_algs, i);
  219. }
  220. if (pbetmp == NULL) {
  221. pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe, OSSL_NELEM(builtin_pbe));
  222. }
  223. if (pbetmp == NULL)
  224. return 0;
  225. if (pcnid != NULL)
  226. *pcnid = pbetmp->cipher_nid;
  227. if (pmnid != NULL)
  228. *pmnid = pbetmp->md_nid;
  229. if (pkeygen != NULL)
  230. *pkeygen = pbetmp->keygen;
  231. if (pkeygen_ex != NULL)
  232. *pkeygen_ex = pbetmp->keygen_ex;
  233. return 1;
  234. }
  235. int EVP_PBE_find(int type, int pbe_nid,
  236. int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen)
  237. {
  238. return EVP_PBE_find_ex(type, pbe_nid, pcnid, pmnid, pkeygen, NULL);
  239. }
  240. static void free_evp_pbe_ctl(EVP_PBE_CTL *pbe)
  241. {
  242. OPENSSL_free(pbe);
  243. }
  244. void EVP_PBE_cleanup(void)
  245. {
  246. sk_EVP_PBE_CTL_pop_free(pbe_algs, free_evp_pbe_ctl);
  247. pbe_algs = NULL;
  248. }
  249. int EVP_PBE_get(int *ptype, int *ppbe_nid, size_t num)
  250. {
  251. const EVP_PBE_CTL *tpbe;
  252. if (num >= OSSL_NELEM(builtin_pbe))
  253. return 0;
  254. tpbe = builtin_pbe + num;
  255. if (ptype)
  256. *ptype = tpbe->pbe_type;
  257. if (ppbe_nid)
  258. *ppbe_nid = tpbe->pbe_nid;
  259. return 1;
  260. }