Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
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.

server_hello.go 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package tlstypes
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/rand"
  6. "crypto/sha256"
  7. "io"
  8. mrand "math/rand"
  9. "golang.org/x/crypto/curve25519"
  10. "mtg/config"
  11. )
  12. type ServerHello struct {
  13. Handshake
  14. clientHello *ClientHello
  15. }
  16. func (s ServerHello) WelcomePacket() []byte {
  17. s.Random = [32]byte{}
  18. rec := Record{
  19. Type: RecordTypeHandshake,
  20. Version: Version12,
  21. Data: &s,
  22. }
  23. buf := bytes.NewBuffer(rec.Bytes())
  24. recChangeCipher := Record{
  25. Type: RecordTypeChangeCipherSpec,
  26. Version: Version12,
  27. Data: RawBytes([]byte{0x01}),
  28. }
  29. buf.Write(recChangeCipher.Bytes())
  30. hostCert := make([]byte, 1024+mrand.Intn(3092))
  31. rand.Read(hostCert) // nolint: errcheck
  32. recData := Record{
  33. Type: RecordTypeApplicationData,
  34. Version: Version12,
  35. Data: RawBytes(hostCert),
  36. }
  37. buf.Write(recData.Bytes())
  38. packet := buf.Bytes()
  39. mac := hmac.New(sha256.New, config.C.Secret)
  40. mac.Write(s.clientHello.Random[:]) // nolint: errcheck
  41. mac.Write(packet) // nolint: errcheck
  42. copy(packet[11:], mac.Sum(nil))
  43. return packet
  44. }
  45. func NewServerHello(clientHello *ClientHello) *ServerHello {
  46. rv := &ServerHello{
  47. clientHello: clientHello,
  48. }
  49. rv.Type = HandshakeTypeServer
  50. rv.Version = Version12
  51. rv.SessionID = make([]byte, len(clientHello.SessionID))
  52. copy(rv.SessionID, clientHello.SessionID)
  53. tail := bytes.NewBuffer(CipherSuiteType_TLS_AES_128_GCM_SHA256_Bytes)
  54. tail.WriteByte(0x00) // no compression
  55. makeTLSExtensions(tail)
  56. rv.Tail = RawBytes(tail.Bytes())
  57. return rv
  58. }
  59. func makeTLSExtensions(buf io.Writer) {
  60. buf.Write([]byte{ // nolint: errcheck
  61. 0x00, 0x2e, // 46 bytes of data
  62. 0x00, 0x33, // Extension - Key Share
  63. 0x00, 0x24, // 36 bytes
  64. 0x00, 0x1d, // x25519 curve
  65. 0x00, 0x20, // 32 bytes of key
  66. })
  67. var scalar [32]byte
  68. rand.Read(scalar[:]) // nolint: errcheck
  69. curve, _ := curve25519.X25519(scalar[:], curve25519.Basepoint)
  70. buf.Write(curve) // nolint: errcheck
  71. buf.Write([]byte{ // nolint: errcheck
  72. 0x00, 0x2b, // Extension - Supported Versions
  73. 0x00, 0x02, // 2 bytes are following
  74. 0x03, 0x04, // TLS 1.3
  75. })
  76. }