s2n_async_pkey.h 4.7 KB

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