2
0

evp_pbe.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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/pkcs12.h>
  13. #include <openssl/x509.h>
  14. #include "evp_locl.h"
  15. /* Password based encryption (PBE) functions */
  16. /* Setup a cipher context from a PBE algorithm */
  17. struct evp_pbe_st {
  18. int pbe_type;
  19. int pbe_nid;
  20. int cipher_nid;
  21. int md_nid;
  22. EVP_PBE_KEYGEN *keygen;
  23. };
  24. static STACK_OF(EVP_PBE_CTL) *pbe_algs;
  25. static const EVP_PBE_CTL builtin_pbe[] = {
  26. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndDES_CBC,
  27. NID_des_cbc, NID_md2, PKCS5_PBE_keyivgen},
  28. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC,
  29. NID_des_cbc, NID_md5, PKCS5_PBE_keyivgen},
  30. {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndRC2_CBC,
  31. NID_rc2_64_cbc, NID_sha1, PKCS5_PBE_keyivgen},
  32. {EVP_PBE_TYPE_OUTER, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen},
  33. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC4,
  34. NID_rc4, NID_sha1, PKCS12_PBE_keyivgen},
  35. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC4,
  36. NID_rc4_40, NID_sha1, PKCS12_PBE_keyivgen},
  37. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  38. NID_des_ede3_cbc, NID_sha1, PKCS12_PBE_keyivgen},
  39. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And2_Key_TripleDES_CBC,
  40. NID_des_ede_cbc, NID_sha1, PKCS12_PBE_keyivgen},
  41. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC2_CBC,
  42. NID_rc2_cbc, NID_sha1, PKCS12_PBE_keyivgen},
  43. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC2_CBC,
  44. NID_rc2_40_cbc, NID_sha1, PKCS12_PBE_keyivgen},
  45. {EVP_PBE_TYPE_OUTER, NID_pbes2, -1, -1, PKCS5_v2_PBE_keyivgen},
  46. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndRC2_CBC,
  47. NID_rc2_64_cbc, NID_md2, PKCS5_PBE_keyivgen},
  48. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndRC2_CBC,
  49. NID_rc2_64_cbc, NID_md5, PKCS5_PBE_keyivgen},
  50. {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndDES_CBC,
  51. NID_des_cbc, NID_sha1, PKCS5_PBE_keyivgen},
  52. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA1, -1, NID_sha1, 0},
  53. {EVP_PBE_TYPE_PRF, NID_hmac_md5, -1, NID_md5, 0},
  54. {EVP_PBE_TYPE_PRF, NID_hmac_sha1, -1, NID_sha1, 0},
  55. {EVP_PBE_TYPE_PRF, NID_hmacWithMD5, -1, NID_md5, 0},
  56. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA224, -1, NID_sha224, 0},
  57. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA256, -1, NID_sha256, 0},
  58. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA384, -1, NID_sha384, 0},
  59. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA512, -1, NID_sha512, 0},
  60. {EVP_PBE_TYPE_PRF, NID_id_HMACGostR3411_94, -1, NID_id_GostR3411_94, 0},
  61. {EVP_PBE_TYPE_PRF, NID_id_tc26_hmac_gost_3411_2012_256, -1,
  62. NID_id_GostR3411_2012_256, 0},
  63. {EVP_PBE_TYPE_PRF, NID_id_tc26_hmac_gost_3411_2012_512, -1,
  64. NID_id_GostR3411_2012_512, 0},
  65. {EVP_PBE_TYPE_KDF, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen},
  66. #ifndef OPENSSL_NO_SCRYPT
  67. {EVP_PBE_TYPE_KDF, NID_id_scrypt, -1, -1, PKCS5_v2_scrypt_keyivgen}
  68. #endif
  69. };
  70. int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
  71. ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
  72. {
  73. const EVP_CIPHER *cipher;
  74. const EVP_MD *md;
  75. int cipher_nid, md_nid;
  76. EVP_PBE_KEYGEN *keygen;
  77. if (!EVP_PBE_find(EVP_PBE_TYPE_OUTER, OBJ_obj2nid(pbe_obj),
  78. &cipher_nid, &md_nid, &keygen)) {
  79. char obj_tmp[80];
  80. EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_PBE_ALGORITHM);
  81. if (!pbe_obj)
  82. OPENSSL_strlcpy(obj_tmp, "NULL", sizeof(obj_tmp));
  83. else
  84. i2t_ASN1_OBJECT(obj_tmp, sizeof(obj_tmp), pbe_obj);
  85. ERR_add_error_data(2, "TYPE=", obj_tmp);
  86. return 0;
  87. }
  88. if (!pass)
  89. passlen = 0;
  90. else if (passlen == -1)
  91. passlen = strlen(pass);
  92. if (cipher_nid == -1)
  93. cipher = NULL;
  94. else {
  95. cipher = EVP_get_cipherbynid(cipher_nid);
  96. if (!cipher) {
  97. EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_CIPHER);
  98. return 0;
  99. }
  100. }
  101. if (md_nid == -1)
  102. md = NULL;
  103. else {
  104. md = EVP_get_digestbynid(md_nid);
  105. if (!md) {
  106. EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_DIGEST);
  107. return 0;
  108. }
  109. }
  110. if (!keygen(ctx, pass, passlen, param, cipher, md, en_de)) {
  111. EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_KEYGEN_FAILURE);
  112. return 0;
  113. }
  114. return 1;
  115. }
  116. DECLARE_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
  117. static int pbe2_cmp(const EVP_PBE_CTL *pbe1, const EVP_PBE_CTL *pbe2)
  118. {
  119. int ret = pbe1->pbe_type - pbe2->pbe_type;
  120. if (ret)
  121. return ret;
  122. else
  123. return pbe1->pbe_nid - pbe2->pbe_nid;
  124. }
  125. IMPLEMENT_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
  126. static int pbe_cmp(const EVP_PBE_CTL *const *a, const EVP_PBE_CTL *const *b)
  127. {
  128. int ret = (*a)->pbe_type - (*b)->pbe_type;
  129. if (ret)
  130. return ret;
  131. else
  132. return (*a)->pbe_nid - (*b)->pbe_nid;
  133. }
  134. /* Add a PBE algorithm */
  135. int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid,
  136. int md_nid, EVP_PBE_KEYGEN *keygen)
  137. {
  138. EVP_PBE_CTL *pbe_tmp;
  139. if (pbe_algs == NULL) {
  140. pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp);
  141. if (pbe_algs == NULL)
  142. goto err;
  143. }
  144. if ((pbe_tmp = OPENSSL_malloc(sizeof(*pbe_tmp))) == NULL)
  145. goto err;
  146. pbe_tmp->pbe_type = pbe_type;
  147. pbe_tmp->pbe_nid = pbe_nid;
  148. pbe_tmp->cipher_nid = cipher_nid;
  149. pbe_tmp->md_nid = md_nid;
  150. pbe_tmp->keygen = keygen;
  151. if (!sk_EVP_PBE_CTL_push(pbe_algs, pbe_tmp)) {
  152. OPENSSL_free(pbe_tmp);
  153. goto err;
  154. }
  155. return 1;
  156. err:
  157. EVPerr(EVP_F_EVP_PBE_ALG_ADD_TYPE, ERR_R_MALLOC_FAILURE);
  158. return 0;
  159. }
  160. int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
  161. EVP_PBE_KEYGEN *keygen)
  162. {
  163. int cipher_nid, md_nid;
  164. if (cipher)
  165. cipher_nid = EVP_CIPHER_nid(cipher);
  166. else
  167. cipher_nid = -1;
  168. if (md)
  169. md_nid = EVP_MD_type(md);
  170. else
  171. md_nid = -1;
  172. return EVP_PBE_alg_add_type(EVP_PBE_TYPE_OUTER, nid,
  173. cipher_nid, md_nid, keygen);
  174. }
  175. int EVP_PBE_find(int type, int pbe_nid,
  176. int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen)
  177. {
  178. EVP_PBE_CTL *pbetmp = NULL, pbelu;
  179. int i;
  180. if (pbe_nid == NID_undef)
  181. return 0;
  182. pbelu.pbe_type = type;
  183. pbelu.pbe_nid = pbe_nid;
  184. if (pbe_algs) {
  185. i = sk_EVP_PBE_CTL_find(pbe_algs, &pbelu);
  186. if (i != -1)
  187. pbetmp = sk_EVP_PBE_CTL_value(pbe_algs, i);
  188. }
  189. if (pbetmp == NULL) {
  190. pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe, OSSL_NELEM(builtin_pbe));
  191. }
  192. if (pbetmp == NULL)
  193. return 0;
  194. if (pcnid)
  195. *pcnid = pbetmp->cipher_nid;
  196. if (pmnid)
  197. *pmnid = pbetmp->md_nid;
  198. if (pkeygen)
  199. *pkeygen = pbetmp->keygen;
  200. return 1;
  201. }
  202. static void free_evp_pbe_ctl(EVP_PBE_CTL *pbe)
  203. {
  204. OPENSSL_free(pbe);
  205. }
  206. void EVP_PBE_cleanup(void)
  207. {
  208. sk_EVP_PBE_CTL_pop_free(pbe_algs, free_evp_pbe_ctl);
  209. pbe_algs = NULL;
  210. }
  211. int EVP_PBE_get(int *ptype, int *ppbe_nid, size_t num)
  212. {
  213. const EVP_PBE_CTL *tpbe;
  214. if (num >= OSSL_NELEM(builtin_pbe))
  215. return 0;
  216. tpbe = builtin_pbe + num;
  217. if (ptype)
  218. *ptype = tpbe->pbe_type;
  219. if (ppbe_nid)
  220. *ppbe_nid = tpbe->pbe_nid;
  221. return 1;
  222. }