tls1.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2007, 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 "os_int.h"
  42. #include "crypto.h"
  43. #include "crypto_misc.h"
  44. #include "config.h"
  45. #define SSL_PROTOCOL_MIN_VERSION 0x31 /* TLS v1.0 */
  46. #define SSL_PROTOCOL_MINOR_VERSION 0x02 /* TLS v1.1 */
  47. #define SSL_PROTOCOL_VERSION_MAX 0x32 /* TLS v1.1 */
  48. #define SSL_PROTOCOL_VERSION1_1 0x32 /* TLS v1.1 */
  49. #define SSL_RANDOM_SIZE 32
  50. #define SSL_SECRET_SIZE 48
  51. #define SSL_FINISHED_HASH_SIZE 12
  52. #define SSL_RECORD_SIZE 5
  53. #define SSL_SERVER_READ 0
  54. #define SSL_SERVER_WRITE 1
  55. #define SSL_CLIENT_READ 2
  56. #define SSL_CLIENT_WRITE 3
  57. #define SSL_HS_HDR_SIZE 4
  58. /* the flags we use while establishing a connection */
  59. #define SSL_NEED_RECORD 0x0001
  60. #define SSL_TX_ENCRYPTED 0x0002
  61. #define SSL_RX_ENCRYPTED 0x0004
  62. #define SSL_SESSION_RESUME 0x0008
  63. #define SSL_IS_CLIENT 0x0010
  64. #define SSL_HAS_CERT_REQ 0x0020
  65. #define SSL_SENT_CLOSE_NOTIFY 0x0040
  66. /* some macros to muck around with flag bits */
  67. #define SET_SSL_FLAG(A) (ssl->flag |= A)
  68. #define CLR_SSL_FLAG(A) (ssl->flag &= ~A)
  69. #define IS_SET_SSL_FLAG(A) (ssl->flag & A)
  70. #define MAX_KEY_BYTE_SIZE 512 /* for a 4096 bit key */
  71. #define RT_MAX_PLAIN_LENGTH 2048//16384
  72. #define RT_EXTRA 512//1024
  73. #define BM_RECORD_OFFSET 5
  74. #define BM_ALL_DATA_SIZE (RT_MAX_PLAIN_LENGTH+RT_EXTRA-BM_RECORD_OFFSET)
  75. #ifdef CONFIG_SSL_SKELETON_MODE
  76. #define NUM_PROTOCOLS 1
  77. #else
  78. #define NUM_PROTOCOLS 4
  79. #endif
  80. #define PARANOIA_CHECK(A, B) if (A < B) { \
  81. ret = SSL_ERROR_INVALID_HANDSHAKE; goto error; }
  82. /* protocol types */
  83. enum
  84. {
  85. PT_CHANGE_CIPHER_SPEC = 20,
  86. PT_ALERT_PROTOCOL,
  87. PT_HANDSHAKE_PROTOCOL,
  88. PT_APP_PROTOCOL_DATA
  89. };
  90. /* handshaking types */
  91. enum
  92. {
  93. HS_HELLO_REQUEST,
  94. HS_CLIENT_HELLO,
  95. HS_SERVER_HELLO,
  96. HS_CERTIFICATE = 11,
  97. HS_SERVER_KEY_XCHG,
  98. HS_CERT_REQ,
  99. HS_SERVER_HELLO_DONE,
  100. HS_CERT_VERIFY,
  101. HS_CLIENT_KEY_XCHG,
  102. HS_FINISHED = 20
  103. };
  104. typedef struct
  105. {
  106. uint8_t cipher;
  107. uint8_t key_size;
  108. uint8_t iv_size;
  109. uint8_t key_block_size;
  110. uint8_t padding_size;
  111. uint8_t digest_size;
  112. hmac_func hmac;
  113. crypt_func encrypt;
  114. crypt_func decrypt;
  115. } cipher_info_t;
  116. struct _SSLObjLoader
  117. {
  118. uint8_t *buf;
  119. int len;
  120. };
  121. typedef struct _SSLObjLoader SSLObjLoader;
  122. typedef struct
  123. {
  124. time_t conn_time;
  125. uint8_t session_id[SSL_SESSION_ID_SIZE];
  126. uint8_t master_secret[SSL_SECRET_SIZE];
  127. } SSL_SESSION;
  128. typedef struct
  129. {
  130. uint8_t *buf;
  131. int size;
  132. } SSL_CERT;
  133. typedef struct
  134. {
  135. MD5_CTX md5_ctx;
  136. SHA1_CTX sha1_ctx;
  137. uint8_t final_finish_mac[SSL_FINISHED_HASH_SIZE];
  138. uint8_t key_block[MAX_KEYBLOCK_SIZE];
  139. uint8_t master_secret[SSL_SECRET_SIZE];
  140. uint8_t client_random[SSL_RANDOM_SIZE]; /* client's random sequence */
  141. uint8_t server_random[SSL_RANDOM_SIZE]; /* server's random sequence */
  142. uint16_t bm_proc_index;
  143. } DISPOSABLE_CTX;
  144. struct _SSL
  145. {
  146. uint32_t flag;
  147. uint16_t need_bytes;
  148. uint16_t got_bytes;
  149. uint8_t record_type;
  150. uint8_t cipher;
  151. uint8_t sess_id_size;
  152. uint8_t version;
  153. uint8_t client_version;
  154. int16_t next_state;
  155. int16_t hs_status;
  156. DISPOSABLE_CTX *dc; /* temporary data which we'll get rid of soon */
  157. int client_fd;
  158. void *connection;
  159. const cipher_info_t *cipher_info;
  160. void *encrypt_ctx;
  161. void *decrypt_ctx;
  162. uint8_t bm_all_data[RT_MAX_PLAIN_LENGTH];
  163. uint8_t *bm_data;
  164. uint16_t bm_index;
  165. uint16_t bm_read_index;
  166. struct _SSL *next; /* doubly linked list */
  167. struct _SSL *prev;
  168. struct _SSL_CTX *ssl_ctx; /* back reference to a clnt/svr ctx */
  169. #ifndef CONFIG_SSL_SKELETON_MODE
  170. uint16_t session_index;
  171. SSL_SESSION *session;
  172. #endif
  173. #ifdef CONFIG_SSL_CERT_VERIFICATION
  174. X509_CTX *x509_ctx;
  175. #endif
  176. uint8_t session_id[SSL_SESSION_ID_SIZE];
  177. uint8_t client_mac[SHA1_SIZE]; /* for HMAC verification */
  178. uint8_t server_mac[SHA1_SIZE]; /* for HMAC verification */
  179. uint8_t read_sequence[8]; /* 64 bit sequence number */
  180. uint8_t write_sequence[8]; /* 64 bit sequence number */
  181. uint8_t hmac_header[SSL_RECORD_SIZE]; /* rx hmac */
  182. };
  183. typedef struct _SSL SSL;
  184. struct _SSL_CTX
  185. {
  186. uint32_t options;
  187. uint8_t chain_length;
  188. RSA_CTX *rsa_ctx;
  189. #ifdef CONFIG_SSL_CERT_VERIFICATION
  190. CA_CERT_CTX *ca_cert_ctx;
  191. #endif
  192. SSL *head;
  193. SSL *tail;
  194. SSL_CERT certs[CONFIG_SSL_MAX_CERTS];
  195. #ifndef CONFIG_SSL_SKELETON_MODE
  196. uint16_t num_sessions;
  197. SSL_SESSION **ssl_sessions;
  198. #endif
  199. #ifdef CONFIG_SSL_CTX_MUTEXING
  200. SSL_CTX_MUTEX_TYPE mutex;
  201. #endif
  202. #ifdef CONFIG_OPENSSL_COMPATIBLE
  203. void *bonus_attr;
  204. #endif
  205. };
  206. typedef struct _SSL_CTX SSL_CTX;
  207. /* backwards compatibility */
  208. typedef struct _SSL_CTX SSLCTX;
  209. extern const uint8_t ssl_prot_prefs[NUM_PROTOCOLS];
  210. SSL *ssl_new(SSL *ssl, int client_fd);
  211. void disposable_new(SSL *ssl);
  212. void disposable_free(SSL *ssl);
  213. int send_packet(SSL *ssl, uint8_t protocol,
  214. const uint8_t *in, int length);
  215. int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
  216. int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
  217. int process_finished(SSL *ssl, uint8_t *buf, int hs_len);
  218. int process_sslv23_client_hello(SSL *ssl);
  219. int send_alert(SSL *ssl, int error_code);
  220. int send_finished(SSL *ssl);
  221. int send_certificate(SSL *ssl);
  222. int basic_read2(SSL *ssl, uint8_t *data, uint32_t length);
  223. int read_record(SSL *ssl);
  224. int basic_decrypt(SSL *ssl, uint8_t *buf, int len);
  225. int process_data(SSL* ssl, uint8_t *in_data, int len);
  226. int ssl_read(SSL *ssl, uint8_t *in_data, int len);
  227. int send_change_cipher_spec(SSL *ssl);
  228. void finished_digest(SSL *ssl, const char *label, uint8_t *digest);
  229. void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret);
  230. void add_packet(SSL *ssl, const uint8_t *pkt, int len);
  231. int add_cert(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
  232. int add_private_key(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj);
  233. void ssl_obj_free(SSLObjLoader *ssl_obj);
  234. int pkcs8_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
  235. int pkcs12_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
  236. int load_key_certs(SSL_CTX *ssl_ctx);
  237. #ifdef CONFIG_SSL_CERT_VERIFICATION
  238. int add_cert_auth(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
  239. void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx);
  240. #endif
  241. #ifdef CONFIG_SSL_ENABLE_CLIENT
  242. int do_client_connect(SSL *ssl);
  243. #endif
  244. #ifdef CONFIG_SSL_FULL_MODE
  245. void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok);
  246. void DISPLAY_BYTES(SSL *ssl, const char *format,
  247. const uint8_t *data, int size, ...);
  248. void DISPLAY_CERT(SSL *ssl, const X509_CTX *x509_ctx);
  249. void DISPLAY_RSA(SSL *ssl, const RSA_CTX *rsa_ctx);
  250. void DISPLAY_ALERT(SSL *ssl, int alert);
  251. #else
  252. #define DISPLAY_STATE(A,B,C,D)
  253. #define DISPLAY_CERT(A,B)
  254. #define DISPLAY_RSA(A,B)
  255. #define DISPLAY_ALERT(A, B)
  256. #ifdef WIN32
  257. void DISPLAY_BYTES(SSL *ssl, const char *format,/* win32 has no variadic macros */
  258. const uint8_t *data, int size, ...);
  259. #else
  260. #define DISPLAY_BYTES(A,B,C,D,...)
  261. #endif
  262. #endif
  263. #ifdef CONFIG_SSL_CERT_VERIFICATION
  264. int process_certificate(SSL *ssl, X509_CTX **x509_ctx);
  265. #endif
  266. SSL_SESSION *ssl_session_update(int max_sessions,
  267. SSL_SESSION *ssl_sessions[], SSL *ssl,
  268. const uint8_t *session_id);
  269. void kill_ssl_session(SSL_SESSION **ssl_sessions, SSL *ssl);
  270. #ifdef __cplusplus
  271. }
  272. #endif
  273. #endif