p5_pbev2.c 6.8 KB

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