evp_pbe.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 1999-2018 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_local.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_PRF, NID_hmacWithSHA512_224, -1, NID_sha512_224, 0},
  66. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA512_256, -1, NID_sha512_256, 0},
  67. {EVP_PBE_TYPE_KDF, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen},
  68. #ifndef OPENSSL_NO_SCRYPT
  69. {EVP_PBE_TYPE_KDF, NID_id_scrypt, -1, -1, PKCS5_v2_scrypt_keyivgen}
  70. #endif
  71. };
  72. int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
  73. ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
  74. {
  75. const EVP_CIPHER *cipher;
  76. const EVP_MD *md;
  77. int cipher_nid, md_nid;
  78. EVP_PBE_KEYGEN *keygen;
  79. if (!EVP_PBE_find(EVP_PBE_TYPE_OUTER, OBJ_obj2nid(pbe_obj),
  80. &cipher_nid, &md_nid, &keygen)) {
  81. char obj_tmp[80];
  82. EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_PBE_ALGORITHM);
  83. if (!pbe_obj)
  84. OPENSSL_strlcpy(obj_tmp, "NULL", sizeof(obj_tmp));
  85. else
  86. i2t_ASN1_OBJECT(obj_tmp, sizeof(obj_tmp), pbe_obj);
  87. ERR_add_error_data(2, "TYPE=", obj_tmp);
  88. return 0;
  89. }
  90. if (!pass)
  91. passlen = 0;
  92. else if (passlen == -1)
  93. passlen = strlen(pass);
  94. if (cipher_nid == -1)
  95. cipher = NULL;
  96. else {
  97. cipher = EVP_get_cipherbynid(cipher_nid);
  98. if (!cipher) {
  99. EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_CIPHER);
  100. return 0;
  101. }
  102. }
  103. if (md_nid == -1)
  104. md = NULL;
  105. else {
  106. md = EVP_get_digestbynid(md_nid);
  107. if (!md) {
  108. EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_DIGEST);
  109. return 0;
  110. }
  111. }
  112. if (!keygen(ctx, pass, passlen, param, cipher, md, en_de)) {
  113. EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_KEYGEN_FAILURE);
  114. return 0;
  115. }
  116. return 1;
  117. }
  118. DECLARE_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
  119. static int pbe2_cmp(const EVP_PBE_CTL *pbe1, const EVP_PBE_CTL *pbe2)
  120. {
  121. int ret = pbe1->pbe_type - pbe2->pbe_type;
  122. if (ret)
  123. return ret;
  124. else
  125. return pbe1->pbe_nid - pbe2->pbe_nid;
  126. }
  127. IMPLEMENT_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
  128. static int pbe_cmp(const EVP_PBE_CTL *const *a, const EVP_PBE_CTL *const *b)
  129. {
  130. int ret = (*a)->pbe_type - (*b)->pbe_type;
  131. if (ret)
  132. return ret;
  133. else
  134. return (*a)->pbe_nid - (*b)->pbe_nid;
  135. }
  136. /* Add a PBE algorithm */
  137. int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid,
  138. int md_nid, EVP_PBE_KEYGEN *keygen)
  139. {
  140. EVP_PBE_CTL *pbe_tmp;
  141. if (pbe_algs == NULL) {
  142. pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp);
  143. if (pbe_algs == NULL)
  144. goto err;
  145. }
  146. if ((pbe_tmp = OPENSSL_malloc(sizeof(*pbe_tmp))) == NULL)
  147. goto err;
  148. pbe_tmp->pbe_type = pbe_type;
  149. pbe_tmp->pbe_nid = pbe_nid;
  150. pbe_tmp->cipher_nid = cipher_nid;
  151. pbe_tmp->md_nid = md_nid;
  152. pbe_tmp->keygen = keygen;
  153. if (!sk_EVP_PBE_CTL_push(pbe_algs, pbe_tmp)) {
  154. OPENSSL_free(pbe_tmp);
  155. goto err;
  156. }
  157. return 1;
  158. err:
  159. EVPerr(EVP_F_EVP_PBE_ALG_ADD_TYPE, ERR_R_MALLOC_FAILURE);
  160. return 0;
  161. }
  162. int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
  163. EVP_PBE_KEYGEN *keygen)
  164. {
  165. int cipher_nid, md_nid;
  166. if (cipher)
  167. cipher_nid = EVP_CIPHER_nid(cipher);
  168. else
  169. cipher_nid = -1;
  170. if (md)
  171. md_nid = EVP_MD_type(md);
  172. else
  173. md_nid = -1;
  174. return EVP_PBE_alg_add_type(EVP_PBE_TYPE_OUTER, nid,
  175. cipher_nid, md_nid, keygen);
  176. }
  177. int EVP_PBE_find(int type, int pbe_nid,
  178. int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen)
  179. {
  180. EVP_PBE_CTL *pbetmp = NULL, pbelu;
  181. int i;
  182. if (pbe_nid == NID_undef)
  183. return 0;
  184. pbelu.pbe_type = type;
  185. pbelu.pbe_nid = pbe_nid;
  186. if (pbe_algs != NULL) {
  187. i = sk_EVP_PBE_CTL_find(pbe_algs, &pbelu);
  188. pbetmp = sk_EVP_PBE_CTL_value(pbe_algs, i);
  189. }
  190. if (pbetmp == NULL) {
  191. pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe, OSSL_NELEM(builtin_pbe));
  192. }
  193. if (pbetmp == NULL)
  194. return 0;
  195. if (pcnid)
  196. *pcnid = pbetmp->cipher_nid;
  197. if (pmnid)
  198. *pmnid = pbetmp->md_nid;
  199. if (pkeygen)
  200. *pkeygen = pbetmp->keygen;
  201. return 1;
  202. }
  203. static void free_evp_pbe_ctl(EVP_PBE_CTL *pbe)
  204. {
  205. OPENSSL_free(pbe);
  206. }
  207. void EVP_PBE_cleanup(void)
  208. {
  209. sk_EVP_PBE_CTL_pop_free(pbe_algs, free_evp_pbe_ctl);
  210. pbe_algs = NULL;
  211. }
  212. int EVP_PBE_get(int *ptype, int *ppbe_nid, size_t num)
  213. {
  214. const EVP_PBE_CTL *tpbe;
  215. if (num >= OSSL_NELEM(builtin_pbe))
  216. return 0;
  217. tpbe = builtin_pbe + num;
  218. if (ptype)
  219. *ptype = tpbe->pbe_type;
  220. if (ppbe_nid)
  221. *ppbe_nid = tpbe->pbe_nid;
  222. return 1;
  223. }