2
0

p12_crpt.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "internal/cryptlib.h"
  11. #include <openssl/core.h>
  12. #include <openssl/core_names.h>
  13. #include "crypto/evp.h"
  14. #include <openssl/pkcs12.h>
  15. /* PKCS#12 PBE algorithms now in static table */
  16. void PKCS12_PBE_add(void)
  17. {
  18. }
  19. int PKCS12_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
  20. ASN1_TYPE *param, const EVP_CIPHER *cipher,
  21. const EVP_MD *md, int en_de,
  22. OSSL_LIB_CTX *libctx, const char *propq)
  23. {
  24. PBEPARAM *pbe;
  25. int saltlen, iter, ret;
  26. unsigned char *salt;
  27. unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
  28. unsigned char *piv = iv;
  29. if (cipher == NULL)
  30. return 0;
  31. /* Extract useful info from parameter */
  32. pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
  33. if (pbe == NULL) {
  34. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
  35. return 0;
  36. }
  37. if (pbe->iter == NULL)
  38. iter = 1;
  39. else
  40. iter = ASN1_INTEGER_get(pbe->iter);
  41. salt = pbe->salt->data;
  42. saltlen = pbe->salt->length;
  43. if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_KEY_ID,
  44. iter, EVP_CIPHER_get_key_length(cipher),
  45. key, md,
  46. libctx, propq)) {
  47. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
  48. PBEPARAM_free(pbe);
  49. return 0;
  50. }
  51. if (EVP_CIPHER_get_iv_length(cipher) > 0) {
  52. if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_IV_ID,
  53. iter, EVP_CIPHER_get_iv_length(cipher),
  54. iv, md,
  55. libctx, propq)) {
  56. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_IV_GEN_ERROR);
  57. PBEPARAM_free(pbe);
  58. return 0;
  59. }
  60. } else {
  61. piv = NULL;
  62. }
  63. PBEPARAM_free(pbe);
  64. ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, piv, en_de);
  65. OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
  66. OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
  67. return ret;
  68. }
  69. int PKCS12_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
  70. ASN1_TYPE *param, const EVP_CIPHER *cipher,
  71. const EVP_MD *md, int en_de)
  72. {
  73. return PKCS12_PBE_keyivgen_ex(ctx, pass, passlen, param, cipher, md, en_de,
  74. NULL, NULL);
  75. }