forked from gokalkan/gokalkan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_cert.go
106 lines (92 loc) · 2.71 KB
/
options_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
package gokalkan
import (
"context"
"crypto/x509"
"fmt"
"io"
"net/http"
"github.com/olegmlsn/gokalkan/ckalkan"
)
func WithCert(cert *x509.Certificate, typ ckalkan.CertType) Option {
return func(o *Options) {
o.Certs = append(o.Certs, OptionsCert{cert, typ})
}
}
func WithCerts(c []OptionsCert) Option {
return func(o *Options) {
o.Certs = c
}
}
func WithRemoteProdCerts(ctx context.Context) Option {
type pair struct {
url string
certType ckalkan.CertType
}
pairs := []pair{
{url: "https://pki.gov.kz/cert/root_gost.crt", certType: ckalkan.CertTypeCA},
{url: "https://pki.gov.kz/cert/root_rsa.crt", certType: ckalkan.CertTypeCA},
{url: "https://pki.gov.kz/cert/root_gost2015_2022.cer", certType: ckalkan.CertTypeCA},
{url: "https://pki.gov.kz/cert/nca_gost.crt", certType: ckalkan.CertTypeIntermediate},
{url: "https://pki.gov.kz/cert/nca_rsa.crt", certType: ckalkan.CertTypeIntermediate},
{url: "https://pki.gov.kz/cert/nca_gost2015.cer", certType: ckalkan.CertTypeIntermediate},
}
return func(o *Options) {
for _, p := range pairs {
bytes, err := download(ctx, p.url)
if err != nil {
panic(err)
}
cert, err := x509.ParseCertificate(bytes)
if err != nil {
panic(err)
}
o.Certs = append(o.Certs, OptionsCert{Cert: cert, Type: p.certType})
}
}
}
func WithRemoteTestCerts(ctx context.Context) Option {
type pair struct {
url string
certType ckalkan.CertType
}
pairs := []pair{
{url: "http://test.pki.gov.kz/cert/root_gost_test.cer", certType: ckalkan.CertTypeCA},
{url: "http://test.pki.gov.kz/cert/root_rsa_test.cer", certType: ckalkan.CertTypeCA},
{url: "http://test.pki.gov.kz/cert/nca_gost_test.cer", certType: ckalkan.CertTypeIntermediate},
{url: "http://test.pki.gov.kz/cert/nca_rsa_test.cer", certType: ckalkan.CertTypeIntermediate},
}
return func(o *Options) {
for _, p := range pairs {
bytes, err := download(ctx, p.url)
if err != nil {
panic(err)
}
cert, err := x509.ParseCertificate(bytes)
if err != nil {
panic(err)
}
o.Certs = append(o.Certs, OptionsCert{Cert: cert, Type: p.certType})
}
}
}
// LoadCertFromURL загружает сертификат по url в хранилище.
func download(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, http.NoBody)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
io.Copy(io.Discard, resp.Body) //nolint:errcheck
return nil, fmt.Errorf("%w: bad status: %d", ErrHTTPCli, resp.StatusCode)
}
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return bytes, nil
}