-
Notifications
You must be signed in to change notification settings - Fork 210
/
cert.go
213 lines (180 loc) · 4.62 KB
/
cert.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package testutil
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"net"
"testing"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
types "github.com/akash-network/akash-api/go/node/cert/v1beta3"
clientmocks "github.com/akash-network/akash-api/go/node/client/v1beta2/mocks"
certutils "github.com/akash-network/node/x/cert/utils"
)
type TestCertificate struct {
Cert []tls.Certificate
Serial big.Int
PEM struct {
Cert []byte
Priv []byte
Pub []byte
}
}
type certificateOption struct {
domains []string
nbf time.Time
naf time.Time
qclient *clientmocks.QueryClient
}
type CertificateOption func(*certificateOption)
func CertificateOptionDomains(domains []string) CertificateOption {
return func(opt *certificateOption) {
opt.domains = domains
}
}
func CertificateOptionNotBefore(tm time.Time) CertificateOption {
return func(opt *certificateOption) {
opt.nbf = tm
}
}
func CertificateOptionNotAfter(tm time.Time) CertificateOption {
return func(opt *certificateOption) {
opt.naf = tm
}
}
func CertificateOptionMocks(val *clientmocks.QueryClient) CertificateOption {
return func(opt *certificateOption) {
opt.qclient = val
}
}
func Certificate(t testing.TB, addr sdk.Address, opts ...CertificateOption) TestCertificate {
t.Helper()
opt := &certificateOption{}
for _, o := range opts {
o(opt)
}
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
if opt.nbf.IsZero() {
opt.nbf = time.Now()
}
if opt.naf.IsZero() {
opt.naf = opt.nbf.Add(time.Hour * 24 * 365)
}
extKeyUsage := []x509.ExtKeyUsage{
x509.ExtKeyUsageClientAuth,
}
if len(opt.domains) != 0 {
extKeyUsage = append(extKeyUsage, x509.ExtKeyUsageServerAuth)
}
template := x509.Certificate{
SerialNumber: new(big.Int).SetInt64(time.Now().UTC().UnixNano()),
Subject: pkix.Name{
CommonName: addr.String(),
ExtraNames: []pkix.AttributeTypeAndValue{
{
Type: certutils.AuthVersionOID,
Value: "v0.0.1",
},
},
},
Issuer: pkix.Name{
CommonName: addr.String(),
},
NotBefore: opt.nbf,
NotAfter: opt.naf,
KeyUsage: x509.KeyUsageDataEncipherment | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: extKeyUsage,
BasicConstraintsValid: true,
}
var ips []net.IP
for i := len(opt.domains) - 1; i >= 0; i-- {
if ip := net.ParseIP(opt.domains[i]); ip != nil {
ips = append(ips, ip)
opt.domains = append(opt.domains[:i], opt.domains[i+1:]...)
}
}
if len(opt.domains) != 0 || len(ips) != 0 {
template.PermittedDNSDomainsCritical = true
template.PermittedDNSDomains = opt.domains
template.DNSNames = opt.domains
template.IPAddresses = ips
}
var certDer []byte
if certDer, err = x509.CreateCertificate(rand.Reader, &template, &template, priv.Public(), priv); err != nil {
t.Fatal(err)
}
var keyDer []byte
if keyDer, err = x509.MarshalPKCS8PrivateKey(priv); err != nil {
t.Fatal(err)
}
var pubKeyDer []byte
if pubKeyDer, err = x509.MarshalPKIXPublicKey(priv.Public()); err != nil {
t.Fatal(err)
}
res := TestCertificate{
Serial: *template.SerialNumber,
PEM: struct {
Cert []byte
Priv []byte
Pub []byte
}{
Cert: pem.EncodeToMemory(&pem.Block{
Type: types.PemBlkTypeCertificate,
Bytes: certDer,
}),
Priv: pem.EncodeToMemory(&pem.Block{
Type: types.PemBlkTypeECPrivateKey,
Bytes: keyDer,
}),
Pub: pem.EncodeToMemory(&pem.Block{
Type: types.PemBlkTypeECPublicKey,
Bytes: pubKeyDer,
}),
},
}
cert, err := tls.X509KeyPair(res.PEM.Cert, res.PEM.Priv)
if err != nil {
t.Fatal(err)
}
res.Cert = append(res.Cert, cert)
if opt.qclient != nil {
opt.qclient.On("Certificates",
context.Background(),
&types.QueryCertificatesRequest{
Filter: types.CertificateFilter{
Owner: addr.String(),
Serial: res.Serial.String(),
State: "valid",
},
}).
Return(&types.QueryCertificatesResponse{
Certificates: types.CertificatesResponse{
types.CertificateResponse{
Certificate: types.Certificate{
State: types.CertificateValid,
Cert: res.PEM.Cert,
Pubkey: res.PEM.Pub,
},
Serial: res.Serial.String(),
},
},
}, nil)
}
return res
}
func CertificateRequireEqualResponse(t *testing.T, cert TestCertificate, resp types.CertificateResponse, state types.Certificate_State) {
t.Helper()
require.Equal(t, state, resp.Certificate.State)
require.Equal(t, cert.PEM.Cert, resp.Certificate.Cert)
require.Equal(t, cert.PEM.Pub, resp.Certificate.Pubkey)
}