This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
369 lines (325 loc) · 9.24 KB
/
helper.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package helper
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"log"
"math/big"
"net"
"os"
"path/filepath"
"time"
"github.com/golang-jwt/jwt/v4"
)
// Global variables for test token creation
var (
DefaultTokenClaims = map[string]interface{}{
"iss": "https://dummy.ega.nbis.se",
"sub": "dummy",
"exp": time.Now().Add(time.Hour * 2).Unix(),
"pilot": "dummy-pilot",
}
WrongUserClaims = map[string]interface{}{
"sub": "c5773f41d17d27bd53b1e6794aedc32d7906e779@elixir-europe.org",
"aud": "15137645-3153-4d49-9ddb-594027cd4ca7",
"azp": "15137645-3153-4d49-9ddb-594027cd4ca7",
"scope": "ga4gh_passport_v1 openid",
"iss": "https://dummy.ega.nbis.se",
"iat": time.Now().Unix(),
"exp": time.Now().Add(time.Hour * 2).Unix(),
"jti": "5e6c6d24-42eb-408e-ba20-203904e388a1",
}
ExpiredClaims = map[string]interface{}{
"sub": "dummy",
"aud": "15137645-3153-4d49-9ddb-594027cd4ca7",
"azp": "15137645-3153-4d49-9ddb-594027cd4ca7",
"scope": "ga4gh_passport_v1 openid",
"iss": "https://dummy.ega.nbis.se",
"exp": time.Now().Add(-time.Hour * 2).Unix(),
"iat": time.Now().Unix(),
"jti": "5e6c6d24-42eb-408e-ba20-203904e388a1",
}
NonValidClaims = map[string]interface{}{
"sub": "c5773f41d17d27bd53b1e6794aedc32d7906e779@elixir-europe.org",
"aud": "15137645-3153-4d49-9ddb-594027cd4ca7",
"azp": "15137645-3153-4d49-9ddb-594027cd4ca7",
"scope": "ga4gh_passport_v1 openid",
"iss": "https://dummy.ega.nbis.se",
"exp": time.Now().Add(time.Hour * 2).Unix(),
"iat": time.Now().Add(time.Hour * 2).Unix(),
"jti": "5e6c6d24-42eb-408e-ba20-203904e388a1",
}
WrongTokenAlgClaims = map[string]interface{}{
"iss": "Online JWT Builder",
"iat": time.Now().Unix(),
"exp": time.Now().Add(time.Hour * 2).Unix(),
"aud": "4e9416a7-3515-447a-b848-d4ac7a57f",
"sub": "pleasefix@snurre-in-the-house.org",
"auth_time": "1632207224",
"jti": "cc847f9c-7608-4b4f-9c6f-6e734813355f",
}
)
// MakeFolder creates a folder and subfolders for the keys pair
// Returns the two paths
func MakeFolder(path string) (string, string, error) {
prKeyPath := path + "/private-key"
pubKeyPath := path + "/public-key"
err := os.MkdirAll(prKeyPath, 0750)
if err != nil {
// fmt.Errorf("error creating directory: %v", err)
return " ", " ", err
}
err = os.MkdirAll(pubKeyPath, 0750)
if err != nil {
// fmt.Errorf("error creatin directory: %w", err)
return " ", " ", err
}
return prKeyPath, pubKeyPath, nil
}
// ParsePrivateRSAKey reads and parses the RSA private key
func ParsePrivateRSAKey(path, keyName string) (*rsa.PrivateKey, error) {
keyPath := path + keyName
prKey, err := os.ReadFile(filepath.Clean(keyPath))
if err != nil {
return nil, err
}
prKeyParsed, err := jwt.ParseRSAPrivateKeyFromPEM(prKey)
if err != nil {
return nil, err
}
return prKeyParsed, nil
}
// CreateRSAkeys creates the RSA key pair
func CreateRSAkeys(prPath, pubPath string) error {
privatekey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
publickey := &privatekey.PublicKey
// dump private key to file
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privatekey)
privateKeyBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privateKeyBytes,
}
privatePem, err := os.Create(prPath + "/dummy.ega.nbis.se")
if err != nil {
return err
}
err = pem.Encode(privatePem, privateKeyBlock)
if err != nil {
return err
}
// dump public key to file
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publickey)
if err != nil {
return err
}
publicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
publicPem, err := os.Create(pubPath + "/dummy.ega.nbis.se.pub")
if err != nil {
return err
}
err = pem.Encode(publicPem, publicKeyBlock)
if err != nil {
return err
}
return nil
}
// CreateRSAToken creates an RSA token
func CreateRSAToken(key *rsa.PrivateKey, headerAlg, headerType string, tokenClaims map[string]interface{}) (string, error) {
token := jwt.New(jwt.SigningMethodRS256)
token.Header["alg"] = headerAlg
token.Header["typ"] = headerType
claims := make(jwt.MapClaims)
for key, value := range tokenClaims {
claims[key] = value
}
token.Claims = claims
tokenString, err := token.SignedString(key)
if err != nil {
return "no-token", err
}
return tokenString, nil
}
// CreateECToken creates an EC token
func CreateECToken(key *ecdsa.PrivateKey, headerAlg, headerType string, tokenClaims map[string]interface{}) (string, error) {
token := jwt.New(jwt.SigningMethodES256)
token.Header["alg"] = headerAlg
token.Header["typ"] = headerType
claims := make(jwt.MapClaims)
for key, value := range tokenClaims {
claims[key] = value
}
token.Claims = claims
tokenString, err := token.SignedString(key)
if err != nil {
return "no-token", err
}
return tokenString, nil
}
// CreateHSToken creates an HS token
func CreateHSToken(key []byte, headerAlg, headerType string, tokenClaims map[string]interface{}) (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
token.Header["alg"] = headerAlg
token.Header["typ"] = headerType
claims := make(jwt.MapClaims)
for key, value := range tokenClaims {
claims[key] = value
}
token.Claims = claims
tokenString, err := token.SignedString(key)
if err != nil {
return "no-token", err
}
return tokenString, nil
}
// ParsePrivateECKey reads and parses the EC private key
func ParsePrivateECKey(path, keyName string) (*ecdsa.PrivateKey, error) {
keyPath := path + keyName
prKey, err := os.ReadFile(filepath.Clean(keyPath))
if err != nil {
return nil, err
}
prKeyParsed, err := jwt.ParseECPrivateKeyFromPEM(prKey)
if err != nil {
return nil, err
}
return prKeyParsed, nil
}
// CreateECkeys creates the EC key pair
func CreateECkeys(prPath, pubPath string) error {
privatekey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return err
}
publickey := &privatekey.PublicKey
// dump private key to file
privateKeyBytes, _ := x509.MarshalECPrivateKey(privatekey)
privateKeyBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKeyBytes,
}
privatePem, err := os.Create(prPath + "/dummy.ega.nbis.se")
if err != nil {
return err
}
err = pem.Encode(privatePem, privateKeyBlock)
if err != nil {
return err
}
// dump public key to file
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publickey)
if err != nil {
return err
}
publicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
publicPem, err := os.Create(pubPath + "/dummy.ega.nbis.se.pub")
if err != nil {
return err
}
err = pem.Encode(publicPem, publicKeyBlock)
if err != nil {
return err
}
return nil
}
func MakeCerts(outDir string) {
// set up our CA certificate
caTemplate := &x509.Certificate{
SerialNumber: big.NewInt(2000),
Subject: pkix.Name{
Organization: []string{"NEIC"},
CommonName: "Root CA",
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(0, 0, 1),
KeyUsage: x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
}
// create our private and public key
caPrivKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatalln(err)
}
// create the CA certificate
caBytes, err := x509.CreateCertificate(rand.Reader, caTemplate, caTemplate, &caPrivKey.PublicKey, caPrivKey)
if err != nil {
log.Fatalln(err)
}
err = TLScertToFile(outDir+"/ca.crt", caBytes)
if err != nil {
log.Fatalln(err)
}
tlsKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatalln(err)
}
err = TLSkeyToFile(outDir+"/tls.key", tlsKey)
if err != nil {
log.Fatalln(err)
}
// set up our server certificate
certTemplate := &x509.Certificate{
SerialNumber: big.NewInt(2121),
Subject: pkix.Name{
Organization: []string{"NEIC"},
CommonName: "test_cert",
},
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback},
DNSNames: []string{"localhost,mq,proxy,s3"},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(0, 0, 1),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
IsCA: false,
}
// create the TLS certificate
certBytes, err := x509.CreateCertificate(rand.Reader, certTemplate, caTemplate, &tlsKey.PublicKey, caPrivKey)
if err != nil {
log.Fatalln(err)
}
err = TLScertToFile(outDir+"/tls.crt", certBytes)
if err != nil {
log.Fatalln(err)
}
log.Printf("certificartes written to: %s", outDir)
}
func TLSkeyToFile(filename string, key *ecdsa.PrivateKey) error {
keyFile, err := os.Create(filename)
if err != nil {
return err
}
defer keyFile.Close()
pk, err := x509.MarshalECPrivateKey(key)
if err != nil {
return err
}
if err := pem.Encode(keyFile, &pem.Block{Type: "EC PRIVATE KEY", Bytes: pk}); err != nil {
return err
}
return nil
}
func TLScertToFile(filename string, derBytes []byte) error {
certFile, err := os.Create(filename)
if err != nil {
return err
}
defer certFile.Close()
if err := pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
return err
}
return nil
}