From 2e872c6b206bdd5294c7256a6d116c7935a69ef6 Mon Sep 17 00:00:00 2001 From: idk Date: Wed, 13 Feb 2019 23:58:24 -0500 Subject: [PATCH] Add signature support --- .gitignore | 1 + README.md.asc | 16 ++++++++-------- sam3.go | 33 ++++++++++++++++++++++++++++----- stream.go | 17 ++++++++++++++++- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index b72f9be..662a97a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *~ *.swp +README.md.asc diff --git a/README.md.asc b/README.md.asc index 80b6563..4901c8b 100644 --- a/README.md.asc +++ b/README.md.asc @@ -94,12 +94,12 @@ Public domain. * Unknown Name (majestrate) -----BEGIN PGP SIGNATURE----- -iQEzBAEBCgAdFiEEcNIGBzi++AUjrK/311wDs5teFOEFAlxfT1MACgkQ11wDs5te -FOFy8Af/VM3Qwk5Sonlh+RGJomdjtjE6hiMWw2Pm/CWG8WVVOURN3aVNP9NRXtcW -Hv8j0SiIuOuAQ08McJjiCbOxYAD8kJmGqkKstie9vaVQNnkhLj6PB4a+gJUvmhp5 -F5mdpEiS/zidOqqYlCHnslGJaUxEQWu06wpuCP+855bfeHLVIP00Yil5KOor7bAI -AWvvIYUEhleq/b5dz0AmYPCDuoH7FkONaTM8sfcF1cnq/WNS16YyksptzaFzlBk5 -7TFOfXbp2D4Mrvc7zvQyInpao6hovjr/KKGU8BQof0VPEm4PlZzTb77Rl6YHw5sl -1e3i3exMZoaZEF+0tB7WoR1mRa/UVQ== -=Ug2d +iQEzBAEBCgAdFiEEcNIGBzi++AUjrK/311wDs5teFOEFAlxk9W0ACgkQ11wDs5te +FOEl8wf/VinlOV3Op5fZ6Qr9eTanj85FoH9KbjA0P6EcUJap4QWoEUIK/mkIlhl/ +jLdAMHry5K3gXObFrVAiO4XvsAF++/JJQroUWPb++UR9ksd+M4b63ia3/BtFQGUu +J2w6dK7S79Z6IumhA6xhdDaIRGiJSu8Ox0M36ZKkfAR3WTYfsz+Nucp+1l8otNhI +fjQrmLCSgAiaCW0h3m208JR20FYipjHc7CZBzF/TVCjPq9qleEMXQCTJBWcuibrR +RdoUct6oMAXNS4S7k4LYa5FK0ETNIap9aUyFa8Tp6tmzhSOun7/Gle3ynH/c9SrO +lopjX4BT4mpXN1Lvwdw+v3tTZPyuWQ== +=lYsZ -----END PGP SIGNATURE----- diff --git a/sam3.go b/sam3.go index ddbee4c..211fc08 100644 --- a/sam3.go +++ b/sam3.go @@ -17,6 +17,7 @@ type SAM struct { conn net.Conn resolver *SAMResolver keys *I2PKeys + sigType int } const ( @@ -27,6 +28,15 @@ const ( session_I2P_ERROR = "SESSION STATUS RESULT=I2P_ERROR MESSAGE=" ) +var SIG_TYPES = []string{ + "", + "SIGNATURE_TYPE=DSA_SHA1", + "SIGNATURE_TYPE=ECDSA_SHA256_P256", + "SIGNATURE_TYPE=ECDSA_SHA384_P384", + "SIGNATURE_TYPE=ECDSA_SHA512_P521", + "SIGNATURE_TYPE=EdDSA_SHA512_Ed25519", +} + // Creates a new controller for the I2P routers SAM bridge. func NewSAM(address string) (*SAM, error) { var s SAM @@ -35,7 +45,7 @@ func NewSAM(address string) (*SAM, error) { if err != nil { return nil, err } - if _, err := conn.Write([]byte("HELLO VERSION MIN=3.0 MAX=3.0\n")); err != nil { + if _, err := conn.Write([]byte("HELLO VERSION MIN=3.0 MAX=3.1\n")); err != nil { conn.Close() return nil, err } @@ -45,7 +55,7 @@ func NewSAM(address string) (*SAM, error) { conn.Close() return nil, err } - if string(buf[:n]) == "HELLO REPLY RESULT=OK VERSION=3.0\n" { + if strings.Contains(string(buf[:n]), "HELLO REPLY RESULT=OK") { s.address = address s.conn = conn s.keys = nil @@ -122,8 +132,12 @@ func (sam *SAM) EnsureKeyfile(fname string) (keys I2PKeys, err error) { // Creates the I2P-equivalent of an IP address, that is unique and only the one // who has the private keys can send messages from. The public keys are the I2P // desination (the address) that anyone can send messages to. -func (sam *SAM) NewKeys() (I2PKeys, error) { - if _, err := sam.conn.Write([]byte("DEST GENERATE\n")); err != nil { +func (sam *SAM) NewKeys(sigType ...int) (I2PKeys, error) { + sigtmp := 0 + if len(sigType) > 0 { + sigtmp = sigType[0] + } + if _, err := sam.conn.Write([]byte("DEST GENERATE" + SIG_TYPES[sigtmp] + "\n")); err != nil { return I2PKeys{}, err } buf := make([]byte, 8192) @@ -164,6 +178,15 @@ func (sam *SAM) Lookup(name string) (I2PAddr, error) { // setting extra to something else than []string{}. // This sam3 instance is now a session func (sam *SAM) newGenericSession(style, id string, keys I2PKeys, options []string, extras []string) (net.Conn, error) { + return sam.newGenericSessionWithSignature(style, id, keys, 0, options, extras) +} + +// Creates a new session with the style of either "STREAM", "DATAGRAM" or "RAW", +// for a new I2P tunnel with name id, using the cypher keys specified, with the +// I2CP/streaminglib-options as specified. Extra arguments can be specified by +// setting extra to something else than []string{}. +// This sam3 instance is now a session +func (sam *SAM) newGenericSessionWithSignature(style, id string, keys I2PKeys, sigType int, options []string, extras []string) (net.Conn, error) { optStr := "" for _, opt := range options { @@ -171,7 +194,7 @@ func (sam *SAM) newGenericSession(style, id string, keys I2PKeys, options []stri } conn := sam.conn - scmsg := []byte("SESSION CREATE STYLE=" + style + " ID=" + id + " DESTINATION=" + keys.String() + " " + optStr + strings.Join(extras, " ") + "\n") + scmsg := []byte("SESSION CREATE STYLE=" + style + " ID=" + id + " DESTINATION=" + keys.String() + " " + SIG_TYPES[sigType] + " " + optStr + strings.Join(extras, " ") + "\n") for m, i := 0, 0; m != len(scmsg); i++ { if i == 15 { conn.Close() diff --git a/stream.go b/stream.go index 58606e8..8f6b67a 100644 --- a/stream.go +++ b/stream.go @@ -19,6 +19,11 @@ type StreamSession struct { keys I2PKeys // i2p destination keys Timeout time.Duration Deadline time.Time + sigType int +} + +func (ss *StreamSession) SignatureType() string { + return SIG_TYPES[ss.sigType] } // Returns the local tunnel name of the I2P tunnel used for the stream session @@ -47,7 +52,17 @@ func (sam *SAM) NewStreamSession(id string, keys I2PKeys, options []string) (*St if err != nil { return nil, err } - return &StreamSession{sam.address, id, conn, keys, time.Duration(600 * time.Second), time.Now()}, nil + return &StreamSession{sam.address, id, conn, keys, time.Duration(600 * time.Second), time.Now(), 0}, nil +} + +// Creates a new StreamSession with the I2CP- and streaminglib options as +// specified. See the I2P documentation for a full list of options. +func (sam *SAM) NewStreamSessionWithSignature(id string, keys I2PKeys, options []string, sigType int) (*StreamSession, error) { + conn, err := sam.newGenericSessionWithSignature("STREAM", id, keys, sigType, options, []string{}) + if err != nil { + return nil, err + } + return &StreamSession{sam.address, id, conn, keys, time.Duration(600 * time.Second), time.Now(), sigType}, nil } // lookup name, convienence function