2023-07-29 00:28:44 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
"encoding/json"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/ecdh"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ECDHState struct {
|
|
|
|
ECKey *ecdh.PrivateKey
|
|
|
|
SharedSecret []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type ECDHStateJSON struct {
|
|
|
|
ECKey []byte `json:"ec_key"`
|
|
|
|
SharedSecret []byte `json:"shared_secret"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (state *ECDHState) MarshalJSON() ([]byte, error) {
|
|
|
|
var key_bytes []byte
|
|
|
|
var err error
|
|
|
|
if state.ECKey != nil {
|
|
|
|
key_bytes, err = x509.MarshalPKCS8PrivateKey(state.ECKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(&ECDHStateJSON{
|
|
|
|
ECKey: key_bytes,
|
|
|
|
SharedSecret: state.SharedSecret,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (state *ECDHState) UnmarshalJSON(data []byte) error {
|
|
|
|
var j ECDHStateJSON
|
|
|
|
err := json.Unmarshal(data, &j)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.SharedSecret = j.SharedSecret
|
|
|
|
if len(j.ECKey) == 0 {
|
|
|
|
state.ECKey = nil
|
|
|
|
} else {
|
|
|
|
tmp_key, err := x509.ParsePKCS8PrivateKey(j.ECKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ecdsa_key, ok := tmp_key.(*ecdsa.PrivateKey)
|
|
|
|
if ok == false {
|
|
|
|
return fmt.Errorf("Parsed wrong key type from DB for ECDHState")
|
|
|
|
}
|
|
|
|
|
|
|
|
state.ECKey, err = ecdsa_key.ECDH()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ECDHMap map[NodeID]ECDHState
|
|
|
|
|
|
|
|
func (m ECDHMap) MarshalJSON() ([]byte, error) {
|
|
|
|
tmp := map[string]ECDHState{}
|
|
|
|
for id, state := range(m) {
|
|
|
|
tmp[id.String()] = state
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ECDHExt struct {
|
|
|
|
ECDHStates ECDHMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewECDHExt() *ECDHExt {
|
|
|
|
return &ECDHExt{
|
|
|
|
ECDHStates: ECDHMap{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ResolveFields[T Extension](t T, name string, field_funcs map[string]func(T)interface{})interface{} {
|
|
|
|
var zero T
|
|
|
|
field_func, ok := field_funcs[name]
|
|
|
|
if ok == false {
|
|
|
|
return fmt.Errorf("%s is not a field of %s", name, zero.Type())
|
|
|
|
}
|
|
|
|
return field_func(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ext *ECDHExt) Field(name string) interface{} {
|
|
|
|
return ResolveFields(ext, name, map[string]func(*ECDHExt)interface{}{
|
|
|
|
"ecdh_states": func(ext *ECDHExt) interface{} {
|
|
|
|
return ext.ECDHStates
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (ext *ECDHExt) HandleECDHSignal(ctx *Context, source NodeID, node *Node, signal *ECDHSignal) {
|
2023-07-30 23:42:47 -06:00
|
|
|
switch signal.Str {
|
2023-07-29 00:28:44 -06:00
|
|
|
case "req":
|
|
|
|
state, exists := ext.ECDHStates[source]
|
|
|
|
if exists == false {
|
|
|
|
state = ECDHState{nil, nil}
|
|
|
|
}
|
|
|
|
resp, shared_secret, err := NewECDHRespSignal(ctx, node, signal)
|
|
|
|
if err == nil {
|
|
|
|
state.SharedSecret = shared_secret
|
|
|
|
ext.ECDHStates[source] = state
|
|
|
|
ctx.Log.Logf("ecdh", "New shared secret for %s<->%s - %+v", node.ID, source, ext.ECDHStates[source].SharedSecret)
|
2023-08-01 20:55:15 -06:00
|
|
|
ctx.Send(node.ID, source, &resp)
|
2023-07-29 00:28:44 -06:00
|
|
|
} else {
|
|
|
|
ctx.Log.Logf("ecdh", "ECDH_REQ_ERR: %s", err)
|
|
|
|
// TODO: send error response
|
|
|
|
}
|
|
|
|
case "resp":
|
|
|
|
state, exists := ext.ECDHStates[source]
|
|
|
|
if exists == false || state.ECKey == nil {
|
2023-08-01 20:55:15 -06:00
|
|
|
resp := NewErrorSignal(signal.ID(), "no_req")
|
|
|
|
ctx.Send(node.ID, source, &resp)
|
2023-07-29 00:28:44 -06:00
|
|
|
} else {
|
|
|
|
err := VerifyECDHSignal(time.Now(), signal, DEFAULT_ECDH_WINDOW)
|
|
|
|
if err == nil {
|
|
|
|
shared_secret, err := state.ECKey.ECDH(signal.ECDH)
|
|
|
|
if err == nil {
|
|
|
|
state.SharedSecret = shared_secret
|
|
|
|
state.ECKey = nil
|
|
|
|
ext.ECDHStates[source] = state
|
|
|
|
ctx.Log.Logf("ecdh", "New shared secret for %s<->%s - %+v", node.ID, source, ext.ECDHStates[source].SharedSecret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2023-07-30 23:42:47 -06:00
|
|
|
ctx.Log.Logf("ecdh", "unknown echd state %s", signal.Str)
|
2023-07-29 00:28:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (ext *ECDHExt) HandleStateSignal(ctx *Context, source NodeID, node *Node, signal *StringSignal) {
|
2023-07-29 00:28:44 -06:00
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (ext *ECDHExt) HandleECDHProxySignal(ctx *Context, source NodeID, node *Node, signal *ECDHProxySignal) {
|
2023-07-29 00:28:44 -06:00
|
|
|
state, exists := ext.ECDHStates[source]
|
|
|
|
if exists == false {
|
2023-08-01 20:55:15 -06:00
|
|
|
resp := NewErrorSignal(signal.ID(), "no_req")
|
|
|
|
ctx.Send(node.ID, source, &resp)
|
2023-07-29 00:28:44 -06:00
|
|
|
} else if state.SharedSecret == nil {
|
2023-08-01 20:55:15 -06:00
|
|
|
resp := NewErrorSignal(signal.ID(), "no_shared")
|
|
|
|
ctx.Send(node.ID, source, &resp)
|
2023-07-29 00:28:44 -06:00
|
|
|
} else {
|
2023-08-01 20:55:15 -06:00
|
|
|
unwrapped_signal, err := ParseECDHProxySignal(ctx, signal, state.SharedSecret)
|
2023-07-29 00:28:44 -06:00
|
|
|
if err != nil {
|
2023-08-01 20:55:15 -06:00
|
|
|
resp := NewErrorSignal(signal.ID(), err.Error())
|
|
|
|
ctx.Send(node.ID, source, &resp)
|
2023-07-29 00:28:44 -06:00
|
|
|
} else {
|
|
|
|
//TODO: Figure out what I was trying to do here and fix it
|
|
|
|
ctx.Send(signal.Source, signal.Dest, unwrapped_signal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ext *ECDHExt) Process(ctx *Context, source NodeID, node *Node, signal Signal) {
|
|
|
|
switch signal.Direction() {
|
|
|
|
case Direct:
|
|
|
|
switch signal.Type() {
|
|
|
|
case ECDHProxySignalType:
|
2023-08-01 20:55:15 -06:00
|
|
|
ecdh_signal := signal.(*ECDHProxySignal)
|
2023-07-29 00:28:44 -06:00
|
|
|
ext.HandleECDHProxySignal(ctx, source, node, ecdh_signal)
|
|
|
|
case ECDHStateSignalType:
|
2023-08-01 20:55:15 -06:00
|
|
|
ecdh_signal := signal.(*StringSignal)
|
2023-07-29 00:28:44 -06:00
|
|
|
ext.HandleStateSignal(ctx, source, node, ecdh_signal)
|
|
|
|
case ECDHSignalType:
|
2023-08-01 20:55:15 -06:00
|
|
|
ecdh_signal := signal.(*ECDHSignal)
|
2023-07-29 00:28:44 -06:00
|
|
|
ext.HandleECDHSignal(ctx, source, node, ecdh_signal)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ext *ECDHExt) Type() ExtType {
|
|
|
|
return ECDHExtType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ext *ECDHExt) Serialize() ([]byte, error) {
|
2023-08-01 20:55:15 -06:00
|
|
|
return json.Marshal(ext)
|
2023-07-29 00:28:44 -06:00
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (ext *ECDHExt) Deserialize(ctx *Context, data []byte) error {
|
|
|
|
return json.Unmarshal(data, &ext)
|
2023-07-29 00:28:44 -06:00
|
|
|
}
|