2019-10-30 03:34:09 +00:00
|
|
|
// xsnet.go - net.Conn compatible channel setup with encrypted/HMAC
|
2018-04-07 20:04:10 +00:00
|
|
|
// negotiation
|
|
|
|
|
2020-08-08 08:54:46 +00:00
|
|
|
// Copyright (c) 2017-2020 Russell Magee
|
2018-04-07 20:04:10 +00:00
|
|
|
// 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
|
|
|
|
2019-10-30 03:34:09 +00:00
|
|
|
package xsnet
|
2018-01-09 03:16:55 +00:00
|
|
|
|
2023-10-18 07:52:08 +00:00
|
|
|
// Implementation of key-exchange-wrapped versions of the golang standard
|
2019-05-10 05:46:08 +00:00
|
|
|
// net package interfaces, allowing clients and servers to simply replace
|
|
|
|
// 'net.Dial' and 'net.Listen' with 'hkex.Dial' and 'hkex.Listen'
|
|
|
|
// (though some extra methods are implemented and must be used
|
|
|
|
// for things outside of the scope of plain sockets).
|
|
|
|
|
2018-09-11 03:28:41 +00:00
|
|
|
// DESIGN PRINCIPLE: There shall be no protocol features which enable
|
|
|
|
// downgrade attacks. The server shall have final authority to accept or
|
|
|
|
// reject any and all proposed KEx and connection parameters proposed by
|
|
|
|
// clients at setup. Action on denial shall be a simple server disconnect
|
|
|
|
// with possibly a status code sent so client can determine why connection
|
|
|
|
// was denied (compare to how failed auth is communicated to client).
|
2018-09-11 03:22:09 +00:00
|
|
|
|
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"
|
2021-11-15 05:17:56 +00:00
|
|
|
crand "crypto/rand"
|
2018-03-26 02:58:04 +00:00
|
|
|
"encoding/binary"
|
2018-02-17 02:43:37 +00:00
|
|
|
"encoding/hex"
|
2018-04-15 20:29:06 +00:00
|
|
|
"errors"
|
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-10-26 05:14:18 +00:00
|
|
|
"io/ioutil"
|
2018-01-18 05:27:00 +00:00
|
|
|
"log"
|
2018-01-09 03:16:55 +00:00
|
|
|
"math/big"
|
2018-05-04 06:53:47 +00:00
|
|
|
"math/rand"
|
2018-01-09 03:16:55 +00:00
|
|
|
"net"
|
2018-02-17 02:46:29 +00:00
|
|
|
"strings"
|
2018-05-02 19:28:56 +00:00
|
|
|
"sync"
|
2023-11-07 07:17:13 +00:00
|
|
|
"syscall"
|
2018-01-17 02:30:57 +00:00
|
|
|
"time"
|
2018-07-05 04:21:23 +00:00
|
|
|
|
2019-06-20 04:42:34 +00:00
|
|
|
hkex "blitter.com/go/herradurakex"
|
2020-01-30 20:18:39 +00:00
|
|
|
"blitter.com/go/kyber"
|
|
|
|
"blitter.com/go/newhope"
|
2020-02-17 08:02:28 +00:00
|
|
|
"blitter.com/go/xs/logger"
|
2020-12-11 03:21:04 +00:00
|
|
|
frodo "github.com/kuking/go-frodokem"
|
2018-01-09 03:16:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------*/
|
2018-10-14 07:20:30 +00:00
|
|
|
const PAD_SZ = 32 // max size of padding applied to each packet
|
2021-01-11 06:04:52 +00:00
|
|
|
const HMAC_CHK_SZ = 8 // leading bytes of HMAC to xmit for verification
|
2018-01-09 03:16:55 +00:00
|
|
|
|
2018-07-20 05:39:06 +00:00
|
|
|
type (
|
|
|
|
WinSize struct {
|
|
|
|
Rows uint16
|
|
|
|
Cols uint16
|
|
|
|
}
|
2018-04-29 02:28:37 +00:00
|
|
|
|
2018-07-20 05:39:06 +00:00
|
|
|
// chaffconfig captures attributes used to send chaff packets betwixt
|
|
|
|
// client and server connections, to obscure true traffic timing and
|
|
|
|
// patterns
|
|
|
|
// see: https://en.wikipedia.org/wiki/chaff_(countermeasure)
|
|
|
|
ChaffConfig struct {
|
|
|
|
shutdown bool //set to inform chaffHelper to shut down
|
|
|
|
msecsMin uint //msecs min interval
|
|
|
|
msecsMax uint //msecs max interval
|
|
|
|
szMax uint // max size in bytes
|
|
|
|
}
|
2018-05-07 00:41:09 +00:00
|
|
|
|
2018-10-09 04:31:11 +00:00
|
|
|
// Conn is a connection wrapping net.Conn with KEX & session state
|
2018-07-20 05:39:06 +00:00
|
|
|
Conn struct {
|
2019-12-15 19:38:04 +00:00
|
|
|
kex KEXAlg // KEX/KEM proposal (client -> server)
|
|
|
|
|
|
|
|
m *sync.Mutex // (internal)
|
|
|
|
c *net.Conn // which also implements io.Reader, io.Writer, ...
|
2019-06-20 04:42:34 +00:00
|
|
|
|
2019-06-22 07:44:23 +00:00
|
|
|
logCipherText bool // somewhat expensive, for debugging
|
|
|
|
logPlainText bool // INSECURE and somewhat expensive, for debugging
|
|
|
|
logTunActivity bool
|
2019-06-20 04:42:34 +00:00
|
|
|
|
2018-11-29 05:03:20 +00:00
|
|
|
cipheropts uint32 // post-KEx cipher/hmac options
|
|
|
|
opts uint32 // post-KEx protocol options (caller-defined)
|
2018-07-20 05:39:06 +00:00
|
|
|
WinCh chan WinSize
|
|
|
|
Rows uint16
|
|
|
|
Cols uint16
|
2018-05-04 06:53:47 +00:00
|
|
|
|
2023-11-05 22:58:24 +00:00
|
|
|
keepalive uint // if this reaches zero, conn is considered dead
|
2023-11-15 08:32:50 +00:00
|
|
|
rekey uint // if nonzero, rekeying interval in seconds
|
2023-11-07 07:17:13 +00:00
|
|
|
Pproc int // proc ID of command run on this conn
|
2023-11-05 22:58:24 +00:00
|
|
|
chaff ChaffConfig
|
|
|
|
tuns *map[uint16](*TunEndpoint)
|
2018-10-27 08:51:40 +00:00
|
|
|
|
2018-09-27 05:57:36 +00:00
|
|
|
closeStat *CSOType // close status (CSOExitStatus)
|
2018-07-20 05:39:06 +00:00
|
|
|
r cipher.Stream //read cipherStream
|
|
|
|
rm hash.Hash
|
|
|
|
w cipher.Stream //write cipherStream
|
|
|
|
wm hash.Hash
|
|
|
|
dBuf *bytes.Buffer //decrypt buffer for Read()
|
|
|
|
}
|
|
|
|
)
|
2018-06-30 02:23:11 +00:00
|
|
|
|
2018-10-26 05:14:18 +00:00
|
|
|
var (
|
2018-10-26 23:05:01 +00:00
|
|
|
Log *logger.Writer // reg. syslog output (no -d)
|
2018-10-26 05:14:18 +00:00
|
|
|
)
|
|
|
|
|
2018-10-27 08:51:40 +00:00
|
|
|
// Return string (suitable as map key) for a tunnel endpoint
|
|
|
|
func (t *TunEndpoint) String() string {
|
2018-10-29 02:17:47 +00:00
|
|
|
return fmt.Sprintf("[%d:%s:%d]", t.Lport, t.Peer, t.Rport)
|
2018-10-27 08:51:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 19:38:04 +00:00
|
|
|
func (k *KEXAlg) String() string {
|
|
|
|
switch *k {
|
|
|
|
case KEX_HERRADURA256:
|
|
|
|
return "KEX_HERRADURA256"
|
|
|
|
case KEX_HERRADURA512:
|
|
|
|
return "KEX_HERRADURA512"
|
|
|
|
case KEX_HERRADURA1024:
|
|
|
|
return "KEX_HERRADURA1024"
|
|
|
|
case KEX_HERRADURA2048:
|
|
|
|
return "KEX_HERRADURA2048"
|
|
|
|
case KEX_KYBER512:
|
|
|
|
return "KEX_KYBER512"
|
|
|
|
case KEX_KYBER768:
|
|
|
|
return "KEX_KYBER768"
|
|
|
|
case KEX_KYBER1024:
|
|
|
|
return "KEX_KYBER1024"
|
|
|
|
case KEX_NEWHOPE:
|
|
|
|
return "KEX_NEWHOPE"
|
|
|
|
case KEX_NEWHOPE_SIMPLE:
|
|
|
|
return "KEX_NEWHOPE_SIMPLE"
|
2020-12-11 03:21:04 +00:00
|
|
|
case KEX_FRODOKEM_1344AES:
|
|
|
|
return "KEX_FRODOKEM_1344AES"
|
|
|
|
case KEX_FRODOKEM_1344SHAKE:
|
|
|
|
return "KEX_FRODOKEM_1344SHAKE"
|
|
|
|
case KEX_FRODOKEM_976AES:
|
|
|
|
return "KEX_FRODOKEM_976AES"
|
|
|
|
case KEX_FRODOKEM_976SHAKE:
|
|
|
|
return "KEX_FRODOKEM_976SHAKE"
|
2019-12-15 19:38:04 +00:00
|
|
|
default:
|
|
|
|
return "KEX_ERR_UNK"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *Conn) CAlg() CSCipherAlg {
|
|
|
|
return CSCipherAlg(hc.cipheropts & 0x0FF)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CSCipherAlg) String() string {
|
|
|
|
switch *c & 0x0FF {
|
|
|
|
case CAlgAES256:
|
|
|
|
return "C_AES_256"
|
|
|
|
case CAlgTwofish128:
|
|
|
|
return "C_TWOFISH_128"
|
|
|
|
case CAlgBlowfish64:
|
|
|
|
return "C_BLOWFISH_64"
|
|
|
|
case CAlgCryptMT1:
|
|
|
|
return "C_CRYPTMT1"
|
2021-11-13 04:39:44 +00:00
|
|
|
case CAlgHopscotch:
|
|
|
|
return "C_HOPSCOTCH"
|
2020-02-22 01:21:19 +00:00
|
|
|
case CAlgChaCha20_12:
|
|
|
|
return "C_CHACHA20_12"
|
2019-12-15 19:38:04 +00:00
|
|
|
default:
|
|
|
|
return "C_ERR_UNK"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *Conn) HAlg() CSHmacAlg {
|
|
|
|
return CSHmacAlg((hc.cipheropts >> 8) & 0x0FF)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CSHmacAlg) String() string {
|
2021-11-15 05:17:56 +00:00
|
|
|
switch *h & 0x0FF {
|
2019-12-15 19:38:04 +00:00
|
|
|
case HmacSHA256:
|
|
|
|
return "H_SHA256"
|
|
|
|
case HmacSHA512:
|
2021-11-15 05:17:56 +00:00
|
|
|
return "H_SHA512"
|
2024-05-02 06:43:24 +00:00
|
|
|
case HmacWHIRLPOOL:
|
|
|
|
return "H_WHIRLPOOL"
|
2019-12-15 19:38:04 +00:00
|
|
|
default:
|
|
|
|
return "H_ERR_UNK"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 23:05:01 +00:00
|
|
|
func _initLogging(d bool, c string, f logger.Priority) {
|
2018-10-26 05:14:18 +00:00
|
|
|
if Log == nil {
|
2019-10-30 03:34:09 +00:00
|
|
|
Log, _ = logger.New(f, fmt.Sprintf("%s:xsnet", c))
|
2018-10-26 05:14:18 +00:00
|
|
|
}
|
|
|
|
if d {
|
|
|
|
log.SetFlags(0) // syslog will have date,time
|
|
|
|
log.SetOutput(Log)
|
|
|
|
} else {
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 23:05:01 +00:00
|
|
|
func Init(d bool, c string, f logger.Priority) {
|
2018-10-26 05:14:18 +00:00
|
|
|
_initLogging(d, c, f)
|
|
|
|
}
|
|
|
|
|
2019-06-28 05:10:59 +00:00
|
|
|
func (hc *Conn) Lock() {
|
2019-06-29 05:50:58 +00:00
|
|
|
hc.m.Lock()
|
2019-06-28 05:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *Conn) Unlock() {
|
2019-06-29 05:50:58 +00:00
|
|
|
hc.m.Unlock()
|
2019-06-28 05:10:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 04:52:58 +00:00
|
|
|
func (hc *Conn) KEX() KEXAlg {
|
2019-12-15 19:38:04 +00:00
|
|
|
return hc.kex
|
|
|
|
}
|
|
|
|
|
2020-07-22 04:52:58 +00:00
|
|
|
func (hc *Conn) GetStatus() CSOType {
|
2018-06-30 02:23:11 +00:00
|
|
|
return *hc.closeStat
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:07:04 +00:00
|
|
|
func (hc *Conn) SetStatus(stat CSOType) {
|
2018-06-30 02:23:11 +00:00
|
|
|
*hc.closeStat = stat
|
2018-09-06 03:36:32 +00:00
|
|
|
log.Println("closeStat:", *hc.closeStat)
|
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.)
|
2020-07-22 04:52:58 +00:00
|
|
|
func (hc *Conn) ConnOpts() uint32 {
|
2018-06-30 02:23:11 +00:00
|
|
|
return hc.cipheropts
|
2018-01-12 03:42:42 +00:00
|
|
|
}
|
|
|
|
|
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-06-30 02:23:11 +00:00
|
|
|
func (hc *Conn) SetConnOpts(copts uint32) {
|
|
|
|
hc.cipheropts = copts
|
2018-01-12 03:42:42 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2023-12-02 09:58:30 +00:00
|
|
|
func (hc *Conn) Opts() uint32 {
|
2018-06-30 02:23:11 +00:00
|
|
|
return hc.opts
|
2018-01-12 03:42:42 +00:00
|
|
|
}
|
|
|
|
|
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-06-30 02:23:11 +00:00
|
|
|
func (hc *Conn) SetOpts(opts uint32) {
|
|
|
|
hc.opts = opts
|
2018-01-12 03:42:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 03:34:09 +00:00
|
|
|
// Return a new xsnet.Conn
|
2018-10-09 04:31:11 +00:00
|
|
|
//
|
|
|
|
// Note this is internal: use Dial() or Accept()
|
|
|
|
func _new(kexAlg KEXAlg, conn *net.Conn) (hc *Conn, e error) {
|
|
|
|
// Set up stuff common to all KEx/KEM types
|
|
|
|
hc = &Conn{kex: kexAlg,
|
|
|
|
m: &sync.Mutex{},
|
|
|
|
c: conn,
|
|
|
|
closeStat: new(CSOType),
|
|
|
|
WinCh: make(chan WinSize, 1),
|
|
|
|
dBuf: new(bytes.Buffer)}
|
2018-11-08 03:35:32 +00:00
|
|
|
tempMap := make(map[uint16]*TunEndpoint)
|
|
|
|
hc.tuns = &tempMap
|
2018-10-09 04:31:11 +00:00
|
|
|
|
|
|
|
// Set up KEx/KEM-specifics
|
2018-10-12 23:16:49 +00:00
|
|
|
switch kexAlg {
|
|
|
|
case KEX_HERRADURA256:
|
|
|
|
fallthrough
|
|
|
|
case KEX_HERRADURA512:
|
|
|
|
fallthrough
|
|
|
|
case KEX_HERRADURA1024:
|
|
|
|
fallthrough
|
|
|
|
case KEX_HERRADURA2048:
|
2019-04-09 04:58:33 +00:00
|
|
|
fallthrough
|
2018-10-12 23:16:49 +00:00
|
|
|
case KEX_KYBER512:
|
|
|
|
fallthrough
|
2018-10-09 04:31:11 +00:00
|
|
|
case KEX_KYBER768:
|
2018-10-12 23:16:49 +00:00
|
|
|
fallthrough
|
|
|
|
case KEX_KYBER1024:
|
2019-04-09 04:58:33 +00:00
|
|
|
fallthrough
|
|
|
|
case KEX_NEWHOPE:
|
|
|
|
fallthrough
|
|
|
|
case KEX_NEWHOPE_SIMPLE:
|
2020-12-11 03:21:04 +00:00
|
|
|
fallthrough
|
|
|
|
case KEX_FRODOKEM_1344AES:
|
|
|
|
fallthrough
|
|
|
|
case KEX_FRODOKEM_1344SHAKE:
|
|
|
|
fallthrough
|
|
|
|
case KEX_FRODOKEM_976AES:
|
|
|
|
fallthrough
|
|
|
|
case KEX_FRODOKEM_976SHAKE:
|
2021-11-15 05:17:56 +00:00
|
|
|
//log.Printf("[KEx alg %d is valid]\n", kexAlg)
|
2018-10-11 04:12:38 +00:00
|
|
|
default:
|
2018-10-14 07:20:30 +00:00
|
|
|
// UNREACHABLE: _getkexalgnum() guarantees a valid KEX value
|
2019-09-15 05:56:03 +00:00
|
|
|
hc.kex = KEX_HERRADURA512
|
2018-10-12 23:16:49 +00:00
|
|
|
log.Printf("[KEx alg %d ?? defaults to %d]\n", kexAlg, hc.kex)
|
2018-10-09 04:31:11 +00:00
|
|
|
}
|
2020-02-22 01:21:19 +00:00
|
|
|
|
|
|
|
//hc.logCipherText = true // !!! DEBUGGING ONLY !!! NEVER DEPLOY this uncommented !!!
|
2018-10-09 04:31:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-10 05:46:08 +00:00
|
|
|
// applyConnExtensions processes optional Dial() negotiation
|
|
|
|
// parameters. See also getkexalgnum().
|
|
|
|
//
|
2023-11-04 06:57:55 +00:00
|
|
|
// # Currently defined extension values
|
2019-05-10 05:46:08 +00:00
|
|
|
//
|
2023-11-04 06:57:55 +00:00
|
|
|
// # KEx algs
|
2019-05-10 05:46:08 +00:00
|
|
|
//
|
|
|
|
// KEX_HERRADURA256 KEX_HERRADURA512 KEX_HERRADURA1024 KEX_HERRADURA2048
|
|
|
|
//
|
|
|
|
// KEX_KYBER512 KEX_KYBER768 KEX_KYBER1024
|
|
|
|
//
|
|
|
|
// KEX_NEWHOPE KEX_NEWHOPE_SIMPLE
|
|
|
|
//
|
2023-11-04 06:57:55 +00:00
|
|
|
// # Session (symmetric) crypto
|
2019-05-10 05:46:08 +00:00
|
|
|
//
|
2021-11-13 04:39:44 +00:00
|
|
|
// C_AES_256 C_TWOFISH_128 C_BLOWFISH_128 C_CRYPTMT1 C_CHACHA20_12 C_HOPSCOTCH
|
2019-05-10 05:46:08 +00:00
|
|
|
//
|
2023-11-04 06:57:55 +00:00
|
|
|
// # Session HMACs
|
2019-05-10 05:46:08 +00:00
|
|
|
//
|
|
|
|
// H_SHA256 H_SHA512
|
2018-06-30 02:23:11 +00:00
|
|
|
func (hc *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-06-30 02:23:11 +00:00
|
|
|
hc.cipheropts &= (0xFFFFFF00)
|
|
|
|
hc.cipheropts |= CAlgAES256
|
2018-01-12 03:42:42 +00:00
|
|
|
case "C_TWOFISH_128":
|
2018-01-18 05:27:00 +00:00
|
|
|
log.Println("[extension arg = C_TWOFISH_128]")
|
2018-06-30 02:23:11 +00:00
|
|
|
hc.cipheropts &= (0xFFFFFF00)
|
|
|
|
hc.cipheropts |= CAlgTwofish128
|
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-06-30 02:23:11 +00:00
|
|
|
hc.cipheropts &= (0xFFFFFF00)
|
|
|
|
hc.cipheropts |= CAlgBlowfish64
|
2018-10-24 07:15:33 +00:00
|
|
|
case "C_CRYPTMT1":
|
|
|
|
log.Println("[extension arg = C_CRYPTMT1]")
|
|
|
|
hc.cipheropts &= (0xFFFFFF00)
|
|
|
|
hc.cipheropts |= CAlgCryptMT1
|
2021-11-13 04:39:44 +00:00
|
|
|
case "C_HOPSCOTCH":
|
|
|
|
log.Println("[extension arg = C_HOPSCOTCH]")
|
|
|
|
hc.cipheropts &= (0xFFFFFF00)
|
|
|
|
hc.cipheropts |= CAlgHopscotch
|
2020-02-22 01:21:19 +00:00
|
|
|
case "C_CHACHA20_12":
|
|
|
|
log.Println("[extension arg = C_CHACHA20_12]")
|
2019-09-27 16:44:57 +00:00
|
|
|
hc.cipheropts &= (0xFFFFFF00)
|
2020-02-22 01:21:19 +00:00
|
|
|
hc.cipheropts |= CAlgChaCha20_12
|
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-06-30 02:23:11 +00:00
|
|
|
hc.cipheropts &= (0xFFFF00FF)
|
|
|
|
hc.cipheropts |= (HmacSHA256 << 8)
|
2018-09-30 07:19:25 +00:00
|
|
|
case "H_SHA512":
|
|
|
|
log.Println("[extension arg = H_SHA512]")
|
|
|
|
hc.cipheropts &= (0xFFFF00FF)
|
|
|
|
hc.cipheropts |= (HmacSHA512 << 8)
|
2024-05-02 06:43:24 +00:00
|
|
|
case "H_WHIRLPOOL":
|
|
|
|
log.Println("[extension arg = H_WHIRLPOOL]")
|
|
|
|
hc.cipheropts &= (0xFFFF00FF)
|
|
|
|
hc.cipheropts |= (HmacWHIRLPOOL << 8)
|
2023-12-02 09:58:30 +00:00
|
|
|
case "OPT_REMOD":
|
|
|
|
log.Println("[extension arg = OPT_REMOD]")
|
|
|
|
hc.opts |= CORemodulateShields
|
2018-10-12 23:16:49 +00:00
|
|
|
//default:
|
|
|
|
// log.Printf("[Dial ext \"%s\" ignored]\n", s)
|
2018-01-12 03:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 05:46:08 +00:00
|
|
|
func getkexalgnum(extensions ...string) (k KEXAlg) {
|
2019-09-15 05:56:03 +00:00
|
|
|
k = KEX_HERRADURA512 // default
|
2019-05-10 05:46:08 +00:00
|
|
|
for _, s := range extensions {
|
|
|
|
switch s {
|
|
|
|
case "KEX_HERRADURA256":
|
|
|
|
k = KEX_HERRADURA256
|
|
|
|
break //out of for
|
|
|
|
case "KEX_HERRADURA512":
|
|
|
|
k = KEX_HERRADURA512
|
|
|
|
break //out of for
|
|
|
|
case "KEX_HERRADURA1024":
|
|
|
|
k = KEX_HERRADURA1024
|
|
|
|
break //out of for
|
|
|
|
case "KEX_HERRADURA2048":
|
|
|
|
k = KEX_HERRADURA2048
|
|
|
|
break //out of for
|
|
|
|
case "KEX_KYBER512":
|
|
|
|
k = KEX_KYBER512
|
|
|
|
break //out of for
|
|
|
|
case "KEX_KYBER768":
|
|
|
|
k = KEX_KYBER768
|
|
|
|
break //out of for
|
|
|
|
case "KEX_KYBER1024":
|
|
|
|
k = KEX_KYBER1024
|
|
|
|
break //out of for
|
|
|
|
case "KEX_NEWHOPE":
|
|
|
|
k = KEX_NEWHOPE
|
|
|
|
break //out of for
|
|
|
|
case "KEX_NEWHOPE_SIMPLE":
|
|
|
|
k = KEX_NEWHOPE_SIMPLE
|
|
|
|
break //out of for
|
2020-12-11 03:21:04 +00:00
|
|
|
case "KEX_FRODOKEM_1344AES":
|
|
|
|
k = KEX_FRODOKEM_1344AES
|
|
|
|
break //out of for
|
|
|
|
case "KEX_FRODOKEM_1344SHAKE":
|
|
|
|
k = KEX_FRODOKEM_1344SHAKE
|
|
|
|
break //out of for
|
|
|
|
case "KEX_FRODOKEM_976AES":
|
|
|
|
k = KEX_FRODOKEM_976AES
|
|
|
|
break //out of for
|
|
|
|
case "KEX_FRODOKEM_976SHAKE":
|
|
|
|
k = KEX_FRODOKEM_976SHAKE
|
|
|
|
break //out of for
|
2019-05-10 05:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
func FrodoKEMDialSetup(c io.ReadWriter, hc *Conn) (err error) {
|
|
|
|
// Send xsnet.Conn parameters to remote side
|
|
|
|
|
|
|
|
// Alice, step 1: Generate a key pair.
|
|
|
|
var kem frodo.FrodoKEM
|
|
|
|
|
|
|
|
switch hc.kex {
|
|
|
|
case KEX_FRODOKEM_1344AES:
|
|
|
|
kem = frodo.Frodo1344AES()
|
|
|
|
case KEX_FRODOKEM_1344SHAKE:
|
|
|
|
kem = frodo.Frodo1344SHAKE()
|
|
|
|
case KEX_FRODOKEM_976AES:
|
|
|
|
kem = frodo.Frodo976AES()
|
|
|
|
default:
|
|
|
|
kem = frodo.Frodo976SHAKE()
|
|
|
|
}
|
|
|
|
pubA, secA := kem.Keygen() // pA
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
// Alice, step 2: Send the public key (na,ea) to Bob
|
2021-01-11 06:04:52 +00:00
|
|
|
n, err := fmt.Fprintf(c, "0x%x\n", pubA)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if n < len(pubA) {
|
|
|
|
panic(errors.New("incomplete Fprintf() of pubA"))
|
|
|
|
}
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2021-01-11 06:04:52 +00:00
|
|
|
// (... and send cipher, connection opts)
|
2020-12-11 03:21:04 +00:00
|
|
|
fmt.Fprintf(c, "0x%x:0x%x\n", hc.cipheropts, hc.opts)
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
// [Bob does the same and sends use a public key (nb, eb)
|
|
|
|
pubB_bigint := big.NewInt(0)
|
|
|
|
_, err = fmt.Fscanf(c, "0x%x\n", pubB_bigint)
|
|
|
|
pubB := pubB_bigint.Bytes()
|
|
|
|
|
2021-01-11 06:04:52 +00:00
|
|
|
// (... and sends us cipher, connection opts)
|
2020-12-11 03:21:04 +00:00
|
|
|
_, err = fmt.Fscanf(c, "0x%x:0x%x\n",
|
|
|
|
&hc.cipheropts, &hc.opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
// Alice, step 3: Create ctAtoB, shareA
|
|
|
|
ctAtoB, shareA, err := kem.Encapsulate(pubB)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
// Alice, step 4: Send ctAtoB to Bob
|
|
|
|
fmt.Fprintf(c, "0x%x\n", ctAtoB)
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
// Alice, step 5: Receive ctBtoA from Bob
|
|
|
|
ctBtoA_bigint := big.NewInt(0)
|
|
|
|
_, err = fmt.Fscanf(c, "0x%x\n", ctBtoA_bigint)
|
|
|
|
ctBtoA := ctBtoA_bigint.Bytes()
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
// Alice, step 6: compute Bob's share
|
|
|
|
shareB, err := kem.Dencapsulate(secA, ctBtoA)
|
|
|
|
sessionKey := append(shareA, shareB...)
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
hc.r, hc.rm, err = hc.getStream(sessionKey)
|
|
|
|
hc.w, hc.wm, err = hc.getStream(sessionKey)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-09 04:58:33 +00:00
|
|
|
func NewHopeDialSetup(c io.ReadWriter, hc *Conn) (err error) {
|
2019-10-30 03:34:09 +00:00
|
|
|
// Send xsnet.Conn parameters to remote side
|
2019-04-09 04:58:33 +00:00
|
|
|
|
|
|
|
// Alice, step 1: Generate a key pair.
|
2021-01-13 07:45:58 +00:00
|
|
|
privKeyAlice, pubKeyAlice, err := newhope.GenerateKeyPairAlice(crand.Reader)
|
2019-04-09 04:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice, step 2: Send the public key to Bob
|
|
|
|
fmt.Fprintf(c, "0x%x\n0x%x:0x%x\n", pubKeyAlice.Send,
|
|
|
|
hc.cipheropts, hc.opts)
|
|
|
|
|
|
|
|
// [Bob does step 1-3], from which we read Bob's pubkey
|
|
|
|
publicKeyBob := big.NewInt(0)
|
|
|
|
fmt.Fscanf(c, "0x%x\n", publicKeyBob)
|
|
|
|
var pubKeyBob newhope.PublicKeyBob
|
2019-05-10 05:46:08 +00:00
|
|
|
for i := range pubKeyBob.Send {
|
|
|
|
pubKeyBob.Send[i] = publicKeyBob.Bytes()[i]
|
2019-04-09 04:58:33 +00:00
|
|
|
}
|
2020-12-11 03:21:04 +00:00
|
|
|
//log.Printf("[Got server pubKey[]:%v]\n", pubKeyBob)
|
2019-04-09 04:58:33 +00:00
|
|
|
|
|
|
|
// Read cipheropts, session opts
|
|
|
|
_, err = fmt.Fscanf(c, "0x%x:0x%x\n",
|
|
|
|
&hc.cipheropts, &hc.opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice, step 3: Derive shared secret
|
|
|
|
// (NOTE: actual over-wire exchange was already done above. This is
|
|
|
|
// the math voodoo 'exchange' done after receiving data from Bob.)
|
|
|
|
aliceSharedSecret, err := newhope.KeyExchangeAlice(&pubKeyBob, privKeyAlice)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-11-15 05:17:56 +00:00
|
|
|
|
2019-04-09 04:58:33 +00:00
|
|
|
hc.r, hc.rm, err = hc.getStream(aliceSharedSecret)
|
|
|
|
hc.w, hc.wm, err = hc.getStream(aliceSharedSecret)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHopeSimpleDialSetup(c io.ReadWriter, hc *Conn) (err error) {
|
2019-10-30 03:34:09 +00:00
|
|
|
// Send xsnet.Conn parameters to remote side
|
2019-04-09 04:58:33 +00:00
|
|
|
|
|
|
|
// Alice, step 1: Generate a key pair.
|
2021-01-13 07:45:58 +00:00
|
|
|
privKeyAlice, pubKeyAlice, err := newhope.GenerateKeyPairSimpleAlice(crand.Reader)
|
2019-04-09 04:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice, step 2: Send the public key to Bob
|
|
|
|
fmt.Fprintf(c, "0x%x\n0x%x:0x%x\n", pubKeyAlice.Send,
|
|
|
|
hc.cipheropts, hc.opts)
|
|
|
|
|
|
|
|
// [Bob does step 1-3], from which we read Bob's pubkey
|
|
|
|
publicKeyBob := big.NewInt(0)
|
|
|
|
fmt.Fscanf(c, "0x%x\n", publicKeyBob)
|
|
|
|
var pubKeyBob newhope.PublicKeySimpleBob
|
2019-05-10 05:46:08 +00:00
|
|
|
for i := range pubKeyBob.Send {
|
|
|
|
pubKeyBob.Send[i] = publicKeyBob.Bytes()[i]
|
2019-04-09 04:58:33 +00:00
|
|
|
}
|
2020-12-11 03:21:04 +00:00
|
|
|
//log.Printf("[Got server pubKey[]:%v]\n", pubKeyBob)
|
2019-04-09 04:58:33 +00:00
|
|
|
|
|
|
|
// Read cipheropts, session opts
|
|
|
|
_, err = fmt.Fscanf(c, "0x%x:0x%x\n",
|
|
|
|
&hc.cipheropts, &hc.opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice, step 3: Derive shared secret
|
|
|
|
// (NOTE: actual over-wire exchange was already done above. This is
|
|
|
|
// the math voodoo 'exchange' done after receiving data from Bob.)
|
|
|
|
aliceSharedSecret, err := newhope.KeyExchangeSimpleAlice(&pubKeyBob, privKeyAlice)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-11-15 05:17:56 +00:00
|
|
|
|
2019-04-09 04:58:33 +00:00
|
|
|
hc.r, hc.rm, err = hc.getStream(aliceSharedSecret)
|
|
|
|
hc.w, hc.wm, err = hc.getStream(aliceSharedSecret)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-12 08:34:23 +00:00
|
|
|
func KyberDialSetup(c io.ReadWriter /*net.Conn*/, hc *Conn) (err error) {
|
2019-10-30 03:34:09 +00:00
|
|
|
// Send xsnet.Conn parameters to remote side
|
2018-10-11 04:12:38 +00:00
|
|
|
|
|
|
|
// Alice, step 1: Generate a key pair.
|
2018-10-12 23:16:49 +00:00
|
|
|
var alicePublicKey *kyber.PublicKey
|
|
|
|
var alicePrivateKey *kyber.PrivateKey
|
|
|
|
switch hc.kex {
|
|
|
|
case KEX_KYBER512:
|
2021-01-13 07:45:58 +00:00
|
|
|
alicePublicKey, alicePrivateKey, err = kyber.Kyber512.GenerateKeyPair(crand.Reader)
|
2018-10-12 23:16:49 +00:00
|
|
|
case KEX_KYBER768:
|
2021-01-13 07:45:58 +00:00
|
|
|
alicePublicKey, alicePrivateKey, err = kyber.Kyber768.GenerateKeyPair(crand.Reader)
|
2018-10-12 23:16:49 +00:00
|
|
|
case KEX_KYBER1024:
|
2021-01-13 07:45:58 +00:00
|
|
|
alicePublicKey, alicePrivateKey, err = kyber.Kyber1024.GenerateKeyPair(crand.Reader)
|
2018-10-12 23:16:49 +00:00
|
|
|
default:
|
2021-01-13 07:45:58 +00:00
|
|
|
alicePublicKey, alicePrivateKey, err = kyber.Kyber768.GenerateKeyPair(crand.Reader)
|
2018-10-12 23:16:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 04:12:38 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice, step 2: Send the public key to Bob
|
2018-10-19 03:44:23 +00:00
|
|
|
fmt.Fprintf(c, "0x%x\n0x%x:0x%x\n", alicePublicKey.Bytes(),
|
2018-10-11 04:12:38 +00:00
|
|
|
hc.cipheropts, hc.opts)
|
|
|
|
|
|
|
|
// [Bob, step 1-3], from which we read cipher text
|
2019-04-09 04:58:33 +00:00
|
|
|
pubKeyB := make([]byte, 4096)
|
|
|
|
fmt.Fscanf(c, "0x%x\n", &pubKeyB)
|
2018-10-19 03:44:23 +00:00
|
|
|
//if err != nil {
|
|
|
|
// return err
|
|
|
|
//}
|
2020-12-11 03:21:04 +00:00
|
|
|
//log.Printf("[Got server pubKeyB[]:%v]\n", pubKeyB)
|
2018-10-11 04:12:38 +00:00
|
|
|
|
|
|
|
// Read cipheropts, session opts
|
2018-10-19 03:44:23 +00:00
|
|
|
_, err = fmt.Fscanf(c, "0x%x:0x%x\n",
|
2018-10-11 04:12:38 +00:00
|
|
|
&hc.cipheropts, &hc.opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice, step 3: Decrypt the KEM cipher text.
|
2019-04-09 04:58:33 +00:00
|
|
|
aliceSharedSecret := alicePrivateKey.KEMDecrypt(pubKeyB)
|
2018-10-11 04:12:38 +00:00
|
|
|
|
|
|
|
hc.r, hc.rm, err = hc.getStream(aliceSharedSecret)
|
|
|
|
hc.w, hc.wm, err = hc.getStream(aliceSharedSecret)
|
|
|
|
return
|
2018-10-09 04:31:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 08:34:23 +00:00
|
|
|
func HKExDialSetup(c io.ReadWriter /*net.Conn*/, hc *Conn) (err error) {
|
2018-10-12 23:16:49 +00:00
|
|
|
var h *hkex.HerraduraKEx
|
|
|
|
switch hc.kex {
|
|
|
|
case KEX_HERRADURA256:
|
|
|
|
h = hkex.New(256, 64)
|
|
|
|
case KEX_HERRADURA512:
|
|
|
|
h = hkex.New(512, 128)
|
|
|
|
case KEX_HERRADURA1024:
|
|
|
|
h = hkex.New(1024, 256)
|
|
|
|
case KEX_HERRADURA2048:
|
|
|
|
h = hkex.New(2048, 512)
|
|
|
|
default:
|
|
|
|
h = hkex.New(256, 64)
|
|
|
|
}
|
|
|
|
|
2019-10-30 03:34:09 +00:00
|
|
|
// Send xsnet.Conn parameters to remote side
|
2018-09-11 07:04:38 +00:00
|
|
|
// d is value for Herradura key exchange
|
2018-10-19 03:44:23 +00:00
|
|
|
fmt.Fprintf(c, "0x%s\n0x%x:0x%x\n", h.D().Text(16),
|
2018-09-11 07:04:38 +00:00
|
|
|
hc.cipheropts, hc.opts)
|
|
|
|
|
2018-10-19 03:44:23 +00:00
|
|
|
// Read peer D over net.Conn (c)
|
2018-09-11 07:04:38 +00:00
|
|
|
d := big.NewInt(0)
|
|
|
|
_, err = fmt.Fscanln(c, d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-19 03:44:23 +00:00
|
|
|
_, err = fmt.Fscanf(c, "0x%x:0x%x\n",
|
2018-09-11 07:04:38 +00:00
|
|
|
&hc.cipheropts, &hc.opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-09 04:31:11 +00:00
|
|
|
h.SetPeerD(d)
|
|
|
|
log.Printf("** local D:%s\n", h.D().Text(16))
|
|
|
|
log.Printf("**(c)** peer D:%s\n", h.PeerD().Text(16))
|
|
|
|
h.ComputeFA()
|
|
|
|
log.Printf("**(c)** FA:%s\n", h.FA())
|
2018-09-11 07:04:38 +00:00
|
|
|
|
2018-10-11 04:12:38 +00:00
|
|
|
hc.r, hc.rm, err = hc.getStream(h.FA().Bytes())
|
|
|
|
hc.w, hc.wm, err = hc.getStream(h.FA().Bytes())
|
2018-09-11 07:04:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
func FrodoKEMAcceptSetup(c *net.Conn, hc *Conn) (err error) {
|
|
|
|
// Bob, step 1: Generate a key pair.
|
|
|
|
var kem frodo.FrodoKEM
|
|
|
|
|
|
|
|
switch hc.kex {
|
|
|
|
case KEX_FRODOKEM_1344AES:
|
|
|
|
kem = frodo.Frodo1344AES()
|
|
|
|
case KEX_FRODOKEM_1344SHAKE:
|
|
|
|
kem = frodo.Frodo1344SHAKE()
|
|
|
|
case KEX_FRODOKEM_976AES:
|
|
|
|
kem = frodo.Frodo976AES()
|
|
|
|
default:
|
|
|
|
kem = frodo.Frodo976SHAKE()
|
|
|
|
}
|
|
|
|
pubB, secB := kem.Keygen()
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2021-01-11 06:04:52 +00:00
|
|
|
// [Alice sends use a public key (na, ea)
|
2020-12-11 03:21:04 +00:00
|
|
|
pubA_bigint := big.NewInt(0)
|
|
|
|
_, err = fmt.Fscanf(*c, "0x%x\n", pubA_bigint)
|
|
|
|
pubA := pubA_bigint.Bytes()
|
|
|
|
|
2021-01-11 06:04:52 +00:00
|
|
|
// (... and sends us cipher, connection opts)
|
2020-12-11 03:21:04 +00:00
|
|
|
_, err = fmt.Fscanf(*c, "0x%x:0x%x\n",
|
|
|
|
&hc.cipheropts, &hc.opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2021-01-11 06:04:52 +00:00
|
|
|
// Bob, step 2: Send the public key (nb,eb) to Alice
|
|
|
|
n, err := fmt.Fprintf(*c, "0x%x\n", pubB)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if n < len(pubB) {
|
|
|
|
panic(errors.New("incomplete Fprintf() of pubB"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// (... and send cipher, connection opts)
|
|
|
|
fmt.Fprintf(*c, "0x%x:0x%x\n", hc.cipheropts, hc.opts)
|
2021-11-15 05:17:56 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
// Bob, step 3: Create ctBtoA, shareB
|
|
|
|
ctBtoA, shareB, err := kem.Encapsulate(pubA)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
// Bob, step 4: Send ctBtoA to Alice
|
|
|
|
fmt.Fprintf(*c, "0x%x\n", ctBtoA)
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
// Bob, step 5: Receive ctAtoB from Alice
|
|
|
|
ctAtoB_bigint := big.NewInt(0)
|
|
|
|
_, err = fmt.Fscanf(*c, "0x%x\n", ctAtoB_bigint)
|
|
|
|
ctAtoB := ctAtoB_bigint.Bytes()
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
// Alice, step 6: compute Bob's share
|
|
|
|
shareA, err := kem.Dencapsulate(secB, ctAtoB)
|
|
|
|
sessionKey := append(shareA, shareB...)
|
2020-12-16 05:15:15 +00:00
|
|
|
|
2020-12-11 03:21:04 +00:00
|
|
|
hc.r, hc.rm, err = hc.getStream(sessionKey)
|
|
|
|
hc.w, hc.wm, err = hc.getStream(sessionKey)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-09 04:58:33 +00:00
|
|
|
func NewHopeAcceptSetup(c *net.Conn, hc *Conn) (err error) {
|
|
|
|
// Bob, step 1: Deserialize Alice's public key from the binary encoding.
|
|
|
|
alicePublicKey := big.NewInt(0)
|
|
|
|
_, err = fmt.Fscanln(*c, alicePublicKey)
|
2020-12-11 03:21:04 +00:00
|
|
|
//log.Printf("[Got client pubKey:0x%x\n]", alicePublicKey)
|
2019-04-09 04:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pubKeyAlice newhope.PublicKeyAlice
|
2019-05-10 05:46:08 +00:00
|
|
|
for i := range pubKeyAlice.Send {
|
|
|
|
pubKeyAlice.Send[i] = alicePublicKey.Bytes()[i]
|
2019-04-09 04:58:33 +00:00
|
|
|
}
|
2019-05-10 05:46:08 +00:00
|
|
|
|
2019-04-09 04:58:33 +00:00
|
|
|
_, err = fmt.Fscanf(*c, "0x%x:0x%x\n",
|
|
|
|
&hc.cipheropts, &hc.opts)
|
|
|
|
log.Printf("[Got cipheropts, opts:%v, %v]", hc.cipheropts, hc.opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob, step 2: Generate the KEM cipher text and shared secret.
|
2021-01-13 07:45:58 +00:00
|
|
|
pubKeyBob, bobSharedSecret, err := newhope.KeyExchangeBob(crand.Reader, &pubKeyAlice)
|
2019-04-09 04:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob, step 3: Send the cipher text to Alice.
|
|
|
|
fmt.Fprintf(*c, "0x%x\n0x%x:0x%x\n", pubKeyBob.Send,
|
|
|
|
hc.cipheropts, hc.opts)
|
|
|
|
|
|
|
|
hc.r, hc.rm, err = hc.getStream(bobSharedSecret)
|
|
|
|
hc.w, hc.wm, err = hc.getStream(bobSharedSecret)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHopeSimpleAcceptSetup(c *net.Conn, hc *Conn) (err error) {
|
|
|
|
// Bob, step 1: Deserialize Alice's public key from the binary encoding.
|
|
|
|
alicePublicKey := big.NewInt(0)
|
|
|
|
_, err = fmt.Fscanln(*c, alicePublicKey)
|
2020-12-11 03:21:04 +00:00
|
|
|
//log.Printf("[Got client pubKey:0x%x\n]", alicePublicKey)
|
2019-04-09 04:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pubKeyAlice newhope.PublicKeySimpleAlice
|
2019-05-10 05:46:08 +00:00
|
|
|
for i := range pubKeyAlice.Send {
|
|
|
|
pubKeyAlice.Send[i] = alicePublicKey.Bytes()[i]
|
2019-04-09 04:58:33 +00:00
|
|
|
}
|
2019-05-10 05:46:08 +00:00
|
|
|
|
2019-04-09 04:58:33 +00:00
|
|
|
_, err = fmt.Fscanf(*c, "0x%x:0x%x\n",
|
|
|
|
&hc.cipheropts, &hc.opts)
|
|
|
|
log.Printf("[Got cipheropts, opts:%v, %v]", hc.cipheropts, hc.opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob, step 2: Generate the KEM cipher text and shared secret.
|
2021-01-13 07:45:58 +00:00
|
|
|
pubKeyBob, bobSharedSecret, err := newhope.KeyExchangeSimpleBob(crand.Reader, &pubKeyAlice)
|
2019-04-09 04:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob, step 3: Send the cipher text to Alice.
|
|
|
|
fmt.Fprintf(*c, "0x%x\n0x%x:0x%x\n", pubKeyBob.Send,
|
|
|
|
hc.cipheropts, hc.opts)
|
|
|
|
|
|
|
|
hc.r, hc.rm, err = hc.getStream(bobSharedSecret)
|
|
|
|
hc.w, hc.wm, err = hc.getStream(bobSharedSecret)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-12 23:16:49 +00:00
|
|
|
func KyberAcceptSetup(c *net.Conn, hc *Conn) (err error) {
|
2018-10-11 04:12:38 +00:00
|
|
|
// Bob, step 1: Deserialize Alice's public key from the binary encoding.
|
|
|
|
alicePublicKey := big.NewInt(0)
|
|
|
|
_, err = fmt.Fscanln(*c, alicePublicKey)
|
2020-12-11 03:21:04 +00:00
|
|
|
//log.Printf("[Got client pubKey:0x%x\n]", alicePublicKey)
|
2018-10-11 04:12:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-19 03:44:23 +00:00
|
|
|
_, err = fmt.Fscanf(*c, "0x%x:0x%x\n",
|
2018-10-11 04:12:38 +00:00
|
|
|
&hc.cipheropts, &hc.opts)
|
|
|
|
log.Printf("[Got cipheropts, opts:%v, %v]", hc.cipheropts, hc.opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-12 23:16:49 +00:00
|
|
|
var peerPublicKey *kyber.PublicKey
|
|
|
|
switch hc.kex {
|
|
|
|
case KEX_KYBER512:
|
|
|
|
peerPublicKey, err = kyber.Kyber512.PublicKeyFromBytes(alicePublicKey.Bytes())
|
|
|
|
case KEX_KYBER768:
|
|
|
|
peerPublicKey, err = kyber.Kyber768.PublicKeyFromBytes(alicePublicKey.Bytes())
|
|
|
|
case KEX_KYBER1024:
|
|
|
|
peerPublicKey, err = kyber.Kyber1024.PublicKeyFromBytes(alicePublicKey.Bytes())
|
|
|
|
default:
|
|
|
|
peerPublicKey, err = kyber.Kyber768.PublicKeyFromBytes(alicePublicKey.Bytes())
|
|
|
|
}
|
|
|
|
|
2018-10-11 04:12:38 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob, step 2: Generate the KEM cipher text and shared secret.
|
2021-01-13 07:45:58 +00:00
|
|
|
cipherText, bobSharedSecret, err := peerPublicKey.KEMEncrypt(crand.Reader)
|
2018-10-11 04:12:38 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:44:23 +00:00
|
|
|
// Bob, step 3: Send the cipher text to Alice.
|
|
|
|
fmt.Fprintf(*c, "0x%x\n0x%x:0x%x\n", cipherText,
|
2018-10-11 04:12:38 +00:00
|
|
|
hc.cipheropts, hc.opts)
|
|
|
|
|
|
|
|
hc.r, hc.rm, err = hc.getStream(bobSharedSecret)
|
|
|
|
hc.w, hc.wm, err = hc.getStream(bobSharedSecret)
|
|
|
|
return
|
2018-10-09 04:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func HKExAcceptSetup(c *net.Conn, hc *Conn) (err error) {
|
2018-10-12 23:16:49 +00:00
|
|
|
var h *hkex.HerraduraKEx
|
|
|
|
switch hc.kex {
|
|
|
|
case KEX_HERRADURA256:
|
|
|
|
h = hkex.New(256, 64)
|
|
|
|
case KEX_HERRADURA512:
|
|
|
|
h = hkex.New(512, 128)
|
|
|
|
case KEX_HERRADURA1024:
|
|
|
|
h = hkex.New(1024, 256)
|
|
|
|
case KEX_HERRADURA2048:
|
|
|
|
h = hkex.New(2048, 512)
|
|
|
|
default:
|
|
|
|
h = hkex.New(256, 64)
|
|
|
|
}
|
|
|
|
|
2019-10-30 03:34:09 +00:00
|
|
|
// Read in xsnet.Conn parameters over raw Conn c
|
2018-09-11 07:04:38 +00:00
|
|
|
// d is value for Herradura key exchange
|
|
|
|
d := big.NewInt(0)
|
2018-10-09 04:31:11 +00:00
|
|
|
_, err = fmt.Fscanln(*c, d)
|
2018-09-11 07:04:38 +00:00
|
|
|
log.Printf("[Got d:%v]", d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-19 03:44:23 +00:00
|
|
|
_, err = fmt.Fscanf(*c, "0x%x:0x%x\n",
|
2018-09-11 07:04:38 +00:00
|
|
|
&hc.cipheropts, &hc.opts)
|
|
|
|
log.Printf("[Got cipheropts, opts:%v, %v]", hc.cipheropts, hc.opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-09 04:31:11 +00:00
|
|
|
h.SetPeerD(d)
|
|
|
|
log.Printf("** D:%s\n", h.D().Text(16))
|
|
|
|
log.Printf("**(s)** peerD:%s\n", h.PeerD().Text(16))
|
|
|
|
h.ComputeFA()
|
|
|
|
log.Printf("**(s)** FA:%s\n", h.FA())
|
2018-09-11 07:04:38 +00:00
|
|
|
|
|
|
|
// Send D and cipheropts/conn_opts to peer
|
2018-10-19 03:44:23 +00:00
|
|
|
fmt.Fprintf(*c, "0x%s\n0x%x:0x%x\n", h.D().Text(16),
|
2018-09-11 07:04:38 +00:00
|
|
|
hc.cipheropts, hc.opts)
|
|
|
|
|
2018-10-11 04:12:38 +00:00
|
|
|
hc.r, hc.rm, err = hc.getStream(h.FA().Bytes())
|
|
|
|
hc.w, hc.wm, err = hc.getStream(h.FA().Bytes())
|
2018-09-11 07:04:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-11 03:22:09 +00:00
|
|
|
// Dial as net.Dial(), but with implicit key exchange to set up secure
|
|
|
|
// channel on connect
|
|
|
|
//
|
2023-11-04 06:57:55 +00:00
|
|
|
// Can be called like net.Dial(), defaulting to C_AES_256/H_SHA256,
|
|
|
|
// or additional extensions can be passed amongst the following:
|
2019-05-10 05:46:08 +00:00
|
|
|
//
|
2023-11-04 06:57:55 +00:00
|
|
|
// "C_AES_256" | "C_TWOFISH_128" | ...
|
2018-01-12 03:42:42 +00:00
|
|
|
//
|
2023-11-04 06:57:55 +00:00
|
|
|
// "H_SHA256" | "H_SHA512" | ...
|
2018-01-12 03:42:42 +00:00
|
|
|
//
|
2019-10-30 03:34:09 +00:00
|
|
|
// See go doc -u xsnet.applyConnExtensions
|
2018-09-29 19:15:53 +00:00
|
|
|
func Dial(protocol string, ipport string, extensions ...string) (hc Conn, err error) {
|
2018-10-26 05:14:18 +00:00
|
|
|
if Log == nil {
|
2018-10-26 23:05:01 +00:00
|
|
|
Init(false, "client", logger.LOG_DAEMON|logger.LOG_DEBUG)
|
2018-10-26 05:14:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 03:54:58 +00:00
|
|
|
var c net.Conn
|
|
|
|
if protocol == "kcp" {
|
2019-08-17 06:16:40 +00:00
|
|
|
c, err = kcpDial(ipport, extensions)
|
2019-08-14 03:54:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Open raw Conn c
|
|
|
|
c, err = net.Dial(protocol, ipport)
|
|
|
|
if err != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
2019-10-30 03:34:09 +00:00
|
|
|
// Init xsnet.Conn hc over net.Conn c
|
2018-10-09 04:31:11 +00:00
|
|
|
ret, err := _new(getkexalgnum(extensions...), &c)
|
|
|
|
if err != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
|
|
|
hc = *ret
|
2018-09-11 03:22:09 +00:00
|
|
|
|
2018-10-09 04:31:11 +00:00
|
|
|
// Client has full control over Conn extensions. It's the server's
|
|
|
|
// responsibility to accept or reject the proposed parameters.
|
|
|
|
hc.applyConnExtensions(extensions...)
|
2018-01-09 03:16:55 +00:00
|
|
|
|
2018-09-11 07:04:38 +00:00
|
|
|
// Perform Key Exchange according to client-request algorithm
|
2018-10-09 04:31:11 +00:00
|
|
|
fmt.Fprintf(c, "%02x\n", hc.kex)
|
2018-09-11 07:04:38 +00:00
|
|
|
switch hc.kex {
|
2018-10-12 23:16:49 +00:00
|
|
|
case KEX_HERRADURA256:
|
|
|
|
fallthrough
|
|
|
|
case KEX_HERRADURA512:
|
|
|
|
fallthrough
|
|
|
|
case KEX_HERRADURA1024:
|
|
|
|
fallthrough
|
|
|
|
case KEX_HERRADURA2048:
|
|
|
|
log.Printf("[Setting up for KEX_HERRADURA %d]\n", hc.kex)
|
2018-09-29 19:15:53 +00:00
|
|
|
if HKExDialSetup(c, &hc) != nil {
|
2018-10-09 04:31:11 +00:00
|
|
|
return Conn{}, nil
|
2018-09-11 07:04:38 +00:00
|
|
|
}
|
2018-10-12 23:16:49 +00:00
|
|
|
case KEX_KYBER512:
|
|
|
|
fallthrough
|
2018-10-09 04:31:11 +00:00
|
|
|
case KEX_KYBER768:
|
2018-10-12 23:16:49 +00:00
|
|
|
fallthrough
|
|
|
|
case KEX_KYBER1024:
|
|
|
|
log.Printf("[Setting up for KEX_KYBER %d]\n", hc.kex)
|
|
|
|
if KyberDialSetup(c, &hc) != nil {
|
2018-10-09 04:31:11 +00:00
|
|
|
return Conn{}, nil
|
2018-09-11 07:04:38 +00:00
|
|
|
}
|
2019-04-09 04:58:33 +00:00
|
|
|
case KEX_NEWHOPE:
|
|
|
|
log.Printf("[Setting up for KEX_NEWHOPE %d]\n", hc.kex)
|
|
|
|
if NewHopeDialSetup(c, &hc) != nil {
|
|
|
|
return Conn{}, nil
|
|
|
|
}
|
|
|
|
case KEX_NEWHOPE_SIMPLE:
|
|
|
|
log.Printf("[Setting up for KEX_NEWHOPE_SIMPLE %d]\n", hc.kex)
|
|
|
|
if NewHopeSimpleDialSetup(c, &hc) != nil {
|
|
|
|
return Conn{}, nil
|
|
|
|
}
|
2020-12-11 03:21:04 +00:00
|
|
|
case KEX_FRODOKEM_1344AES:
|
|
|
|
fallthrough
|
|
|
|
case KEX_FRODOKEM_1344SHAKE:
|
|
|
|
fallthrough
|
|
|
|
case KEX_FRODOKEM_976AES:
|
|
|
|
fallthrough
|
|
|
|
case KEX_FRODOKEM_976SHAKE:
|
|
|
|
log.Printf("[Setting up for KEX_FRODOKEM %d]\n", hc.kex)
|
|
|
|
if FrodoKEMDialSetup(c, &hc) != nil {
|
|
|
|
return Conn{}, nil
|
|
|
|
}
|
2018-09-11 07:04:38 +00:00
|
|
|
default:
|
2018-10-19 03:44:23 +00:00
|
|
|
return Conn{}, err
|
2018-01-12 03:42:42 +00:00
|
|
|
}
|
2018-01-09 03:16:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:44:11 +00:00
|
|
|
// Close a hkex.Conn
|
2018-09-05 05:24:16 +00:00
|
|
|
func (hc *Conn) Close() (err error) {
|
2023-11-05 22:58:24 +00:00
|
|
|
hc.ShutdownChaff()
|
2018-09-06 20:50:56 +00:00
|
|
|
s := make([]byte, 4)
|
2018-09-18 06:07:04 +00:00
|
|
|
binary.BigEndian.PutUint32(s, uint32(*hc.closeStat))
|
2018-09-17 00:14:50 +00:00
|
|
|
log.Printf("** Writing closeStat %d at Close()\n", *hc.closeStat)
|
2018-11-21 02:50:09 +00:00
|
|
|
//(*hc.c).SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
|
2018-09-06 20:50:56 +00:00
|
|
|
hc.WritePacket(s, CSOExitStatus)
|
2022-10-13 04:27:48 +00:00
|
|
|
|
|
|
|
// HACK: Bug #22,#33: Need to wait for rcvr to get final data
|
|
|
|
// TODO: Find a way to explicitly check if any outgoing data is pending
|
|
|
|
|
|
|
|
//Ensure socket has sent all data to client prior to closing
|
|
|
|
//NOTE: This is not ideal, as it would be better to somehow
|
|
|
|
//determine if there is any pending outgoing (write) data to the
|
|
|
|
//underlying socket (TCP/KCP) prior to closing; however Go's net pkg
|
|
|
|
//completely hides lower-level stuff. net.Conn.Close() according to
|
|
|
|
//docs sends written data in the background, so how best to determine
|
|
|
|
//all data has been sent? -rlm 2022-10-04
|
|
|
|
time.Sleep(10 * time.Millisecond) //nolint:gomnd
|
|
|
|
|
2019-08-23 06:18:20 +00:00
|
|
|
err = (*hc.c).Close()
|
2018-11-02 01:52:01 +00:00
|
|
|
logger.LogDebug(fmt.Sprintln("[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.
|
2018-09-05 05:24:16 +00:00
|
|
|
func (hc *Conn) LocalAddr() net.Addr {
|
2018-10-09 04:31:11 +00:00
|
|
|
return (*hc.c).LocalAddr()
|
2018-01-18 00:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address.
|
2018-09-05 05:24:16 +00:00
|
|
|
func (hc *Conn) RemoteAddr() net.Addr {
|
2018-10-09 04:31:11 +00:00
|
|
|
return (*hc.c).RemoteAddr()
|
2018-01-18 00:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-09-05 05:24:16 +00:00
|
|
|
func (hc *Conn) SetDeadline(t time.Time) error {
|
2018-10-09 04:31:11 +00:00
|
|
|
return (*hc.c).SetDeadline(t)
|
2018-01-18 00:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-09-05 05:24:16 +00:00
|
|
|
func (hc *Conn) SetWriteDeadline(t time.Time) error {
|
2018-10-09 04:31:11 +00:00
|
|
|
return (*hc.c).SetWriteDeadline(t)
|
2018-01-18 00:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-09-05 05:24:16 +00:00
|
|
|
func (hc *Conn) SetReadDeadline(t time.Time) error {
|
2018-10-09 04:31:11 +00:00
|
|
|
return (*hc.c).SetReadDeadline(t)
|
2018-01-18 00:39:01 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-08-14 03:54:58 +00:00
|
|
|
l net.Listener
|
|
|
|
proto string
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 06:13:01 +00:00
|
|
|
// Listen for a connection
|
|
|
|
//
|
|
|
|
// See go doc net.Listen
|
2019-08-17 06:16:40 +00:00
|
|
|
func Listen(proto string, ipport string, extensions ...string) (hl HKExListener, e error) {
|
2018-10-26 05:14:18 +00:00
|
|
|
if Log == nil {
|
2018-10-26 23:05:01 +00:00
|
|
|
Init(false, "server", logger.LOG_DAEMON|logger.LOG_DEBUG)
|
2018-10-26 05:14:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 03:54:58 +00:00
|
|
|
var lErr error
|
|
|
|
var l net.Listener
|
2019-08-17 06:16:40 +00:00
|
|
|
|
2019-08-14 03:54:58 +00:00
|
|
|
if proto == "kcp" {
|
2019-08-17 06:16:40 +00:00
|
|
|
l, lErr = kcpListen(ipport, extensions)
|
2019-08-14 03:54:58 +00:00
|
|
|
} else {
|
|
|
|
l, lErr = net.Listen(proto, ipport)
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
2019-08-14 03:54:58 +00:00
|
|
|
if lErr != nil {
|
|
|
|
return HKExListener{nil, proto}, lErr
|
|
|
|
}
|
|
|
|
logger.LogDebug(fmt.Sprintf("[Listening (proto '%s') on %s]\n", proto, ipport))
|
2018-01-09 03:16:55 +00:00
|
|
|
hl.l = l
|
2019-08-14 03:54:58 +00:00
|
|
|
hl.proto = proto
|
2018-01-09 03:16:55 +00:00
|
|
|
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-11-02 01:52:01 +00:00
|
|
|
logger.LogDebug(fmt.Sprintln("[Listener Closed]"))
|
2018-01-13 06:13:01 +00:00
|
|
|
return hl.l.Close()
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 07:52:08 +00:00
|
|
|
// Addr returns the listener's network address.
|
2018-01-27 00:15:39 +00:00
|
|
|
//
|
|
|
|
// 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-09-29 19:15:53 +00:00
|
|
|
func (hl *HKExListener) Accept() (hc Conn, err error) {
|
2019-08-14 03:54:58 +00:00
|
|
|
var c net.Conn
|
|
|
|
if hl.proto == "kcp" {
|
2019-08-17 06:16:40 +00:00
|
|
|
c, err = hl.AcceptKCP()
|
2019-08-14 03:54:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
|
|
|
logger.LogDebug(fmt.Sprintln("[kcp.Listener Accepted]"))
|
|
|
|
} else {
|
|
|
|
// Open raw Conn c
|
|
|
|
c, err = hl.l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.LogDebug(fmt.Sprintln("[net.Listener Accepted]"))
|
|
|
|
}
|
2018-10-09 04:31:11 +00:00
|
|
|
// Read KEx alg proposed by client
|
|
|
|
var kexAlg KEXAlg
|
2018-10-12 23:16:49 +00:00
|
|
|
//! NB. Was using fmt.FScanln() here, but integers with a leading zero
|
|
|
|
// were being mis-scanned? (is it an octal thing? Investigate.)
|
|
|
|
_, err = fmt.Fscanf(c, "%02x\n", &kexAlg)
|
2018-09-11 03:22:09 +00:00
|
|
|
if err != nil {
|
2018-10-09 04:31:11 +00:00
|
|
|
return Conn{}, err
|
2018-09-11 03:22:09 +00:00
|
|
|
}
|
2018-09-11 07:04:38 +00:00
|
|
|
log.Printf("[Client proposed KEx alg: %v]\n", kexAlg)
|
2018-09-11 03:22:09 +00:00
|
|
|
// --
|
|
|
|
|
2018-10-09 04:31:11 +00:00
|
|
|
ret, err := _new(kexAlg, &c)
|
|
|
|
if err != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
|
|
|
hc = *ret
|
|
|
|
|
|
|
|
switch hc.kex {
|
2018-10-12 23:16:49 +00:00
|
|
|
case KEX_HERRADURA256:
|
|
|
|
fallthrough
|
|
|
|
case KEX_HERRADURA512:
|
|
|
|
fallthrough
|
|
|
|
case KEX_HERRADURA1024:
|
|
|
|
fallthrough
|
|
|
|
case KEX_HERRADURA2048:
|
|
|
|
log.Printf("[Setting up for KEX_HERRADURA %d]\n", hc.kex)
|
2018-10-09 04:31:11 +00:00
|
|
|
if HKExAcceptSetup(&c, &hc) != nil {
|
2018-10-19 03:44:23 +00:00
|
|
|
return Conn{}, err
|
2018-10-09 04:31:11 +00:00
|
|
|
}
|
2018-10-12 23:16:49 +00:00
|
|
|
case KEX_KYBER512:
|
|
|
|
fallthrough
|
2018-10-09 04:31:11 +00:00
|
|
|
case KEX_KYBER768:
|
2018-10-12 23:16:49 +00:00
|
|
|
fallthrough
|
|
|
|
case KEX_KYBER1024:
|
|
|
|
log.Printf("[Setting up for KEX_KYBER %d]\n", hc.kex)
|
|
|
|
if KyberAcceptSetup(&c, &hc) != nil {
|
2018-10-19 03:44:23 +00:00
|
|
|
return Conn{}, err
|
2018-09-11 07:04:38 +00:00
|
|
|
}
|
2019-04-09 04:58:33 +00:00
|
|
|
case KEX_NEWHOPE:
|
|
|
|
log.Printf("[Setting up for KEX_NEWHOPE %d]\n", hc.kex)
|
|
|
|
if NewHopeAcceptSetup(&c, &hc) != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
|
|
|
case KEX_NEWHOPE_SIMPLE:
|
|
|
|
log.Printf("[Setting up for KEX_NEWHOPE_SIMPLE %d]\n", hc.kex)
|
|
|
|
if NewHopeSimpleAcceptSetup(&c, &hc) != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
2020-12-11 03:21:04 +00:00
|
|
|
case KEX_FRODOKEM_1344AES:
|
|
|
|
log.Printf("[Setting up for KEX_FRODOKEM_1344AES %d]\n", hc.kex)
|
|
|
|
if FrodoKEMAcceptSetup(&c, &hc) != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
|
|
|
case KEX_FRODOKEM_1344SHAKE:
|
|
|
|
log.Printf("[Setting up for KEX_FRODOKEM_1344SHAKE %d]\n", hc.kex)
|
|
|
|
if FrodoKEMAcceptSetup(&c, &hc) != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
|
|
|
case KEX_FRODOKEM_976AES:
|
|
|
|
log.Printf("[Setting up for KEX_FRODOKEM_976AES %d]\n", hc.kex)
|
|
|
|
if FrodoKEMAcceptSetup(&c, &hc) != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
|
|
|
case KEX_FRODOKEM_976SHAKE:
|
|
|
|
log.Printf("[Setting up for KEX_FRODOKEM_976SHAKE %d]\n", hc.kex)
|
|
|
|
if FrodoKEMAcceptSetup(&c, &hc) != nil {
|
|
|
|
return Conn{}, err
|
|
|
|
}
|
2018-09-11 07:04:38 +00:00
|
|
|
default:
|
2018-10-19 03:44:23 +00:00
|
|
|
return Conn{}, err
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
2019-12-15 19:38:04 +00:00
|
|
|
|
2018-10-09 04:31:11 +00:00
|
|
|
log.Println("[hc.Accept successful]")
|
2021-11-15 05:17:56 +00:00
|
|
|
return hc, err
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
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
|
|
|
|
//
|
2018-12-01 06:52:57 +00:00
|
|
|
// In addition to regular io.Reader behaviour this does demultiplexing of
|
|
|
|
// secured terminal comms and (if defined) tunnel traffic and session control
|
|
|
|
// packet processing.
|
|
|
|
//
|
2018-01-13 06:13:01 +00:00
|
|
|
// See go doc io.Reader
|
2023-11-06 02:48:19 +00:00
|
|
|
func (hc *Conn) Read(b []byte) (n int, err error) {
|
2018-03-26 02:58:04 +00:00
|
|
|
for {
|
2018-10-31 04:07:42 +00:00
|
|
|
if hc.dBuf.Len() > 0 {
|
2018-03-26 02:58:04 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-04-15 20:29:06 +00:00
|
|
|
var ctrlStatOp uint8
|
2018-10-14 07:20:30 +00:00
|
|
|
var hmacIn [HMAC_CHK_SZ]uint8
|
2018-03-26 02:58:04 +00:00
|
|
|
var payloadLen uint32
|
|
|
|
|
2022-09-25 18:33:09 +00:00
|
|
|
//------------- Read ctrl/status opcode --------------------
|
2018-04-29 02:28:37 +00:00
|
|
|
// Read ctrl/status opcode (CSOHmacInvalid on hmac mismatch)
|
2018-10-09 04:31:11 +00:00
|
|
|
err = binary.Read(*hc.c, binary.BigEndian, &ctrlStatOp)
|
2018-10-31 04:07:42 +00:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "EOF" {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
2023-11-06 02:48:19 +00:00
|
|
|
logger.LogDebug(fmt.Sprintln("[Client hung up(1)]"))
|
|
|
|
//!rlm hc.SetStatus(CSENone) //FIXME: re-examine this (exit 9 w/o it - 2023-11-05)
|
2018-10-31 04:07:42 +00:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
etxt := fmt.Sprintf("** Failed read:%s (%s) **", "ctrlStatOp", err)
|
2018-11-02 01:52:01 +00:00
|
|
|
logger.LogDebug(etxt)
|
2018-10-31 04:07:42 +00:00
|
|
|
return 0, errors.New(etxt)
|
|
|
|
}
|
2018-04-29 02:28:37 +00:00
|
|
|
log.Printf("[ctrlStatOp: %v]\n", ctrlStatOp)
|
|
|
|
if ctrlStatOp == CSOHmacInvalid {
|
2018-04-15 20:29:06 +00:00
|
|
|
// Other side indicated channel tampering, close channel
|
2018-06-30 02:23:11 +00:00
|
|
|
hc.Close()
|
2018-10-31 04:07:42 +00:00
|
|
|
return 0, errors.New("** ALERT - remote end detected HMAC mismatch - possible channel tampering **")
|
2018-04-15 20:29:06 +00:00
|
|
|
}
|
2022-09-25 18:33:09 +00:00
|
|
|
//----------------------------------------------------------
|
2018-04-15 20:29:06 +00:00
|
|
|
|
2022-09-25 18:33:09 +00:00
|
|
|
//------------------ Read HMAC len ------------------------
|
2018-04-15 19:58:24 +00:00
|
|
|
// Read the hmac and payload len first
|
2018-10-09 04:31:11 +00:00
|
|
|
err = binary.Read(*hc.c, binary.BigEndian, &hmacIn)
|
2018-03-27 04:58:42 +00:00
|
|
|
if err != nil {
|
2018-10-31 04:07:42 +00:00
|
|
|
if err.Error() == "EOF" {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
2023-11-06 02:48:19 +00:00
|
|
|
logger.LogDebug(fmt.Sprintln("[Client hung up(2)]"))
|
2018-10-31 04:07:42 +00:00
|
|
|
return 0, io.EOF
|
2018-03-26 04:47:38 +00:00
|
|
|
}
|
2018-10-31 04:07:42 +00:00
|
|
|
etxt := fmt.Sprintf("** Failed read:%s (%s) **", "HMAC", err)
|
2018-11-02 01:52:01 +00:00
|
|
|
logger.LogDebug(etxt)
|
2018-10-31 04:07:42 +00:00
|
|
|
return 0, errors.New(etxt)
|
2018-03-26 02:58:04 +00:00
|
|
|
}
|
2022-09-25 18:33:09 +00:00
|
|
|
//----------------------------------------------------------
|
2018-03-26 02:58:04 +00:00
|
|
|
|
2022-09-25 18:33:09 +00:00
|
|
|
//------------------ Read Payload len ---------------------
|
2018-10-09 04:31:11 +00:00
|
|
|
err = binary.Read(*hc.c, binary.BigEndian, &payloadLen)
|
2018-03-26 02:58:04 +00:00
|
|
|
if err != nil {
|
2018-10-31 04:07:42 +00:00
|
|
|
if err.Error() == "EOF" {
|
|
|
|
return 0, io.EOF
|
2018-04-28 23:05:33 +00:00
|
|
|
}
|
2018-10-31 04:07:42 +00:00
|
|
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
2023-11-06 02:48:19 +00:00
|
|
|
logger.LogDebug(fmt.Sprintln("[Client hung up(3)]"))
|
2018-10-31 04:07:42 +00:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
etxt := fmt.Sprintf("** Failed read:%s (%s) **", "payloadLen", err)
|
2018-11-02 01:52:01 +00:00
|
|
|
logger.LogDebug(etxt)
|
2018-10-31 04:07:42 +00:00
|
|
|
return 0, errors.New(etxt)
|
2018-03-26 02:58:04 +00:00
|
|
|
}
|
2022-09-25 18:33:09 +00:00
|
|
|
//----------------------------------------------------------
|
2018-04-28 23:05:33 +00:00
|
|
|
|
2018-09-03 02:58:13 +00:00
|
|
|
if payloadLen > MAX_PAYLOAD_LEN {
|
2018-11-02 01:52:01 +00:00
|
|
|
logger.LogDebug(fmt.Sprintf("[Insane payloadLen:%v]\n", payloadLen))
|
2018-06-30 02:23:11 +00:00
|
|
|
hc.Close()
|
2018-04-28 23:05:33 +00:00
|
|
|
return 1, errors.New("Insane payloadLen")
|
2018-03-26 02:58:04 +00:00
|
|
|
}
|
2018-04-28 23:05:33 +00:00
|
|
|
|
2022-09-25 18:33:09 +00:00
|
|
|
//-------------------- Read Payload ------------------------
|
2018-03-26 02:58:04 +00:00
|
|
|
var payloadBytes = make([]byte, payloadLen)
|
2018-10-09 04:31:11 +00:00
|
|
|
n, err = io.ReadFull(*hc.c, payloadBytes)
|
2018-10-31 04:07:42 +00:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "EOF" {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
2023-11-06 02:48:19 +00:00
|
|
|
logger.LogDebug(fmt.Sprintln("[Client hung up(4)]"))
|
2018-10-31 04:07:42 +00:00
|
|
|
return 0, io.EOF
|
2018-03-26 02:58:04 +00:00
|
|
|
}
|
2018-10-31 04:07:42 +00:00
|
|
|
etxt := fmt.Sprintf("** Failed read:%s (%s) **", "payloadBytes", err)
|
2018-11-02 01:52:01 +00:00
|
|
|
logger.LogDebug(etxt)
|
2018-10-31 04:07:42 +00:00
|
|
|
return 0, errors.New(etxt)
|
2018-02-17 02:46:29 +00:00
|
|
|
}
|
2022-09-25 18:33:09 +00:00
|
|
|
//----------------------------------------------------------
|
2018-02-17 06:12:27 +00:00
|
|
|
|
2019-06-20 04:42:34 +00:00
|
|
|
if hc.logCipherText {
|
|
|
|
log.Printf(" <:ctext:\r\n%s\r\n", hex.Dump(payloadBytes[:n]))
|
|
|
|
}
|
2021-01-11 06:04:52 +00:00
|
|
|
//fmt.Printf(" <:ctext:\r\n%s\r\n", hex.Dump(payloadBytes[:n]))
|
2018-03-26 02:58:04 +00:00
|
|
|
|
2022-09-25 18:33:09 +00:00
|
|
|
//---------------- Verify Payload via HMAC -----------------
|
2020-12-16 06:09:18 +00:00
|
|
|
hc.rm.Write(payloadBytes) // Calc hmac on received data
|
|
|
|
hTmp := hc.rm.Sum(nil)[0:HMAC_CHK_SZ]
|
|
|
|
//log.Printf("<%04x) HMAC:(i)%s (c)%02x\r\n", decryptN, hex.EncodeToString([]byte(hmacIn[0:])), hTmp)
|
|
|
|
|
|
|
|
// Log alert if hmac didn't match, corrupted channel
|
|
|
|
if !bytes.Equal(hTmp, []byte(hmacIn[0:])) /*|| hmacIn[0] > 0xf8*/ {
|
|
|
|
logger.LogDebug(fmt.Sprintln("** ALERT - detected HMAC mismatch, possible channel tampering **"))
|
|
|
|
_, _ = (*hc.c).Write([]byte{CSOHmacInvalid})
|
|
|
|
}
|
2022-09-25 18:33:09 +00:00
|
|
|
//----------------------------------------------------------
|
2020-12-16 06:09:18 +00:00
|
|
|
|
2022-09-25 18:33:09 +00:00
|
|
|
//------------------- Decrypt Payload ----------------------
|
2018-03-26 02:58:04 +00:00
|
|
|
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
|
2018-06-30 02:23:11 +00:00
|
|
|
rs := &cipher.StreamReader{S: hc.r, R: db}
|
2018-03-26 02:58:04 +00:00
|
|
|
// The caller isn't necessarily reading the full payload so we need
|
2018-10-02 03:35:50 +00:00
|
|
|
// to decrypt to an intermediate buffer, draining it on demand of caller
|
2018-03-26 02:58:04 +00:00
|
|
|
decryptN, err := rs.Read(payloadBytes)
|
2022-09-25 18:33:09 +00:00
|
|
|
//----------------------------------------------------------
|
2021-01-11 06:04:52 +00:00
|
|
|
|
2019-06-20 04:42:34 +00:00
|
|
|
if hc.logPlainText {
|
2021-01-11 06:04:52 +00:00
|
|
|
log.Printf(" <:ptext:\r\n%s\r\n", hex.Dump(payloadBytes[:n]))
|
2019-06-20 04:42:34 +00:00
|
|
|
}
|
2018-03-26 02:58:04 +00:00
|
|
|
if err != nil {
|
2019-10-30 03:34:09 +00:00
|
|
|
log.Println("xsnet.Read():", err)
|
2018-09-06 07:16:44 +00:00
|
|
|
//panic(err)
|
|
|
|
} else {
|
2022-09-25 18:33:09 +00:00
|
|
|
//------------ Discard Padding ---------------------
|
2018-10-02 03:35:50 +00:00
|
|
|
// Padding: Read padSide, padLen, (padding | d) or (d | padding)
|
|
|
|
padSide := payloadBytes[0]
|
|
|
|
padLen := payloadBytes[1]
|
|
|
|
|
|
|
|
payloadBytes = payloadBytes[2:]
|
|
|
|
if padSide == 0 {
|
|
|
|
payloadBytes = payloadBytes[padLen:]
|
|
|
|
} else {
|
|
|
|
payloadBytes = payloadBytes[0 : len(payloadBytes)-int(padLen)]
|
|
|
|
}
|
2022-09-25 18:33:09 +00:00
|
|
|
//--------------------------------------------------
|
2018-10-02 03:35:50 +00:00
|
|
|
|
2022-09-25 18:33:09 +00:00
|
|
|
switch ctrlStatOp {
|
|
|
|
case CSOChaff:
|
|
|
|
// Throw away pkt if it's chaff (ie., caller to Read() won't see this data)
|
2018-09-06 07:16:44 +00:00
|
|
|
log.Printf("[Chaff pkt, discarded (len %d)]\n", decryptN)
|
2023-11-05 22:58:24 +00:00
|
|
|
case CSOKeepAlive:
|
2023-11-07 05:57:19 +00:00
|
|
|
//logger.LogDebug(fmt.Sprintf("[got keepAlive pkt, discarded (len %d)]\n", decryptN))
|
|
|
|
// payload of keepalive (2 bytes) is not currently used (0x55aa fixed)
|
2023-11-06 00:40:12 +00:00
|
|
|
_ = binary.BigEndian.Uint16(payloadBytes[0:2])
|
2023-11-05 22:58:24 +00:00
|
|
|
hc.ResetKeepAlive()
|
2023-11-15 08:32:50 +00:00
|
|
|
case CSORekey:
|
|
|
|
// rekey
|
|
|
|
//logger.LogDebug(fmt.Sprintf("[Got rekey [%02x %02x %02x ...]\n",
|
|
|
|
// payloadBytes[0], payloadBytes[1], payloadBytes[2]))
|
|
|
|
rekeyData := payloadBytes
|
2023-12-02 09:58:30 +00:00
|
|
|
if (hc.opts & CORemodulateShields) != 0 {
|
|
|
|
hc.Lock()
|
|
|
|
hc.cipheropts = getNewStreamAlgs(rekeyData[0], rekeyData[1])
|
|
|
|
hc.Unlock()
|
|
|
|
}
|
2023-11-15 08:32:50 +00:00
|
|
|
hc.r, hc.rm, err = hc.getStream(rekeyData)
|
2022-09-25 18:33:09 +00:00
|
|
|
case CSOTermSize:
|
2018-09-06 07:16:44 +00:00
|
|
|
fmt.Sscanf(string(payloadBytes), "%d %d", &hc.Rows, &hc.Cols)
|
|
|
|
log.Printf("[TermSize pkt: rows %v cols %v]\n", hc.Rows, hc.Cols)
|
|
|
|
hc.WinCh <- WinSize{hc.Rows, hc.Cols}
|
2022-09-25 18:33:09 +00:00
|
|
|
case CSOExitStatus:
|
2018-09-06 07:16:44 +00:00
|
|
|
if len(payloadBytes) > 0 {
|
2018-09-18 06:07:04 +00:00
|
|
|
hc.SetStatus(CSOType(binary.BigEndian.Uint32(payloadBytes)))
|
2018-09-06 07:16:44 +00:00
|
|
|
} else {
|
2018-11-02 01:52:01 +00:00
|
|
|
logger.LogDebug(fmt.Sprintln("[truncated payload, cannot determine CSOExitStatus]"))
|
2018-09-17 00:14:50 +00:00
|
|
|
hc.SetStatus(CSETruncCSO)
|
2018-09-05 05:24:16 +00:00
|
|
|
}
|
2018-09-17 00:14:50 +00:00
|
|
|
hc.Close()
|
2022-09-25 18:33:09 +00:00
|
|
|
case CSOTunSetup:
|
2018-11-01 03:11:00 +00:00
|
|
|
// server side tunnel setup in response to client
|
2018-11-02 01:52:01 +00:00
|
|
|
lport := binary.BigEndian.Uint16(payloadBytes[0:2])
|
2018-10-29 02:17:47 +00:00
|
|
|
rport := binary.BigEndian.Uint16(payloadBytes[2:4])
|
2018-11-08 03:35:32 +00:00
|
|
|
if _, ok := (*hc.tuns)[rport]; !ok {
|
|
|
|
// tunnel first-time open
|
|
|
|
logger.LogDebug(fmt.Sprintf("[Server] Got Initial CSOTunSetup [%d:%d]", lport, rport))
|
|
|
|
hc.StartServerTunnel(lport, rport)
|
|
|
|
} else {
|
|
|
|
logger.LogDebug(fmt.Sprintf("[Server] Got CSOTunSetup [%d:%d]", lport, rport))
|
|
|
|
}
|
|
|
|
(*hc.tuns)[rport].Ctl <- 'd' // Dial() rport
|
2022-09-25 18:33:09 +00:00
|
|
|
case CSOTunSetupAck:
|
2018-11-02 01:52:01 +00:00
|
|
|
lport := binary.BigEndian.Uint16(payloadBytes[0:2])
|
2018-11-01 03:11:00 +00:00
|
|
|
rport := binary.BigEndian.Uint16(payloadBytes[2:4])
|
2018-11-08 03:35:32 +00:00
|
|
|
if _, ok := (*hc.tuns)[rport]; !ok {
|
|
|
|
// tunnel first-time open
|
|
|
|
logger.LogDebug(fmt.Sprintf("[Client] Got Initial CSOTunSetupAck [%d:%d]", lport, rport))
|
|
|
|
hc.StartClientTunnel(lport, rport)
|
|
|
|
} else {
|
|
|
|
logger.LogDebug(fmt.Sprintf("[Client] Got CSOTunSetupAck [%d:%d]", lport, rport))
|
|
|
|
}
|
|
|
|
(*hc.tuns)[rport].Ctl <- 'a' // Listen() for lport connection
|
2022-09-25 18:33:09 +00:00
|
|
|
case CSOTunRefused:
|
2018-11-08 03:35:32 +00:00
|
|
|
// client side receiving CSOTunRefused means the remote side
|
|
|
|
// could not dial() rport. So we cannot yet listen()
|
|
|
|
// for client-side on lport.
|
2018-11-02 01:52:01 +00:00
|
|
|
lport := binary.BigEndian.Uint16(payloadBytes[0:2])
|
|
|
|
rport := binary.BigEndian.Uint16(payloadBytes[2:4])
|
2018-11-08 03:35:32 +00:00
|
|
|
logger.LogDebug(fmt.Sprintf("[Client] Got CSOTunRefused [%d:%d]", lport, rport))
|
2018-11-12 04:12:29 +00:00
|
|
|
if _, ok := (*hc.tuns)[rport]; ok {
|
2019-06-28 05:10:59 +00:00
|
|
|
hc.MarkTunDead(rport)
|
2018-11-12 04:12:29 +00:00
|
|
|
} else {
|
|
|
|
logger.LogDebug(fmt.Sprintf("[Client] CSOTunRefused on already-closed tun [%d:%d]", lport, rport))
|
|
|
|
}
|
2022-09-25 18:33:09 +00:00
|
|
|
case CSOTunDisconn:
|
2018-11-02 01:52:01 +00:00
|
|
|
// server side's rport has disconnected (server lost)
|
|
|
|
lport := binary.BigEndian.Uint16(payloadBytes[0:2])
|
|
|
|
rport := binary.BigEndian.Uint16(payloadBytes[2:4])
|
2018-11-08 03:35:32 +00:00
|
|
|
logger.LogDebug(fmt.Sprintf("[Client] Got CSOTunDisconn [%d:%d]", lport, rport))
|
2018-11-12 02:56:08 +00:00
|
|
|
if _, ok := (*hc.tuns)[rport]; ok {
|
2019-06-28 05:10:59 +00:00
|
|
|
hc.MarkTunDead(rport)
|
2018-11-12 02:56:08 +00:00
|
|
|
} else {
|
|
|
|
logger.LogDebug(fmt.Sprintf("[Client] CSOTunDisconn on already-closed tun [%d:%d]", lport, rport))
|
|
|
|
}
|
2022-09-25 18:33:09 +00:00
|
|
|
case CSOTunHangup:
|
2018-11-02 01:52:01 +00:00
|
|
|
// client side's lport has hung up
|
|
|
|
lport := binary.BigEndian.Uint16(payloadBytes[0:2])
|
|
|
|
rport := binary.BigEndian.Uint16(payloadBytes[2:4])
|
2018-11-08 03:35:32 +00:00
|
|
|
logger.LogDebug(fmt.Sprintf("[Server] Got CSOTunHangup [%d:%d]", lport, rport))
|
2018-11-12 02:56:08 +00:00
|
|
|
if _, ok := (*hc.tuns)[rport]; ok {
|
2019-06-28 05:10:59 +00:00
|
|
|
hc.MarkTunDead(rport)
|
2018-11-12 02:56:08 +00:00
|
|
|
} else {
|
|
|
|
logger.LogDebug(fmt.Sprintf("[Server] CSOTunHangup to already-closed tun [%d:%d]", lport, rport))
|
|
|
|
}
|
2022-09-25 18:33:09 +00:00
|
|
|
case CSOTunData:
|
2018-11-02 01:52:01 +00:00
|
|
|
lport := binary.BigEndian.Uint16(payloadBytes[0:2])
|
2018-10-29 02:17:47 +00:00
|
|
|
rport := binary.BigEndian.Uint16(payloadBytes[2:4])
|
2018-10-31 04:07:42 +00:00
|
|
|
//fmt.Printf("[Got CSOTunData: [lport %d:rport %d] data:%v\n", lport, rport, payloadBytes[4:])
|
2018-11-08 03:35:32 +00:00
|
|
|
if _, ok := (*hc.tuns)[rport]; ok {
|
2019-06-22 07:44:23 +00:00
|
|
|
if hc.logTunActivity {
|
|
|
|
logger.LogDebug(fmt.Sprintf("[Writing data to rport [%d:%d]", lport, rport))
|
|
|
|
}
|
2018-11-08 03:35:32 +00:00
|
|
|
(*hc.tuns)[rport].Data <- payloadBytes[4:]
|
2019-06-28 05:10:59 +00:00
|
|
|
hc.ResetTunnelAge(rport)
|
2018-11-02 05:14:01 +00:00
|
|
|
} else {
|
|
|
|
logger.LogDebug(fmt.Sprintf("[Attempt to write data to closed tun [%d:%d]", lport, rport))
|
|
|
|
}
|
2022-09-25 18:33:09 +00:00
|
|
|
case CSOTunKeepAlive:
|
2018-11-29 05:03:20 +00:00
|
|
|
// client side has sent keepalive for tunnels -- if client
|
|
|
|
// dies or exits unexpectedly the absence of this will
|
|
|
|
// let the server know to hang up on Dial()ed server rports.
|
2018-11-12 06:46:39 +00:00
|
|
|
_ = binary.BigEndian.Uint16(payloadBytes[0:2])
|
|
|
|
//logger.LogDebug(fmt.Sprintf("[Server] Got CSOTunKeepAlive"))
|
2023-11-04 06:57:55 +00:00
|
|
|
// though CSOTunKeepAlive sends an endp (uint16), we don't use it,
|
|
|
|
// preferring to refresh *all* tunnels on the message.
|
|
|
|
// (?rlm 2023-11-04 -- TODO: verify this, it's been a while.)
|
2018-11-12 06:46:39 +00:00
|
|
|
for _, t := range *hc.tuns {
|
2019-06-29 05:50:58 +00:00
|
|
|
hc.Lock()
|
2018-11-12 06:46:39 +00:00
|
|
|
t.KeepAlive = 0
|
2019-06-29 05:50:58 +00:00
|
|
|
hc.Unlock()
|
2018-11-12 06:46:39 +00:00
|
|
|
}
|
2022-09-25 18:33:09 +00:00
|
|
|
case CSONone:
|
2018-09-06 07:16:44 +00:00
|
|
|
hc.dBuf.Write(payloadBytes)
|
2022-09-25 18:33:09 +00:00
|
|
|
default:
|
2018-11-02 01:52:01 +00:00
|
|
|
logger.LogDebug(fmt.Sprintf("[Unknown CSOType:%d]", ctrlStatOp))
|
2018-08-06 04:43:21 +00:00
|
|
|
}
|
2018-04-15 19:58:24 +00:00
|
|
|
}
|
2018-02-17 03:25:11 +00:00
|
|
|
}
|
2018-04-28 23:05:33 +00:00
|
|
|
|
2018-06-30 02:23:11 +00:00
|
|
|
retN := hc.dBuf.Len()
|
2018-03-26 04:47:38 +00:00
|
|
|
if retN > len(b) {
|
|
|
|
retN = len(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Read() got %d bytes\n", retN)
|
2021-01-11 06:04:52 +00:00
|
|
|
|
2018-06-30 02:23:11 +00:00
|
|
|
copy(b, hc.dBuf.Next(retN))
|
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
|
2023-11-15 08:32:50 +00:00
|
|
|
func (hc *Conn) Write(b []byte) (n int, err error) {
|
|
|
|
//logger.LogDebug("[+Write]")
|
2018-06-30 02:23:11 +00:00
|
|
|
n, err = hc.WritePacket(b, CSONone)
|
2023-11-15 08:32:50 +00:00
|
|
|
//logger.LogDebug("[-Write]")
|
2018-05-01 09:39:45 +00:00
|
|
|
return n, err
|
2018-04-29 02:28:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 03:11:00 +00:00
|
|
|
// Write a byte slice with specified ctrlStatOp byte
|
|
|
|
func (hc *Conn) WritePacket(b []byte, ctrlStatOp byte) (n int, err error) {
|
2023-11-15 08:32:50 +00:00
|
|
|
hc.Lock()
|
|
|
|
defer hc.Unlock()
|
|
|
|
|
2018-02-17 02:46:29 +00:00
|
|
|
//log.Printf("[Encrypting...]\r\n")
|
2018-04-15 19:58:24 +00:00
|
|
|
var hmacOut []uint8
|
2018-03-26 02:58:04 +00:00
|
|
|
var payloadLen uint32
|
2018-09-17 00:14:50 +00:00
|
|
|
|
2018-09-11 07:04:38 +00:00
|
|
|
if hc.m == nil || hc.wm == nil {
|
2018-09-17 00:14:50 +00:00
|
|
|
return 0, errors.New("Secure chan not ready for writing")
|
2018-09-11 07:04:38 +00:00
|
|
|
}
|
2018-09-17 00:14:50 +00:00
|
|
|
|
2018-11-01 03:11:00 +00:00
|
|
|
//Padding prior to encryption
|
2021-01-11 06:04:52 +00:00
|
|
|
padSz := rand.Intn(PAD_SZ-1) + 1 /*(rand.Intn(PAD_SZ) / 2) + (PAD_SZ / 2)*/
|
2018-10-02 18:03:10 +00:00
|
|
|
padLen := padSz - ((len(b) + padSz) % padSz)
|
|
|
|
if padLen == padSz {
|
2018-10-02 03:35:50 +00:00
|
|
|
// No padding required
|
|
|
|
padLen = 0
|
|
|
|
}
|
2021-01-11 06:04:52 +00:00
|
|
|
|
2018-10-02 03:35:50 +00:00
|
|
|
padBytes := make([]byte, padLen)
|
|
|
|
rand.Read(padBytes)
|
|
|
|
// For a little more confusion let's support padding either before
|
|
|
|
// or after the payload.
|
|
|
|
padSide := rand.Intn(2)
|
|
|
|
if padSide == 0 {
|
|
|
|
b = append([]byte{byte(padSide)}, append([]byte{byte(padLen)}, append(padBytes, b...)...)...)
|
|
|
|
} else {
|
|
|
|
b = append([]byte{byte(padSide)}, append([]byte{byte(padLen)}, append(b, padBytes...)...)...)
|
|
|
|
}
|
|
|
|
|
2018-09-05 05:24:16 +00:00
|
|
|
payloadLen = uint32(len(b))
|
2019-06-20 04:42:34 +00:00
|
|
|
if hc.logPlainText {
|
2021-01-11 06:04:52 +00:00
|
|
|
log.Printf(" >:ptext:\r\n%s\r\n", hex.Dump(b[0:payloadLen]))
|
2019-06-20 04:42:34 +00:00
|
|
|
}
|
2018-09-05 05:24:16 +00:00
|
|
|
|
2020-12-16 06:09:18 +00:00
|
|
|
// NOTE releases prior to v0.9 used Authenticate-then-Encrypt,
|
|
|
|
// which in block modes is insecure; however
|
|
|
|
// 1) we use exclusively XOR-stream modes with random padding,
|
2020-12-16 05:15:15 +00:00
|
|
|
// 2) are padding randomly either before or after the real payload, and
|
|
|
|
// 3) the padding side indicator value itself is part of the ciphertext
|
|
|
|
// ... thus are not subject to oracle attacks of the type used on SSL
|
2020-12-16 06:09:18 +00:00
|
|
|
// (described in (Krawczyk 2001/2014,
|
|
|
|
// https://link.springer.com/content/pdf/10.1007%2F3-540-44647-8_19.pdf)
|
2020-12-16 05:15:15 +00:00
|
|
|
//
|
2020-12-16 06:09:18 +00:00
|
|
|
// Nevertheless, to address any future concerns v0.9 onwards switches to
|
|
|
|
// Encrypt-then-Auth and breaks interop with earlier versions.
|
2020-12-16 05:15:15 +00:00
|
|
|
// -rlm 2020-12-15
|
|
|
|
|
2018-09-05 05:24:16 +00:00
|
|
|
var wb bytes.Buffer
|
|
|
|
// The StreamWriter acts like a pipe, forwarding whatever is
|
|
|
|
// written to it through the cipher, encrypting as it goes
|
|
|
|
ws := &cipher.StreamWriter{S: hc.w, W: &wb}
|
2021-01-11 06:04:52 +00:00
|
|
|
wN, err := ws.Write(b[0:payloadLen])
|
2018-09-05 05:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-01-11 06:04:52 +00:00
|
|
|
if wN < int(payloadLen) {
|
|
|
|
panic("truncated Write to cipher *****")
|
|
|
|
}
|
|
|
|
|
2019-06-20 04:42:34 +00:00
|
|
|
if hc.logCipherText {
|
2021-01-11 06:04:52 +00:00
|
|
|
log.Printf(" >:ctext:\r\n%s\r\n", hex.Dump(wb.Bytes()))
|
2019-06-20 04:42:34 +00:00
|
|
|
}
|
2021-01-11 06:04:52 +00:00
|
|
|
//fmt.Printf(" >:ctext:\r\n%s\r\n", hex.Dump(wb.Bytes()))
|
2018-09-03 02:58:13 +00:00
|
|
|
|
2020-12-16 06:09:18 +00:00
|
|
|
// Calculate hmac on cipher payload
|
|
|
|
hc.wm.Write(wb.Bytes())
|
|
|
|
hmacOut = hc.wm.Sum(nil)[0:HMAC_CHK_SZ] //finalize
|
|
|
|
//log.Printf(" (%08x> HMAC(o):%s\r\n", payloadLen, hex.EncodeToString(hmacOut))
|
|
|
|
|
2018-10-09 04:31:11 +00:00
|
|
|
err = binary.Write(*hc.c, binary.BigEndian, &ctrlStatOp)
|
2018-09-05 05:24:16 +00:00
|
|
|
if err == nil {
|
|
|
|
// Write hmac LSB, payloadLen followed by payload
|
2018-10-09 04:31:11 +00:00
|
|
|
err = binary.Write(*hc.c, binary.BigEndian, hmacOut)
|
2018-05-04 06:53:47 +00:00
|
|
|
if err == nil {
|
2018-10-09 04:31:11 +00:00
|
|
|
err = binary.Write(*hc.c, binary.BigEndian, payloadLen)
|
2018-05-04 06:53:47 +00:00
|
|
|
if err == nil {
|
2018-10-09 04:31:11 +00:00
|
|
|
n, err = (*hc.c).Write(wb.Bytes())
|
2018-09-05 05:24:16 +00:00
|
|
|
} else {
|
|
|
|
//fmt.Println("[c]WriteError!")
|
2018-05-04 06:53:47 +00:00
|
|
|
}
|
2018-09-05 05:24:16 +00:00
|
|
|
} else {
|
|
|
|
//fmt.Println("[b]WriteError!")
|
2018-05-04 06:53:47 +00:00
|
|
|
}
|
2018-09-05 05:24:16 +00:00
|
|
|
} else {
|
|
|
|
//fmt.Println("[a]WriteError!")
|
2018-01-13 06:13:01 +00:00
|
|
|
}
|
2018-04-28 23:05:33 +00:00
|
|
|
|
2018-02-17 06:12:27 +00:00
|
|
|
if err != nil {
|
2018-03-27 04:58:42 +00:00
|
|
|
log.Println(err)
|
2018-02-17 06:12:27 +00:00
|
|
|
}
|
2018-10-02 03:35:50 +00:00
|
|
|
|
|
|
|
// We must 'lie' to caller indicating the length of THEIR
|
|
|
|
// data written (ie., not including the padding and padding headers)
|
2021-01-11 06:04:52 +00:00
|
|
|
retN := n - 2 - int(padLen)
|
|
|
|
if retN <= 0 {
|
|
|
|
retN = 0
|
|
|
|
}
|
|
|
|
return retN, err
|
2018-01-09 03:16:55 +00:00
|
|
|
}
|
2018-05-02 19:28:56 +00:00
|
|
|
|
2023-11-05 22:58:24 +00:00
|
|
|
func (hc *Conn) StartupChaff() {
|
2018-06-30 02:23:11 +00:00
|
|
|
hc.chaff.shutdown = false
|
2018-05-07 00:41:09 +00:00
|
|
|
log.Println("Chaffing ENABLED")
|
2018-06-30 02:23:11 +00:00
|
|
|
hc.chaffHelper()
|
2018-05-07 00:41:09 +00:00
|
|
|
}
|
2018-05-04 06:53:47 +00:00
|
|
|
|
2018-06-30 02:23:11 +00:00
|
|
|
func (hc *Conn) ShutdownChaff() {
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Lock()
|
2018-06-30 02:23:11 +00:00
|
|
|
hc.chaff.shutdown = true
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Unlock()
|
2018-06-27 03:14:43 +00:00
|
|
|
log.Println("Chaffing SHUTDOWN")
|
|
|
|
}
|
|
|
|
|
2018-06-30 02:23:11 +00:00
|
|
|
func (hc *Conn) SetupChaff(msecsMin uint, msecsMax uint, szMax uint) {
|
2023-12-04 03:22:05 +00:00
|
|
|
// Enforce bounds on chaff frequency and pkt size
|
|
|
|
hc.Lock()
|
|
|
|
if hc.chaff.msecsMin < CHAFF_FREQ_MSECS_MIN {
|
|
|
|
hc.chaff.msecsMin = CHAFF_FREQ_MSECS_MIN
|
|
|
|
}
|
|
|
|
if hc.chaff.msecsMax > CHAFF_FREQ_MSECS_MAX {
|
|
|
|
hc.chaff.msecsMax = CHAFF_FREQ_MSECS_MAX
|
|
|
|
}
|
|
|
|
hc.Unlock()
|
|
|
|
|
2018-06-30 02:23:11 +00:00
|
|
|
hc.chaff.msecsMin = msecsMin //move these to params of chaffHelper() ?
|
|
|
|
hc.chaff.msecsMax = msecsMax
|
|
|
|
hc.chaff.szMax = szMax
|
2018-05-04 06:53:47 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 08:32:50 +00:00
|
|
|
func (hc *Conn) ShutdownRekey() {
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Lock()
|
2023-11-15 08:32:50 +00:00
|
|
|
hc.rekey = 0
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Unlock()
|
2023-11-15 08:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *Conn) RekeyHelper(intervalSecs uint) {
|
2023-12-02 09:58:30 +00:00
|
|
|
if intervalSecs < REKEY_SECS_MIN {
|
|
|
|
intervalSecs = REKEY_SECS_MIN
|
|
|
|
}
|
2023-12-04 03:22:05 +00:00
|
|
|
if intervalSecs > REKEY_SECS_MAX {
|
|
|
|
intervalSecs = REKEY_SECS_MAX
|
|
|
|
}
|
2023-12-02 09:58:30 +00:00
|
|
|
|
2023-11-15 08:32:50 +00:00
|
|
|
go func() {
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Lock()
|
2023-11-15 08:32:50 +00:00
|
|
|
hc.rekey = intervalSecs
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Unlock()
|
|
|
|
|
2023-11-15 08:32:50 +00:00
|
|
|
for {
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Lock()
|
|
|
|
rekey := hc.rekey
|
|
|
|
hc.Unlock()
|
2023-12-04 03:22:05 +00:00
|
|
|
|
2023-12-02 09:58:30 +00:00
|
|
|
if rekey != 0 {
|
2023-12-04 03:22:05 +00:00
|
|
|
jitter := rand.Intn(int(rekey)) / 4
|
|
|
|
rekey = rekey - uint(jitter)
|
|
|
|
if rekey < 1 {
|
|
|
|
rekey = 1
|
|
|
|
}
|
|
|
|
|
2023-11-15 08:32:50 +00:00
|
|
|
//logger.LogDebug(fmt.Sprintf("[rekeyHelper Loop]\n"))
|
2023-12-02 09:58:30 +00:00
|
|
|
time.Sleep(time.Duration(rekey) * time.Second)
|
2023-11-15 08:32:50 +00:00
|
|
|
|
|
|
|
// Send rekey to other end
|
|
|
|
rekeyData := make([]byte, 64)
|
|
|
|
_, err := crand.Read(rekeyData)
|
|
|
|
//logger.LogDebug(fmt.Sprintf("[rekey [%02x %02x %02x ...]\n",
|
|
|
|
// rekeyData[0], rekeyData[1], rekeyData[2]))
|
|
|
|
//logger.LogDebug("[+rekeyHelper]")
|
|
|
|
_, err = hc.WritePacket(rekeyData, CSORekey)
|
|
|
|
hc.Lock()
|
2023-12-02 09:58:30 +00:00
|
|
|
if (hc.opts & CORemodulateShields) != 0 {
|
|
|
|
hc.cipheropts = getNewStreamAlgs(rekeyData[0], rekeyData[1])
|
|
|
|
}
|
2023-11-15 08:32:50 +00:00
|
|
|
hc.w, hc.wm, err = hc.getStream(rekeyData)
|
|
|
|
//logger.LogDebug("[-rekeyHelper]")
|
|
|
|
hc.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[rekey WritePacket err! (%v) rekey dying ...]\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-05-04 06:53:47 +00:00
|
|
|
// Helper routine to spawn a chaffing goroutine for each Conn
|
2018-06-30 02:23:11 +00:00
|
|
|
func (hc *Conn) chaffHelper() {
|
2018-05-04 06:53:47 +00:00
|
|
|
go func() {
|
2023-11-05 22:58:24 +00:00
|
|
|
var nextDuration int
|
2018-05-04 06:53:47 +00:00
|
|
|
for {
|
2023-11-05 22:58:24 +00:00
|
|
|
//logger.LogDebug(fmt.Sprintf("[chaffHelper Loop]\n"))
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Lock()
|
|
|
|
shutdown := hc.chaff.shutdown
|
|
|
|
hc.Unlock()
|
|
|
|
if !shutdown {
|
2018-09-27 05:57:36 +00:00
|
|
|
var bufTmp []byte
|
2018-10-02 03:35:50 +00:00
|
|
|
bufTmp = make([]byte, rand.Intn(int(hc.chaff.szMax)))
|
2018-06-30 02:23:11 +00:00
|
|
|
min := int(hc.chaff.msecsMin)
|
|
|
|
nextDuration = rand.Intn(int(hc.chaff.msecsMax)-min) + min
|
2018-05-07 00:41:09 +00:00
|
|
|
_, _ = rand.Read(bufTmp)
|
2023-11-15 08:32:50 +00:00
|
|
|
//logger.LogDebug("[+chaffHelper]")
|
2018-06-30 02:23:11 +00:00
|
|
|
_, err := hc.WritePacket(bufTmp, CSOChaff)
|
2023-11-15 08:32:50 +00:00
|
|
|
//logger.LogDebug("[-chaffHelper]")
|
2018-05-04 06:53:47 +00:00
|
|
|
if err != nil {
|
2023-11-05 22:58:24 +00:00
|
|
|
log.Println("[ *** error - chaffHelper shutting down *** ]")
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Lock()
|
2023-11-05 22:58:24 +00:00
|
|
|
hc.chaff.shutdown = true
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Unlock()
|
2018-05-04 06:53:47 +00:00
|
|
|
break
|
|
|
|
}
|
2023-11-05 22:58:24 +00:00
|
|
|
} else {
|
|
|
|
log.Println("[ *** chaffHelper shutting down *** ]")
|
|
|
|
break
|
2018-05-04 06:53:47 +00:00
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(nextDuration) * time.Millisecond)
|
2023-11-05 22:58:24 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *Conn) StartupKeepAlive() {
|
|
|
|
hc.ResetKeepAlive()
|
|
|
|
log.Println("KeepAlive ENABLED")
|
|
|
|
hc.keepaliveHelper()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *Conn) ShutdownKeepAlive() {
|
|
|
|
log.Println("Conn SHUTDOWN")
|
|
|
|
hc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *Conn) ResetKeepAlive() {
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Lock()
|
2023-11-05 22:58:24 +00:00
|
|
|
hc.keepalive = 3
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Unlock()
|
2023-11-05 22:58:24 +00:00
|
|
|
log.Println("KeepAlive RESET")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper routine to spawn a keepalive goroutine for each Conn
|
|
|
|
func (hc *Conn) keepaliveHelper() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
nextDuration := 10000
|
|
|
|
bufTmp := []byte{0x55, 0xaa}
|
2023-11-15 08:32:50 +00:00
|
|
|
//logger.LogDebug("[+keepaliveHelper]")
|
2023-11-05 22:58:24 +00:00
|
|
|
_, err := hc.WritePacket(bufTmp, CSOKeepAlive)
|
2023-11-15 08:32:50 +00:00
|
|
|
//logger.LogDebug("[-keepaliveHelper]")
|
2023-11-07 08:38:51 +00:00
|
|
|
//logger.LogDebug(fmt.Sprintf("[keepalive]\n"))
|
2023-11-05 22:58:24 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.LogDebug(fmt.Sprintf("[ *** error - keepaliveHelper quitting *** ]\n"))
|
2018-06-27 03:14:43 +00:00
|
|
|
break
|
|
|
|
}
|
2023-11-05 22:58:24 +00:00
|
|
|
time.Sleep(time.Duration(nextDuration) * time.Millisecond)
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Lock()
|
2023-11-05 22:58:24 +00:00
|
|
|
hc.keepalive -= 1
|
2023-12-02 09:58:30 +00:00
|
|
|
hc.Unlock()
|
2023-11-07 08:38:51 +00:00
|
|
|
//logger.LogDebug(fmt.Sprintf("[keepAlive is now %d]\n", hc.keepalive))
|
2023-11-07 07:17:13 +00:00
|
|
|
|
|
|
|
//if rand.Intn(8) == 0 {
|
2023-11-05 22:58:24 +00:00
|
|
|
// hc.keepalive = 0
|
|
|
|
//}
|
2018-06-27 03:14:43 +00:00
|
|
|
|
2023-11-05 22:58:24 +00:00
|
|
|
if hc.keepalive == 0 {
|
|
|
|
logger.LogDebug(fmt.Sprintf("*** keepaliveHelper shutting down\n"))
|
2023-11-07 05:57:19 +00:00
|
|
|
hc.SetStatus(CSEConnDead)
|
2023-11-05 22:58:24 +00:00
|
|
|
hc.ShutdownKeepAlive()
|
2023-11-07 07:17:13 +00:00
|
|
|
if hc.Pproc != 0 {
|
|
|
|
//fmt.Printf("[pid %d needs to be killed]\n", hc.Pproc)
|
|
|
|
syscall.Kill(hc.Pproc, syscall.SIGABRT) //nolint:errcheck
|
|
|
|
}
|
2023-11-05 22:58:24 +00:00
|
|
|
break
|
|
|
|
}
|
2018-05-04 06:53:47 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|