p5_pbev2.c 7.6 KB

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