p5_crpt2.c 6.9 KB

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