Debian Codegraph @

crt/aws-c-io/source/pkcs11_private.h

raw symbols SCIP
1 #ifndef AWS_IO_PKCS11_PRIVATE_H
2 #define AWS_IO_PKCS11_PRIVATE_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8 #include <aws/io/tls_channel_handler.h>
9
10 #include "pkcs11/v2.40/pkcs11.h"
11
12 /**
13 * pkcs11_private.h
14 * This file declares symbols that are private to aws-c-io but need to be
15 * accessed from multiple .c files.
16 *
17 * NOTE: Not putting this file under `include/private/...` like we usually
18 * do with private headers because it breaks aws-crt-swift. Swift was trying
19 * to compile each file under include/, but the official PKCS#11 header files
20 * are too weird break it.
21 */
22
23 struct aws_pkcs11_lib;
24 struct aws_pkcs11_tls_key_handler;
25 struct aws_string;
26
27 AWS_EXTERN_C_BEGIN
28
29 /**
30 * Return c-string for PKCS#11 CKR_* constant.
31 * For use in tests only.
32 */
33 AWS_IO_API
34 const char *aws_pkcs11_ckr_str(CK_RV rv);
35
36 /**
37 * Return the raw function list.
38 * For use in tests only.
39 */
40 AWS_IO_API
41 CK_FUNCTION_LIST *aws_pkcs11_lib_get_function_list(struct aws_pkcs11_lib *pkcs11_lib);
42
43 /**
44 * Find the slot that meets all criteria:
45 * - has a token
46 * - if match_slot_id is non-null, then slot IDs must match
47 * - if match_token_label is non-null, then labels must match
48 * The function fails unless it finds exactly one slot meeting all criteria.
49 */
50 AWS_IO_API
51 int aws_pkcs11_lib_find_slot_with_token(
52 struct aws_pkcs11_lib *pkcs11_lib,
53 const uint64_t *match_slot_id,
54 const struct aws_string *match_token_label,
55 CK_SLOT_ID *out_slot_id);
56
57 AWS_IO_API
58 int aws_pkcs11_lib_open_session(
59 struct aws_pkcs11_lib *pkcs11_lib,
60 CK_SLOT_ID slot_id,
61 CK_SESSION_HANDLE *out_session_handle);
62
63 AWS_IO_API
64 void aws_pkcs11_lib_close_session(struct aws_pkcs11_lib *pkcs11_lib, CK_SESSION_HANDLE session_handle);
65
66 AWS_IO_API
67 int aws_pkcs11_lib_login_user(
68 struct aws_pkcs11_lib *pkcs11_lib,
69 CK_SESSION_HANDLE session_handle,
70 const struct aws_string *optional_user_pin);
71
72 /**
73 * Find the object that meets all criteria:
74 * - is private key
75 * - if match_label is non-null, then labels must match
76 * The function fails unless it finds exactly one object meeting all criteria.
77 */
78 AWS_IO_API
79 int aws_pkcs11_lib_find_private_key(
80 struct aws_pkcs11_lib *pkcs11_lib,
81 CK_SESSION_HANDLE session_handle,
82 const struct aws_string *match_label,
83 CK_OBJECT_HANDLE *out_key_handle,
84 CK_KEY_TYPE *out_key_type);
85
86 /**
87 * Decrypt the encrypted data.
88 * out_data should be passed in uninitialized.
89 * If successful, out_data will be initialized and contain the recovered data.
90 */
91 AWS_IO_API
92 int aws_pkcs11_lib_decrypt(
93 struct aws_pkcs11_lib *pkcs11_lib,
94 CK_SESSION_HANDLE session_handle,
95 CK_OBJECT_HANDLE key_handle,
96 CK_KEY_TYPE key_type,
97 struct aws_byte_cursor encrypted_data,
98 struct aws_allocator *allocator,
99 struct aws_byte_buf *out_data);
100
101 /**
102 * Sign a digest with the private key during TLS negotiation.
103 * out_signature should be passed in uninitialized.
104 * If successful, out_signature will be initialized and contain the signature.
105 */
106 AWS_IO_API
107 int aws_pkcs11_lib_sign(
108 struct aws_pkcs11_lib *pkcs11_lib,
109 CK_SESSION_HANDLE session_handle,
110 CK_OBJECT_HANDLE key_handle,
111 CK_KEY_TYPE key_type,
112 struct aws_byte_cursor digest_data,
113 struct aws_allocator *allocator,
114 enum aws_tls_hash_algorithm digest_alg,
115 enum aws_tls_signature_algorithm signature_alg,
116 struct aws_byte_buf *out_signature);
117
118 /**
119 * Get the DER encoded DigestInfo value to be prefixed to the hash, used for RSA signing
120 * See https://tools.ietf.org/html/rfc3447#page-43
121 */
122 AWS_IO_API
123 int aws_get_prefix_to_rsa_sig(enum aws_tls_hash_algorithm digest_alg, struct aws_byte_cursor *out_prefix);
124
125 /**
126 * ASN.1 DER encode a big unsigned integer. Note that the source integer may be zero padded. It may also have
127 * most significant bit set. The encoded format is canonical and unambiguous - that is, most significant
128 * bit is never set.
129 */
130 AWS_IO_API
131 int aws_pkcs11_asn1_enc_ubigint(struct aws_byte_buf *const buffer, struct aws_byte_cursor bigint);
132
133 /**
134 * Creates a new PKCS11 TLS operation handler with an associated aws_custom_key_op_handler
135 * with a reference count set to 1.
136 *
137 * The PKCS11 TLS operation handler will automatically be destroyed when the reference count reaches zero
138 * on the aws_custom_key_op_handler.
139 */
140 AWS_IO_API
141 struct aws_custom_key_op_handler *aws_pkcs11_tls_op_handler_new(
142 struct aws_allocator *allocator,
143 struct aws_pkcs11_lib *pkcs11_lib,
144 const struct aws_byte_cursor *user_pin,
145 const struct aws_byte_cursor *match_token_label,
146 const struct aws_byte_cursor *match_private_key_label,
147 const uint64_t *match_slot_id);
148
149 AWS_EXTERN_C_END
150 #endif /* AWS_IO_PKCS11_PRIVATE_H */