2022-07-11 23:41:58 -04:00
|
|
|
package key_certificate
|
2016-02-20 19:02:55 -08:00
|
|
|
|
|
|
|
import (
|
2024-12-04 01:40:23 -05:00
|
|
|
"testing"
|
2016-02-20 19:02:55 -08:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2024-12-04 01:40:23 -05:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
func TestSigningPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2016-03-28 22:16:52 -07:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
// Create certificate with signing key type P521 (3)
|
|
|
|
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x03, 0x00, 0x07})
|
|
|
|
assert.Nil(err)
|
2022-04-27 10:48:59 -04:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
pk_type := key_cert.SigningPublicKeyType()
|
2025-03-02 16:43:47 -05:00
|
|
|
assert.Equal(KEYCERT_SIGN_P521, pk_type, "SigningPublicKeyType() did not return correct type")
|
2025-02-10 16:41:59 -05:00
|
|
|
}
|
2016-03-28 22:16:52 -07:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
func TestSigningPublicKeyTypeWithInvalidData(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2016-02-20 19:02:55 -08:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
// 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)
|
|
|
|
}
|
2016-03-28 22:16:52 -07:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
func TestPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2016-03-28 22:16:52 -07:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
// Create certificate with crypto type ELG (0)
|
|
|
|
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
|
|
|
assert.Nil(err)
|
2016-02-20 19:02:55 -08:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
pk_type := key_cert.PublicKeyType()
|
|
|
|
assert.Equal(KEYCERT_CRYPTO_ELG, pk_type, "PublicKeyType() did not return correct type")
|
|
|
|
}
|
2016-03-28 22:16:52 -07:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
func TestPublicKeyTypeWithInvalidData(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2016-03-28 22:16:52 -07:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
// 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)
|
|
|
|
}
|
2024-11-24 14:34:14 -05:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
func TestConstructPublicKeyWithInsufficientData(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2016-02-20 19:02:55 -08:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
|
|
|
assert.Nil(err)
|
2016-03-28 22:16:52 -07:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
// Test with data smaller than required size
|
|
|
|
data := make([]byte, 255) // ELG requires 256 bytes
|
|
|
|
_, err = key_cert.ConstructPublicKey(data)
|
2016-03-28 22:16:52 -07:00
|
|
|
|
2025-02-10 16:41:59 -05:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal("error constructing public key: not enough data", err.Error())
|
|
|
|
}
|
2025-02-16 14:38:06 -05:00
|
|
|
|
2016-02-20 19:02:55 -08:00
|
|
|
func TestConstructPublicKeyReturnsCorrectDataWithElg(t *testing.T) {
|
2016-03-28 22:16:52 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-05-09 20:43:24 -04:00
|
|
|
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
2016-02-20 19:02:55 -08:00
|
|
|
data := make([]byte, 256)
|
|
|
|
pk, err := key_cert.ConstructPublicKey(data)
|
2016-03-28 22:16:52 -07:00
|
|
|
|
|
|
|
assert.Nil(err, "ConstructPublicKey() returned error with valid data")
|
|
|
|
assert.Equal(pk.Len(), 256, "ConstructPublicKey() did not return public key with correct length")
|
2016-02-20 19:02:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstructSigningPublicKeyReportsWhenDataTooSmall(t *testing.T) {
|
2016-03-28 22:16:52 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-05-09 20:43:24 -04:00
|
|
|
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
2016-02-20 19:02:55 -08:00
|
|
|
data := make([]byte, 127)
|
2022-04-27 10:48:59 -04:00
|
|
|
_, err = key_cert.ConstructSigningPublicKey(data)
|
2016-03-28 22:16:52 -07:00
|
|
|
|
|
|
|
if assert.NotNil(err) {
|
|
|
|
assert.Equal("error constructing signing public key: not enough data", err.Error(), "correct error message should be returned")
|
2016-02-20 19:02:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstructSigningPublicKeyWithDSASHA1(t *testing.T) {
|
2016-03-28 22:16:52 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-05-09 20:43:24 -04:00
|
|
|
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
2016-02-20 19:02:55 -08:00
|
|
|
data := make([]byte, 128)
|
|
|
|
spk, err := key_cert.ConstructSigningPublicKey(data)
|
2016-03-28 22:16:52 -07:00
|
|
|
|
|
|
|
assert.Nil(err, "ConstructSigningPublicKey() with DSA SHA1 returned error with valid data")
|
2024-11-09 00:53:17 -05:00
|
|
|
assert.Equal(spk.Len(), KEYCERT_SIGN_DSA_SHA1_SIZE, "ConstructSigningPublicKey() with DSA SHA1 returned incorrect signingPublicKey length")
|
2016-02-20 19:02:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstructSigningPublicKeyWithP256(t *testing.T) {
|
2016-03-28 22:16:52 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-05-09 20:43:24 -04:00
|
|
|
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x01})
|
2016-02-20 19:02:55 -08:00
|
|
|
data := make([]byte, 128)
|
|
|
|
spk, err := key_cert.ConstructSigningPublicKey(data)
|
2016-03-28 22:16:52 -07:00
|
|
|
|
|
|
|
assert.Nil(err, "ConstructSigningPublicKey() with P256 returned err on valid data")
|
2024-11-09 00:53:17 -05:00
|
|
|
assert.Equal(spk.Len(), KEYCERT_SIGN_P256_SIZE, "ConstructSigningPublicKey() with P256 returned incorrect signingPublicKey length")
|
2016-02-20 19:02:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstructSigningPublicKeyWithP384(t *testing.T) {
|
2016-03-28 22:16:52 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-05-09 20:43:24 -04:00
|
|
|
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x02, 0x00, 0x02})
|
2016-02-20 19:02:55 -08:00
|
|
|
data := make([]byte, 128)
|
|
|
|
spk, err := key_cert.ConstructSigningPublicKey(data)
|
2016-03-28 22:16:52 -07:00
|
|
|
|
|
|
|
assert.Nil(err, "ConstructSigningPublicKey() with P384 returned err on valid data")
|
2024-11-09 00:53:17 -05:00
|
|
|
assert.Equal(spk.Len(), KEYCERT_SIGN_P384_SIZE, "ConstructSigningPublicKey() with P384 returned incorrect signingPublicKey length")
|
2016-02-20 19:02:55 -08:00
|
|
|
}
|
|
|
|
|
2024-11-17 12:55:34 -05:00
|
|
|
/*
|
2016-02-20 19:02:55 -08:00
|
|
|
func TestConstructSigningPublicKeyWithP521(t *testing.T) {
|
2016-03-28 22:16:52 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-05-09 20:43:24 -04:00
|
|
|
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x08, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00})
|
2024-11-12 23:54:50 +01:00
|
|
|
data := make([]byte, 132)
|
2016-02-20 19:02:55 -08:00
|
|
|
spk, err := key_cert.ConstructSigningPublicKey(data)
|
2016-03-28 22:16:52 -07:00
|
|
|
|
|
|
|
assert.Nil(err, "ConstructSigningPublicKey() with P521 returned err on valid data")
|
2024-11-09 00:53:17 -05:00
|
|
|
assert.Equal(spk.Len(), KEYCERT_SIGN_P521_SIZE, "ConstructSigningPublicKey() with P521 returned incorrect signingPublicKey length")
|
2016-02-20 19:02:55 -08:00
|
|
|
}
|
2024-11-17 12:55:34 -05:00
|
|
|
*/ //TODO -> Before implementing this test, we need to implement P521 first.
|