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.

372 lines
11 KiB

  1. package conn
  2. import (
  3. "bytes"
  4. crand "crypto/rand"
  5. "crypto/sha256"
  6. "encoding/binary"
  7. "errors"
  8. "io"
  9. "net"
  10. "sync"
  11. "time"
  12. "golang.org/x/crypto/chacha20poly1305"
  13. "golang.org/x/crypto/curve25519"
  14. "golang.org/x/crypto/nacl/box"
  15. "github.com/tendermint/tendermint/crypto"
  16. cmn "github.com/tendermint/tendermint/libs/common"
  17. "golang.org/x/crypto/hkdf"
  18. )
  19. // 4 + 1024 == 1028 total frame size
  20. const dataLenSize = 4
  21. const dataMaxSize = 1024
  22. const totalFrameSize = dataMaxSize + dataLenSize
  23. const aeadSizeOverhead = 16 // overhead of poly 1305 authentication tag
  24. const aeadKeySize = chacha20poly1305.KeySize
  25. const aeadNonceSize = chacha20poly1305.NonceSize
  26. // SecretConnection implements net.Conn.
  27. // It is an implementation of the STS protocol.
  28. // See https://github.com/tendermint/tendermint/blob/0.1/docs/sts-final.pdf for
  29. // details on the protocol.
  30. //
  31. // Consumers of the SecretConnection are responsible for authenticating
  32. // the remote peer's pubkey against known information, like a nodeID.
  33. // Otherwise they are vulnerable to MITM.
  34. // (TODO(ismail): see also https://github.com/tendermint/tendermint/issues/3010)
  35. type SecretConnection struct {
  36. // immutable
  37. recvSecret *[aeadKeySize]byte
  38. sendSecret *[aeadKeySize]byte
  39. remPubKey crypto.PubKey
  40. conn io.ReadWriteCloser
  41. // net.Conn must be thread safe:
  42. // https://golang.org/pkg/net/#Conn.
  43. // Since we have internal mutable state,
  44. // we need mtxs. But recv and send states
  45. // are independent, so we can use two mtxs.
  46. // All .Read are covered by recvMtx,
  47. // all .Write are covered by sendMtx.
  48. recvMtx sync.Mutex
  49. recvBuffer []byte
  50. recvNonce *[aeadNonceSize]byte
  51. sendMtx sync.Mutex
  52. sendNonce *[aeadNonceSize]byte
  53. }
  54. // MakeSecretConnection performs handshake and returns a new authenticated
  55. // SecretConnection.
  56. // Returns nil if there is an error in handshake.
  57. // Caller should call conn.Close()
  58. // See docs/sts-final.pdf for more information.
  59. func MakeSecretConnection(conn io.ReadWriteCloser, locPrivKey crypto.PrivKey) (*SecretConnection, error) {
  60. locPubKey := locPrivKey.PubKey()
  61. // Generate ephemeral keys for perfect forward secrecy.
  62. locEphPub, locEphPriv := genEphKeys()
  63. // Write local ephemeral pubkey and receive one too.
  64. // NOTE: every 32-byte string is accepted as a Curve25519 public key
  65. // (see DJB's Curve25519 paper: http://cr.yp.to/ecdh/curve25519-20060209.pdf)
  66. remEphPub, err := shareEphPubKey(conn, locEphPub)
  67. if err != nil {
  68. return nil, err
  69. }
  70. // Sort by lexical order.
  71. loEphPub, _ := sort32(locEphPub, remEphPub)
  72. // Check if the local ephemeral public key
  73. // was the least, lexicographically sorted.
  74. locIsLeast := bytes.Equal(locEphPub[:], loEphPub[:])
  75. // Compute common diffie hellman secret using X25519.
  76. dhSecret := computeDHSecret(remEphPub, locEphPriv)
  77. // generate the secret used for receiving, sending, challenge via hkdf-sha2 on dhSecret
  78. recvSecret, sendSecret, challenge := deriveSecretAndChallenge(dhSecret, locIsLeast)
  79. // Construct SecretConnection.
  80. sc := &SecretConnection{
  81. conn: conn,
  82. recvBuffer: nil,
  83. recvNonce: new([aeadNonceSize]byte),
  84. sendNonce: new([aeadNonceSize]byte),
  85. recvSecret: recvSecret,
  86. sendSecret: sendSecret,
  87. }
  88. // Sign the challenge bytes for authentication.
  89. locSignature := signChallenge(challenge, locPrivKey)
  90. // Share (in secret) each other's pubkey & challenge signature
  91. authSigMsg, err := shareAuthSignature(sc, locPubKey, locSignature)
  92. if err != nil {
  93. return nil, err
  94. }
  95. remPubKey, remSignature := authSigMsg.Key, authSigMsg.Sig
  96. if !remPubKey.VerifyBytes(challenge[:], remSignature) {
  97. return nil, errors.New("Challenge verification failed")
  98. }
  99. // We've authorized.
  100. sc.remPubKey = remPubKey
  101. return sc, nil
  102. }
  103. // RemotePubKey returns authenticated remote pubkey
  104. func (sc *SecretConnection) RemotePubKey() crypto.PubKey {
  105. return sc.remPubKey
  106. }
  107. // Writes encrypted frames of `totalFrameSize + aeadSizeOverhead`.
  108. // CONTRACT: data smaller than dataMaxSize is written atomically.
  109. func (sc *SecretConnection) Write(data []byte) (n int, err error) {
  110. sc.sendMtx.Lock()
  111. defer sc.sendMtx.Unlock()
  112. for 0 < len(data) {
  113. var frame = make([]byte, totalFrameSize)
  114. var chunk []byte
  115. if dataMaxSize < len(data) {
  116. chunk = data[:dataMaxSize]
  117. data = data[dataMaxSize:]
  118. } else {
  119. chunk = data
  120. data = nil
  121. }
  122. chunkLength := len(chunk)
  123. binary.LittleEndian.PutUint32(frame, uint32(chunkLength))
  124. copy(frame[dataLenSize:], chunk)
  125. aead, err := chacha20poly1305.New(sc.sendSecret[:])
  126. if err != nil {
  127. return n, errors.New("Invalid SecretConnection Key")
  128. }
  129. // encrypt the frame
  130. var sealedFrame = make([]byte, aeadSizeOverhead+totalFrameSize)
  131. aead.Seal(sealedFrame[:0], sc.sendNonce[:], frame, nil)
  132. incrNonce(sc.sendNonce)
  133. // end encryption
  134. _, err = sc.conn.Write(sealedFrame)
  135. if err != nil {
  136. return n, err
  137. }
  138. n += len(chunk)
  139. }
  140. return
  141. }
  142. // CONTRACT: data smaller than dataMaxSize is read atomically.
  143. func (sc *SecretConnection) Read(data []byte) (n int, err error) {
  144. sc.recvMtx.Lock()
  145. defer sc.recvMtx.Unlock()
  146. // read off and update the recvBuffer, if non-empty
  147. if 0 < len(sc.recvBuffer) {
  148. n = copy(data, sc.recvBuffer)
  149. sc.recvBuffer = sc.recvBuffer[n:]
  150. return
  151. }
  152. // read off the conn
  153. sealedFrame := make([]byte, totalFrameSize+aeadSizeOverhead)
  154. _, err = io.ReadFull(sc.conn, sealedFrame)
  155. if err != nil {
  156. return
  157. }
  158. aead, err := chacha20poly1305.New(sc.recvSecret[:])
  159. if err != nil {
  160. return n, errors.New("Invalid SecretConnection Key")
  161. }
  162. // decrypt the frame.
  163. // reads and updates the sc.recvNonce
  164. var frame = make([]byte, totalFrameSize)
  165. _, err = aead.Open(frame[:0], sc.recvNonce[:], sealedFrame, nil)
  166. if err != nil {
  167. return n, errors.New("Failed to decrypt SecretConnection")
  168. }
  169. incrNonce(sc.recvNonce)
  170. // end decryption
  171. // copy checkLength worth into data,
  172. // set recvBuffer to the rest.
  173. var chunkLength = binary.LittleEndian.Uint32(frame) // read the first four bytes
  174. if chunkLength > dataMaxSize {
  175. return 0, errors.New("chunkLength is greater than dataMaxSize")
  176. }
  177. var chunk = frame[dataLenSize : dataLenSize+chunkLength]
  178. n = copy(data, chunk)
  179. sc.recvBuffer = chunk[n:]
  180. return
  181. }
  182. // Implements net.Conn
  183. // nolint
  184. func (sc *SecretConnection) Close() error { return sc.conn.Close() }
  185. func (sc *SecretConnection) LocalAddr() net.Addr { return sc.conn.(net.Conn).LocalAddr() }
  186. func (sc *SecretConnection) RemoteAddr() net.Addr { return sc.conn.(net.Conn).RemoteAddr() }
  187. func (sc *SecretConnection) SetDeadline(t time.Time) error { return sc.conn.(net.Conn).SetDeadline(t) }
  188. func (sc *SecretConnection) SetReadDeadline(t time.Time) error {
  189. return sc.conn.(net.Conn).SetReadDeadline(t)
  190. }
  191. func (sc *SecretConnection) SetWriteDeadline(t time.Time) error {
  192. return sc.conn.(net.Conn).SetWriteDeadline(t)
  193. }
  194. func genEphKeys() (ephPub, ephPriv *[32]byte) {
  195. var err error
  196. ephPub, ephPriv, err = box.GenerateKey(crand.Reader)
  197. if err != nil {
  198. panic("Could not generate ephemeral keypairs")
  199. }
  200. return
  201. }
  202. func shareEphPubKey(conn io.ReadWriteCloser, locEphPub *[32]byte) (remEphPub *[32]byte, err error) {
  203. // Send our pubkey and receive theirs in tandem.
  204. var trs, _ = cmn.Parallel(
  205. func(_ int) (val interface{}, err error, abort bool) {
  206. var _, err1 = cdc.MarshalBinaryLengthPrefixedWriter(conn, locEphPub)
  207. if err1 != nil {
  208. return nil, err1, true // abort
  209. }
  210. return nil, nil, false
  211. },
  212. func(_ int) (val interface{}, err error, abort bool) {
  213. var _remEphPub [32]byte
  214. var _, err2 = cdc.UnmarshalBinaryLengthPrefixedReader(conn, &_remEphPub, 1024*1024) // TODO
  215. if err2 != nil {
  216. return nil, err2, true // abort
  217. }
  218. return _remEphPub, nil, false
  219. },
  220. )
  221. // If error:
  222. if trs.FirstError() != nil {
  223. err = trs.FirstError()
  224. return
  225. }
  226. // Otherwise:
  227. var _remEphPub = trs.FirstValue().([32]byte)
  228. return &_remEphPub, nil
  229. }
  230. func deriveSecretAndChallenge(dhSecret *[32]byte, locIsLeast bool) (recvSecret, sendSecret *[aeadKeySize]byte, challenge *[32]byte) {
  231. hash := sha256.New
  232. hkdf := hkdf.New(hash, dhSecret[:], nil, []byte("TENDERMINT_SECRET_CONNECTION_KEY_AND_CHALLENGE_GEN"))
  233. // get enough data for 2 aead keys, and a 32 byte challenge
  234. res := new([2*aeadKeySize + 32]byte)
  235. _, err := io.ReadFull(hkdf, res[:])
  236. if err != nil {
  237. panic(err)
  238. }
  239. challenge = new([32]byte)
  240. recvSecret = new([aeadKeySize]byte)
  241. sendSecret = new([aeadKeySize]byte)
  242. // Use the last 32 bytes as the challenge
  243. copy(challenge[:], res[2*aeadKeySize:2*aeadKeySize+32])
  244. // bytes 0 through aeadKeySize - 1 are one aead key.
  245. // bytes aeadKeySize through 2*aeadKeySize -1 are another aead key.
  246. // which key corresponds to sending and receiving key depends on whether
  247. // the local key is less than the remote key.
  248. if locIsLeast {
  249. copy(recvSecret[:], res[0:aeadKeySize])
  250. copy(sendSecret[:], res[aeadKeySize:aeadKeySize*2])
  251. } else {
  252. copy(sendSecret[:], res[0:aeadKeySize])
  253. copy(recvSecret[:], res[aeadKeySize:aeadKeySize*2])
  254. }
  255. return
  256. }
  257. func computeDHSecret(remPubKey, locPrivKey *[32]byte) (shrKey *[32]byte) {
  258. shrKey = new([32]byte)
  259. curve25519.ScalarMult(shrKey, locPrivKey, remPubKey)
  260. return
  261. }
  262. func sort32(foo, bar *[32]byte) (lo, hi *[32]byte) {
  263. if bytes.Compare(foo[:], bar[:]) < 0 {
  264. lo = foo
  265. hi = bar
  266. } else {
  267. lo = bar
  268. hi = foo
  269. }
  270. return
  271. }
  272. func signChallenge(challenge *[32]byte, locPrivKey crypto.PrivKey) (signature []byte) {
  273. signature, err := locPrivKey.Sign(challenge[:])
  274. // TODO(ismail): let signChallenge return an error instead
  275. if err != nil {
  276. panic(err)
  277. }
  278. return
  279. }
  280. type authSigMessage struct {
  281. Key crypto.PubKey
  282. Sig []byte
  283. }
  284. func shareAuthSignature(sc *SecretConnection, pubKey crypto.PubKey, signature []byte) (recvMsg authSigMessage, err error) {
  285. // Send our info and receive theirs in tandem.
  286. var trs, _ = cmn.Parallel(
  287. func(_ int) (val interface{}, err error, abort bool) {
  288. var _, err1 = cdc.MarshalBinaryLengthPrefixedWriter(sc, authSigMessage{pubKey, signature})
  289. if err1 != nil {
  290. return nil, err1, true // abort
  291. }
  292. return nil, nil, false
  293. },
  294. func(_ int) (val interface{}, err error, abort bool) {
  295. var _recvMsg authSigMessage
  296. var _, err2 = cdc.UnmarshalBinaryLengthPrefixedReader(sc, &_recvMsg, 1024*1024) // TODO
  297. if err2 != nil {
  298. return nil, err2, true // abort
  299. }
  300. return _recvMsg, nil, false
  301. },
  302. )
  303. // If error:
  304. if trs.FirstError() != nil {
  305. err = trs.FirstError()
  306. return
  307. }
  308. var _recvMsg = trs.FirstValue().(authSigMessage)
  309. return _recvMsg, nil
  310. }
  311. //--------------------------------------------------------------------------------
  312. // Increment nonce little-endian by 1 with wraparound.
  313. // Due to chacha20poly1305 expecting a 12 byte nonce we do not use the first four
  314. // bytes. We only increment a 64 bit unsigned int in the remaining 8 bytes
  315. // (little-endian in nonce[4:]).
  316. func incrNonce(nonce *[aeadNonceSize]byte) {
  317. counter := binary.LittleEndian.Uint64(nonce[4:])
  318. counter++
  319. binary.LittleEndian.PutUint64(nonce[4:], counter)
  320. }