2020-04-10 19:29:01 +00:00
|
|
|
package mockid
|
|
|
|
|
|
|
|
import (
|
2020-08-01 23:59:20 +00:00
|
|
|
"bytes"
|
2020-07-25 09:13:19 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2020-08-01 07:32:17 +00:00
|
|
|
"crypto/rsa"
|
2020-07-25 09:13:19 +00:00
|
|
|
"encoding/json"
|
2020-08-01 07:32:17 +00:00
|
|
|
"errors"
|
2020-07-25 09:13:19 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-08-01 07:32:17 +00:00
|
|
|
"log"
|
|
|
|
mathrand "math/rand"
|
2020-07-25 09:13:19 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2020-04-10 19:41:10 +00:00
|
|
|
"testing"
|
2020-08-01 07:32:17 +00:00
|
|
|
|
2020-08-01 23:59:20 +00:00
|
|
|
"git.coolaj86.com/coolaj86/go-mockid/mockid/api"
|
|
|
|
"git.coolaj86.com/coolaj86/go-mockid/xkeypairs"
|
2020-08-01 07:32:17 +00:00
|
|
|
"git.rootprojects.org/root/keypairs"
|
2020-04-10 19:41:10 +00:00
|
|
|
//keypairs "github.com/big-squid/go-keypairs"
|
|
|
|
//"github.com/big-squid/go-keypairs/keyfetch/uncached"
|
2020-04-10 19:29:01 +00:00
|
|
|
)
|
|
|
|
|
2020-07-25 09:13:19 +00:00
|
|
|
var srv *httptest.Server
|
|
|
|
|
2020-08-01 07:32:17 +00:00
|
|
|
type TestReader struct{}
|
|
|
|
|
|
|
|
func (TestReader) Read(p []byte) (n int, err error) {
|
|
|
|
return mathrand.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testrnd = TestReader{}
|
|
|
|
|
|
|
|
func init() {
|
2020-08-01 23:59:20 +00:00
|
|
|
api.RandomReader = testrnd
|
2020-08-01 07:32:17 +00:00
|
|
|
rndsrc = testrnd
|
|
|
|
}
|
|
|
|
|
2020-07-25 09:13:19 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2020-08-01 07:32:17 +00:00
|
|
|
mathrand.Seed(0) // Predictable results
|
|
|
|
|
2020-07-25 09:13:19 +00:00
|
|
|
os.Setenv("SALT", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
|
|
|
|
jwksPrefix := "public-jwks"
|
|
|
|
err := os.MkdirAll(jwksPrefix, 0755)
|
|
|
|
if nil != err {
|
|
|
|
fmt.Fprintf(os.Stderr, "couldn't write %q: %s", jwksPrefix, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-08-01 07:32:17 +00:00
|
|
|
privkey, _ := ecdsa.GenerateKey(elliptic.P256(), rndsrc)
|
2020-07-25 09:13:19 +00:00
|
|
|
mux := Route(jwksPrefix, privkey)
|
|
|
|
|
|
|
|
srv = httptest.NewServer(mux)
|
|
|
|
|
|
|
|
//fs := http.FileServer(http.Dir("public"))
|
|
|
|
//http.Handle("/", fs)
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2020-08-01 07:32:17 +00:00
|
|
|
func TestGenerateJWK(t *testing.T) {
|
2020-07-25 09:13:19 +00:00
|
|
|
client := srv.Client()
|
|
|
|
urlstr, _ := url.Parse(srv.URL + "/private.jwk.json")
|
|
|
|
//fmt.Println("URL:", srv.URL, urlstr)
|
|
|
|
res, err := client.Do(&http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: urlstr,
|
|
|
|
})
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
2020-08-01 07:32:17 +00:00
|
|
|
return
|
2020-07-25 09:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(res.Body)
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
2020-08-01 07:32:17 +00:00
|
|
|
return
|
2020-07-25 09:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jwk := map[string]string{}
|
|
|
|
err = json.Unmarshal(data, &jwk)
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
2020-08-01 07:32:17 +00:00
|
|
|
return
|
2020-07-25 09:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if "" == jwk["d"] {
|
|
|
|
t.Fatal("Missing key 'd' from supposed private key")
|
|
|
|
}
|
2020-08-01 07:32:17 +00:00
|
|
|
|
|
|
|
key, err := keypairs.ParsePrivateKey(data)
|
|
|
|
if nil != err {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch key.(type) {
|
|
|
|
case *rsa.PrivateKey:
|
|
|
|
// no-op
|
|
|
|
log.Println("is RSA")
|
|
|
|
case *ecdsa.PrivateKey:
|
|
|
|
// no-op
|
|
|
|
log.Println("is EC")
|
|
|
|
default:
|
|
|
|
t.Fatal(errors.New("impossible key type"))
|
|
|
|
}
|
|
|
|
|
2020-07-25 09:13:19 +00:00
|
|
|
//fmt.Printf("%#v\n", jwk)
|
2020-04-10 19:29:01 +00:00
|
|
|
}
|
2020-08-01 07:32:17 +00:00
|
|
|
|
2020-08-01 23:59:20 +00:00
|
|
|
func TestGenWithSeed(t *testing.T) {
|
|
|
|
// Key A
|
|
|
|
client := srv.Client()
|
|
|
|
urlstr, _ := url.Parse(srv.URL + "/private.jwk.json")
|
|
|
|
res, err := client.Do(&http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: urlstr,
|
2020-08-02 08:16:28 +00:00
|
|
|
Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"seed":"test"}`))),
|
2020-08-01 23:59:20 +00:00
|
|
|
})
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dataA, err := ioutil.ReadAll(res.Body)
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-02 08:16:28 +00:00
|
|
|
// See https://github.com/square/go-jose/issues/189
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
// Key B
|
|
|
|
client = srv.Client()
|
|
|
|
urlstr, _ = url.Parse(srv.URL + "/private.jwk.json")
|
|
|
|
res, err = client.Do(&http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: urlstr,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"seed":"test"}`))),
|
|
|
|
})
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dataB, err := ioutil.ReadAll(res.Body)
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if '{' != dataA[0] || len(dataA) < 100 || string(dataA) != string(dataB) {
|
|
|
|
log.Println(string(dataA))
|
|
|
|
log.Println(string(dataB))
|
|
|
|
t.Error(errors.New("keys with identical seeds should be identical"))
|
|
|
|
return
|
|
|
|
}
|
2020-08-01 23:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenWithRand(t *testing.T) {
|
|
|
|
// Key A
|
|
|
|
client := srv.Client()
|
|
|
|
urlstr, _ := url.Parse(srv.URL + "/private.jwk.json")
|
|
|
|
res, err := client.Do(&http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: urlstr,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"seed":""}`))),
|
|
|
|
})
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dataA, err := ioutil.ReadAll(res.Body)
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key B
|
|
|
|
client = srv.Client()
|
|
|
|
urlstr, _ = url.Parse(srv.URL + "/private.jwk.json")
|
|
|
|
res, err = client.Do(&http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: urlstr,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"seed":""}`))),
|
|
|
|
})
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dataB, err := ioutil.ReadAll(res.Body)
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(dataA) == string(dataB) {
|
|
|
|
t.Error(errors.New("keys with identical seeds should yield identical keys"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 07:32:17 +00:00
|
|
|
func TestGeneratePEM(t *testing.T) {
|
|
|
|
client := srv.Client()
|
|
|
|
urlstr, _ := url.Parse(srv.URL + "/priv.pem")
|
|
|
|
//fmt.Println("URL:", srv.URL, urlstr)
|
|
|
|
res, err := client.Do(&http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: urlstr,
|
|
|
|
})
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(res.Body)
|
|
|
|
if nil != err {
|
|
|
|
//t.Fatal(err)
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-01 23:59:20 +00:00
|
|
|
key, err := xkeypairs.ParsePEMPrivateKey(data)
|
2020-08-01 07:32:17 +00:00
|
|
|
if nil != err {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch key.(type) {
|
|
|
|
case *rsa.PrivateKey:
|
|
|
|
// no-op
|
|
|
|
log.Println("is RSA")
|
|
|
|
case *ecdsa.PrivateKey:
|
|
|
|
// no-op
|
|
|
|
log.Println("is EC")
|
|
|
|
default:
|
|
|
|
t.Fatal(errors.New("impossible key type"))
|
|
|
|
}
|
|
|
|
|
|
|
|
//fmt.Printf("%#v\n", key)
|
|
|
|
}
|