Files
kryon/include/ksync_sync.h
T
waozi c670587788 ksync: harden crypto, exports, JSON, delete, and sync transport
- Add shared ksync_crypto module: SHA-256, HMAC-SHA256, PBKDF2-SHA256,
  ChaCha20-Poly1305 (RFC 8439), hex helpers, random bytes; all verified
  against RFC test vectors in tests/ksync_crypto_test.c
- Encrypted account exports (v2): PBKDF2 (600k iterations) +
  ChaCha20-Poly1305, passphrase-protected import/export APIs; the v1
  plaintext format remains importable
- DeleteKsyncSyncAccount no longer transmits the private key: it signs
  a challenge against /api/v1/account/delete and falls back to the
  legacy delete-with-key endpoint on 404/405
- JSON writer escapes control characters (including \b, \f, \uXXXX);
  reader now decodes escapes, rejects substring keys and keys embedded
  in string values
- Token expiry accounts for server clock skew (server_time in the
  login response, clamped to +/-24h)
- Optional retry with exponential backoff for transport errors, 429,
  and 5xx via retry_max/retry_delay_ms/sleep_ms config fields
- Opt-in end-to-end payload encryption (encrypt_payload flag plus
  Wrap/UnwrapKsyncSyncPayload) derived from the account private key
2026-08-16 19:54:53 -03:00

118 lines
5.3 KiB
C

#ifndef KSYNC_SYNC_H
#define KSYNC_SYNC_H
#include "ksync_account.h"
#include <stddef.h>
typedef enum KsyncSyncResult {
KSYNC_SYNC_OK = 0,
KSYNC_SYNC_INVALID_URL,
KSYNC_SYNC_NO_ACCOUNT,
KSYNC_SYNC_PAYLOAD_FAILED,
KSYNC_SYNC_CHALLENGE_FAILED,
KSYNC_SYNC_SIGN_FAILED,
KSYNC_SYNC_REQUEST_FAILED,
KSYNC_SYNC_AUTH_FAILED
} KsyncSyncResult;
typedef struct KsyncSyncBuffer {
char *data;
size_t len;
size_t cap;
} KsyncSyncBuffer;
typedef int (*KsyncSyncHttpRequestFn)(const char *method, const char *url,
const char *body,
const char *const *headers,
int header_count,
KsyncSyncBuffer *response,
long *status, void *user);
typedef const char *(*KsyncSyncGetTextFn)(const char *key, void *user);
typedef void (*KsyncSyncSetTextFn)(const char *key, const char *value, void *user);
typedef char *(*KsyncSyncBuildPayloadFn)(const char *user_id_hash,
const char *public_key_hex, void *user);
typedef void (*KsyncSyncFreePayloadFn)(char *payload, void *user);
typedef int (*KsyncSyncApplyResponseFn)(const char *response_json, void *user);
typedef void (*KsyncSyncVoidFn)(void *user);
typedef void (*KsyncSyncLogFn)(const char *step, long status,
const char *response, void *user);
typedef struct KsyncSyncConfig {
const char *base_url;
const KsyncAccount *account;
const char *client_id;
const char *signature_context;
const char *user_header_name;
const char *signature_header_name;
KsyncSyncHttpRequestFn http_request;
KsyncSyncGetTextFn get_text;
KsyncSyncSetTextFn set_text;
KsyncSyncBuildPayloadFn build_payload;
KsyncSyncFreePayloadFn free_payload;
KsyncSyncApplyResponseFn apply_response;
KsyncSyncVoidFn purge_synced_deleted;
KsyncSyncLogFn log_http_failure;
/* Retry policy for transient failures (transport errors, 429, 5xx).
* Retries only happen when sleep_ms is provided; without it the single
* attempt behaviour is unchanged. Delay grows exponentially:
* retry_delay_ms << attempt, capped at 30s. */
int retry_max;
int retry_delay_ms;
void (*sleep_ms)(int ms, void *user);
/* When set, RunKsyncSync encrypts the outgoing payload with a key
* derived from the account and decrypts envelope responses before
* apply_response (server sees opaque ciphertext). */
int encrypt_payload;
void *user;
} KsyncSyncConfig;
const char *GetKsyncSyncResultName(KsyncSyncResult result);
int IsKsyncSyncURLValid(const char *url);
int NormalizeKsyncSyncURL(const char *input, char *out, size_t out_size);
int JoinKsyncSyncURL(char *out, size_t out_size,
const char *base_url, const char *path);
int JoinKsyncSyncWebSocketURL(char *out, size_t out_size,
const char *base_url, const char *path);
int AppendKsyncSyncBuffer(KsyncSyncBuffer *buffer,
const void *data, size_t bytes);
int AppendKsyncSyncBufferJSONString(KsyncSyncBuffer *buffer,
const char *text);
void FreeKsyncSyncBuffer(KsyncSyncBuffer *buffer);
int FindKsyncSyncJSONString(const char *json, const char *key,
char *out, size_t out_size);
long long FindKsyncSyncJSONInt64(const char *json, const char *key,
long long fallback);
void ClearKsyncSyncAuthToken(const KsyncSyncConfig *cfg);
KsyncSyncResult LoginKsyncSync(const KsyncSyncConfig *cfg);
KsyncSyncResult RunKsyncSync(const KsyncSyncConfig *cfg);
KsyncSyncResult RequestKsyncSyncBearer(const KsyncSyncConfig *cfg,
const char *method,
const char *path,
const char *body,
char *out,
size_t out_size);
KsyncSyncResult DeleteKsyncSyncAccount(const KsyncSyncConfig *cfg);
/* Opt-in end-to-end payload encryption: envelope JSON
* {"v":1,"nonce":"hex","ciphertext":"hex"} sealed with ChaCha20-Poly1305
* under a key derived from the account private key. Unwrap returns 0 for
* input that is not an envelope. Caller frees *out. */
int WrapKsyncSyncPayload(const KsyncAccount *account, const char *payload, char **out);
int UnwrapKsyncSyncPayload(const KsyncAccount *account, const char *envelope_json, char **out);
int KsyncDefaultHttpRequest(const char *method, const char *url,
const char *body,
const char *const *headers,
int header_count,
KsyncSyncBuffer *response,
long *status, void *user);
KsyncSyncResult KsyncRemoteEventWait(const KsyncSyncConfig *cfg,
const char *path);
#if defined(__EMSCRIPTEN__)
int KsyncWebSyncStart(const KsyncSyncConfig *cfg);
int KsyncWebSyncPoll(KsyncSyncResult *result, int *changed);
int KsyncWebRemoteEventsStart(const KsyncSyncConfig *cfg, const char *path);
int KsyncWebRemoteEventsPoll(void);
#endif
#endif