statem_locl.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. * The following definitions are PRIVATE to the state machine. They should *
  12. * NOT be used outside of the state machine. *
  13. * *
  14. *****************************************************************************/
  15. /* Max message length definitions */
  16. /* The spec allows for a longer length than this, but we limit it */
  17. #define HELLO_VERIFY_REQUEST_MAX_LENGTH 258
  18. #define END_OF_EARLY_DATA_MAX_LENGTH 0
  19. #define SERVER_HELLO_MAX_LENGTH 20000
  20. #define HELLO_RETRY_REQUEST_MAX_LENGTH 20000
  21. #define ENCRYPTED_EXTENSIONS_MAX_LENGTH 20000
  22. #define SERVER_KEY_EXCH_MAX_LENGTH 102400
  23. #define SERVER_HELLO_DONE_MAX_LENGTH 0
  24. #define KEY_UPDATE_MAX_LENGTH 1
  25. #define CCS_MAX_LENGTH 1
  26. /* Max should actually be 36 but we are generous */
  27. #define FINISHED_MAX_LENGTH 64
  28. /* The maximum number of incoming KeyUpdate messages we will accept */
  29. #define MAX_KEY_UPDATE_MESSAGES 32
  30. /* Dummy message type */
  31. #define SSL3_MT_DUMMY -1
  32. /* Message processing return codes */
  33. typedef enum {
  34. /* Something bad happened */
  35. MSG_PROCESS_ERROR,
  36. /* We've finished reading - swap to writing */
  37. MSG_PROCESS_FINISHED_READING,
  38. /*
  39. * We've completed the main processing of this message but there is some
  40. * post processing to be done.
  41. */
  42. MSG_PROCESS_CONTINUE_PROCESSING,
  43. /* We've finished this message - read the next message */
  44. MSG_PROCESS_CONTINUE_READING
  45. } MSG_PROCESS_RETURN;
  46. /* Flush the write BIO */
  47. int statem_flush(SSL *s);
  48. typedef int (*confunc_f) (SSL *s, WPACKET *pkt);
  49. int check_in_list(SSL *s, unsigned int group_id, const unsigned char *groups,
  50. size_t num_groups, int checkallow);
  51. int create_synthetic_message_hash(SSL *s);
  52. int parse_ca_names(SSL *s, PACKET *pkt, int *al);
  53. int construct_ca_names(SSL *s, WPACKET *pkt);
  54. /*
  55. * TLS/DTLS client state machine functions
  56. */
  57. int ossl_statem_client_read_transition(SSL *s, int mt);
  58. WRITE_TRAN ossl_statem_client_write_transition(SSL *s);
  59. WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst);
  60. WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst);
  61. int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
  62. confunc_f *confunc, int *mt);
  63. size_t ossl_statem_client_max_message_size(SSL *s);
  64. MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt);
  65. WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst);
  66. /*
  67. * TLS/DTLS server state machine functions
  68. */
  69. int ossl_statem_server_read_transition(SSL *s, int mt);
  70. WRITE_TRAN ossl_statem_server_write_transition(SSL *s);
  71. WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst);
  72. WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst);
  73. int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
  74. confunc_f *confunc,int *mt);
  75. size_t ossl_statem_server_max_message_size(SSL *s);
  76. MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt);
  77. WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst);
  78. /* Functions for getting new message data */
  79. __owur int tls_get_message_header(SSL *s, int *mt);
  80. __owur int tls_get_message_body(SSL *s, size_t *len);
  81. __owur int dtls_get_message(SSL *s, int *mt, size_t *len);
  82. /* Message construction and processing functions */
  83. __owur int tls_process_initial_server_flight(SSL *s, int *al);
  84. __owur MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt);
  85. __owur MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt);
  86. __owur int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt);
  87. __owur int dtls_construct_change_cipher_spec(SSL *s, WPACKET *pkt);
  88. __owur int tls_construct_finished(SSL *s, WPACKET *pkt);
  89. __owur int tls_construct_key_update(SSL *s, WPACKET *pkt);
  90. __owur MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt);
  91. __owur WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs);
  92. __owur WORK_STATE dtls_wait_for_dry(SSL *s);
  93. /* some client-only functions */
  94. __owur int tls_construct_client_hello(SSL *s, WPACKET *pkt);
  95. __owur MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt);
  96. __owur MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt);
  97. __owur MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt);
  98. __owur int tls_process_cert_status_body(SSL *s, PACKET *pkt, int *al);
  99. __owur MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt);
  100. __owur MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt);
  101. __owur int tls_construct_cert_verify(SSL *s, WPACKET *pkt);
  102. __owur WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst);
  103. __owur int tls_construct_client_certificate(SSL *s, WPACKET *pkt);
  104. __owur int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey);
  105. __owur int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt);
  106. __owur int tls_client_key_exchange_post_work(SSL *s);
  107. __owur int tls_construct_cert_status_body(SSL *s, WPACKET *pkt);
  108. __owur int tls_construct_cert_status(SSL *s, WPACKET *pkt);
  109. __owur MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt);
  110. __owur MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt);
  111. __owur int ssl3_check_cert_and_algorithm(SSL *s);
  112. #ifndef OPENSSL_NO_NEXTPROTONEG
  113. __owur int tls_construct_next_proto(SSL *s, WPACKET *pkt);
  114. #endif
  115. __owur MSG_PROCESS_RETURN tls_process_hello_req(SSL *s, PACKET *pkt);
  116. __owur MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt);
  117. __owur int tls_construct_end_of_early_data(SSL *s, WPACKET *pkt);
  118. /* some server-only functions */
  119. __owur MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt);
  120. __owur WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst);
  121. __owur int tls_construct_server_hello(SSL *s, WPACKET *pkt);
  122. __owur int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt);
  123. __owur int tls_construct_server_certificate(SSL *s, WPACKET *pkt);
  124. __owur int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt);
  125. __owur int tls_construct_certificate_request(SSL *s, WPACKET *pkt);
  126. __owur int tls_construct_server_done(SSL *s, WPACKET *pkt);
  127. __owur MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt);
  128. __owur MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt);
  129. __owur WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst);
  130. __owur MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt);
  131. #ifndef OPENSSL_NO_NEXTPROTONEG
  132. __owur MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt);
  133. #endif
  134. __owur int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt);
  135. MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt);
  136. /* Extension processing */
  137. __owur int extension_is_relevant(SSL *s, unsigned int extctx,
  138. unsigned int thisctx);
  139. __owur int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
  140. RAW_EXTENSION **res, int *al, size_t *len,
  141. int init);
  142. __owur int tls_parse_extension(SSL *s, TLSEXT_INDEX idx, int context,
  143. RAW_EXTENSION *exts, X509 *x, size_t chainidx,
  144. int *al);
  145. __owur int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
  146. X509 *x, size_t chainidx, int *al, int fin);
  147. __owur int should_add_extension(SSL *s, unsigned int extctx,
  148. unsigned int thisctx, int max_version);
  149. __owur int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
  150. X509 *x, size_t chainidx, int *al);
  151. __owur int tls_psk_do_binder(SSL *s, const EVP_MD *md,
  152. const unsigned char *msgstart,
  153. size_t binderoffset, const unsigned char *binderin,
  154. unsigned char *binderout,
  155. SSL_SESSION *sess, int sign);
  156. /* Server Extension processing */
  157. int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
  158. X509 *x, size_t chainidx, int *al);
  159. int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
  160. X509 *x, size_t chainidx, int *al);
  161. #ifndef OPENSSL_NO_SRP
  162. int tls_parse_ctos_srp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  163. size_t chainidx, int *al);
  164. #endif
  165. int tls_parse_ctos_early_data(SSL *s, PACKET *pkt, unsigned int context,
  166. X509 *x, size_t chainidx, int *al);
  167. #ifndef OPENSSL_NO_EC
  168. int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
  169. X509 *x, size_t chainidx, int *al);
  170. int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, unsigned int context,
  171. X509 *x, size_t chainidx, int *al);
  172. #endif
  173. int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
  174. X509 *x, size_t chainidx, int *al);
  175. int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  176. size_t chainidx, int *al);
  177. #ifndef OPENSSL_NO_OCSP
  178. int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, unsigned int context,
  179. X509 *x, size_t chainidx, int *al);
  180. #endif
  181. #ifndef OPENSSL_NO_NEXTPROTONEG
  182. int tls_parse_ctos_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  183. size_t chainidx, int *al);
  184. #endif
  185. int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  186. size_t chainidx, int *al);
  187. #ifndef OPENSSL_NO_SRTP
  188. int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  189. size_t chainidx, int *al);
  190. #endif
  191. int tls_parse_ctos_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  192. size_t chainidx, int *al);
  193. int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  194. size_t chainidx, int *al);
  195. int tls_parse_ctos_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  196. size_t chainidx, int *al);
  197. int tls_parse_ctos_psk_kex_modes(SSL *s, PACKET *pkt, unsigned int context,
  198. X509 *x, size_t chainidx, int *al);
  199. int tls_parse_ctos_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  200. size_t chainidx, int *al);
  201. int tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt, unsigned int context,
  202. X509 *x, size_t chainidx, int *al);
  203. int tls_construct_stoc_server_name(SSL *s, WPACKET *pkt, unsigned int context,
  204. X509 *x, size_t chainidx, int *al);
  205. int tls_construct_stoc_early_data(SSL *s, WPACKET *pkt, unsigned int context,
  206. X509 *x, size_t chainidx, int *al);
  207. #ifndef OPENSSL_NO_EC
  208. int tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt, unsigned int context,
  209. X509 *x, size_t chainidx, int *al);
  210. #endif
  211. int tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt,
  212. unsigned int context, X509 *x,
  213. size_t chainidx, int *al);
  214. #ifndef OPENSSL_NO_OCSP
  215. int tls_construct_stoc_status_request(SSL *s, WPACKET *pkt,
  216. unsigned int context, X509 *x,
  217. size_t chainidx, int *al);
  218. #endif
  219. #ifndef OPENSSL_NO_NEXTPROTONEG
  220. int tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt,
  221. unsigned int context, X509 *x,
  222. size_t chainidx, int *al);
  223. #endif
  224. int tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  225. size_t chainidx, int *al);
  226. #ifndef OPENSSL_NO_SRTP
  227. int tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt, unsigned int context,
  228. X509 *x, size_t chainidx, int *al);
  229. #endif
  230. int tls_construct_stoc_etm(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  231. size_t chainidx, int *al);
  232. int tls_construct_stoc_ems(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  233. size_t chainidx, int *al);
  234. int tls_construct_stoc_key_share(SSL *s, WPACKET *pkt, unsigned int context,
  235. X509 *x, size_t chainidx, int *al);
  236. /*
  237. * Not in public headers as this is not an official extension. Only used when
  238. * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set.
  239. */
  240. #define TLSEXT_TYPE_cryptopro_bug 0xfde8
  241. int tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt, unsigned int context,
  242. X509 *x, size_t chainidx, int *al);
  243. int tls_construct_stoc_psk(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  244. size_t chainidx, int *al);
  245. /* Client Extension processing */
  246. int tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt, unsigned int context,
  247. X509 *x, size_t chainidx, int *al);
  248. int tls_construct_ctos_server_name(SSL *s, WPACKET *pkt, unsigned int context,
  249. X509 *x, size_t chainidx, int *al);
  250. #ifndef OPENSSL_NO_SRP
  251. int tls_construct_ctos_srp(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  252. size_t chainidx, int *al);
  253. #endif
  254. #ifndef OPENSSL_NO_EC
  255. int tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt, unsigned int context,
  256. X509 *x, size_t chainidx, int *al);
  257. int tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt,
  258. unsigned int context, X509 *x,
  259. size_t chainidx, int *al);
  260. #endif
  261. int tls_construct_ctos_early_data(SSL *s, WPACKET *pkt, unsigned int context,
  262. X509 *x, size_t chainidx, int *al);
  263. int tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt,
  264. unsigned int context, X509 *x,
  265. size_t chainidx, int *al);
  266. int tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt, unsigned int context,
  267. X509 *x, size_t chainidx, int *al);
  268. #ifndef OPENSSL_NO_OCSP
  269. int tls_construct_ctos_status_request(SSL *s, WPACKET *pkt,
  270. unsigned int context, X509 *x,
  271. size_t chainidx, int *al);
  272. #endif
  273. #ifndef OPENSSL_NO_NEXTPROTONEG
  274. int tls_construct_ctos_npn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  275. size_t chainidx, int *al);
  276. #endif
  277. int tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  278. size_t chainidx, int *al);
  279. #ifndef OPENSSL_NO_SRTP
  280. int tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt, unsigned int context,
  281. X509 *x, size_t chainidx, int *al);
  282. #endif
  283. int tls_construct_ctos_etm(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  284. size_t chainidx, int *al);
  285. #ifndef OPENSSL_NO_CT
  286. int tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  287. size_t chainidx, int *al);
  288. #endif
  289. int tls_construct_ctos_ems(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  290. size_t chainidx, int *al);
  291. int tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt,
  292. unsigned int context, X509 *x,
  293. size_t chainidx, int *al);
  294. int tls_construct_ctos_key_share(SSL *s, WPACKET *pkt, unsigned int context,
  295. X509 *x, size_t chainidx, int *al);
  296. int tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt, unsigned int context,
  297. X509 *x, size_t chainidx, int *al);
  298. int tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, unsigned int context,
  299. X509 *x, size_t chainidx, int *al);
  300. int tls_construct_ctos_padding(SSL *s, WPACKET *pkt, unsigned int context,
  301. X509 *x, size_t chainidx, int *al);
  302. int tls_construct_ctos_psk(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  303. size_t chainidx, int *al);
  304. int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
  305. X509 *x, size_t chainidx, int *al);
  306. int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context,
  307. X509 *x, size_t chainidx, int *al);
  308. int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context,
  309. X509 *x, size_t chainidx, int *al);
  310. #ifndef OPENSSL_NO_EC
  311. int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
  312. X509 *x, size_t chainidx, int *al);
  313. #endif
  314. int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
  315. X509 *x, size_t chainidx, int *al);
  316. #ifndef OPENSSL_NO_OCSP
  317. int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context,
  318. X509 *x, size_t chainidx, int *al);
  319. #endif
  320. #ifndef OPENSSL_NO_CT
  321. int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  322. size_t chainidx, int *al);
  323. #endif
  324. #ifndef OPENSSL_NO_NEXTPROTONEG
  325. int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  326. size_t chainidx, int *al);
  327. #endif
  328. int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  329. size_t chainidx, int *al);
  330. #ifndef OPENSSL_NO_SRTP
  331. int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  332. size_t chainidx, int *al);
  333. #endif
  334. int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  335. size_t chainidx, int *al);
  336. int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  337. size_t chainidx, int *al);
  338. int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  339. size_t chainidx, int *al);
  340. int tls_parse_stoc_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  341. size_t chainidx, int *al);
  342. int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  343. size_t chainidx, int *al);