tls-private.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #define TLS_MAX_VERSION L_TLS_V12
  23. #define TLS_MIN_VERSION L_TLS_V10
  24. enum tls_cipher_type {
  25. TLS_CIPHER_STREAM,
  26. TLS_CIPHER_BLOCK,
  27. TLS_CIPHER_AEAD,
  28. };
  29. struct tls_bulk_encryption_algorithm {
  30. enum tls_cipher_type cipher_type;
  31. union {
  32. enum l_cipher_type l_id;
  33. enum l_aead_cipher_type l_aead_id;
  34. };
  35. size_t key_length;
  36. size_t iv_length;
  37. size_t fixed_iv_length;
  38. size_t block_length;
  39. size_t auth_tag_length;
  40. };
  41. /*
  42. * Support the minimum required set of handshake hash types for the
  43. * Certificate Verify digital signature and the Finished PRF seed so we
  44. * don't have to accumulate all of messages full contents until the
  45. * Finished message. If we're sent a hash of a different type (in TLS 1.2+)
  46. * and need to verify we'll give up.
  47. * SHA1 and MD5 are explicitly required by versions < 1.2 and 1.2 requires
  48. * that the Finished hash is the same as used for the PRF so we need to
  49. * keep at least the hashes our supported cipher suites specify for the PRF.
  50. */
  51. enum handshake_hash_type {
  52. HANDSHAKE_HASH_SHA384,
  53. HANDSHAKE_HASH_SHA256,
  54. HANDSHAKE_HASH_MD5,
  55. HANDSHAKE_HASH_SHA1,
  56. __HANDSHAKE_HASH_COUNT,
  57. };
  58. #define HANDSHAKE_HASH_MAX_SIZE 48
  59. struct tls_hash_algorithm {
  60. uint8_t tls_id;
  61. enum handshake_hash_type type;
  62. enum l_checksum_type l_id;
  63. const char *name;
  64. };
  65. extern const struct tls_hash_algorithm tls_handshake_hash_data[];
  66. typedef bool (*tls_get_hash_t)(struct l_tls *tls,
  67. enum handshake_hash_type type,
  68. const uint8_t *data, size_t data_len,
  69. uint8_t *out, size_t *out_len);
  70. struct tls_signature_algorithm {
  71. uint8_t id;
  72. bool (*validate_cert_key_type)(struct l_cert *cert);
  73. ssize_t (*sign)(struct l_tls *tls, uint8_t *out, size_t out_len,
  74. tls_get_hash_t get_hash,
  75. const uint8_t *data, size_t data_len);
  76. bool (*verify)(struct l_tls *tls, const uint8_t *in, size_t in_len,
  77. tls_get_hash_t get_hash,
  78. const uint8_t *data, size_t data_len);
  79. };
  80. struct tls_key_exchange_algorithm {
  81. bool need_ecc;
  82. bool need_ffdh;
  83. bool (*send_server_key_exchange)(struct l_tls *tls);
  84. void (*handle_server_key_exchange)(struct l_tls *tls,
  85. const uint8_t *buf, size_t len);
  86. bool (*send_client_key_exchange)(struct l_tls *tls);
  87. void (*handle_client_key_exchange)(struct l_tls *tls,
  88. const uint8_t *buf, size_t len);
  89. void (*free_params)(struct l_tls *tls);
  90. };
  91. struct tls_mac_algorithm {
  92. uint8_t id;
  93. enum l_checksum_type hmac_type;
  94. size_t mac_length;
  95. };
  96. struct tls_cipher_suite {
  97. uint8_t id[2];
  98. const char *name;
  99. int verify_data_length;
  100. struct tls_bulk_encryption_algorithm *encryption;
  101. struct tls_signature_algorithm *signature;
  102. struct tls_key_exchange_algorithm *key_xchg;
  103. struct tls_mac_algorithm *mac;
  104. enum l_checksum_type prf_hmac;
  105. };
  106. extern struct tls_cipher_suite *tls_cipher_suite_pref[];
  107. struct tls_compression_method {
  108. int id;
  109. const char *name;
  110. };
  111. struct tls_hello_extension {
  112. const char *name;
  113. const char *short_name;
  114. uint16_t id;
  115. ssize_t (*client_write)(struct l_tls *tls, uint8_t *buf, size_t len);
  116. /* Handle a Client Hello extension (on server), can't be NULL */
  117. bool (*client_handle)(struct l_tls *tls,
  118. const uint8_t *buf, size_t len);
  119. /* Handle a Client Hello extension's absence (on server) */
  120. bool (*client_handle_absent)(struct l_tls *tls);
  121. ssize_t (*server_write)(struct l_tls *tls, uint8_t *buf, size_t len);
  122. /* Handle a Server Hello extension (on client) */
  123. bool (*server_handle)(struct l_tls *tls,
  124. const uint8_t *buf, size_t len);
  125. /* Handle a Server Hello extension's absence (on client) */
  126. bool (*server_handle_absent)(struct l_tls *tls);
  127. };
  128. extern const struct tls_hello_extension tls_extensions[];
  129. struct tls_named_group {
  130. const char *name;
  131. uint16_t id;
  132. enum {
  133. TLS_GROUP_TYPE_EC,
  134. TLS_GROUP_TYPE_FF,
  135. } type;
  136. union {
  137. struct {
  138. const uint8_t *prime;
  139. size_t prime_len;
  140. unsigned int generator;
  141. } ff;
  142. };
  143. };
  144. enum tls_handshake_state {
  145. TLS_HANDSHAKE_WAIT_START,
  146. TLS_HANDSHAKE_WAIT_HELLO,
  147. TLS_HANDSHAKE_WAIT_CERTIFICATE,
  148. TLS_HANDSHAKE_WAIT_KEY_EXCHANGE,
  149. TLS_HANDSHAKE_WAIT_HELLO_DONE,
  150. TLS_HANDSHAKE_WAIT_CERTIFICATE_VERIFY,
  151. TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC,
  152. TLS_HANDSHAKE_WAIT_FINISHED,
  153. TLS_HANDSHAKE_DONE,
  154. };
  155. enum tls_content_type {
  156. TLS_CT_CHANGE_CIPHER_SPEC = 20,
  157. TLS_CT_ALERT = 21,
  158. TLS_CT_HANDSHAKE = 22,
  159. TLS_CT_APPLICATION_DATA = 23,
  160. };
  161. enum tls_handshake_type {
  162. TLS_HELLO_REQUEST = 0,
  163. TLS_CLIENT_HELLO = 1,
  164. TLS_SERVER_HELLO = 2,
  165. TLS_CERTIFICATE = 11,
  166. TLS_SERVER_KEY_EXCHANGE = 12,
  167. TLS_CERTIFICATE_REQUEST = 13,
  168. TLS_SERVER_HELLO_DONE = 14,
  169. TLS_CERTIFICATE_VERIFY = 15,
  170. TLS_CLIENT_KEY_EXCHANGE = 16,
  171. TLS_FINISHED = 20,
  172. };
  173. struct l_tls {
  174. bool server;
  175. l_tls_write_cb_t tx, rx;
  176. l_tls_ready_cb_t ready_handle;
  177. l_tls_disconnect_cb_t disconnected;
  178. void *user_data;
  179. l_tls_debug_cb_t debug_handler;
  180. l_tls_destroy_cb_t debug_destroy;
  181. void *debug_data;
  182. char *cert_dump_path;
  183. enum l_tls_version min_version;
  184. enum l_tls_version max_version;
  185. struct l_queue *ca_certs;
  186. struct l_certchain *cert;
  187. struct l_key *priv_key;
  188. size_t priv_key_size;
  189. char **subject_mask;
  190. struct tls_cipher_suite **cipher_suite_pref_list;
  191. bool in_callback;
  192. bool pending_destroy;
  193. /* Record layer */
  194. uint8_t *record_buf;
  195. int record_buf_len;
  196. int record_buf_max_len;
  197. bool record_flush;
  198. uint8_t *message_buf;
  199. int message_buf_len;
  200. int message_buf_max_len;
  201. enum tls_content_type message_content_type;
  202. /* Handshake protocol layer */
  203. enum tls_handshake_state state;
  204. struct l_checksum *handshake_hash[__HANDSHAKE_HASH_COUNT];
  205. uint8_t prev_digest[__HANDSHAKE_HASH_COUNT][HANDSHAKE_HASH_MAX_SIZE];
  206. enum l_tls_version client_version;
  207. enum l_tls_version negotiated_version;
  208. bool cert_requested, cert_sent;
  209. bool peer_authenticated;
  210. struct l_cert *peer_cert;
  211. struct l_key *peer_pubkey;
  212. size_t peer_pubkey_size;
  213. enum handshake_hash_type signature_hash;
  214. const struct tls_hash_algorithm *prf_hmac;
  215. const struct tls_named_group *negotiated_curve;
  216. const struct tls_named_group *negotiated_ff_group;
  217. /* SecurityParameters current and pending */
  218. struct {
  219. struct tls_cipher_suite *cipher_suite;
  220. struct tls_compression_method *compression_method;
  221. uint8_t master_secret[48];
  222. uint8_t client_random[32];
  223. uint8_t server_random[32];
  224. /*
  225. * Max key block size per 6.3 v1.1 is 136 bytes but if we
  226. * allow AES_256_CBC_SHA256 with v1.0 we get 128 per section
  227. * 6.3 v1.2 + two IVs of 32 bytes.
  228. */
  229. uint8_t key_block[192];
  230. void *key_xchg_params;
  231. } pending;
  232. enum tls_cipher_type cipher_type[2];
  233. struct tls_cipher_suite *cipher_suite[2];
  234. union {
  235. struct l_cipher *cipher[2];
  236. struct l_aead_cipher *aead_cipher[2];
  237. };
  238. struct l_checksum *mac[2];
  239. size_t mac_length[2];
  240. size_t block_length[2];
  241. size_t record_iv_length[2];
  242. size_t fixed_iv_length[2];
  243. uint8_t fixed_iv[2][32];
  244. size_t auth_tag_length[2];
  245. uint64_t seq_num[2];
  246. /*
  247. * Some of the key and IV parts of the "current" state are kept
  248. * inside the cipher and mac states in the kernel so we don't
  249. * duplicate them here.
  250. */
  251. bool ready;
  252. };
  253. bool tls10_prf(const void *secret, size_t secret_len,
  254. const char *label,
  255. const void *seed, size_t seed_len,
  256. uint8_t *out, size_t out_len);
  257. bool tls12_prf(enum l_checksum_type type,
  258. const void *secret, size_t secret_len,
  259. const char *label,
  260. const void *seed, size_t seed_len,
  261. uint8_t *out, size_t out_len);
  262. void tls_disconnect(struct l_tls *tls, enum l_tls_alert_desc desc,
  263. enum l_tls_alert_desc local_desc);
  264. void tls_tx_record(struct l_tls *tls, enum tls_content_type type,
  265. const uint8_t *data, size_t len);
  266. bool tls_handle_message(struct l_tls *tls, const uint8_t *message,
  267. int len, enum tls_content_type type, uint16_t version);
  268. #define TLS_HANDSHAKE_HEADER_SIZE 4
  269. void tls_tx_handshake(struct l_tls *tls, int type, uint8_t *buf, size_t length);
  270. bool tls_cipher_suite_is_compatible(struct l_tls *tls,
  271. const struct tls_cipher_suite *suite,
  272. const char **error);
  273. /* Optionally limit allowed cipher suites to a custom set */
  274. bool tls_set_cipher_suites(struct l_tls *tls, const char **suite_list);
  275. void tls_generate_master_secret(struct l_tls *tls,
  276. const uint8_t *pre_master_secret,
  277. int pre_master_secret_len);
  278. const struct tls_named_group *tls_find_group(uint16_t id);
  279. const struct tls_named_group *tls_find_ff_group(const uint8_t *prime,
  280. size_t prime_len,
  281. const uint8_t *generator,
  282. size_t generator_len);
  283. ssize_t tls_write_signature_algorithms(struct l_tls *tls,
  284. uint8_t *buf, size_t len);
  285. ssize_t tls_parse_signature_algorithms(struct l_tls *tls,
  286. const uint8_t *buf, size_t len);
  287. int tls_parse_certificate_list(const void *data, size_t len,
  288. struct l_certchain **out_certchain);
  289. #define TLS_DEBUG(fmt, args...) \
  290. l_util_debug(tls->debug_handler, tls->debug_data, "%s:%i " fmt, \
  291. __func__, __LINE__, ## args)
  292. #define TLS_SET_STATE(new_state) \
  293. do { \
  294. TLS_DEBUG("New state %s", \
  295. tls_handshake_state_to_str(new_state)); \
  296. tls->state = new_state; \
  297. } while (0)
  298. #define TLS_DISCONNECT(desc, local_desc, fmt, args...) \
  299. do { \
  300. TLS_DEBUG("Disconnect desc=%s local-desc=%s reason=" fmt,\
  301. l_tls_alert_to_str(desc), \
  302. l_tls_alert_to_str(local_desc), ## args);\
  303. tls_disconnect(tls, desc, local_desc); \
  304. } while (0)
  305. #define TLS_VER_FMT "1.%i"
  306. #define TLS_VER_ARGS(version) (((version) & 0xff) - 1)
  307. const char *tls_handshake_state_to_str(enum tls_handshake_state state);