-
Notifications
You must be signed in to change notification settings - Fork 4
/
keystore.go
161 lines (139 loc) · 4.24 KB
/
keystore.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
package validator
import (
"bufio"
"bytes"
"crypto/rsa"
"crypto/x509"
"encoding/asn1"
"encoding/pem"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/trimble-oss/tierceron/pkg/core"
eUtils "github.com/trimble-oss/tierceron/pkg/utils"
"github.com/pavlo-v-chernykh/keystore-go/v4"
"github.com/youmark/pkcs8"
pkcs "golang.org/x/crypto/pkcs12"
"golang.org/x/crypto/ssh"
)
// Copied from pkcs12.go... why can't they just make these public. Gr...
// PEM block types
const (
certificateType = "CERTIFICATE"
privateKeyType = "PRIVATE KEY"
rsaPrivateKeyType = "RSA PRIVATE KEY"
)
func StoreKeystore(driverConfig *eUtils.DriverConfig, trustStorePassword string) ([]byte, error) {
buffer := &bytes.Buffer{}
keystoreWriter := bufio.NewWriter(buffer)
if driverConfig.KeyStore == nil {
return nil, errors.New("cert bundle not properly named")
}
storeErr := driverConfig.KeyStore.Store(keystoreWriter, []byte(trustStorePassword))
if storeErr != nil {
return nil, storeErr
}
keystoreWriter.Flush()
return buffer.Bytes(), nil
}
func AddToKeystore(driverConfig *eUtils.DriverConfig, alias string, password []byte, certBundleJks string, data []byte) error {
// TODO: Add support for this format? golang.org/x/crypto/pkcs12
if !strings.HasSuffix(driverConfig.WantKeystore, ".jks") && strings.HasSuffix(certBundleJks, ".jks") {
driverConfig.WantKeystore = certBundleJks
}
if driverConfig.KeyStore == nil {
fmt.Println("Making new keystore.")
ks := keystore.New()
driverConfig.KeyStore = &ks
}
block, _ := pem.Decode(data)
if block == nil {
key, cert, err := pkcs.Decode(data, string(password)) // Note the order of the return values.
if err != nil {
return err
}
pkcs8Key, err := pkcs8.ConvertPrivateKeyToPKCS8(key, password)
if err != nil {
return err
}
driverConfig.KeyStore.SetPrivateKeyEntry(alias, keystore.PrivateKeyEntry{
CreationTime: time.Now(),
PrivateKey: pkcs8Key,
CertificateChain: []keystore.Certificate{
{
Type: "X509",
Content: cert.Raw,
},
},
}, password)
} else {
if block.Type == certificateType {
aliasCommon := strings.Replace(alias, "cert.pem", "", 1)
driverConfig.KeyStore.SetTrustedCertificateEntry(aliasCommon, keystore.TrustedCertificateEntry{
CreationTime: time.Now(),
Certificate: keystore.Certificate{
Type: "X509",
Content: block.Bytes,
},
})
return nil
}
privateKeyBytes, err := ssh.ParseRawPrivateKey(data)
if err == nil {
privateKeyBytes, err := pkcs8.MarshalPrivateKey(privateKeyBytes, []byte{}, nil)
if err != nil {
return err
}
aliasCommon := strings.Replace(alias, "key.pem", "", 1)
driverConfig.KeyStore.SetPrivateKeyEntry(aliasCommon, keystore.PrivateKeyEntry{
CreationTime: time.Now(),
PrivateKey: privateKeyBytes,
}, password)
} else {
return err
}
}
return nil
}
// ValidateKeyStore validates the sendgrid API key.
func ValidateKeyStore(config *core.CoreConfig, filename string, pass string) (bool, error) {
file, err := os.ReadFile(filename)
if err != nil {
return false, err
}
pemBlocks, errToPEM := pkcs.ToPEM(file, pass)
if errToPEM != nil {
return false, errors.New("failed to parse: " + err.Error())
}
isValid := false
for _, pemBlock := range pemBlocks {
// PEM constancts defined but not exposed in
// certificateType = "CERTIFICATE"
// privateKeyType = "PRIVATE KEY"
switch (*pemBlock).Type {
case certificateType:
var cert x509.Certificate
_, errUnmarshal := asn1.Unmarshal((*pemBlock).Bytes, &cert)
if errUnmarshal != nil {
return false, errors.New("failed to parse: " + err.Error())
}
isCertValid, err := VerifyCertificate(&cert, "")
if err != nil {
eUtils.LogInfo(config, "Certificate validation failure.")
}
isValid = isCertValid
case privateKeyType:
var key rsa.PrivateKey
_, errUnmarshal := asn1.Unmarshal((*pemBlock).Bytes, &key)
if errUnmarshal != nil {
return false, errors.New("failed to parse: " + err.Error())
}
if err := key.Validate(); err != nil {
eUtils.LogInfo(config, "key validation didn't work")
}
}
}
return isValid, err
}