p5_crpt2.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 1999-2020 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 pkcs5_pbkdf2_hmac_with_libctx(const char *pass, int passlen,
  21. const unsigned char *salt, int saltlen,
  22. int iter, const EVP_MD *digest, int keylen,
  23. unsigned char *out,
  24. OPENSSL_CTX *libctx, const char *propq)
  25. {
  26. const char *empty = "";
  27. int rv = 1, mode = 1;
  28. EVP_KDF *kdf;
  29. EVP_KDF_CTX *kctx;
  30. const char *mdname = EVP_MD_name(digest);
  31. OSSL_PARAM params[6], *p = params;
  32. /* Keep documented behaviour. */
  33. if (pass == NULL) {
  34. pass = empty;
  35. passlen = 0;
  36. } else if (passlen == -1) {
  37. passlen = strlen(pass);
  38. }
  39. if (salt == NULL && saltlen == 0)
  40. salt = (unsigned char *)empty;
  41. kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, propq);
  42. kctx = EVP_KDF_CTX_new(kdf);
  43. EVP_KDF_free(kdf);
  44. if (kctx == NULL)
  45. return 0;
  46. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
  47. (char *)pass, (size_t)passlen);
  48. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
  49. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  50. (unsigned char *)salt, saltlen);
  51. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
  52. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  53. (char *)mdname, 0);
  54. *p = OSSL_PARAM_construct_end();
  55. if (EVP_KDF_CTX_set_params(kctx, params) != 1
  56. || EVP_KDF_derive(kctx, out, keylen) != 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 pkcs5_pbkdf2_hmac_with_libctx(pass, passlen, salt, saltlen, iter,
  81. digest, 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. return PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, EVP_sha1(),
  88. keylen, out);
  89. }
  90. /*
  91. * Now the key derivation function itself. This is a bit evil because it has
  92. * to check the ASN1 parameters are valid: and there are quite a few of
  93. * them...
  94. */
  95. int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
  96. ASN1_TYPE *param, const EVP_CIPHER *c,
  97. const EVP_MD *md, int en_de)
  98. {
  99. PBE2PARAM *pbe2 = NULL;
  100. const EVP_CIPHER *cipher;
  101. EVP_PBE_KEYGEN *kdf;
  102. int rv = 0;
  103. pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param);
  104. if (pbe2 == NULL) {
  105. EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_DECODE_ERROR);
  106. goto err;
  107. }
  108. /* See if we recognise the key derivation function */
  109. if (!EVP_PBE_find(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm),
  110. NULL, NULL, &kdf)) {
  111. EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN,
  112. EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
  113. goto err;
  114. }
  115. /*
  116. * lets see if we recognise the encryption algorithm.
  117. */
  118. cipher = EVP_get_cipherbyobj(pbe2->encryption->algorithm);
  119. if (!cipher) {
  120. EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_UNSUPPORTED_CIPHER);
  121. goto err;
  122. }
  123. /* Fixup cipher based on AlgorithmIdentifier */
  124. if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
  125. goto err;
  126. if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) < 0) {
  127. EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_CIPHER_PARAMETER_ERROR);
  128. goto err;
  129. }
  130. rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de);
  131. err:
  132. PBE2PARAM_free(pbe2);
  133. return rv;
  134. }
  135. int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
  136. int passlen, ASN1_TYPE *param,
  137. const EVP_CIPHER *c, const EVP_MD *md, int en_de)
  138. {
  139. unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
  140. int saltlen, iter, t;
  141. int rv = 0;
  142. unsigned int keylen = 0;
  143. int prf_nid, hmac_md_nid;
  144. PBKDF2PARAM *kdf = NULL;
  145. const EVP_MD *prfmd;
  146. if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
  147. EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_NO_CIPHER_SET);
  148. goto err;
  149. }
  150. keylen = EVP_CIPHER_CTX_key_length(ctx);
  151. OPENSSL_assert(keylen <= sizeof(key));
  152. /* Decode parameter */
  153. kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
  154. if (kdf == NULL) {
  155. EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_DECODE_ERROR);
  156. goto err;
  157. }
  158. t = EVP_CIPHER_CTX_key_length(ctx);
  159. if (t < 0) {
  160. EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_INVALID_KEY_LENGTH);
  161. goto err;
  162. }
  163. keylen = t;
  164. /* Now check the parameters of the kdf */
  165. if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
  166. EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_KEYLENGTH);
  167. goto err;
  168. }
  169. if (kdf->prf)
  170. prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
  171. else
  172. prf_nid = NID_hmacWithSHA1;
  173. if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
  174. EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
  175. goto err;
  176. }
  177. prfmd = EVP_get_digestbynid(hmac_md_nid);
  178. if (prfmd == NULL) {
  179. EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
  180. goto err;
  181. }
  182. if (kdf->salt->type != V_ASN1_OCTET_STRING) {
  183. EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_SALT_TYPE);
  184. goto err;
  185. }
  186. /* it seems that its all OK */
  187. salt = kdf->salt->value.octet_string->data;
  188. saltlen = kdf->salt->value.octet_string->length;
  189. iter = ASN1_INTEGER_get(kdf->iter);
  190. if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, prfmd,
  191. keylen, key))
  192. goto err;
  193. rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
  194. err:
  195. OPENSSL_cleanse(key, keylen);
  196. PBKDF2PARAM_free(kdf);
  197. return rv;
  198. }