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.

444 lines
14 KiB

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