statem.h 5.4 KB

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