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