Begin to convert the common structures from being byte-slice based to struct based

This commit is contained in:
idk
2021-04-22 22:55:32 -04:00
parent 366db272f4
commit 1ea426da9c
8 changed files with 189 additions and 113 deletions

View File

@ -23,3 +23,6 @@ test:
clean:
$(GO) clean -v
fmt:
find . -name '*.go' -exec gofmt -w -s {} \;

View File

@ -46,14 +46,30 @@ const (
CERT_MIN_SIZE = 3
)
type Certificate []byte
type CertificateInterface interface {
Cert() []byte
Length() (length int, err error)
Data() (data []byte, err error)
}
type Certificate struct {
CertType int
CertLen int
CertBytes []byte
}
var ci CertificateInterface = &Certificate{}
func (certificate Certificate) Cert() []byte {
return certificate.CertBytes
}
//
// Return the Certificate Type specified in the first byte of the Certificate,
// and an error if the certificate is shorter than the minimum certificate size.
//
func (certificate Certificate) Type() (cert_type int, err error) {
cert_len := len(certificate)
cert_len := len(certificate.Cert())
if cert_len < CERT_MIN_SIZE {
log.WithFields(log.Fields{
"at": "(Certificate) Type",
@ -63,7 +79,7 @@ func (certificate Certificate) Type() (cert_type int, err error) {
err = errors.New("error parsing certificate length: certificate is too short")
return
}
cert_type = Integer([]byte{certificate[0]})
cert_type = certificate.CertType
return
}
@ -73,12 +89,51 @@ func (certificate Certificate) Type() (cert_type int, err error) {
// match the provided data.
//
func (certificate Certificate) Length() (length int, err error) {
cert_len := len(certificate)
cert_len := len(certificate.Cert())
_, err = certificate.Type()
if err != nil {
return
}
length = Integer(certificate[1:CERT_MIN_SIZE])
length = certificate.CertLen
return
}
//
// Return the Certificate data and any errors encountered parsing the Certificate.
//
func (certificate Certificate) Data() (data []byte, err error) {
length, err := certificate.Length()
if err != nil {
switch err.Error() {
case "error parsing certificate length: certificate is too short":
return
case "certificate parsing warning: certificate data is shorter than specified by length":
data = certificate.Cert()[CERT_MIN_SIZE:]
return
case "certificate parsing warning: certificate contains data beyond length":
data = certificate.Cert()[CERT_MIN_SIZE : length+CERT_MIN_SIZE]
return
}
}
data = certificate.Cert()[CERT_MIN_SIZE:]
return
}
//
// Read a Certificate from a slice of bytes, returning any extra data on the end of the slice
// and any errors if a valid Certificate could not be read.
//
func ReadCertificate(data []byte) (Certificate, []byte, error) {
var remainder []byte
var err error
var certificate Certificate //= Certificate(data)
certificate.CertType = Integer([]byte{data[0]})
cert_len := len(data)
_, err = certificate.Type()
if err != nil {
return certificate, nil, err
}
length := Integer(data[1:CERT_MIN_SIZE])
inferred_len := length + CERT_MIN_SIZE
if inferred_len > cert_len {
log.WithFields(log.Fields{
@ -99,41 +154,13 @@ func (certificate Certificate) Length() (length int, err error) {
}).Warn("certificate format warning")
err = errors.New("certificate parsing warning: certificate contains data beyond length")
}
return
}
//
// Return the Certificate data and any errors encountered parsing the Certificate.
//
func (certificate Certificate) Data() (data []byte, err error) {
length, err := certificate.Length()
if err != nil {
switch err.Error() {
case "error parsing certificate length: certificate is too short":
return
case "certificate parsing warning: certificate data is shorter than specified by length":
data = certificate[CERT_MIN_SIZE:]
return
case "certificate parsing warning: certificate contains data beyond length":
data = certificate[CERT_MIN_SIZE : length+CERT_MIN_SIZE]
return
}
}
data = certificate[CERT_MIN_SIZE:]
return
}
//
// Read a Certificate from a slice of bytes, returning any extra data on the end of the slice
// and any errors if a valid Certificate could not be read.
//
func ReadCertificate(data []byte) (certificate Certificate, remainder []byte, err error) {
certificate = Certificate(data)
length, err := certificate.Length()
certificate.CertLen = length
certificate.CertBytes = data[CERT_MIN_SIZE:]
length, err = certificate.Length()
if err != nil && err.Error() == "certificate parsing warning: certificate contains data beyond length" {
certificate = Certificate(data[:length+CERT_MIN_SIZE])
certificate.CertBytes = data[:length+CERT_MIN_SIZE]
remainder = data[length+CERT_MIN_SIZE:]
err = nil
}
return
return certificate, remainder, err
}

View File

@ -19,23 +19,25 @@ import (
// A Destination is a KeysAndCert with functionallity
// for generating base32 and base64 addresses.
//
type Destination []byte
type Destination struct {
KeysAndCert
}
func (destination Destination) PublicKey() (crypto.PublicKey, error) {
return KeysAndCert(destination).PublicKey()
return destination.KeysAndCert.PublicKey()
}
func (destination Destination) SigningPublicKey() (crypto.SigningPublicKey, error) {
return KeysAndCert(destination).SigningPublicKey()
return destination.KeysAndCert.SigningPublicKey()
}
func (destination Destination) Certificate() (Certificate, error) {
return KeysAndCert(destination).Certificate()
return destination.KeysAndCert.GetCertificate()
}
func ReadDestination(data []byte) (destination Destination, remainder []byte, err error) {
keys_and_cert, remainder, err := ReadKeysAndCert(data)
destination = Destination(keys_and_cert)
destination.KeysAndCert = keys_and_cert
return
}
@ -43,7 +45,7 @@ func ReadDestination(data []byte) (destination Destination, remainder []byte, er
// Generate the I2P base32 address for this Destination.
//
func (destination Destination) Base32Address() (str string) {
hash := crypto.SHA256(destination)
hash := crypto.SHA256(destination.Cert())
str = strings.Trim(base32.EncodeToString(hash[:]), "=")
str = str + ".b32.i2p"
return
@ -53,5 +55,5 @@ func (destination Destination) Base32Address() (str string) {
// Generate the I2P base64 address for this Destination.
//
func (destination Destination) Base64() string {
return base64.EncodeToString(destination)
return base64.EncodeToString(destination.Cert())
}

View File

@ -30,3 +30,11 @@ func Integer(number []byte) (value int) {
value = int(binary.BigEndian.Uint64(number))
return
}
func IntegerBytes(value int) (number []byte) {
number = make([]byte, INTEGER_SIZE)
binary.BigEndian.PutUint64(number, uint64(value))
return
}

View File

@ -74,13 +74,25 @@ const (
KEYCERT_SPK_SIZE = 128
)
type KeyCertificate []byte
type KeyCertificate struct {
PKType int
PKExtra []byte
SPKType int
SPKExtra []byte
}//[]byte
//
// The data contained in the Key Certificate.
//
func (key_certificate KeyCertificate) Data() ([]byte, error) {
return Certificate(key_certificate).Data()
var r []byte
pk := IntegerBytes(key_certificate.PKType)
r = append(r, pk...)
spk := IntegerBytes(key_certificate.SPKType)
r = append(r, spk...)
return r, nil
}
//
@ -88,27 +100,7 @@ func (key_certificate KeyCertificate) Data() ([]byte, error) {
// parsing the KeyCertificate.
//
func (key_certificate KeyCertificate) SigningPublicKeyType() (signing_pubkey_type int, err error) {
data, err := key_certificate.Data()
if err != nil {
log.WithFields(log.Fields{
"at": "(KeyCertificate) SigningPublicKeyType",
"reason": err.Error(),
}).Error("error getting signing public key")
return
}
data_len := len(data)
if data_len < 2 {
log.WithFields(log.Fields{
"at": "(KeyCertificate) SigningPublicKeyType",
"data_len": data_len,
"required_len": 2,
"reason": "not enough data",
}).Error("error parsing key certificate")
err = errors.New("error parsing key certificate: not enough data")
return
}
signing_pubkey_type = Integer(data[:2])
return
return key_certificate.SPKType, nil
}
//
@ -116,23 +108,7 @@ func (key_certificate KeyCertificate) SigningPublicKeyType() (signing_pubkey_typ
// this KeyCertificate.
//
func (key_certificate KeyCertificate) PublicKeyType() (pubkey_type int, err error) {
data, err := key_certificate.Data()
if err != nil {
return
}
data_len := len(data)
if data_len < 4 {
log.WithFields(log.Fields{
"at": "(KeyCertificate) PublicKeyType",
"data_len": data_len,
"required_len": 4,
"reason": "not enough data",
}).Error("error parsing key certificate")
err = errors.New("error parsing key certificate: not enough data")
return
}
pubkey_type = Integer(data[2:4])
return
return key_certificate.PKType, nil
}
//
@ -201,7 +177,8 @@ func (key_certificate KeyCertificate) ConstructSigningPublicKey(data []byte) (si
var ec_key crypto.ECP521PublicKey
extra := KEYCERT_SIGN_P521_SIZE - KEYCERT_SPK_SIZE
copy(ec_key[:], data)
copy(ec_key[KEYCERT_SPK_SIZE:], key_certificate[4:4+extra])
d , _ := key_certificate.Data()
copy(ec_key[KEYCERT_SPK_SIZE:], d[4:4+extra])
signing_public_key = ec_key
case KEYCERT_SIGN_RSA2048:
//var rsa_key crypto.RSA2048PublicKey
@ -244,3 +221,33 @@ func (key_certificate KeyCertificate) SignatureSize() (size int) {
}
return sizes[int(key_type)]
}
//
// Read a KeyCertificate from a slice of bytes
//
func ReadKeyCertificate(data []byte) (key_certificate KeyCertificate, err error) {
data_len := len(data)
if data_len < 2 {
log.WithFields(log.Fields{
"at": "(KeyCertificate) SigningPublicKeyType",
"data_len": data_len,
"required_len": 2,
"reason": "not enough data",
}).Error("error parsing key certificate")
err = errors.New("error parsing key certificate: not enough data")
return
}
key_certificate.SPKType = Integer(data[:2])
if data_len < 4 {
log.WithFields(log.Fields{
"at": "(KeyCertificate) PublicKeyType",
"data_len": data_len,
"required_len": 4,
"reason": "not enough data",
}).Error("error parsing key certificate")
err = errors.New("error parsing key certificate: not enough data")
return
}
key_certificate.PKType = Integer(data[2:4])
return
}

View File

@ -59,14 +59,19 @@ const (
KEYS_AND_CERT_DATA_SIZE = 384
)
type KeysAndCert []byte
type KeysAndCert struct {
//crypto.SigningPublicKey
//crypto.PublicKey
Certificate
}
//[]byte
//
// Return the PublicKey for this KeysAndCert, reading from the Key Certificate if it is present to
// determine correct lengths.
//
func (keys_and_cert KeysAndCert) PublicKey() (key crypto.PublicKey, err error) {
cert, err := keys_and_cert.Certificate()
cert, err:= keys_and_cert.GetCertificate()
if err != nil {
return
}
@ -78,7 +83,7 @@ func (keys_and_cert KeysAndCert) PublicKey() (key crypto.PublicKey, err error) {
// No Certificate is present, return the KEYS_AND_CERT_PUBKEY_SIZE byte
// PublicKey space as ElgPublicKey.
var elg_key crypto.ElgPublicKey
copy(keys_and_cert[:KEYS_AND_CERT_PUBKEY_SIZE], elg_key[:])
copy(keys_and_cert.Cert()[:KEYS_AND_CERT_PUBKEY_SIZE], elg_key[:])
key = elg_key
} else {
// A Certificate is present in this KeysAndCert
@ -87,15 +92,15 @@ func (keys_and_cert KeysAndCert) PublicKey() (key crypto.PublicKey, err error) {
// This KeysAndCert contains a Key Certificate, construct
// a PublicKey from the data in the KeysAndCert and
// any additional data in the Certificate.
key, err = KeyCertificate(cert).ConstructPublicKey(
keys_and_cert[:KEYS_AND_CERT_PUBKEY_SIZE],
key, err = KeyCertificate{PKType: cert_type}.ConstructPublicKey(
keys_and_cert.Cert()[:KEYS_AND_CERT_PUBKEY_SIZE],
)
} else {
// Key Certificate is not present, return the KEYS_AND_CERT_PUBKEY_SIZE byte
// PublicKey space as ElgPublicKey. No other Certificate
// types are currently in use.
var elg_key crypto.ElgPublicKey
copy(keys_and_cert[:KEYS_AND_CERT_PUBKEY_SIZE], elg_key[:])
copy(keys_and_cert.Cert()[:KEYS_AND_CERT_PUBKEY_SIZE], elg_key[:])
key = elg_key
log.WithFields(log.Fields{
"at": "(KeysAndCert) PublicKey",
@ -112,7 +117,7 @@ func (keys_and_cert KeysAndCert) PublicKey() (key crypto.PublicKey, err error) {
// determine correct lengths.
//
func (keys_and_cert KeysAndCert) SigningPublicKey() (signing_public_key crypto.SigningPublicKey, err error) {
cert, err := keys_and_cert.Certificate()
cert, err := keys_and_cert.GetCertificate()
if err != nil {
return
}
@ -124,7 +129,7 @@ func (keys_and_cert KeysAndCert) SigningPublicKey() (signing_public_key crypto.S
// No Certificate is present, return the KEYS_AND_CERT_SPK_SIZE byte
// SigningPublicKey space as legacy DSA SHA1 SigningPublicKey.
var dsa_pk crypto.DSAPublicKey
copy(dsa_pk[:], keys_and_cert[KEYS_AND_CERT_PUBKEY_SIZE:KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE])
copy(dsa_pk[:], keys_and_cert.Cert()[KEYS_AND_CERT_PUBKEY_SIZE:KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE])
signing_public_key = dsa_pk
} else {
// A Certificate is present in this KeysAndCert
@ -133,15 +138,15 @@ func (keys_and_cert KeysAndCert) SigningPublicKey() (signing_public_key crypto.S
// This KeysAndCert contains a Key Certificate, construct
// a SigningPublicKey from the data in the KeysAndCert and
// any additional data in the Certificate.
signing_public_key, err = KeyCertificate(cert).ConstructSigningPublicKey(
keys_and_cert[KEYS_AND_CERT_PUBKEY_SIZE : KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE],
signing_public_key, err = KeyCertificate{SPKType:cert_type}.ConstructSigningPublicKey(
keys_and_cert.Cert()[KEYS_AND_CERT_PUBKEY_SIZE : KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE],
)
} else {
// Key Certificate is not present, return the KEYS_AND_CERT_SPK_SIZE byte
// SigningPublicKey space as legacy SHA DSA1 SigningPublicKey.
// No other Certificate types are currently in use.
var dsa_pk crypto.DSAPublicKey
copy(dsa_pk[:], keys_and_cert[KEYS_AND_CERT_PUBKEY_SIZE:KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE])
copy(dsa_pk[:], keys_and_cert.Cert()[KEYS_AND_CERT_PUBKEY_SIZE:KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE])
signing_public_key = dsa_pk
}
@ -153,8 +158,8 @@ func (keys_and_cert KeysAndCert) SigningPublicKey() (signing_public_key crypto.S
// Return the Certificate contained in the KeysAndCert and any errors encountered while parsing the
// KeysAndCert or Certificate.
//
func (keys_and_cert KeysAndCert) Certificate() (cert Certificate, err error) {
keys_cert_len := len(keys_and_cert)
func (keys_and_cert KeysAndCert) GetCertificate() (cert Certificate, err error) {
keys_cert_len := len(keys_and_cert.Cert())
if keys_cert_len < KEYS_AND_CERT_MIN_SIZE {
log.WithFields(log.Fields{
"at": "(KeysAndCert) Certificate",
@ -165,7 +170,7 @@ func (keys_and_cert KeysAndCert) Certificate() (cert Certificate, err error) {
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
return
}
cert, _, err = ReadCertificate(keys_and_cert[KEYS_AND_CERT_DATA_SIZE:])
cert, _, err = ReadCertificate(keys_and_cert.Cert()[KEYS_AND_CERT_DATA_SIZE:])
return
}
@ -185,18 +190,24 @@ func ReadKeysAndCert(data []byte) (keys_and_cert KeysAndCert, remainder []byte,
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()
/* keys_and_cert = KeysAndCert{
KeyCertificate: KeyCertificate{
Certificate: data[:KEYS_AND_CERT_MIN_SIZE],
PublicKey:,
SigningPublicKey:,
}*/
cert, _ := keys_and_cert.GetCertificate()
cert_len, cert_len_err := 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:]...)
keys_and_cert.Certificate.CertBytes = append(keys_and_cert.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]...)
keys_and_cert.Certificate.CertBytes = append(keys_and_cert.Cert(), data[KEYS_AND_CERT_MIN_SIZE:KEYS_AND_CERT_MIN_SIZE+cert_len]...)
remainder = data[KEYS_AND_CERT_MIN_SIZE+cert_len:]
}
return

View File

@ -34,15 +34,21 @@ const (
LEASE_SIZE = 44
LEASE_HASH_SIZE = 32
LEASE_TUNNEL_ID_SIZE = 4
LEASE_TUNNEL_DATE_SIZE = 8
)
type Lease [LEASE_SIZE]byte
type Lease struct {
LeaseHash [LEASE_HASH_SIZE]byte
TunnelIdent [LEASE_TUNNEL_ID_SIZE]byte
TunnelDate [LEASE_TUNNEL_DATE_SIZE]byte
}
//[LEASE_SIZE]byte
//
// Return the first 32 bytes of the Lease as a Hash.
//
func (lease Lease) TunnelGateway() (hash Hash) {
copy(hash[:], lease[:LEASE_HASH_SIZE])
copy(hash[:], lease.LeaseHash[:])
return
}
@ -50,15 +56,24 @@ func (lease Lease) TunnelGateway() (hash Hash) {
// Parse the TunnelID Integer in the Lease.
//
func (lease Lease) TunnelID() uint32 {
return uint32(
Integer(lease[LEASE_HASH_SIZE : LEASE_HASH_SIZE+LEASE_TUNNEL_ID_SIZE]),
)
return uint32(Integer(lease.TunnelIdent[:]))
}
//
// Return the Date inside the Lease.
//
func (lease Lease) Date() (date Date) {
copy(date[:], lease[LEASE_HASH_SIZE+LEASE_TUNNEL_ID_SIZE:])
copy(date[:], lease.TunnelDate[:])
return
}
//
// Possibly temporary? Just to make it compile for now
//
func (lease Lease) Bytes() (bytes []byte) {
var r []byte
r = append(r, lease.LeaseHash[:]...)
r = append(r, lease.TunnelIdent[:]...)
r = append(r, lease.TunnelDate[:]...)
return r
}

View File

@ -93,7 +93,10 @@ const (
LEASE_SET_SIG_SIZE = 40
)
type LeaseSet []byte
type LeaseSet struct {
Destination
Leases []Lease
}
//
// Read a Destination from the LeaseSet.
@ -101,7 +104,7 @@ type LeaseSet []byte
func (lease_set LeaseSet) Destination() (destination Destination, err error) {
keys_and_cert, _, err := ReadKeysAndCert(lease_set)
destination = Destination(keys_and_cert)
return
return Destination
}
//
@ -241,7 +244,7 @@ func (lease_set LeaseSet) Leases() (leases []Lease, err error) {
return
}
var lease Lease
copy(lease[:], lease_set[start:end])
copy(lease.Bytes(), lease_set[start:end])
leases = append(leases, lease)
}
return