-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgowebpusher.go
456 lines (376 loc) · 13.5 KB
/
gowebpusher.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//Package webpusher helps sending push notifications to web browsers
package webpusher
import (
"bytes"
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"golang.org/x/crypto/hkdf"
)
//PushSubscription interface of the Push API provides a subscription's URL endpoint.
type PushSubscription struct {
Endpoint string
Key PushSubscriptionKey
}
//Sender instance
type Sender struct {
PushSubscriptions []PushSubscription
VAPIDPublicKey string
VAPIDPrivateKey string
}
//VAPIDKeys contains the public and private VAPID keys
type VAPIDKeys struct {
Public string
Private string
}
//PushSubscriptionKey represents a client public key, which can then be sent to a server and used in encrypting push message data.
// P256dh: 🔒 Receiver public key (‘p256dh’): The p256dh key received as part of the Subscription data.
// Auth: 🔑 Auth key (‘auth’): Auth key The auth key received as part of the Subscription data.
type PushSubscriptionKey struct {
P256dh string
Auth string
}
//NewSender will initialize an instance of sender
func NewSender() *Sender {
s := &Sender{}
s.Initialize()
return s
}
//Initialize will set the default values of the sender instance
func (s *Sender) Initialize() {
//s.PushSubscriptions = make([]PushSubscription, 0, 1000)
}
//Send will deliver the notification to all subscriptions
func (s *Sender) Send() int {
subscriptionURL, _ := url.Parse("https://fcm.googleapis.com/fcm/send/")
claims := map[string]interface{}{
"aud": fmt.Sprintf("%s://%s", subscriptionURL.Scheme, subscriptionURL.Host),
"exp": time.Now().Add(time.Hour * 12).Unix(),
"sub": "mailto:mail@mail.com"}
for _, sub := range s.PushSubscriptions {
res, err := s.sendNotification([]byte("{\"body\":\"Hello World\"}"), &sub, claims)
fmt.Println(res, err)
}
//Testing return
return len(s.PushSubscriptions)
}
//sendNotification will send a message/payload to a subscriber
func (s *Sender) sendNotification(payload []byte, sub *PushSubscription, claims map[string]interface{}) (*http.Response, error) {
//VAPID keys
VAPIDkeys := VAPIDKeys{
s.VAPIDPublicKey, s.VAPIDPrivateKey,
}
// 🔒 Receiver public key [p256dh]
buf := bytes.NewBufferString(sub.Key.P256dh)
receiverPubKey, err := base64.StdEncoding.DecodeString(buf.String())
//receiver Public Key must have "=" padding added back before it can be decoded.
if rem := len(receiverPubKey) % 4; rem != 0 {
buf.WriteString(strings.Repeat("=", 4-rem))
}
if err != nil {
receiverPubKey, err = base64.URLEncoding.DecodeString(buf.String())
if err != nil {
return nil, err
}
}
// Generate shared ECDH && local Public key
sharedECDH, localPubKey, err := generateSharedECDH(receiverPubKey)
if err != nil {
return nil, err
}
// 🔑 Auth key (‘auth’)
// Auth key: The auth key received as part of the Subscription data.
secretBuf := bytes.NewBufferString(sub.Key.Auth)
if rem := len(sub.Key.Auth) % 4; rem != 0 {
secretBuf.WriteString(strings.Repeat("=", 4-rem))
}
authKey, err := base64.StdEncoding.DecodeString(secretBuf.String())
if err != nil {
authKey, _ = base64.URLEncoding.DecodeString(secretBuf.String())
}
// Encrypt payload
encryptionHeaderBuf, encryptedPayload, err := encryptPayload(payload, localPubKey, receiverPubKey, sharedECDH, authKey)
if err != nil {
return nil, err
}
// POST notification request
req, err := http.NewRequest("POST", sub.Endpoint, encryptionHeaderBuf)
if err != nil {
return nil, err
}
//The TTL Header is the number of seconds the notification should stay in storage if the remote user agent isn’t actively connected.
//“0” (Zed/Zero) means that the notification is discarded immediately if the remote user agent is not connected; this is the default.
//This header must be specified, even if the value is “0”.
req.Header.Set("TTL", strconv.Itoa(30))
req.Header.Set("Content-Encoding", "aes128gcm")
req.Header.Set("Content-Length", strconv.Itoa(len(encryptedPayload)))
req.Header.Set("Content-Type", "application/octet-stream")
//Generate VAPID Authorization header which contains JWT signed token and VAPID public key
AuthorizationHeader, err := generateVAPIDAuth(VAPIDkeys, claims)
if err != nil {
return nil, err
}
// Set VAPID authorization header
req.Header.Set("Authorization", AuthorizationHeader)
tr := &http.Transport{
IdleConnTimeout: 30 * time.Second,
}
client := &http.Client{Transport: tr}
return client.Do(req)
}
//GenerateVAPID will generate public and private VAPID keys using ECDH protocl
func GenerateVAPID() (vapidPrivateKey string, vapidPublicKey string, err error) {
curve := elliptic.P256()
privateKey, x, y, err := elliptic.GenerateKey(curve, rand.Reader)
if err != nil {
return "", "", err
}
publicKey := elliptic.Marshal(curve, x, y)
privKey := base64.RawURLEncoding.EncodeToString(privateKey)
pubKey := base64.RawURLEncoding.EncodeToString(publicKey)
return privKey, pubKey, nil
}
// validateVAPIDKeys will validate the length and encoding of VAPID keys
func validateVAPIDKeys(keys VAPIDKeys) error {
if len(keys.Public) != 87 {
return errors.New("Invalid Public key length")
}
if len(keys.Private) != 43 {
return errors.New("Invalid Private key length")
}
_, err := base64.RawURLEncoding.DecodeString(keys.Private)
if err != nil {
return errors.New("Invalid Private key")
}
_, err = base64.RawURLEncoding.DecodeString(keys.Public)
if err != nil {
return errors.New("Invalid Public key")
}
return nil
}
//verifyClaims will verify the claims of JWT string
func verifyClaims(claims map[string]interface{}) error {
//Validate claims
// sub: The “Subscriber” a mailto link for the administrative contact for this feed.
// It’s best if this email is not a personal email address,
// but rather a group email so that if a person leaves an organization,
// is unavailable for an extended period, or otherwise can’t respond, someone else on the list can.
if _, ok := claims["sub"]; ok {
if !(strings.HasPrefix(claims["sub"].(string), "mailto:")) && !(strings.HasPrefix(claims["sub"].(string), "https://")) {
return errors.New("“Subscriber” claim (sub) is invalid, it should be an email or contact URL")
}
}
//exp : “Expires” this is an integer that is the date and time that this VAPID header should remain valid until.
// It doesn’t reflect how long your VAPID signature key should be valid, just this specific update.
// It can be no more than 24 hours
if _, ok := claims["exp"]; ok {
now := time.Now().Unix()
tomorrow := time.Now().Add(24 * time.Hour).Unix()
if now > claims["exp"].(int64) {
return errors.New("Expiry claim (exp) already expired")
}
if claims["exp"].(int64) > tomorrow {
return errors.New("Expiry claim (exp) maximum value is 24 hours")
}
}
return nil
}
//GenerateVAPIDAuth will generate Authorization header for web push notifications
func generateVAPIDAuth(keys VAPIDKeys, claims map[string]interface{}) (string, error) {
//Validate VAPID Keys
if err := validateVAPIDKeys(keys); err != nil {
return "", err
}
//Verify Claims
if err := verifyClaims(claims); err != nil {
return "", err
}
// JWTInfo is base64 Encoded {"typ":"JWT","alg":"ES256"} which is the first part of the JWT Token
JWTInfo := "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9."
// JWTData is the second part of the token which contains all the claims encoded in base64
jsonValue, err := json.Marshal(claims)
if err != nil {
return "", errors.New("Marshaling Claims JSON failed" + err.Error())
}
JWTData := strings.TrimRight(base64.URLEncoding.EncodeToString(jsonValue), "=")
JWTSignature, err := generateJWTSignature(keys, JWTInfo+JWTData)
if err != nil {
return "", err
}
//Compose the JWT Token string
JWTString := JWTInfo + JWTData + JWTSignature
// Construct the VAPID header
VAPIDAuth := fmt.Sprintf(
"vapid t=%s, k=%s",
JWTString,
keys.Public,
)
return VAPIDAuth, nil
}
//generateJWTSignature ..
func generateJWTSignature(keys VAPIDKeys, JWTInfoAndData string) (string, error) {
// Signature is the third part of the token, which includes the data above signed with the private key
// Preparing ecdsa.PrivateKey for signing
privKeyDecoded, err := base64.RawURLEncoding.DecodeString(keys.Private)
if err != nil {
return "", errors.New("Invalid VAPID private key string, cannot decode it")
}
curve := elliptic.P256()
px, py := curve.ScalarMult(
curve.Params().Gx,
curve.Params().Gy,
privKeyDecoded,
)
pubKey := ecdsa.PublicKey{
Curve: curve,
X: px,
Y: py,
}
// Private key
d := &big.Int{}
d.SetBytes(privKeyDecoded)
privKey := &ecdsa.PrivateKey{
PublicKey: pubKey,
D: d,
}
// Get the key
hash := crypto.SHA256
hasher := hash.New()
hasher.Write([]byte(JWTInfoAndData))
// Sign JWTInfo and JWTData using the private key
r, s, err := ecdsa.Sign(rand.Reader, privKey, hasher.Sum(nil))
if err != nil {
return "", errors.New("Err singing data")
}
curveBits := privKey.Curve.Params().BitSize
if curveBits != 256 {
return "", errors.New("curveBits should be 256")
}
keyBytes := curveBits / 8
if curveBits%8 > 0 {
keyBytes++
}
rBytes := r.Bytes()
rBytesPadded := make([]byte, keyBytes)
copy(rBytesPadded[keyBytes-len(rBytes):], rBytes)
sBytes := s.Bytes()
sBytesPadded := make([]byte, keyBytes)
copy(sBytesPadded[keyBytes-len(sBytes):], sBytes)
out := append(rBytesPadded, sBytesPadded...)
return "." + strings.TrimRight(base64.URLEncoding.EncodeToString(out), "="), nil
}
//ECDH a secure way to share public keys and generate a shared secret and local Public key
func generateSharedECDH(receiverPubKey []byte) ([]byte, []byte, error) {
curve := elliptic.P256()
localPrivateKey, x, y, err := elliptic.GenerateKey(curve, rand.Reader)
if err != nil {
return nil, nil, err
}
// Get the shared x,y point using client/receiver's Public key and local private key
sharedX, sharedY := elliptic.Unmarshal(curve, receiverPubKey)
if sharedX == nil {
return nil, nil, errors.New("Invalid Public key")
}
sx, sy := curve.ScalarMult(sharedX, sharedY, localPrivateKey)
if !curve.IsOnCurve(sx, sy) {
return nil, nil, errors.New("Shared point is not on the curve")
}
sharedECDH := make([]byte, curve.Params().BitSize/8)
sx.FillBytes(sharedECDH)
return sharedECDH, elliptic.Marshal(curve, x, y), nil
}
//encryptPayload ..
func encryptPayload(message []byte, localPubKey []byte, receiverPubKey []byte, sharedECDH []byte, authKey []byte) (*bytes.Buffer, []byte, error) {
// 🏭 Build using / derive
prkInfoBuf := bytes.NewBuffer([]byte("WebPush: info\x00"))
prkInfoBuf.Write(receiverPubKey)
prkInfoBuf.Write(localPubKey)
ikm, err := readKey(hkdf.New(sha256.New, sharedECDH, authKey, prkInfoBuf.Bytes()), 32)
if err != nil {
return nil, nil, err
}
/******* the Encryption Key and Nonce *****/
// 📎 salt
// The salt needs to be 16 bytes of random data.
salt := make([]byte, 16)
_, err = io.ReadFull(rand.Reader, salt)
if err != nil {
return nil, nil, err
}
//Content-Encoding: aes128gcm
//🔓 = HKDF(🔑, “Content-Encoding: aes128gcm\x00” + ⚓).🏭(🙊)
// This is the scheme described in RFC 8188. It's supported in Firefox 55+ and Chrome 60+, and replaces the older aesgcm scheme from earlier drafts. This scheme includes the salt, record size, and sender public key in a binary header block in the payload.
encryptionKey, err := readKey(hkdf.New(sha256.New, ikm, salt, []byte("Content-Encoding: aes128gcm\x00")), 16)
if err != nil {
return nil, nil, err
}
//🎲 message nonce
//🎲 = HKDF(🔑 , “Content-Encoding: nonce\x00” + ⚓).🏭(🙊)
// The sender and receiver combine the PRK with a random 16-byte salt. The salt is generated by the sender, and shared with the receiver as part of the message payload.
nonceInfo := []byte("Content-Encoding: nonce\x00")
nonceHKDF := hkdf.New(sha256.New, ikm, salt, nonceInfo)
encryptionNonce, err := readKey(nonceHKDF, 12)
if err != nil {
return nil, nil, err
}
// Cipher
c, err := aes.NewCipher(encryptionKey)
if err != nil {
return nil, nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, nil, err
}
encryptionHeaderBuf := bytes.NewBuffer(salt)
//The sender chunks the plaintext into fixed-size records, and includes this size in the message payload as the rs
rs := make([]byte, 4)
binary.BigEndian.PutUint32(rs, 4096)
encryptionHeaderBuf.Write(rs)
encryptionHeaderBuf.Write([]byte{byte(len(localPubKey))})
encryptionHeaderBuf.Write(localPubKey)
// 📄 Payload
payloadBuf := bytes.NewBuffer(message)
// The encrypter prefixes a “\x00\x00” to the data chunk, processes it completely, and then concatenates its encryption tag to the end of the completed chunk.
payloadBuf.Write([]byte("\x02"))
maxPadLen := (4096 - 16) - encryptionHeaderBuf.Len()
payloadLen := payloadBuf.Len()
if payloadLen > maxPadLen {
return nil, nil, errors.New("payload has exceeded the maximum length")
}
padLen := maxPadLen - payloadLen
padding := make([]byte, padLen)
payloadBuf.Write(padding)
// Encrypt the payload using the content encryption key (CEK)
encryptedPayload := gcm.Seal([]byte{}, encryptionNonce, payloadBuf.Bytes(), nil)
encryptionHeaderBuf.Write(encryptedPayload)
return encryptionHeaderBuf, encryptedPayload, nil
}
//readKey will the key with specified length
func readKey(hkdf io.Reader, length int) ([]byte, error) {
key := make([]byte, length)
n, err := io.ReadFull(hkdf, key)
if n != len(key) {
return key, errors.New("Read length doesn't match key length")
}
if err != nil {
return key, err
}
return key, nil
}