Exemple #1
0
void *
d2i_fp_max(const ASN1_ITEM *it, FILE *fp, void *x, unsigned int max)
{
    AUTO(BIO, bio);

    bio = BIO_new_fp(fp, BIO_NOCLOSE);
    if (bio == NULL)
        return NULL;

    return d2i_bio_max(it, bio, x, max);
}
Exemple #2
0
static int
decryptd(int argc, char *argv[])
{
    int ret = EXIT_FAILURE;
    AUTO(DEO_MSG, in);
    AUTO(BIO, sio);
    AUTO(ctx, ctx);

    ctx = load_ctx(argc, argv);
    if (ctx == NULL)
        goto error;

    sio = start_tls(ctx->ctx);
    if (sio == NULL)
        goto error;

    in = d2i_bio_max(&DEO_MSG_it, sio, NULL, DEO_MAX_INPUT);
    if (in == NULL)
        goto error;

    switch (in->type) {
    case DEO_MSG_TYPE_CRT_REQ:
        ASN1_item_i2d_bio(&DEO_MSG_it, sio, &(DEO_MSG) {
            .type = DEO_MSG_TYPE_CRT_REP,
            .value.crt_rep = ctx->crt
        });
        break;

    case DEO_MSG_TYPE_DEC_REQ: {
        DEO_ERR err = DEO_ERR_NONE;
        AUTO(ASN1_OCTET_STRING, pt);

        err = decrypt(ctx, in->value.dec_req, &pt);
        if (err != DEO_ERR_NONE) {
            SEND_ERR(sio, err);
            break;
        }

        ASN1_item_i2d_bio(&DEO_MSG_it, sio, &(DEO_MSG) {
            .type = DEO_MSG_TYPE_DEC_REP,
            .value.dec_rep = pt
        });
        break;
    }
Exemple #3
0
static int
decryptd(int argc, char *argv[])
{
    const char *hp = DEO_SOCKET;
    const char *tlsfile = NULL;
    const char *encfile = NULL;
    const char *decdir = NULL;
    int ret = EXIT_FAILURE;
    AUTO(ctx, ctx);
    int lfds = 0;
    int sock = 0;

    signal(SIGINT, on_signal);
    signal(SIGQUIT, on_signal);
    signal(SIGTERM, on_signal);
    signal(SIGUSR1, on_signal);
    signal(SIGUSR2, on_signal);

    if (!deo_getopt(argc, argv, "ht:e:d:l:", "", NULL, NULL,
                       option, &tlsfile, option, &encfile,
                       option, &decdir, option, &hp)
        || tlsfile == NULL || encfile == NULL || decdir == NULL
        || (ctx = ctx_init(tlsfile, encfile, decdir)) == NULL) {
        ERR_print_errors_fp(stderr);
        fprintf(stderr, "Usage: deo decryptd "
                        "[-l <[host:]port>] -t <tlsfile> "
                        "-e <encfile> -d <decdir>\n");
        return EXIT_FAILURE;
    }

    lfds = sd_listen_fds(0);
    if (lfds <= 0) {
        sock = BIO_get_accept_socket((char *) hp, 0);
        if (sock < 0) {
            ERR_print_errors_fp(stderr);
            goto error;
        }

        if (listen(sock, 64) != 0)
            goto error;
    }

    while (true) {
        DEO_ERR err = DEO_ERR_NONE;
        AUTO(ASN1_OCTET_STRING, pt);
        AUTO(DEO_MSG, in);
        AUTO(BIO, sio);
        AUTO_FD(cfd);
        int lfd;

        if (!have_conn(lfds, sock, &lfd))
            break;

        if (!do_accept(ctx->ctx, lfd, &cfd, &sio))
            continue;

        in = d2i_bio_max(&DEO_MSG_it, sio, NULL, DEO_MAX_INPUT);
        if (in == NULL)
            continue;

        switch (in->type) {
        case DEO_MSG_TYPE_CRT_REQ:
            ASN1_item_i2d_bio(&DEO_MSG_it, sio, &(DEO_MSG) {
                .type = DEO_MSG_TYPE_CRT_REP,
                .value.crt_rep = ctx->crt
            });
            break;

        case DEO_MSG_TYPE_DEC_REQ:
            err = decrypt(ctx, in->value.dec_req, &pt);
            if (err != DEO_ERR_NONE) {
                SEND_ERR(sio, err);
                break;
            }

            ASN1_item_i2d_bio(&DEO_MSG_it, sio, &(DEO_MSG) {
                .type = DEO_MSG_TYPE_DEC_REP,
                .value.dec_rep = pt
            });
            break;

        default:
            break;
        }