spk and cpk reads were reversed

This commit is contained in:
eyedeekay
2025-02-10 16:41:59 -05:00
parent 440b9e8118
commit 6b2f231c36
5 changed files with 88 additions and 93 deletions

View File

@@ -28,8 +28,8 @@ payload :: data
*/
import (
"errors"
"fmt"
"github.com/go-i2p/go-i2p/lib/common/signature"
"github.com/go-i2p/go-i2p/lib/util/logger"
@@ -148,7 +148,7 @@ func (keyCertificate KeyCertificate) ConstructPublicKey(data []byte) (public_key
"required_len": KEYCERT_PUBKEY_SIZE,
"reason": "not enough data",
}).Error("error constructing public key")
err = errors.New("error constructing public key: not enough data")
err = fmt.Errorf("error constructing public key: not enough data")
return
}
switch key_type {
@@ -240,7 +240,7 @@ func (keyCertificate KeyCertificate) ConstructSigningPublicKey(data []byte) (sig
"required_len": KEYCERT_SPK_SIZE,
"reason": "not enough data",
}).Error("error constructing signing public key")
err = errors.New("error constructing signing public key: not enough data")
err = fmt.Errorf("error constructing signing public key: not enough data")
return
}
switch signing_key_type {
@@ -297,7 +297,7 @@ func (keyCertificate KeyCertificate) ConstructSigningPublicKey(data []byte) (sig
log.WithFields(logrus.Fields{
"signing_key_type": signing_key_type,
}).Warn("Unknown signing key type")
panic(err)
return nil, fmt.Errorf("unknown signing key type")
}
return
@@ -364,19 +364,23 @@ func NewKeyCertificate(bytes []byte) (key_certificate *KeyCertificate, remainder
return
}
payload := certificate.Data()
if certificate.Type() != CERT_KEY {
return nil, remainder, fmt.Errorf("invalid certificate type: %d", certificate.Type())
}
cpkTypeBytes := payload[0:2]
spkTypeBytes := payload[2:4]
cpkType := Integer(cpkTypeBytes)
spkType := Integer(spkTypeBytes)
if len(certificate.Data()) < 4 {
return nil, remainder, fmt.Errorf("key certificate data too short")
}
log.Println("Certificate Data in NewKeyCertificate: ", certificate.Data()[0:2], certificate.Data()[2:4])
cpkType, _ := ReadInteger(certificate.Data()[2:4], 2)
spkType, _ := ReadInteger(certificate.Data()[0:2], 2)
key_certificate = &KeyCertificate{
Certificate: certificate,
SpkType: spkType,
CpkType: cpkType,
SpkType: spkType,
}
log.Println("cpkType in NewKeyCertificate: ", cpkType.Int(), "spkType in NewKeyCertificate: ", spkType.Int())
log.WithFields(logrus.Fields{
"spk_type": key_certificate.SpkType.Int(),

View File

@@ -1,75 +1,66 @@
package key_certificate
import (
"github.com/stretchr/testify/assert"
"testing"
"github.com/stretchr/testify/assert"
)
/*
//TODO: Redo these tests
func TestSigningPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
assert := assert.New(t)
func TestSingingPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
assert := assert.New(t)
// Create certificate with signing key type P521 (3)
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x03, 0x00, 0x07})
assert.Nil(err)
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x03, 0x00, 0x00})
pk_type := key_cert.SigningPublicKeyType()
pk_type := key_cert.SigningPublicKeyType()
assert.Equal(KEYCERT_SIGN_ED25519, pk_type, "SigningPublicKeyType() did not return correct type")
}
assert.Nil(err, "SigningPublicKeyType() returned error with valid data")
assert.Equal(pk_type, KEYCERT_SIGN_P521, "SigningPublicKeyType() did not return correct typec")
}
func TestSigningPublicKeyTypeWithInvalidData(t *testing.T) {
assert := assert.New(t)
func TestSingingPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) {
assert := assert.New(t)
// Test with invalid short data
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x01, 0x00})
assert.NotNil(err)
assert.Contains(err.Error(), "key certificate data too short")
assert.Nil(key_cert)
}
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x01, 0x00})
func TestPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
assert := assert.New(t)
assert.NotNil(err, "Expected error when data is too small")
assert.Equal("key certificate payload too short", err.Error(), "Correct error message should be returned")
assert.Nil(key_cert, "key_cert should be nil when an error occurs")
// Create certificate with crypto type ELG (0)
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
assert.Nil(err)
if key_cert != nil {
sk_type := key_cert.SigningPublicKeyType()
assert.Equal(sk_type, 0, "SigningPublicKeyType() did not return correct type")
}
}
pk_type := key_cert.PublicKeyType()
assert.Equal(KEYCERT_CRYPTO_ELG, pk_type, "PublicKeyType() did not return correct type")
}
func TestPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
assert := assert.New(t)
func TestPublicKeyTypeWithInvalidData(t *testing.T) {
assert := assert.New(t)
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03})
pk_type := key_cert.PublicKeyType()
// Test with invalid short data
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x02})
assert.NotNil(err)
assert.Contains(err.Error(), "key certificate data too short", "Expected error for invalid data")
assert.Nil(key_cert)
}
assert.Nil(err, "publicKey() returned error with valid data")
assert.Equal(pk_type, KEYCERT_SIGN_P521, "PublicKeyType() did not return correct typec")
}
func TestConstructPublicKeyWithInsufficientData(t *testing.T) {
assert := assert.New(t)
func TestPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) {
assert := assert.New(t)
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
assert.Nil(err)
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x02, 0x00, 0x00})
// Test with data smaller than required size
data := make([]byte, 255) // ELG requires 256 bytes
_, err = key_cert.ConstructPublicKey(data)
assert.NotNil(err, "Expected error when data is too small")
assert.Equal("key certificate payload too short", err.Error(), "Correct error message should be returned")
assert.Nil(key_cert, "key_cert should be nil when an error occurs")
if key_cert != nil {
pk_type := key_cert.PublicKeyType()
assert.Equal(pk_type, 0, "PublicKeyType() did not return correct type")
}
}
func TestConstructPublicKeyReportsWhenDataTooSmall(t *testing.T) {
assert := assert.New(t)
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
data := make([]byte, 255)
_, err = key_cert.ConstructPublicKey(data)
if assert.NotNil(err) {
assert.Equal("error constructing public key: not enough data", err.Error(), "correct error message should be returned")
}
}
*/
assert.NotNil(err)
assert.Equal("error constructing public key: not enough data", err.Error())
}
func TestConstructPublicKeyReturnsCorrectDataWithElg(t *testing.T) {
assert := assert.New(t)