p5_crpt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 1999-2016 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. /*
  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. return 0;
  48. }
  49. kl = EVP_CIPHER_key_length(cipher);
  50. if (kl < 0 || kl > (int)sizeof(md_tmp)) {
  51. EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, EVP_R_INVALID_KEY_LENGTH);
  52. return 0;
  53. }
  54. if (pbe->iter == NULL)
  55. iter = 1;
  56. else
  57. iter = ASN1_INTEGER_get(pbe->iter);
  58. salt = pbe->salt->data;
  59. saltlen = pbe->salt->length;
  60. if (pass == NULL)
  61. passlen = 0;
  62. else if (passlen == -1)
  63. passlen = strlen(pass);
  64. ctx = EVP_MD_CTX_new();
  65. if (ctx == NULL) {
  66. EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, ERR_R_MALLOC_FAILURE);
  67. goto err;
  68. }
  69. if (!EVP_DigestInit_ex(ctx, md, NULL))
  70. goto err;
  71. if (!EVP_DigestUpdate(ctx, pass, passlen))
  72. goto err;
  73. if (!EVP_DigestUpdate(ctx, salt, saltlen))
  74. goto err;
  75. PBEPARAM_free(pbe);
  76. if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
  77. goto err;
  78. mdsize = EVP_MD_size(md);
  79. if (mdsize < 0)
  80. return 0;
  81. for (i = 1; i < iter; i++) {
  82. if (!EVP_DigestInit_ex(ctx, md, NULL))
  83. goto err;
  84. if (!EVP_DigestUpdate(ctx, md_tmp, mdsize))
  85. goto err;
  86. if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
  87. goto err;
  88. }
  89. memcpy(key, md_tmp, kl);
  90. memcpy(iv, md_tmp + (16 - ivl), ivl);
  91. if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de))
  92. goto err;
  93. OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
  94. OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
  95. OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
  96. rv = 1;
  97. err:
  98. EVP_MD_CTX_free(ctx);
  99. return rv;
  100. }