evp_pbe.c 7.6 KB

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