p5_pbev2.c 7.9 KB

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