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