tordam

A library for peer discovery inside the Tor network
git clone https://git.parazyd.org/tordam
Log | Files | Refs | README | LICENSE

commit 7a8b2ca208e22bd2c99b47af8eec7b96e1bda418
parent 2b43a7b2ad6b6652dd63df16dc33816fa02ca617
Author: parazyd <parazyd@dyne.org>
Date:   Wed, 13 Dec 2017 03:36:48 +0100

Style fixes.

Diffstat:
Mcmd/dam-client/main.go | 5++---
Mcmd/dam-dir/main.go | 6+++---
Mpkg/damlib/crypto_rsa.go | 23+++++++----------------
Mpkg/damlib/tor.go | 3+--
Mpkg/damlib/validate.go | 17+++++++----------
5 files changed, 20 insertions(+), 34 deletions(-)

diff --git a/cmd/dam-client/main.go b/cmd/dam-client/main.go @@ -185,12 +185,11 @@ func main() { } wg.Wait() - if ann > 0 { - log.Printf("Successfully authenticated with %d nodes.\n", ann) - } else { + if ann < 1 { cmd.Process.Kill() log.Fatalln("No successful authentications. Exiting.") } + log.Printf("Successfully authenticated with %d nodes.\n", ann) err = cmd.Wait() // Hidden service Python daemon lib.CheckError(err) diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go @@ -65,8 +65,7 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { } decoder := json.NewDecoder(request.Body) - err := decoder.Decode(&n) - if err != nil { + if err := decoder.Decode(&n); err != nil { log.Println("Failed decoding request:", err) return } @@ -142,8 +141,9 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { } } +// handleElse is a noop for anything that isn't /announce. We don't care about +// other requests (yet). func handleElse(rw http.ResponseWriter, request *http.Request) { - // noop for anything that isn't /announce. return } diff --git a/pkg/damlib/crypto_rsa.go b/pkg/damlib/crypto_rsa.go @@ -49,12 +49,10 @@ func SavePubRsa(filename string, pubkey rsa.PublicKey) error { Type: "RSA PUBLIC KEY", Bytes: asn1Bytes, } - err = pem.Encode(outfile, pemkey) - if err != nil { + if err = pem.Encode(outfile, pemkey); err != nil { return err } - err = outfile.Chmod(0400) - if err != nil { + if err = outfile.Chmod(0400); err != nil { return err } return nil @@ -74,12 +72,10 @@ func SavePrivRsa(filename string, privkey *rsa.PrivateKey) error { Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privkey), } - err = pem.Encode(outfile, pemkey) - if err != nil { + if err = pem.Encode(outfile, pemkey); err != nil { return err } - err = outfile.Chmod(0400) - if err != nil { + if err = outfile.Chmod(0400); err != nil { return err } return nil @@ -151,12 +147,9 @@ func DecryptMsgRsa(message []byte, privkey *rsa.PrivateKey) ([]byte, error) { func VerifyMsgRsa(message []byte, signature []byte, pubkey *rsa.PublicKey) (bool, error) { log.Println("Verifying message signature") hashed := sha512.Sum512(message) - err := rsa.VerifyPKCS1v15(pubkey, crypto.SHA512, hashed[:], signature) - if err != nil { - log.Println("Signature invalid") + if err := rsa.VerifyPKCS1v15(pubkey, crypto.SHA512, hashed[:], signature); err != nil { return false, err } - log.Println("Signature valid") return true, nil } @@ -169,8 +162,7 @@ func OnionFromPubkeyRsa(pubkey rsa.PublicKey) ([]byte, error) { return nil, err } hashed := sha1.New() - _, err = hashed.Write(asn1Bytes) - if err != nil { + if _, err = hashed.Write(asn1Bytes); err != nil { return nil, err } encoded := strings.ToLower(base32.StdEncoding.EncodeToString(hashed.Sum(nil)))[:16] @@ -185,8 +177,7 @@ func ParsePubkeyRsa(pubkey []byte) (*rsa.PublicKey, error) { var pub rsa.PublicKey var ret *rsa.PublicKey block, _ := pem.Decode(pubkey) - _, err := asn1.Unmarshal(block.Bytes, &pub) - if err != nil { + if _, err := asn1.Unmarshal(block.Bytes, &pub); err != nil { return nil, err } ret = &pub diff --git a/pkg/damlib/tor.go b/pkg/damlib/tor.go @@ -22,8 +22,7 @@ func FetchHSPubkey(addr string) string { err := cmd.Start() CheckError(err) - err = cmd.Wait() - if err != nil { + if err = cmd.Wait(); err != nil { log.Println("Could not fetch descriptor:", err) return "" } diff --git a/pkg/damlib/validate.go b/pkg/damlib/validate.go @@ -31,6 +31,7 @@ func sanityCheck(req map[string]string, handshake int) (bool, string) { if _, err := base64.StdEncoding.DecodeString(req["signature"]); err != nil { return false, err.Error() } + // TODO: When a node wants to promote itself from something it already was, // what to do? switch req["nodetype"] { @@ -69,8 +70,7 @@ func sanityCheck(req map[string]string, handshake int) (bool, string) { // On any failure, the function will return false, and produce an according // string which is to be considered as an error message. func ValidateFirstHandshake(req map[string]string) (bool, string) { - sane, what := sanityCheck(req, 1) - if !(sane) { + if sane, what := sanityCheck(req, 1); !(sane) { return false, what } @@ -114,8 +114,7 @@ func ValidateFirstHandshake(req map[string]string) (bool, string) { sig := []byte(decSig) pubkey, err := ParsePubkeyRsa([]byte(pub)) // pubkey is their public key in *rsa.PublicKey type CheckError(err) - val, _ := VerifyMsgRsa(msg, sig, pubkey) - if val != true { + if val, _ := VerifyMsgRsa(msg, sig, pubkey); !(val) { log.Println("crypto/rsa: verification failure") return false, "Signature verification failure." } @@ -170,8 +169,7 @@ func ValidateFirstHandshake(req map[string]string) (bool, string) { // will return false, and an according string which is to be considered an error // message. func ValidateSecondHandshake(req map[string]string) (bool, string) { - sane, what := sanityCheck(req, 2) - if !(sane) { + if sane, what := sanityCheck(req, 2); !(sane) { return false, what } @@ -198,7 +196,7 @@ func ValidateSecondHandshake(req map[string]string) (bool, string) { CheckError(err) if !(localSec == req["secret"] && localSec == req["message"]) { - log.Println("Secrets don't match.") + log.Printf("%s: Secrets don't match.\n", req["address"]) return false, "Secrets don't match." } @@ -208,9 +206,8 @@ func ValidateSecondHandshake(req map[string]string) (bool, string) { sig := []byte(decSig) pubkey, err := ParsePubkeyRsa([]byte(pub)) // pubkey is their public key in *rsa.PublicKey type CheckError(err) - val, _ := VerifyMsgRsa(msg, sig, pubkey) - if val != true { - log.Println("crypto/rsa: verification failure") + if val, _ := VerifyMsgRsa(msg, sig, pubkey); !(val) { + log.Printf("%s: Signature verification failure\n", req["address"]) return false, "Signature verification failure." }