You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tls.h 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #ifndef _IPXE_TLS_H
  2. #define _IPXE_TLS_H
  3. /**
  4. * @file
  5. *
  6. * Transport Layer Security Protocol
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <ipxe/refcnt.h>
  11. #include <ipxe/interface.h>
  12. #include <ipxe/process.h>
  13. #include <ipxe/crypto.h>
  14. #include <ipxe/md5.h>
  15. #include <ipxe/sha1.h>
  16. #include <ipxe/sha256.h>
  17. #include <ipxe/x509.h>
  18. #include <ipxe/pending.h>
  19. #include <ipxe/iobuf.h>
  20. #include <ipxe/tables.h>
  21. /** A TLS header */
  22. struct tls_header {
  23. /** Content type
  24. *
  25. * This is a TLS_TYPE_XXX constant
  26. */
  27. uint8_t type;
  28. /** Protocol version
  29. *
  30. * This is a TLS_VERSION_XXX constant
  31. */
  32. uint16_t version;
  33. /** Length of payload */
  34. uint16_t length;
  35. } __attribute__ (( packed ));
  36. /** TLS version 1.0 */
  37. #define TLS_VERSION_TLS_1_0 0x0301
  38. /** TLS version 1.1 */
  39. #define TLS_VERSION_TLS_1_1 0x0302
  40. /** TLS version 1.2 */
  41. #define TLS_VERSION_TLS_1_2 0x0303
  42. /** Change cipher content type */
  43. #define TLS_TYPE_CHANGE_CIPHER 20
  44. /** Alert content type */
  45. #define TLS_TYPE_ALERT 21
  46. /** Handshake content type */
  47. #define TLS_TYPE_HANDSHAKE 22
  48. /** Application data content type */
  49. #define TLS_TYPE_DATA 23
  50. /* Handshake message types */
  51. #define TLS_HELLO_REQUEST 0
  52. #define TLS_CLIENT_HELLO 1
  53. #define TLS_SERVER_HELLO 2
  54. #define TLS_NEW_SESSION_TICKET 4
  55. #define TLS_CERTIFICATE 11
  56. #define TLS_SERVER_KEY_EXCHANGE 12
  57. #define TLS_CERTIFICATE_REQUEST 13
  58. #define TLS_SERVER_HELLO_DONE 14
  59. #define TLS_CERTIFICATE_VERIFY 15
  60. #define TLS_CLIENT_KEY_EXCHANGE 16
  61. #define TLS_FINISHED 20
  62. /* TLS alert levels */
  63. #define TLS_ALERT_WARNING 1
  64. #define TLS_ALERT_FATAL 2
  65. /* TLS cipher specifications */
  66. #define TLS_RSA_WITH_NULL_MD5 0x0001
  67. #define TLS_RSA_WITH_NULL_SHA 0x0002
  68. #define TLS_RSA_WITH_AES_128_CBC_SHA 0x002f
  69. #define TLS_RSA_WITH_AES_256_CBC_SHA 0x0035
  70. #define TLS_RSA_WITH_AES_128_CBC_SHA256 0x003c
  71. #define TLS_RSA_WITH_AES_256_CBC_SHA256 0x003d
  72. /* TLS hash algorithm identifiers */
  73. #define TLS_MD5_ALGORITHM 1
  74. #define TLS_SHA1_ALGORITHM 2
  75. #define TLS_SHA224_ALGORITHM 3
  76. #define TLS_SHA256_ALGORITHM 4
  77. #define TLS_SHA384_ALGORITHM 5
  78. #define TLS_SHA512_ALGORITHM 6
  79. /* TLS signature algorithm identifiers */
  80. #define TLS_RSA_ALGORITHM 1
  81. /* TLS server name extension */
  82. #define TLS_SERVER_NAME 0
  83. #define TLS_SERVER_NAME_HOST_NAME 0
  84. /* TLS maximum fragment length extension */
  85. #define TLS_MAX_FRAGMENT_LENGTH 1
  86. #define TLS_MAX_FRAGMENT_LENGTH_512 1
  87. #define TLS_MAX_FRAGMENT_LENGTH_1024 2
  88. #define TLS_MAX_FRAGMENT_LENGTH_2048 3
  89. #define TLS_MAX_FRAGMENT_LENGTH_4096 4
  90. /* TLS signature algorithms extension */
  91. #define TLS_SIGNATURE_ALGORITHMS 13
  92. /* TLS session ticket extension */
  93. #define TLS_SESSION_TICKET 35
  94. /* TLS renegotiation information extension */
  95. #define TLS_RENEGOTIATION_INFO 0xff01
  96. /** TLS verification data */
  97. struct tls_verify_data {
  98. /** Client verification data */
  99. uint8_t client[12];
  100. /** Server verification data */
  101. uint8_t server[12];
  102. } __attribute__ (( packed ));
  103. /** TLS RX state machine state */
  104. enum tls_rx_state {
  105. TLS_RX_HEADER = 0,
  106. TLS_RX_DATA,
  107. };
  108. /** TLS TX pending flags */
  109. enum tls_tx_pending {
  110. TLS_TX_CLIENT_HELLO = 0x0001,
  111. TLS_TX_CERTIFICATE = 0x0002,
  112. TLS_TX_CLIENT_KEY_EXCHANGE = 0x0004,
  113. TLS_TX_CERTIFICATE_VERIFY = 0x0008,
  114. TLS_TX_CHANGE_CIPHER = 0x0010,
  115. TLS_TX_FINISHED = 0x0020,
  116. };
  117. /** A TLS cipher suite */
  118. struct tls_cipher_suite {
  119. /** Public-key encryption algorithm */
  120. struct pubkey_algorithm *pubkey;
  121. /** Bulk encryption cipher algorithm */
  122. struct cipher_algorithm *cipher;
  123. /** MAC digest algorithm */
  124. struct digest_algorithm *digest;
  125. /** Key length */
  126. uint16_t key_len;
  127. /** Numeric code (in network-endian order) */
  128. uint16_t code;
  129. };
  130. /** TLS cipher suite table */
  131. #define TLS_CIPHER_SUITES \
  132. __table ( struct tls_cipher_suite, "tls_cipher_suites" )
  133. /** Declare a TLS cipher suite */
  134. #define __tls_cipher_suite( pref ) \
  135. __table_entry ( TLS_CIPHER_SUITES, pref )
  136. /** A TLS cipher specification */
  137. struct tls_cipherspec {
  138. /** Cipher suite */
  139. struct tls_cipher_suite *suite;
  140. /** Dynamically-allocated storage */
  141. void *dynamic;
  142. /** Public key encryption context */
  143. void *pubkey_ctx;
  144. /** Bulk encryption cipher context */
  145. void *cipher_ctx;
  146. /** Next bulk encryption cipher context (TX only) */
  147. void *cipher_next_ctx;
  148. /** MAC secret */
  149. void *mac_secret;
  150. };
  151. /** A TLS signature and hash algorithm identifier */
  152. struct tls_signature_hash_id {
  153. /** Hash algorithm */
  154. uint8_t hash;
  155. /** Signature algorithm */
  156. uint8_t signature;
  157. } __attribute__ (( packed ));
  158. /** A TLS signature algorithm */
  159. struct tls_signature_hash_algorithm {
  160. /** Digest algorithm */
  161. struct digest_algorithm *digest;
  162. /** Public-key algorithm */
  163. struct pubkey_algorithm *pubkey;
  164. /** Numeric code */
  165. struct tls_signature_hash_id code;
  166. };
  167. /** TLS signature hash algorithm table
  168. *
  169. * Note that the default (TLSv1.1 and earlier) algorithm using
  170. * MD5+SHA1 is never explicitly specified.
  171. */
  172. #define TLS_SIG_HASH_ALGORITHMS \
  173. __table ( struct tls_signature_hash_algorithm, \
  174. "tls_sig_hash_algorithms" )
  175. /** Declare a TLS signature hash algorithm */
  176. #define __tls_sig_hash_algorithm \
  177. __table_entry ( TLS_SIG_HASH_ALGORITHMS, 01 )
  178. /** TLS pre-master secret */
  179. struct tls_pre_master_secret {
  180. /** TLS version */
  181. uint16_t version;
  182. /** Random data */
  183. uint8_t random[46];
  184. } __attribute__ (( packed ));
  185. /** TLS client random data */
  186. struct tls_client_random {
  187. /** GMT Unix time */
  188. uint32_t gmt_unix_time;
  189. /** Random data */
  190. uint8_t random[28];
  191. } __attribute__ (( packed ));
  192. /** An MD5+SHA1 context */
  193. struct md5_sha1_context {
  194. /** MD5 context */
  195. uint8_t md5[MD5_CTX_SIZE];
  196. /** SHA-1 context */
  197. uint8_t sha1[SHA1_CTX_SIZE];
  198. } __attribute__ (( packed ));
  199. /** MD5+SHA1 context size */
  200. #define MD5_SHA1_CTX_SIZE sizeof ( struct md5_sha1_context )
  201. /** An MD5+SHA1 digest */
  202. struct md5_sha1_digest {
  203. /** MD5 digest */
  204. uint8_t md5[MD5_DIGEST_SIZE];
  205. /** SHA-1 digest */
  206. uint8_t sha1[SHA1_DIGEST_SIZE];
  207. } __attribute__ (( packed ));
  208. /** MD5+SHA1 digest size */
  209. #define MD5_SHA1_DIGEST_SIZE sizeof ( struct md5_sha1_digest )
  210. /** A TLS session */
  211. struct tls_session {
  212. /** Reference counter */
  213. struct refcnt refcnt;
  214. /** List of sessions */
  215. struct list_head list;
  216. /** Server name */
  217. const char *name;
  218. /** Session ID */
  219. uint8_t id[32];
  220. /** Length of session ID */
  221. size_t id_len;
  222. /** Session ticket */
  223. void *ticket;
  224. /** Length of session ticket */
  225. size_t ticket_len;
  226. /** Master secret */
  227. uint8_t master_secret[48];
  228. /** List of connections */
  229. struct list_head conn;
  230. };
  231. /** A TLS connection */
  232. struct tls_connection {
  233. /** Reference counter */
  234. struct refcnt refcnt;
  235. /** Session */
  236. struct tls_session *session;
  237. /** List of connections within the same session */
  238. struct list_head list;
  239. /** Session ID */
  240. uint8_t session_id[32];
  241. /** Length of session ID */
  242. size_t session_id_len;
  243. /** New session ticket */
  244. void *new_session_ticket;
  245. /** Length of new session ticket */
  246. size_t new_session_ticket_len;
  247. /** Plaintext stream */
  248. struct interface plainstream;
  249. /** Ciphertext stream */
  250. struct interface cipherstream;
  251. /** Protocol version */
  252. uint16_t version;
  253. /** Current TX cipher specification */
  254. struct tls_cipherspec tx_cipherspec;
  255. /** Next TX cipher specification */
  256. struct tls_cipherspec tx_cipherspec_pending;
  257. /** Current RX cipher specification */
  258. struct tls_cipherspec rx_cipherspec;
  259. /** Next RX cipher specification */
  260. struct tls_cipherspec rx_cipherspec_pending;
  261. /** Premaster secret */
  262. struct tls_pre_master_secret pre_master_secret;
  263. /** Master secret */
  264. uint8_t master_secret[48];
  265. /** Server random bytes */
  266. uint8_t server_random[32];
  267. /** Client random bytes */
  268. struct tls_client_random client_random;
  269. /** MD5+SHA1 context for handshake verification */
  270. uint8_t handshake_md5_sha1_ctx[MD5_SHA1_CTX_SIZE];
  271. /** SHA256 context for handshake verification */
  272. uint8_t handshake_sha256_ctx[SHA256_CTX_SIZE];
  273. /** Digest algorithm used for handshake verification */
  274. struct digest_algorithm *handshake_digest;
  275. /** Digest algorithm context used for handshake verification */
  276. uint8_t *handshake_ctx;
  277. /** Client certificate (if used) */
  278. struct x509_certificate *cert;
  279. /** Secure renegotiation flag */
  280. int secure_renegotiation;
  281. /** Verification data */
  282. struct tls_verify_data verify;
  283. /** Server certificate chain */
  284. struct x509_chain *chain;
  285. /** Certificate validator */
  286. struct interface validator;
  287. /** Client security negotiation pending operation */
  288. struct pending_operation client_negotiation;
  289. /** Server security negotiation pending operation */
  290. struct pending_operation server_negotiation;
  291. /** TX sequence number */
  292. uint64_t tx_seq;
  293. /** TX pending transmissions */
  294. unsigned int tx_pending;
  295. /** TX process */
  296. struct process process;
  297. /** RX sequence number */
  298. uint64_t rx_seq;
  299. /** RX state */
  300. enum tls_rx_state rx_state;
  301. /** Current received record header */
  302. struct tls_header rx_header;
  303. /** Current received record header (static I/O buffer) */
  304. struct io_buffer rx_header_iobuf;
  305. /** List of received data buffers */
  306. struct list_head rx_data;
  307. };
  308. /** RX I/O buffer size
  309. *
  310. * The maximum fragment length extension is optional, and many common
  311. * implementations (including OpenSSL) do not support it. We must
  312. * therefore be prepared to receive records of up to 16kB in length.
  313. * The chance of an allocation of this size failing is non-negligible,
  314. * so we must split received data into smaller allocations.
  315. */
  316. #define TLS_RX_BUFSIZE 4096
  317. /** Minimum RX I/O buffer size
  318. *
  319. * To simplify manipulations, we ensure that no RX I/O buffer is
  320. * smaller than this size. This allows us to assume that the MAC and
  321. * padding are entirely contained within the final I/O buffer.
  322. */
  323. #define TLS_RX_MIN_BUFSIZE 512
  324. /** RX I/O buffer alignment */
  325. #define TLS_RX_ALIGN 16
  326. extern int add_tls ( struct interface *xfer, const char *name,
  327. struct interface **next );
  328. #endif /* _IPXE_TLS_H */