p5_crpt2.c 8.8 KB

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