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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /** A TLS session */
  97. struct tls_session {
  98. /** Reference counter */
  99. struct refcnt refcnt;
  100. /** Plaintext stream */
  101. struct xfer_filter_half plainstream;
  102. /** Ciphertext stream */
  103. struct xfer_filter_half cipherstream;
  104. /** Current TX cipher specification */
  105. struct tls_cipherspec tx_cipherspec;
  106. /** Next TX cipher specification */
  107. struct tls_cipherspec tx_cipherspec_pending;
  108. /** Current RX cipher specification */
  109. struct tls_cipherspec rx_cipherspec;
  110. /** Next RX cipher specification */
  111. struct tls_cipherspec rx_cipherspec_pending;
  112. /** Premaster secret */
  113. uint8_t pre_master_secret[48];
  114. /** Master secret */
  115. uint8_t master_secret[48];
  116. /** Server random bytes */
  117. uint8_t server_random[32];
  118. /** Client random bytes */
  119. uint8_t client_random[32];
  120. /** MD5 context for handshake verification */
  121. uint8_t handshake_md5_ctx[MD5_CTX_SIZE];
  122. /** SHA1 context for handshake verification */
  123. uint8_t handshake_sha1_ctx[SHA1_CTX_SIZE];
  124. /** Hack: server RSA public key */
  125. uint8_t *rsa_mod;
  126. size_t rsa_mod_len;
  127. uint8_t *rsa_pub_exp;
  128. size_t rsa_pub_exp_len;
  129. /** TX sequence number */
  130. uint64_t tx_seq;
  131. /** TX state */
  132. enum tls_tx_state tx_state;
  133. /** TX process */
  134. struct process process;
  135. /** RX sequence number */
  136. uint64_t rx_seq;
  137. /** RX state */
  138. enum tls_rx_state rx_state;
  139. /** Offset within current RX state */
  140. size_t rx_rcvd;
  141. /** Current received record header */
  142. struct tls_header rx_header;
  143. /** Current received raw data buffer */
  144. void *rx_data;
  145. };
  146. extern int add_tls ( struct xfer_interface *xfer,
  147. struct xfer_interface **next );
  148. #endif /* _GPXE_TLS_H */