Skip to content
pi-c Artifact [5f9769c3a2]
Login

Artifact [5f9769c3a2]

Artifact 5f9769c3a27fb2f03b93c2d588e4cffcad4a17d8f0aee6ca4a7ef4564cccdc5b:


/* Web Push from pic straight to a phone's push service (docs/design/remote-protocol.md, section
 * 5): the VAPID token (RFC 8292), the payload encrypted to the subscription, and one POST through
 * pic_http_post whose status is sorted into what the caller does next. The cryptography is
 * noise.c's (pic_noise_es256, pic_noise_webpush, the key pairs); this file assembles the token
 * and the request. 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_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 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 (PRIV not a valid key among
 * the reasons). */
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);

/* 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