Fix mapping values, remove redundant, unused functions

This commit is contained in:
eyedeekay
2024-02-05 10:42:57 -05:00
parent 8d631239b7
commit b0fcf4bc11
22 changed files with 99 additions and 143 deletions

View File

@@ -19,7 +19,11 @@ $(EXE):
$(GO) build -v -o $(EXE)
test:
$(GO) test -v -failfast ./lib/common/data/...
#$(GO) test -v -failfast ./lib/common/data/...
$(GO) test -v -failfast ./lib/common/keys_and_cert/...
clean:
$(GO) clean -v
fmt:
find . -name '*.go' -exec gofmt -w -s {} \;

View File

@@ -1,4 +1,2 @@
//
// provides generic interfaces for initial bootstrap into network and network reseeding
//
package bootstrap

View File

@@ -160,6 +160,9 @@ func NewCertificate(data []byte) (certificate *Certificate, err error) {
"at": "(Certificate) NewCertificate",
"certificate_bytes_length": certificate.len.Int(),
"certificate_payload_length": payleng,
"data_bytes:": data,
"kind_bytes": data[0:1],
"len_bytes": data[1:3],
"reason": err.Error(),
}).Error("invalid certificate")
return
@@ -169,6 +172,9 @@ func NewCertificate(data []byte) (certificate *Certificate, err error) {
"at": "(Certificate) NewCertificate",
"certificate_bytes_length": certificate.len.Int(),
"certificate_payload_length": payleng,
"data_bytes:": data,
"kind_bytes": data[0:1],
"len_bytes": data[1:3],
"reason": err.Error(),
}).Error("invalid certificate")
return

View File

@@ -2,6 +2,7 @@ package data
import (
"encoding/binary"
"fmt"
)
// MAX_INTEGER_SIZE is the maximum length of an I2P integer in bytes.
@@ -50,6 +51,7 @@ func NewInteger(bytes []byte, size int) (integer *Integer, remainder []byte, err
if size < MAX_INTEGER_SIZE {
integerSize = size
}
fmt.Println("IntegerSize: ", integerSize, "IntegerBytes:", bytes)
i, remainder := ReadInteger(bytes, integerSize)
integer = &i
return

View File

@@ -138,7 +138,6 @@ func ReadMapping(bytes []byte) (mapping Mapping, remainder []byte, err []error)
if e != nil {
err = append(err, e)
}
mapping.size = size
if e != nil {
log.WithFields(log.Fields{

View File

@@ -82,7 +82,7 @@ const (
KEYCERT_SPK_SIZE = 128
)
//type KeyCertificate []byte
// type KeyCertificate []byte
type KeyCertificate struct {
*Certificate
spkType Integer
@@ -205,9 +205,9 @@ func (key_certificate KeyCertificate) SignatureSize() (size int) {
func NewKeyCertificate(bytes []byte) (key_certificate *KeyCertificate, remainder []byte, err error) {
var certificate *Certificate
certificate, remainder, err = ReadCertificate(bytes)
//if err != nil {
// return nil, err
//}
if err != nil {
return
}
if len(bytes) < KEYCERT_MIN_SIZE {
err = errors.New("error parsing key certificate: not enough data")
}
@@ -237,7 +237,7 @@ func NewKeyCertificate(bytes []byte) (key_certificate *KeyCertificate, remainder
cpkType: Integer(bytes[6:7]),
}
}
remainder = bytes[7:]
remainder = bytes[len(bytes):]
//key_certificate.PublicKey = NewPublicKey(bytes)
return
}

View File

@@ -165,45 +165,6 @@ func (keys_and_cert *KeysAndCert) Certificate() (cert *Certificate) {
return keys_and_cert.KeyCertificate.Certificate
}
//
// Read a KeysAndCert from a slice of bytes, retuning it and the remaining data as well as any errors
// encoutered parsing the KeysAndCert.
//
// ReadKeysAndCert returns KeysAndCert from a []byte.
// The remaining bytes after the specified length are also returned.
// Returns a list of errors that occurred during parsing.
func ReadKeysAndCert(data []byte) (keys_and_cert KeysAndCert, remainder []byte, err error) {
/*data_len := len(data)
if data_len < KEYS_AND_CERT_MIN_SIZE {
log.WithFields(log.Fields{
"at": "ReadKeysAndCert",
"data_len": data_len,
"required_len": KEYS_AND_CERT_MIN_SIZE,
"reason": "not enough data",
}).Error("error parsing keys and cert")
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
return
}
keys_and_cert = KeysAndCert(data[:KEYS_AND_CERT_MIN_SIZE])
cert, _ := keys_and_cert.Certificate()
cert_len := cert.Length()
if cert_len == 0 {
remainder = data[KEYS_AND_CERT_MIN_SIZE:]
return
}
if data_len < KEYS_AND_CERT_MIN_SIZE+cert_len {
keys_and_cert = append(keys_and_cert, data[KEYS_AND_CERT_MIN_SIZE:]...)
//err = cert_len_err
} else {
keys_and_cert = append(keys_and_cert, data[KEYS_AND_CERT_MIN_SIZE:KEYS_AND_CERT_MIN_SIZE+cert_len]...)
remainder = data[KEYS_AND_CERT_MIN_SIZE+cert_len:]
}*/
keys_and_cert_pointer, remainder, err := NewKeysAndCert(data)
keys_and_cert = *keys_and_cert_pointer
return
}
// NewKeysAndCert creates a new *KeysAndCert from []byte using ReadKeysAndCert.
// Returns a pointer to KeysAndCert unlike ReadKeysAndCert.
func NewKeysAndCert(data []byte) (keys_and_cert *KeysAndCert, remainder []byte, err error) {
@@ -217,21 +178,21 @@ func NewKeysAndCert(data []byte) (keys_and_cert *KeysAndCert, remainder []byte,
"reason": "not enough data",
}).Error("error parsing keys and cert")
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
keys_and_cert.KeyCertificate, remainder, _ = NewKeyCertificate(data)
return
}
cert, remainder, err := NewKeyCertificate(data)
keys_and_cert.KeyCertificate = cert
keys_and_cert.KeyCertificate, remainder, err = NewKeyCertificate(data)
if err != nil {
return nil, nil, err
}
padding := data[KEYS_AND_CERT_MIN_SIZE+cert.Length():]
padding := data[KEYS_AND_CERT_MIN_SIZE+keys_and_cert.KeyCertificate.Length():]
keys_and_cert.padding = padding
publicKey, err := cert.ConstructPublicKey(padding)
publicKey, err := keys_and_cert.KeyCertificate.ConstructPublicKey(padding)
keys_and_cert.publicKey = publicKey
if err != nil {
return nil, nil, err
}
signingPublicKey, err := cert.ConstructSigningPublicKey(padding)
signingPublicKey, err := keys_and_cert.KeyCertificate.ConstructSigningPublicKey(padding)
keys_and_cert.signingPublicKey = signingPublicKey
if err != nil {
return nil, nil, err

View File

@@ -12,16 +12,10 @@ func TestCertificateWithMissingData(t *testing.T) {
cert_data := []byte{0x05, 0x00, 0x04, 0x00, 0x01}
data := make([]byte, 128+256)
data = append(data, cert_data...)
keys_and_cert, _, err := ReadKeysAndCert(data)
cert := keys_and_cert.Certificate()
_, _, err := NewKeysAndCert(data)
if assert.NotNil(err) {
assert.Equal("certificate parsing warning: certificate data is shorter than specified by length", err.Error())
}
cert_bytes := cert.Bytes()
if assert.Equal(len(cert_data), len(cert_bytes)) {
assert.Equal(cert_bytes, cert_data, "keys_and_cert.Certificate() did not return available data when cert was missing some data")
}
}
func TestCertificateWithValidData(t *testing.T) {
@@ -30,7 +24,7 @@ func TestCertificateWithValidData(t *testing.T) {
cert_data := []byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00}
data := make([]byte, 128+256)
data = append(data, cert_data...)
keys_and_cert, _, err := ReadKeysAndCert(data)
keys_and_cert, _, err := NewKeysAndCert(data)
cert := keys_and_cert.Certificate()
assert.Nil(err)
@@ -48,7 +42,7 @@ func TestPublicKeyWithBadData(t *testing.T) {
data := make([]byte, 128)
data = append(data, pub_key_data...)
data = append(data, cert_data...)
keys_and_cert, _, err := ReadKeysAndCert(data)
keys_and_cert, _, err := NewKeysAndCert(data)
pub_key := keys_and_cert.PublicKey()
if assert.NotNil(err) {
@@ -65,7 +59,7 @@ func TestPublicKeyWithBadCertificate(t *testing.T) {
data := make([]byte, 128)
data = append(data, pub_key_data...)
data = append(data, cert_data...)
keys_and_cert, _, err := ReadKeysAndCert(data)
keys_and_cert, _, err := NewKeysAndCert(data)
pub_key := keys_and_cert.PublicKey()
if assert.NotNil(err) {
@@ -82,7 +76,7 @@ func TestPublicKeyWithNullCertificate(t *testing.T) {
data := make([]byte, 128)
data = append(data, pub_key_data...)
data = append(data, cert_data...)
keys_and_cert, _, err := ReadKeysAndCert(data)
keys_and_cert, _, err := NewKeysAndCert(data)
pub_key := keys_and_cert.PublicKey()
assert.Nil(err)
@@ -97,7 +91,7 @@ func TestPublicKeyWithKeyCertificate(t *testing.T) {
data := make([]byte, 128)
data = append(data, pub_key_data...)
data = append(data, cert_data...)
keys_and_cert, _, err := ReadKeysAndCert(data)
keys_and_cert, _, err := NewKeysAndCert(data)
pub_key := keys_and_cert.PublicKey()
assert.Nil(err)
@@ -112,7 +106,7 @@ func TestSigningPublicKeyWithBadData(t *testing.T) {
data := make([]byte, 93)
data = append(data, pub_key_data...)
data = append(data, cert_data...)
keys_and_cert, _, err := ReadKeysAndCert(data)
keys_and_cert, _, err := NewKeysAndCert(data)
signing_pub_key := keys_and_cert.SigningPublicKey()
if assert.NotNil(err) {
@@ -129,7 +123,7 @@ func TestSigningPublicKeyWithBadCertificate(t *testing.T) {
data := make([]byte, 128)
data = append(data, pub_key_data...)
data = append(data, cert_data...)
keys_and_cert, _, err := ReadKeysAndCert(data)
keys_and_cert, _, err := NewKeysAndCert(data)
signing_pub_key := keys_and_cert.SigningPublicKey()
if assert.NotNil(err) {
@@ -146,7 +140,7 @@ func TestSigningPublicKeyWithNullCertificate(t *testing.T) {
signing_pub_key_data := make([]byte, 128)
data := append(pub_key_data, signing_pub_key_data...)
data = append(data, cert_data...)
keys_and_cert, _, err := ReadKeysAndCert(data)
keys_and_cert, _, err := NewKeysAndCert(data)
signing_pub_key := keys_and_cert.SigningPublicKey()
assert.Nil(err)
@@ -161,18 +155,18 @@ func TestSigningPublicKeyWithKeyCertificate(t *testing.T) {
signing_pub_key_data := make([]byte, 128)
data := append(pub_key_data, signing_pub_key_data...)
data = append(data, cert_data...)
keys_and_cert, _, err := ReadKeysAndCert(data)
keys_and_cert, _, err := NewKeysAndCert(data)
signing_pub_key := keys_and_cert.SigningPublicKey()
assert.Nil(err)
assert.Equal(len(signing_pub_key_data), signing_pub_key.Len())
}
func TestReadKeysAndCertWithMissingData(t *testing.T) {
func TestNewKeysAndCertWithMissingData(t *testing.T) {
assert := assert.New(t)
cert_data := make([]byte, 128)
_, remainder, err := ReadKeysAndCert(cert_data)
_, remainder, err := NewKeysAndCert(cert_data)
assert.Equal(0, len(remainder))
if assert.NotNil(err) {
assert.Equal("error parsing KeysAndCert: data is smaller than minimum valid size", err.Error())
@@ -180,56 +174,56 @@ func TestReadKeysAndCertWithMissingData(t *testing.T) {
}
func TestReadKeysAndCertWithMissingCertData(t *testing.T) {
func TestNewKeysAndCertWithMissingCertData(t *testing.T) {
assert := assert.New(t)
cert_data := make([]byte, 128+256)
cert_data = append(cert_data, []byte{0x05, 0x00, 0x04, 0x00, 0x01}...)
_, remainder, err := ReadKeysAndCert(cert_data)
_, remainder, err := NewKeysAndCert(cert_data)
assert.Equal(0, len(remainder))
if assert.NotNil(err) {
assert.Equal("certificate parsing warning: certificate data is shorter than specified by length", err.Error())
}
}
func TestReadKeysAndCertWithValidDataWithCertificate(t *testing.T) {
func TestNewKeysAndCertWithValidDataWithCertificate(t *testing.T) {
assert := assert.New(t)
cert_data := make([]byte, 128+256)
cert_data = append(cert_data, []byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00}...)
_, remainder, err := ReadKeysAndCert(cert_data)
_, remainder, err := NewKeysAndCert(cert_data)
assert.Equal(0, len(remainder))
assert.Nil(err)
}
func TestReadKeysAndCertWithValidDataWithoutCertificate(t *testing.T) {
func TestNewKeysAndCertWithValidDataWithoutCertificate(t *testing.T) {
assert := assert.New(t)
cert_data := make([]byte, 128+256)
cert_data = append(cert_data, []byte{0x00, 0x00, 0x00}...)
_, remainder, err := ReadKeysAndCert(cert_data)
_, remainder, err := NewKeysAndCert(cert_data)
assert.Equal(0, len(remainder))
assert.Nil(err)
}
func TestReadKeysAndCertWithValidDataWithCertificateAndRemainder(t *testing.T) {
func TestNewKeysAndCertWithValidDataWithCertificateAndRemainder(t *testing.T) {
assert := assert.New(t)
cert_data := make([]byte, 128+256)
cert_data = append(cert_data, []byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x41}...)
_, remainder, err := ReadKeysAndCert(cert_data)
_, remainder, err := NewKeysAndCert(cert_data)
if assert.Equal(1, len(remainder)) {
assert.Equal("A", string(remainder[0]))
}
assert.Nil(err)
}
func TestReadKeysAndCertWithValidDataWithoutCertificateAndRemainder(t *testing.T) {
func TestNewKeysAndCertWithValidDataWithoutCertificateAndRemainder(t *testing.T) {
assert := assert.New(t)
cert_data := make([]byte, 128+256)
cert_data = append(cert_data, []byte{0x00, 0x00, 0x00, 0x41}...)
_, remainder, err := ReadKeysAndCert(cert_data)
_, remainder, err := NewKeysAndCert(cert_data)
if assert.Equal(1, len(remainder)) {
assert.Equal("A", string(remainder[0]))
}

View File

@@ -132,7 +132,10 @@ type LeaseSet struct {
// Destination returns the Destination as []byte.
func (lease_set LeaseSet) Destination() (destination Destination, err error) {
keys_and_cert, _, err := ReadKeysAndCert(lease_set)
keys_and_cert, _, err := NewKeysAndCert(lease_set)
if err != nil {
return
}
destination, _, err = ReadDestination(keys_and_cert.Bytes())
return
}
@@ -140,7 +143,7 @@ func (lease_set LeaseSet) Destination() (destination Destination, err error) {
// PublicKey returns the public key as crypto.ElgPublicKey.
// Returns errors encountered during parsing.
func (lease_set LeaseSet) PublicKey() (public_key crypto.ElgPublicKey, err error) {
_, remainder, err := ReadKeysAndCert(lease_set)
_, remainder, err := NewKeysAndCert(lease_set)
remainder_len := len(remainder)
if remainder_len < LEASE_SET_PUBKEY_SIZE {
log.WithFields(log.Fields{
@@ -212,7 +215,7 @@ func (lease_set LeaseSet) SigningKey() (signing_public_key crypto.SigningPublicK
// LeaseCount returns the numbert of leases specified by the LeaseCount value as int.
// returns errors encountered during parsing.
func (lease_set LeaseSet) LeaseCount() (count int, err error) {
_, remainder, err := ReadKeysAndCert(lease_set)
_, remainder, err := NewKeysAndCert(lease_set)
if err != nil {
return
}

View File

@@ -107,9 +107,7 @@ func (router_address RouterAddress) Options() Mapping {
return *router_address.options
}
//
// Check if the RouterAddress is empty or if it is too small to contain valid data.
//
func (router_address RouterAddress) checkValid() (err error, exit bool) {
/*addr_len := len(router_address)
exit = false

View File

@@ -1,4 +1,2 @@
//
// package for i2p specific crpytography
//
package crypto

View File

@@ -23,9 +23,7 @@ func (hk HMACKey) xor(p byte) (i []byte) {
return
}
//
// do i2p hmac
//
func I2PHMAC(data []byte, k HMACKey) (d HMACDigest) {
buff := make([]byte, 64+len(data))

View File

@@ -1,5 +1 @@
//
//
//
//
package netdb

View File

@@ -50,9 +50,7 @@ func (db StdNetDB) Path() string {
return string(db)
}
//
// return how many routers we know about in our network database
//
func (db StdNetDB) Size() (routers int) {
// TODO: implement this
var err error

View File

@@ -14,6 +14,7 @@
// content the error ErrInvalidSignature will be returned.
//
// Example usage:
//
// // Let's say we are reading an SU3 file from an HTTP body, which is an io.Reader.
// su3File, err := su3.Read(body)
// if err != nil {
@@ -31,12 +32,14 @@
// }
//
// If you want to parse from a []byte, you can wrap it like this:
//
// mySU3FileBytes := []byte{0x00, 0x01, 0x02, 0x03}
// su3File, err := su3.Read(bytes.NewReader(mySU3FileBytes))
//
// One of the advantages of this library's design is that you can avoid buffering
// the file contents in memory. Here's how you would stream from an HTTP body
// directly to disk:
//
// su3File, err := su3.Read(body)
// if err != nil {
// // Handle error.

View File

@@ -1,4 +1,6 @@
/**
/*
*
i2np messages transports
*/
package transport

View File

@@ -1,6 +1,4 @@
/*
i2p ssu transport implementation
*/
package ssu

View File

@@ -380,6 +380,7 @@ func (delivery_instructions DeliveryInstructions) TunnelID() (tunnel_id uint32,
}
// Return the hash for these DeliveryInstructions, which varies by hash type.
//
// If the type is DT_TUNNEL, hash is the SHA256 of the gateway router, if
// the type is DT_ROUTER it is the SHA256 of the router.
func (delivery_instructions DeliveryInstructions) Hash() (hash common.Hash, err error) {

View File

@@ -1,4 +1,4 @@
/*
i2p garlic tunnel implementation
i2p garlic tunnel implementation
*/
package tunnel

View File

@@ -153,10 +153,8 @@ func (decrypted_tunnel_message DecryptedTunnelMessage) Checksum() crypto.TunnelI
return decrypted_tunnel_message[4+16 : 4+4+16]
}
//
// Returns the contents of a decrypted tunnel message that contain the data for the
// DeliveryInstructions.
//
func (decrypted_tunnel_message DecryptedTunnelMessage) deliveryInstructionData() []byte {
data_area := decrypted_tunnel_message[4+4+16:]
for i := 0; i < len(data_area); i++ {
@@ -167,11 +165,8 @@ func (decrypted_tunnel_message DecryptedTunnelMessage) deliveryInstructionData()
return []byte{}
}
//
// Returns a slice of DeliveryInstructionWithFragment structures, which all of the Delivery Instructions
// in the tunnel message and their corresponding MessageFragment structures.
//
//
func (decrypted_tunnel_message DecryptedTunnelMessage) DeliveryInstructionsWithFragments() []DeliveryInstructionsWithFragment {
set := make([]DeliveryInstructionsWithFragment, 0)
data := decrypted_tunnel_message.deliveryInstructionData()

View File

@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package signals

View File

@@ -1,3 +1,4 @@
//go:build windows
// +build windows
package signals