-
Notifications
You must be signed in to change notification settings - Fork 2
/
filesystem.go
188 lines (164 loc) · 5.02 KB
/
filesystem.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
package cryptoengines
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
"runtime"
"github.com/lamassuiot/lamassuiot/v2/pkg/config"
"github.com/lamassuiot/lamassuiot/v2/pkg/helpers"
"github.com/lamassuiot/lamassuiot/v2/pkg/models"
"github.com/sirupsen/logrus"
)
var lGo *logrus.Entry
type GoCryptoEngine struct {
config models.CryptoEngineInfo
storageDirectory string
}
func NewGolangPEMEngine(logger *logrus.Entry, conf config.GolangEngineConfig) CryptoEngine {
lGo = logger.WithField("subsystem-provider", "GoSoft")
defaultMeta := map[string]interface{}{
"lamassu.io/cryptoengine.golang.storage-path": conf.StorageDirectory,
}
meta := helpers.MergeMaps[interface{}](&defaultMeta, &conf.Metadata)
return &GoCryptoEngine{
storageDirectory: conf.StorageDirectory,
config: models.CryptoEngineInfo{
Type: models.Golang,
SecurityLevel: models.SL0,
Provider: "Golang",
Name: runtime.Version(),
Metadata: *meta,
SupportedKeyTypes: []models.SupportedKeyTypeInfo{
{
Type: models.KeyType(x509.RSA),
Sizes: []int{
1024,
2048,
3072,
4096,
},
},
{
Type: models.KeyType(x509.ECDSA),
Sizes: []int{
224,
256,
384,
521,
},
},
},
},
}
}
func (p *GoCryptoEngine) GetEngineConfig() models.CryptoEngineInfo {
return p.config
}
func (p *GoCryptoEngine) GetPrivateKeyByID(keyID string) (crypto.Signer, error) {
lGo.Debugf("reading %s Key", keyID)
privatePEM, err := os.ReadFile(p.storageDirectory + "/" + keyID)
if err != nil {
lGo.Errorf("Could not read %s Key: %s", keyID, err)
return nil, err
}
block, _ := pem.Decode(privatePEM)
if block == nil {
lGo.Errorf("could not decode %s PEM Key. Block is nil", keyID)
return nil, fmt.Errorf("failed to parse PEM block containing the key")
}
switch block.Type {
case "RSA PRIVATE KEY":
lGo.Debugf("key %s is RSA in PKCS1 encoding format", keyID)
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
lGo.Errorf("could not parse RSA PKCS1 %s key", keyID)
return nil, err
}
lGo.Debugf("successfully decoded PKCS1 %s key", keyID)
return key, err
case "EC PRIVATE KEY":
lGo.Debugf("Key %s is EC", keyID)
key, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
lGo.Errorf("could not parse EC %s key", keyID)
return nil, err
}
lGo.Debugf("successfully decoded EC %s key", keyID)
return key, err
default:
lGo.Errorf("could not parse key %s in PEM '%s' format", keyID, block.Type)
return nil, fmt.Errorf("unsupported key type")
}
}
func (p *GoCryptoEngine) CreateRSAPrivateKey(keySize int, keyID string) (crypto.Signer, error) {
lGo.Debugf("creating RSA %d key for keyID: %s", keySize, keyID)
key, err := rsa.GenerateKey(rand.Reader, keySize)
if err != nil {
lGo.Errorf("could not create %s RSA key: %s", keyID, err)
return nil, err
}
return p.ImportRSAPrivateKey(key, keyID)
}
func (p *GoCryptoEngine) CreateECDSAPrivateKey(curve elliptic.Curve, keyID string) (crypto.Signer, error) {
lGo.Debugf("creating ECDSA %d key for keyID: %s", curve.Params().BitSize, keyID)
key, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
lGo.Errorf("could not create %s ECDSA key: %s", keyID, err)
return nil, err
}
return p.ImportECDSAPrivateKey(key, keyID)
}
func (p *GoCryptoEngine) DeleteKey(keyID string) error {
return os.Remove(p.storageDirectory + "/" + keyID)
}
func (p *GoCryptoEngine) ImportRSAPrivateKey(key *rsa.PrivateKey, keyID string) (crypto.Signer, error) {
lGo.Debugf("importing RSA %d key for keyID: %s", key.Size(), keyID)
p.checkAndCreateStorageDir()
err := os.WriteFile(p.storageDirectory+"/"+keyID, pem.EncodeToMemory(&pem.Block{
Bytes: x509.MarshalPKCS1PrivateKey(key),
Type: "RSA PRIVATE KEY",
}), 0600)
if err != nil {
lGo.Errorf("could not save %s RSA key: %s", keyID, err)
return nil, err
}
return p.GetPrivateKeyByID(keyID)
}
func (p *GoCryptoEngine) ImportECDSAPrivateKey(key *ecdsa.PrivateKey, keyID string) (crypto.Signer, error) {
lGo.Debugf("importing ECDSA %d key for keyID: %s", key.Params().BitSize, keyID)
p.checkAndCreateStorageDir()
b, err := x509.MarshalECPrivateKey(key)
if err != nil {
return nil, err
}
err = os.WriteFile(p.storageDirectory+"/"+keyID, pem.EncodeToMemory(&pem.Block{
Bytes: b,
Type: "EC PRIVATE KEY",
}), 0600)
if err != nil {
lGo.Errorf("could not save %s ECDSA key: %s", keyID, err)
return nil, err
}
return p.GetPrivateKeyByID(keyID)
}
func (p *GoCryptoEngine) checkAndCreateStorageDir() error {
var err error
if _, err = os.Stat(p.storageDirectory); os.IsNotExist(err) {
lGo.Warnf("storage directory %s does not exist. Will create such directory", p.storageDirectory)
err = os.MkdirAll(p.storageDirectory, 0750)
if err != nil {
lGo.Errorf("something went wrong while creating storage path: %s", err)
}
return err
} else if err != nil {
lGo.Errorf("something went wrong while checking storage: %s", err)
return err
}
return nil
}