evp_pbe.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* evp_pbe.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project 1999.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/evp.h>
  61. #include <openssl/pkcs12.h>
  62. #include <openssl/x509.h>
  63. #include "evp_locl.h"
  64. /* Password based encryption (PBE) functions */
  65. DECLARE_STACK_OF(EVP_PBE_CTL)
  66. static STACK_OF(EVP_PBE_CTL) *pbe_algs;
  67. /* Setup a cipher context from a PBE algorithm */
  68. typedef struct
  69. {
  70. int pbe_type;
  71. int pbe_nid;
  72. int cipher_nid;
  73. int md_nid;
  74. EVP_PBE_KEYGEN *keygen;
  75. } EVP_PBE_CTL;
  76. static const EVP_PBE_CTL builtin_pbe[] =
  77. {
  78. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndDES_CBC,
  79. NID_des_cbc, NID_md2, PKCS5_PBE_keyivgen},
  80. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC,
  81. NID_des_cbc, NID_md5, PKCS5_PBE_keyivgen},
  82. {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndRC2_CBC,
  83. NID_rc2_64_cbc, NID_sha1, PKCS5_PBE_keyivgen},
  84. #ifndef OPENSSL_NO_HMAC
  85. {EVP_PBE_TYPE_OUTER, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen},
  86. #endif
  87. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC4,
  88. NID_rc4, NID_sha1, PKCS12_PBE_keyivgen},
  89. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC4,
  90. NID_rc4_40, NID_sha1, PKCS12_PBE_keyivgen},
  91. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  92. NID_des_ede3_cbc, NID_sha1, PKCS12_PBE_keyivgen},
  93. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And2_Key_TripleDES_CBC,
  94. NID_des_ede_cbc, NID_sha1, PKCS12_PBE_keyivgen},
  95. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC2_CBC,
  96. NID_rc2_cbc, NID_sha1, PKCS12_PBE_keyivgen},
  97. {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC2_CBC,
  98. NID_rc2_40_cbc, NID_sha1, PKCS12_PBE_keyivgen},
  99. #ifndef OPENSSL_NO_HMAC
  100. {EVP_PBE_TYPE_OUTER, NID_pbes2, -1, -1, PKCS5_v2_PBE_keyivgen},
  101. #endif
  102. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndRC2_CBC,
  103. NID_rc2_64_cbc, NID_md2, PKCS5_PBE_keyivgen},
  104. {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndRC2_CBC,
  105. NID_rc2_64_cbc, NID_md5, PKCS5_PBE_keyivgen},
  106. {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndDES_CBC,
  107. NID_des_cbc, NID_sha1, PKCS5_PBE_keyivgen},
  108. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA1, -1, NID_sha1, 0},
  109. {EVP_PBE_TYPE_PRF, NID_hmacWithMD5, -1, NID_md5, 0},
  110. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA224, -1, NID_sha224, 0},
  111. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA256, -1, NID_sha256, 0},
  112. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA384, -1, NID_sha384, 0},
  113. {EVP_PBE_TYPE_PRF, NID_hmacWithSHA512, -1, NID_sha512, 0},
  114. {EVP_PBE_TYPE_PRF, NID_id_HMACGostR3411_94, -1, NID_id_GostR3411_94, 0},
  115. };
  116. #ifdef TEST
  117. int main(int argc, char **argv)
  118. {
  119. int i, nid_md, nid_cipher;
  120. EVP_PBE_CTL *tpbe, *tpbe2;
  121. /*OpenSSL_add_all_algorithms();*/
  122. for (i = 0; i < sizeof(builtin_pbe)/sizeof(EVP_PBE_CTL); i++)
  123. {
  124. tpbe = builtin_pbe + i;
  125. fprintf(stderr, "%d %d %s ", tpbe->pbe_type, tpbe->pbe_nid,
  126. OBJ_nid2sn(tpbe->pbe_nid));
  127. if (EVP_PBE_find(tpbe->pbe_type, tpbe->pbe_nid,
  128. &nid_cipher ,&nid_md,0))
  129. fprintf(stderr, "Found %s %s\n",
  130. OBJ_nid2sn(nid_cipher),
  131. OBJ_nid2sn(nid_md));
  132. else
  133. fprintf(stderr, "Find ERROR!!\n");
  134. }
  135. return 0;
  136. }
  137. #endif
  138. int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
  139. ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
  140. {
  141. const EVP_CIPHER *cipher;
  142. const EVP_MD *md;
  143. int cipher_nid, md_nid;
  144. EVP_PBE_KEYGEN *keygen;
  145. if (!EVP_PBE_find(EVP_PBE_TYPE_OUTER, OBJ_obj2nid(pbe_obj),
  146. &cipher_nid, &md_nid, &keygen))
  147. {
  148. char obj_tmp[80];
  149. EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_UNKNOWN_PBE_ALGORITHM);
  150. if (!pbe_obj) BUF_strlcpy (obj_tmp, "NULL", sizeof obj_tmp);
  151. else i2t_ASN1_OBJECT(obj_tmp, sizeof obj_tmp, pbe_obj);
  152. ERR_add_error_data(2, "TYPE=", obj_tmp);
  153. return 0;
  154. }
  155. if(!pass)
  156. passlen = 0;
  157. else if (passlen == -1)
  158. passlen = strlen(pass);
  159. if (cipher_nid == -1)
  160. cipher = NULL;
  161. else
  162. {
  163. cipher = EVP_get_cipherbynid(cipher_nid);
  164. if (!cipher)
  165. {
  166. EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_UNKNOWN_CIPHER);
  167. return 0;
  168. }
  169. }
  170. if (md_nid == -1)
  171. md = NULL;
  172. else
  173. {
  174. md = EVP_get_digestbynid(md_nid);
  175. if (!md)
  176. {
  177. EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_UNKNOWN_DIGEST);
  178. return 0;
  179. }
  180. }
  181. if (!keygen(ctx, pass, passlen, param, cipher, md, en_de))
  182. {
  183. EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_KEYGEN_FAILURE);
  184. return 0;
  185. }
  186. return 1;
  187. }
  188. DECLARE_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
  189. static int pbe2_cmp(const EVP_PBE_CTL *pbe1, const EVP_PBE_CTL *pbe2)
  190. {
  191. int ret = pbe1->pbe_type - pbe2->pbe_type;
  192. if (ret)
  193. return ret;
  194. else
  195. return pbe1->pbe_nid - pbe2->pbe_nid;
  196. }
  197. IMPLEMENT_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
  198. static int pbe_cmp(const EVP_PBE_CTL * const *a, const EVP_PBE_CTL * const *b)
  199. {
  200. int ret = (*a)->pbe_type - (*b)->pbe_type;
  201. if (ret)
  202. return ret;
  203. else
  204. return (*a)->pbe_nid - (*b)->pbe_nid;
  205. }
  206. /* Add a PBE algorithm */
  207. int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid, int md_nid,
  208. EVP_PBE_KEYGEN *keygen)
  209. {
  210. EVP_PBE_CTL *pbe_tmp;
  211. if (!pbe_algs)
  212. pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp);
  213. if (!(pbe_tmp = (EVP_PBE_CTL*) OPENSSL_malloc (sizeof(EVP_PBE_CTL))))
  214. {
  215. EVPerr(EVP_F_EVP_PBE_ALG_ADD_TYPE,ERR_R_MALLOC_FAILURE);
  216. return 0;
  217. }
  218. pbe_tmp->pbe_type = pbe_type;
  219. pbe_tmp->pbe_nid = pbe_nid;
  220. pbe_tmp->cipher_nid = cipher_nid;
  221. pbe_tmp->md_nid = md_nid;
  222. pbe_tmp->keygen = keygen;
  223. sk_EVP_PBE_CTL_push (pbe_algs, pbe_tmp);
  224. return 1;
  225. }
  226. int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
  227. EVP_PBE_KEYGEN *keygen)
  228. {
  229. int cipher_nid, md_nid;
  230. if (cipher)
  231. cipher_nid = EVP_CIPHER_type(cipher);
  232. else
  233. cipher_nid = -1;
  234. if (md)
  235. md_nid = EVP_MD_type(md);
  236. else
  237. md_nid = -1;
  238. return EVP_PBE_alg_add_type(EVP_PBE_TYPE_OUTER, nid,
  239. cipher_nid, md_nid, keygen);
  240. }
  241. int EVP_PBE_find(int type, int pbe_nid,
  242. int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen)
  243. {
  244. EVP_PBE_CTL *pbetmp = NULL, pbelu;
  245. int i;
  246. if (pbe_nid == NID_undef)
  247. return 0;
  248. pbelu.pbe_type = type;
  249. pbelu.pbe_nid = pbe_nid;
  250. if (pbe_algs)
  251. {
  252. i = sk_EVP_PBE_CTL_find(pbe_algs, &pbelu);
  253. if (i != -1)
  254. pbetmp = sk_EVP_PBE_CTL_value (pbe_algs, i);
  255. }
  256. if (pbetmp == NULL)
  257. {
  258. pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe,
  259. sizeof(builtin_pbe)/sizeof(EVP_PBE_CTL));
  260. }
  261. if (pbetmp == NULL)
  262. return 0;
  263. if (pcnid)
  264. *pcnid = pbetmp->cipher_nid;
  265. if (pmnid)
  266. *pmnid = pbetmp->md_nid;
  267. if (pkeygen)
  268. *pkeygen = pbetmp->keygen;
  269. return 1;
  270. }
  271. static void free_evp_pbe_ctl(EVP_PBE_CTL *pbe)
  272. {
  273. OPENSSL_freeFunc(pbe);
  274. }
  275. void EVP_PBE_cleanup(void)
  276. {
  277. sk_EVP_PBE_CTL_pop_free(pbe_algs, free_evp_pbe_ctl);
  278. pbe_algs = NULL;
  279. }