-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJwsPs256Signature.go
219 lines (195 loc) · 8.63 KB
/
JwsPs256Signature.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
package main
import (
"fmt"
"strings"
"regexp"
"time"
"strconv"
"encoding/json"
"github.com/dvsekhvalnov/jose2go"
Rsa "github.com/dvsekhvalnov/jose2go/keys/rsa"
)
func main() {
// uses https://github.com/dvsekhvalnov/jose2go
fmt.Println("JWT JWS PS256 RSA with RSASSA-PSS + MGF1 with SHA-256 padding and SHA-256 hashing")
jwsAlgorithm := string("PS256")
fmt.Println("\n* * * sign the payload object with the RSA private key * * *")
// build a JWT payload object
issuer := string("https://java-crypto.github.io/cross_platform_crypto/")
subject := string("JWT PS256 signature")
tokenExpirationInSeconds := 120
payload := BuildPayloadObject(subject, issuer, tokenExpirationInSeconds)
fmt.Println("jwtClaimsSet: " + payload)
// // read RSA private key as PEM object
privateKeyByte := []byte(LoadRsaPrivateKeyPem());
fmt.Println("private key loaded")
// sign the payload
jwsSignedToken := JwsSignatureToString(jwsAlgorithm, privateKeyByte, payload)
fmt.Println("\nsigned jwsToken:\n" + jwsSignedToken)
// send the token to the recipient
fmt.Println("\n* * * verify the jwsToken with the RSA public key * * *")
jwsSignedTokenReceived := jwsSignedToken
// load public key
publicKeyByte := []byte(LoadRsaPublicKeyPem())
tokenVerified := JwsSignatureVerification(publicKeyByte, jwsSignedTokenReceived);
fmt.Print("\nthe tokens signature is verified: ")
fmt.Println(tokenVerified)
fmt.Println("\ncheck signature with not matching public key")
publicKeyByte2 := []byte(LoadRsaPublicKeyPem2());
tokenVerified2 := JwsSignatureVerification(publicKeyByte2, jwsSignedTokenReceived)
fmt.Print("the tokens signature is verified: ")
fmt.Println(tokenVerified2)
// check for expiration
fmt.Println("\ncheck expiration that is valid")
decodedPayload := JwsGetPayload(publicKeyByte, jwsSignedTokenReceived);
tokenValid := CheckExpiration(decodedPayload)
fmt.Print("the token is valid and not expired: ")
fmt.Println(tokenValid)
// with expired token
fmt.Println("\ncheck expiration that is invalid")
jwsSignedTokenReceivedExpired := `eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL2phdmEtY3J5cHRvLmdpdGh1Yi5pby9jcm9zc19wbGF0Zm9ybV9jcnlwdG8vIiwic3ViIjoiSldUIFJTMjU2IHNpZ25hdHVyZSIsImlhdCI6MTYxMzQ3NDQ4MSwiZXhwIjoxNjEzNDc0NDgzfQ.pWw0U6nMwTikn1WImAi8phL6kxrVqwpyNAH7u1DVL26DyBbmtEFLTqAU3szrrVbB6vZirZx8fvJTMRaze3ExGZOzEgH4sUQkKz3HdUymz2LKWWpzlucRCB5CCMm5uVRf_f6MNsfAw3Fx41DryXU7BBoV3fp8Qt8rzdCPwabHSlCyNTC69AI46OmS25IUXzBpuhxa8-M9hh97YnSNwOYOfow3evJDNa9dUsO3ZLgUNTPQuxct3f0Bv95zmWzoMJYY-YsOSnBncSnac1t0GlhcwQCceata1fBrBqIc3a-r7hhrFmwv3Bam0LdCYTiqH5UU83osCIabFb3qlnQcRdLO0Q`
decodedPayload = JwsGetPayload(publicKeyByte, jwsSignedTokenReceivedExpired);
tokenValid = CheckExpiration(decodedPayload)
fmt.Print("the token is valid and not expired: ")
fmt.Println(tokenValid)
fmt.Printf("\nprint payload = %v\n",JwsGetPayload(publicKeyByte, jwsSignedTokenReceived))
fmt.Printf("\nheader = %v\n",JwsGetHeader(publicKeyByte, jwsSignedTokenReceived))
}
func JwsSignatureToString(jwsAlgorithm string, privateKeyByte []byte, payload string)(string) {
privateKey,e:=Rsa.ReadPrivate(privateKeyByte)
if(e!=nil) {
panic("invalid key format")
}
token,err := jose.Sign(payload,jose.PS256, privateKey)
if(err==nil) {
//go use token
return token
}
return "" // error
}
func JwsSignatureVerification(publicKeyByte []byte, jwsSignedTokenReceived string)(bool) {
publicKey, e:=Rsa.ReadPublic(publicKeyByte)
if(e!=nil) {
panic("invalid key format")
}
payload, headers, err := jose.Decode(jwsSignedTokenReceived, publicKey)
if(err==nil) {
//go use token
fmt.Printf("\npayload = %v\n",payload)
//and/or use headers
fmt.Printf("headers = %v\n",headers)
return true
}
if(e!=nil) {
panic("error in verification")
}
return false;
}
func JwsGetPayload(publicKeyByte []byte, jwsSignedTokenReceived string)(string) {
publicKey, e:=Rsa.ReadPublic(publicKeyByte)
if(e!=nil) {
panic("invalid key format")
}
payload, _, err := jose.Decode(jwsSignedTokenReceived, publicKey)
if(err==nil) {
return payload
}
if(e!=nil) {
panic("error in verification")
}
return "error";
}
func JwsGetHeader(publicKeyByte []byte, jwsSignedTokenReceived string)(string) {
publicKey, e:=Rsa.ReadPublic(publicKeyByte)
if(e!=nil) {
panic("invalid key format")
}
_, header, err := jose.Decode(jwsSignedTokenReceived, publicKey)
if(err==nil) {
b, _ := json.Marshal(header)
//fmt.Println(string(b))
headerString := string(b)
return string(headerString)
}
if(e!=nil) {
panic("error in verification")
}
return "error";
}
func CheckExpiration(jwsPayload string)(bool) {
iat := time.Now().Unix() // actual time
expString := ExtractValue(jwsPayload, "exp")
exp, _ := strconv.ParseInt(expString, 10, 64)
if (iat < exp) {
return true // not expired
}
return false // expired
}
// extracts the value for a key from a JSON-formatted string
// body - the JSON-response as a string. Usually retrieved via the request body
// key - the key for which the value should be extracted
// returns - the value for the given key
func ExtractValue(body string, key string) string {
keystr := "\"" + key + "\":[^,;\\]}]*"
r, _ := regexp.Compile(keystr)
match := r.FindString(body)
keyValMatch := strings.Split(match, ":")
return strings.ReplaceAll(keyValMatch[1], "\"", "")
}
func BuildPayloadObject(subject string, issuer string, tokenExpirationInSeconds int)(string) {
iat := time.Now().Unix()
exp := iat + int64(tokenExpirationInSeconds)
return string(`{"iss":"`) + string(issuer) + string(`","sub":"`) + string(subject) + string(`","iat":`) + strconv.FormatInt(iat, 10) + string(`,"exp":`) + strconv.FormatInt(exp, 10) + string(`}`)
}
func LoadRsaPrivateKeyPem()([]byte) {
return []byte(`-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDwSZYlRn86zPi9
e1RTZL7QzgE/36zjbeCMyOhf6o/WIKeVxFwVbG2FAY3YJZIxnBH+9j1XS6f+ewjG
FlJY4f2IrOpS1kPiO3fmOo5N4nc8JKvjwmKtUM0t63uFFPfs69+7mKJ4w3tk2mSN
4gb8J9P9BCXtH6Q78SdOYvdCMspA1X8eERsdLb/jjHs8+gepKqQ6+XwZbSq0vf2B
MtaAB7zTX/Dk+ZxDfwIobShPaB0mYmojE2YAQeRq1gYdwwO1dEGk6E5J2toWPpKY
/IcSYsGKyFqrsmbw0880r1BwRDer4RFrkzp4zvY+kX3eDanlyMqDLPN+ghXT1lv8
snZpbaBDAgMBAAECggEBAIVxmHzjBc11/73bPB2EGaSEg5UhdzZm0wncmZCLB453
XBqEjk8nhDsVfdzIIMSEVEowHijYz1c4pMq9osXR26eHwCp47AI73H5zjowadPVl
uEAot/xgn1IdMN/boURmSj44qiI/DcwYrTdOi2qGA+jD4PwrUl4nsxiJRZ/x7PjL
hMzRbvDxQ4/Q4ThYXwoEGiIBBK/iB3Z5eR7lFa8E5yAaxM2QP9PENBr/OqkGXLWV
qA/YTxs3g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IBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8EmWJUZ/Osz4vXtUU2S+
0M4BP9+s423gjMjoX+qP1iCnlcRcFWxthQGN2CWSMZwR/vY9V0un/nsIxhZSWOH9
iKzqUtZD4jt35jqOTeJ3PCSr48JirVDNLet7hRT37Ovfu5iieMN7ZNpkjeIG/CfT
/QQl7R+kO/EnTmL3QjLKQNV/HhEbHS2/44x7PPoHqSqkOvl8GW0qtL39gTLWgAe8
01/w5PmcQ38CKG0oT2gdJmJqIxNmAEHkatYGHcMDtXRBpOhOSdraFj6SmPyHEmLB
ishaq7Jm8NPPNK9QcEQ3q+ERa5M6eM72PpF93g2p5cjKgyzzfoIV09Zb/LJ2aW2g
QwIDAQAB
-----END PUBLIC KEY-----`)
}
func LoadRsaPublicKeyPem2()([]byte) {
return []byte(`-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8EmWJUZ/Osz4vXtUU2S+
0M4BP9+s423gjMjoX+qP1iCnlcRcFWxthQGN2CWSMZwR/vY9V0un/nsIxhZSWOH9
iKzqUtZD4jt35jqOTeJ3PCSr48JirVDNLet7hRT37Ovfu5iieMN7ZNpkjeIG/CfT
/QQl7R+kO/EnTmL3QjLKQNV/HhEbHS2/44x7PPoHqSqkOvl8GW0qtL39gTLWgAe8
01/w5PmcQ38CKG0oT2gdJmJqIxNmAEHkatYGHcMDtXRBpOhOSdraFj6SmPyHEmLB
ishaq7Jm8NPPNK9QcEQ3q+ERa5M6eM72PpF93g2p5cjKgyzzfoIV09Zb/LJ2aW3g
QwIDAQAB
-----END PUBLIC KEY-----`)
}