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.

474 lines
15 KiB

  1. package conn
  2. import (
  3. "bytes"
  4. "crypto/cipher"
  5. crand "crypto/rand"
  6. "crypto/sha256"
  7. "crypto/subtle"
  8. "encoding/binary"
  9. "io"
  10. "math"
  11. "net"
  12. "sync"
  13. "time"
  14. pool "github.com/libp2p/go-buffer-pool"
  15. "github.com/pkg/errors"
  16. "golang.org/x/crypto/chacha20poly1305"
  17. "golang.org/x/crypto/curve25519"
  18. "golang.org/x/crypto/hkdf"
  19. "golang.org/x/crypto/nacl/box"
  20. "github.com/tendermint/tendermint/crypto"
  21. "github.com/tendermint/tendermint/crypto/ed25519"
  22. cmn "github.com/tendermint/tendermint/libs/common"
  23. )
  24. // 4 + 1024 == 1028 total frame size
  25. const dataLenSize = 4
  26. const dataMaxSize = 1024
  27. const totalFrameSize = dataMaxSize + dataLenSize
  28. const aeadSizeOverhead = 16 // overhead of poly 1305 authentication tag
  29. const aeadKeySize = chacha20poly1305.KeySize
  30. const aeadNonceSize = chacha20poly1305.NonceSize
  31. var (
  32. ErrSmallOrderRemotePubKey = errors.New("detected low order point from remote peer")
  33. ErrSharedSecretIsZero = errors.New("shared secret is all zeroes")
  34. )
  35. // SecretConnection implements net.Conn.
  36. // It is an implementation of the STS protocol.
  37. // See https://github.com/tendermint/tendermint/blob/0.1/docs/sts-final.pdf for
  38. // details on the protocol.
  39. //
  40. // Consumers of the SecretConnection are responsible for authenticating
  41. // the remote peer's pubkey against known information, like a nodeID.
  42. // Otherwise they are vulnerable to MITM.
  43. // (TODO(ismail): see also https://github.com/tendermint/tendermint/issues/3010)
  44. type SecretConnection struct {
  45. // immutable
  46. recvAead cipher.AEAD
  47. sendAead cipher.AEAD
  48. remPubKey crypto.PubKey
  49. conn io.ReadWriteCloser
  50. // net.Conn must be thread safe:
  51. // https://golang.org/pkg/net/#Conn.
  52. // Since we have internal mutable state,
  53. // we need mtxs. But recv and send states
  54. // are independent, so we can use two mtxs.
  55. // All .Read are covered by recvMtx,
  56. // all .Write are covered by sendMtx.
  57. recvMtx sync.Mutex
  58. recvBuffer []byte
  59. recvNonce *[aeadNonceSize]byte
  60. sendMtx sync.Mutex
  61. sendNonce *[aeadNonceSize]byte
  62. }
  63. // MakeSecretConnection performs handshake and returns a new authenticated
  64. // SecretConnection.
  65. // Returns nil if there is an error in handshake.
  66. // Caller should call conn.Close()
  67. // See docs/sts-final.pdf for more information.
  68. func MakeSecretConnection(conn io.ReadWriteCloser, locPrivKey crypto.PrivKey) (*SecretConnection, error) {
  69. locPubKey := locPrivKey.PubKey()
  70. // Generate ephemeral keys for perfect forward secrecy.
  71. locEphPub, locEphPriv := genEphKeys()
  72. // Write local ephemeral pubkey and receive one too.
  73. // NOTE: every 32-byte string is accepted as a Curve25519 public key
  74. // (see DJB's Curve25519 paper: http://cr.yp.to/ecdh/curve25519-20060209.pdf)
  75. remEphPub, err := shareEphPubKey(conn, locEphPub)
  76. if err != nil {
  77. return nil, err
  78. }
  79. // Sort by lexical order.
  80. loEphPub, _ := sort32(locEphPub, remEphPub)
  81. // Check if the local ephemeral public key
  82. // was the least, lexicographically sorted.
  83. locIsLeast := bytes.Equal(locEphPub[:], loEphPub[:])
  84. // Compute common diffie hellman secret using X25519.
  85. dhSecret, err := computeDHSecret(remEphPub, locEphPriv)
  86. if err != nil {
  87. return nil, err
  88. }
  89. // generate the secret used for receiving, sending, challenge via hkdf-sha2 on dhSecret
  90. recvSecret, sendSecret, challenge := deriveSecretAndChallenge(dhSecret, locIsLeast)
  91. sendAead, err := chacha20poly1305.New(sendSecret[:])
  92. if err != nil {
  93. return nil, errors.New("invalid send SecretConnection Key")
  94. }
  95. recvAead, err := chacha20poly1305.New(recvSecret[:])
  96. if err != nil {
  97. return nil, errors.New("invalid receive SecretConnection Key")
  98. }
  99. // Construct SecretConnection.
  100. sc := &SecretConnection{
  101. conn: conn,
  102. recvBuffer: nil,
  103. recvNonce: new([aeadNonceSize]byte),
  104. sendNonce: new([aeadNonceSize]byte),
  105. recvAead: recvAead,
  106. sendAead: sendAead,
  107. }
  108. // Sign the challenge bytes for authentication.
  109. locSignature := signChallenge(challenge, locPrivKey)
  110. // Share (in secret) each other's pubkey & challenge signature
  111. authSigMsg, err := shareAuthSignature(sc, locPubKey, locSignature)
  112. if err != nil {
  113. return nil, err
  114. }
  115. remPubKey, remSignature := authSigMsg.Key, authSigMsg.Sig
  116. if _, ok := remPubKey.(ed25519.PubKeyEd25519); !ok {
  117. return nil, errors.Errorf("expected ed25519 pubkey, got %T", remPubKey)
  118. }
  119. if !remPubKey.VerifyBytes(challenge[:], remSignature) {
  120. return nil, errors.New("challenge verification failed")
  121. }
  122. // We've authorized.
  123. sc.remPubKey = remPubKey
  124. return sc, nil
  125. }
  126. // RemotePubKey returns authenticated remote pubkey
  127. func (sc *SecretConnection) RemotePubKey() crypto.PubKey {
  128. return sc.remPubKey
  129. }
  130. // Writes encrypted frames of `totalFrameSize + aeadSizeOverhead`.
  131. // CONTRACT: data smaller than dataMaxSize is written atomically.
  132. func (sc *SecretConnection) Write(data []byte) (n int, err error) {
  133. sc.sendMtx.Lock()
  134. defer sc.sendMtx.Unlock()
  135. for 0 < len(data) {
  136. if err := func() error {
  137. var sealedFrame = pool.Get(aeadSizeOverhead + totalFrameSize)
  138. var frame = pool.Get(totalFrameSize)
  139. defer func() {
  140. pool.Put(sealedFrame)
  141. pool.Put(frame)
  142. }()
  143. var chunk []byte
  144. if dataMaxSize < len(data) {
  145. chunk = data[:dataMaxSize]
  146. data = data[dataMaxSize:]
  147. } else {
  148. chunk = data
  149. data = nil
  150. }
  151. chunkLength := len(chunk)
  152. binary.LittleEndian.PutUint32(frame, uint32(chunkLength))
  153. copy(frame[dataLenSize:], chunk)
  154. // encrypt the frame
  155. sc.sendAead.Seal(sealedFrame[:0], sc.sendNonce[:], frame, nil)
  156. incrNonce(sc.sendNonce)
  157. // end encryption
  158. _, err = sc.conn.Write(sealedFrame)
  159. if err != nil {
  160. return err
  161. }
  162. n += len(chunk)
  163. return nil
  164. }(); err != nil {
  165. return n, err
  166. }
  167. }
  168. return n, err
  169. }
  170. // CONTRACT: data smaller than dataMaxSize is read atomically.
  171. func (sc *SecretConnection) Read(data []byte) (n int, err error) {
  172. sc.recvMtx.Lock()
  173. defer sc.recvMtx.Unlock()
  174. // read off and update the recvBuffer, if non-empty
  175. if 0 < len(sc.recvBuffer) {
  176. n = copy(data, sc.recvBuffer)
  177. sc.recvBuffer = sc.recvBuffer[n:]
  178. return
  179. }
  180. // read off the conn
  181. var sealedFrame = pool.Get(aeadSizeOverhead + totalFrameSize)
  182. defer pool.Put(sealedFrame)
  183. _, err = io.ReadFull(sc.conn, sealedFrame)
  184. if err != nil {
  185. return
  186. }
  187. // decrypt the frame.
  188. // reads and updates the sc.recvNonce
  189. var frame = pool.Get(totalFrameSize)
  190. defer pool.Put(frame)
  191. _, err = sc.recvAead.Open(frame[:0], sc.recvNonce[:], sealedFrame, nil)
  192. if err != nil {
  193. return n, errors.New("failed to decrypt SecretConnection")
  194. }
  195. incrNonce(sc.recvNonce)
  196. // end decryption
  197. // copy checkLength worth into data,
  198. // set recvBuffer to the rest.
  199. var chunkLength = binary.LittleEndian.Uint32(frame) // read the first four bytes
  200. if chunkLength > dataMaxSize {
  201. return 0, errors.New("chunkLength is greater than dataMaxSize")
  202. }
  203. var chunk = frame[dataLenSize : dataLenSize+chunkLength]
  204. n = copy(data, chunk)
  205. if n < len(chunk) {
  206. sc.recvBuffer = make([]byte, len(chunk)-n)
  207. copy(sc.recvBuffer, chunk[n:])
  208. }
  209. return n, err
  210. }
  211. // Implements net.Conn
  212. // nolint
  213. func (sc *SecretConnection) Close() error { return sc.conn.Close() }
  214. func (sc *SecretConnection) LocalAddr() net.Addr { return sc.conn.(net.Conn).LocalAddr() }
  215. func (sc *SecretConnection) RemoteAddr() net.Addr { return sc.conn.(net.Conn).RemoteAddr() }
  216. func (sc *SecretConnection) SetDeadline(t time.Time) error { return sc.conn.(net.Conn).SetDeadline(t) }
  217. func (sc *SecretConnection) SetReadDeadline(t time.Time) error {
  218. return sc.conn.(net.Conn).SetReadDeadline(t)
  219. }
  220. func (sc *SecretConnection) SetWriteDeadline(t time.Time) error {
  221. return sc.conn.(net.Conn).SetWriteDeadline(t)
  222. }
  223. func genEphKeys() (ephPub, ephPriv *[32]byte) {
  224. var err error
  225. // TODO: Probably not a problem but ask Tony: different from the rust implementation (uses x25519-dalek),
  226. // we do not "clamp" the private key scalar:
  227. // see: https://github.com/dalek-cryptography/x25519-dalek/blob/34676d336049df2bba763cc076a75e47ae1f170f/src/x25519.rs#L56-L74
  228. ephPub, ephPriv, err = box.GenerateKey(crand.Reader)
  229. if err != nil {
  230. panic("Could not generate ephemeral key-pair")
  231. }
  232. return
  233. }
  234. func shareEphPubKey(conn io.ReadWriter, locEphPub *[32]byte) (remEphPub *[32]byte, err error) {
  235. // Send our pubkey and receive theirs in tandem.
  236. var trs, _ = cmn.Parallel(
  237. func(_ int) (val interface{}, err error, abort bool) {
  238. var _, err1 = cdc.MarshalBinaryLengthPrefixedWriter(conn, locEphPub)
  239. if err1 != nil {
  240. return nil, err1, true // abort
  241. }
  242. return nil, nil, false
  243. },
  244. func(_ int) (val interface{}, err error, abort bool) {
  245. var _remEphPub [32]byte
  246. var _, err2 = cdc.UnmarshalBinaryLengthPrefixedReader(conn, &_remEphPub, 1024*1024) // TODO
  247. if err2 != nil {
  248. return nil, err2, true // abort
  249. }
  250. if hasSmallOrder(_remEphPub) {
  251. return nil, ErrSmallOrderRemotePubKey, true
  252. }
  253. return _remEphPub, nil, false
  254. },
  255. )
  256. // If error:
  257. if trs.FirstError() != nil {
  258. err = trs.FirstError()
  259. return
  260. }
  261. // Otherwise:
  262. var _remEphPub = trs.FirstValue().([32]byte)
  263. return &_remEphPub, nil
  264. }
  265. // use the samne blacklist as lib sodium (see https://eprint.iacr.org/2017/806.pdf for reference):
  266. // https://github.com/jedisct1/libsodium/blob/536ed00d2c5e0c65ac01e29141d69a30455f2038/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.c#L11-L17
  267. var blacklist = [][32]byte{
  268. // 0 (order 4)
  269. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  270. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  271. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  272. // 1 (order 1)
  273. {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  274. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  275. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  276. // 325606250916557431795983626356110631294008115727848805560023387167927233504
  277. // (order 8)
  278. {0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae, 0x16, 0x56, 0xe3,
  279. 0xfa, 0xf1, 0x9f, 0xc4, 0x6a, 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32,
  280. 0xb1, 0xfd, 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00},
  281. // 39382357235489614581723060781553021112529911719440698176882885853963445705823
  282. // (order 8)
  283. {0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24, 0xb1, 0xd0, 0xb1,
  284. 0x55, 0x9c, 0x83, 0xef, 0x5b, 0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c,
  285. 0x8e, 0x86, 0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57},
  286. // p-1 (order 2)
  287. {0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  288. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  289. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
  290. // p (=0, order 4)
  291. {0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  292. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  293. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
  294. // p+1 (=1, order 1)
  295. {0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  296. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  297. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
  298. }
  299. func hasSmallOrder(pubKey [32]byte) bool {
  300. isSmallOrderPoint := false
  301. for _, bl := range blacklist {
  302. if subtle.ConstantTimeCompare(pubKey[:], bl[:]) == 1 {
  303. isSmallOrderPoint = true
  304. break
  305. }
  306. }
  307. return isSmallOrderPoint
  308. }
  309. func deriveSecretAndChallenge(
  310. dhSecret *[32]byte,
  311. locIsLeast bool,
  312. ) (recvSecret, sendSecret *[aeadKeySize]byte, challenge *[32]byte) {
  313. hash := sha256.New
  314. hkdf := hkdf.New(hash, dhSecret[:], nil, []byte("TENDERMINT_SECRET_CONNECTION_KEY_AND_CHALLENGE_GEN"))
  315. // get enough data for 2 aead keys, and a 32 byte challenge
  316. res := new([2*aeadKeySize + 32]byte)
  317. _, err := io.ReadFull(hkdf, res[:])
  318. if err != nil {
  319. panic(err)
  320. }
  321. challenge = new([32]byte)
  322. recvSecret = new([aeadKeySize]byte)
  323. sendSecret = new([aeadKeySize]byte)
  324. // Use the last 32 bytes as the challenge
  325. copy(challenge[:], res[2*aeadKeySize:2*aeadKeySize+32])
  326. // bytes 0 through aeadKeySize - 1 are one aead key.
  327. // bytes aeadKeySize through 2*aeadKeySize -1 are another aead key.
  328. // which key corresponds to sending and receiving key depends on whether
  329. // the local key is less than the remote key.
  330. if locIsLeast {
  331. copy(recvSecret[:], res[0:aeadKeySize])
  332. copy(sendSecret[:], res[aeadKeySize:aeadKeySize*2])
  333. } else {
  334. copy(sendSecret[:], res[0:aeadKeySize])
  335. copy(recvSecret[:], res[aeadKeySize:aeadKeySize*2])
  336. }
  337. return
  338. }
  339. // computeDHSecret computes a Diffie-Hellman shared secret key
  340. // from our own local private key and the other's public key.
  341. //
  342. // It returns an error if the computed shared secret is all zeroes.
  343. func computeDHSecret(remPubKey, locPrivKey *[32]byte) (shrKey *[32]byte, err error) {
  344. shrKey = new([32]byte)
  345. curve25519.ScalarMult(shrKey, locPrivKey, remPubKey)
  346. // reject if the returned shared secret is all zeroes
  347. // related to: https://github.com/tendermint/tendermint/issues/3010
  348. zero := new([32]byte)
  349. if subtle.ConstantTimeCompare(shrKey[:], zero[:]) == 1 {
  350. return nil, ErrSharedSecretIsZero
  351. }
  352. return
  353. }
  354. func sort32(foo, bar *[32]byte) (lo, hi *[32]byte) {
  355. if bytes.Compare(foo[:], bar[:]) < 0 {
  356. lo = foo
  357. hi = bar
  358. } else {
  359. lo = bar
  360. hi = foo
  361. }
  362. return
  363. }
  364. func signChallenge(challenge *[32]byte, locPrivKey crypto.PrivKey) (signature []byte) {
  365. signature, err := locPrivKey.Sign(challenge[:])
  366. // TODO(ismail): let signChallenge return an error instead
  367. if err != nil {
  368. panic(err)
  369. }
  370. return
  371. }
  372. type authSigMessage struct {
  373. Key crypto.PubKey
  374. Sig []byte
  375. }
  376. func shareAuthSignature(sc io.ReadWriter, pubKey crypto.PubKey, signature []byte) (recvMsg authSigMessage, err error) {
  377. // Send our info and receive theirs in tandem.
  378. var trs, _ = cmn.Parallel(
  379. func(_ int) (val interface{}, err error, abort bool) {
  380. var _, err1 = cdc.MarshalBinaryLengthPrefixedWriter(sc, authSigMessage{pubKey, signature})
  381. if err1 != nil {
  382. return nil, err1, true // abort
  383. }
  384. return nil, nil, false
  385. },
  386. func(_ int) (val interface{}, err error, abort bool) {
  387. var _recvMsg authSigMessage
  388. var _, err2 = cdc.UnmarshalBinaryLengthPrefixedReader(sc, &_recvMsg, 1024*1024) // TODO
  389. if err2 != nil {
  390. return nil, err2, true // abort
  391. }
  392. return _recvMsg, nil, false
  393. },
  394. )
  395. // If error:
  396. if trs.FirstError() != nil {
  397. err = trs.FirstError()
  398. return
  399. }
  400. var _recvMsg = trs.FirstValue().(authSigMessage)
  401. return _recvMsg, nil
  402. }
  403. //--------------------------------------------------------------------------------
  404. // Increment nonce little-endian by 1 with wraparound.
  405. // Due to chacha20poly1305 expecting a 12 byte nonce we do not use the first four
  406. // bytes. We only increment a 64 bit unsigned int in the remaining 8 bytes
  407. // (little-endian in nonce[4:]).
  408. func incrNonce(nonce *[aeadNonceSize]byte) {
  409. counter := binary.LittleEndian.Uint64(nonce[4:])
  410. if counter == math.MaxUint64 {
  411. // Terminates the session and makes sure the nonce would not re-used.
  412. // See https://github.com/tendermint/tendermint/issues/3531
  413. panic("can't increase nonce without overflow")
  414. }
  415. counter++
  416. binary.LittleEndian.PutUint64(nonce[4:], counter)
  417. }