p5_crpt.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/core_names.h>
  15. #include <openssl/kdf.h>
  16. /*
  17. * Doesn't do anything now: Builtin PBE algorithms in static table.
  18. */
  19. void PKCS5_PBE_add(void)
  20. {
  21. }
  22. int PKCS5_PBE_keyivgen_ex(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
  23. ASN1_TYPE *param, const EVP_CIPHER *cipher,
  24. const EVP_MD *md, int en_de, OSSL_LIB_CTX *libctx,
  25. const char *propq)
  26. {
  27. unsigned char md_tmp[EVP_MAX_MD_SIZE];
  28. unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
  29. int ivl, kl;
  30. PBEPARAM *pbe = NULL;
  31. int saltlen, iter;
  32. unsigned char *salt;
  33. int mdsize;
  34. int rv = 0;
  35. EVP_KDF *kdf;
  36. EVP_KDF_CTX *kctx = NULL;
  37. OSSL_PARAM params[5], *p = params;
  38. const char *mdname = EVP_MD_name(md);
  39. /* Extract useful info from parameter */
  40. if (param == NULL || param->type != V_ASN1_SEQUENCE ||
  41. param->value.sequence == NULL) {
  42. ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
  43. return 0;
  44. }
  45. pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
  46. if (pbe == NULL) {
  47. ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
  48. return 0;
  49. }
  50. ivl = EVP_CIPHER_get_iv_length(cipher);
  51. if (ivl < 0 || ivl > 16) {
  52. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
  53. goto err;
  54. }
  55. kl = EVP_CIPHER_get_key_length(cipher);
  56. if (kl < 0 || kl > (int)sizeof(md_tmp)) {
  57. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
  58. goto err;
  59. }
  60. if (pbe->iter == NULL)
  61. iter = 1;
  62. else
  63. iter = ASN1_INTEGER_get(pbe->iter);
  64. salt = pbe->salt->data;
  65. saltlen = pbe->salt->length;
  66. if (pass == NULL)
  67. passlen = 0;
  68. else if (passlen == -1)
  69. passlen = strlen(pass);
  70. mdsize = EVP_MD_get_size(md);
  71. if (mdsize < 0)
  72. goto err;
  73. kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF1, propq);
  74. kctx = EVP_KDF_CTX_new(kdf);
  75. EVP_KDF_free(kdf);
  76. if (kctx == NULL)
  77. goto err;
  78. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
  79. (char *)pass, (size_t)passlen);
  80. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  81. salt, saltlen);
  82. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
  83. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  84. (char *)mdname, 0);
  85. *p = OSSL_PARAM_construct_end();
  86. if (EVP_KDF_derive(kctx, md_tmp, mdsize, params) != 1)
  87. goto err;
  88. memcpy(key, md_tmp, kl);
  89. memcpy(iv, md_tmp + (16 - ivl), ivl);
  90. if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de))
  91. goto err;
  92. OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
  93. OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
  94. OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
  95. rv = 1;
  96. err:
  97. EVP_KDF_CTX_free(kctx);
  98. PBEPARAM_free(pbe);
  99. return rv;
  100. }
  101. int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
  102. ASN1_TYPE *param, const EVP_CIPHER *cipher,
  103. const EVP_MD *md, int en_de)
  104. {
  105. return PKCS5_PBE_keyivgen_ex(cctx, pass, passlen, param, cipher, md, en_de,
  106. NULL, NULL);
  107. }