Beispiel #1
0
func TestSoftwareSignHandler(t *testing.T) {
	// We associate both key types with this signing service to bypass the
	// ID -> keyType logic in the tests
	sigService := api.NewEdDSASigningService(keys.NewKeyDB())
	setup(signer.SigningServiceIndex{data.ED25519Key: sigService, data.RSAKey: sigService})

	key, err := sigService.CreateKey()
	assert.Nil(t, err)

	sigRequest := &pb.SignatureRequest{KeyID: key.KeyInfo.KeyID, Content: make([]byte, 10)}
	requestJson, _ := json.Marshal(sigRequest)

	reader = strings.NewReader(string(requestJson))

	request, err := http.NewRequest("POST", signBaseURL, reader)

	assert.Nil(t, err)

	res, err := http.DefaultClient.Do(request)
	assert.Nil(t, err)

	assert.Equal(t, 200, res.StatusCode)

	jsonBlob, err := ioutil.ReadAll(res.Body)
	assert.Nil(t, err)

	var sig *pb.Signature
	err = json.Unmarshal(jsonBlob, &sig)
	assert.Nil(t, err)

	assert.Equal(t, key.KeyInfo.KeyID.ID, sig.KeyInfo.KeyID.ID)
}
Beispiel #2
0
func init() {
	sigService := api.NewEdDSASigningService(keys.NewKeyDB())
	sigServices := signer.SigningServiceIndex{data.ED25519Key: sigService, data.RSAKey: sigService}
	void = &pb.Void{}
	//server setup
	kms := &api.KeyManagementServer{SigServices: sigServices}
	ss := &api.SignerServer{SigServices: sigServices}
	grpcServer = grpc.NewServer()
	pb.RegisterKeyManagementServer(grpcServer, kms)
	pb.RegisterSignerServer(grpcServer, ss)
	lis, err := net.Listen("tcp", "127.0.0.1:7899")
	if err != nil {
		log.Fatalf("failed to listen %v", err)
	}

	go grpcServer.Serve(lis)

	//client setup
	conn, err := grpc.Dial("127.0.0.1:7899")
	if err != nil {
		log.Fatalf("fail to dial: %v", err)
	}
	kmClient = pb.NewKeyManagementClient(conn)
	sClient = pb.NewSignerClient(conn)
}
Beispiel #3
0
func TestKeyInfoHandler(t *testing.T) {
	// We associate both key types with this signing service to bypass the
	// ID -> keyType logic in the tests
	sigService := api.NewEdDSASigningService(keys.NewKeyDB())
	setup(signer.SigningServiceIndex{data.ED25519Key: sigService, data.RSAKey: sigService})

	key, _ := sigService.CreateKey()

	keyInfoURL := fmt.Sprintf("%s/%s", keyInfoBaseURL, key.KeyInfo.KeyID.ID)

	request, err := http.NewRequest("GET", keyInfoURL, nil)
	assert.Nil(t, err)

	res, err := http.DefaultClient.Do(request)
	assert.Nil(t, err)

	jsonBlob, err := ioutil.ReadAll(res.Body)
	assert.Nil(t, err)

	var pubKey *pb.PublicKey
	err = json.Unmarshal(jsonBlob, &pubKey)
	assert.Nil(t, err)

	assert.Equal(t, key.KeyInfo.KeyID.ID, pubKey.KeyInfo.KeyID.ID)
	assert.Equal(t, 200, res.StatusCode)
}
func TestSigner(t *testing.T) {
	fakeKeyID := "830158bb5a4af00a3f689a8f29120f0fa7f8ae57cf00ce1fede8ae8652b5181a"
	m := FakeKeyDB{}
	sigService := api.NewEdDSASigningService(&m)

	m.On("GetKey", fakeKeyID).Return(&data.PrivateKey{}, nil).Once()
	_, err := sigService.Signer(&pb.KeyID{ID: fakeKeyID})

	m.Mock.AssertExpectations(t)
	assert.Nil(t, err)
}
func TestCreateKey(t *testing.T) {
	m := FakeKeyDB{}
	sigService := api.NewEdDSASigningService(&m)

	m.On("AddKey").Return(nil).Once()

	_, err := sigService.CreateKey()

	m.Mock.AssertExpectations(t)
	assert.Nil(t, err)
}
func BenchmarkSign(b *testing.B) {
	blob := []byte("7d16f1d0b95310a7bc557747fc4f20fcd41c1c5095ae42f189df")

	keyDB := keys.NewKeyDB()
	var sigService = api.NewEdDSASigningService(keyDB)

	key, _ := sigService.CreateKey()
	privkey, _ := keyDB.GetKey(key.KeyInfo.KeyID)

	signer := api.NewEd25519Signer(privkey)
	sigRequest := &pb.SignatureRequest{KeyID: key.KeyInfo.KeyID, Content: blob}

	for n := 0; n < b.N; n++ {
		_, _ = signer.Sign(sigRequest)
	}
}
Beispiel #7
0
func TestKeyInfoHandlerReturns404WithNonexistentKey(t *testing.T) {
	// We associate both key types with this signing service to bypass the
	// ID -> keyType logic in the tests
	sigService := api.NewEdDSASigningService(keys.NewKeyDB())
	setup(signer.SigningServiceIndex{data.ED25519Key: sigService, data.RSAKey: sigService})

	fakeID := "c62e6d68851cef1f7e55a9d56e3b0c05f3359f16838cad43600f0554e7d3b54d"
	keyInfoURL := fmt.Sprintf("%s/%s", keyInfoBaseURL, fakeID)

	request, err := http.NewRequest("GET", keyInfoURL, nil)
	assert.Nil(t, err)

	res, err := http.DefaultClient.Do(request)
	assert.Nil(t, err)

	assert.Equal(t, 404, res.StatusCode)
}
Beispiel #8
0
func TestDeleteKeyHandler(t *testing.T) {
	// We associate both key types with this signing service to bypass the
	// ID -> keyType logic in the tests
	sigService := api.NewEdDSASigningService(keys.NewKeyDB())
	setup(signer.SigningServiceIndex{data.ED25519Key: sigService, data.RSAKey: sigService})

	key, _ := sigService.CreateKey()

	requestJson, _ := json.Marshal(key.KeyInfo.KeyID)
	reader = strings.NewReader(string(requestJson))

	request, err := http.NewRequest("POST", deleteKeyBaseURL, reader)
	assert.Nil(t, err)

	res, err := http.DefaultClient.Do(request)
	assert.Nil(t, err)

	assert.Equal(t, 200, res.StatusCode)
}
Beispiel #9
0
func TestDeleteKeyHandlerReturns404WithNonexistentKey(t *testing.T) {
	// We associate both key types with this signing service to bypass the
	// ID -> keyType logic in the tests
	sigService := api.NewEdDSASigningService(keys.NewKeyDB())
	setup(signer.SigningServiceIndex{data.ED25519Key: sigService, data.RSAKey: sigService})

	fakeID := "c62e6d68851cef1f7e55a9d56e3b0c05f3359f16838cad43600f0554e7d3b54d"

	keyID := &pb.KeyID{ID: fakeID}
	requestJson, _ := json.Marshal(keyID)
	reader = strings.NewReader(string(requestJson))

	request, err := http.NewRequest("POST", deleteKeyBaseURL, reader)
	assert.Nil(t, err)

	res, err := http.DefaultClient.Do(request)
	assert.Nil(t, err)

	assert.Equal(t, 404, res.StatusCode)
}
Beispiel #10
0
func TestSoftwareCreateKeyHandler(t *testing.T) {
	// We associate both key types with this signing service to bypass the
	// ID -> keyType logic in the tests
	sigService := api.NewEdDSASigningService(keys.NewKeyDB())
	setup(signer.SigningServiceIndex{data.ED25519Key: sigService, data.RSAKey: sigService})

	createKeyURL := fmt.Sprintf("%s/%s", createKeyBaseURL, data.ED25519Key)

	request, err := http.NewRequest("POST", createKeyURL, nil)
	assert.Nil(t, err)

	res, err := http.DefaultClient.Do(request)
	assert.Nil(t, err)

	assert.Equal(t, 200, res.StatusCode)

	jsonBlob, err := ioutil.ReadAll(res.Body)
	assert.Nil(t, err)

	var keyInfo *pb.PublicKey
	err = json.Unmarshal(jsonBlob, &keyInfo)
	assert.Nil(t, err)
}
Beispiel #11
0
func TestSoftwareSignWithInvalidRequestHandler(t *testing.T) {
	// We associate both key types with this signing service to bypass the
	// ID -> keyType logic in the tests
	sigService := api.NewEdDSASigningService(keys.NewKeyDB())
	setup(signer.SigningServiceIndex{data.ED25519Key: sigService, data.RSAKey: sigService})

	requestJson := "{\"blob\":\"7d16f1d0b95310a7bc557747fc4f20fcd41c1c5095ae42f189df0717e7d7f4a0a2b55debce630f43c4ac099769c612965e3fda3cd4c0078ee6a460f14fa19307\"}"
	reader = strings.NewReader(requestJson)

	request, err := http.NewRequest("POST", signBaseURL, reader)

	assert.Nil(t, err)

	res, err := http.DefaultClient.Do(request)
	assert.Nil(t, err)

	jsonBlob, err := ioutil.ReadAll(res.Body)
	assert.Nil(t, err)

	var sig *pb.Signature
	err = json.Unmarshal(jsonBlob, &sig)

	assert.Equal(t, 400, res.StatusCode)
}
func BenchmarkCreateKey(b *testing.B) {
	sigService := api.NewEdDSASigningService(keys.NewKeyDB())
	for n := 0; n < b.N; n++ {
		_, _ = sigService.CreateKey()
	}
}