keypairs/keypairs_test.go

135 lines
3.1 KiB
Go
Raw Permalink Normal View History

2019-02-07 19:30:04 +00:00
package keypairs
import (
"crypto/ecdsa"
2019-02-07 20:05:26 +00:00
"crypto/rsa"
2019-02-07 19:30:04 +00:00
"io/ioutil"
2019-03-07 00:16:00 +00:00
"log"
"net/http"
2019-02-07 19:30:04 +00:00
"testing"
)
2019-02-07 20:05:26 +00:00
// TODO package all fixtures with fileb0x
func TestParsePrivateKeyEC(t *testing.T) {
keys := [][]string{
[]string{"fixtures/privkey-ec-p256.jwk.json", "bJiCcZHuAF9dDetKEdGjJU3pFvFLoB_QHe9_6cAuY8c"},
// has openssl EC Param block
[]string{"fixtures/privkey-ec-p256.sec1.pem", "bJiCcZHuAF9dDetKEdGjJU3pFvFLoB_QHe9_6cAuY8c"},
[]string{"fixtures/privkey-ec-p256.pkcs8.pem", "bJiCcZHuAF9dDetKEdGjJU3pFvFLoB_QHe9_6cAuY8c"},
[]string{"fixtures/privkey-ec-p384.jwk.json", "-WoRXrk3FZ7tGi8oj5wJHDDfFMBCGlUbpwil1WhpxrU"},
[]string{"fixtures/privkey-ec-p384.sec1.pem", "-WoRXrk3FZ7tGi8oj5wJHDDfFMBCGlUbpwil1WhpxrU"},
[]string{"fixtures/privkey-ec-p384.pkcs8.pem", "-WoRXrk3FZ7tGi8oj5wJHDDfFMBCGlUbpwil1WhpxrU"},
}
for i := range keys {
path := keys[i][0]
thumb := keys[i][1]
b, err := ioutil.ReadFile(path)
if nil != err {
t.Fatal(path, err)
}
key, err := ParsePrivateKey(b)
if nil != err {
t.Fatal(path, err)
}
eckey := key.(*ecdsa.PrivateKey)
thumb2 := ThumbprintECPublicKey(eckey.Public().(*ecdsa.PublicKey))
if thumb != thumb2 {
t.Fatalf("EC thumbprints do not match: %q, %q, %q", path, thumb, thumb2)
}
}
}
2019-07-11 16:59:19 +00:00
func TestParseUnexpectedPrivateKey(t *testing.T) {
keypaths := []string{
"fixtures/privkey-ec-p256.jwk.json",
"fixtures/privkey-ec-p256.sec1.pem",
"fixtures/privkey-ec-p256.pkcs8.pem",
"fixtures/privkey-rsa-2048.jwk.json",
"fixtures/privkey-rsa-2048.pkcs1.pem",
"fixtures/privkey-rsa-2048.pkcs8.pem",
}
for i := range keypaths {
path := keypaths[i]
b, err := ioutil.ReadFile(path)
if nil != err {
t.Fatal(path, err)
}
_, err = ParsePublicKey(b)
switch err {
case ErrUnexpectedPrivateKey:
continue
default:
t.Fatal(path, err)
}
}
}
func TestParseUnexpectedPublicKey(t *testing.T) {
keypaths := []string{
"fixtures/pub-ec-p256.jwk.json",
}
for i := range keypaths {
path := keypaths[i]
b, err := ioutil.ReadFile(path)
if nil != err {
t.Fatal(path, err)
}
_, err = ParsePrivateKey(b)
switch err {
case ErrUnexpectedPublicKey:
continue
default:
t.Fatal(path, err)
}
}
}
2019-02-07 20:05:26 +00:00
func TestParsePrivateKeyRSA(t *testing.T) {
2019-02-07 19:30:04 +00:00
keypaths := []string{
2019-02-07 20:05:26 +00:00
"fixtures/privkey-rsa-2048.jwk.json",
"fixtures/privkey-rsa-2048.pkcs1.pem",
"fixtures/privkey-rsa-2048.pkcs8.pem",
2019-02-07 19:30:04 +00:00
}
for i := range keypaths {
path := keypaths[i]
b, err := ioutil.ReadFile(path)
if nil != err {
2019-02-07 20:05:26 +00:00
t.Fatal(path, err)
2019-02-07 19:30:04 +00:00
}
key, err := ParsePrivateKey(b)
if nil != err {
2019-02-07 20:05:26 +00:00
t.Fatal(path, err)
2019-02-07 19:30:04 +00:00
}
2019-02-07 20:05:26 +00:00
rsakey := key.(*rsa.PrivateKey)
thumb := "UIyZzFXPL-mTLnxQeSAHgu7gV16tro3evksnFb8fFQQ"
thumb2 := ThumbprintRSAPublicKey(rsakey.Public().(*rsa.PublicKey))
if thumb != thumb2 {
t.Fatalf("RSA thumbprints do not match: %q, %q, %q", path, thumb, thumb2)
}
2019-02-07 19:30:04 +00:00
}
}
2019-03-07 00:16:00 +00:00
func TestParseCertificate(t *testing.T) {
2019-07-11 16:59:19 +00:00
resp, err := http.Get("https://example.auth0.com/pem")
2019-03-07 00:16:00 +00:00
if nil != err {
log.Fatal(err)
}
bytes, err := ioutil.ReadAll(resp.Body)
if nil != err {
log.Fatal(err)
}
_, err = ParsePublicKey(bytes)
if nil != err {
log.Fatal("Could not parse PEM/cert from auth0")
log.Fatal(err)
}
}