hanki

webpush

stdlib/extra/webpush.hk: the encrypted payload of one Web Push message.

RFC 8291 seals a push message for one subscription: P-256 ECDH against the user agent's public key, HKDF-SHA-256 over the two secrets, and the RFC 8188 aes128gcm content coding for the body. A push service relays the body and cannot read it, and the browser with the subscription's private key and authentication secret is the only party that can open it.

The protocol fixes every algorithm, which is what makes one function enough: encrypt! seals under AES-128-GCM with a key derived as RFC 8291 section 3.4 says, and no caller picks a cipher. extra/aead remains XChaCha20-Poly1305 alone for the same reason from the other side, and a browser cannot open an aead ciphertext.

The native work is seam members that are pure functions of their inputs: sys.p256_ecdh and the two HKDF calls come through extra/pubkey, and sys.aes_128_gcm_seal is AES-128-GCM's one appearance in the tree. encrypt! charges [random] alone, for the ECDH key pair and the salt the body begins with.

Using it

A subscription gives an endpoint, a p256dh public key and an auth secret (RFC 8030, RFC 8291 section 2.1). Seal the body, then send it to the endpoint with Content-Encoding: aes128gcm and the TTL header:

use pubkey
use webpush

def seal_for!(p256dh: bytes, auth: bytes, message: bytes) -> Result<bytes, string> [random]
  match pubkey.p256_public(p256dh)
    Ok(subscriber) -> webpush.encrypt!(subscriber, auth, message).map_err(|e| e.to_string())
    Err(e) -> Err("the subscription key: #{e}")
  end
end

The caller maps both refusals because they are different facts about the subscription. encrypt! takes a key pubkey has already validated, which leaves a p256dh that does not decode outside this module.

authsecret_length

_auth_secret_length: int = 16

Bytes a subscription's authentication secret is.

saltlength

_salt_length: i32 = 16

Bytes in the salt an aes128gcm body leads with.

recordsize

_record_size: int = 4096

The record size an aes128gcm body declares: RFC 8030's 4096-octet message cap, which leaves one record enough for a whole message.

headerlength

_header_length: int = 86

Bytes the body's header takes: the salt, the record size, the key id's length byte and the 65-byte key id.

taglength

_tag_length: int = 16

Bytes of tag one record appends.

maxpayload

_max_payload: int = _record_size - _header_length - _tag_length - 1

The largest payload that fits RFC 8030's 4096-octet message, which is the whole body: its header, the payload, the padding delimiter and the tag. RFC 8291 section 4 works the same bound out, and the record size must also exceed the padded plaintext and the tag together.

WebPushError

type WebPushError
  BadAuthLength(int)
  PayloadTooLarge(int)
  BadSaltLength(int)
  BadDerivation
end

Why a payload could not be sealed. Every variant names a fact about the subscription or the message, and an application discards a subscription whose key or secret is wrong in place of retrying it.

impl Eq<WebPushError>

eq?

def eq?(self, other: Self) -> bool

Structural equality; the length errors compare the length they carry.

(BadAuthLength(5) == BadAuthLength(5)) => true
(BadAuthLength(5) == BadAuthLength(6)) => false
(BadAuthLength(5) == BadSaltLength(5)) => false

impl Display<WebPushError>

to_string

def to_string(self) -> string

Lowercase and without a trailing stop, matching pubkey.PubkeyError's messages.

BadAuthLength(5).to_string() => "a web push authentication secret is 16 bytes, got 5"
PayloadTooLarge(5000).to_string() => "a web push payload is at most 3993 bytes, got 5000"

encrypt!

def encrypt!(subscriber: pubkey.P256Public, auth: bytes, payload: bytes) -> Result<bytes, WebPushError> [random]

Seal payload for subscriber, returning the RFC 8188 aes128gcm body RFC 8291 specifies, ready to send to the subscription's endpoint with Content-Encoding: aes128gcm.

auth is the subscription's authentication secret, 16 bytes. The application server's ECDH key pair and the body's salt are drawn here and discarded with the call, which makes the result a function of the subscription, the payload and the [random] stream.

p = pubkey.p256_public(hex.decode("04fe33f4ab0dea71914db55823f73b54948f41306d920732dbb9a59a53286482200e597a7b7bc260ba1c227998580992e93973002f3012a28ae8f06bbb78e5ec0f").unwrap_or("".to_bytes())).or_crash!("the RFC 8291 subscription key parses")
auth = hex.decode("05305932a1c7eabe13b6cec9fda48882").unwrap_or("".to_bytes())
encrypt!(p, auth, "hello".to_bytes()).map(|body| body.length).unwrap_or(0) => 108
encrypt!(p, "short".to_bytes(), "hello".to_bytes()) => Err(BadAuthLength(5))

_seal

def _seal(subscriber: pubkey.P256Public, auth: bytes, payload: bytes, salt: bytes, secret: pubkey.P256Secret) -> Result<bytes, WebPushError>

The body for one subscription under an explicit salt and application-server key. encrypt! draws both; this is where RFC 8291 section 3.4's derivation runs, and the section 5 example reaches it with the key pair and salt that appendix publishes. It is private: a caller supplying its own salt or key pair is a caller reusing one, which the protocol forbids.

keyinfo

def _key_info(subscriber: bytes, key_id: bytes) -> bytes

The info string RFC 8291 section 3.3 names key_info: the label, a zero octet, the user agent's public key and the application server's.

cekinfo

def _cek_info() -> bytes

The content-encryption key's info string, RFC 8291 section 3.4.

nonceinfo

def _nonce_info() -> bytes

The nonce's info string, RFC 8291 section 3.4.

_header

def _header(salt: bytes, key_id: bytes) -> bytes

The body's header, RFC 8188 section 2.1: the salt, the big-endian record size, the key id's length and the key id.

recordsize_bytes

def _record_size_bytes() -> bytes

The record size as the four big-endian bytes the header ends its key-id length with. 4096 is 0x00001000.

_padded

def _padded(payload: bytes) -> bytes

payload with the padding delimiter that ends the message's last record.

_bytes

def _bytes(text: string) -> bytes

hex.decode answered as bytes: the test vectors are published in hex, and a Result in a test assertion reads worse than the decode it wraps.