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.

350 lines
9.3 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
  1. // Uses nacl's secret_box to encrypt a net.Conn.
  2. // It is (meant to be) an implementation of the STS protocol.
  3. // Note we do not (yet) assume that a remote peer's pubkey
  4. // is known ahead of time, and thus we are technically
  5. // still vulnerable to MITM. (TODO!)
  6. // See docs/sts-final.pdf for more info
  7. package conn
  8. import (
  9. "bytes"
  10. crand "crypto/rand"
  11. "crypto/sha256"
  12. "encoding/binary"
  13. "errors"
  14. "io"
  15. "net"
  16. "time"
  17. "golang.org/x/crypto/nacl/box"
  18. "golang.org/x/crypto/nacl/secretbox"
  19. "golang.org/x/crypto/ripemd160"
  20. "github.com/tendermint/go-crypto"
  21. cmn "github.com/tendermint/tmlibs/common"
  22. )
  23. // 4 + 1024 == 1028 total frame size
  24. const dataLenSize = 4
  25. const dataMaxSize = 1024
  26. const totalFrameSize = dataMaxSize + dataLenSize
  27. const sealedFrameSize = totalFrameSize + secretbox.Overhead
  28. // Implements net.Conn
  29. type SecretConnection struct {
  30. conn io.ReadWriteCloser
  31. recvBuffer []byte
  32. recvNonce *[24]byte
  33. sendNonce *[24]byte
  34. remPubKey crypto.PubKey
  35. shrSecret *[32]byte // shared secret
  36. }
  37. // Performs handshake and returns a new authenticated SecretConnection.
  38. // Returns nil if error in handshake.
  39. // Caller should call conn.Close()
  40. // See docs/sts-final.pdf for more information.
  41. func MakeSecretConnection(conn io.ReadWriteCloser, locPrivKey crypto.PrivKey) (*SecretConnection, error) {
  42. locPubKey := locPrivKey.PubKey()
  43. // Generate ephemeral keys for perfect forward secrecy.
  44. locEphPub, locEphPriv := genEphKeys()
  45. // Write local ephemeral pubkey and receive one too.
  46. // NOTE: every 32-byte string is accepted as a Curve25519 public key
  47. // (see DJB's Curve25519 paper: http://cr.yp.to/ecdh/curve25519-20060209.pdf)
  48. remEphPub, err := shareEphPubKey(conn, locEphPub)
  49. if err != nil {
  50. return nil, err
  51. }
  52. // Compute common shared secret.
  53. shrSecret := computeSharedSecret(remEphPub, locEphPriv)
  54. // Sort by lexical order.
  55. loEphPub, hiEphPub := sort32(locEphPub, remEphPub)
  56. // Check if the local ephemeral public key
  57. // was the least, lexicographically sorted.
  58. locIsLeast := bytes.Equal(locEphPub[:], loEphPub[:])
  59. // Generate nonces to use for secretbox.
  60. recvNonce, sendNonce := genNonces(loEphPub, hiEphPub, locIsLeast)
  61. // Generate common challenge to sign.
  62. challenge := genChallenge(loEphPub, hiEphPub)
  63. // Construct SecretConnection.
  64. sc := &SecretConnection{
  65. conn: conn,
  66. recvBuffer: nil,
  67. recvNonce: recvNonce,
  68. sendNonce: sendNonce,
  69. shrSecret: shrSecret,
  70. }
  71. // Sign the challenge bytes for authentication.
  72. locSignature := signChallenge(challenge, locPrivKey)
  73. // Share (in secret) each other's pubkey & challenge signature
  74. authSigMsg, err := shareAuthSignature(sc, locPubKey, locSignature)
  75. if err != nil {
  76. return nil, err
  77. }
  78. remPubKey, remSignature := authSigMsg.Key, authSigMsg.Sig
  79. if !remPubKey.VerifyBytes(challenge[:], remSignature) {
  80. return nil, errors.New("Challenge verification failed")
  81. }
  82. // We've authorized.
  83. sc.remPubKey = remPubKey
  84. return sc, nil
  85. }
  86. // Returns authenticated remote pubkey
  87. func (sc *SecretConnection) RemotePubKey() crypto.PubKey {
  88. return sc.remPubKey
  89. }
  90. // Writes encrypted frames of `sealedFrameSize`
  91. // CONTRACT: data smaller than dataMaxSize is read atomically.
  92. func (sc *SecretConnection) Write(data []byte) (n int, err error) {
  93. for 0 < len(data) {
  94. var frame []byte = make([]byte, totalFrameSize)
  95. var chunk []byte
  96. if dataMaxSize < len(data) {
  97. chunk = data[:dataMaxSize]
  98. data = data[dataMaxSize:]
  99. } else {
  100. chunk = data
  101. data = nil
  102. }
  103. chunkLength := len(chunk)
  104. binary.BigEndian.PutUint32(frame, uint32(chunkLength))
  105. copy(frame[dataLenSize:], chunk)
  106. // encrypt the frame
  107. var sealedFrame = make([]byte, sealedFrameSize)
  108. secretbox.Seal(sealedFrame[:0], frame, sc.sendNonce, sc.shrSecret)
  109. // fmt.Printf("secretbox.Seal(sealed:%X,sendNonce:%X,shrSecret:%X\n", sealedFrame, sc.sendNonce, sc.shrSecret)
  110. incr2Nonce(sc.sendNonce)
  111. // end encryption
  112. _, err := sc.conn.Write(sealedFrame)
  113. if err != nil {
  114. return n, err
  115. } else {
  116. n += len(chunk)
  117. }
  118. }
  119. return
  120. }
  121. // CONTRACT: data smaller than dataMaxSize is read atomically.
  122. func (sc *SecretConnection) Read(data []byte) (n int, err error) {
  123. if 0 < len(sc.recvBuffer) {
  124. n = copy(data, sc.recvBuffer)
  125. sc.recvBuffer = sc.recvBuffer[n:]
  126. return
  127. }
  128. sealedFrame := make([]byte, sealedFrameSize)
  129. _, err = io.ReadFull(sc.conn, sealedFrame)
  130. if err != nil {
  131. return
  132. }
  133. // decrypt the frame
  134. var frame = make([]byte, totalFrameSize)
  135. // fmt.Printf("secretbox.Open(sealed:%X,recvNonce:%X,shrSecret:%X\n", sealedFrame, sc.recvNonce, sc.shrSecret)
  136. _, ok := secretbox.Open(frame[:0], sealedFrame, sc.recvNonce, sc.shrSecret)
  137. if !ok {
  138. return n, errors.New("Failed to decrypt SecretConnection")
  139. }
  140. incr2Nonce(sc.recvNonce)
  141. // end decryption
  142. var chunkLength = binary.BigEndian.Uint32(frame) // read the first two bytes
  143. if chunkLength > dataMaxSize {
  144. return 0, errors.New("chunkLength is greater than dataMaxSize")
  145. }
  146. var chunk = frame[dataLenSize : dataLenSize+chunkLength]
  147. n = copy(data, chunk)
  148. sc.recvBuffer = chunk[n:]
  149. return
  150. }
  151. // Implements net.Conn
  152. func (sc *SecretConnection) Close() error { return sc.conn.Close() }
  153. func (sc *SecretConnection) LocalAddr() net.Addr { return sc.conn.(net.Conn).LocalAddr() }
  154. func (sc *SecretConnection) RemoteAddr() net.Addr { return sc.conn.(net.Conn).RemoteAddr() }
  155. func (sc *SecretConnection) SetDeadline(t time.Time) error { return sc.conn.(net.Conn).SetDeadline(t) }
  156. func (sc *SecretConnection) SetReadDeadline(t time.Time) error {
  157. return sc.conn.(net.Conn).SetReadDeadline(t)
  158. }
  159. func (sc *SecretConnection) SetWriteDeadline(t time.Time) error {
  160. return sc.conn.(net.Conn).SetWriteDeadline(t)
  161. }
  162. func genEphKeys() (ephPub, ephPriv *[32]byte) {
  163. var err error
  164. ephPub, ephPriv, err = box.GenerateKey(crand.Reader)
  165. if err != nil {
  166. panic("Could not generate ephemeral keypairs")
  167. }
  168. return
  169. }
  170. func shareEphPubKey(conn io.ReadWriteCloser, locEphPub *[32]byte) (remEphPub *[32]byte, err error) {
  171. // Send our pubkey and receive theirs in tandem.
  172. var trs, _ = cmn.Parallel(
  173. func(_ int) (val interface{}, err error, abort bool) {
  174. var _, err1 = cdc.MarshalBinaryWriter(conn, locEphPub)
  175. if err1 != nil {
  176. return nil, err1, true // abort
  177. } else {
  178. return nil, nil, false
  179. }
  180. },
  181. func(_ int) (val interface{}, err error, abort bool) {
  182. var _remEphPub [32]byte
  183. var _, err2 = cdc.UnmarshalBinaryReader(conn, &_remEphPub, 1024*1024) // TODO
  184. if err2 != nil {
  185. return nil, err2, true // abort
  186. } else {
  187. return _remEphPub, nil, false
  188. }
  189. },
  190. )
  191. // If error:
  192. if trs.FirstError() != nil {
  193. err = trs.FirstError()
  194. return
  195. }
  196. // Otherwise:
  197. var _remEphPub = trs.FirstValue().([32]byte)
  198. return &_remEphPub, nil
  199. }
  200. func computeSharedSecret(remPubKey, locPrivKey *[32]byte) (shrSecret *[32]byte) {
  201. shrSecret = new([32]byte)
  202. box.Precompute(shrSecret, remPubKey, locPrivKey)
  203. return
  204. }
  205. func sort32(foo, bar *[32]byte) (lo, hi *[32]byte) {
  206. if bytes.Compare(foo[:], bar[:]) < 0 {
  207. lo = foo
  208. hi = bar
  209. } else {
  210. lo = bar
  211. hi = foo
  212. }
  213. return
  214. }
  215. func genNonces(loPubKey, hiPubKey *[32]byte, locIsLo bool) (recvNonce, sendNonce *[24]byte) {
  216. nonce1 := hash24(append(loPubKey[:], hiPubKey[:]...))
  217. nonce2 := new([24]byte)
  218. copy(nonce2[:], nonce1[:])
  219. nonce2[len(nonce2)-1] ^= 0x01
  220. if locIsLo {
  221. recvNonce = nonce1
  222. sendNonce = nonce2
  223. } else {
  224. recvNonce = nonce2
  225. sendNonce = nonce1
  226. }
  227. return
  228. }
  229. func genChallenge(loPubKey, hiPubKey *[32]byte) (challenge *[32]byte) {
  230. return hash32(append(loPubKey[:], hiPubKey[:]...))
  231. }
  232. func signChallenge(challenge *[32]byte, locPrivKey crypto.PrivKey) (signature crypto.Signature) {
  233. signature = locPrivKey.Sign(challenge[:])
  234. return
  235. }
  236. type authSigMessage struct {
  237. Key crypto.PubKey
  238. Sig crypto.Signature
  239. }
  240. func shareAuthSignature(sc *SecretConnection, pubKey crypto.PubKey, signature crypto.Signature) (recvMsg authSigMessage, err error) {
  241. // Send our info and receive theirs in tandem.
  242. var trs, _ = cmn.Parallel(
  243. func(_ int) (val interface{}, err error, abort bool) {
  244. var _, err1 = cdc.MarshalBinaryWriter(sc, authSigMessage{pubKey, signature})
  245. if err1 != nil {
  246. return nil, err1, true // abort
  247. } else {
  248. return nil, nil, false
  249. }
  250. },
  251. func(_ int) (val interface{}, err error, abort bool) {
  252. var _recvMsg authSigMessage
  253. var _, err2 = cdc.UnmarshalBinaryReader(sc, &_recvMsg, 1024*1024) // TODO
  254. if err2 != nil {
  255. return nil, err2, true // abort
  256. } else {
  257. return _recvMsg, nil, false
  258. }
  259. },
  260. )
  261. // If error:
  262. if trs.FirstError() != nil {
  263. err = trs.FirstError()
  264. return
  265. }
  266. var _recvMsg = trs.FirstValue().(authSigMessage)
  267. return _recvMsg, nil
  268. }
  269. //--------------------------------------------------------------------------------
  270. // sha256
  271. func hash32(input []byte) (res *[32]byte) {
  272. hasher := sha256.New()
  273. hasher.Write(input) // nolint: errcheck, gas
  274. resSlice := hasher.Sum(nil)
  275. res = new([32]byte)
  276. copy(res[:], resSlice)
  277. return
  278. }
  279. // We only fill in the first 20 bytes with ripemd160
  280. func hash24(input []byte) (res *[24]byte) {
  281. hasher := ripemd160.New()
  282. hasher.Write(input) // nolint: errcheck, gas
  283. resSlice := hasher.Sum(nil)
  284. res = new([24]byte)
  285. copy(res[:], resSlice)
  286. return
  287. }
  288. // increment nonce big-endian by 2 with wraparound.
  289. func incr2Nonce(nonce *[24]byte) {
  290. incrNonce(nonce)
  291. incrNonce(nonce)
  292. }
  293. // increment nonce big-endian by 1 with wraparound.
  294. func incrNonce(nonce *[24]byte) {
  295. for i := 23; 0 <= i; i-- {
  296. nonce[i] += 1
  297. if nonce[i] != 0 {
  298. return
  299. }
  300. }
  301. }