p12_crpt.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "internal/cryptlib.h"
  11. #include <openssl/pkcs12.h>
  12. /* PKCS#12 PBE algorithms now in static table */
  13. void PKCS12_PBE_add(void)
  14. {
  15. }
  16. int PKCS12_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
  17. ASN1_TYPE *param, const EVP_CIPHER *cipher,
  18. const EVP_MD *md, int en_de)
  19. {
  20. PBEPARAM *pbe;
  21. int saltlen, iter, ret;
  22. unsigned char *salt;
  23. unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
  24. int (*pkcs12_key_gen)(const char *pass, int passlen,
  25. unsigned char *salt, int slen,
  26. int id, int iter, int n,
  27. unsigned char *out,
  28. const EVP_MD *md_type);
  29. pkcs12_key_gen = PKCS12_key_gen_utf8;
  30. if (cipher == NULL)
  31. return 0;
  32. /* Extract useful info from parameter */
  33. pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
  34. if (pbe == NULL) {
  35. PKCS12err(PKCS12_F_PKCS12_PBE_KEYIVGEN, PKCS12_R_DECODE_ERROR);
  36. return 0;
  37. }
  38. if (pbe->iter == NULL)
  39. iter = 1;
  40. else
  41. iter = ASN1_INTEGER_get(pbe->iter);
  42. salt = pbe->salt->data;
  43. saltlen = pbe->salt->length;
  44. if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_KEY_ID,
  45. iter, EVP_CIPHER_key_length(cipher), key, md)) {
  46. PKCS12err(PKCS12_F_PKCS12_PBE_KEYIVGEN, PKCS12_R_KEY_GEN_ERROR);
  47. PBEPARAM_free(pbe);
  48. return 0;
  49. }
  50. if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_IV_ID,
  51. iter, EVP_CIPHER_iv_length(cipher), iv, md)) {
  52. PKCS12err(PKCS12_F_PKCS12_PBE_KEYIVGEN, PKCS12_R_IV_GEN_ERROR);
  53. PBEPARAM_free(pbe);
  54. return 0;
  55. }
  56. PBEPARAM_free(pbe);
  57. ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, en_de);
  58. OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
  59. OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
  60. return ret;
  61. }