forked from keys-pub/keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert_test.go
64 lines (53 loc) · 1.96 KB
/
cert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package keys_test
import (
"crypto/x509"
"testing"
"github.com/keys-pub/keys"
"github.com/stretchr/testify/require"
)
func TestGenerateCertificateKey(t *testing.T) {
caCert, err := keys.GenerateCertificateKey("localhost", true, nil)
require.NoError(t, err)
require.True(t, len(caCert.Public()) > 0)
require.True(t, len(caCert.Private()) > 0)
certPool := x509.NewCertPool()
ok := certPool.AppendCertsFromPEM([]byte(caCert.Public()))
if !ok {
t.Fatal("failed to add to cert pool")
}
// TODO: Generated cert fails to verify
// xcaCert, xerr := caCert.X509Certificate()
// require.NoError(t, xerr)
// cert, certErr := GenerateCertificateKey("localhost", false, xcaCert)
// require.NoError(t, certErr)
xcert, err := caCert.X509Certificate()
require.NoError(t, err)
_, err = xcert.Verify(x509.VerifyOptions{
DNSName: "localhost",
Roots: certPool,
})
require.NoError(t, err)
certKey, err := keys.NewCertificateKey(caCert.Private(), caCert.Public())
require.NoError(t, err)
require.NotNil(t, certKey)
}
func TestCertificateKey(t *testing.T) {
public := `-----BEGIN CERTIFICATE-----
MIIBbDCCARKgAwIBAgIQI3ViQTyP8XxlaXUnwbKORjAKBggqhkjOPQQDAjAQMQ4w
DAYDVQQKEwVLZXl1cDAeFw0xOTA3MjQwMjAwMTZaFw0yOTA3MjEwMjAwMTZaMBAx
DjAMBgNVBAoTBUtleXVwMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEOXhT88Pe
/Ql5LFyxYUb9a0v+HOKqs2PGO/0CE4UPSj5XpocMUotMSm4Yau1/1j1SV+/Vktin
ixCC7hfVyswyFqNOMEwwDgYDVR0PAQH/BAQDAgKkMBMGA1UdJQQMMAoGCCsGAQUF
BwMBMA8GA1UdEwEB/wQFMAMBAf8wFAYDVR0RBA0wC4IJbG9jYWxob3N0MAoGCCqG
SM49BAMCA0gAMEUCIQDyOYbe6kzrU8Z45/KmkYX3fzfDAvjq3vqSUe5Xaf/KwQIg
CmvKhhT2XYwfNim1eLnU78spAetAyk//C7w+BfxgnPo=
-----END CERTIFICATE-----`
private := `-----BEGIN ECDSA PRIVATE KEY-----
MHcCAQEEIPflp/bXqmjd6AvkzfsGd2q1F+wjlJ8rVL1TEYYl3giVoAoGCCqGSM49
AwEHoUQDQgAEOXhT88Pe/Ql5LFyxYUb9a0v+HOKqs2PGO/0CE4UPSj5XpocMUotM
Sm4Yau1/1j1SV+/VktinixCC7hfVyswyFg==
-----END ECDSA PRIVATE KEY-----`
certKey, err := keys.NewCertificateKey(private, public)
require.NoError(t, err)
require.NotNil(t, certKey)
}