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 : : 16 : : #pragma once 17 : : 18 : : #include "tls/s2n_connection.h" 19 : : #include "utils/s2n_blob.h" 20 : : #include "utils/s2n_result.h" 21 : : 22 : : typedef int (*s2n_async_pkey_sign_complete)(struct s2n_connection *conn, struct s2n_blob *signature); 23 : : typedef int (*s2n_async_pkey_decrypt_complete)(struct s2n_connection *conn, bool rsa_failed, struct s2n_blob *decrypted); 24 : : 25 : : struct s2n_async_pkey_op; 26 : : 27 : : /* Guard to handle async states inside handler which uses async pkey operations. If async operation was not invoked 28 : : * it means that we enter this handler for the first time and handler may or may not use async operation, so we let it 29 : : * continue. If async operation is invoking or was invoked, but yet to be complete, we error out of the handler to let 30 : : * s2n_handle_retry_state try again. If async operation was complete we clear the state and let s2n_handle_retry_state 31 : : * proceed to the next handler */ 32 : : #define S2N_ASYNC_PKEY_GUARD(conn) \ 33 : 5456 : do { \ 34 : 5456 : __typeof(conn) __tmp_conn = (conn); \ 35 : 5456 : POSIX_GUARD_PTR(__tmp_conn); \ 36 : 5456 : switch (conn->handshake.async_state) { \ 37 : 5283 : case S2N_ASYNC_NOT_INVOKED: \ 38 : 5283 : break; \ 39 : 0 : \ 40 : 74 : case S2N_ASYNC_INVOKED: \ 41 : 74 : POSIX_BAIL(S2N_ERR_ASYNC_BLOCKED); \ 42 : 74 : \ 43 : 99 : case S2N_ASYNC_COMPLETE: \ 44 : 99 : /* clean up state and return a success from handler */ \ 45 : 99 : __tmp_conn->handshake.async_state = S2N_ASYNC_NOT_INVOKED; \ 46 : 99 : return S2N_SUCCESS; \ 47 : 5456 : } \ 48 : 5456 : } while (0) 49 : : 50 : : /* Macros for safe exection of async sign/decrypt. 51 : : * 52 : : * When operation is done asynchronously, we drop to s2n_negotiate loop with S2N_ERR_ASYNC_BLOCKED error and do not 53 : : * perform any of the operations to follow after s2n_async* call. To enforce that there are no operations after the 54 : : * call, we use a macro which directly returns the result of s2n_async* operation forcing compiler to error out on 55 : : * unreachable code and forcing developer to use on_complete function instead */ 56 : : #define S2N_ASYNC_PKEY_DECRYPT(conn, encrypted, init_decrypted, on_complete) \ 57 : 1450 : return s2n_result_is_ok(s2n_async_pkey_decrypt(conn, encrypted, init_decrypted, on_complete)) ? S2N_SUCCESS : S2N_FAILURE; 58 : : 59 : : #define S2N_ASYNC_PKEY_SIGN(conn, sig_alg, digest, on_complete) \ 60 : 3800 : return s2n_result_is_ok(s2n_async_pkey_sign(conn, sig_alg, digest, on_complete)) ? S2N_SUCCESS : S2N_FAILURE; 61 : : 62 : : int s2n_async_pkey_op_perform(struct s2n_async_pkey_op *op, s2n_cert_private_key *key); 63 : : int s2n_async_pkey_op_apply(struct s2n_async_pkey_op *op, struct s2n_connection *conn); 64 : : int s2n_async_pkey_op_free(struct s2n_async_pkey_op *op); 65 : : 66 : : int s2n_async_pkey_op_get_op_type(struct s2n_async_pkey_op *op, s2n_async_pkey_op_type *type); 67 : : int s2n_async_pkey_op_get_input_size(struct s2n_async_pkey_op *op, uint32_t *data_len); 68 : : int s2n_async_pkey_op_get_input(struct s2n_async_pkey_op *op, uint8_t *data, uint32_t data_len); 69 : : int s2n_async_pkey_op_set_output(struct s2n_async_pkey_op *op, const uint8_t *data, uint32_t data_len); 70 : : int s2n_async_pkey_op_set_validation_mode(struct s2n_async_pkey_op *op, s2n_async_pkey_validation_mode mode); 71 : : 72 : : S2N_RESULT s2n_async_pkey_verify_signature(struct s2n_connection *conn, s2n_signature_algorithm sig_alg, 73 : : struct s2n_hash_state *digest, struct s2n_blob *signature); 74 : : S2N_RESULT s2n_async_pkey_decrypt(struct s2n_connection *conn, struct s2n_blob *encrypted, struct s2n_blob *init_decrypted, 75 : : s2n_async_pkey_decrypt_complete on_complete); 76 : : S2N_RESULT s2n_async_pkey_sign(struct s2n_connection *conn, s2n_signature_algorithm sig_alg, struct s2n_hash_state *digest, 77 : : s2n_async_pkey_sign_complete on_complete);