p5_crpt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. /*
  15. * Doesn't do anything now: Builtin PBE algorithms in static table.
  16. */
  17. void PKCS5_PBE_add(void)
  18. {
  19. }
  20. int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
  21. ASN1_TYPE *param, const EVP_CIPHER *cipher,
  22. const EVP_MD *md, int en_de)
  23. {
  24. EVP_MD_CTX *ctx;
  25. unsigned char md_tmp[EVP_MAX_MD_SIZE];
  26. unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
  27. int i, ivl, kl;
  28. PBEPARAM *pbe;
  29. int saltlen, iter;
  30. unsigned char *salt;
  31. int mdsize;
  32. int rv = 0;
  33. /* Extract useful info from parameter */
  34. if (param == NULL || param->type != V_ASN1_SEQUENCE ||
  35. param->value.sequence == NULL) {
  36. EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, EVP_R_DECODE_ERROR);
  37. return 0;
  38. }
  39. pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
  40. if (pbe == NULL) {
  41. EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, EVP_R_DECODE_ERROR);
  42. return 0;
  43. }
  44. ivl = EVP_CIPHER_iv_length(cipher);
  45. if (ivl < 0 || ivl > 16) {
  46. EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, EVP_R_INVALID_IV_LENGTH);
  47. PBEPARAM_free(pbe);
  48. return 0;
  49. }
  50. kl = EVP_CIPHER_key_length(cipher);
  51. if (kl < 0 || kl > (int)sizeof(md_tmp)) {
  52. EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, EVP_R_INVALID_KEY_LENGTH);
  53. PBEPARAM_free(pbe);
  54. return 0;
  55. }
  56. if (!pbe->iter)
  57. iter = 1;
  58. else
  59. iter = ASN1_INTEGER_get(pbe->iter);
  60. salt = pbe->salt->data;
  61. saltlen = pbe->salt->length;
  62. if (!pass)
  63. passlen = 0;
  64. else if (passlen == -1)
  65. passlen = strlen(pass);
  66. ctx = EVP_MD_CTX_new();
  67. if (ctx == NULL) {
  68. EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, ERR_R_MALLOC_FAILURE);
  69. goto err;
  70. }
  71. if (!EVP_DigestInit_ex(ctx, md, NULL))
  72. goto err;
  73. if (!EVP_DigestUpdate(ctx, pass, passlen))
  74. goto err;
  75. if (!EVP_DigestUpdate(ctx, salt, saltlen))
  76. goto err;
  77. PBEPARAM_free(pbe);
  78. pbe = NULL;
  79. if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
  80. goto err;
  81. mdsize = EVP_MD_size(md);
  82. if (mdsize < 0)
  83. return 0;
  84. for (i = 1; i < iter; i++) {
  85. if (!EVP_DigestInit_ex(ctx, md, NULL))
  86. goto err;
  87. if (!EVP_DigestUpdate(ctx, md_tmp, mdsize))
  88. goto err;
  89. if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
  90. goto err;
  91. }
  92. memcpy(key, md_tmp, kl);
  93. memcpy(iv, md_tmp + (16 - ivl), ivl);
  94. if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de))
  95. goto err;
  96. OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
  97. OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
  98. OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
  99. rv = 1;
  100. err:
  101. PBEPARAM_free(pbe);
  102. EVP_MD_CTX_free(ctx);
  103. return rv;
  104. }