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 4.3KB

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