2018-01-09 07:08:58 +00:00
|
|
|
/* Herradura - a Key exchange scheme in the style of Diffie-Hellman Key Exchange.
|
|
|
|
Copyright (C) 2017 Omar Alejandro Herrera Reyna
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
golang implementation by Russ Magee (rmagee_at_gmail.com) */
|
2018-01-11 21:44:11 +00:00
|
|
|
|
2018-01-09 03:16:55 +00:00
|
|
|
package herradurakex
|
|
|
|
|
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-01-09 03:16:55 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------*/
|
|
|
|
|
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
|
|
|
|
cipheropts uint32 // post-KEx cipher/hmac options
|
2018-01-13 18:01:27 +00:00
|
|
|
opts uint32 // post-KEx protocol options (caller-defined)
|
|
|
|
op uint8 // post-KEx 'op' (caller-defined)
|
2018-01-12 03:42:42 +00:00
|
|
|
r cipher.Stream
|
|
|
|
w cipher.Stream
|
|
|
|
}
|
|
|
|
|
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.)
|
|
|
|
func (c *Conn) ConnOpts() uint32 {
|
|
|
|
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
|
|
|
|
func (c *Conn) SetConnOpts(copts uint32) {
|
|
|
|
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.
|
|
|
|
func (c *Conn) Opts() uint32 {
|
|
|
|
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
|
|
|
|
func (c *Conn) SetOpts(opts uint32) {
|
|
|
|
c.opts = opts
|
|
|
|
}
|
|
|
|
|
2018-01-13 18:01:27 +00:00
|
|
|
// Op returns the 'op' value, which is sent to the peer
|
|
|
|
// but is not itself part of the KEx or connection (cipher/hmac) setup.
|
|
|
|
//
|
|
|
|
// Consumers of this lib may use this to indicate connection-specific
|
|
|
|
// operations not part of the KEx or encryption info used by the connection.
|
|
|
|
func (c *Conn) Op() uint8 {
|
|
|
|
return c.op
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOp sets the 'op' value, which is sent to the peer
|
|
|
|
// but is not itself part of the KEx or connection (cipher/hmac) setup.
|
|
|
|
//
|
|
|
|
// Consumers of this lib may use this to indicate connection-specific
|
|
|
|
// operations not part of the KEx or encryption info used by the connection.
|
|
|
|
//
|
|
|
|
// op - a uint8, caller-defined
|
|
|
|
func (c *Conn) SetOp(op uint8) {
|
|
|
|
c.op = op
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:13:01 +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-13 06:13:01 +00:00
|
|
|
fmt.Println("[extension arg = C_AES_256]")
|
|
|
|
c.cipheropts &= (0xFFFFFF00)
|
|
|
|
c.cipheropts |= CAlgAES256
|
2018-01-12 03:42:42 +00:00
|
|
|
break
|
|
|
|
case "C_TWOFISH_128":
|
|
|
|
fmt.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":
|
|
|
|
fmt.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-13 06:13:01 +00:00
|
|
|
fmt.Println("[extension arg = H_SHA256]")
|
|
|
|
c.cipheropts &= (0xFFFF00FF)
|
|
|
|
c.cipheropts |= (HmacSHA256 << 8)
|
2018-01-12 03:42:42 +00:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
fmt.Printf("[Dial ext \"%s\" ignored]\n", s)
|
|
|
|
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-01-09 03:16:55 +00:00
|
|
|
c, err := net.Dial(protocol, ipport)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-13 18:01:27 +00:00
|
|
|
hc = &Conn{c: c, h: New(0, 0), cipheropts: 0, opts: 0, op:0, r: nil, w: nil}
|
2018-01-12 03:42:42 +00:00
|
|
|
|
|
|
|
hc.applyConnExtensions(extensions...)
|
2018-01-09 03:16:55 +00:00
|
|
|
|
2018-01-13 18:01:27 +00:00
|
|
|
fmt.Fprintf(c, "0x%s\n%08x:%08x:%02x\n", hc.h.d.Text(16),
|
|
|
|
hc.cipheropts, hc.opts, hc.op)
|
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-13 18:01:27 +00:00
|
|
|
_, err = fmt.Fscanf(c, "%08x:%08x:%02x\n",
|
|
|
|
&hc.cipheropts, &hc.opts, &hc.op)
|
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
|
|
|
|
fmt.Printf("** D:%s\n", hc.h.d.Text(16))
|
|
|
|
fmt.Printf("**(c)** peerD:%s\n", hc.h.PeerD.Text(16))
|
|
|
|
hc.h.FA()
|
|
|
|
fmt.Printf("**(c)** FA:%s\n", hc.h.fa)
|
2018-01-09 07:26:24 +00:00
|
|
|
|
2018-01-12 03:42:42 +00:00
|
|
|
hc.r = hc.getStream(hc.h.fa)
|
|
|
|
hc.w = 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-13 06:13:01 +00:00
|
|
|
func (c *Conn) Close() (err error) {
|
|
|
|
err = c.c.Close()
|
2018-01-09 03:16:55 +00:00
|
|
|
fmt.Println("[Conn Closing]")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------*/
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
fmt.Println("[Listening]")
|
|
|
|
hl.l = l
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:44:11 +00:00
|
|
|
// Close a hkex Listener
|
2018-01-13 06:13:01 +00:00
|
|
|
//
|
|
|
|
// See go doc io.Close
|
|
|
|
func (hl *HKExListener) Close() error {
|
2018-01-09 03:16:55 +00:00
|
|
|
fmt.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-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-09 03:16:55 +00:00
|
|
|
func (hl *HKExListener) Accept() (hc Conn, err error) {
|
|
|
|
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-12 03:42:42 +00:00
|
|
|
fmt.Println("[Accepted]")
|
|
|
|
|
2018-01-13 18:01:27 +00:00
|
|
|
hc = Conn{c: c, h: New(0, 0), cipheropts: 0, opts: 0, op:0, r: nil, w: nil}
|
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-13 18:01:27 +00:00
|
|
|
_, err = fmt.Fscanf(c, "%08x:%08x:%02x\n",
|
|
|
|
&hc.cipheropts, &hc.opts, &hc.op)
|
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
|
|
|
|
fmt.Printf("** D:%s\n", hc.h.d.Text(16))
|
|
|
|
fmt.Printf("**(s)** peerD:%s\n", hc.h.PeerD.Text(16))
|
|
|
|
hc.h.FA()
|
|
|
|
fmt.Printf("**(s)** FA:%s\n", hc.h.fa)
|
|
|
|
|
2018-01-13 18:01:27 +00:00
|
|
|
fmt.Fprintf(c, "0x%s\n%08x:%08x:%02x\n", hc.h.d.Text(16),
|
|
|
|
hc.cipheropts, hc.opts, hc.op)
|
2018-01-09 03:16:55 +00:00
|
|
|
|
2018-01-12 03:42:42 +00:00
|
|
|
hc.r = hc.getStream(hc.h.fa)
|
|
|
|
hc.w = hc.getStream(hc.h.fa)
|
2018-01-09 03:16:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------*/
|
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-01-09 03:16:55 +00:00
|
|
|
fmt.Printf("[Decrypting...]\n")
|
2018-01-13 06:13:01 +00:00
|
|
|
n, err = c.c.Read(b)
|
2018-01-11 06:50:13 +00:00
|
|
|
if err != nil && err.Error() != "EOF" {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-11 17:13:18 +00:00
|
|
|
fmt.Printf(" ctext:%+v\n", b[:n]) // print only used portion
|
|
|
|
db := bytes.NewBuffer(b[:n])
|
|
|
|
// The StreamReader acts like a pipe, decrypting
|
|
|
|
// whatever is available and forwarding the result
|
|
|
|
// to the parameter of Read() as a normal io.Reader
|
2018-01-13 06:13:01 +00:00
|
|
|
rs := &cipher.StreamReader{S: c.r, R: db}
|
2018-01-11 17:13:18 +00:00
|
|
|
n, err = rs.Read(b)
|
|
|
|
fmt.Printf(" ptext:%+v\n", b[:n])
|
2018-01-09 03:16:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-01-09 03:16:55 +00:00
|
|
|
fmt.Printf("[Encrypting...]\n")
|
2018-01-11 06:50:13 +00:00
|
|
|
fmt.Printf(" ptext:%+v\n", b)
|
|
|
|
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-01-11 06:50:13 +00:00
|
|
|
fmt.Printf(" ctext:%+v\n", wb.Bytes())
|
2018-01-13 06:13:01 +00:00
|
|
|
n, err = c.c.Write(wb.Bytes())
|
2018-01-09 03:16:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return c coerced into a HKEx Conn (which implements interface net.Conn)
|
|
|
|
// Only useful if one wants to convert an open connection later to HKEx
|
|
|
|
// (Use Dial() instead to start with HKEx automatically.)
|
2018-01-11 21:44:11 +00:00
|
|
|
/*
|
|
|
|
func NewHKExConn(c *net.Conn) (hc *Conn) {
|
2018-01-09 03:16:55 +00:00
|
|
|
hc = new(Conn)
|
|
|
|
|
|
|
|
hc.c = *c
|
|
|
|
hc.h = New(0, 0)
|
|
|
|
d := big.NewInt(0)
|
|
|
|
_, err := fmt.Fscanln(hc.c, d)
|
|
|
|
if err != nil {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
hc.h.PeerD = d
|
|
|
|
fmt.Printf("** D:%s\n", hc.h.d.Text(16))
|
|
|
|
fmt.Printf("** peerD:%s\n", hc.h.PeerD.Text(16))
|
|
|
|
return
|
|
|
|
}
|
2018-01-11 21:44:11 +00:00
|
|
|
*/
|