forked from gokalkan/gokalkan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.go
More file actions
160 lines (121 loc) · 3.6 KB
/
wrapper.go
File metadata and controls
160 lines (121 loc) · 3.6 KB
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
package gokalkan
import (
"context"
"errors"
"fmt"
"net/http"
"sync"
"time"
)
// Kalkan - это обертка над методами KalkanCrypt (KC)
type Kalkan interface {
LoadCertsGOST(ctx context.Context) (err error)
LoadCertsRSA(ctx context.Context) (err error)
LoadKeyStore(path, password string) (err error)
LoadKeyStoreFromBytes(key []byte, password string) (err error)
SignXML(dataXML string) (signedXML string, err error)
VerifyXML(signedXML string) (result string, err error)
SignCMSB64(dataB64 string) (signedCMSB64 string, err error)
VerifyCMSB64(signedCMSB64 string) (result *VerifiedData, err error)
SignWSSE(dataXML, id string) (signedXML string, err error)
VerifyCert(cert string, t KCValidateType, path ...string) (result string, err error)
GetCertInfo(certPEM string) (result *X509RawInfo, err error)
GetCertKeyUsage(certPEM string) (result KeyUsage, err error)
GetCertFromCMSB64(signedCMSB64 string) (certPEM string, err error)
GetCertFromKeyStore() (certPEM string, err error)
Close() error
}
var _ Kalkan = (*Client)(nil)
var (
ErrLoadKey = errors.New("load key error")
ErrInit = errors.New("unable to refer to KC_GetFunctionList")
ErrHTTPCli = errors.New("http cli error")
)
type Client struct {
log Logger
kc KC
o Options
c *http.Client
mu sync.Mutex
}
// NewClient возвращает клиента для работы с KC.
func NewClient(opts ...Option) (*Client, error) {
o := Options{log: defaultLogger}
o.setDefaults()
for _, op := range opts {
op(&o)
}
o.log.Debug("---------kalkan-config-------------")
o.log.Debug("Load CA certs on init: ", o.LoadCACertsOnInit)
o.log.Debug("Load CRL cache on init: ", o.LoadCRLCacheOnInit)
o.log.Debug("TSP url: ", o.TSP)
o.log.Debug("OCSP url: ", o.OCSP)
o.log.Debug("CRL cache duration: ", o.CRLCacheDuration)
o.log.Debug("CRL GOST url: ", o.CRLGOST)
o.log.Debug("CRL RSA url: ", o.CRLRSA)
o.log.Debug("CA GOST url: ", o.CACertGOST)
o.log.Debug("CA RSA url: ", o.CACertRSA)
kc, err := NewKCClient()
if err != nil {
return nil, err
}
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 100
t.MaxIdleConnsPerHost = 100
t.DisableKeepAlives = true
if o.Proxy != nil {
o.log.Debug("Proxy: ", o.Proxy.Hostname())
t.Proxy = http.ProxyURL(o.Proxy)
}
c := &http.Client{
Transport: t,
Timeout: time.Second * 5,
}
cli := &Client{
log: o.log,
kc: kc,
o: o,
c: c,
}
cli.log.Debug("kc init...")
err = cli.kc.KCInit()
if err != nil {
cli.log.Error("kc init error: ", err)
return nil, fmt.Errorf("%w: %s", ErrInit, err)
}
cli.log.Debug("kc init OK")
cli.log.Debug("setting TSP: ", cli.o.TSP)
cli.kc.KCTSASetURL(cli.o.TSP)
if cli.o.Proxy != nil {
cli.log.Debug("setting proxy: ", cli.o.Proxy.Hostname())
er := cli.kc.KCSetProxy(KCFlagProxyOn, cli.o.Proxy)
if er != nil {
cli.log.Error("setting proxy error: ", er)
}
cli.log.Debug("setting proxy OK")
}
if cli.o.LoadCACertsOnInit {
var er error
cli.log.Debug("loading CA certs RSA...")
er = cli.LoadCertsRSA(context.Background())
if er != nil {
cli.log.Error("load CA certs RSA error: ", er)
}
cli.log.Debug("load CA certs RSA OK")
cli.log.Debug("loading CA certs GOST...")
er = cli.LoadCertsGOST(context.Background())
if er != nil {
cli.log.Error("load CA certs GOST error: ", er)
}
cli.log.Debug("load CA certs GOST OK")
}
if cli.o.LoadCRLCacheOnInit {
cli.log.Debug("loading CRL cache...")
er := cli.LoadCRLCache(context.Background())
if er != nil {
cli.log.Error("load CRL cache error: ", er)
}
cli.log.Debug("load CRL cache OK")
}
return cli, nil
}