statem.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_INTERNAL_STATEM_H
  10. # define OSSL_INTERNAL_STATEM_H
  11. /*****************************************************************************
  12. * *
  13. * These enums should be considered PRIVATE to the state machine. No *
  14. * non-state machine code should need to use these *
  15. * *
  16. *****************************************************************************/
  17. /*
  18. * Valid return codes used for functions performing work prior to or after
  19. * sending or receiving a message
  20. */
  21. typedef enum {
  22. /* Something went wrong */
  23. WORK_ERROR,
  24. /* We're done working and there shouldn't be anything else to do after */
  25. WORK_FINISHED_STOP,
  26. /* We're done working move onto the next thing */
  27. WORK_FINISHED_CONTINUE,
  28. /* We're done writing, start reading (or vice versa) */
  29. WORK_FINISHED_SWAP,
  30. /* We're working on phase A */
  31. WORK_MORE_A,
  32. /* We're working on phase B */
  33. WORK_MORE_B,
  34. /* We're working on phase C */
  35. WORK_MORE_C
  36. } WORK_STATE;
  37. /* Write transition return codes */
  38. typedef enum {
  39. /* Something went wrong */
  40. WRITE_TRAN_ERROR,
  41. /* A transition was successfully completed and we should continue */
  42. WRITE_TRAN_CONTINUE,
  43. /* There is no more write work to be done */
  44. WRITE_TRAN_FINISHED
  45. } WRITE_TRAN;
  46. /* Message flow states */
  47. typedef enum {
  48. /* No handshake in progress */
  49. MSG_FLOW_UNINITED,
  50. /* A permanent error with this connection */
  51. MSG_FLOW_ERROR,
  52. /* We are reading messages */
  53. MSG_FLOW_READING,
  54. /* We are writing messages */
  55. MSG_FLOW_WRITING,
  56. /* Handshake has finished */
  57. MSG_FLOW_FINISHED
  58. } MSG_FLOW_STATE;
  59. /* Read states */
  60. typedef enum {
  61. READ_STATE_HEADER,
  62. READ_STATE_BODY,
  63. READ_STATE_POST_PROCESS
  64. } READ_STATE;
  65. /* Write states */
  66. typedef enum {
  67. WRITE_STATE_TRANSITION,
  68. WRITE_STATE_PRE_WORK,
  69. WRITE_STATE_SEND,
  70. WRITE_STATE_POST_WORK
  71. } WRITE_STATE;
  72. typedef enum {
  73. CON_FUNC_ERROR = 0,
  74. CON_FUNC_SUCCESS,
  75. CON_FUNC_DONT_SEND
  76. } CON_FUNC_RETURN;
  77. typedef int (*ossl_statem_mutate_handshake_cb)(const unsigned char *msgin,
  78. size_t inlen,
  79. unsigned char **msgout,
  80. size_t *outlen,
  81. void *arg);
  82. typedef void (*ossl_statem_finish_mutate_handshake_cb)(void *arg);
  83. /*****************************************************************************
  84. * *
  85. * This structure should be considered "opaque" to anything outside of the *
  86. * state machine. No non-state machine code should be accessing the members *
  87. * of this structure. *
  88. * *
  89. *****************************************************************************/
  90. struct ossl_statem_st {
  91. MSG_FLOW_STATE state;
  92. WRITE_STATE write_state;
  93. WORK_STATE write_state_work;
  94. READ_STATE read_state;
  95. WORK_STATE read_state_work;
  96. OSSL_HANDSHAKE_STATE hand_state;
  97. /* The handshake state requested by an API call (e.g. HelloRequest) */
  98. OSSL_HANDSHAKE_STATE request_state;
  99. int in_init;
  100. int read_state_first_init;
  101. /* true when we are actually in SSL_accept() or SSL_connect() */
  102. int in_handshake;
  103. /*
  104. * True when are processing a "real" handshake that needs cleaning up (not
  105. * just a HelloRequest or similar).
  106. */
  107. int cleanuphand;
  108. /* Should we skip the CertificateVerify message? */
  109. unsigned int no_cert_verify;
  110. int use_timer;
  111. /* Test harness message mutator callbacks */
  112. ossl_statem_mutate_handshake_cb mutate_handshake_cb;
  113. ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb;
  114. void *mutatearg;
  115. unsigned int write_in_progress : 1;
  116. };
  117. typedef struct ossl_statem_st OSSL_STATEM;
  118. /*****************************************************************************
  119. * *
  120. * The following macros/functions represent the libssl internal API to the *
  121. * state machine. Any libssl code may call these functions/macros *
  122. * *
  123. *****************************************************************************/
  124. typedef struct ssl_connection_st SSL_CONNECTION;
  125. __owur int ossl_statem_accept(SSL *s);
  126. __owur int ossl_statem_connect(SSL *s);
  127. OSSL_HANDSHAKE_STATE ossl_statem_get_state(SSL_CONNECTION *s);
  128. void ossl_statem_clear(SSL_CONNECTION *s);
  129. void ossl_statem_set_renegotiate(SSL_CONNECTION *s);
  130. void ossl_statem_send_fatal(SSL_CONNECTION *s, int al);
  131. void ossl_statem_fatal(SSL_CONNECTION *s, int al, int reason,
  132. const char *fmt, ...);
  133. # define SSLfatal_alert(s, al) ossl_statem_send_fatal((s), (al))
  134. # define SSLfatal(s, al, r) SSLfatal_data((s), (al), (r), NULL)
  135. # define SSLfatal_data \
  136. (ERR_new(), \
  137. ERR_set_debug(OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC), \
  138. ossl_statem_fatal)
  139. int ossl_statem_in_error(const SSL_CONNECTION *s);
  140. void ossl_statem_set_in_init(SSL_CONNECTION *s, int init);
  141. int ossl_statem_get_in_handshake(SSL_CONNECTION *s);
  142. void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand);
  143. __owur int ossl_statem_skip_early_data(SSL_CONNECTION *s);
  144. int ossl_statem_check_finish_init(SSL_CONNECTION *s, int send);
  145. void ossl_statem_set_hello_verify_done(SSL_CONNECTION *s);
  146. __owur int ossl_statem_app_data_allowed(SSL_CONNECTION *s);
  147. __owur int ossl_statem_export_allowed(SSL_CONNECTION *s);
  148. __owur int ossl_statem_export_early_allowed(SSL_CONNECTION *s);
  149. /* Flush the write BIO */
  150. int statem_flush(SSL_CONNECTION *s);
  151. int ossl_statem_set_mutator(SSL *s,
  152. ossl_statem_mutate_handshake_cb mutate_handshake_cb,
  153. ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb,
  154. void *mutatearg);
  155. #endif