p5_pbev2.c 6.0 KB

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