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.

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