pkcs11.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef AWS_IO_PKCS11_H
  2. #define AWS_IO_PKCS11_H
  3. /**
  4. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. * SPDX-License-Identifier: Apache-2.0.
  6. */
  7. #include <aws/io/io.h>
  8. struct aws_allocator;
  9. /**
  10. * Handle to a loaded PKCS#11 library.
  11. */
  12. struct aws_pkcs11_lib;
  13. /**
  14. * Controls how aws_pkcs11_lib calls C_Initialize() and C_Finalize() on the PKCS#11 library.
  15. */
  16. enum aws_pkcs11_lib_behavior {
  17. /**
  18. * Default behavior that accommodates most use cases.
  19. * C_Initialize() is called on creation, and "already-initialized" errors are ignored.
  20. * C_Finalize() is never called, just in case another part of your
  21. * application is still using the PKCS#11 library.
  22. */
  23. AWS_PKCS11_LIB_DEFAULT_BEHAVIOR,
  24. /**
  25. * Skip calling C_Initialize() and C_Finalize().
  26. * Use this if your application has already initialized the PKCS#11 library,
  27. * and you do not want C_Initialize() called again.
  28. */
  29. AWS_PKCS11_LIB_OMIT_INITIALIZE,
  30. /**
  31. * C_Initialize() is called on creation and C_Finalize() is called on cleanup.
  32. * If C_Initialize() reports that's it's already initialized, this is treated as an error.
  33. * Use this if you need perfect cleanup (ex: running valgrind with --leak-check).
  34. */
  35. AWS_PKCS11_LIB_STRICT_INITIALIZE_FINALIZE,
  36. };
  37. /* The enum above was misspelled, and later got fixed (pcks11 -> pkcs11).
  38. * This macro maintain backwards compatibility with the old spelling */
  39. #define aws_pcks11_lib_behavior aws_pkcs11_lib_behavior
  40. /**
  41. * Options for aws_pkcs11_lib_new()
  42. */
  43. struct aws_pkcs11_lib_options {
  44. /**
  45. * Name of PKCS#11 library file to load (UTF-8).
  46. * Zero out if your application is compiled with PKCS#11 symbols linked in.
  47. */
  48. struct aws_byte_cursor filename;
  49. /**
  50. * Behavior for calling C_Initialize() and C_Finalize() on the PKCS#11 library.
  51. */
  52. enum aws_pkcs11_lib_behavior initialize_finalize_behavior;
  53. };
  54. AWS_EXTERN_C_BEGIN
  55. /**
  56. * Load and initialize a PKCS#11 library.
  57. * See `aws_pkcs11_lib_options` for options.
  58. *
  59. * If successful a valid pointer is returned. You must call aws_pkcs11_lib_release() when you are done with it.
  60. * If unsuccessful, NULL is returned and an error is set.
  61. */
  62. AWS_IO_API
  63. struct aws_pkcs11_lib *aws_pkcs11_lib_new(
  64. struct aws_allocator *allocator,
  65. const struct aws_pkcs11_lib_options *options);
  66. /**
  67. * Acquire a reference to a PKCS#11 library, preventing it from being cleaned up.
  68. * You must call aws_pkcs11_lib_release() when you are done with it.
  69. * This function returns whatever was passed in. It cannot fail.
  70. */
  71. AWS_IO_API
  72. struct aws_pkcs11_lib *aws_pkcs11_lib_acquire(struct aws_pkcs11_lib *pkcs11_lib);
  73. /**
  74. * Release a reference to the PKCS#11 library.
  75. * When the last reference is released, the library is cleaned up.
  76. */
  77. AWS_IO_API
  78. void aws_pkcs11_lib_release(struct aws_pkcs11_lib *pkcs11_lib);
  79. AWS_EXTERN_C_END
  80. #endif /* AWS_IO_PKCS11_H */