/
loader.go
217 lines (194 loc) · 5.54 KB
/
loader.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
/**
* Copyright 2019 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package cipher
import (
"crypto"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"github.com/Comcast/webpa-common/logging"
"github.com/go-kit/kit/log"
"github.com/goph/emperror"
"github.com/pkg/errors"
"io/ioutil"
"strings"
)
var (
hashFunctions = map[string]crypto.Hash{
"BLAKE2B512": crypto.BLAKE2b_512,
"SHA1": crypto.SHA1,
"SHA512": crypto.SHA512,
"MD5": crypto.MD5,
}
)
type Options struct {
// Logger is the go-kit Logger to use for server startup and error logging. If not
// supplied, logging.DefaultLogger() is used instead.
Logger log.Logger `json:"-"`
Algorithm map[string]interface{} `json:"algorithm,omitempty"`
SenderPrivateKey string `json:"senderPrivateKey,omitempty"`
SenderPublicKey string `json:"senderPublicKey,omitempty"`
RecipientPrivateKey string `json:"recipientPrivateKey,omitempty"`
RecipientPublicKey string `json:"recipientPublicKey,omitempty"`
}
type KeyLoader interface {
GetBytes() ([]byte, error)
}
type EncryptLoader interface {
LoadEncrypt() (Encrypt, error)
}
type DecryptLoader interface {
LoadDecrypt() (Decrypt, error)
}
type FileLoader struct {
Path string
}
func (f *FileLoader) GetBytes() ([]byte, error) {
return ioutil.ReadFile(f.Path)
}
type BytesLoader struct {
Data []byte
}
func (b *BytesLoader) GetBytes() ([]byte, error) {
return b.Data, nil
}
func GetPrivateKey(loader KeyLoader) (*rsa.PrivateKey, error) {
data, err := loader.GetBytes()
if err != nil {
return nil, err
}
privPem, _ := pem.Decode(data)
if privPem.Type != "RSA PRIVATE KEY" {
return nil, errors.New("incorrect pem type: " + privPem.Type)
}
var parsedKey interface{}
if parsedKey, err = x509.ParsePKCS1PrivateKey(privPem.Bytes); err != nil {
return nil, err
}
if privateKey, ok := parsedKey.(*rsa.PrivateKey); !ok {
return nil, errors.New("failed convert parsed key to private key")
} else {
return privateKey, nil
}
}
func GetPublicKey(loader KeyLoader) (*rsa.PublicKey, error) {
data, err := loader.GetBytes()
if err != nil {
return nil, err
}
publicPem, _ := pem.Decode(data)
if publicPem.Type != "RSA PUBLIC KEY" {
return nil, errors.New("incorrect pem type: " + publicPem.Type)
}
var parsedKey interface{}
if parsedKey, err = x509.ParsePKCS1PublicKey(publicPem.Bytes); err != nil {
return nil, emperror.Wrap(err, "failed to load public key x509.ParsePKCS1PublicKey")
}
if publicKey, ok := parsedKey.(*rsa.PublicKey); !ok {
return nil, errors.New("failed convert parsed key to public key")
} else {
return publicKey, nil
}
}
func (o *Options) LoadEncrypt() (Encrypt, error) {
if o.Logger == nil {
o.Logger = logging.DefaultLogger()
}
logging.Info(o.Logger).Log(logging.MessageKey(), "new encrypter", "options", o)
if algorithm, ok := o.Algorithm["type"].(string); ok {
switch strings.ToLower(algorithm) {
case "noop":
return DefaultCipherEncrypter(), nil
case "box":
if o.SenderPrivateKey == "" || o.RecipientPublicKey == "" {
break
}
boxLoader := BoxLoader{
PrivateKey: &FileLoader{
Path: o.SenderPrivateKey,
},
PublicKey: &FileLoader{
Path: o.RecipientPublicKey,
},
}
return boxLoader.LoadEncrypt()
case "basic":
if hashName, ok := o.Algorithm["hash"].(string); ok {
if o.SenderPrivateKey == "" || o.RecipientPublicKey == "" {
break
}
basicLoader := BasicLoader{
Hash: &BasicHashLoader{hashName},
PrivateKey: &FileLoader{
Path: o.SenderPrivateKey,
},
PublicKey: &FileLoader{
Path: o.RecipientPublicKey,
},
}
return basicLoader.LoadEncrypt()
} else {
return nil, errors.New("failed to find hash name for basic algorithm cipher type")
}
}
}
return DefaultCipherEncrypter(), errors.New("failed to load custom algorithm")
}
func (o *Options) LoadDecrypt() (Decrypt, error) {
if o.Logger == nil {
o.Logger = logging.DefaultLogger()
}
logging.Info(o.Logger).Log(logging.MessageKey(), "new decrypter", "options", o)
if algorithm, ok := o.Algorithm["type"].(string); ok {
switch strings.ToLower(algorithm) {
case "noop":
return DefaultCipherDecrypter(), nil
case "box":
if o.RecipientPrivateKey == "" || o.SenderPublicKey == "" {
break
}
boxLoader := BoxLoader{
PrivateKey: &FileLoader{
Path: o.RecipientPrivateKey,
},
PublicKey: &FileLoader{
Path: o.SenderPublicKey,
},
}
return boxLoader.LoadDecrypt()
case "basic":
if o.RecipientPrivateKey == "" || o.SenderPublicKey == "" {
break
}
if hashName, ok := o.Algorithm["hash"].(string); ok {
basicLoader := BasicLoader{
Hash: &BasicHashLoader{hashName},
PrivateKey: &FileLoader{
Path: o.RecipientPrivateKey,
},
PublicKey: &FileLoader{
Path: o.SenderPublicKey,
},
}
return basicLoader.LoadDecrypt()
} else {
return nil, errors.New("failed to find hash name for basic algorithm cipher type")
}
}
}
return DefaultCipherDecrypter(), errors.New("failed to load custom algorithm")
}