Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

tls.h 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 );
  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. /** A TLS header */
  19. struct tls_header {
  20. /** Content type
  21. *
  22. * This is a TLS_TYPE_XXX constant
  23. */
  24. uint8_t type;
  25. /** Protocol version
  26. *
  27. * This is a TLS_VERSION_XXX constant
  28. */
  29. uint16_t version;
  30. /** Length of payload */
  31. uint16_t length;
  32. } __attribute__ (( packed ));
  33. /** TLS version 1.0 */
  34. #define TLS_VERSION_TLS_1_0 0x0301
  35. /** TLS version 1.1 */
  36. #define TLS_VERSION_TLS_1_1 0x0302
  37. /** TLS version 1.2 */
  38. #define TLS_VERSION_TLS_1_2 0x0303
  39. /** Change cipher content type */
  40. #define TLS_TYPE_CHANGE_CIPHER 20
  41. /** Alert content type */
  42. #define TLS_TYPE_ALERT 21
  43. /** Handshake content type */
  44. #define TLS_TYPE_HANDSHAKE 22
  45. /** Application data content type */
  46. #define TLS_TYPE_DATA 23
  47. /* Handshake message types */
  48. #define TLS_HELLO_REQUEST 0
  49. #define TLS_CLIENT_HELLO 1
  50. #define TLS_SERVER_HELLO 2
  51. #define TLS_CERTIFICATE 11
  52. #define TLS_SERVER_KEY_EXCHANGE 12
  53. #define TLS_CERTIFICATE_REQUEST 13
  54. #define TLS_SERVER_HELLO_DONE 14
  55. #define TLS_CERTIFICATE_VERIFY 15
  56. #define TLS_CLIENT_KEY_EXCHANGE 16
  57. #define TLS_FINISHED 20
  58. /* TLS alert levels */
  59. #define TLS_ALERT_WARNING 1
  60. #define TLS_ALERT_FATAL 2
  61. /* TLS cipher specifications */
  62. #define TLS_RSA_WITH_NULL_MD5 0x0001
  63. #define TLS_RSA_WITH_NULL_SHA 0x0002
  64. #define TLS_RSA_WITH_AES_128_CBC_SHA 0x002f
  65. #define TLS_RSA_WITH_AES_256_CBC_SHA 0x0035
  66. #define TLS_RSA_WITH_AES_128_CBC_SHA256 0x003c
  67. #define TLS_RSA_WITH_AES_256_CBC_SHA256 0x003d
  68. /* TLS hash algorithm identifiers */
  69. #define TLS_MD5_ALGORITHM 1
  70. #define TLS_SHA1_ALGORITHM 2
  71. #define TLS_SHA256_ALGORITHM 4
  72. /* TLS signature algorithm identifiers */
  73. #define TLS_RSA_ALGORITHM 1
  74. /* TLS extension types */
  75. #define TLS_SERVER_NAME 0
  76. #define TLS_SERVER_NAME_HOST_NAME 0
  77. /** TLS RX state machine state */
  78. enum tls_rx_state {
  79. TLS_RX_HEADER = 0,
  80. TLS_RX_DATA,
  81. };
  82. /** TLS TX pending flags */
  83. enum tls_tx_pending {
  84. TLS_TX_CLIENT_HELLO = 0x0001,
  85. TLS_TX_CERTIFICATE = 0x0002,
  86. TLS_TX_CLIENT_KEY_EXCHANGE = 0x0004,
  87. TLS_TX_CERTIFICATE_VERIFY = 0x0008,
  88. TLS_TX_CHANGE_CIPHER = 0x0010,
  89. TLS_TX_FINISHED = 0x0020,
  90. };
  91. /** A TLS cipher suite */
  92. struct tls_cipher_suite {
  93. /** Public-key encryption algorithm */
  94. struct pubkey_algorithm *pubkey;
  95. /** Bulk encryption cipher algorithm */
  96. struct cipher_algorithm *cipher;
  97. /** MAC digest algorithm */
  98. struct digest_algorithm *digest;
  99. /** Key length */
  100. uint16_t key_len;
  101. /** Numeric code (in network-endian order) */
  102. uint16_t code;
  103. };
  104. /** A TLS cipher specification */
  105. struct tls_cipherspec {
  106. /** Cipher suite */
  107. struct tls_cipher_suite *suite;
  108. /** Dynamically-allocated storage */
  109. void *dynamic;
  110. /** Public key encryption context */
  111. void *pubkey_ctx;
  112. /** Bulk encryption cipher context */
  113. void *cipher_ctx;
  114. /** Next bulk encryption cipher context (TX only) */
  115. void *cipher_next_ctx;
  116. /** MAC secret */
  117. void *mac_secret;
  118. };
  119. /** A TLS signature and hash algorithm identifier */
  120. struct tls_signature_hash_id {
  121. /** Hash algorithm */
  122. uint8_t hash;
  123. /** Signature algorithm */
  124. uint8_t signature;
  125. } __attribute__ (( packed ));
  126. /** A TLS signature algorithm */
  127. struct tls_signature_hash_algorithm {
  128. /** Digest algorithm */
  129. struct digest_algorithm *digest;
  130. /** Public-key algorithm */
  131. struct pubkey_algorithm *pubkey;
  132. /** Numeric code */
  133. struct tls_signature_hash_id code;
  134. };
  135. /** TLS pre-master secret */
  136. struct tls_pre_master_secret {
  137. /** TLS version */
  138. uint16_t version;
  139. /** Random data */
  140. uint8_t random[46];
  141. } __attribute__ (( packed ));
  142. /** TLS client random data */
  143. struct tls_client_random {
  144. /** GMT Unix time */
  145. uint32_t gmt_unix_time;
  146. /** Random data */
  147. uint8_t random[28];
  148. } __attribute__ (( packed ));
  149. /** An MD5+SHA1 context */
  150. struct md5_sha1_context {
  151. /** MD5 context */
  152. uint8_t md5[MD5_CTX_SIZE];
  153. /** SHA-1 context */
  154. uint8_t sha1[SHA1_CTX_SIZE];
  155. } __attribute__ (( packed ));
  156. /** MD5+SHA1 context size */
  157. #define MD5_SHA1_CTX_SIZE sizeof ( struct md5_sha1_context )
  158. /** An MD5+SHA1 digest */
  159. struct md5_sha1_digest {
  160. /** MD5 digest */
  161. uint8_t md5[MD5_DIGEST_SIZE];
  162. /** SHA-1 digest */
  163. uint8_t sha1[SHA1_DIGEST_SIZE];
  164. } __attribute__ (( packed ));
  165. /** MD5+SHA1 digest size */
  166. #define MD5_SHA1_DIGEST_SIZE sizeof ( struct md5_sha1_digest )
  167. /** A TLS session */
  168. struct tls_session {
  169. /** Reference counter */
  170. struct refcnt refcnt;
  171. /** Server name */
  172. const char *name;
  173. /** Plaintext stream */
  174. struct interface plainstream;
  175. /** Ciphertext stream */
  176. struct interface cipherstream;
  177. /** Protocol version */
  178. uint16_t version;
  179. /** Current TX cipher specification */
  180. struct tls_cipherspec tx_cipherspec;
  181. /** Next TX cipher specification */
  182. struct tls_cipherspec tx_cipherspec_pending;
  183. /** Current RX cipher specification */
  184. struct tls_cipherspec rx_cipherspec;
  185. /** Next RX cipher specification */
  186. struct tls_cipherspec rx_cipherspec_pending;
  187. /** Premaster secret */
  188. struct tls_pre_master_secret pre_master_secret;
  189. /** Master secret */
  190. uint8_t master_secret[48];
  191. /** Server random bytes */
  192. uint8_t server_random[32];
  193. /** Client random bytes */
  194. struct tls_client_random client_random;
  195. /** MD5+SHA1 context for handshake verification */
  196. uint8_t handshake_md5_sha1_ctx[MD5_SHA1_CTX_SIZE];
  197. /** SHA256 context for handshake verification */
  198. uint8_t handshake_sha256_ctx[SHA256_CTX_SIZE];
  199. /** Digest algorithm used for handshake verification */
  200. struct digest_algorithm *handshake_digest;
  201. /** Digest algorithm context used for handshake verification */
  202. uint8_t *handshake_ctx;
  203. /** Public-key algorithm used for Certificate Verify (if sent) */
  204. struct pubkey_algorithm *verify_pubkey;
  205. /** TX sequence number */
  206. uint64_t tx_seq;
  207. /** TX pending transmissions */
  208. unsigned int tx_pending;
  209. /** TX process */
  210. struct process process;
  211. /** TX ready for plaintext data */
  212. int tx_ready;
  213. /** RX sequence number */
  214. uint64_t rx_seq;
  215. /** RX state */
  216. enum tls_rx_state rx_state;
  217. /** Offset within current RX state */
  218. size_t rx_rcvd;
  219. /** Current received record header */
  220. struct tls_header rx_header;
  221. /** Current received raw data buffer */
  222. void *rx_data;
  223. };
  224. extern int add_tls ( struct interface *xfer, const char *name,
  225. struct interface **next );
  226. #endif /* _IPXE_TLS_H */