tls1.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 <cfg/arch.h>
  41. #include <cfg/tls.h>
  42. #include <tls/version.h>
  43. #include <crypto/crypto.h>
  44. #include <tls/tls_misc.h>
  45. #include <time.h>
  46. #define SSL_PROTOCOL_MIN_VERSION 0x31 /* TLS v1.0 */
  47. #define SSL_PROTOCOL_MINOR_VERSION 0x02 /* TLS v1.1 */
  48. #define SSL_PROTOCOL_VERSION_MAX 0x32 /* TLS v1.1 */
  49. #define SSL_PROTOCOL_VERSION1_1 0x32 /* TLS v1.1 */
  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 /* We want to save some memory, max. SSL record length: 16384 */
  73. #define RT_EXTRA 1024 /* original RT_EXTRA: 1024 */
  74. #define BM_RECORD_OFFSET 5
  75. #define NUM_PROTOCOLS 4
  76. #define PARANOIA_CHECK(A, B) if (A < B) { \
  77. ret = SSL_ERROR_INVALID_HANDSHAKE; goto error; }
  78. /* protocol types */
  79. enum
  80. {
  81. PT_CHANGE_CIPHER_SPEC = 20,
  82. PT_ALERT_PROTOCOL,
  83. PT_HANDSHAKE_PROTOCOL,
  84. PT_APP_PROTOCOL_DATA
  85. };
  86. /* handshaking types */
  87. enum
  88. {
  89. HS_HELLO_REQUEST,
  90. HS_CLIENT_HELLO,
  91. HS_SERVER_HELLO,
  92. HS_CERTIFICATE = 11,
  93. HS_SERVER_KEY_XCHG,
  94. HS_CERT_REQ,
  95. HS_SERVER_HELLO_DONE,
  96. HS_CERT_VERIFY,
  97. HS_CLIENT_KEY_XCHG,
  98. HS_FINISHED = 20
  99. };
  100. typedef struct
  101. {
  102. uint8_t cipher;
  103. uint8_t key_size;
  104. uint8_t iv_size;
  105. uint8_t key_block_size;
  106. uint8_t padding_size;
  107. uint8_t digest_size;
  108. hmac_func hmac;
  109. crypt_func encrypt;
  110. crypt_func decrypt;
  111. } cipher_info_t;
  112. struct _SSLObjLoader
  113. {
  114. uint8_t *buf;
  115. int len;
  116. };
  117. typedef struct _SSLObjLoader SSLObjLoader;
  118. typedef struct
  119. {
  120. time_t conn_time;
  121. uint8_t session_id[SSL_SESSION_ID_SIZE];
  122. uint8_t master_secret[SSL_SECRET_SIZE];
  123. } SSL_SESSION;
  124. typedef struct
  125. {
  126. uint8_t *buf;
  127. int size;
  128. } SSL_CERT;
  129. typedef struct
  130. {
  131. MD5_CTX md5_ctx;
  132. SHA1_CTX sha1_ctx;
  133. uint8_t final_finish_mac[SSL_FINISHED_HASH_SIZE];
  134. uint8_t *key_block;
  135. uint8_t master_secret[SSL_SECRET_SIZE];
  136. uint8_t client_random[SSL_RANDOM_SIZE]; /* client's random sequence */
  137. uint8_t server_random[SSL_RANDOM_SIZE]; /* server's random sequence */
  138. uint16_t bm_proc_index;
  139. } DISPOSABLE_CTX;
  140. struct _SSL
  141. {
  142. uint32_t flag;
  143. uint16_t need_bytes;
  144. uint16_t got_bytes;
  145. uint8_t record_type;
  146. uint8_t cipher;
  147. uint8_t sess_id_size;
  148. uint8_t version;
  149. uint8_t client_version;
  150. int16_t next_state;
  151. int16_t hs_status;
  152. DISPOSABLE_CTX *dc; /* temporary data which we'll get rid of soon */
  153. int client_fd;
  154. const cipher_info_t *cipher_info;
  155. void *encrypt_ctx;
  156. void *decrypt_ctx;
  157. uint8_t bm_all_data[RT_MAX_PLAIN_LENGTH+RT_EXTRA];
  158. uint8_t *bm_data;
  159. uint16_t bm_index;
  160. uint16_t bm_read_index;
  161. struct _SSL *next; /* doubly linked list */
  162. struct _SSL *prev;
  163. struct _SSL_CTX *ssl_ctx; /* back reference to a clnt/svr ctx */
  164. uint16_t session_index;
  165. SSL_SESSION *session;
  166. #ifdef TLS_SSL_CERT_VERIFICATION
  167. X509_CTX *x509_ctx;
  168. #endif
  169. uint8_t session_id[SSL_SESSION_ID_SIZE];
  170. uint8_t client_mac[SHA1_SIZE]; /* for HMAC verification */
  171. uint8_t server_mac[SHA1_SIZE]; /* for HMAC verification */
  172. uint8_t read_sequence[8]; /* 64 bit sequence number */
  173. uint8_t write_sequence[8]; /* 64 bit sequence number */
  174. uint8_t hmac_header[SSL_RECORD_SIZE]; /* rx hmac */
  175. };
  176. typedef struct _SSL SSL;
  177. struct _SSL_CTX
  178. {
  179. uint32_t options;
  180. uint8_t chain_length;
  181. RSA_CTX *rsa_ctx;
  182. #ifdef TLS_SSL_CERT_VERIFICATION
  183. CA_CERT_CTX *ca_cert_ctx;
  184. #endif
  185. SSL *head;
  186. SSL *tail;
  187. SSL_CERT certs[TLS_SSL_MAX_CERTS];
  188. uint16_t num_sessions;
  189. SSL_SESSION **ssl_sessions;
  190. #ifdef TLS_SSL_CTX_MUTEXING
  191. SSL_CTX_MUTEX_TYPE mutex;
  192. #endif
  193. #ifdef TLS_OPENSSL_COMPATIBLE
  194. void *bonus_attr;
  195. #endif
  196. };
  197. typedef struct _SSL_CTX SSL_CTX;
  198. /* backwards compatibility */
  199. typedef struct _SSL_CTX SSLCTX;
  200. extern const uint8_t ssl_prot_prefs[NUM_PROTOCOLS];
  201. SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd);
  202. void disposable_new(SSL *ssl);
  203. void disposable_free(SSL *ssl);
  204. int send_packet(SSL *ssl, uint8_t protocol,
  205. const uint8_t *in, int length);
  206. int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
  207. int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
  208. int process_finished(SSL *ssl, uint8_t *buf, int hs_len);
  209. int process_sslv23_client_hello(SSL *ssl);
  210. int send_alert(SSL *ssl, int error_code);
  211. int send_finished(SSL *ssl);
  212. int send_certificate(SSL *ssl);
  213. int basic_read(SSL *ssl, uint8_t **in_data);
  214. int send_change_cipher_spec(SSL *ssl);
  215. void finished_digest(SSL *ssl, const char *label, uint8_t *digest);
  216. void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret);
  217. void add_packet(SSL *ssl, const uint8_t *pkt, int len);
  218. int add_cert(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
  219. int add_private_key(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj);
  220. void ssl_obj_free(SSLObjLoader *ssl_obj);
  221. int pkcs8_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
  222. int pkcs12_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
  223. int load_key_certs(SSL_CTX *ssl_ctx);
  224. #ifdef TLS_SSL_CERT_VERIFICATION
  225. int add_cert_auth(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
  226. void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx);
  227. #endif
  228. #ifdef TLS_SSL_ENABLE_CLIENT
  229. int do_client_connect(SSL *ssl);
  230. #endif
  231. #ifdef TLS_SSL_FULL_MODE
  232. void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok);
  233. void DISPLAY_BYTES(SSL *ssl, const char *format,
  234. const uint8_t *data, int size, ...);
  235. void DISPLAY_CERT(SSL *ssl, const X509_CTX *x509_ctx);
  236. void DISPLAY_RSA(SSL *ssl, const RSA_CTX *rsa_ctx);
  237. void DISPLAY_ALERT(SSL *ssl, int alert);
  238. #else
  239. #define DISPLAY_STATE(A,B,C,D)
  240. #define DISPLAY_CERT(A,B)
  241. #define DISPLAY_RSA(A,B)
  242. #define DISPLAY_ALERT(A, B)
  243. #define DISPLAY_BYTES(A,B,C,D,...)
  244. #endif
  245. #ifdef TLS_SSL_CERT_VERIFICATION
  246. int process_certificate(SSL *ssl, X509_CTX **x509_ctx);
  247. #endif
  248. SSL_SESSION *ssl_session_update(int max_sessions,
  249. SSL_SESSION *ssl_sessions[], SSL *ssl,
  250. const uint8_t *session_id);
  251. void kill_ssl_session(SSL_SESSION **ssl_sessions, SSL *ssl);
  252. #ifdef __cplusplus
  253. }
  254. #endif
  255. #endif