Skip to content
suit Artifact [7ded39d4fc]
Login

Artifact [7ded39d4fc]

Artifact 7ded39d4fc71086e2f3e74ab875dee02f8f083e8c167b6b57ad83dec5c660681:


#ifndef SUIT_ANCHORS_H
#define SUIT_ANCHORS_H

#include <bearssl.h>
#include <stddef.h>

// The certificates an https transfer trusts, read at run time from a PEM file:
// a remote's own CA file, or else the system's bundle. Nothing is compiled in.

constexpr size_t ANCHORS_FILE_MAX = 1u << 20;   // bytes of PEM
constexpr size_t ANCHORS_MAX = 512;             // certificates kept
constexpr size_t ANCHORS_DER_MAX = 16384;       // bytes of one certificate

typedef struct {
    size_t n;        // trust anchors in ta
    size_t skipped;  // certificates BearSSL cannot use, such as an unsupported key type
    br_x509_trust_anchor ta[ANCHORS_MAX];
    // The anchors' names and keys. Each is a part of its certificate, and the
    // certificates are shorter than the PEM text they came from, so all of
    // them fit in as many bytes as the file may have.
    size_t used;
    unsigned char arena[ANCHORS_FILE_MAX];
} Anchors;

// Decode LEN bytes of PEM. Objects other than CERTIFICATE are ignored. On
// failure A is empty and WHY says what is wrong. The anchors point into A's
// own memory, so a check of one file never disturbs another's anchors.
[[nodiscard]] bool anchors_parse(Anchors *a, const unsigned char *data, size_t len, char *why, size_t cap);

// Read CA_FILE when it is not null; else the first that exists of
// $SSL_CERT_FILE and the system's usual bundles. PATH gets the file used.
// False, with WHY, when there is none, it cannot be read, it is not PEM, or
// it holds no certificate BearSSL can use, and when it cannot be trusted: a
// file that is not regular, belongs to someone other than you or root, or
// that group or others can write, or one in a folder that fails the same
// test. CA_FILE may not be a symbolic link; the bundles may, since systems
// ship them as links.
[[nodiscard]] bool anchors_load(Anchors *a, const char *ca_file, char *path, size_t pcap, char *why, size_t cap);

// Nullptr if PATH may be recorded as a remote's CA file, else why not.
const char *anchors_path_check(const char *path);

#endif