-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpush.go
More file actions
324 lines (284 loc) · 9.13 KB
/
Copy pathwebpush.go
File metadata and controls
324 lines (284 loc) · 9.13 KB
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
package webpush
import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/ecdh"
"crypto/hkdf"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"hash"
"net/http"
"strings"
"time"
)
const MaxRecordSize uint32 = 4096
var (
ErrRecordSizeTooSmall = errors.New("record size too small for message")
ErrInvalidSubject = errors.New("invalid subject")
ErrInvalidTopic = errors.New("invalid topic")
ErrInvalidExpiration = errors.New("invalid VAPID expiration")
ErrMissingVAPIDKeys = errors.New("missing VAPID keys")
invalidAuthKeyLength = errors.New("invalid auth key length (must be 16)")
defaultHTTPClient HTTPClient = &http.Client{}
defaultClient = NewClient(Config{})
randomRead = rand.Read
newECDHPublicKey = func(key []byte) (*ecdh.PublicKey, error) { return ecdh.P256().NewPublicKey(key) }
generateECDHPrivateKey = func() (*ecdh.PrivateKey, error) { return ecdh.P256().GenerateKey(rand.Reader) }
hkdfKey = func(h func() hash.Hash, secret, salt []byte, info string, keyLength int) ([]byte, error) {
return hkdf.Key(h, secret, salt, info, keyLength)
}
newAESCipher = aes.NewCipher
newGCM = cipher.NewGCM
)
// HTTPClient is an interface for sending the notification HTTP request / testing.
type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}
// SendOptions are the parameters used to send a notification.
type SendOptions struct {
Subject string // Subject for the VAPID JWT token: email, mailto: URL, or HTTPS URL.
TTL int // TTL on the endpoint POST request, in seconds.
Urgency Urgency // Optional urgency header.
Topic string // Optional topic header.
VAPIDKeys *VAPIDKeys // VAPID public-private keypair for the Authorization header.
VAPIDExpiration time.Time // Optional VAPID JWT expiration (defaults to now + 12 hours).
RecordSize uint32 // Per-record encrypted payload size.
RequestReceipt bool // Request push receipt metadata from the push service.
ReceiptSubscription string // Optional receipt subscription URI to send as a Link relation.
}
// Keys represent a subscription's keys (its ECDH public key on the P-256 curve
// and its 16-byte authentication secret).
type Keys struct {
Auth [16]byte
P256dh *ecdh.PublicKey
}
// Equal compares two Keys for equality.
func (k *Keys) Equal(o Keys) bool {
if k == nil {
return o.P256dh == nil && o.Auth == [16]byte{}
}
if k.Auth != o.Auth {
return false
}
if k.P256dh == nil || o.P256dh == nil {
return k.P256dh == nil && o.P256dh == nil
}
return k.P256dh.Equal(o.P256dh)
}
var _ json.Marshaler = (*Keys)(nil)
var _ json.Unmarshaler = (*Keys)(nil)
type marshaledKeys struct {
Auth string `json:"auth"`
P256dh string `json:"p256dh"`
}
// MarshalJSON implements json.Marshaler, allowing serialization to JSON.
func (k *Keys) MarshalJSON() ([]byte, error) {
if k == nil {
return nil, fmt.Errorf("keys are nil")
}
if k.P256dh == nil {
return nil, fmt.Errorf("keys.p256dh is nil")
}
m := marshaledKeys{
Auth: base64.RawStdEncoding.EncodeToString(k.Auth[:]),
P256dh: base64.RawStdEncoding.EncodeToString(k.P256dh.Bytes()),
}
return json.Marshal(&m)
}
// UnmarshalJSON implements json.Unmarshaler, allowing deserialization from JSON.
func (k *Keys) UnmarshalJSON(b []byte) (err error) {
var m marshaledKeys
if err := json.Unmarshal(b, &m); err != nil {
return err
}
authBytes, err := decodeSubscriptionKey(m.Auth)
if err != nil {
return err
}
if len(authBytes) != 16 {
return fmt.Errorf("invalid auth bytes length %d (must be 16)", len(authBytes))
}
copy(k.Auth[:], authBytes)
rawDHKey, err := decodeSubscriptionKey(m.P256dh)
if err != nil {
return err
}
k.P256dh, err = newECDHPublicKey(rawDHKey)
return err
}
// DecodeSubscriptionKeys decodes and validates a base64-encoded pair of subscription keys
// (the authentication secret and ECDH public key).
func DecodeSubscriptionKeys(auth, p256dh string) (keys Keys, err error) {
authBytes, err := decodeSubscriptionKey(auth)
if err != nil {
return
}
if len(authBytes) != 16 {
err = invalidAuthKeyLength
return
}
copy(keys.Auth[:], authBytes)
dhBytes, err := decodeSubscriptionKey(p256dh)
if err != nil {
return
}
keys.P256dh, err = newECDHPublicKey(dhBytes)
if err != nil {
return
}
return
}
// Subscription represents a PushSubscription object from the Push API.
type Subscription struct {
Endpoint string `json:"endpoint"`
Keys Keys `json:"keys"`
ExpirationTime *time.Time `json:"expirationTime"`
}
// SendNotification sends a push notification using the package default client.
func SendNotification(ctx context.Context, message []byte, s *Subscription, options *SendOptions) (*http.Response, error) {
var opts SendOptions
if options != nil {
opts = *options
}
result, err := defaultClient.Send(ctx, message, s, opts)
if result != nil {
return result.Response, err
}
return nil, err
}
// EncryptNotification implements the encryption algorithm specified by RFC 8291 for web push
// using RFC 8188 aes128gcm content encoding.
func EncryptNotification(message []byte, keys Keys, recordSize uint32) ([]byte, error) {
body, _, err := encryptNotificationRecords(message, keys, recordSize)
return body, err
}
func encryptNotificationRecords(message []byte, keys Keys, recordSize uint32) ([]byte, int, error) {
if recordSize == 0 {
recordSize = MaxRecordSize
}
if recordSize < 18 {
return nil, 0, ErrRecordSizeTooSmall
}
if keys.P256dh == nil {
return nil, 0, fmt.Errorf("invalid subscription: missing keys.p256dh")
}
localPrivateKey, err := generateECDHPrivateKey()
if err != nil {
return nil, 0, err
}
localPublicKey := localPrivateKey.PublicKey()
localPublicKeyBytes := localPublicKey.Bytes()
salt := make([]byte, 16)
if _, err := randomRead(salt); err != nil {
return nil, 0, err
}
sharedECDHSecret, err := localPrivateKey.ECDH(keys.P256dh)
if err != nil {
return nil, 0, fmt.Errorf("deriving shared secret: %w", err)
}
prkInfoBuf := bytes.NewBuffer([]byte("WebPush: info\x00"))
prkInfoBuf.Write(keys.P256dh.Bytes())
prkInfoBuf.Write(localPublicKeyBytes)
ikm, err := hkdfKey(sha256.New, sharedECDHSecret, keys.Auth[:], prkInfoBuf.String(), 32)
if err != nil {
return nil, 0, fmt.Errorf("deriving ikm: %w", err)
}
contentEncryptionKeyInfo := "Content-Encoding: aes128gcm\x00"
contentEncryptionKey, err := hkdfKey(sha256.New, ikm, salt, contentEncryptionKeyInfo, 16)
if err != nil {
return nil, 0, fmt.Errorf("deriving content encryption key: %w", err)
}
nonceInfo := "Content-Encoding: nonce\x00"
baseNonce, err := hkdfKey(sha256.New, ikm, salt, nonceInfo, 12)
if err != nil {
return nil, 0, fmt.Errorf("deriving nonce: %w", err)
}
c, err := newAESCipher(contentEncryptionKey)
if err != nil {
return nil, 0, err
}
gcm, err := newGCM(c)
if err != nil {
return nil, 0, err
}
header := make([]byte, 16+4+1+len(localPublicKeyBytes))
copy(header[:16], salt)
binary.BigEndian.PutUint32(header[16:20], recordSize)
header[20] = byte(len(localPublicKeyBytes))
copy(header[21:], localPublicKeyBytes)
maxChunk := int(recordSize) - 17
if maxChunk <= 0 {
return nil, 0, ErrRecordSizeTooSmall
}
totalRecords := 1
if len(message) > 0 {
totalRecords = (len(message) + maxChunk - 1) / maxChunk
}
buf := bytes.NewBuffer(make([]byte, 0, len(header)+len(message)+totalRecords*17))
buf.Write(header)
offset := 0
for recordIndex := 0; recordIndex < totalRecords; recordIndex++ {
end := offset + maxChunk
if end > len(message) {
end = len(message)
}
delimiter := byte(1)
if recordIndex == totalRecords-1 {
delimiter = 2
}
plaintext := make([]byte, end-offset+1)
copy(plaintext, message[offset:end])
plaintext[len(plaintext)-1] = delimiter
nonce := deriveRecordNonce(baseNonce, uint64(recordIndex))
ciphertext := gcm.Seal(nil, nonce, plaintext, nil)
buf.Write(ciphertext)
offset = end
}
return buf.Bytes(), totalRecords, nil
}
func deriveRecordNonce(baseNonce []byte, recordIndex uint64) []byte {
nonce := make([]byte, len(baseNonce))
copy(nonce, baseNonce)
var sequence [12]byte
binary.BigEndian.PutUint64(sequence[4:], recordIndex)
for i := range nonce {
nonce[i] ^= sequence[i]
}
return nonce
}
func validateTopic(topic string) error {
if topic == "" {
return nil
}
if len(topic) > 32 {
return fmt.Errorf("%w: must be 32 characters or fewer", ErrInvalidTopic)
}
for _, r := range topic {
if r > 127 || !isTopicChar(byte(r)) {
return fmt.Errorf("%w: contains invalid character %q", ErrInvalidTopic, r)
}
}
return nil
}
func isTopicChar(ch byte) bool {
switch ch {
case '!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '^', '_', '`', '|', '~':
return true
}
return ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z'
}
// decodeSubscriptionKey decodes a base64 subscription key.
func decodeSubscriptionKey(key string) ([]byte, error) {
key = strings.TrimRight(key, "=")
if strings.IndexByte(key, '+') != -1 || strings.IndexByte(key, '/') != -1 {
return base64.RawStdEncoding.DecodeString(key)
}
return base64.RawURLEncoding.DecodeString(key)
}