tWhitespace fixes - tordam - A library for peer discovery inside the Tor network (HTM) git clone https://git.parazyd.org/tordam (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit 8a7d4978609d7a0d1fcf8ff24d0d2c377724aed3 (DIR) parent 9f3f66b4104bb19c9a3beda0afb565041d097be8 (HTM) Author: parazyd <parazyd@dyne.org> Date: Fri, 8 Dec 2017 19:03:03 +0100 Whitespace fixes Diffstat: M cmd/dam-client/main.go | 4 +--- M cmd/dam-dir/main.go | 6 ------ M pkg/lib/crypto.go | 20 -------------------- M pkg/lib/helpers.go | 8 -------- M python/decodehs.py | 1 + 5 files changed, 2 insertions(+), 37 deletions(-) --- (DIR) diff --git a/cmd/dam-client/main.go b/cmd/dam-client/main.go t@@ -87,12 +87,10 @@ func main() { "signature": encodedSig, "secret": "", } - - log.Println("Announcing keypair for:", vals["address"]) - jsonVal, err := json.Marshal(vals) lib.CheckError(err) + log.Println("Announcing keypair for:", vals["address"]) log.Println("Sending request") resp, err := lib.HTTPPost("http://localhost:8080/announce", jsonVal) lib.CheckError(err) (DIR) diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go t@@ -86,9 +86,6 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { randString, err := lib.GenRandomASCII(64) lib.CheckError(err) - // FIXME: delete this line after debug mode - log.Println("Secret:", randString) - secret, err := lib.EncryptMsg([]byte(randString), pubkey) lib.CheckError(err) t@@ -118,7 +115,6 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { info["firstseen"] = n.Firstseen info["valid"] = 0 // This should be 1 after the node is not considered malicious } - log.Println("Writing to Redis") redRet, err := RedisCli.HMSet(n.Address, info).Result() lib.CheckError(err) t@@ -142,14 +138,12 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { log.Println("Secrets match!") correct = true } - if correct { log.Printf("Welcoming %s to the network\n", n.Address) ret := map[string]string{ "secret": "Welcome to the DECODE network!", } n.Valid = 0 - jsonVal, err := json.Marshal(ret) lib.CheckError(err) (DIR) diff --git a/pkg/lib/crypto.go b/pkg/lib/crypto.go t@@ -39,17 +39,14 @@ func SavePub(filename string, pubkey rsa.PublicKey) (bool, error) { if err != nil { return false, err } - asn1Bytes, err := asn1.Marshal(pubkey) if err != nil { return false, err } - var pemkey = &pem.Block{ Type: "RSA PUBLIC KEY", Bytes: asn1Bytes, } - err = pem.Encode(outfile, pemkey) if err != nil { return false, err t@@ -70,12 +67,10 @@ func SavePriv(filename string, privkey *rsa.PrivateKey) (bool, error) { if err != nil { return false, err } - var pemkey = &pem.Block{ Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privkey), } - err = pem.Encode(outfile, pemkey) if err != nil { return false, err t@@ -94,17 +89,14 @@ func LoadKeyFromFile(filename string) (*rsa.PrivateKey, error) { if err != nil { return nil, err } - block, _ := pem.Decode(dat) if block == nil { return nil, errors.New("failed to parse PEM block containing the key") } - priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) if err != nil { return nil, err } - return priv, nil } t@@ -112,13 +104,11 @@ func LoadKeyFromFile(filename string) (*rsa.PrivateKey, error) { func SignMsg(message []byte, privkey *rsa.PrivateKey) ([]byte, error) { log.Println("Signing message...") rng := rand.Reader - hashed := sha512.Sum512(message) sig, err := rsa.SignPKCS1v15(rng, privkey, crypto.SHA512, hashed[:]) if err != nil { return nil, err } - return sig, nil } t@@ -127,12 +117,10 @@ func SignMsg(message []byte, privkey *rsa.PrivateKey) ([]byte, error) { func EncryptMsg(message []byte, pubkey *rsa.PublicKey) ([]byte, error) { log.Println("Encrypting message...") rng := rand.Reader - msg, err := rsa.EncryptPKCS1v15(rng, pubkey, message) if err != nil { return nil, err } - return msg, nil } t@@ -141,12 +129,10 @@ func EncryptMsg(message []byte, pubkey *rsa.PublicKey) ([]byte, error) { func DecryptMsg(message []byte, privkey *rsa.PrivateKey) ([]byte, error) { log.Println("Decrypting message...") rng := rand.Reader - msg, err := rsa.DecryptPKCS1v15(rng, privkey, message) if err != nil { return nil, err } - return msg, nil } t@@ -154,13 +140,11 @@ func DecryptMsg(message []byte, privkey *rsa.PrivateKey) ([]byte, error) { // RSA pubkey. func VerifyMsg(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 { return false, err } - log.Println("Signature valid") return true, nil } t@@ -171,13 +155,11 @@ func OnionFromPubkey(pubkey rsa.PublicKey) ([]byte, error) { if err != nil { return nil, err } - hashed := sha1.New() _, err = hashed.Write(asn1Bytes) if err != nil { return nil, err } - encoded := strings.ToLower(base32.StdEncoding.EncodeToString(hashed.Sum(nil)))[:16] encoded += ".onion" t@@ -189,13 +171,11 @@ func OnionFromPubkey(pubkey rsa.PublicKey) ([]byte, error) { func ParsePubkey(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 { return nil, err } - ret = &pub return ret, nil } (DIR) diff --git a/pkg/lib/helpers.go b/pkg/lib/helpers.go t@@ -37,7 +37,6 @@ func FetchHSPubkey(addr string) string { cmd := exec.Command("dirauth.py", addr) cmd.Stdout = &outb cmd.Stderr = &errb - err := cmd.Start() CheckError(err) t@@ -56,14 +55,12 @@ func ValidateReq(req map[string]string) ([]byte, bool) { if req["nodetype"] != "node" { return nil, false } - // Validate address. re, err := regexp.Compile("^[a-z2-7]{16}\\.onion$") CheckError(err) if len(re.FindString(req["address"])) != 22 { return nil, false } - // Address is valid, we try to fetch its pubkey from a HSDir var pubkey string var cnt = 0 t@@ -83,7 +80,6 @@ func ValidateReq(req map[string]string) ([]byte, bool) { } time.Sleep(2000 * time.Millisecond) } - // Validate signature. msg := []byte(req["message"]) sig := []byte(req["signature"]) t@@ -103,7 +99,6 @@ func ValidateReq(req map[string]string) ([]byte, bool) { // application/json. func HTTPPost(host string, data []byte) (*http.Response, error) { socksify := false - parsedHost, err := url.Parse(host) if err != nil { return nil, err t@@ -112,7 +107,6 @@ func HTTPPost(host string, data []byte) (*http.Response, error) { if strings.HasSuffix(hostname, ".onion") { socksify = true } - httpTransp := &http.Transport{} httpClient := &http.Client{Transport: httpTransp} if socksify { t@@ -123,7 +117,6 @@ func HTTPPost(host string, data []byte) (*http.Response, error) { } httpTransp.Dial = dialer.Dial } - request, err := http.NewRequest("POST", host, bytes.NewBuffer(data)) if err != nil { return nil, err t@@ -149,7 +142,6 @@ func GenRandomASCII(length int) (string, error) { if err != nil { return "", err } - n := num.Int64() if n > 32 && n < 127 { res += string(n) (DIR) diff --git a/python/decodehs.py b/python/decodehs.py t@@ -54,5 +54,6 @@ def main(): stdout.flush() sleep(10) + if __name__ == '__main__': main()