p5_crpt2.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <stdlib.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/x509.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/kdf.h>
  15. #include <openssl/hmac.h>
  16. #include <openssl/trace.h>
  17. #include <openssl/core_names.h>
  18. #include "crypto/evp.h"
  19. #include "evp_local.h"
  20. int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
  21. const unsigned char *salt, int saltlen, int iter,
  22. const EVP_MD *digest, int keylen, unsigned char *out,
  23. OSSL_LIB_CTX *libctx, const char *propq)
  24. {
  25. const char *empty = "";
  26. int rv = 1, mode = 1;
  27. EVP_KDF *kdf;
  28. EVP_KDF_CTX *kctx;
  29. const char *mdname = EVP_MD_get0_name(digest);
  30. OSSL_PARAM params[6], *p = params;
  31. /* Keep documented behaviour. */
  32. if (pass == NULL) {
  33. pass = empty;
  34. passlen = 0;
  35. } else if (passlen == -1) {
  36. passlen = strlen(pass);
  37. }
  38. if (salt == NULL && saltlen == 0)
  39. salt = (unsigned char *)empty;
  40. kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, propq);
  41. kctx = EVP_KDF_CTX_new(kdf);
  42. EVP_KDF_free(kdf);
  43. if (kctx == NULL)
  44. return 0;
  45. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
  46. (char *)pass, (size_t)passlen);
  47. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
  48. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  49. (unsigned char *)salt, saltlen);
  50. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
  51. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  52. (char *)mdname, 0);
  53. *p = OSSL_PARAM_construct_end();
  54. if (EVP_KDF_derive(kctx, out, keylen, params) != 1)
  55. rv = 0;
  56. EVP_KDF_CTX_free(kctx);
  57. OSSL_TRACE_BEGIN(PKCS5V2) {
  58. BIO_printf(trc_out, "Password:\n");
  59. BIO_hex_string(trc_out,
  60. 0, passlen, pass, passlen);
  61. BIO_printf(trc_out, "\n");
  62. BIO_printf(trc_out, "Salt:\n");
  63. BIO_hex_string(trc_out,
  64. 0, saltlen, salt, saltlen);
  65. BIO_printf(trc_out, "\n");
  66. BIO_printf(trc_out, "Iteration count %d\n", iter);
  67. BIO_printf(trc_out, "Key:\n");
  68. BIO_hex_string(trc_out,
  69. 0, keylen, out, keylen);
  70. BIO_printf(trc_out, "\n");
  71. } OSSL_TRACE_END(PKCS5V2);
  72. return rv;
  73. }
  74. int PKCS5_PBKDF2_HMAC(const char *pass, int passlen, const unsigned char *salt,
  75. int saltlen, int iter, const EVP_MD *digest, int keylen,
  76. unsigned char *out)
  77. {
  78. return ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, digest,
  79. keylen, out, NULL, NULL);
  80. }
  81. int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
  82. const unsigned char *salt, int saltlen, int iter,
  83. int keylen, unsigned char *out)
  84. {
  85. EVP_MD *digest;
  86. int r = 0;
  87. if ((digest = EVP_MD_fetch(NULL, SN_sha1, NULL)) != NULL)
  88. r = ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter,
  89. digest, keylen, out, NULL, NULL);
  90. EVP_MD_free(digest);
  91. return r;
  92. }
  93. /*
  94. * Now the key derivation function itself. This is a bit evil because it has
  95. * to check the ASN1 parameters are valid: and there are quite a few of
  96. * them...
  97. */
  98. int PKCS5_v2_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
  99. ASN1_TYPE *param, const EVP_CIPHER *c,
  100. const EVP_MD *md, int en_de,
  101. OSSL_LIB_CTX *libctx, const char *propq)
  102. {
  103. PBE2PARAM *pbe2 = NULL;
  104. char ciph_name[80];
  105. const EVP_CIPHER *cipher = NULL;
  106. EVP_CIPHER *cipher_fetch = NULL;
  107. EVP_PBE_KEYGEN_EX *kdf;
  108. int rv = 0;
  109. pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param);
  110. if (pbe2 == NULL) {
  111. ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
  112. goto err;
  113. }
  114. /* See if we recognise the key derivation function */
  115. if (!EVP_PBE_find_ex(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm),
  116. NULL, NULL, NULL, &kdf)) {
  117. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
  118. goto err;
  119. }
  120. /*
  121. * lets see if we recognise the encryption algorithm.
  122. */
  123. if (OBJ_obj2txt(ciph_name, sizeof(ciph_name), pbe2->encryption->algorithm, 0) <= 0) {
  124. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
  125. goto err;
  126. }
  127. (void)ERR_set_mark();
  128. cipher = cipher_fetch = EVP_CIPHER_fetch(libctx, ciph_name, propq);
  129. /* Fallback to legacy method */
  130. if (cipher == NULL)
  131. cipher = EVP_get_cipherbyname(ciph_name);
  132. if (cipher == NULL) {
  133. (void)ERR_clear_last_mark();
  134. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
  135. goto err;
  136. }
  137. (void)ERR_pop_to_mark();
  138. /* Fixup cipher based on AlgorithmIdentifier */
  139. if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
  140. goto err;
  141. if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) < 0) {
  142. ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
  143. goto err;
  144. }
  145. rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de, libctx, propq);
  146. err:
  147. EVP_CIPHER_free(cipher_fetch);
  148. PBE2PARAM_free(pbe2);
  149. return rv;
  150. }
  151. int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
  152. ASN1_TYPE *param, const EVP_CIPHER *c,
  153. const EVP_MD *md, int en_de)
  154. {
  155. return PKCS5_v2_PBE_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de, NULL, NULL);
  156. }
  157. int PKCS5_v2_PBKDF2_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass,
  158. int passlen, ASN1_TYPE *param,
  159. const EVP_CIPHER *c, const EVP_MD *md, int en_de,
  160. OSSL_LIB_CTX *libctx, const char *propq)
  161. {
  162. unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
  163. int saltlen, iter, t;
  164. int rv = 0;
  165. unsigned int keylen = 0;
  166. int prf_nid, hmac_md_nid;
  167. PBKDF2PARAM *kdf = NULL;
  168. const EVP_MD *prfmd = NULL;
  169. EVP_MD *prfmd_fetch = NULL;
  170. if (EVP_CIPHER_CTX_get0_cipher(ctx) == NULL) {
  171. ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
  172. goto err;
  173. }
  174. keylen = EVP_CIPHER_CTX_get_key_length(ctx);
  175. OPENSSL_assert(keylen <= sizeof(key));
  176. /* Decode parameter */
  177. kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
  178. if (kdf == NULL) {
  179. ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
  180. goto err;
  181. }
  182. t = EVP_CIPHER_CTX_get_key_length(ctx);
  183. if (t < 0) {
  184. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
  185. goto err;
  186. }
  187. keylen = t;
  188. /* Now check the parameters of the kdf */
  189. if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
  190. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEYLENGTH);
  191. goto err;
  192. }
  193. if (kdf->prf)
  194. prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
  195. else
  196. prf_nid = NID_hmacWithSHA1;
  197. if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
  198. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
  199. goto err;
  200. }
  201. prfmd = prfmd_fetch = EVP_MD_fetch(libctx, OBJ_nid2sn(hmac_md_nid), propq);
  202. if (prfmd == NULL)
  203. prfmd = EVP_get_digestbynid(hmac_md_nid);
  204. if (prfmd == NULL) {
  205. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
  206. goto err;
  207. }
  208. if (kdf->salt->type != V_ASN1_OCTET_STRING) {
  209. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_SALT_TYPE);
  210. goto err;
  211. }
  212. /* it seems that its all OK */
  213. salt = kdf->salt->value.octet_string->data;
  214. saltlen = kdf->salt->value.octet_string->length;
  215. iter = ASN1_INTEGER_get(kdf->iter);
  216. if (!ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, prfmd,
  217. keylen, key, libctx, propq))
  218. goto err;
  219. rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
  220. err:
  221. OPENSSL_cleanse(key, keylen);
  222. PBKDF2PARAM_free(kdf);
  223. EVP_MD_free(prfmd_fetch);
  224. return rv;
  225. }
  226. int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
  227. int passlen, ASN1_TYPE *param,
  228. const EVP_CIPHER *c, const EVP_MD *md, int en_de)
  229. {
  230. return PKCS5_v2_PBKDF2_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de,
  231. NULL, NULL);
  232. }