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