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