Debian Codegraph @

crt/aws-c-io/source/pkcs11_tls_op_handler.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/io/pkcs11.h>
6
7 #include "pkcs11_private.h"
8
9 #include <aws/common/mutex.h>
10 #include <aws/common/string.h>
11 #include <aws/io/logging.h>
12
13 struct aws_pkcs11_tls_op_handler {
14 /* The custom key operation handler needed for the callbacks */
15 struct aws_custom_key_op_handler base;
16
17 struct aws_allocator *alloc;
18 struct aws_pkcs11_lib *lib;
19
20 /* Use a single PKCS#11 session for all TLS connections on an aws_tls_ctx.
21 * We do this because PKCS#11 tokens may only support a
22 * limited number of sessions (PKCS11-UG-v2.40 section 2.6.7).
23 * If this one shared session turns out to be a severe bottleneck,
24 * we could look into other setups (ex: put session on its own thread,
25 * 1 session per event-loop, 1 session per connection, etc).
26 *
27 * The lock must be held while performing session operations.
28 * Otherwise, it would not be safe for multiple threads to share a
29 * session (PKCS11-UG-v2.40 section 2.6.7). The lock isn't needed for
30 * setup and teardown though, since we ensure nothing parallel is going
31 * on at these times */
32 struct aws_mutex session_lock;
33 CK_SESSION_HANDLE session_handle;
34 CK_OBJECT_HANDLE private_key_handle;
35 CK_KEY_TYPE private_key_type;
36 };
37
38 static void s_aws_custom_key_op_handler_destroy(struct aws_custom_key_op_handler *key_op_handler) {
39
40 struct aws_pkcs11_tls_op_handler *handler = (struct aws_pkcs11_tls_op_handler *)key_op_handler->impl;
41
42 if (handler->session_handle != 0) {
43 aws_pkcs11_lib_close_session(handler->lib, handler->session_handle);
44 }
45 aws_mutex_clean_up(&handler->session_lock);
46 aws_pkcs11_lib_release(handler->lib);
47
48 aws_mem_release(handler->alloc, handler);
49 }
50
51 /**
52 * Performs the PKCS11 TLS private key operation. This is called automatically when performing a mutual TLS handshake.
53 */
54 void s_aws_pkcs11_tls_op_handler_do_operation(
55 struct aws_custom_key_op_handler *handler,
56 struct aws_tls_key_operation *operation) {
57
58 struct aws_pkcs11_tls_op_handler *pkcs11_handler = (struct aws_pkcs11_tls_op_handler *)handler->impl;
59 struct aws_byte_buf output_buf; /* initialized later */
60 AWS_ZERO_STRUCT(output_buf);
61
62 /*********** BEGIN CRITICAL SECTION ***********/
63 aws_mutex_lock(&pkcs11_handler->session_lock);
64 bool success_while_locked = false;
65
66 switch (aws_tls_key_operation_get_type(operation)) {
67 case AWS_TLS_KEY_OPERATION_DECRYPT:
68 if (aws_pkcs11_lib_decrypt(
69 pkcs11_handler->lib,
70 pkcs11_handler->session_handle,
71 pkcs11_handler->private_key_handle,
72 pkcs11_handler->private_key_type,
73 aws_tls_key_operation_get_input(operation),
74 pkcs11_handler->alloc,
75 &output_buf)) {
76
77 goto unlock;
78 }
79 break;
80
81 case AWS_TLS_KEY_OPERATION_SIGN:
82 if (aws_pkcs11_lib_sign(
83 pkcs11_handler->lib,
84 pkcs11_handler->session_handle,
85 pkcs11_handler->private_key_handle,
86 pkcs11_handler->private_key_type,
87 aws_tls_key_operation_get_input(operation),
88 pkcs11_handler->alloc,
89 aws_tls_key_operation_get_digest_algorithm(operation),
90 aws_tls_key_operation_get_signature_algorithm(operation),
91 &output_buf)) {
92
93 goto unlock;
94 }
95 break;
96
97 default:
98 AWS_LOGF_ERROR(
99 AWS_LS_IO_PKCS11,
100 "PKCS11 Handler %p: Unknown TLS key operation with value of %u",
101 (void *)handler,
102 aws_tls_key_operation_get_type(operation));
103 aws_raise_error(AWS_ERROR_INVALID_STATE);
104 goto unlock;
105 }
106
107 success_while_locked = true;
108 unlock:
109 aws_mutex_unlock(&pkcs11_handler->session_lock);
110 /*********** END CRITICAL SECTION ***********/
111
112 if (success_while_locked) {
113 aws_tls_key_operation_complete(operation, aws_byte_cursor_from_buf(&output_buf));
114 } else {
115 aws_tls_key_operation_complete_with_error(operation, aws_last_error());
116 }
117
118 aws_byte_buf_clean_up(&output_buf);
119 }
120
121 static struct aws_custom_key_op_handler_vtable s_aws_custom_key_op_handler_vtable = {
122 .on_key_operation = s_aws_pkcs11_tls_op_handler_do_operation,
123 };
124
125 struct aws_custom_key_op_handler *aws_pkcs11_tls_op_handler_new(
126 struct aws_allocator *allocator,
127 struct aws_pkcs11_lib *pkcs11_lib,
128 const struct aws_byte_cursor *user_pin,
129 const struct aws_byte_cursor *match_token_label,
130 const struct aws_byte_cursor *match_private_key_label,
131 const uint64_t *match_slot_id) {
132
133 bool success = false;
134
135 struct aws_pkcs11_tls_op_handler *pkcs11_handler =
136 aws_mem_calloc(allocator, 1, sizeof(struct aws_pkcs11_tls_op_handler));
137
138 // Optional data
139 struct aws_string *pkcs_user_pin = NULL;
140 struct aws_string *pkcs_token_label = NULL;
141 struct aws_string *pkcs_private_key_object_label = NULL;
142
143 aws_ref_count_init(
144 &pkcs11_handler->base.ref_count,
145 &pkcs11_handler->base,
146 (aws_simple_completion_callback *)s_aws_custom_key_op_handler_destroy);
147
148 pkcs11_handler->base.impl = (void *)pkcs11_handler;
149 pkcs11_handler->base.vtable = &s_aws_custom_key_op_handler_vtable;
150
151 pkcs11_handler->alloc = allocator;
152
153 /* pkcs11_lib is required */
154 if (pkcs11_lib == NULL) {
155 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
156 AWS_LOGF_ERROR(AWS_LS_IO_PKCS11, "PKCS11 Handler %p new: PKCS11 library is null", (void *)pkcs11_handler);
157 goto done;
158 }
159 pkcs11_handler->lib = aws_pkcs11_lib_acquire(pkcs11_lib); /* cannot fail */
160 aws_mutex_init(&pkcs11_handler->session_lock);
161
162 /* user_pin is optional */
163 if (user_pin->ptr != NULL) {
164 pkcs_user_pin = aws_string_new_from_cursor(allocator, user_pin);
165 }
166
167 /* token_label is optional */
168 if (match_token_label->ptr != NULL) {
169 pkcs_token_label = aws_string_new_from_cursor(allocator, match_token_label);
170 }
171
172 /* private_key_object_label is optional */
173 if (match_private_key_label->ptr != NULL) {
174 pkcs_private_key_object_label = aws_string_new_from_cursor(allocator, match_private_key_label);
175 }
176
177 CK_SLOT_ID slot_id;
178 if (aws_pkcs11_lib_find_slot_with_token(pkcs11_handler->lib, match_slot_id, pkcs_token_label, &slot_id /*out*/)) {
179 goto done;
180 }
181
182 if (aws_pkcs11_lib_open_session(pkcs11_handler->lib, slot_id, &pkcs11_handler->session_handle)) {
183 goto done;
184 }
185
186 if (aws_pkcs11_lib_login_user(pkcs11_handler->lib, pkcs11_handler->session_handle, pkcs_user_pin)) {
187 goto done;
188 }
189
190 if (aws_pkcs11_lib_find_private_key(
191 pkcs11_handler->lib,
192 pkcs11_handler->session_handle,
193 pkcs_private_key_object_label,
194 &pkcs11_handler->private_key_handle /*out*/,
195 &pkcs11_handler->private_key_type /*out*/)) {
196 goto done;
197 }
198 success = true;
199
200 done:
201
202 /* CLEANUP */
203 if (pkcs_user_pin != NULL) {
204 aws_string_destroy_secure(pkcs_user_pin);
205 }
206 if (pkcs_token_label != NULL) {
207 aws_string_destroy(pkcs_token_label);
208 }
209 if (pkcs_private_key_object_label != NULL) {
210 aws_string_destroy(pkcs_private_key_object_label);
211 }
212
213 if (success) {
214 return &pkcs11_handler->base;
215 } else {
216 aws_custom_key_op_handler_release(&pkcs11_handler->base);
217 return NULL;
218 }
219 }