-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcertgen_test.go
135 lines (118 loc) · 3.56 KB
/
certgen_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"crypto/ecdsa"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"testing"
"time"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type WinRMCliSuite struct{}
var _ = Suite(&WinRMCliSuite{})
var rsaByteSizes = []int{512, 1024, 2048, 4096}
func (s *WinRMCliSuite) TestNewCertRSASize(c *C) {
for _, size := range rsaByteSizes {
config := CertConfig{
Subject: pkix.Name{CommonName: "winrm client cert"},
ValidFrom: time.Now(),
ValidFor: 365 * 24 * time.Hour,
SizeT: size,
Method: RSA,
}
certPem, privPem, err := NewCert(config)
c.Assert(err, IsNil)
c.Assert(certPem, Not(IsNil))
c.Assert(privPem, Not(IsNil))
caCert, caKey, err := parseCertAndKeyRSA(certPem, privPem)
c.Assert(err, IsNil)
c.Check(caCert.Subject.CommonName, Equals, "winrm client cert")
c.Check(caKey, FitsTypeOf, (*rsa.PrivateKey)(nil))
c.Check(caCert.Version, Equals, 3)
value, err := getUPNExtensionValue(config.Subject)
c.Assert(err, IsNil)
c.Assert(value, Not(IsNil))
expected := []pkix.Extension{
{
Id: subjAltName,
Value: value,
Critical: false,
},
}
c.Assert(caCert.Extensions[1], DeepEquals, expected[0])
c.Assert(caCert.PublicKeyAlgorithm, Equals, x509.RSA)
c.Assert(caCert.ExtKeyUsage[0], Equals, x509.ExtKeyUsageClientAuth)
}
}
var formats = []int{P224, P256, P384, P521}
func (s *WinRMCliSuite) TestNewCertECDSATypes(c *C) {
for _, f := range formats {
config := CertConfig{
Subject: pkix.Name{CommonName: "winrm client cert"},
ValidFrom: time.Now(),
ValidFor: 365 * 24 * time.Hour,
Method: ECDSA,
SizeT: f,
}
certPem, privPem, err := NewCert(config)
c.Assert(err, IsNil)
caCert, caKey, err := parseCertAndKeyECDSA(certPem, privPem)
c.Assert(err, IsNil)
c.Assert(certPem, Not(IsNil))
c.Assert(privPem, Not(IsNil))
c.Check(caCert.Subject.CommonName, Equals, "winrm client cert")
c.Check(caKey, FitsTypeOf, (*ecdsa.PrivateKey)(nil))
c.Check(caCert.Version, Equals, 3)
value, err := getUPNExtensionValue(config.Subject)
c.Assert(err, IsNil)
c.Assert(value, Not(IsNil))
expected := []pkix.Extension{
{
Id: subjAltName,
Value: value,
Critical: false,
},
}
c.Assert(caCert.Extensions[1], DeepEquals, expected[0])
c.Assert(caCert.PublicKeyAlgorithm, Equals, x509.ECDSA)
c.Assert(caCert.ExtKeyUsage[0], Equals, x509.ExtKeyUsageClientAuth)
}
}
// ParseCertAndKey parses the given PEM-formatted X509 certificate
// and RSA private key.
func parseCertAndKeyRSA(certPEM, keyPEM string) (*x509.Certificate, *rsa.PrivateKey, error) {
tlsCert, err := tls.X509KeyPair([]byte(certPEM), []byte(keyPEM))
if err != nil {
return nil, nil, err
}
cert, err := x509.ParseCertificate(tlsCert.Certificate[0])
if err != nil {
return nil, nil, err
}
key, ok := tlsCert.PrivateKey.(*rsa.PrivateKey)
if !ok {
return nil, nil, fmt.Errorf("private key with unexpected type %T", key)
}
return cert, key, nil
}
// ParseCertAndKey parses the given PEM-formatted X509 certificate
// and ECSA private key.
func parseCertAndKeyECDSA(certPEM, keyPEM string) (*x509.Certificate, *ecdsa.PrivateKey, error) {
tlsCert, err := tls.X509KeyPair([]byte(certPEM), []byte(keyPEM))
if err != nil {
return nil, nil, err
}
cert, err := x509.ParseCertificate(tlsCert.Certificate[0])
if err != nil {
return nil, nil, err
}
key, ok := tlsCert.PrivateKey.(*ecdsa.PrivateKey)
if !ok {
return nil, nil, fmt.Errorf("private key with unexpected type %T", key)
}
return cert, key, nil
}