-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer.go
114 lines (95 loc) · 2.33 KB
/
signer.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
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"log"
"github.com/lestrrat-go/jwx/v2/jwk"
)
const kid = "test key"
const alg = "RS256"
var sgn *signer
type signer struct {
priv *rsa.PrivateKey
fake *rsa.PrivateKey
key jwk.Key
keySet jwk.Set
}
type header struct {
KID string `json:"kid,omitempty"`
Alg string `json:"alg,omitempty"`
}
func initSigner() {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Fatal("rsa.GenerateKey error:", err)
}
fake, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Fatal("rsa.GenerateKey error:", err)
}
key, err := jwk.FromRaw(priv.PublicKey)
if err != nil {
log.Fatal("jwk.New error:", err)
}
if err := key.Set(jwk.KeyIDKey, kid); err != nil {
log.Fatal("jwk.Key.Set error:", err)
}
if err := key.Set(jwk.AlgorithmKey, alg); err != nil {
log.Fatal("jwk.Key.Set error:", err)
}
keySet := jwk.NewSet()
keySet.AddKey(key)
log.Print("generated server RSA key")
sgn = &signer{
priv: priv,
fake: fake,
key: key,
keySet: keySet,
}
}
func (sgn *signer) sign(key *rsa.PrivateKey, hdr *header, pl interface{}) string {
hdrJSON, err := json.Marshal(hdr)
if err != nil {
log.Fatal("json.Marshal error:", err)
}
plJSON, err := json.Marshal(pl)
if err != nil {
log.Fatal("json.Marshal error:", err)
}
hdrEnc := base64.RawURLEncoding.EncodeToString(hdrJSON)
plEnc := base64.RawURLEncoding.EncodeToString(plJSON)
signed := hdrEnc + "." + plEnc
var sign []byte
switch hdr.Alg {
case "none":
// leave sign and err set to nil
case "RS256":
hash := sha256.Sum256([]byte(signed))
sign, err = rsa.SignPKCS1v15(rand.Reader, key, crypto.SHA256, hash[:])
case "RS384":
hash := sha512.Sum384([]byte(signed))
sign, err = rsa.SignPKCS1v15(rand.Reader, key, crypto.SHA384, hash[:])
case "RS512":
hash := sha512.Sum512([]byte(signed))
sign, err = rsa.SignPKCS1v15(rand.Reader, key, crypto.SHA512, hash[:])
default:
log.Fatalf("alg '%s' not supported by signer", hdr.Alg)
}
if err != nil {
log.Fatal("rsa.SignPKCS1v15 error:", err)
}
signEnc := base64.RawURLEncoding.EncodeToString(sign)
return signed + "." + signEnc
}
func (sgn *signer) simple(pl interface{}) string {
hdr := &header{
KID: kid,
Alg: alg,
}
return sgn.sign(sgn.priv, hdr, pl)
}