Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #ifndef _GPXE_TLS_H
  2. #define _GPXE_TLS_H
  3. /**
  4. * @file
  5. *
  6. * Transport Layer Security Protocol
  7. */
  8. #include <stdint.h>
  9. #include <gpxe/refcnt.h>
  10. #include <gpxe/filter.h>
  11. #include <gpxe/process.h>
  12. #include <gpxe/crypto.h>
  13. #include <gpxe/md5.h>
  14. #include <gpxe/sha1.h>
  15. #include <gpxe/x509.h>
  16. /** A TLS header */
  17. struct tls_header {
  18. /** Content type
  19. *
  20. * This is a TLS_TYPE_XXX constant
  21. */
  22. uint8_t type;
  23. /** Protocol version
  24. *
  25. * This is a TLS_VERSION_XXX constant
  26. */
  27. uint16_t version;
  28. /** Length of payload */
  29. uint16_t length;
  30. } __attribute__ (( packed ));
  31. /** TLS version 1.0 */
  32. #define TLS_VERSION_TLS_1_0 0x0301
  33. /** TLS version 1.1 */
  34. #define TLS_VERSION_TLS_1_1 0x0302
  35. /** Change cipher content type */
  36. #define TLS_TYPE_CHANGE_CIPHER 20
  37. /** Alert content type */
  38. #define TLS_TYPE_ALERT 21
  39. /** Handshake content type */
  40. #define TLS_TYPE_HANDSHAKE 22
  41. /** Application data content type */
  42. #define TLS_TYPE_DATA 23
  43. /* Handshake message types */
  44. #define TLS_HELLO_REQUEST 0
  45. #define TLS_CLIENT_HELLO 1
  46. #define TLS_SERVER_HELLO 2
  47. #define TLS_CERTIFICATE 11
  48. #define TLS_SERVER_KEY_EXCHANGE 12
  49. #define TLS_CERTIFICATE_REQUEST 13
  50. #define TLS_SERVER_HELLO_DONE 14
  51. #define TLS_CERTIFICATE_VERIFY 15
  52. #define TLS_CLIENT_KEY_EXCHANGE 16
  53. #define TLS_FINISHED 20
  54. /* TLS alert levels */
  55. #define TLS_ALERT_WARNING 1
  56. #define TLS_ALERT_FATAL 2
  57. /* TLS cipher specifications */
  58. #define TLS_RSA_WITH_NULL_MD5 0x0001
  59. #define TLS_RSA_WITH_NULL_SHA 0x0002
  60. #define TLS_RSA_WITH_AES_128_CBC_SHA 0x002f
  61. #define TLS_RSA_WITH_AES_256_CBC_SHA 0x0035
  62. /** TLS RX state machine state */
  63. enum tls_rx_state {
  64. TLS_RX_HEADER = 0,
  65. TLS_RX_DATA,
  66. };
  67. /** TLS TX state machine state */
  68. enum tls_tx_state {
  69. TLS_TX_NONE = 0,
  70. TLS_TX_CLIENT_HELLO,
  71. TLS_TX_CLIENT_KEY_EXCHANGE,
  72. TLS_TX_CHANGE_CIPHER,
  73. TLS_TX_FINISHED,
  74. TLS_TX_DATA
  75. };
  76. /** A TLS cipher specification */
  77. struct tls_cipherspec {
  78. /** Public-key encryption algorithm */
  79. struct pubkey_algorithm *pubkey;
  80. /** Bulk encryption cipher algorithm */
  81. struct cipher_algorithm *cipher;
  82. /** MAC digest algorithm */
  83. struct digest_algorithm *digest;
  84. /** Key length */
  85. size_t key_len;
  86. /** Dynamically-allocated storage */
  87. void *dynamic;
  88. /** Public key encryption context */
  89. void *pubkey_ctx;
  90. /** Bulk encryption cipher context */
  91. void *cipher_ctx;
  92. /** Next bulk encryption cipher context (TX only) */
  93. void *cipher_next_ctx;
  94. /** MAC secret */
  95. void *mac_secret;
  96. };
  97. /** TLS pre-master secret */
  98. struct tls_pre_master_secret {
  99. /** TLS version */
  100. uint16_t version;
  101. /** Random data */
  102. uint8_t random[46];
  103. } __attribute__ (( packed ));
  104. /** TLS client random data */
  105. struct tls_client_random {
  106. /** GMT Unix time */
  107. uint32_t gmt_unix_time;
  108. /** Random data */
  109. uint8_t random[28];
  110. } __attribute__ (( packed ));
  111. /** A TLS session */
  112. struct tls_session {
  113. /** Reference counter */
  114. struct refcnt refcnt;
  115. /** Plaintext stream */
  116. struct xfer_filter_half plainstream;
  117. /** Ciphertext stream */
  118. struct xfer_filter_half cipherstream;
  119. /** Current TX cipher specification */
  120. struct tls_cipherspec tx_cipherspec;
  121. /** Next TX cipher specification */
  122. struct tls_cipherspec tx_cipherspec_pending;
  123. /** Current RX cipher specification */
  124. struct tls_cipherspec rx_cipherspec;
  125. /** Next RX cipher specification */
  126. struct tls_cipherspec rx_cipherspec_pending;
  127. /** Premaster secret */
  128. struct tls_pre_master_secret pre_master_secret;
  129. /** Master secret */
  130. uint8_t master_secret[48];
  131. /** Server random bytes */
  132. uint8_t server_random[32];
  133. /** Client random bytes */
  134. struct tls_client_random client_random;
  135. /** MD5 context for handshake verification */
  136. uint8_t handshake_md5_ctx[MD5_CTX_SIZE];
  137. /** SHA1 context for handshake verification */
  138. uint8_t handshake_sha1_ctx[SHA1_CTX_SIZE];
  139. /** Hack: server RSA public key */
  140. struct x509_rsa_public_key rsa;
  141. /** TX sequence number */
  142. uint64_t tx_seq;
  143. /** TX state */
  144. enum tls_tx_state tx_state;
  145. /** TX process */
  146. struct process process;
  147. /** RX sequence number */
  148. uint64_t rx_seq;
  149. /** RX state */
  150. enum tls_rx_state rx_state;
  151. /** Offset within current RX state */
  152. size_t rx_rcvd;
  153. /** Current received record header */
  154. struct tls_header rx_header;
  155. /** Current received raw data buffer */
  156. void *rx_data;
  157. };
  158. extern int add_tls ( struct xfer_interface *xfer,
  159. struct xfer_interface **next );
  160. #endif /* _GPXE_TLS_H */