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.3KB

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