Skip to content

Commit

Permalink
Multiple changes based on code review
Browse files Browse the repository at this point in the history
* Add RSA private key encoded as PKCS8
* Refactor tests to use a table, DRY

Signed-off-by: Aditya Sirish <[email protected]>
  • Loading branch information
adityasaky committed Jan 8, 2024
1 parent 217bba2 commit 603a3a4
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 64 deletions.
2 changes: 1 addition & 1 deletion signerverifier/signerverifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func LoadKey(keyBytes []byte) (*SSLibKey, error) {
KeyType: RSAKeyType,
KeyVal: KeyVal{
Public: strings.TrimSpace(string(generatePEMBlock(pubKeyBytes, PublicKeyPEM))),
Private: strings.TrimSpace(string(generatePEMBlock(pemBlock.Bytes, RSAPrivateKeyPEM))),
Private: strings.TrimSpace(string(generatePEMBlock(pemBlock.Bytes, pemBlock.Type))),
},
Scheme: RSAKeyScheme,
}
Expand Down
150 changes: 87 additions & 63 deletions signerverifier/signerverifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package signerverifier

import (
_ "embed"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -10,6 +12,9 @@ import (
//go:embed test-data/rsa-test-key
var rsaPrivateKey []byte

//go:embed test-data/rsa-test-key-pkcs8
var rsaPrivateKeyPKCS8 []byte

//go:embed test-data/rsa-test-key.pub
var rsaPublicKey []byte

Expand All @@ -27,8 +32,9 @@ var ecdsaPublicKey []byte

func TestLoadKey(t *testing.T) {
// RSA expected values
expectedRSAPrivateKey := "-----BEGIN RSA PRIVATE KEY-----\nMIIG5AIBAAKCAYEA04egZRic+dZMVtiQc56DejU4FF1q3aOkUKnD+Q4lTbj1zp6O\nDKJTcktupmrad68jqtMiSGG8he6ELFs377q8bbgEUMWgAf+06Q8oFvUSfOXzZNFI\n7H5SMPOJY5aDWIMIEZ8DlcO7TfkA7D3iAEJXxxTOVS3UAIk5umO7Y7t7yXr8O/C4\nu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i2jmuIOqI2zCOqX67\nI+YQsJgrHE0TmYxxRkgeYUy7s5DoHE25rfvdy5Lx+xAOH8ZgD1SGOw==\n-----END RSA PRIVATE KEY-----"
expectedRSAPublicKey := "-----BEGIN PUBLIC KEY-----\nMIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEA04egZRic+dZMVtiQc56D\nejU4FF1q3aOkUKnD+Q4lTbj1zp6ODKJTcktupmrad68jqtMiSGG8he6ELFs377q8\nbbgEUMWgAf+06Q8oFvUSfOXzZNFI7H5SMPOJY5aDWIMIEZ8DlcO7TfkA7D3iAEJX\nxxTOVS3UAIk5umO7Y7t7yXr8O/C4u78krGazCnoblcekMLJZV4O/5BloWNAe/B1c\nvZdaZUf3brD4ZZrxEtXw/tefhn1aHsSUajVW2wwjSpKhqj7Z0XS3bDS3T95/3xsN\n6+hlS6A7rJfiWpKIRHj0vh2SXLDmmhQl1In8TD/aiycTUyWcBRHVPlYFgYPt6SaT\nVQSgMzSxC43/2fINb2fyt8SbUHJ3Ct+mzRzd/1AQikWhBdstJLxInewzjYE/sb+c\n2CmCxMPQG2BwmAWXaaumeJcXVPBlMgAcjMatM8bPByTbXpKDnQslOE7g/gswDIwn\nEm53T13mZzYUvbLJ0q3aljZVLIC3IZn3ZwA2yCWchBkVAgMBAAE=\n-----END PUBLIC KEY-----"
expectedRSAPrivateKey := strings.TrimSpace(strings.ReplaceAll(string(rsaPrivateKey), "\r\n", "\n"))
expectedRSAPrivateKeyPKCS8 := strings.TrimSpace(strings.ReplaceAll(string(rsaPrivateKeyPKCS8), "\r\n", "\n"))
expectedRSAPublicKey := strings.TrimSpace(strings.ReplaceAll(string(rsaPublicKey), "\r\n", "\n"))
expectedRSAKeyID := "4e8d20af09fcaed6c388a186427f94a5f7ff5591ec295f4aab2cff49ffe39e9b"

// ED25519 expected values
Expand All @@ -37,67 +43,85 @@ func TestLoadKey(t *testing.T) {
expectedED25519KeyID := "52e3b8e73279d6ebdd62a5016e2725ff284f569665eb92ccb145d83817a02997"

// ECDSA expected values
expectedECDSAPrivateKey := "-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgCjoPFeWCpjL5OS+h\nwg7JaWoDcpW2np2VJjuVLeSR4QyhRANCAAS74cSqqlctrjyVcf2uRHKCx+wIqrVc\nzfqhfKYn3DGkzrycKqStkJWdn2WQR4LAPypZhM3EPnJ9ZfAmMWH4ruot\n-----END PRIVATE KEY-----"
expectedECDSAPublicKey := "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEu+HEqqpXLa48lXH9rkRygsfsCKq1\nXM36oXymJ9wxpM68nCqkrZCVnZ9lkEeCwD8qWYTNxD5yfWXwJjFh+K7qLQ==\n-----END PUBLIC KEY-----"
expectedECDSAPrivateKey := strings.TrimSpace(strings.ReplaceAll(string(ecdsaPrivateKey), "\r\n", "\n"))
expectedECDSAPublicKey := strings.TrimSpace(strings.ReplaceAll(string(ecdsaPublicKey), "\r\n", "\n"))
expectedECDSAKeyID := "98adf38602c48c5479e9a991ee3f8cbf541ee4f985e00f7a5fc4148d9a45b704"

t.Run("RSA private key", func(t *testing.T) {
key, err := LoadKey(rsaPrivateKey)
assert.Nil(t, err)
assert.Equal(t, expectedRSAKeyID, key.KeyID)
assert.Equal(t, expectedRSAPublicKey, key.KeyVal.Public)
assert.Equal(t, expectedRSAPrivateKey, key.KeyVal.Private)
assert.Equal(t, RSAKeyScheme, key.Scheme)
assert.Equal(t, RSAKeyType, key.KeyType)
})

t.Run("RSA public key", func(t *testing.T) {
key, err := LoadKey(rsaPublicKey)
assert.Nil(t, err)
assert.Equal(t, expectedRSAKeyID, key.KeyID)
assert.Equal(t, expectedRSAPublicKey, key.KeyVal.Public)
assert.Equal(t, "", key.KeyVal.Private)
assert.Equal(t, RSAKeyScheme, key.Scheme)
assert.Equal(t, RSAKeyType, key.KeyType)
})

t.Run("ED25519 private key", func(t *testing.T) {
key, err := LoadKey(ed25519PrivateKey)
assert.Nil(t, err)
assert.Equal(t, expectedED25519KeyID, key.KeyID)
assert.Equal(t, expectedED25519PublicKey, key.KeyVal.Public)
assert.Equal(t, expectedED25519PrivateKey, key.KeyVal.Private)
assert.Equal(t, ED25519KeyType, key.Scheme)
assert.Equal(t, ED25519KeyType, key.KeyType)
})

t.Run("ED25519 public key", func(t *testing.T) {
key, err := LoadKey(ed25519PublicKey)
assert.Nil(t, err)
assert.Equal(t, expectedED25519KeyID, key.KeyID)
assert.Equal(t, expectedED25519PublicKey, key.KeyVal.Public)
assert.Equal(t, "", key.KeyVal.Private)
assert.Equal(t, ED25519KeyType, key.Scheme)
assert.Equal(t, ED25519KeyType, key.KeyType)
})

t.Run("ECDSA private key", func(t *testing.T) {
key, err := LoadKey(ecdsaPrivateKey)
assert.Nil(t, err)
assert.Equal(t, expectedECDSAKeyID, key.KeyID)
assert.Equal(t, expectedECDSAPublicKey, key.KeyVal.Public)
assert.Equal(t, expectedECDSAPrivateKey, key.KeyVal.Private)
assert.Equal(t, ECDSAKeyScheme, key.Scheme)
assert.Equal(t, ECDSAKeyType, key.KeyType)
})

t.Run("ECDSA public key", func(t *testing.T) {
key, err := LoadKey(ecdsaPublicKey)
assert.Nil(t, err)
assert.Equal(t, expectedECDSAKeyID, key.KeyID)
assert.Equal(t, expectedECDSAPublicKey, key.KeyVal.Public)
assert.Equal(t, "", key.KeyVal.Private)
assert.Equal(t, ECDSAKeyScheme, key.Scheme)
assert.Equal(t, ECDSAKeyType, key.KeyType)
})
tests := map[string]struct {
keyBytes []byte
expectedPrivateKey string
expectedPublicKey string
expectedKeyID string
expectedKeyType string
expectedScheme string
}{
"RSA private key": {
keyBytes: rsaPrivateKey,
expectedPrivateKey: expectedRSAPrivateKey,
expectedPublicKey: expectedRSAPublicKey,
expectedKeyID: expectedRSAKeyID,
expectedKeyType: RSAKeyType,
expectedScheme: RSAKeyScheme,
},
"RSA private key (PKCS8)": {
keyBytes: rsaPrivateKeyPKCS8,
expectedPrivateKey: expectedRSAPrivateKeyPKCS8,
expectedPublicKey: expectedRSAPublicKey,
expectedKeyID: expectedRSAKeyID,
expectedKeyType: RSAKeyType,
expectedScheme: RSAKeyScheme,
},
"RSA public key": {
keyBytes: rsaPublicKey,
expectedPrivateKey: "",
expectedPublicKey: expectedRSAPublicKey,
expectedKeyID: expectedRSAKeyID,
expectedKeyType: RSAKeyType,
expectedScheme: RSAKeyScheme,
},
"ED25519 private key": {
keyBytes: ed25519PrivateKey,
expectedPrivateKey: expectedED25519PrivateKey,
expectedPublicKey: expectedED25519PublicKey,
expectedKeyID: expectedED25519KeyID,
expectedKeyType: ED25519KeyType,
expectedScheme: ED25519KeyType,
},
"ED25519 public key": {
keyBytes: ed25519PublicKey,
expectedPrivateKey: "",
expectedPublicKey: expectedED25519PublicKey,
expectedKeyID: expectedED25519KeyID,
expectedKeyType: ED25519KeyType,
expectedScheme: ED25519KeyType,
},
"ECDSA private key": {
keyBytes: ecdsaPrivateKey,
expectedPrivateKey: expectedECDSAPrivateKey,
expectedPublicKey: expectedECDSAPublicKey,
expectedKeyID: expectedECDSAKeyID,
expectedKeyType: ECDSAKeyType,
expectedScheme: ECDSAKeyScheme,
},
"ECDSA public key": {
keyBytes: ecdsaPublicKey,
expectedPrivateKey: "",
expectedPublicKey: expectedECDSAPublicKey,
expectedKeyID: expectedECDSAKeyID,
expectedKeyType: ECDSAKeyType,
expectedScheme: ECDSAKeyScheme,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
key, err := LoadKey(test.keyBytes)
assert.Nil(t, err, fmt.Sprintf("unexpected error in test '%s'", name))
assert.Equal(t, test.expectedKeyID, key.KeyID)
assert.Equal(t, test.expectedPublicKey, key.KeyVal.Public)
assert.Equal(t, test.expectedPrivateKey, key.KeyVal.Private)
assert.Equal(t, test.expectedScheme, key.Scheme)
assert.Equal(t, test.expectedKeyType, key.KeyType)
})
}
}
40 changes: 40 additions & 0 deletions signerverifier/test-data/rsa-test-key-pkcs8
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-----BEGIN PRIVATE KEY-----
MIIG/gIBADANBgkqhkiG9w0BAQEFAASCBugwggbkAgEAAoIBgQDTh6BlGJz51kxW
2JBznoN6NTgUXWrdo6RQqcP5DiVNuPXOno4MolNyS26matp3ryOq0yJIYbyF7oQs
WzfvurxtuARQxaAB/7TpDygW9RJ85fNk0UjsflIw84ljloNYgwgRnwOVw7tN+QDs
PeIAQlfHFM5VLdQAiTm6Y7tju3vJevw78Li7vySsZrMKehuVx6QwsllXg7/kGWhY
0B78HVy9l1plR/dusPhlmvES1fD+15+GfVoexJRqNVbbDCNKkqGqPtnRdLdsNLdP
3n/fGw3r6GVLoDusl+JakohEePS+HZJcsOaaFCXUifxMP9qLJxNTJZwFEdU+VgWB
g+3pJpNVBKAzNLELjf/Z8g1vZ/K3xJtQcncK36bNHN3/UBCKRaEF2y0kvEid7DON
gT+xv5zYKYLEw9AbYHCYBZdpq6Z4lxdU8GUyAByMxq0zxs8HJNtekoOdCyU4TuD+
CzAMjCcSbndPXeZnNhS9ssnSrdqWNlUsgLchmfdnADbIJZyEGRUCAwEAAQKCAYAq
zAB4I8wywhhM4+EJ+3K3aYgQmrvx3tdyWH6lZ6bFfFwDM1IjHT6L29Ylxm2wFFe/
ipsfkOKvKExGmygMkTfpbPc2jl1quh0UoDPovSnB6lZdufgqnP4tGEDeTMgDQvOB
eS4lAWZmhrAYnAabLdELntqGIlEH0LvEycPSkE6hu4fH6tK9bZVSai7kOkNVgQBn
t9XannXXBQr3GRiDwr8OPs6TukVF+HkxOXF38aAiN2b3s2nl2rj6ahNKG0ESp+q+
K/U58NrfYMhIA5wQqILaODQ73qSEPPZiMrjQcBtasaNUHW/7+RYpNYnr910tv4OT
vsop5nilI8ph5QzuNeXJWKSe2YVPDgJcW+TZCo58nIHEDFzHh4H+fqPWaoi+ovpO
OWhef8GFz/rkWvl56feEttGeG03FcFRHBWhhgnAvDu1k4wqq3nWpspT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