Skip to content
pi-c Documentation
Login

Documentation

/* The host platform (see platform.h). */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <sys/random.h>

#include "platform.h"

static int64_t host_wall_ns(void) {
    struct timespec ts;
    if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
        return -1;
    }
    return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
}

static uint64_t host_mono_ns(void) {
    struct timespec ts;
    (void)clock_gettime(CLOCK_MONOTONIC, &ts);
    return (uint64_t)ts.tv_sec * 1000000000u + (uint64_t)ts.tv_nsec;
}

static int host_entropy(void *buf, size_t n) { return getentropy(buf, n); }

static int host_lock(const char *path) {
    int fd = open(path, O_RDWR | O_CREAT | O_CLOEXEC, 0600);
    if (fd < 0) {
        return -1;
    }
    if (flock(fd, LOCK_EX | LOCK_NB) != 0) {
        int e = errno;
        close(fd);
        errno = e;
        return -2;
    }
    return fd;
}

static void host_unlock(int handle) { close(handle); }

/* ---- host only: the terminal ---------------------------------------- */

long pic_tty_secret(const char *prompt, char *buf, size_t cap, char *err, size_t errcap) {
    int fd = open("/dev/tty", O_RDWR | O_NOCTTY | O_CLOEXEC);
    struct termios old, quiet;
    size_t n = 0;
    long rc = -1;
    if (fd < 0) {
        (void)snprintf(err, errcap, "no terminal to ask on");
        return -1;
    }
    if (tcgetattr(fd, &old) != 0) {
        (void)snprintf(err, errcap, "no terminal to ask on");
        close(fd);
        return -1;
    }
    quiet = old;
    quiet.c_lflag &= ~(tcflag_t)(ECHO | ECHOE | ECHOK | ECHONL);
    quiet.c_lflag |= ICANON;
    /* Echo off before the prompt shows, so nothing typed after it can echo;
     * input typed before it is dropped. */
    if (tcsetattr(fd, TCSAFLUSH, &quiet) != 0) {
        (void)snprintf(err, errcap, "cannot turn the terminal's echo off");
        close(fd);
        return -1;
    }
    (void)write(fd, prompt, strlen(prompt));
    for (;;) {
        char c;
        ssize_t got = read(fd, &c, 1);
        if (got < 0 && errno == EINTR) {
            continue;
        }
        if (got <= 0) {
            (void)snprintf(err, errcap, "no answer was typed");
            break;
        }
        if (c == '\n' || c == '\r') {
            buf[n] = '\0';
            rc = (long)n;
            break;
        }
        if (n + 1 >= cap) {
            (void)snprintf(err, errcap, "the answer is longer than %zu bytes", cap - 1);
            break;
        }
        buf[n++] = c;
    }
    (void)tcsetattr(fd, TCSAFLUSH, &old);
    (void)write(fd, "\n", 1);
    close(fd);
    if (rc < 0) {
        volatile char *v = buf;
        for (size_t i = 0; i < cap; i++) {
            v[i] = 0;
        }
    }
    return rc;
}

/* The passphrase, asked on the controlling terminal (twice for a new
 * store). A dev build (PIC_FAULTS) also takes it from PIC_TEST_PASSPHRASE so
 * the suite can run without a terminal; a release build never reads it. */
static int host_secret_key(int fresh, unsigned char *buf, size_t cap, size_t *len, int *stretch, char *err,
                           size_t errcap) {
    char again[512];
    long n;
    *stretch = 1;
#ifdef PIC_FAULTS
    {
        const char *t = getenv("PIC_TEST_PASSPHRASE");
        if (t) {
            size_t k = strlen(t);
            if (k == 0 || k > cap) {
                (void)snprintf(err, errcap, "PIC_TEST_PASSPHRASE is empty or too long");
                return -1;
            }
            memcpy(buf, t, k);
            *len = k;
            return 0;
        }
    }
#endif
    n = pic_tty_secret(fresh ? "New passphrase for pic's credential store: " : "Passphrase for pic's credential store: ",
                       (char *)buf, cap, err, errcap);
    if (n < 0) {
        return -1;
    }
    if (n == 0) {
        (void)snprintf(err, errcap, "the passphrase is empty");
        return -1;
    }
    if (fresh) {
        long m = pic_tty_secret("Same passphrase again: ", again, sizeof again, err, errcap);
        int same = m == n && memcmp(again, buf, (size_t)n) == 0;
        volatile char *v = again;
        for (size_t i = 0; i < sizeof again; i++) {
            v[i] = 0;
        }
        if (m < 0) {
            return -1;
        }
        if (!same) {
            (void)snprintf(err, errcap, "the two passphrases differ");
            return -1;
        }
    }
    *len = (size_t)n;
    return 0;
}

static const pic_platform host = {
    host_wall_ns, host_mono_ns, host_entropy, host_lock, host_unlock, pic_input_read_host, pic_input_list_host, NULL,
    host_secret_key,
};

static const pic_platform *current = &host;

const pic_platform *pic_platform_host(void) { return &host; }
const pic_platform *pic_platform_get(void) { return current; }
void pic_platform_set(const pic_platform *p) { current = p ? p : &host; }