@@ -12,11 +12,13 @@ import (
12
12
"crypto/elliptic"
13
13
"crypto/rand"
14
14
"crypto/rsa"
15
+ "crypto/sha1"
15
16
"crypto/sha256"
16
17
"crypto/x509"
17
18
"encoding/base64"
18
19
"encoding/pem"
19
20
"fmt"
21
+ "hash"
20
22
"io"
21
23
"regexp"
22
24
"strings"
@@ -186,24 +188,26 @@ type SanitizedConfig struct {
186
188
PrivateKey string `json:"privatekey,omitempty" yaml:"privatekey,omitempty"`
187
189
IssuerPrivKey string `json:"issuerprivkey,omitempty" yaml:"issuerprivkey,omitempty"`
188
190
189
- // TODO: To fully replace the config-sanitizer tool, we should also include
190
- // fingerprints and expiration times of the certificate (if present).
191
+ // If a certificate is present, add fingerprints and expiration dates.
192
+ CertFingerprintSha1 string `json:"cert_sha1,omitempty" yaml:"cert_sha1,omitempty"`
193
+ CertFingerprintSha256 string `json:"cert_sha256,omitempty" yaml:"cert_sha256,omitempty"`
194
+ CertDateStart string `json:"cert_date_start,omitempty" yaml:"cert_date_start,omitempty"`
195
+ CertDateEnd string `json:"cert_date_end,omitempty" yaml:"cert_date_end,omitempty"`
191
196
}
192
197
193
- func hashSecretString (secret string ) string {
198
+ func hashFingerprint (secret [] byte , algorithm hash. Hash ) string {
194
199
// Empty strings should stay empty
195
- if secret == "" {
200
+ if len ( secret ) == 0 {
196
201
return ""
197
202
}
198
203
199
- h := sha256 .New ()
200
- h .Write ([]byte (secret ))
201
- return fmt .Sprintf ("%x" , h .Sum (nil ))
204
+ algorithm .Write (secret )
205
+ return fmt .Sprintf ("%x" , algorithm .Sum (nil ))
202
206
}
203
207
204
208
// Sanitize configuration to make it suitable for public export
205
209
func (cfg * Configuration ) Sanitize () * SanitizedConfig {
206
- return & SanitizedConfig {
210
+ result := & SanitizedConfig {
207
211
// Copy public values verbatim.
208
212
ID : cfg .ID ,
209
213
Type : cfg .Type ,
@@ -222,9 +226,29 @@ func (cfg *Configuration) Sanitize() *SanitizedConfig {
222
226
SaltLength : cfg .SaltLength ,
223
227
224
228
// Hash private keys, if present.
225
- PrivateKey : hashSecretString (cfg .PrivateKey ),
226
- IssuerPrivKey : hashSecretString (cfg .IssuerPrivKey ),
229
+ PrivateKey : hashFingerprint ([]byte (cfg .PrivateKey ), sha256 .New ()),
230
+ IssuerPrivKey : hashFingerprint ([]byte (cfg .IssuerPrivKey ), sha256 .New ()),
231
+ }
232
+
233
+ // If a certificate exists - parse it.
234
+ certDER , _ := pem .Decode ([]byte (cfg .Certificate ))
235
+ if certDER != nil && certDER .Type == "CERTIFICATE" {
236
+ certX509 , err := x509 .ParseCertificate (certDER .Bytes )
237
+ if err == nil {
238
+ result .CertFingerprintSha1 = hashFingerprint (certDER .Bytes , sha1 .New ())
239
+ result .CertFingerprintSha256 = hashFingerprint (certDER .Bytes , sha256 .New ())
240
+ start , err := certX509 .NotBefore .MarshalText ()
241
+ if err == nil {
242
+ result .CertDateStart = string (start )
243
+ }
244
+ end , err := certX509 .NotAfter .MarshalText ()
245
+ if err == nil {
246
+ result .CertDateEnd = string (end )
247
+ }
248
+ }
227
249
}
250
+
251
+ return result
228
252
}
229
253
230
254
// InitHSM indicates that an HSM has been initialized
0 commit comments