statem.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2015-2023 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 working on phase A */
  29. WORK_MORE_A,
  30. /* We're working on phase B */
  31. WORK_MORE_B,
  32. /* We're working on phase C */
  33. WORK_MORE_C
  34. } WORK_STATE;
  35. /* Write transition return codes */
  36. typedef enum {
  37. /* Something went wrong */
  38. WRITE_TRAN_ERROR,
  39. /* A transition was successfully completed and we should continue */
  40. WRITE_TRAN_CONTINUE,
  41. /* There is no more write work to be done */
  42. WRITE_TRAN_FINISHED
  43. } WRITE_TRAN;
  44. /* Message flow states */
  45. typedef enum {
  46. /* No handshake in progress */
  47. MSG_FLOW_UNINITED,
  48. /* A permanent error with this connection */
  49. MSG_FLOW_ERROR,
  50. /* We are reading messages */
  51. MSG_FLOW_READING,
  52. /* We are writing messages */
  53. MSG_FLOW_WRITING,
  54. /* Handshake has finished */
  55. MSG_FLOW_FINISHED
  56. } MSG_FLOW_STATE;
  57. /* Read states */
  58. typedef enum {
  59. READ_STATE_HEADER,
  60. READ_STATE_BODY,
  61. READ_STATE_POST_PROCESS
  62. } READ_STATE;
  63. /* Write states */
  64. typedef enum {
  65. WRITE_STATE_TRANSITION,
  66. WRITE_STATE_PRE_WORK,
  67. WRITE_STATE_SEND,
  68. WRITE_STATE_POST_WORK
  69. } WRITE_STATE;
  70. typedef enum {
  71. CON_FUNC_ERROR = 0,
  72. CON_FUNC_SUCCESS,
  73. CON_FUNC_DONT_SEND
  74. } CON_FUNC_RETURN;
  75. typedef int (*ossl_statem_mutate_handshake_cb)(const unsigned char *msgin,
  76. size_t inlen,
  77. unsigned char **msgout,
  78. size_t *outlen,
  79. void *arg);
  80. typedef void (*ossl_statem_finish_mutate_handshake_cb)(void *arg);
  81. /*****************************************************************************
  82. * *
  83. * This structure should be considered "opaque" to anything outside of the *
  84. * state machine. No non-state machine code should be accessing the members *
  85. * of this structure. *
  86. * *
  87. *****************************************************************************/
  88. struct ossl_statem_st {
  89. MSG_FLOW_STATE state;
  90. WRITE_STATE write_state;
  91. WORK_STATE write_state_work;
  92. READ_STATE read_state;
  93. WORK_STATE read_state_work;
  94. OSSL_HANDSHAKE_STATE hand_state;
  95. /* The handshake state requested by an API call (e.g. HelloRequest) */
  96. OSSL_HANDSHAKE_STATE request_state;
  97. int in_init;
  98. int read_state_first_init;
  99. /* true when we are actually in SSL_accept() or SSL_connect() */
  100. int in_handshake;
  101. /*
  102. * True when are processing a "real" handshake that needs cleaning up (not
  103. * just a HelloRequest or similar).
  104. */
  105. int cleanuphand;
  106. /* Should we skip the CertificateVerify message? */
  107. unsigned int no_cert_verify;
  108. int use_timer;
  109. /* Test harness message mutator callbacks */
  110. ossl_statem_mutate_handshake_cb mutate_handshake_cb;
  111. ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb;
  112. void *mutatearg;
  113. unsigned int write_in_progress : 1;
  114. };
  115. typedef struct ossl_statem_st OSSL_STATEM;
  116. /*****************************************************************************
  117. * *
  118. * The following macros/functions represent the libssl internal API to the *
  119. * state machine. Any libssl code may call these functions/macros *
  120. * *
  121. *****************************************************************************/
  122. typedef struct ssl_connection_st SSL_CONNECTION;
  123. __owur int ossl_statem_accept(SSL *s);
  124. __owur int ossl_statem_connect(SSL *s);
  125. OSSL_HANDSHAKE_STATE ossl_statem_get_state(SSL_CONNECTION *s);
  126. void ossl_statem_clear(SSL_CONNECTION *s);
  127. void ossl_statem_set_renegotiate(SSL_CONNECTION *s);
  128. void ossl_statem_send_fatal(SSL_CONNECTION *s, int al);
  129. void ossl_statem_fatal(SSL_CONNECTION *s, int al, int reason,
  130. const char *fmt, ...);
  131. # define SSLfatal_alert(s, al) ossl_statem_send_fatal((s), (al))
  132. # define SSLfatal(s, al, r) SSLfatal_data((s), (al), (r), NULL)
  133. # define SSLfatal_data \
  134. (ERR_new(), \
  135. ERR_set_debug(OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC), \
  136. ossl_statem_fatal)
  137. int ossl_statem_in_error(const SSL_CONNECTION *s);
  138. void ossl_statem_set_in_init(SSL_CONNECTION *s, int init);
  139. int ossl_statem_get_in_handshake(SSL_CONNECTION *s);
  140. void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand);
  141. __owur int ossl_statem_skip_early_data(SSL_CONNECTION *s);
  142. void ossl_statem_check_finish_init(SSL_CONNECTION *s, int send);
  143. void ossl_statem_set_hello_verify_done(SSL_CONNECTION *s);
  144. __owur int ossl_statem_app_data_allowed(SSL_CONNECTION *s);
  145. __owur int ossl_statem_export_allowed(SSL_CONNECTION *s);
  146. __owur int ossl_statem_export_early_allowed(SSL_CONNECTION *s);
  147. /* Flush the write BIO */
  148. int statem_flush(SSL_CONNECTION *s);
  149. int ossl_statem_set_mutator(SSL *s,
  150. ossl_statem_mutate_handshake_cb mutate_handshake_cb,
  151. ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb,
  152. void *mutatearg);
  153. #endif