Skip to content

Commit

Permalink
Add pkcs8 tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ali Yousuf <[email protected]>
  • Loading branch information
alyyousuf7 committed Apr 10, 2017
1 parent f899e98 commit c330536
Showing 1 changed file with 271 additions and 0 deletions.
271 changes: 271 additions & 0 deletions tuf/utils/pkcs8_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
package utils

import (
"crypto/rand"
"crypto/x509"
"encoding/asn1"
"encoding/pem"
"testing"

"github.com/docker/notary/tuf/data"
"github.com/stretchr/testify/require"
)

func TestConvertTUFKeyToPKCS8(t *testing.T) {
testRSAKey, err := GenerateRSAKey(rand.Reader, 1024)
require.NoError(t, err)

testECKey, err := GenerateECDSAKey(rand.Reader)
require.NoError(t, err)

testEDKey, err := GenerateED25519Key(rand.Reader)
require.NoError(t, err)

testConvertKeyToPKCS8(t, testRSAKey)
testConvertKeyToPKCS8(t, testRSAKey, []byte("poonies"))
testConvertKeyToPKCS8(t, testECKey)
testConvertKeyToPKCS8(t, testECKey, []byte("poonies"))
testConvertKeyToPKCS8(t, testEDKey)
testConvertKeyToPKCS8(t, testEDKey, []byte("poonies"))
}

func testConvertKeyToPKCS8(t *testing.T, privKey data.PrivateKey, v ...[]byte) {
var der []byte
var err error

if v != nil {
der, err = ConvertTUFKeyToPKCS8(privKey, v[0])
require.NoError(t, err, "could not convert private key to pkcs8")

keyInfo, err := ParsePKCS8ToTufKey(der, v[0])
require.NoError(t, err, "could not decrypt the newly created pkcs8 key")
require.EqualValues(t, keyInfo.Private(), privKey.Private())
} else {
der, err = ConvertTUFKeyToPKCS8(privKey)
require.NoError(t, err, "could not convert private key to pkcs8")

var keyInfo privateKeyInfo
_, err = asn1.Unmarshal(der, &keyInfo)
require.NoError(t, err, "could not unmarshal pkcs8")

require.EqualValues(t, keyInfo.PrivateKey, privKey.Private(), "private key did not match")
}
}

func TestParsePKCS8ToTufKey(t *testing.T) {
testRSAKeyParsing(t)
testECKeyParsing(t)
testEDKeyPars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JVX7usyMxfQBm5J0qsQYDCFmCsdP1wzMGLYce
U9bJpFzkiBfaQRucvtTJpQ7PEVLYCg40CGQGpHFrhzeYAO19TQGZolkv6xcYrYmg
PY+2QtRt4EfaacNpxSJcrCYFT81OP6UV0x8XSvdFtAGbf5PjChFDTsoAkbNSVrd2
NREZMhxFATh3Ek9y/2nxo8pDZ1N0Vid6sOg5dtryB7p9ZZgczleY7B8xaLsoCN4y
6T1wYok7Pg9inKoB4J3yun4Td85Xwo8aT0aulX/oN6XjZt3ZjjtK+RXoZMfARAJ1
u+rmy9/CMkLtFQM4Hlbb75WeFA==
-----END ENCRYPTED PRIVATE KEY-----
`)

block, _ := pem.Decode(testRSAPEM)
key, err := ParsePKCS8ToTufKey(block.Bytes)
require.NoError(t, err, "could not parse pkcs8 to tuf key")

block, _ = pem.Decode(testEncryptedRSAPEM)
encryptedKey, err := ParsePKCS8ToTufKey(block.Bytes, []byte("poonies"))
require.NoError(t, err, "could not parse encrypted pkcs8 to tuf key")

require.Equal(t, "rsa", key.Algorithm())
require.Equal(t, "rsa", encryptedKey.Algorithm())
require.EqualValues(t, key.Private(), encryptedKey.Private())
}

func testECKeyParsing(t *testing.T) {
testECPEM := []byte(`-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgiwt5YfD/xQdVwJZ0
2TpiJDQQ8DRHYVeWzIscya52BvChRANCAAT58IHVQJwbo3/MS/dFjh+xM85gVydX
xY+wxYDkaougZDPIgvu3+bQZ4xYSAnCGX7UJIiLloKuuuvbmXQlnSGqw
-----END PRIVATE KEY-----
`)

testEncryptedECPEM := []byte(`-----BEGIN ENCRYPTED PRIVATE KEY-----
MIHeMEkGCSqGSIb3DQEFDTA8MBsGCSqGSIb3DQEFDDAOBAhau2ksQB01lgICCAAw
HQYJYIZIAWUDBAEqBBABQd7kN7aKsbD62UQ8QQl7BIGQO4cuxkugQZLMLUPV39Jl
24jAl0lFLdvAPWZoD9Z5jCa7Fbw/uFza/PVZkScHv6LC5BAah4+NmyydiJiIbP4D
9udZlQ/T4ui4/zrm/19LGP7TPB8LtxaNl6xyKSbDiJHqkz30h+qGr5naCu5xb1dd
P2RiPFJIOaO8pEegcJ5EL++ZJXDaX0UAi3X7E1tS27ye
-----END ENCRYPTED PRIVATE KEY-----
`)

block, _ := pem.Decode(testECPEM)
key, err := ParsePKCS8ToTufKey(block.Bytes)
require.NoError(t, err, "could not parse pkcs8 to tuf key")

block, _ = pem.Decode(testEncryptedECPEM)
encryptedKey, err := ParsePKCS8ToTufKey(block.Bytes, []byte("poonies"))
require.NoError(t, err, "could not parse encrypted pkcs8 to tuf key")

require.Equal(t, "ecdsa", key.Algorithm())
require.Equal(t, "ecdsa", encryptedKey.Algorithm())
require.EqualValues(t, key.Private(), encryptedKey.Private())
}

func testEDKeyParsing(t *testing.T) {
testEDPEM := []byte(`-----BEGIN PRIVATE KEY-----
MHICAQAwCwYJKwYBBAHaRw8BBGCfPLwirynVJ9GtVhP09qv6S/pxXk6ShNkSZXET
ZTevbDrnPXK5qTt/0my/S8C3u2Mpu9p/iccMdC3LKIHSVcHZnzy8Iq8p1SfRrVYT
9Par+kv6cV5OkoTZEmVxE2U3r2w=
-----END PRIVATE KEY-----
`)

testEncryptedEDPEM := []byte(`-----BEGIN ENCRYPTED PRIVATE KEY-----
MIHOMEkGCSqGSIb3DQEFDTA8MBsGCSqGSIb3DQEFDDAOBAj+MwzZJHeOhAICCAAw
HQYJYIZIAWUDBAEqBBAMXNkY2MNIjsrD45U26kTVBIGADbwDy6D1oS97IMRHehLt
tuUqWEQU+WAaeLV+Q4MuLDfEbC6QndePxCjI54kcTdbc4cZTrNYOZ36bKub2AYlF
6NHmknrZU9UAi82ke+vhhQGxj9x1iMpKbRLhX9P7M36AvrCEeu2ELnwTUJwTVdRb
U8bzszAw0sz1TAyqygiWq8c=
-----END ENCRYPTED PRIVATE KEY-----
`)

block, _ := pem.Decode(testEDPEM)
key, err := ParsePKCS8ToTufKey(block.Bytes)
require.NoError(t, err, "could not parse pkcs8 to tuf key")

block, _ = pem.Decode(testEncryptedEDPEM)
encryptedKey, err := ParsePKCS8ToTufKey(block.Bytes, []byte("poonies"))
require.NoError(t, err, "could not parse encrypted pkcs8 to tuf key")

require.Equal(t, "ed25519", key.Algorithm())
require.Equal(t, "ed25519", encryptedKey.Algorithm())
require.EqualValues(t, key.Private(), encryptedKey.Private())
}

func TestPEMtoPEM(t *testing.T) {
testInputPKCS1 := []byte(`-----BEGIN RSA PRIVATE KEY-----
MIIEpgIBAAKCAQEA3vRQI7s20MF0Zc3fywttsw72OkRXuTT0/JQrSuoilzOSaoLK
p7sYprIeIu9OeXqvBbwAxe3i1GViGwWM8cH9QqD05XhMz0Crr9vu2zHaZFEI9mgT
XcQxMQGntZ4xYV/rL/fddzj7+n1oKNvovS800NvPEMUkkgApdp5ES605V1q51tBp
LEYJ82xb5vT8cVseFYfA4G+gVqLNfQlasa0QsQT4YlVEDbbwT3/wuMG/m+wTx2p8
urhD+69oQbORkpqkNiEzMNidOrvtD7qyab+cUNamYU0CKOFn/KhWuoZV7EVYnc+o
evm7naYsenDq43Q5hGyacEuTjGtLnUG32d8RewIDAQABAoIBAQDeLOBfewSY6u8v
NAU7tVvP/6znS4uPiHJJ8O1jbgaiXkYd1dBVbWCXXRAjCA5PiC45rKuokfJkbdNh
0houIH5ck0D4GvWP4oY0bRqNXBShuw8PXY9O9V9/0oJpvga/XnJkDsCnOiX/7FCL
xvka7ZvYNfMWZx6WT4sCJZ0xPKHTpT09SzbhDOCLOsM4nWbs5xkXuEGPkD289z+N
OmENdcKDHz0mgYAr7hKJI3oAt2ogTjSyiQBLmxgudBUP5oJ1qY6/kYUCTYE3cjss
Y/mqfNcKtylQpTIUaUCw8BhOf3yJFA0GSI6C7hp96cjEk2dRQxAtYhSZJPA2uN+D
1/UIUeSBAoGBAO9VnVaTvkvc55upY9kUKnZcztJwG2Hf9FRrJrgC2RIaj3KNEImU
QyPgIVBXRzvd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GBAMsn
QufNMZmFL9xb737Assbf5QRJd1bCj1Zfx7FIzMFFVtlYENDWIrW9R47cDmSAUGgC
923cavbEh7A3e8V/ctKhpeuU40YidIIPFyUQYNo57amI0R+yo1vw5roW2YrOedFK
AIWg901asyzZFeskCufcyiHrkBbDeo+JNtmrGJazAoGBAMOxKBm9HpFXB0h90+FA
6aQgL6FfF578QTW+s5UsZUJjKunYuGmXnYOb3iF6Kvvc/qmKDB1TInVtho1N5TcD
pLTbO3/JPtJyolvYXjnBI7qXjpPxJeicDbA919iDaEVtW1tQOQ9g7WBG0VorWUSr
oQSGi2o00tBJXEiEPmsJK2HL
-----END PRIVATE KEY-----
`)

block, _ := pem.Decode(testInputPKCS1)
require.NotEmpty(t, block)

rsaKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
require.NoError(t, err)

testPrivKey, err := RSAToPrivateKey(rsaKey)
require.NoError(t, err)

der, err := ConvertTUFKeyToPKCS8(testPrivKey)
require.NoError(t, err, "could not convert pkcs1 to pkcs8")

testOutput := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der})
require.EqualValues(t, testOutputPKCS8, testOutput)
}

0 comments on commit c330536

Please sign in to comment.