p12_p8e.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2001-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/pkcs12.h>
  13. #include "crypto/x509.h"
  14. X509_SIG *PKCS8_encrypt_ex(int pbe_nid, const EVP_CIPHER *cipher,
  15. const char *pass, int passlen,
  16. unsigned char *salt, int saltlen, int iter,
  17. PKCS8_PRIV_KEY_INFO *p8inf,
  18. OSSL_LIB_CTX *libctx, const char *propq)
  19. {
  20. X509_SIG *p8 = NULL;
  21. X509_ALGOR *pbe;
  22. if (pbe_nid == -1) {
  23. if (cipher == NULL) {
  24. ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
  25. return NULL;
  26. }
  27. pbe = PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL, -1,
  28. libctx);
  29. } else {
  30. ERR_set_mark();
  31. if (EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
  32. ERR_clear_last_mark();
  33. if (cipher == NULL) {
  34. ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
  35. return NULL;
  36. }
  37. pbe = PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL,
  38. pbe_nid, libctx);
  39. } else {
  40. ERR_pop_to_mark();
  41. pbe = PKCS5_pbe_set_ex(pbe_nid, iter, salt, saltlen, libctx);
  42. }
  43. }
  44. if (pbe == NULL) {
  45. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  46. return NULL;
  47. }
  48. p8 = PKCS8_set0_pbe_ex(pass, passlen, p8inf, pbe, libctx, propq);
  49. if (p8 == NULL) {
  50. X509_ALGOR_free(pbe);
  51. return NULL;
  52. }
  53. return p8;
  54. }
  55. X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher,
  56. const char *pass, int passlen,
  57. unsigned char *salt, int saltlen, int iter,
  58. PKCS8_PRIV_KEY_INFO *p8inf)
  59. {
  60. return PKCS8_encrypt_ex(pbe_nid, cipher, pass, passlen, salt, saltlen, iter,
  61. p8inf, NULL, NULL);
  62. }
  63. X509_SIG *PKCS8_set0_pbe_ex(const char *pass, int passlen,
  64. PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe,
  65. OSSL_LIB_CTX *ctx, const char *propq)
  66. {
  67. X509_SIG *p8;
  68. ASN1_OCTET_STRING *enckey;
  69. enckey =
  70. PKCS12_item_i2d_encrypt_ex(pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO),
  71. pass, passlen, p8inf, 1, ctx, propq);
  72. if (!enckey) {
  73. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
  74. return NULL;
  75. }
  76. p8 = OPENSSL_zalloc(sizeof(*p8));
  77. if (p8 == NULL) {
  78. ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
  79. ASN1_OCTET_STRING_free(enckey);
  80. return NULL;
  81. }
  82. p8->algor = pbe;
  83. p8->digest = enckey;
  84. return p8;
  85. }
  86. X509_SIG *PKCS8_set0_pbe(const char *pass, int passlen,
  87. PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe)
  88. {
  89. return PKCS8_set0_pbe_ex(pass, passlen, p8inf, pbe, NULL, NULL);
  90. }