diff --git a/lib/common/certificate/certificate.go b/lib/common/certificate/certificate.go index d50a85f..6fc4208 100644 --- a/lib/common/certificate/certificate.go +++ b/lib/common/certificate/certificate.go @@ -172,14 +172,14 @@ func readCertificate(data []byte) (certificate Certificate, err error) { default: certificate.kind = Integer(data[0:1]) certificate.len = Integer(data[1:3]) - payleng := len(data) - CERT_MIN_SIZE + payloadLength := len(data) - CERT_MIN_SIZE certificate.payload = data[CERT_MIN_SIZE:] if certificate.len.Int() > len(data)-CERT_MIN_SIZE { err = fmt.Errorf("certificate parsing warning: certificate data is shorter than specified by length") log.WithFields(logrus.Fields{ "at": "(Certificate) NewCertificate", "certificate_bytes_length": certificate.len.Int(), - "certificate_payload_length": payleng, + "certificate_payload_length": payloadLength, "data_bytes:": string(data), "kind_bytes": data[0:1], "len_bytes": data[1:3], diff --git a/lib/common/keys_and_cert/keys_and_cert.go b/lib/common/keys_and_cert/keys_and_cert.go index 0de018e..9f6b907 100644 --- a/lib/common/keys_and_cert/keys_and_cert.go +++ b/lib/common/keys_and_cert/keys_and_cert.go @@ -88,6 +88,9 @@ func (keys_and_cert KeysAndCert) Bytes() []byte { log.WithFields(logrus.Fields{ "bytes_length": len(bytes), }).Debug("Retrieved bytes from KeysAndCert") + bytes = append(bytes, keys_and_cert.publicKey.Bytes()...) + bytes = append(bytes, keys_and_cert.Padding...) + bytes = append(bytes, keys_and_cert.signingPublicKey.Bytes()...) return bytes } diff --git a/lib/common/router_info/router_info.go b/lib/common/router_info/router_info.go index 99a73d0..9673abe 100644 --- a/lib/common/router_info/router_info.go +++ b/lib/common/router_info/router_info.go @@ -137,17 +137,30 @@ func (router_info RouterInfo) Bytes() (bytes []byte, err error) { return bytes, err } +// Convert a byte slice into a string like [1, 2, 3] -> "1, 2, 3" +func bytesToString(bytes []byte) string { + str := "[" + for i, b := range bytes { + str += strconv.Itoa(int(b)) + if i < len(bytes)-1 { + str += ", " + } + } + str += "]" + return str +} + func (router_info RouterInfo) String() string { log.Debug("Converting RouterInfo to string") - str := "Certificate: " + string(router_info.router_identity.Bytes()) - str += "Published: " + string(router_info.published.Bytes()) - str += "Addresses:" + string(router_info.size.Bytes()) + str := "Certificate: " + bytesToString(router_info.router_identity.Bytes()) + str += "Published: " + bytesToString(router_info.published.Bytes()) + str += "Addresses:" + bytesToString(router_info.size.Bytes()) for index, router_address := range router_info.addresses { str += "Address " + strconv.Itoa(index) + ": " + router_address.String() } - str += "Peer Size: " + string(router_info.peer_size.Bytes()) - str += "Options: " + string(router_info.options.Data()) - str += "Signature: " + string([]byte(*router_info.signature)) + str += "Peer Size: " + bytesToString(router_info.peer_size.Bytes()) + str += "Options: " + bytesToString(router_info.options.Data()) + str += "Signature: " + bytesToString([]byte(*router_info.signature)) log.WithField("string_length", len(str)).Debug("Converted RouterInfo to string") return str } diff --git a/lib/common/router_info/router_info2_test.go b/lib/common/router_info/router_info2_test.go index e74774e..cd03ccd 100644 --- a/lib/common/router_info/router_info2_test.go +++ b/lib/common/router_info/router_info2_test.go @@ -98,7 +98,7 @@ func TestCreateRouterInfo(t *testing.T) { t.Run("Serialize and Deserialize RouterInfo", func(t *testing.T) { routerInfoBytes, err := routerInfo.Bytes() - t.Log(routerInfo.String(), routerInfoBytes) + t.Log(len(routerInfoBytes), routerInfo.String(), routerInfoBytes) if err != nil { t.Fatalf("Failed to write RouterInfo to bytes: %v\n", err) } diff --git a/lib/crypto/dsa.go b/lib/crypto/dsa.go index 0663fe9..e30b519 100644 --- a/lib/crypto/dsa.go +++ b/lib/crypto/dsa.go @@ -95,6 +95,10 @@ type DSAVerifier struct { type DSAPublicKey [128]byte +func (k DSAPublicKey) Bytes() []byte { + return k[:] +} + // create a new dsa verifier func (k DSAPublicKey) NewVerifier() (v Verifier, err error) { log.Debug("Creating new DSA verifier") diff --git a/lib/crypto/ecdsa.go b/lib/crypto/ecdsa.go index d7b3f60..c4f911f 100644 --- a/lib/crypto/ecdsa.go +++ b/lib/crypto/ecdsa.go @@ -73,6 +73,10 @@ func (k ECP256PublicKey) Len() int { return len(k) } +func (k ECP256PublicKey) Bytes() []byte { + return k[:] +} + func (k ECP256PublicKey) NewVerifier() (Verifier, error) { log.Debug("Creating new P256 ECDSA verifier") // return createECVerifier(elliptic.P256(), crypto.SHA256, k[:]) @@ -88,6 +92,10 @@ type ( ECP384PrivateKey [48]byte ) +func (k ECP384PublicKey) Bytes() []byte { + return k[:] +} + func (k ECP384PublicKey) Len() int { return len(k) } @@ -107,6 +115,10 @@ type ( ECP521PrivateKey [66]byte ) +func (k ECP521PublicKey) Bytes() []byte { + return k[:] +} + func (k ECP521PublicKey) Len() int { return len(k) } diff --git a/lib/crypto/ed25519.go b/lib/crypto/ed25519.go index 0545386..4d60a37 100644 --- a/lib/crypto/ed25519.go +++ b/lib/crypto/ed25519.go @@ -34,6 +34,10 @@ func (k Ed25519PublicKey) Len() int { return len(k) } +func (k Ed25519PublicKey) Bytes() []byte { + return k +} + func createEd25519PublicKey(data []byte) (k *ed25519.PublicKey) { log.WithField("data_length", len(data)).Debug("Creating Ed25519 public key") if len(data) == 256 { diff --git a/lib/crypto/elg.go b/lib/crypto/elg.go index 702d357..4ad11cc 100644 --- a/lib/crypto/elg.go +++ b/lib/crypto/elg.go @@ -237,6 +237,10 @@ func (elg ElgPublicKey) Len() int { return len(elg) } +func (elg ElgPublicKey) Bytes() []byte { + return elg[:] +} + func (elg ElgPublicKey) NewEncrypter() (enc Encrypter, err error) { log.Debug("Creating new ElGamal encrypter") k := createElgamalPublicKey(elg[:]) diff --git a/lib/crypto/sign.go b/lib/crypto/sign.go index 1388e24..bc04de6 100644 --- a/lib/crypto/sign.go +++ b/lib/crypto/sign.go @@ -26,9 +26,11 @@ type SigningPublicKey interface { NewVerifier() (Verifier, error) // get the size of this public key Len() int + Bytes() []byte } type PublicKey interface { Len() int + Bytes() []byte NewEncrypter() (Encrypter, error) }