p5_pbev2.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright 1999-2023 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 "crypto/asn1.h"
  12. #include "crypto/evp.h"
  13. #include <openssl/asn1t.h>
  14. #include <openssl/core.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/rand.h>
  18. /* PKCS#5 v2.0 password based encryption structures */
  19. ASN1_SEQUENCE(PBE2PARAM) = {
  20. ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
  21. ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
  22. } ASN1_SEQUENCE_END(PBE2PARAM)
  23. IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
  24. ASN1_SEQUENCE(PBKDF2PARAM) = {
  25. ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY),
  26. ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER),
  27. ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER),
  28. ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR)
  29. } ASN1_SEQUENCE_END(PBKDF2PARAM)
  30. IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM)
  31. /*
  32. * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: yes I know
  33. * this is horrible! Extended version to allow application supplied PRF NID
  34. * and IV.
  35. */
  36. X509_ALGOR *PKCS5_pbe2_set_iv_ex(const EVP_CIPHER *cipher, int iter,
  37. unsigned char *salt, int saltlen,
  38. unsigned char *aiv, int prf_nid,
  39. OSSL_LIB_CTX *libctx)
  40. {
  41. X509_ALGOR *scheme = NULL, *ret = NULL;
  42. int alg_nid, keylen, ivlen;
  43. EVP_CIPHER_CTX *ctx = NULL;
  44. unsigned char iv[EVP_MAX_IV_LENGTH];
  45. PBE2PARAM *pbe2 = NULL;
  46. alg_nid = EVP_CIPHER_get_type(cipher);
  47. if (alg_nid == NID_undef) {
  48. ERR_raise(ERR_LIB_ASN1, ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  49. goto err;
  50. }
  51. if ((pbe2 = PBE2PARAM_new()) == NULL) {
  52. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  53. goto err;
  54. }
  55. /* Setup the AlgorithmIdentifier for the encryption scheme */
  56. scheme = pbe2->encryption;
  57. scheme->algorithm = OBJ_nid2obj(alg_nid);
  58. if ((scheme->parameter = ASN1_TYPE_new()) == NULL) {
  59. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  60. goto err;
  61. }
  62. /* Create random IV */
  63. ivlen = EVP_CIPHER_get_iv_length(cipher);
  64. if (ivlen > 0) {
  65. if (aiv)
  66. memcpy(iv, aiv, ivlen);
  67. else if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
  68. goto err;
  69. }
  70. ctx = EVP_CIPHER_CTX_new();
  71. if (ctx == NULL) {
  72. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  73. goto err;
  74. }
  75. /* Dummy cipherinit to just setup the IV, and PRF */
  76. if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0))
  77. goto err;
  78. if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) <= 0) {
  79. ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
  80. goto err;
  81. }
  82. /*
  83. * If prf NID unspecified see if cipher has a preference. An error is OK
  84. * here: just means use default PRF.
  85. */
  86. ERR_set_mark();
  87. if ((prf_nid == -1) &&
  88. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) {
  89. prf_nid = NID_hmacWithSHA256;
  90. }
  91. ERR_pop_to_mark();
  92. EVP_CIPHER_CTX_free(ctx);
  93. ctx = NULL;
  94. /* If its RC2 then we'd better setup the key length */
  95. if (alg_nid == NID_rc2_cbc)
  96. keylen = EVP_CIPHER_get_key_length(cipher);
  97. else
  98. keylen = -1;
  99. /* Setup keyfunc */
  100. X509_ALGOR_free(pbe2->keyfunc);
  101. pbe2->keyfunc = PKCS5_pbkdf2_set_ex(iter, salt, saltlen, prf_nid, keylen,
  102. libctx);
  103. if (pbe2->keyfunc == NULL) {
  104. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  105. goto err;
  106. }
  107. /* Now set up top level AlgorithmIdentifier */
  108. if ((ret = X509_ALGOR_new()) == NULL) {
  109. ERR_raise(ERR_LIB_ASN1, ERR_R_X509_LIB);
  110. goto err;
  111. }
  112. ret->algorithm = OBJ_nid2obj(NID_pbes2);
  113. /* Encode PBE2PARAM into parameter */
  114. if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
  115. &ret->parameter)) {
  116. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  117. goto err;
  118. }
  119. PBE2PARAM_free(pbe2);
  120. pbe2 = NULL;
  121. return ret;
  122. err:
  123. EVP_CIPHER_CTX_free(ctx);
  124. PBE2PARAM_free(pbe2);
  125. /* Note 'scheme' is freed as part of pbe2 */
  126. X509_ALGOR_free(ret);
  127. return NULL;
  128. }
  129. X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
  130. unsigned char *salt, int saltlen,
  131. unsigned char *aiv, int prf_nid)
  132. {
  133. return PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, aiv, prf_nid,
  134. NULL);
  135. }
  136. X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
  137. unsigned char *salt, int saltlen)
  138. {
  139. return PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL, -1,
  140. NULL);
  141. }
  142. X509_ALGOR *PKCS5_pbkdf2_set_ex(int iter, unsigned char *salt, int saltlen,
  143. int prf_nid, int keylen,
  144. OSSL_LIB_CTX *libctx)
  145. {
  146. X509_ALGOR *keyfunc = NULL;
  147. PBKDF2PARAM *kdf = NULL;
  148. ASN1_OCTET_STRING *osalt = NULL;
  149. if ((kdf = PBKDF2PARAM_new()) == NULL) {
  150. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  151. goto err;
  152. }
  153. if ((osalt = ASN1_OCTET_STRING_new()) == NULL) {
  154. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  155. goto err;
  156. }
  157. kdf->salt->value.octet_string = osalt;
  158. kdf->salt->type = V_ASN1_OCTET_STRING;
  159. if (saltlen < 0) {
  160. ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT);
  161. goto err;
  162. }
  163. if (saltlen == 0)
  164. saltlen = PKCS5_DEFAULT_PBE2_SALT_LEN;
  165. if ((osalt->data = OPENSSL_malloc(saltlen)) == NULL)
  166. goto err;
  167. osalt->length = saltlen;
  168. if (salt) {
  169. memcpy(osalt->data, salt, saltlen);
  170. } else if (RAND_bytes_ex(libctx, osalt->data, saltlen, 0) <= 0) {
  171. ERR_raise(ERR_LIB_ASN1, ERR_R_RAND_LIB);
  172. goto err;
  173. }
  174. if (iter <= 0)
  175. iter = PKCS5_DEFAULT_ITER;
  176. if (!ASN1_INTEGER_set(kdf->iter, iter)) {
  177. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  178. goto err;
  179. }
  180. /* If have a key len set it up */
  181. if (keylen > 0) {
  182. if ((kdf->keylength = ASN1_INTEGER_new()) == NULL) {
  183. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  184. goto err;
  185. }
  186. if (!ASN1_INTEGER_set(kdf->keylength, keylen)) {
  187. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  188. goto err;
  189. }
  190. }
  191. /* prf can stay NULL if we are using hmacWithSHA1 */
  192. if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) {
  193. kdf->prf = ossl_X509_ALGOR_from_nid(prf_nid, V_ASN1_NULL, NULL);
  194. if (kdf->prf == NULL) {
  195. ERR_raise(ERR_LIB_ASN1, ERR_R_X509_LIB);
  196. goto err;
  197. }
  198. }
  199. /* Finally setup the keyfunc structure */
  200. keyfunc = X509_ALGOR_new();
  201. if (keyfunc == NULL) {
  202. ERR_raise(ERR_LIB_ASN1, ERR_R_X509_LIB);
  203. goto err;
  204. }
  205. keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
  206. /* Encode PBKDF2PARAM into parameter of pbe2 */
  207. if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), kdf,
  208. &keyfunc->parameter)) {
  209. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  210. goto err;
  211. }
  212. PBKDF2PARAM_free(kdf);
  213. return keyfunc;
  214. err:
  215. PBKDF2PARAM_free(kdf);
  216. X509_ALGOR_free(keyfunc);
  217. return NULL;
  218. }
  219. X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
  220. int prf_nid, int keylen)
  221. {
  222. return PKCS5_pbkdf2_set_ex(iter, salt, saltlen, prf_nid, keylen, NULL);
  223. }