p12_init.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 1999-2023 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. #include "crypto/pkcs7.h"
  13. #include "p12_local.h"
  14. /* Initialise a PKCS12 structure to take data */
  15. PKCS12 *PKCS12_init_ex(int mode, OSSL_LIB_CTX *ctx, const char *propq)
  16. {
  17. PKCS12 *pkcs12;
  18. if ((pkcs12 = PKCS12_new()) == NULL) {
  19. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  20. return NULL;
  21. }
  22. if (!ASN1_INTEGER_set(pkcs12->version, 3))
  23. goto err;
  24. pkcs12->authsafes->type = OBJ_nid2obj(mode);
  25. ossl_pkcs7_set0_libctx(pkcs12->authsafes, ctx);
  26. if (!ossl_pkcs7_set1_propq(pkcs12->authsafes, propq)) {
  27. ERR_raise(ERR_LIB_PKCS12, ERR_R_PKCS7_LIB);
  28. goto err;
  29. }
  30. switch (mode) {
  31. case NID_pkcs7_data:
  32. if ((pkcs12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL) {
  33. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  34. goto err;
  35. }
  36. break;
  37. default:
  38. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNSUPPORTED_PKCS12_MODE);
  39. goto err;
  40. }
  41. return pkcs12;
  42. err:
  43. PKCS12_free(pkcs12);
  44. return NULL;
  45. }
  46. PKCS12 *PKCS12_init(int mode)
  47. {
  48. return PKCS12_init_ex(mode, NULL, NULL);
  49. }
  50. const PKCS7_CTX *ossl_pkcs12_get0_pkcs7ctx(const PKCS12 *p12)
  51. {
  52. if (p12 == NULL || p12->authsafes == NULL)
  53. return NULL;
  54. return &p12->authsafes->ctx;
  55. }