Debian Codegraph @

crt/s2n/tls/s2n_async_offload.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15 #include "tls/s2n_async_offload.h"
16
17 #include "api/s2n.h"
18 #include "crypto/s2n_hash.h"
19 #include "crypto/s2n_signature.h"
20 #include "error/s2n_errno.h"
21 #include "tls/s2n_connection.h"
22 #include "tls/s2n_handshake.h"
23 #include "utils/s2n_blob.h"
24 #include "utils/s2n_mem.h"
25 #include "utils/s2n_result.h"
26 #include "utils/s2n_safety.h"
27
28 S2N_RESULT s2n_async_offload_cb_invoke(struct s2n_connection *conn, struct s2n_async_offload_op *op)
29 {
30 RESULT_ENSURE_REF(conn);
31 RESULT_ENSURE_REF(conn->config);
32 RESULT_ENSURE_REF(conn->config->async_offload_cb);
33
34 RESULT_ENSURE_REF(op);
35 RESULT_ENSURE_REF(op->perform);
36 RESULT_ENSURE_REF(op->op_data_free);
37 RESULT_ENSURE(op->async_state == S2N_ASYNC_NOT_INVOKED, S2N_ERR_ASYNC_MORE_THAN_ONE);
38
39 op->async_state = S2N_ASYNC_INVOKED;
40 RESULT_ENSURE(conn->config->async_offload_cb(conn, op, conn->config->async_offload_ctx) == S2N_SUCCESS,
41 S2N_ERR_CANCELLED);
42
43 /*
44 * If the callback already completed the operation, continue.
45 * Otherwise, we need to block s2n_negotiate and wait for the operation to complete.
46 */
47 if (op->async_state == S2N_ASYNC_COMPLETE) {
48 return S2N_RESULT_OK;
49 }
50 RESULT_BAIL(S2N_ERR_ASYNC_BLOCKED);
51 }
52
53 int s2n_async_offload_op_perform(struct s2n_async_offload_op *op)
54 {
55 POSIX_ENSURE_REF(op);
56 POSIX_ENSURE(op->async_state == S2N_ASYNC_INVOKED, S2N_ERR_INVALID_STATE);
57 POSIX_ENSURE(op->type != 0, S2N_ERR_INVALID_STATE);
58
59 POSIX_ENSURE_REF(op->conn);
60 POSIX_ENSURE_REF(op->perform);
61
62 POSIX_GUARD_RESULT(op->perform(op));
63 op->async_state = S2N_ASYNC_COMPLETE;
64 return S2N_SUCCESS;
65 }
66
67 S2N_RESULT s2n_async_offload_op_wipe(struct s2n_async_offload_op *op)
68 {
69 RESULT_ENSURE_REF(op);
70 if (op->op_data_free == NULL) {
71 return S2N_RESULT_OK;
72 }
73
74 RESULT_GUARD(op->op_data_free(op));
75 RESULT_CHECKED_MEMSET(op, 0, sizeof(struct s2n_async_offload_op));
76 return S2N_RESULT_OK;
77 }
78
79 /**
80 * MUST be called at the end of each handshake state handler that may invoke async_offload_cb
81 * to clean up the op object for its next use.
82 */
83 S2N_RESULT s2n_async_offload_op_reset(struct s2n_async_offload_op *op)
84 {
85 RESULT_ENSURE_REF(op);
86 /* Sync case without the callback: async_offload_cb not invoked in the current state */
87 if (op->async_state == S2N_ASYNC_NOT_INVOKED) {
88 return S2N_RESULT_OK;
89 }
90
91 RESULT_ENSURE(op->async_state == S2N_ASYNC_COMPLETE, S2N_ERR_INVALID_STATE);
92 RESULT_GUARD(s2n_async_offload_op_wipe(op));
93 return S2N_RESULT_OK;
94 }
95
96 bool s2n_async_offload_op_is_in_allow_list(struct s2n_config *config, s2n_async_offload_op_type op_type)
97 {
98 return config && (config->async_offload_allow_list & op_type);
99 }