-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificates.go
292 lines (272 loc) · 10.1 KB
/
certificates.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package connect
import "encoding/json"
// ValidType - Certificate Time properties info
type ValidType string
const (
Valid ValidType = "Valid"
NotValidYet ValidType = "NotValidYet"
ExpireSoon ValidType = "ExpireSoon"
Expired ValidType = "Expired"
)
// ValidPeriod - Certificate Time properties
type ValidPeriod struct {
ValidFromDate Date `json:"validFromDate"` // @see SharedStructures.idl shared in lib
ValidFromTime Time `json:"validFromTime"` // @see SharedStructures.idl shared in lib
ValidToDate Date `json:"validToDate"` // @see SharedStructures.idl shared in lib
ValidToTime Time `json:"validToTime"` // @see SharedStructures.idl shared in lib
ValidType ValidType `json:"validType"`
}
type CertificateType string
const (
ActiveCertificate CertificateType = "ActiveCertificate"
InactiveCertificate CertificateType = "InactiveCertificate"
CertificateRequest CertificateType = "CertificateRequest"
Authority CertificateType = "Authority"
LocalAuthority CertificateType = "LocalAuthority"
BuiltInAuthority CertificateType = "BuiltInAuthority"
ServerCertificate CertificateType = "ServerCertificate"
)
// Certificate properties
// issuer & subject valid names:
// hostname; // max 127 bytes
// organizationName; // max 127 bytes
// organizationalUnitName; // max 127 bytes
// city; // max 127 bytes
// state; // max 127 bytes
// country; // ISO 3166 code
// Certificate - emailAddress; // max 255 bytes
type Certificate struct {
Id KId `json:"id"`
Status StoreStatus `json:"status"`
Name string `json:"name"`
Issuer NamedValueList `json:"issuer"`
Subject NamedValueList `json:"subject"`
SubjectAlternativeNameList NamedMultiValueList `json:"subjectAlternativeNameList"`
Fingerprint string `json:"fingerprint"` // 128-bit MD5, i.e. 16 hexa values separated by colons
FingerprintSha1 string `json:"fingerprintSha1"` // 160-bit SHA1, i.e. 20 hexa values separated by colons
FingerprintSha256 string `json:"fingerprintSha256"` // 512-bit SHA256, i.e. 64 hexa values separated by colons
ValidPeriod ValidPeriod `json:"validPeriod"`
Valid bool `json:"valid"` // exists and valid content
Type CertificateType `json:"type"`
IsUntrusted bool `json:"isUntrusted"`
VerificationMessage string `json:"verificationMessage"`
ChainInfo StringList `json:"chainInfo"`
IsSelfSigned bool `json:"isSelfSigned"`
}
type CertificateList []Certificate
// Manager of Certificates
// CertificatesGet - Obtain a list of certificates
// query - conditions and limits. Included from weblib.
// Return
// certificates - current list of certificates
// totalItems - count of all services on server (before the start/limit applied)
func (s *ServerConnection) CertificatesGet(query SearchQuery) (CertificateList, int, error) {
query = addMissedParametersToSearchQuery(query)
params := struct {
Query SearchQuery `json:"query"`
}{query}
data, err := s.CallRaw("Certificates.get", params)
if err != nil {
return nil, 0, err
}
certificates := struct {
Result struct {
Certificates CertificateList `json:"certificates"`
TotalItems int `json:"totalItems"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &certificates)
return certificates.Result.Certificates, certificates.Result.TotalItems, err
}
// CertificatesSetName - Renames certificate
// id - ID of certificate to rename
// name - new name of the certificate
func (s *ServerConnection) CertificatesSetName(id KId, name string) error {
params := struct {
Id KId `json:"id"`
Name string `json:"name"`
}{id, name}
_, err := s.CallRaw("Certificates.setName", params)
return err
}
// CertificatesRemove - Remove list of certificate records
// ids - list of identifiers of deleted user templates
// Return
// errors - error message list
func (s *ServerConnection) CertificatesRemove(ids KIdList) (ErrorList, error) {
params := struct {
Ids KIdList `json:"ids"`
}{ids}
data, err := s.CallRaw("Certificates.remove", params)
if err != nil {
return nil, err
}
errors := struct {
Result struct {
Errors ErrorList `json:"errors"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &errors)
return errors.Result.Errors, err
}
// CertificatesGenerate - Generate certificate.
// subject - properties specified by user
// name - name of the new certificate
// certificateType - type of certificate to be generated, valid input is one of: InactiveCertificate/CertificateRequest/LocalAuthority
// period - time properties specified by user, not relevant for CertificateRequest
// Return
// id - ID of generated certificate
func (s *ServerConnection) CertificatesGenerate(subject NamedValueList, name string, certificateType CertificateType, period ValidPeriod) (KId, error) {
params := struct {
Subject NamedValueList `json:"subject"`
Name string `json:"name"`
Type CertificateType `json:"type"`
Period ValidPeriod `json:"period"`
}{subject, name, certificateType, period}
data, err := s.CallRaw("Certificates.generate", params)
if err != nil {
return "", err
}
id := struct {
Result struct {
Id KId `json:"id"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &id)
return id.Result.Id, err
}
// CertificatesGetCountryList - Get a list of countries.
// Return
// countries - list of countries (name and ISO 3166 code)
func (s *ServerConnection) CertificatesGetCountryList() (NamedValueList, error) {
data, err := s.CallRaw("Certificates.getCountryList", nil)
if err != nil {
return nil, err
}
countries := struct {
Result struct {
Countries NamedValueList `json:"countries"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &countries)
return countries.Result.Countries, err
}
// CertificatesImportCertificate - Import certificate in PEM format
// keyId - ID assigned to imported private key, @see importPrivateKey
// fileId - id of uploaded file
// name - name of the new certificate
// certificateType - type of certificate to be imported, valid input is one of: InactiveCertificate/Authority/LocalAuthority
// Return
// id - ID of generated certificate
func (s *ServerConnection) CertificatesImportCertificate(keyId KId, fileId string, name string, certificateType CertificateType) (KId, error) {
params := struct {
KeyId KId `json:"keyId"`
FileId string `json:"fileId"`
Name string `json:"name"`
Type CertificateType `json:"type"`
}{keyId, fileId, name, certificateType}
data, err := s.CallRaw("Certificates.importCertificate", params)
if err != nil {
return "", err
}
id := struct {
Result struct {
Id KId `json:"id"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &id)
return id.Result.Id, err
}
// CertificatesImportPrivateKey - Import private key. It generates ID, so it can be linked to Certificate content imported later, @see importCertificate
// fileId - id of uploaded file
// Return
// keyId - generated ID for new key
// needPassword - true if private key is encrypted with password
func (s *ServerConnection) CertificatesImportPrivateKey(fileId string) (KId, bool, error) {
params := struct {
FileId string `json:"fileId"`
}{fileId}
data, err := s.CallRaw("Certificates.importPrivateKey", params)
if err != nil {
return "", false, err
}
keyId := struct {
Result struct {
KeyId KId `json:"keyId"`
NeedPassword bool `json:"needPassword"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &keyId)
return keyId.Result.KeyId, keyId.Result.NeedPassword, err
}
// CertificatesUnlockPrivateKey - Try to parse imported private key. Need to be called, when @importPrivateKey returns needPassword == true.
// keyId - ID assigned to imported private key, @see importPrivateKey
// password - certificate password
func (s *ServerConnection) CertificatesUnlockPrivateKey(keyId KId, password string) error {
params := struct {
KeyId KId `json:"keyId"`
Password string `json:"password"`
}{keyId, password}
_, err := s.CallRaw("Certificates.unlockPrivateKey", params)
return err
}
// CertificatesExportCertificate - Export of certificate or certificate request
// id - ID of the certificate or certificate request
// Return
// fileDownload - description of the output file
func (s *ServerConnection) CertificatesExportCertificate(id KId) (*Download, error) {
params := struct {
Id KId `json:"id"`
}{id}
data, err := s.CallRaw("Certificates.exportCertificate", params)
if err != nil {
return nil, err
}
fileDownload := struct {
Result struct {
FileDownload Download `json:"fileDownload"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &fileDownload)
return &fileDownload.Result.FileDownload, err
}
// CertificatesExportPrivateKey - Export of certificate or request privatekey
// id - ID of the certificate or certificate request
// Return
// fileDownload - description of the output file
func (s *ServerConnection) CertificatesExportPrivateKey(id KId) (*Download, error) {
params := struct {
Id KId `json:"id"`
}{id}
data, err := s.CallRaw("Certificates.exportPrivateKey", params)
if err != nil {
return nil, err
}
fileDownload := struct {
Result struct {
FileDownload Download `json:"fileDownload"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &fileDownload)
return &fileDownload.Result.FileDownload, err
}
// CertificatesToSource - Obtain source (plain-text representation) of the certificate
// id - global identifier
// Return
// source - certificate in plain text
func (s *ServerConnection) CertificatesToSource(id KId) (string, error) {
params := struct {
Id KId `json:"id"`
}{id}
data, err := s.CallRaw("Certificates.toSource", params)
if err != nil {
return "", err
}
source := struct {
Result struct {
Source string `json:"source"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &source)
return source.Result.Source, err
}