tls1.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (c) 2007-2016, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @file tls1.h
  32. *
  33. * @brief The definitions for the TLS library.
  34. */
  35. #ifndef HEADER_SSL_LIB_H
  36. #define HEADER_SSL_LIB_H
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #include "version.h"
  41. #include "config.h"
  42. #include "os_int.h"
  43. #include "os_port.h"
  44. #include "crypto.h"
  45. #include "crypto_misc.h"
  46. #define SSL_PROTOCOL_MIN_VERSION 0x31 /* TLS v1.0 */
  47. #define SSL_PROTOCOL_VERSION_MAX 0x33 /* TLS v1.3 */
  48. #define SSL_PROTOCOL_VERSION_TLS1_1 0x32 /* TLS v1.1 */
  49. #define SSL_PROTOCOL_VERSION_TLS1_2 0x33 /* TLS v1.2 */
  50. #define SSL_RANDOM_SIZE 32
  51. #define SSL_SECRET_SIZE 48
  52. #define SSL_FINISHED_HASH_SIZE 12
  53. #define SSL_RECORD_SIZE 5
  54. #define SSL_SERVER_READ 0
  55. #define SSL_SERVER_WRITE 1
  56. #define SSL_CLIENT_READ 2
  57. #define SSL_CLIENT_WRITE 3
  58. #define SSL_HS_HDR_SIZE 4
  59. /* the flags we use while establishing a connection */
  60. #define SSL_NEED_RECORD 0x0001
  61. #define SSL_TX_ENCRYPTED 0x0002
  62. #define SSL_RX_ENCRYPTED 0x0004
  63. #define SSL_SESSION_RESUME 0x0008
  64. #define SSL_IS_CLIENT 0x0010
  65. #define SSL_HAS_CERT_REQ 0x0020
  66. #define SSL_SENT_CLOSE_NOTIFY 0x0040
  67. /* some macros to muck around with flag bits */
  68. #define SET_SSL_FLAG(A) (ssl->flag |= A)
  69. #define CLR_SSL_FLAG(A) (ssl->flag &= ~A)
  70. #define IS_SET_SSL_FLAG(A) (ssl->flag & A)
  71. #define MAX_KEY_BYTE_SIZE 512 /* for a 4096 bit key */
  72. #define RT_MAX_PLAIN_LENGTH 16384
  73. #define RT_EXTRA 1024
  74. #define BM_RECORD_OFFSET 5
  75. #define NUM_PROTOCOLS 4
  76. #define MAX_SIG_ALGORITHMS 4
  77. #define SIG_ALG_SHA1 2
  78. #define SIG_ALG_SHA256 4
  79. #define SIG_ALG_SHA384 5
  80. #define SIG_ALG_SHA512 6
  81. #define SIG_ALG_RSA 1
  82. #define PARANOIA_CHECK(A, B) if (A < B) { \
  83. ret = SSL_ERROR_INVALID_HANDSHAKE; goto error; }
  84. /* protocol types */
  85. enum
  86. {
  87. PT_CHANGE_CIPHER_SPEC = 20,
  88. PT_ALERT_PROTOCOL,
  89. PT_HANDSHAKE_PROTOCOL,
  90. PT_APP_PROTOCOL_DATA
  91. };
  92. /* handshaking types */
  93. enum
  94. {
  95. HS_HELLO_REQUEST,
  96. HS_CLIENT_HELLO,
  97. HS_SERVER_HELLO,
  98. HS_CERTIFICATE = 11,
  99. HS_SERVER_KEY_XCHG,
  100. HS_CERT_REQ,
  101. HS_SERVER_HELLO_DONE,
  102. HS_CERT_VERIFY,
  103. HS_CLIENT_KEY_XCHG,
  104. HS_FINISHED = 20
  105. };
  106. /* SSL extension types */
  107. enum
  108. {
  109. SSL_EXT_SERVER_NAME = 0,
  110. SSL_EXT_MAX_FRAGMENT_SIZE,
  111. SSL_EXT_SIG_ALG = 0x0d,
  112. };
  113. typedef struct
  114. {
  115. uint8_t cipher;
  116. uint8_t key_size;
  117. uint8_t iv_size;
  118. uint8_t padding_size;
  119. uint8_t digest_size;
  120. uint8_t key_block_size;
  121. hmac_func hmac;
  122. crypt_func encrypt;
  123. crypt_func decrypt;
  124. } cipher_info_t;
  125. struct _SSLObjLoader
  126. {
  127. uint8_t *buf;
  128. int len;
  129. };
  130. typedef struct _SSLObjLoader SSLObjLoader;
  131. typedef struct
  132. {
  133. time_t conn_time;
  134. uint8_t session_id[SSL_SESSION_ID_SIZE];
  135. uint8_t master_secret[SSL_SECRET_SIZE];
  136. } SSL_SESSION;
  137. typedef struct
  138. {
  139. uint8_t *buf;
  140. int size;
  141. uint8_t hash_alg;
  142. } SSL_CERT;
  143. typedef struct
  144. {
  145. MD5_CTX md5_ctx;
  146. SHA1_CTX sha1_ctx;
  147. SHA256_CTX sha256_ctx;
  148. uint8_t client_random[SSL_RANDOM_SIZE]; /* client's random sequence */
  149. uint8_t server_random[SSL_RANDOM_SIZE]; /* server's random sequence */
  150. uint8_t final_finish_mac[128];
  151. uint8_t master_secret[SSL_SECRET_SIZE];
  152. uint8_t key_block[256];
  153. uint16_t bm_proc_index;
  154. uint8_t key_block_generated;
  155. } DISPOSABLE_CTX;
  156. typedef struct
  157. {
  158. const char *host_name; /* Needed for the SNI support */
  159. /* Needed for the Max Fragment Size Extension.
  160. Allowed values: 2^9, 2^10 .. 2^14 */
  161. uint16_t max_fragment_size;
  162. } SSL_EXTENSIONS;
  163. struct _SSL
  164. {
  165. uint32_t flag;
  166. uint16_t need_bytes;
  167. uint16_t got_bytes;
  168. uint8_t record_type;
  169. uint8_t cipher;
  170. uint8_t sess_id_size;
  171. uint8_t version;
  172. uint8_t client_version;
  173. int16_t next_state;
  174. int16_t hs_status;
  175. DISPOSABLE_CTX *dc; /* temporary data which we'll get rid of soon */
  176. int client_fd;
  177. const cipher_info_t *cipher_info;
  178. void *encrypt_ctx;
  179. void *decrypt_ctx;
  180. uint8_t bm_all_data[RT_MAX_PLAIN_LENGTH+RT_EXTRA];
  181. uint8_t *bm_data;
  182. /*read buffer*/
  183. uint8_t bm_read_data[RT_MAX_PLAIN_LENGTH+RT_EXTRA];
  184. uint16_t read_data_in_buffer_bytes;
  185. uint16_t read_data_in_buffer_start;
  186. uint16_t bm_index;
  187. uint16_t bm_read_index;
  188. uint8_t sig_algs[MAX_SIG_ALGORITHMS];
  189. uint8_t num_sig_algs;
  190. struct _SSL *next; /* doubly linked list */
  191. struct _SSL *prev;
  192. struct _SSL_CTX *ssl_ctx; /* back reference to a clnt/svr ctx */
  193. #ifndef CONFIG_SSL_SKELETON_MODE
  194. uint16_t session_index;
  195. SSL_SESSION *session;
  196. #endif
  197. #ifdef CONFIG_SSL_CERT_VERIFICATION
  198. X509_CTX *x509_ctx;
  199. #endif
  200. uint8_t session_id[SSL_SESSION_ID_SIZE];
  201. uint8_t client_mac[SHA256_SIZE]; /* for HMAC verification */
  202. uint8_t server_mac[SHA256_SIZE]; /* for HMAC verification */
  203. uint8_t read_sequence[8]; /* 64 bit sequence number */
  204. uint8_t write_sequence[8]; /* 64 bit sequence number */
  205. uint8_t hmac_header[SSL_RECORD_SIZE]; /* rx hmac */
  206. SSL_EXTENSIONS *extensions; /* Contains the SSL (client) extensions */
  207. };
  208. typedef struct _SSL SSL;
  209. struct _SSL_CTX
  210. {
  211. uint32_t options;
  212. uint8_t chain_length;
  213. RSA_CTX *rsa_ctx;
  214. #ifdef CONFIG_SSL_CERT_VERIFICATION
  215. CA_CERT_CTX *ca_cert_ctx;
  216. #endif
  217. SSL *head;
  218. SSL *tail;
  219. SSL_CERT certs[CONFIG_SSL_MAX_CERTS];
  220. #ifndef CONFIG_SSL_SKELETON_MODE
  221. uint16_t num_sessions;
  222. SSL_SESSION **ssl_sessions;
  223. #endif
  224. #ifdef CONFIG_SSL_CTX_MUTEXING
  225. SSL_CTX_MUTEX_TYPE mutex;
  226. #endif
  227. #ifdef CONFIG_OPENSSL_COMPATIBLE
  228. void *bonus_attr;
  229. #endif
  230. };
  231. typedef struct _SSL_CTX SSL_CTX;
  232. /* backwards compatibility */
  233. typedef struct _SSL_CTX SSLCTX;
  234. extern const uint8_t ssl_prot_prefs[NUM_PROTOCOLS];
  235. SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd);
  236. void disposable_new(SSL *ssl);
  237. void disposable_free(SSL *ssl);
  238. int send_packet(SSL *ssl, uint8_t protocol,
  239. const uint8_t *in, int length);
  240. int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
  241. int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
  242. int process_finished(SSL *ssl, uint8_t *buf, int hs_len);
  243. int process_sslv23_client_hello(SSL *ssl);
  244. int send_alert(SSL *ssl, int error_code);
  245. int send_finished(SSL *ssl);
  246. int send_certificate(SSL *ssl);
  247. int basic_read(SSL *ssl, uint8_t **in_data, int in_num);
  248. int send_change_cipher_spec(SSL *ssl);
  249. int finished_digest(SSL *ssl, const char *label, uint8_t *digest);
  250. void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret);
  251. void add_packet(SSL *ssl, const uint8_t *pkt, int len);
  252. int add_cert(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
  253. int add_private_key(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj);
  254. void ssl_obj_free(SSLObjLoader *ssl_obj);
  255. int pkcs8_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
  256. int pkcs12_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
  257. int load_key_certs_with_params(SSL_CTX *ssl_ctx,
  258. const char *ssl_private_key, size_t ssl_private_key_len,
  259. const char *ssl_private_key_password,
  260. const char *ssl_x509_cert, size_t ssl_x509_cert_len);
  261. int load_key_certs(SSL_CTX *ssl_ctx);
  262. #ifdef CONFIG_SSL_CERT_VERIFICATION
  263. int add_cert_auth(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
  264. void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx);
  265. #endif
  266. #ifdef CONFIG_SSL_ENABLE_CLIENT
  267. int do_client_connect(SSL *ssl);
  268. #endif
  269. #ifdef CONFIG_SSL_FULL_MODE
  270. void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok);
  271. void DISPLAY_BYTES(SSL *ssl, const char *format,
  272. const uint8_t *data, int size, ...);
  273. void DISPLAY_CERT(SSL *ssl, const X509_CTX *x509_ctx);
  274. void DISPLAY_RSA(SSL *ssl, const RSA_CTX *rsa_ctx);
  275. void DISPLAY_ALERT(SSL *ssl, int alert);
  276. #else
  277. #define DISPLAY_STATE(A,B,C,D)
  278. #define DISPLAY_CERT(A,B)
  279. #define DISPLAY_RSA(A,B)
  280. #define DISPLAY_ALERT(A, B)
  281. #ifdef WIN32
  282. void DISPLAY_BYTES(SSL *ssl, const char *format,/* win32 has no variadic macros */
  283. const uint8_t *data, int size, ...);
  284. #else
  285. #define DISPLAY_BYTES(A,B,C,D,...)
  286. #endif
  287. #endif
  288. #ifdef CONFIG_SSL_CERT_VERIFICATION
  289. int process_certificate(SSL *ssl, X509_CTX **x509_ctx);
  290. #endif
  291. SSL_SESSION *ssl_session_update(int max_sessions,
  292. SSL_SESSION *ssl_sessions[], SSL *ssl,
  293. const uint8_t *session_id);
  294. void kill_ssl_session(SSL_SESSION **ssl_sessions, SSL *ssl);
  295. #ifdef __cplusplus
  296. }
  297. #endif
  298. #endif