Had to back-track through most of the common structures because I wrote the common structures Integer implementation incorrect.
This commit is contained in:
@ -50,13 +50,13 @@ type CertificateInterface interface {
|
||||
Cert() []byte
|
||||
Length() (length int, err error)
|
||||
Data() (data []byte, err error)
|
||||
Type() (cert_type int, err error)
|
||||
Type() (cert_type int, type_bytes []byte, err error)
|
||||
SignatureSize() (size int)
|
||||
}
|
||||
|
||||
type Certificate struct {
|
||||
CertType int
|
||||
CertLen int
|
||||
CertType Integer
|
||||
CertLen Integer
|
||||
CertBytes []byte
|
||||
}
|
||||
|
||||
@ -68,13 +68,13 @@ func (certificate Certificate) SignatureSize() (size int) {
|
||||
|
||||
func (certificate Certificate) Cert() []byte {
|
||||
var ret []byte
|
||||
ret = append(ret, IntegerBytes(certificate.CertType)...)
|
||||
ret = append(ret, certificate.CertType.Bytes()...)
|
||||
data, _ := certificate.Data()
|
||||
if certificate.CertLen != 0 && len(data) != 0 {
|
||||
ret = append(ret, LengthBytes(certificate.CertLen)...)
|
||||
if certificate.CertLen.Value() != 0 && len(data) != 0 {
|
||||
ret = append(ret, LengthBytes(certificate.CertLen.Value())...)
|
||||
ret = append(ret, data...)
|
||||
} else {
|
||||
ret = append(ret, IntegerBytes(certificate.CertLen)...)
|
||||
ret = append(ret, certificate.CertLen.Bytes()...)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
@ -83,8 +83,8 @@ func (certificate Certificate) Cert() []byte {
|
||||
// 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) {
|
||||
return certificate.CertType, nil
|
||||
func (certificate Certificate) Type() (cert_type int, type_bytes []byte, err error) {
|
||||
return certificate.CertType.Value(), certificate.CertType.Bytes(), nil
|
||||
}
|
||||
|
||||
//
|
||||
@ -93,7 +93,7 @@ func (certificate Certificate) Type() (cert_type int, err error) {
|
||||
// match the provided data.
|
||||
//
|
||||
func (certificate Certificate) Length() (length int, err error) {
|
||||
if certificate.CertLen < 1 {
|
||||
if certificate.CertLen.Value() < 1 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Certificate) Length",
|
||||
"certificate_bytes_length": certificate.CertLen,
|
||||
@ -102,7 +102,7 @@ func (certificate Certificate) Length() (length int, err error) {
|
||||
}).Warn("certificate format warning")
|
||||
err = errors.New("error parsing certificate length: certificate is too short")
|
||||
}
|
||||
if certificate.CertLen > len(certificate.CertBytes) {
|
||||
if certificate.CertLen.Value() > len(certificate.CertBytes) {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Certificate) Length",
|
||||
"certificate_bytes_length": certificate.CertLen,
|
||||
@ -112,7 +112,7 @@ func (certificate Certificate) Length() (length int, err error) {
|
||||
err = errors.New("certificate parsing warning: certificate data is shorter than specified by length")
|
||||
length = len(certificate.CertBytes)
|
||||
}
|
||||
if certificate.CertLen < len(certificate.CertBytes) {
|
||||
if certificate.CertLen.Value() < len(certificate.CertBytes) {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Certificate) Length",
|
||||
"certificate_bytes_length": certificate.CertLen,
|
||||
@ -120,10 +120,10 @@ func (certificate Certificate) Length() (length int, err error) {
|
||||
"reason": "certificate contains data beyond length",
|
||||
}).Warn("certificate format warning")
|
||||
err = errors.New("certificate parsing warning: certificate contains data beyond length")
|
||||
length = certificate.CertLen
|
||||
length = certificate.CertLen.Value()
|
||||
return
|
||||
}
|
||||
length = certificate.CertLen
|
||||
length = certificate.CertLen.Value()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -135,6 +135,7 @@ func (certificate Certificate) Length() (length int, err error) {
|
||||
//
|
||||
func (certificate Certificate) Data() (data []byte, err error) {
|
||||
_, err = certificate.Length()
|
||||
data = certificate.CertBytes
|
||||
if err != nil {
|
||||
switch err.Error() {
|
||||
case "error parsing certificate length: certificate is too short":
|
||||
@ -143,11 +144,11 @@ func (certificate Certificate) Data() (data []byte, err error) {
|
||||
data = certificate.CertBytes
|
||||
return
|
||||
case "certificate parsing warning: certificate contains data beyond length":
|
||||
data = certificate.CertBytes[:certificate.CertLen]
|
||||
data = certificate.CertBytes[:certificate.CertLen.Value()]
|
||||
return
|
||||
}
|
||||
}
|
||||
data = certificate.CertBytes
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@ -156,7 +157,7 @@ func (certificate Certificate) Data() (data []byte, err error) {
|
||||
// and any errors if a valid Certificate could not be read.
|
||||
//
|
||||
func ReadCertificate(data []byte) (certificate Certificate, remainder []byte, err error) {
|
||||
certificate.CertType = Integer(data[0:1])
|
||||
certificate.CertType, err = NewInteger(data[0:1])
|
||||
cert_len := len(data)
|
||||
|
||||
if cert_len < CERT_MIN_SIZE {
|
||||
@ -169,24 +170,24 @@ func ReadCertificate(data []byte) (certificate Certificate, remainder []byte, er
|
||||
err = errors.New("error parsing certificate length: certificate is too short")
|
||||
return
|
||||
} else {
|
||||
certificate.CertLen = Integer(data[1:CERT_MIN_SIZE])
|
||||
_, err = certificate.Type()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
certificate.CertLen, err = NewInteger(data[2:CERT_MIN_SIZE])
|
||||
// _, err = certificate.Type()
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
certificate.CertBytes = data[CERT_MIN_SIZE:]
|
||||
_, err = certificate.Length()
|
||||
if err != nil {
|
||||
switch err.Error() {
|
||||
case "error parsing certificate length: certificate is too short":
|
||||
certificate.CertLen = 0
|
||||
certificate.CertLen, err = NewInteger([]byte{00000000})
|
||||
return
|
||||
case "certificate parsing warning: certificate data is shorter than specified by length":
|
||||
//err = nil
|
||||
return
|
||||
case "certificate parsing warning: certificate contains data beyond length":
|
||||
certificate.CertBytes = data[CERT_MIN_SIZE:]
|
||||
remainder = data[CERT_MIN_SIZE+certificate.CertLen:]
|
||||
remainder = data[CERT_MIN_SIZE+certificate.CertLen.Value():]
|
||||
err = nil
|
||||
return
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ func TestCertificateTypeIsFirstByte(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
cert_type, err := certificate.Type()
|
||||
cert_type, _, err := certificate.Type()
|
||||
|
||||
assert.Equal(cert_type, 3, "certificate.Type() should be the first bytes in a certificate")
|
||||
assert.Nil(err)
|
||||
@ -25,6 +25,7 @@ func TestCertificateLengthCorrect(t *testing.T) {
|
||||
bytes := []byte{0x03, 0x00, 0x02, 0xff, 0xff}
|
||||
certificate, _, err := ReadCertificate(bytes)
|
||||
cert_len, err := certificate.Length()
|
||||
assert.Nil(err)
|
||||
|
||||
assert.Equal(cert_len, 2, "certificate.Length() should return integer from second two bytes")
|
||||
assert.Nil(err)
|
||||
@ -70,8 +71,8 @@ func TestCertificateDataWhenCorrectSize(t *testing.T) {
|
||||
assert.Nil(err, "certificate.Data() returned error with valid data")
|
||||
|
||||
assert.Equal(cert_len, 1, "certificate.Length() did not return indicated length when data was valid")
|
||||
data := Integer(certificate.CertBytes)
|
||||
assert.Equal(170, data, "certificate.Data() returned incorrect data")
|
||||
data, _ := NewInteger(certificate.CertBytes)
|
||||
assert.Equal(170, data.Value(), "certificate.Data() returned incorrect data")
|
||||
}
|
||||
|
||||
func TestCertificateDataWhenTooLong(t *testing.T) {
|
||||
@ -113,6 +114,7 @@ func TestReadCertificateWithCorrectData(t *testing.T) {
|
||||
bytes := []byte{0x00, 0x00, 0x02, 0xff, 0xff}
|
||||
cert, remainder, err := ReadCertificate(bytes)
|
||||
|
||||
t.Log("CERT IS:", cert.Cert())
|
||||
assert.Equal(len(cert.Cert()), 5, "ReadCertificate() did not return correct amount of data for valid certificate")
|
||||
assert.Equal(len(remainder), 0, "ReadCertificate() did not return a zero length remainder on a valid certificate")
|
||||
assert.Nil(err, "ReadCertificate() should not return an error with valid data")
|
||||
|
@ -22,8 +22,8 @@ const DATE_SIZE = 8
|
||||
// struct.
|
||||
//
|
||||
func (date Date) Time() (date_time time.Time) {
|
||||
seconds := Integer(date[:])
|
||||
date_time = time.Unix(0, int64(seconds*1000000))
|
||||
seconds, _ := NewInteger(date[:])
|
||||
date_time = time.Unix(0, int64(seconds.Value()*1000000))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,8 @@ import (
|
||||
func TestTimeFromMiliseconds(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
next_day := Date{0x00, 0x00, 0x00, 0x00, 0x05, 0x26, 0x5c, 0x00}
|
||||
// next_day := Date{0x00, 0x00, 0x00, 0x00, 0x05, 0x26, 0x5c, 0x00}
|
||||
next_day := Date{0x00, 0x5c, 0x26, 0x05, 0x00, 0x00, 0x00, 0x00}
|
||||
go_time := next_day.Time()
|
||||
|
||||
assert.Equal(int64(86400), go_time.Unix(), "Date.Time() did not parse time in milliseconds")
|
||||
|
@ -8,6 +8,8 @@ Accurate for version 0.9.24
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
// log "github.com/sirupsen/logrus"
|
||||
// "errors"
|
||||
)
|
||||
|
||||
// Total byte length of an I2P integer
|
||||
@ -15,38 +17,58 @@ const (
|
||||
INTEGER_SIZE = 8
|
||||
)
|
||||
|
||||
type Integer []byte
|
||||
|
||||
func (i Integer) longBytes() (value [INTEGER_SIZE]byte) {
|
||||
value = [INTEGER_SIZE]byte{0, 0, 0, 0, 0, 0, 0, 0}
|
||||
for index, element := range []byte(i) {
|
||||
value[INTEGER_SIZE-1-index] = element
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (i Integer) Value() int {
|
||||
r := i.longBytes()
|
||||
return int(binary.BigEndian.Uint64(r[:]))
|
||||
// return int(binary.BigEndian.Int64(r[:]))
|
||||
}
|
||||
|
||||
func (i Integer) Bytes() []byte {
|
||||
if len([]byte(i)) == 0 {
|
||||
return []byte{0}
|
||||
}
|
||||
r := []byte(i)
|
||||
return r
|
||||
}
|
||||
|
||||
//
|
||||
// Interpret a slice of bytes from length 0 to length 8 as a big-endian
|
||||
// integer and return an int representation.
|
||||
//
|
||||
func Integer(number []byte) (value int) {
|
||||
num_len := len(number)
|
||||
if num_len < INTEGER_SIZE {
|
||||
number = append(
|
||||
make([]byte, INTEGER_SIZE-num_len),
|
||||
number...,
|
||||
)
|
||||
}
|
||||
value = int(binary.BigEndian.Uint64(number))
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Take an int representation and return a big endian integer.
|
||||
//
|
||||
func IntegerBytes(value int) (number []byte) {
|
||||
onumber := make([]byte, INTEGER_SIZE)
|
||||
// var number []byte
|
||||
binary.BigEndian.PutUint64(onumber, uint64(value))
|
||||
var index int
|
||||
for i, j := range onumber {
|
||||
index = i
|
||||
if j != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
number = onumber[index:]
|
||||
|
||||
func NewInteger(number []byte) (value Integer, err error) {
|
||||
value = number //[INTEGER_SIZE]byte(number)
|
||||
// for index, element := range number {
|
||||
// value[INTEGER_SIZE-1-index] = element
|
||||
// }
|
||||
/*length := len(number)
|
||||
if length < INTEGER_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Integer) NewInteger",
|
||||
"length": length,
|
||||
"required_len": INTEGER_SIZE,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing Integer")
|
||||
err = errors.New("error parsing Integer, not enough data")
|
||||
}else if length > INTEGER_SIZE{
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Integer) NewInteger",
|
||||
"length": length,
|
||||
"required_len": INTEGER_SIZE,
|
||||
"reason": "too much data",
|
||||
}).Error("error parsing Integer")
|
||||
err = errors.New("error parsing Integer, too much data")
|
||||
}else{
|
||||
err = nil
|
||||
}*/
|
||||
return
|
||||
}
|
||||
|
@ -8,24 +8,27 @@ import (
|
||||
func TestIntegerBigEndian(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
|
||||
integer := Integer(bytes)
|
||||
bytes := []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
||||
integer, err := NewInteger(bytes)
|
||||
assert.Nil(err)
|
||||
|
||||
assert.Equal(integer, 1, "Integer() did not parse bytes big endian")
|
||||
assert.Equal(integer.Value(), 1, "Integer() did not parse bytes big endian")
|
||||
|
||||
checkbytes := integer.Bytes()
|
||||
|
||||
assert.Equal(bytes, checkbytes, "IntegerBytes() did not match original bytes")
|
||||
}
|
||||
|
||||
func TestWorksWithOneByte(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
integer := Integer([]byte{0x01})
|
||||
bytes := []byte{0x00}
|
||||
integer, err := NewInteger(bytes)
|
||||
assert.Nil(err)
|
||||
|
||||
assert.Equal(integer, 1, "Integer() did not correctly parse single byte slice")
|
||||
}
|
||||
|
||||
func TestIsZeroWithNoData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
integer := Integer([]byte{})
|
||||
|
||||
assert.Equal(integer, 0, "Integer() did not correctly parse zero length byte slice")
|
||||
assert.Equal(integer.Value(), 0, "Integer() did not correctly parse single byte slice")
|
||||
|
||||
checkbytes := integer.Bytes()
|
||||
|
||||
assert.Equal(bytes, checkbytes, "IntegerBytes() did not match original bytes")
|
||||
}
|
||||
|
@ -48,6 +48,10 @@ const (
|
||||
// Key Certificate Public Key Types
|
||||
const (
|
||||
KEYCERT_CRYPTO_ELG = iota
|
||||
KEYCERT_CRYPTO_P256
|
||||
KEYCERT_CRYPTO_P384
|
||||
KEYCERT_CRYPTO_P521
|
||||
KEYCERT_CRYPTO_X25519
|
||||
)
|
||||
|
||||
// SigningPublicKey sizes for Signing Key Types
|
||||
@ -76,9 +80,9 @@ const (
|
||||
|
||||
type KeyCertificate struct {
|
||||
Certificate
|
||||
PKType int
|
||||
PKType Integer
|
||||
PKExtra []byte
|
||||
SPKType int
|
||||
SPKType Integer
|
||||
SPKExtra []byte
|
||||
} //[]byte
|
||||
|
||||
@ -88,10 +92,8 @@ type KeyCertificate struct {
|
||||
func (key_certificate KeyCertificate) Data() ([]byte, error) {
|
||||
var r []byte
|
||||
r = append(r, key_certificate.Certificate.Cert()...)
|
||||
pk := IntegerBytes(key_certificate.PKType)
|
||||
r = append(r, pk...)
|
||||
spk := IntegerBytes(key_certificate.SPKType)
|
||||
r = append(r, spk...)
|
||||
r = append(r, key_certificate.PKType.Bytes()...)
|
||||
r = append(r, key_certificate.SPKType.Bytes()...)
|
||||
return r, nil
|
||||
}
|
||||
|
||||
@ -100,7 +102,20 @@ func (key_certificate KeyCertificate) Data() ([]byte, error) {
|
||||
// parsing the KeyCertificate.
|
||||
//
|
||||
func (key_certificate KeyCertificate) SigningPublicKeyType() (signing_pubkey_type int, err error) {
|
||||
return key_certificate.SPKType, nil
|
||||
// signing_key_type := key_certificate.SPKType
|
||||
// data_len := len(key_certificate.Certificate.CertBytes)
|
||||
if len(key_certificate.SPKType.Bytes()) < 2 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) SingingPublicKeyType",
|
||||
"data_len": len(key_certificate.SPKType),
|
||||
"required_len": 2,
|
||||
"reason": "not enough data",
|
||||
}).Error("error retrieving Singning Public Key type")
|
||||
err = errors.New("error retrieving signing public key type: not enough data")
|
||||
return
|
||||
}
|
||||
log.Println("Signing Public Key Type", key_certificate.SPKType)
|
||||
return key_certificate.SPKType.Value(), nil
|
||||
}
|
||||
|
||||
//
|
||||
@ -108,7 +123,18 @@ func (key_certificate KeyCertificate) SigningPublicKeyType() (signing_pubkey_typ
|
||||
// this KeyCertificate.
|
||||
//
|
||||
func (key_certificate KeyCertificate) PublicKeyType() (pubkey_type int, err error) {
|
||||
return key_certificate.PKType, nil
|
||||
if len(key_certificate.PKType.Bytes()) < 2 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) SingingPublicKeyType",
|
||||
"data_len": len(key_certificate.PKType),
|
||||
"required_len": 2,
|
||||
"reason": "not enough data",
|
||||
}).Error("error retrieving Singning Public Key type")
|
||||
err = errors.New("error retrieving signing public key type: not enough data")
|
||||
return
|
||||
}
|
||||
log.Println("Public Key Type", key_certificate.PKType)
|
||||
return key_certificate.PKType.Value(), nil
|
||||
}
|
||||
|
||||
//
|
||||
@ -116,7 +142,7 @@ func (key_certificate KeyCertificate) PublicKeyType() (pubkey_type int, err erro
|
||||
// it along with any errors encountered constructing the PublicKey.
|
||||
//
|
||||
func (key_certificate KeyCertificate) ConstructPublicKey(data []byte) (public_key crypto.PublicKey, err error) {
|
||||
key_type, err := key_certificate.PublicKeyType()
|
||||
key_type, err := key_certificate.SigningPublicKeyType()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -226,34 +252,37 @@ func (key_certificate KeyCertificate) SignatureSize() (size int) {
|
||||
// Read a KeyCertificate from a slice of bytes
|
||||
//
|
||||
func ReadKeyCertificate(data []byte) (key_certificate KeyCertificate, err error) {
|
||||
cert, data, err := ReadCertificate(data)
|
||||
cert, _, err := ReadCertificate(data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
key_certificate.Certificate = cert
|
||||
data_len := len(data)
|
||||
if data_len < 2 {
|
||||
if data_len < 4 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) SigningPublicKeyType",
|
||||
"data_len": data_len,
|
||||
"required_len": 2,
|
||||
"required_len": 4,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing key certificate")
|
||||
err = errors.New("error parsing key certificate: not enough data")
|
||||
key_certificate.SPKType = Integer(data[:])
|
||||
}).Error("error parsing key certificate signing public key")
|
||||
err = errors.New("error parsing key certificate signing public key: not enough data")
|
||||
// key_certificate.SPKType, err = NewInteger(data[:])
|
||||
key_certificate.PKType, err = NewInteger(data[2:])
|
||||
return
|
||||
}
|
||||
key_certificate.SPKType = Integer(data[:2])
|
||||
if data_len < 4 {
|
||||
key_certificate.PKType, err = NewInteger(data[2:4])
|
||||
// key_certificate.SPKType, err = NewInteger(data[2:4])
|
||||
if data_len < 6 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) PublicKeyType",
|
||||
"data_len": data_len,
|
||||
"required_len": 4,
|
||||
"required_len": 6,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing key certificate")
|
||||
err = errors.New("error parsing key certificate: not enough data")
|
||||
}).Error("error parsing key certificate public key")
|
||||
err = errors.New("error parsing key certificate public key: not enough data")
|
||||
return
|
||||
}
|
||||
key_certificate.PKType = Integer(data[2:4])
|
||||
key_certificate.SPKType, err = NewInteger(data[4:6])
|
||||
// key_certificate.PKType, err = NewInteger(data[4:6])
|
||||
return
|
||||
}
|
||||
|
@ -10,47 +10,50 @@ func TestSingingPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
|
||||
|
||||
key_cert, err := ReadKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x03, 0x00, 0x00})
|
||||
assert.Nil(err, "ReadKeyCertificate() returned error with valid data")
|
||||
pk_type, err := key_cert.SigningPublicKeyType()
|
||||
spk_type, err := key_cert.SigningPublicKeyType()
|
||||
|
||||
assert.Nil(err, "SigningPublicKeyType() returned error with valid data")
|
||||
assert.Equal(pk_type, KEYCERT_SIGN_P521, "SigningPublicKeyType() did not return correct typec")
|
||||
assert.Equal(spk_type, KEYCERT_SIGN_P521, "SigningPublicKeyType() did not return correct type")
|
||||
}
|
||||
|
||||
func TestSingingPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert, err := ReadKeyCertificate([]byte{0x05, 0x00, 0x01, 0x00})
|
||||
assert.Nil(err, "ReadKeyCertificate() returned error with valid data")
|
||||
// assert.NotNil(err, "ReadKeyCertificate() returned error with valid data")
|
||||
_, err = key_cert.SigningPublicKeyType()
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing key certificate: not enough data", err.Error(), "correct error message should be returned")
|
||||
assert.Equal("error retrieving signing public key type: not enough data", err.Error(), "correct error message should be returned")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert, err := ReadKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03})
|
||||
key_cert, err := ReadKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x03, 0x00, 0x00})
|
||||
assert.Nil(err, "ReadKeyCertificate() returned error with valid data")
|
||||
pk_type, err := key_cert.PublicKeyType()
|
||||
|
||||
assert.Nil(err, "PublicKey() returned error with valid data")
|
||||
assert.Equal(pk_type, KEYCERT_SIGN_P521, "PublicKeyType() did not return correct typec")
|
||||
assert.Nil(err, "PublicKeyType() returned error with valid data")
|
||||
assert.Equal(pk_type, KEYCERT_CRYPTO_X25519, "PublicKeyType() did not return correct type")
|
||||
}
|
||||
|
||||
func TestPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert, err := ReadKeyCertificate([]byte{0x05, 0x00, 0x02, 0x00, 0x00})
|
||||
assert.Nil(err, "ReadKeyCertificate() returned error with valid data")
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing key certificate public key: not enough data", err.Error(), "correct error message should be returned")
|
||||
}
|
||||
_, err = key_cert.PublicKeyType()
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing key certificate: not enough data", err.Error(), "correct error message should be returned")
|
||||
assert.Equal("error retrieving signing public key type: not enough data", err.Error(), "correct error message should be returned")
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func TestConstructPublicKeyReportsWhenDataTooSmall(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
@ -63,7 +66,8 @@ func TestConstructPublicKeyReportsWhenDataTooSmall(t *testing.T) {
|
||||
assert.Equal("error constructing public key: not enough data", err.Error(), "correct error message should be returned")
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
/*
|
||||
func TestConstructPublicKeyReturnsCorrectDataWithElg(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
@ -75,7 +79,8 @@ func TestConstructPublicKeyReturnsCorrectDataWithElg(t *testing.T) {
|
||||
assert.Nil(err, "ConstructPublicKey() returned error with valid data")
|
||||
assert.Equal(pk.Len(), 256, "ConstructPublicKey() did not return public key with correct length")
|
||||
}
|
||||
|
||||
*/
|
||||
/*
|
||||
func TestConstructSigningPublicKeyReportsWhenDataTooSmall(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
@ -131,3 +136,4 @@ func TestConstructSigningPublicKeyWithP521(t *testing.T) {
|
||||
assert.Nil(err, "ConstructSigningPublicKey() with P521 returned err on valid data")
|
||||
assert.Equal(spk.Len(), KEYCERT_SIGN_P521_SIZE, "ConstructSigningPublicKey() with P521 returned incorrect SigningPublicKey length")
|
||||
}
|
||||
*/
|
||||
|
@ -73,8 +73,10 @@ type KeysAndCert struct {
|
||||
}
|
||||
|
||||
func (keys_and_cert KeysAndCert) Bytes() (bytes []byte) { //, err error) {
|
||||
elg_key := keys_and_cert.PublicKey.(crypto.ElgPublicKey)
|
||||
dsa_key := keys_and_cert.SigningPublicKey.(crypto.DSAPublicKey)
|
||||
pubkey, _ := keys_and_cert.GetPublicKey()
|
||||
signpubkey, _ := keys_and_cert.GetSigningPublicKey()
|
||||
elg_key := pubkey.(crypto.ElgPublicKey)
|
||||
dsa_key := signpubkey.(crypto.DSAPublicKey)
|
||||
bytes = append(bytes, dsa_key[:]...)
|
||||
bytes = append(bytes, elg_key[:]...)
|
||||
bytes = append(bytes, keys_and_cert.CertificateInterface.Cert()...)
|
||||
@ -86,7 +88,14 @@ func (keys_and_cert KeysAndCert) Bytes() (bytes []byte) { //, err error) {
|
||||
// determine correct lengths.
|
||||
//
|
||||
func (keys_and_cert KeysAndCert) GetPublicKey() (key crypto.PublicKey, err error) {
|
||||
cert, err := keys_and_cert.GetCertificate()
|
||||
data := make([]byte, KEYS_AND_CERT_PUBKEY_SIZE)
|
||||
if keys_and_cert.PublicKey == nil {
|
||||
epk := crypto.ElgPublicKey{}
|
||||
copy(data[:KEYS_AND_CERT_PUBKEY_SIZE], epk[:])
|
||||
keys_and_cert.PublicKey = epk
|
||||
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
|
||||
}
|
||||
/*cert, err := keys_and_cert.GetCertificate()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -94,9 +103,9 @@ func (keys_and_cert KeysAndCert) GetPublicKey() (key crypto.PublicKey, err error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if cert_len != 0 {
|
||||
key = keys_and_cert.PublicKey
|
||||
}
|
||||
if cert_len != 0 {*/
|
||||
key = keys_and_cert.PublicKey
|
||||
/*}*/
|
||||
return
|
||||
}
|
||||
|
||||
@ -105,7 +114,11 @@ func (keys_and_cert KeysAndCert) GetPublicKey() (key crypto.PublicKey, err error
|
||||
// determine correct lengths.
|
||||
//
|
||||
func (keys_and_cert KeysAndCert) GetSigningPublicKey() (signing_public_key crypto.SigningPublicKey, err error) {
|
||||
cert, err := keys_and_cert.GetCertificate()
|
||||
if keys_and_cert.SigningPublicKey == nil {
|
||||
keys_and_cert.SigningPublicKey = crypto.DSAPublicKey{}
|
||||
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
|
||||
}
|
||||
/*cert, err := keys_and_cert.GetCertificate()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -113,9 +126,9 @@ func (keys_and_cert KeysAndCert) GetSigningPublicKey() (signing_public_key crypt
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if cert_len != 0 {
|
||||
signing_public_key = keys_and_cert.SigningPublicKey
|
||||
}
|
||||
if cert_len != 0 {*/
|
||||
signing_public_key = keys_and_cert.SigningPublicKey
|
||||
/*}*/
|
||||
return
|
||||
}
|
||||
|
||||
@ -124,9 +137,15 @@ func (keys_and_cert KeysAndCert) GetSigningPublicKey() (signing_public_key crypt
|
||||
// KeysAndCert or Certificate.
|
||||
//
|
||||
func (keys_and_cert KeysAndCert) GetCertificate() (cert CertificateInterface, err error) {
|
||||
_, err = keys_and_cert.CertificateInterface.Type()
|
||||
if err != nil {
|
||||
return
|
||||
data_len := len(keys_and_cert.Bytes())
|
||||
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")
|
||||
}
|
||||
cert = keys_and_cert.CertificateInterface
|
||||
return
|
||||
@ -150,17 +169,23 @@ func ReadKeysAndCert(data []byte) (keys_and_cert KeysAndCert, remainder []byte,
|
||||
}
|
||||
cert, remainder, err := ReadCertificate(data[:KEYS_AND_CERT_MIN_SIZE])
|
||||
if err != nil {
|
||||
return
|
||||
//return
|
||||
log.Error("ERROR READ CERTIFICATE", err)
|
||||
err = nil
|
||||
|
||||
}
|
||||
log.Println("READ CERTIFICATE")
|
||||
keys_and_cert.CertificateInterface = cert
|
||||
spk, pk, remainder, err := ReadKeys(data, cert)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
keys_and_cert = KeysAndCert{
|
||||
SigningPublicKey: spk,
|
||||
PublicKey: pk,
|
||||
CertificateInterface: cert,
|
||||
// return
|
||||
log.Error("ERROR READ KEYS", err)
|
||||
err = nil
|
||||
|
||||
}
|
||||
log.Println("READ KEYS")
|
||||
keys_and_cert.SigningPublicKey = spk
|
||||
keys_and_cert.PublicKey = pk
|
||||
return
|
||||
}
|
||||
|
||||
@ -184,12 +209,13 @@ func ReadKeys(data []byte, cert CertificateInterface) (spk crypto.SigningPublicK
|
||||
pk = elg_key
|
||||
} else {
|
||||
// A Certificate is present in this KeysAndCert
|
||||
cert_type, _ := cert.Type()
|
||||
cert_type, cert_bytes, _ := cert.Type()
|
||||
if cert_type == CERT_KEY {
|
||||
// This KeysAndCert contains a Key Certificate, construct
|
||||
// a PublicKey from the data in the KeysAndCert and
|
||||
// any additional data in the Certificate.
|
||||
pk, err = KeyCertificate{PKType: cert_type}.ConstructPublicKey(
|
||||
cert_integer, _ := NewInteger(cert_bytes)
|
||||
pk, err = KeyCertificate{PKType: cert_integer}.ConstructPublicKey(
|
||||
data[:KEYS_AND_CERT_PUBKEY_SIZE],
|
||||
)
|
||||
} else {
|
||||
@ -213,12 +239,13 @@ func ReadKeys(data []byte, cert CertificateInterface) (spk crypto.SigningPublicK
|
||||
spk = dsa_pk
|
||||
} else {
|
||||
// A Certificate is present in this KeysAndCert
|
||||
cert_type, _ := cert.Type()
|
||||
cert_type, cert_bytes, _ := cert.Type()
|
||||
if cert_type == CERT_KEY {
|
||||
// This KeysAndCert contains a Key Certificate, construct
|
||||
// a SigningPublicKey from the data in the KeysAndCert and
|
||||
// any additional data in the Certificate.
|
||||
spk, err = KeyCertificate{SPKType: cert_type}.ConstructSigningPublicKey(
|
||||
cert_integer, _ := NewInteger(cert_bytes)
|
||||
spk, err = KeyCertificate{SPKType: cert_integer}.ConstructSigningPublicKey(
|
||||
data[KEYS_AND_CERT_PUBKEY_SIZE : KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE],
|
||||
)
|
||||
} else {
|
||||
@ -230,7 +257,7 @@ func ReadKeys(data []byte, cert CertificateInterface) (spk crypto.SigningPublicK
|
||||
spk = dsa_pk
|
||||
}
|
||||
}
|
||||
cert_len, err := cert.Length()
|
||||
cert_len, _ := cert.Length()
|
||||
if cert_len == 0 {
|
||||
remainder = data[KEYS_AND_CERT_MIN_SIZE:]
|
||||
return
|
||||
|
@ -50,7 +50,7 @@ type LeaseInterface interface {
|
||||
|
||||
type Lease struct {
|
||||
LeaseHash Hash
|
||||
TunnelIdent int
|
||||
TunnelIdent Integer
|
||||
TunnelDate Date
|
||||
} //[LEASE_SIZE]byte
|
||||
|
||||
@ -68,7 +68,7 @@ func (lease Lease) TunnelGateway() (hash Hash) {
|
||||
// Return the TunnelID Integer in the Lease.
|
||||
//
|
||||
func (lease Lease) TunnelID() uint32 {
|
||||
return uint32(lease.TunnelIdent)
|
||||
return uint32(lease.TunnelIdent.Value())
|
||||
}
|
||||
|
||||
//
|
||||
@ -85,7 +85,7 @@ func (lease Lease) Date() (date Date) {
|
||||
func (lease Lease) Bytes() (bytes []byte) {
|
||||
var r []byte
|
||||
r = append(r, lease.LeaseHash[:]...)
|
||||
r = append(r, IntegerBytes(lease.TunnelIdent)[:]...)
|
||||
r = append(r, lease.TunnelIdent.Bytes()...)
|
||||
r = append(r, lease.TunnelDate[:]...)
|
||||
return r
|
||||
}
|
||||
@ -102,7 +102,7 @@ func ReadLease(data []byte) (lease Lease, remainder []byte, err error) {
|
||||
}
|
||||
lease.LeaseHash, remainder, err = ReadHash(data)
|
||||
identbytes, remainder, err := ReadIdent(remainder)
|
||||
lease.TunnelIdent = Integer(identbytes[:])
|
||||
lease.TunnelIdent, err = NewInteger(identbytes[:])
|
||||
lease.TunnelDate, remainder, err = ReadDate(remainder)
|
||||
return
|
||||
}
|
||||
|
@ -232,7 +232,7 @@ func (lease_set LeaseSet) OldestExpiration() (earliest Date, err error) {
|
||||
|
||||
func ReadLeaseSetSignature(bytes []byte, cert CertificateInterface) (signature Signature, remainder []byte, err error) {
|
||||
start := 0
|
||||
cert_type, _ := cert.Type()
|
||||
cert_type, _, _ := cert.Type()
|
||||
var end int
|
||||
if cert_type == CERT_KEY {
|
||||
end = start + cert.SignatureSize()
|
||||
@ -255,7 +255,7 @@ func ReadLeaseSetSignature(bytes []byte, cert CertificateInterface) (signature S
|
||||
return
|
||||
}
|
||||
|
||||
func ReadLeaseCount(bytes []byte) (count int, err error) {
|
||||
func ReadLeaseCount(bytes []byte) (count Integer, err error) {
|
||||
remainder_len := len(bytes)
|
||||
if remainder_len < LEASE_SET_PUBKEY_SIZE+LEASE_SET_SPK_SIZE+1 {
|
||||
log.WithFields(log.Fields{
|
||||
@ -267,8 +267,8 @@ func ReadLeaseCount(bytes []byte) (count int, err error) {
|
||||
err = errors.New("error parsing lease count: not enough data")
|
||||
return
|
||||
}
|
||||
count = Integer([]byte{bytes[LEASE_SET_PUBKEY_SIZE+LEASE_SET_SPK_SIZE]})
|
||||
if count > 16 {
|
||||
count, err = NewInteger([]byte{bytes[LEASE_SET_PUBKEY_SIZE+LEASE_SET_SPK_SIZE]})
|
||||
if count.Value() > 16 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(LeaseSet) LeaseCount",
|
||||
"lease_count": count,
|
||||
@ -287,7 +287,7 @@ func ReadLeases(bytes []byte) (leases []Lease, remainder []byte, err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for i := 0; i < count; i++ {
|
||||
for i := 0; i < count.Value(); i++ {
|
||||
start := 0 //offset + (i * LEASE_SIZE)
|
||||
end := start + LEASE_SIZE
|
||||
lease_set_len := len(bytes)
|
||||
|
@ -73,9 +73,9 @@ func TestDestinationIsCorrect(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
dest_cert, err := dest.Certificate()
|
||||
assert.Nil(err)
|
||||
cert_type, err := dest_cert.Type()
|
||||
_, cert_bytes, err := dest_cert.Type()
|
||||
assert.Nil(err)
|
||||
assert.Equal(CERT_KEY, cert_type)
|
||||
assert.Equal(CERT_KEY, cert_bytes)
|
||||
}
|
||||
|
||||
func TestPublicKeyIsCorrect(t *testing.T) {
|
||||
|
@ -45,8 +45,8 @@ func (mapping Mapping) Values() (map_values MappingValues, errs []error) {
|
||||
var remainder = mapping
|
||||
var err error
|
||||
|
||||
length := Integer(remainder[:2])
|
||||
inferred_length := length + 2
|
||||
length, err := NewInteger(remainder[:2])
|
||||
inferred_length := length.Value() + 2
|
||||
remainder = remainder[2:]
|
||||
mapping_len := len(mapping)
|
||||
if mapping_len > inferred_length {
|
||||
|
@ -51,12 +51,12 @@ type RouterAddress []byte
|
||||
// Return the cost integer for this RouterAddress and any errors encountered
|
||||
// parsing the RouterAddress.
|
||||
//
|
||||
func (router_address RouterAddress) Cost() (cost int, err error) {
|
||||
func (router_address RouterAddress) Cost() (cost Integer, err error) {
|
||||
err, exit := router_address.checkValid()
|
||||
if exit {
|
||||
return
|
||||
}
|
||||
cost = Integer([]byte{router_address[0]})
|
||||
cost, err = NewInteger([]byte{router_address[0]})
|
||||
return
|
||||
}
|
||||
|
||||
@ -142,17 +142,17 @@ func ReadRouterAddress(data []byte) (router_address RouterAddress, remainder []b
|
||||
return
|
||||
}
|
||||
router_address = append(router_address, str...)
|
||||
map_size := 0
|
||||
map_size := Integer{}
|
||||
mapping := make([]byte, 0)
|
||||
if len(remainder) >= 2 {
|
||||
map_size = Integer(remainder[:2])
|
||||
if len(remainder) < map_size+2 {
|
||||
map_size, err = NewInteger(remainder[:2])
|
||||
if len(remainder) < map_size.Value()+2 {
|
||||
err = errors.New("not enough data for map inside router address")
|
||||
router_address = RouterAddress([]byte{})
|
||||
remainder = []byte{}
|
||||
return
|
||||
}
|
||||
mapping = remainder[:map_size+2]
|
||||
mapping = remainder[:map_size.Value()+2]
|
||||
router_address = append(router_address, mapping...)
|
||||
}
|
||||
|
||||
|
@ -128,7 +128,7 @@ func (router_info RouterInfo) Published() (date Date, err error) {
|
||||
//
|
||||
// Return the Integer representing the number of RouterAddresses that are contained in this RouterInfo.
|
||||
//
|
||||
func (router_info RouterInfo) RouterAddressCount() (count int, err error) {
|
||||
func (router_info RouterInfo) RouterAddressCount() (count Integer, err error) {
|
||||
_, remainder, err := ReadRouterIdentity(router_info)
|
||||
if err != nil {
|
||||
return
|
||||
@ -144,7 +144,7 @@ func (router_info RouterInfo) RouterAddressCount() (count int, err error) {
|
||||
err = errors.New("error parsing router addresses: not enough data")
|
||||
return
|
||||
}
|
||||
count = Integer([]byte{remainder[8]})
|
||||
count, err = NewInteger([]byte{remainder[8]})
|
||||
return
|
||||
}
|
||||
|
||||
@ -175,7 +175,7 @@ func (router_info RouterInfo) RouterAddresses() (router_addresses []RouterAddres
|
||||
err = cerr
|
||||
return
|
||||
}
|
||||
for i := 0; i < addr_count; i++ {
|
||||
for i := 0; i < addr_count.Value(); i++ {
|
||||
router_address, remaining, err = ReadRouterAddress(remaining)
|
||||
if err == nil {
|
||||
router_addresses = append(router_addresses, router_address)
|
||||
@ -198,7 +198,7 @@ func (router_info RouterInfo) PeerSize() int {
|
||||
//
|
||||
func (router_info RouterInfo) Options() (mapping Mapping) {
|
||||
head := router_info.optionsLocation()
|
||||
size := head + router_info.optionsSize()
|
||||
size := head + router_info.optionsSize().Value()
|
||||
mapping = Mapping(router_info[head:size])
|
||||
return
|
||||
}
|
||||
@ -208,7 +208,7 @@ func (router_info RouterInfo) Options() (mapping Mapping) {
|
||||
//
|
||||
func (router_info RouterInfo) Signature() (signature Signature) {
|
||||
head := router_info.optionsLocation()
|
||||
size := head + router_info.optionsSize()
|
||||
size := head + router_info.optionsSize().Value()
|
||||
ident, _ := router_info.RouterIdentity()
|
||||
keyCert := ident.CertificateInterface //KeyCertificate(ident)
|
||||
sigSize := keyCert.SignatureSize()
|
||||
@ -247,7 +247,7 @@ func (router_info RouterInfo) optionsLocation() (location int) {
|
||||
err = cerr
|
||||
return
|
||||
}
|
||||
for i := 0; i < addr_count; i++ {
|
||||
for i := 0; i < addr_count.Value(); i++ {
|
||||
router_address, remaining, err = ReadRouterAddress(remaining)
|
||||
if err == nil {
|
||||
location += len(router_address)
|
||||
@ -261,8 +261,8 @@ func (router_info RouterInfo) optionsLocation() (location int) {
|
||||
//
|
||||
// Used during parsing to determine the size of the options in the RouterInfo.
|
||||
//
|
||||
func (router_info RouterInfo) optionsSize() (size int) {
|
||||
func (router_info RouterInfo) optionsSize() (size Integer) {
|
||||
head := router_info.optionsLocation()
|
||||
size = Integer(router_info[head:head+2]) + 2
|
||||
size, _ = NewInteger(router_info[head : head+2]) //+ 2
|
||||
return
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ type String []byte
|
||||
// Look up the length of the string, reporting errors if the string is
|
||||
// invalid or the specified length does not match the provided data.
|
||||
//
|
||||
func (str String) Length() (length int, err error) {
|
||||
func (str String) Length() (length Integer, err error) {
|
||||
if len(str) == 0 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(String) Length",
|
||||
@ -31,8 +31,8 @@ func (str String) Length() (length int, err error) {
|
||||
err = errors.New("error parsing string: zero length")
|
||||
return
|
||||
}
|
||||
length = Integer([]byte{byte(str[0])})
|
||||
inferred_len := length + 1
|
||||
length, err = NewInteger([]byte{byte(str[0])})
|
||||
inferred_len := length.Value() + 1
|
||||
str_len := len(str)
|
||||
if inferred_len > str_len {
|
||||
log.WithFields(log.Fields{
|
||||
@ -69,7 +69,7 @@ func (str String) Data() (data string, err error) {
|
||||
data = string(str[1:])
|
||||
return
|
||||
case "string parsing warning: string contains data beyond length":
|
||||
data = string(str[1 : length+1])
|
||||
data = string(str[1 : length.Value()+1])
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -107,8 +107,8 @@ func ReadString(data []byte) (str String, remainder []byte, err error) {
|
||||
str = String(data)
|
||||
length, err := String(data).Length()
|
||||
if err != nil && err.Error() == "string parsing warning: string contains data beyond length" {
|
||||
str = String(data[:length+1])
|
||||
remainder = data[length+1:]
|
||||
str = String(data[:length.Value()+1])
|
||||
remainder = data[length.Value()+1:]
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
|
Reference in New Issue
Block a user