-
Notifications
You must be signed in to change notification settings - Fork 3
/
jwkgen.go
90 lines (81 loc) · 2.24 KB
/
jwkgen.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
package main
import (
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
"log"
)
var (
buildDate string
version string
bareOutput = false
allowUnsafe = kingpin.
Flag("allow-unsafe", "Allow unsafe parameters").Bool()
useColor = kingpin.
Flag("color", "Use color in JSON output (true by default)").
Default("true").Bool()
curve = kingpin.
Flag("curve", "Named elliptic curve to use to generate a key. Valid values are P256, P384, P521, X25519, Ed25519").
Short('e').Default("Ed25519").String()
bits = kingpin.
Flag("bits", "Number of bits to use for RSA or octet keys").
Short('b').Default("2048").Int()
onlyPEM = kingpin.Flag("pem", "Print only PEM format").Bool()
onlyJWK = kingpin.Flag("jwk", "Print only JWK format").Bool()
keyType = kingpin.Arg("key type", "Key type: oct, rsa, ec").Default("ec").Enum("oct", "rsa", "ec")
filename = kingpin.Arg("filename", "Output filename (without extension)").String()
)
func main() {
if version == "" {
kingpin.Version("JWK Generator (jwkgen) development snapshot")
} else {
kingpin.Version(fmt.Sprintf("JWK Generator (jwkgen) version %s\nBuild date: %s", version, buildDate))
}
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Parse()
if *keyType == "oct" {
printSymmetricKey()
} else {
printKeyPair()
}
if !bareOutput {
fmt.Println()
}
}
func printSymmetricKey() {
if *onlyPEM {
return // Write nothing
}
bareOutput = true
key := generateOctKey()
err := writeJSONFor(ObjectInfo{".json", "Key (JWK)"}, &key)
checkKeyError(err)
}
func printKeyPair() {
priv, pub, privJwk, pubJwk := generateKeyPair()
bareOutput = *filename == "" && (*onlyJWK || *onlyPEM)
writePublic := !bareOutput
writeJWK := !*onlyPEM
writePEM := !*onlyJWK
var err error
if writeJWK {
err = writeJSONFor(ObjectInfo{".json", "Private Key (JWK)"}, &privJwk)
checkKeyError(err)
}
if writePublic {
err = writeJSONFor(ObjectInfo{".pub.json", "Public Key (JWK)"}, &pubJwk)
checkKeyError(err)
}
if writePEM {
err = writePemFor(ObjectInfo{".pem", "Private Key (PEM)"}, priv)
checkKeyError(err)
}
if writePublic {
err = writePemFor(ObjectInfo{".pub.pem", "Public Key (PEM)"}, pub)
checkKeyError(err)
}
}
func checkKeyError(err error) {
if err != nil {
log.Fatalf("Could not write key: %v", err)
}
}