2018-04-07 20:04:10 +00:00
|
|
|
// hkexnet.go - net.Conn compatible channel setup with encrypted/HMAC
|
|
|
|
// negotiation
|
|
|
|
|
|
|
|
// Copyright (c) 2017-2018 Russell Magee
|
|
|
|
// Licensed under the terms of the MIT license (see LICENSE.mit in this
|
|
|
|
// distribution)
|
|
|
|
//
|
|
|
|
// golang implementation by Russ Magee (rmagee_at_gmail.com)
|
2018-01-11 21:44:11 +00:00
|
|
|
|
2018-04-04 22:43:27 +00:00
|
|
|
package hkexsh
|
2018-01-09 03:16:55 +00:00
|
|
|
|
2018-01-09 07:08:58 +00:00
|
|
|
// Implementation of HKEx-wrapped versions of the golang standard
|
|
|
|
// net package interfaces, allowing clients and servers to simply replace
|
2018-01-11 21:44:11 +00:00
|
|
|
// 'net.Dial' and 'net.Listen' with 'hkex.Dial' and 'hkex.Listen'.
|
2018-01-09 03:16:55 +00:00
|
|
|
import (
|
2018-01-11 06:50:13 +00:00
|
|
|
"bytes"
|
2018-01-09 07:26:24 +00:00
|
|
|
"crypto/cipher"
|
2018-03-26 02:58:04 +00:00
|
|
|
"encoding/binary"
|
2018-02-17 02:43:37 +00:00
|
|
|
"encoding/hex"
|
2018-01-09 03:16:55 +00:00
|
|
|
"fmt"
|
2018-02-17 02:43:37 +00:00
|
|
|
"hash"
|
2018-03-26 02:58:04 +00:00
|
|
|
"io"
|
2018-01-18 05:27:00 +00:00
|
|
|
"log"
|
2018-01-09 03:16:55 +00:00
|
|
|
"math/big"
|
|
|
|
"net"
|
2018-02-17 02:46:29 +00:00
|
|
|
"strings"
|
2018-01-17 02:30:57 +00:00
|
|
|
"time"
|
2018-01-09 03:16:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------*/
|
|
|
|
|
2018-01-13 06:13:01 +00:00
|
|
|
// Conn is a HKex connection - a drop-in replacement for net.Conn
|
2018-01-09 03:16:55 +00:00
|
|
|
type Conn struct {
|
2018-01-12 03:42:42 +00:00
|
|
|
c net.Conn // which also implements io.Reader, io.Writer, ...
|
|
|
|
h *HerraduraKEx
|
2018-02-17 02:43:37 +00:00
|
|
|
cipheropts uint32 // post-KEx cipher/hmac options
|
|
|
|
opts uint32 // post-KEx protocol options (caller-defined)
|
|
|
|
r cipher.Stream //read cipherStream
|
|
|
|
rm hash.Hash
|
|
|
|
w cipher.Stream //write cipherStream
|
|
|
|
wm hash.Hash
|
2018-03-26 02:58:04 +00:00
|
|
|
dBuf *bytes.Buffer //decrypt buffer for Read()
|
2018-02-17 03:25:11 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 06:13:01 +00:00
|
|
|
// ConnOpts returns the cipher/hmac options value, which is sent to the
|
|
|
|
// peer but is not itself part of the KEx.
|
|
|
|
//
|
2018-01-12 03:42:42 +00:00
|
|
|
// (Used for protocol-level negotiations after KEx such as
|
|
|
|
// cipher/HMAC algorithm options etc.)
|
2018-01-18 00:39:01 +00:00
|
|
|
func (c Conn) ConnOpts() uint32 {
|
2018-01-12 03:42:42 +00:00
|
|
|
return c.cipheropts
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:13:01 +00:00
|
|
|
// SetConnOpts sets the cipher/hmac options value, which is sent to the
|
|
|
|
// peer as part of KEx but not part of the KEx itself.
|
2018-01-12 03:42:42 +00:00
|
|
|
//
|
|
|
|
// opts - bitfields for cipher and hmac alg. to use after KEx
|
2018-01-21 04:37:27 +00:00
|
|
|
func (c *Conn) SetConnOpts(copts uint32) {
|
2018-01-12 03:42:42 +00:00
|
|
|
c.cipheropts = copts
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:13:01 +00:00
|
|
|
// Opts returns the protocol options value, which is sent to the peer
|
|
|
|
// but is not itself part of the KEx or connection (cipher/hmac) setup.
|
2018-01-12 03:42:42 +00:00
|
|
|
//
|
|
|
|
// Consumers of this lib may use this for protocol-level options not part
|
|
|
|
// of the KEx or encryption info used by the connection.
|
2018-01-18 00:39:01 +00:00
|
|
|
func (c Conn) Opts() uint32 {
|
2018-01-12 03:42:42 +00:00
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:13:01 +00:00
|
|
|
// SetOpts sets the protocol options value, which is sent to the peer
|
|
|
|
// but is not itself part of the KEx or connection (cipher/hmac) setup.
|
|
|
|
//
|
2018-01-12 03:42:42 +00:00
|
|
|
// Consumers of this lib may use this for protocol-level options not part
|
|
|
|
// of the KEx of encryption info used by the connection.
|
|
|
|
//
|
|
|
|
// opts - a uint32, caller-defined
|
2018-01-21 04:37:27 +00:00
|
|
|
func (c *Conn) SetOpts(opts uint32) {
|
2018-01-12 03:42:42 +00:00
|
|
|
c.opts = opts
|
|
|
|
}
|
|
|
|
|
2018-01-21 04:37:27 +00:00
|
|
|
func (c *Conn) applyConnExtensions(extensions ...string) {
|
2018-01-12 03:42:42 +00:00
|
|
|
for _, s := range extensions {
|
|
|
|
switch s {
|
|
|
|
case "C_AES_256":
|
2018-01-18 05:27:00 +00:00
|
|
|
log.Println("[extension arg = C_AES_256]")
|
2018-01-13 06:13:01 +00:00
|
|
|
c.cipheropts &= (0xFFFFFF00)
|
|
|
|
c.cipheropts |= CAlgAES256
|
2018-01-12 03:42:42 +00:00
|
|
|
break
|
|
|
|
case "C_TWOFISH_128":
|
2018-01-18 05:27:00 +00:00
|
|
|
log.Println("[extension arg = C_TWOFISH_128]")
|
2018-01-13 06:13:01 +00:00
|
|
|
c.cipheropts &= (0xFFFFFF00)
|
|
|
|
c.cipheropts |= CAlgTwofish128
|
2018-01-12 03:42:42 +00:00
|
|
|
break
|
2018-01-12 07:01:39 +00:00
|
|
|
case "C_BLOWFISH_64":
|
2018-01-18 05:27:00 +00:00
|
|
|
log.Println("[extension arg = C_BLOWFISH_64]")
|
2018-01-13 06:13:01 +00:00
|
|
|
c.cipheropts &= (0xFFFFFF00)
|
|
|
|
c.cipheropts |= CAlgBlowfish64
|
2018-01-12 07:01:39 +00:00
|
|
|
break
|
2018-01-12 03:42:42 +00:00
|
|
|
case "H_SHA256":
|
2018-01-18 05:27:00 +00:00
|
|
|
log.Println("[extension arg = H_SHA256]")
|
2018-01-13 06:13:01 +00:00
|
|
|
c.cipheropts &= (0xFFFF00FF)
|
|
|
|
c.cipheropts |= (HmacSHA256 << 8)
|
2018-01-12 03:42:42 +00:00
|
|
|
break
|
|
|
|
default:
|
2018-01-18 05:27:00 +00:00
|
|
|
log.Printf("[Dial ext \"%s\" ignored]\n", s)
|
2018-01-12 03:42:42 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dial as net.Dial(), but with implicit HKEx PeerD read on connect
|
2018-01-12 03:42:42 +00:00
|
|
|
// Can be called like net.Dial(), defaulting to C_AES_256/H_SHA256,
|
|
|
|
// or additional option arguments can be passed amongst the following:
|
|
|
|
//
|
|
|
|
// "C_AES_256" | "C_TWOFISH_128"
|
|
|
|
//
|
|
|
|
// "H_SHA256"
|
|
|
|
func Dial(protocol string, ipport string, extensions ...string) (hc *Conn, err error) {
|
2018-03-25 17:40:23 +00:00
|
|
|
// Open raw Conn c
|
2018-01-09 03:16:55 +00:00
|
|
|
c, err := net.Dial(protocol, ipport)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-25 17:40:23 +00:00
|
|
|
// Init hkexnet.Conn hc over net.Conn c
|
2018-03-26 02:58:04 +00:00
|
|
|
hc = &Conn{c: c, h: New(0, 0), dBuf: new(bytes.Buffer)}
|
2018-01-12 03:42:42 +00:00
|
|
|
hc.applyConnExtensions(extensions...)
|
2018-01-09 03:16:55 +00:00
|
|
|
|
2018-03-25 17:40:23 +00:00
|
|
|
// Send hkexnet.Conn parameters to remote side
|
|
|
|
// d is value for Herradura key exchange
|
2018-01-21 04:37:27 +00:00
|
|
|
fmt.Fprintf(c, "0x%s\n%08x:%08x\n", hc.h.d.Text(16),
|
|
|
|
hc.cipheropts, hc.opts)
|
2018-01-09 03:16:55 +00:00
|
|
|
|
|
|
|
d := big.NewInt(0)
|
|
|
|
_, err = fmt.Fscanln(c, d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-21 04:37:27 +00:00
|
|
|
_, err = fmt.Fscanf(c, "%08x:%08x\n",
|
|
|
|
&hc.cipheropts, &hc.opts)
|
2018-01-12 03:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-09 03:16:55 +00:00
|
|
|
hc.h.PeerD = d
|
2018-01-18 05:27:00 +00:00
|
|
|
log.Printf("** D:%s\n", hc.h.d.Text(16))
|
|
|
|
log.Printf("**(c)** peerD:%s\n", hc.h.PeerD.Text(16))
|
2018-01-09 03:16:55 +00:00
|
|
|
hc.h.FA()
|
2018-01-18 05:27:00 +00:00
|
|
|
log.Printf("**(c)** FA:%s\n", hc.h.fa)
|
2018-01-09 07:26:24 +00:00
|
|
|
|
2018-02-17 02:43:37 +00:00
|
|
|
hc.r, hc.rm = hc.getStream(hc.h.fa)
|
|
|
|
hc.w, hc.wm = hc.getStream(hc.h.fa)
|
2018-01-09 03:16:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:44:11 +00:00
|
|
|
// Close a hkex.Conn
|
2018-01-18 00:39:01 +00:00
|
|
|
func (c Conn) Close() (err error) {
|
2018-01-13 06:13:01 +00:00
|
|
|
err = c.c.Close()
|
2018-01-19 02:57:37 +00:00
|
|
|
log.Println("[Conn Closing]")
|
2018-01-09 03:16:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-18 00:39:01 +00:00
|
|
|
// LocalAddr returns the local network address.
|
|
|
|
func (c Conn) LocalAddr() net.Addr {
|
|
|
|
return c.c.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address.
|
|
|
|
func (c Conn) RemoteAddr() net.Addr {
|
|
|
|
return c.c.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDeadline sets the read and write deadlines associated
|
|
|
|
// with the connection. It is equivalent to calling both
|
|
|
|
// SetReadDeadline and SetWriteDeadline.
|
|
|
|
//
|
|
|
|
// A deadline is an absolute time after which I/O operations
|
|
|
|
// fail with a timeout (see type Error) instead of
|
|
|
|
// blocking. The deadline applies to all future and pending
|
|
|
|
// I/O, not just the immediately following call to Read or
|
|
|
|
// Write. After a deadline has been exceeded, the connection
|
|
|
|
// can be refreshed by setting a deadline in the future.
|
|
|
|
//
|
|
|
|
// An idle timeout can be implemented by repeatedly extending
|
|
|
|
// the deadline after successful Read or Write calls.
|
|
|
|
//
|
|
|
|
// A zero value for t means I/O operations will not time out.
|
|
|
|
func (c Conn) SetDeadline(t time.Time) error {
|
|
|
|
return c.SetDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteDeadline sets the deadline for future Write calls
|
|
|
|
// and any currently-blocked Write call.
|
|
|
|
// Even if write times out, it may return n > 0, indicating that
|
|
|
|
// some of the data was successfully written.
|
|
|
|
// A zero value for t means Write will not time out.
|
|
|
|
func (c Conn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return c.SetWriteDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReadDeadline sets the deadline for future Read calls
|
|
|
|
// and any currently-blocked Read call.
|
|
|
|
// A zero value for t means Read will not time out.
|
|
|
|
func (c Conn) SetReadDeadline(t time.Time) error {
|
|
|
|
return c.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
|
2018-01-09 03:16:55 +00:00
|
|
|
/*---------------------------------------------------------------------*/
|
|
|
|
|
2018-01-13 06:13:01 +00:00
|
|
|
// HKExListener is a Listener conforming to net.Listener
|
|
|
|
//
|
|
|
|
// See go doc net.Listener
|
2018-01-09 03:16:55 +00:00
|
|
|
type HKExListener struct {
|
|
|
|
l net.Listener
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:13:01 +00:00
|
|
|
// Listen for a connection
|
|
|
|
//
|
|
|
|
// See go doc net.Listen
|
2018-01-09 03:16:55 +00:00
|
|
|
func Listen(protocol string, ipport string) (hl HKExListener, e error) {
|
|
|
|
l, err := net.Listen(protocol, ipport)
|
|
|
|
if err != nil {
|
|
|
|
return HKExListener{nil}, err
|
|
|
|
}
|
2018-01-19 02:57:37 +00:00
|
|
|
log.Println("[Listening]")
|
2018-01-09 03:16:55 +00:00
|
|
|
hl.l = l
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-27 00:15:39 +00:00
|
|
|
// Close a hkex Listener - closes the Listener.
|
|
|
|
// Any blocked Accept operations will be unblocked and return errors.
|
2018-01-13 06:13:01 +00:00
|
|
|
//
|
2018-01-27 00:15:39 +00:00
|
|
|
// See go doc net.Listener.Close
|
2018-01-18 00:39:01 +00:00
|
|
|
func (hl HKExListener) Close() error {
|
2018-01-19 02:57:37 +00:00
|
|
|
log.Println("[Listener Closed]")
|
2018-01-13 06:13:01 +00:00
|
|
|
return hl.l.Close()
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 00:15:39 +00:00
|
|
|
// Addr returns a the listener's network address.
|
|
|
|
//
|
|
|
|
// See go doc net.Listener.Addr
|
|
|
|
func (hl HKExListener) Addr() net.Addr {
|
2018-02-17 02:43:37 +00:00
|
|
|
return hl.l.Addr()
|
2018-01-27 00:15:39 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 21:44:11 +00:00
|
|
|
// Accept a client connection, conforming to net.Listener.Accept()
|
2018-01-13 06:13:01 +00:00
|
|
|
//
|
|
|
|
// See go doc net.Listener.Accept
|
2018-01-18 00:39:01 +00:00
|
|
|
func (hl HKExListener) Accept() (hc Conn, err error) {
|
2018-03-25 17:40:23 +00:00
|
|
|
// Open raw Conn c
|
2018-01-09 03:16:55 +00:00
|
|
|
c, err := hl.l.Accept()
|
|
|
|
if err != nil {
|
2018-01-12 03:42:42 +00:00
|
|
|
return Conn{c: nil, h: nil, cipheropts: 0, opts: 0,
|
|
|
|
r: nil, w: nil}, err
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
2018-01-19 02:57:37 +00:00
|
|
|
log.Println("[Accepted]")
|
2018-01-12 03:42:42 +00:00
|
|
|
|
2018-03-26 02:58:04 +00:00
|
|
|
hc = Conn{c: c, h: New(0, 0), dBuf: new(bytes.Buffer)}
|
2018-01-09 03:16:55 +00:00
|
|
|
|
2018-03-25 17:40:23 +00:00
|
|
|
// Read in hkexnet.Conn parameters over raw Conn c
|
|
|
|
// d is value for Herradura key exchange
|
2018-01-09 03:16:55 +00:00
|
|
|
d := big.NewInt(0)
|
|
|
|
_, err = fmt.Fscanln(c, d)
|
|
|
|
if err != nil {
|
2018-01-12 03:42:42 +00:00
|
|
|
return hc, err
|
|
|
|
}
|
2018-01-21 04:37:27 +00:00
|
|
|
_, err = fmt.Fscanf(c, "%08x:%08x\n",
|
|
|
|
&hc.cipheropts, &hc.opts)
|
2018-01-12 03:42:42 +00:00
|
|
|
if err != nil {
|
2018-01-09 03:16:55 +00:00
|
|
|
return hc, err
|
|
|
|
}
|
|
|
|
hc.h.PeerD = d
|
2018-01-18 05:27:00 +00:00
|
|
|
log.Printf("** D:%s\n", hc.h.d.Text(16))
|
|
|
|
log.Printf("**(s)** peerD:%s\n", hc.h.PeerD.Text(16))
|
2018-01-09 03:16:55 +00:00
|
|
|
hc.h.FA()
|
2018-01-18 05:27:00 +00:00
|
|
|
log.Printf("**(s)** FA:%s\n", hc.h.fa)
|
2018-01-09 03:16:55 +00:00
|
|
|
|
2018-01-21 04:37:27 +00:00
|
|
|
fmt.Fprintf(c, "0x%s\n%08x:%08x\n", hc.h.d.Text(16),
|
|
|
|
hc.cipheropts, hc.opts)
|
2018-01-09 03:16:55 +00:00
|
|
|
|
2018-02-17 02:43:37 +00:00
|
|
|
hc.r, hc.rm = hc.getStream(hc.h.fa)
|
|
|
|
hc.w, hc.wm = hc.getStream(hc.h.fa)
|
2018-01-09 03:16:55 +00:00
|
|
|
return
|
|
|
|
}
|
2018-01-17 02:30:57 +00:00
|
|
|
|
2018-01-09 03:16:55 +00:00
|
|
|
/*---------------------------------------------------------------------*/
|
2018-01-13 06:13:01 +00:00
|
|
|
|
|
|
|
// Read into a byte slice
|
|
|
|
//
|
|
|
|
// See go doc io.Reader
|
|
|
|
func (c Conn) Read(b []byte) (n int, err error) {
|
2018-02-17 02:46:29 +00:00
|
|
|
//log.Printf("[Decrypting...]\r\n")
|
2018-03-26 04:47:38 +00:00
|
|
|
log.Printf("Read() requests %d bytes\n", len(b))
|
2018-03-26 06:00:37 +00:00
|
|
|
|
2018-03-26 02:58:04 +00:00
|
|
|
for {
|
2018-03-26 02:59:07 +00:00
|
|
|
//log.Printf("c.dBuf.Len(): %d\n", c.dBuf.Len())
|
2018-03-26 04:47:38 +00:00
|
|
|
if c.dBuf.Len() > 0 /* len(b) */ {
|
2018-03-26 02:58:04 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var hmacIn uint8
|
|
|
|
var payloadLen uint32
|
|
|
|
|
|
|
|
// Read the hmac LSB and payload len first
|
|
|
|
err = binary.Read(c.c, binary.BigEndian, &hmacIn)
|
2018-03-26 06:00:37 +00:00
|
|
|
// Normal client 'exit' from interactive session will cause
|
|
|
|
// (on server side) err.Error() == "<iface/addr info ...>: use of closed network connection"
|
2018-03-27 04:58:42 +00:00
|
|
|
if err != nil {
|
2018-03-26 06:00:37 +00:00
|
|
|
if !strings.HasSuffix(err.Error(), "use of closed network connection") {
|
|
|
|
log.Println("unexpected Read() err:", err)
|
2018-03-26 04:47:38 +00:00
|
|
|
} else {
|
2018-03-26 06:00:37 +00:00
|
|
|
log.Println("[Client hung up]")
|
2018-03-26 04:47:38 +00:00
|
|
|
}
|
2018-03-27 04:58:42 +00:00
|
|
|
return 0, err
|
2018-03-26 02:58:04 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 06:00:37 +00:00
|
|
|
//if err != nil {
|
|
|
|
// if err.Error() != "EOF" {
|
|
|
|
// log.Println("Error was:", err.Error())
|
|
|
|
// } else {
|
|
|
|
// return 0, err
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2018-03-26 02:58:04 +00:00
|
|
|
err = binary.Read(c.c, binary.BigEndian, &payloadLen)
|
|
|
|
if err != nil {
|
2018-03-26 06:00:37 +00:00
|
|
|
if err.Error() != "EOF" {
|
|
|
|
panic(err)
|
|
|
|
} // else {
|
|
|
|
// return 0, err
|
|
|
|
//}
|
2018-03-26 02:58:04 +00:00
|
|
|
}
|
|
|
|
if payloadLen > 16384 {
|
|
|
|
panic("Insane payloadLen")
|
|
|
|
}
|
2018-03-26 02:59:07 +00:00
|
|
|
//log.Println("payloadLen:", payloadLen)
|
2018-03-26 02:58:04 +00:00
|
|
|
var payloadBytes = make([]byte, payloadLen)
|
|
|
|
n, err = io.ReadFull(c.c, payloadBytes)
|
2018-03-26 02:59:07 +00:00
|
|
|
//log.Print(" << Read ", n, " payloadBytes")
|
2018-03-26 02:58:04 +00:00
|
|
|
|
|
|
|
// Normal client 'exit' from interactive session will cause
|
|
|
|
// (on server side) err.Error() == "<iface/addr info ...>: use of closed network connection"
|
|
|
|
if err != nil && err.Error() != "EOF" {
|
|
|
|
if !strings.HasSuffix(err.Error(), "use of closed network connection") {
|
|
|
|
log.Println("unexpected Read() err:", err)
|
|
|
|
} else {
|
|
|
|
log.Println("[Client hung up]")
|
|
|
|
}
|
2018-02-17 02:46:29 +00:00
|
|
|
}
|
2018-02-17 06:12:27 +00:00
|
|
|
|
2018-03-26 02:58:04 +00:00
|
|
|
log.Printf(" <:ctext:\r\n%s\r\n", hex.Dump(payloadBytes[:n])) //EncodeToString(b[:n])) // print only used portion
|
|
|
|
|
|
|
|
db := bytes.NewBuffer(payloadBytes[:n]) //copying payloadBytes to db
|
|
|
|
// The StreamReader acts like a pipe, decrypting
|
|
|
|
// whatever is available and forwarding the result
|
|
|
|
// to the parameter of Read() as a normal io.Reader
|
|
|
|
rs := &cipher.StreamReader{S: c.r, R: db}
|
|
|
|
// The caller isn't necessarily reading the full payload so we need
|
|
|
|
// to decrypt ot an intermediate buffer, draining it on demand of caller
|
|
|
|
decryptN, err := rs.Read(payloadBytes)
|
|
|
|
log.Printf(" <-ptext:\r\n%s\r\n", hex.Dump(payloadBytes[:n])) //EncodeToString(b[:n]))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
c.dBuf.Write(payloadBytes)
|
2018-03-26 02:59:07 +00:00
|
|
|
//log.Printf("c.dBuf: %s\n", hex.Dump(c.dBuf.Bytes()))
|
2018-03-26 02:58:04 +00:00
|
|
|
|
|
|
|
// Re-calculate hmac, compare with received value
|
|
|
|
c.rm.Write(payloadBytes)
|
2018-02-17 06:12:27 +00:00
|
|
|
hTmp := c.rm.Sum(nil)[0]
|
2018-03-26 02:58:04 +00:00
|
|
|
log.Printf("<%04x) HMAC:(i)%02x (c)%02x\r\n", decryptN, hmacIn, hTmp)
|
2018-02-17 03:25:11 +00:00
|
|
|
}
|
2018-03-26 04:47:38 +00:00
|
|
|
retN := c.dBuf.Len()
|
|
|
|
if retN > len(b) {
|
|
|
|
retN = len(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Read() got %d bytes\n", retN)
|
|
|
|
copy(b, c.dBuf.Next(retN))
|
2018-03-26 02:59:07 +00:00
|
|
|
//log.Printf("As Read() returns, c.dBuf is %d long: %s\n", c.dBuf.Len(), hex.Dump(c.dBuf.Bytes()))
|
2018-03-26 04:47:38 +00:00
|
|
|
return retN, nil
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 06:13:01 +00:00
|
|
|
// Write a byte slice
|
|
|
|
//
|
|
|
|
// See go doc io.Writer
|
|
|
|
func (c Conn) Write(b []byte) (n int, err error) {
|
2018-02-17 02:46:29 +00:00
|
|
|
//log.Printf("[Encrypting...]\r\n")
|
2018-03-26 02:58:04 +00:00
|
|
|
var hmacOut uint8
|
|
|
|
var payloadLen uint32
|
2018-02-17 06:12:27 +00:00
|
|
|
|
2018-02-17 02:46:29 +00:00
|
|
|
log.Printf(" :>ptext:\r\n%s\r\n", hex.Dump(b)) //EncodeToString(b))
|
2018-02-17 03:25:11 +00:00
|
|
|
|
2018-03-26 02:58:04 +00:00
|
|
|
payloadLen = uint32(len(b))
|
|
|
|
|
|
|
|
// Calculate hmac on payload
|
|
|
|
c.wm.Write(b)
|
|
|
|
hmacOut = uint8(c.wm.Sum(nil)[0])
|
|
|
|
|
|
|
|
log.Printf(" (%04x> HMAC(o):%02x\r\n", payloadLen, hmacOut)
|
2018-02-17 03:25:11 +00:00
|
|
|
|
2018-01-11 06:50:13 +00:00
|
|
|
var wb bytes.Buffer
|
2018-01-11 17:13:18 +00:00
|
|
|
// The StreamWriter acts like a pipe, forwarding whatever is
|
|
|
|
// written to it through the cipher, encrypting as it goes
|
2018-01-13 06:13:01 +00:00
|
|
|
ws := &cipher.StreamWriter{S: c.w, W: &wb}
|
|
|
|
_, err = ws.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-26 02:58:04 +00:00
|
|
|
log.Printf(" ->ctext:\r\n%s\r\n", hex.Dump(wb.Bytes()))
|
|
|
|
|
|
|
|
// Write hmac LSB, payloadLen followed by payload
|
|
|
|
_ = binary.Write(c.c, binary.BigEndian, hmacOut)
|
|
|
|
_ = binary.Write(c.c, binary.BigEndian, payloadLen)
|
2018-02-17 06:12:27 +00:00
|
|
|
|
2018-01-13 06:13:01 +00:00
|
|
|
n, err = c.c.Write(wb.Bytes())
|
2018-02-17 06:12:27 +00:00
|
|
|
if err != nil {
|
2018-03-27 04:58:42 +00:00
|
|
|
//panic(err)
|
|
|
|
log.Println(err)
|
2018-02-17 06:12:27 +00:00
|
|
|
}
|
2018-01-09 03:16:55 +00:00
|
|
|
return
|
|
|
|
}
|