statem.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2015-2018 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. /* The enc_write_ctx can be used normally */
  70. ENC_WRITE_STATE_VALID,
  71. /* The enc_write_ctx cannot be used */
  72. ENC_WRITE_STATE_INVALID,
  73. /* Write alerts in plaintext, but otherwise use the enc_write_ctx */
  74. ENC_WRITE_STATE_WRITE_PLAIN_ALERTS
  75. } ENC_WRITE_STATES;
  76. typedef enum {
  77. /* The enc_read_ctx can be used normally */
  78. ENC_READ_STATE_VALID,
  79. /* We may receive encrypted or plaintext alerts */
  80. ENC_READ_STATE_ALLOW_PLAIN_ALERTS
  81. } ENC_READ_STATES;
  82. /*****************************************************************************
  83. * *
  84. * This structure should be considered "opaque" to anything outside of the *
  85. * state machine. No non-state machine code should be accessing the members *
  86. * of this structure. *
  87. * *
  88. *****************************************************************************/
  89. struct ossl_statem_st {
  90. MSG_FLOW_STATE state;
  91. WRITE_STATE write_state;
  92. WORK_STATE write_state_work;
  93. READ_STATE read_state;
  94. WORK_STATE read_state_work;
  95. OSSL_HANDSHAKE_STATE hand_state;
  96. /* The handshake state requested by an API call (e.g. HelloRequest) */
  97. OSSL_HANDSHAKE_STATE request_state;
  98. int in_init;
  99. int read_state_first_init;
  100. /* true when we are actually in SSL_accept() or SSL_connect() */
  101. int in_handshake;
  102. /*
  103. * True when are processing a "real" handshake that needs cleaning up (not
  104. * just a HelloRequest or similar).
  105. */
  106. int cleanuphand;
  107. /* Should we skip the CertificateVerify message? */
  108. unsigned int no_cert_verify;
  109. int use_timer;
  110. ENC_WRITE_STATES enc_write_state;
  111. ENC_READ_STATES enc_read_state;
  112. };
  113. typedef struct ossl_statem_st OSSL_STATEM;
  114. /*****************************************************************************
  115. * *
  116. * The following macros/functions represent the libssl internal API to the *
  117. * state machine. Any libssl code may call these functions/macros *
  118. * *
  119. *****************************************************************************/
  120. __owur int ossl_statem_accept(SSL *s);
  121. __owur int ossl_statem_connect(SSL *s);
  122. void ossl_statem_clear(SSL *s);
  123. void ossl_statem_set_renegotiate(SSL *s);
  124. void ossl_statem_fatal(SSL *s, int al, int func, int reason, const char *file,
  125. int line);
  126. # define SSL_AD_NO_ALERT -1
  127. # ifndef OPENSSL_NO_ERR
  128. # define SSLfatal(s, al, f, r) ossl_statem_fatal((s), (al), (f), (r), \
  129. OPENSSL_FILE, OPENSSL_LINE)
  130. # else
  131. # define SSLfatal(s, al, f, r) ossl_statem_fatal((s), (al), (f), (r), NULL, 0)
  132. # endif
  133. int ossl_statem_in_error(const SSL *s);
  134. void ossl_statem_set_in_init(SSL *s, int init);
  135. int ossl_statem_get_in_handshake(SSL *s);
  136. void ossl_statem_set_in_handshake(SSL *s, int inhand);
  137. __owur int ossl_statem_skip_early_data(SSL *s);
  138. void ossl_statem_check_finish_init(SSL *s, int send);
  139. void ossl_statem_set_hello_verify_done(SSL *s);
  140. __owur int ossl_statem_app_data_allowed(SSL *s);
  141. __owur int ossl_statem_export_allowed(SSL *s);
  142. __owur int ossl_statem_export_early_allowed(SSL *s);
  143. /* Flush the write BIO */
  144. int statem_flush(SSL *s);