/* Web Push from pic straight to a phone's push service (docs/design/remote-protocol.md, section
* 5): a VAPID token (RFC 8292, ES256 over P-256), the payload encrypted to the subscription
* (RFC 8291 with RFC 8188's aes128gcm, one record), and one POST through pic_http_post whose
* status is sorted into what the caller does next. The pieces below the send are pure functions
* over caller buffers, so the protocol's vectors test them; the send takes the platform's entropy
* for each push's key pair and salt, and its clock for the token's expiry. Secrets are wiped
* after use. */
#ifndef PIC_WEBPUSH_H
#define PIC_WEBPUSH_H
#include <stddef.h>
#include <stdint.h>
#include "net.h"
#define PIC_WEBPUSH_PUB 65 /* 0x04 || X || Y */
#define PIC_WEBPUSH_PRIV 32
#define PIC_WEBPUSH_AUTH 16
#define PIC_WEBPUSH_SALT 16
#define PIC_WEBPUSH_OVERHEAD 103 /* salt, record size, key length, key, delimiter, tag */
#define PIC_WEBPUSH_MAX 3993 /* plaintext bytes that fit one 4096-byte record */
#define PIC_WEBPUSH_TOPIC 23 /* 22 base64url characters and the NUL */
#define PIC_WEBPUSH_EXPIRY (12 * 3600)
/* A subscription, as the page's pushManager gave it. */
typedef struct {
const char *endpoint;
unsigned char p256dh[PIC_WEBPUSH_PUB];
unsigned char auth[PIC_WEBPUSH_AUTH];
} pic_webpush_sub;
/* pic's VAPID key, kept by the caller, and the token's sub claim (an https: origin). */
typedef struct {
unsigned char priv[PIC_WEBPUSH_PRIV];
const char *subject;
} pic_webpush_vapid;
typedef enum {
PIC_WEBPUSH_SENT, /* 201 */
PIC_WEBPUSH_GONE, /* 404 or 410: drop the subscription */
PIC_WEBPUSH_TOO_LARGE, /* 413, or a payload over PIC_WEBPUSH_MAX (status 0) */
PIC_WEBPUSH_RETRY, /* 429: retry_after holds the seconds the service asked for */
PIC_WEBPUSH_FAILED /* any other status, or no response (status 0) */
} pic_webpush_outcome;
typedef struct {
pic_webpush_outcome outcome;
int status; /* the response's, or 0 when none arrived */
long retry_after; /* seconds, or -1 when absent or not a number of seconds */
char err[160];
} pic_webpush_result;
/* Encrypts PAYLOAD to SUB and POSTs it with a token from VAPID, TTL seconds and TOPIC (NULL for
* none; at most 32 base64url characters). The outcome is always set; 0 when SENT, else -1. The
* endpoint is used as given (pic_http_post allows https, and http only to loopback): check it
* with pic_webpush_endpoint_ok when it arrives. */
int pic_webpush_send(pic_net *net, const pic_webpush_sub *sub, const pic_webpush_vapid *vapid, const char *topic,
const void *payload, size_t len, int ttl, pic_webpush_result *res);
/* The section 5 check of a subscription's endpoint: https, port 443, a host name and not an
* address, no user part. 1 or 0. */
int pic_webpush_endpoint_ok(const char *endpoint);
/* The origin of an http or https URL ("https://host" or "https://host:port", the scheme's default
* port left out), the token's aud. Its length, or -1 when URL has none or CAP is too small. */
int pic_webpush_origin(const char *url, char *out, size_t cap);
/* The public key of a P-256 private key. 0, or -1 when PRIV is not a valid scalar. */
int pic_webpush_public(const unsigned char priv[PIC_WEBPUSH_PRIV], unsigned char pub[PIC_WEBPUSH_PUB]);
/* The Authorization header's value, "vapid t=JWT, k=KEY", for aud AUD, exp EXP (Unix seconds)
* and sub SUBJECT. The JWT's header is {"typ":"JWT","alg":"ES256"}, its claims
* {"aud":AUD,"exp":EXP,"sub":SUBJECT}, its signature r || s, deterministic (RFC 6979). AUD and
* SUBJECT may hold no character JSON would escape. Its length, or -1. */
int pic_webpush_authorization(const unsigned char priv[PIC_WEBPUSH_PRIV], const char *aud, int64_t exp,
const char *subject, char *out, size_t cap);
/* RFC 8291's body for one push: the ephemeral private key AS_PRIV, SALT, the subscription's
* UA_PUB and AUTH. OUT holds LEN + PIC_WEBPUSH_OVERHEAD bytes. Its length, or 0 when LEN is over
* PIC_WEBPUSH_MAX or a key is not valid. */
size_t pic_webpush_encrypt(const unsigned char as_priv[PIC_WEBPUSH_PRIV], const unsigned char salt[PIC_WEBPUSH_SALT],
const unsigned char ua_pub[PIC_WEBPUSH_PUB], const unsigned char auth[PIC_WEBPUSH_AUTH],
const void *pt, size_t len, unsigned char *out);
/* The Topic for a conversation: b64u(trunc16(HMAC(auth, "pic-rc/1 topic" || 0x00 || SESSION))). */
void pic_webpush_topic(const unsigned char auth[PIC_WEBPUSH_AUTH], const char *session, char out[PIC_WEBPUSH_TOPIC]);
#endif