- Search can be narrowed by space, author and last change; the index records a mapping version and rebuilds when it changes. - Editors can pin up to 12 pages to a space's home. - Find and replace in the editor (Ctrl+F), with match case. - Pages can be set to full width and a serif or mono font. - Tailwind now scans the editor package, so its classes are generated. - Fix golangci-lint findings, including push device names that were saved before being trimmed and capped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
119 lines
3.2 KiB
Go
119 lines
3.2 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package push
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/ecdh"
|
|
"crypto/hkdf"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/binary"
|
|
"errors"
|
|
)
|
|
|
|
// Web Push message encryption (RFC 8291, "aes128gcm" from RFC 8188), which
|
|
// UnifiedPush uses: only the app, which holds the private key, can read a
|
|
// message, so the distributor's server (ntfy.sh, …) sees nothing but its
|
|
// size. The app drops messages that don't decrypt, so knowing an endpoint
|
|
// is not enough to send someone a fake notification.
|
|
|
|
// recordSize is the RFC 8188 record size; a message fits in one record.
|
|
const recordSize = 4096
|
|
|
|
// decodeKey reads a base64url key (padded or not).
|
|
func decodeKey(s string) ([]byte, error) {
|
|
if b, err := base64.RawURLEncoding.DecodeString(s); err == nil {
|
|
return b, nil
|
|
}
|
|
return base64.URLEncoding.DecodeString(s)
|
|
}
|
|
|
|
// checkKeys validates a phone's public key (uncompressed P-256) and auth secret.
|
|
func checkKeys(p256dh, auth string) error {
|
|
pub, err := decodeKey(p256dh)
|
|
if err != nil {
|
|
return errors.New("bad public key")
|
|
}
|
|
if _, err := ecdh.P256().NewPublicKey(pub); err != nil {
|
|
return errors.New("bad public key")
|
|
}
|
|
secret, err := decodeKey(auth)
|
|
if err != nil || len(secret) != 16 {
|
|
return errors.New("bad auth secret")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// encrypt seals plaintext for the phone with public key p256dh and auth
|
|
// secret auth, returning the aes128gcm body.
|
|
func encrypt(plaintext []byte, p256dh, auth string) ([]byte, error) {
|
|
uaPub, err := decodeKey(p256dh)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
secret, err := decodeKey(auth)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ua, err := ecdh.P256().NewPublicKey(uaPub)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
as, err := ecdh.P256().GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
shared, err := as.ECDH(ua)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
asPub := as.PublicKey().Bytes()
|
|
|
|
// RFC 8291 section 3.4: mix the auth secret and both public keys in.
|
|
keyInfo := append(append([]byte("WebPush: info\x00"), uaPub...), asPub...)
|
|
ikm, err := hkdf.Key(sha256.New, shared, secret, string(keyInfo), 32)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
salt := make([]byte, 16)
|
|
if _, err := rand.Read(salt); err != nil {
|
|
return nil, err
|
|
}
|
|
cek, err := hkdf.Key(sha256.New, ikm, salt, "Content-Encoding: aes128gcm\x00", 16)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
nonce, err := hkdf.Key(sha256.New, ikm, salt, "Content-Encoding: nonce\x00", 12)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
block, err := aes.NewCipher(cek)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(plaintext)+1+gcm.Overhead() > recordSize {
|
|
return nil, errors.New("message too long")
|
|
}
|
|
// One record, so it is the last: padding delimiter 0x02.
|
|
sealed := gcm.Seal(nil, nonce, append(plaintext, 2), nil)
|
|
|
|
// An uncompressed P-256 point, the only kind ECDH hands out.
|
|
const keyLen = 65
|
|
if len(asPub) != keyLen {
|
|
return nil, errors.New("webpush: unexpected server key length")
|
|
}
|
|
header := make([]byte, 0, 16+4+1+len(asPub))
|
|
header = append(header, salt...)
|
|
header = binary.BigEndian.AppendUint32(header, recordSize)
|
|
header = append(header, keyLen)
|
|
header = append(header, asPub...)
|
|
return append(header, sealed...), nil
|
|
}
|