Branch data Line data Source code
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 : 12 : { 30 [ - + ][ # # ]: 12 : RESULT_ENSURE_REF(conn); 31 [ - + ][ # # ]: 12 : RESULT_ENSURE_REF(conn->config); 32 [ # # ][ - + ]: 12 : RESULT_ENSURE_REF(conn->config->async_offload_cb); 33 : : 34 [ - + ][ # # ]: 12 : RESULT_ENSURE_REF(op); 35 [ - + ][ # # ]: 12 : RESULT_ENSURE_REF(op->perform); 36 [ - + ][ # # ]: 12 : RESULT_ENSURE_REF(op->op_data_free); 37 [ - + ][ # # ]: 12 : RESULT_ENSURE(op->async_state == S2N_ASYNC_NOT_INVOKED, S2N_ERR_ASYNC_MORE_THAN_ONE); 38 : : 39 : 12 : op->async_state = S2N_ASYNC_INVOKED; 40 [ + + ][ + - ]: 12 : RESULT_ENSURE(conn->config->async_offload_cb(conn, op, conn->config->async_offload_ctx) == S2N_SUCCESS, 41 : 10 : 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 [ + + ]: 10 : if (op->async_state == S2N_ASYNC_COMPLETE) { 48 : 4 : return S2N_RESULT_OK; 49 : 4 : } 50 [ + - ]: 6 : RESULT_BAIL(S2N_ERR_ASYNC_BLOCKED); 51 : 6 : } 52 : : 53 : : int s2n_async_offload_op_perform(struct s2n_async_offload_op *op) 54 : 20 : { 55 [ + + ][ + - ]: 20 : POSIX_ENSURE_REF(op); 56 [ + + ][ + - ]: 19 : POSIX_ENSURE(op->async_state == S2N_ASYNC_INVOKED, S2N_ERR_INVALID_STATE); 57 [ - + ][ # # ]: 12 : POSIX_ENSURE(op->type != 0, S2N_ERR_INVALID_STATE); 58 : : 59 [ - + ][ # # ]: 12 : POSIX_ENSURE_REF(op->conn); 60 [ # # ][ - + ]: 12 : POSIX_ENSURE_REF(op->perform); 61 : : 62 [ - + ]: 12 : POSIX_GUARD_RESULT(op->perform(op)); 63 : 12 : op->async_state = S2N_ASYNC_COMPLETE; 64 : 12 : return S2N_SUCCESS; 65 : 12 : } 66 : : 67 : : S2N_RESULT s2n_async_offload_op_wipe(struct s2n_async_offload_op *op) 68 : 3548355 : { 69 [ - + ][ # # ]: 3548355 : RESULT_ENSURE_REF(op); 70 [ + + ]: 3548355 : if (op->op_data_free == NULL) { 71 : 3548343 : return S2N_RESULT_OK; 72 : 3548343 : } 73 : : 74 [ - + ]: 12 : RESULT_GUARD(op->op_data_free(op)); 75 [ - + ][ # # ]: 12 : RESULT_CHECKED_MEMSET(op, 0, sizeof(struct s2n_async_offload_op)); [ + - ] 76 : 12 : return S2N_RESULT_OK; 77 : 12 : } 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 : 3225 : { 85 [ # # ][ - + ]: 3225 : RESULT_ENSURE_REF(op); 86 : : /* Sync case without the callback: async_offload_cb not invoked in the current state */ 87 [ + + ]: 3225 : if (op->async_state == S2N_ASYNC_NOT_INVOKED) { 88 : 3215 : return S2N_RESULT_OK; 89 : 3215 : } 90 : : 91 [ # # ][ - + ]: 10 : RESULT_ENSURE(op->async_state == S2N_ASYNC_COMPLETE, S2N_ERR_INVALID_STATE); 92 [ - + ]: 10 : RESULT_GUARD(s2n_async_offload_op_wipe(op)); 93 : 10 : return S2N_RESULT_OK; 94 : 10 : } 95 : : 96 : : bool s2n_async_offload_op_is_in_allow_list(struct s2n_config *config, s2n_async_offload_op_type op_type) 97 : 3250 : { 98 [ + - ][ + + ]: 3250 : return config && (config->async_offload_allow_list & op_type); 99 : 3250 : }