Blame apps/rand.c

Packit c4476c
/*
Packit c4476c
 * Copyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
 *
Packit c4476c
 * Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
 * this file except in compliance with the License.  You can obtain a copy
Packit c4476c
 * in the file LICENSE in the source distribution or at
Packit c4476c
 * https://www.openssl.org/source/license.html
Packit c4476c
 */
Packit c4476c
Packit c4476c
#include "apps.h"
Packit c4476c
#include "progs.h"
Packit c4476c
Packit c4476c
#include <ctype.h>
Packit c4476c
#include <stdio.h>
Packit c4476c
#include <string.h>
Packit c4476c
Packit c4476c
#include <openssl/bio.h>
Packit c4476c
#include <openssl/err.h>
Packit c4476c
#include <openssl/rand.h>
Packit c4476c
Packit c4476c
typedef enum OPTION_choice {
Packit c4476c
    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
Packit c4476c
    OPT_OUT, OPT_ENGINE, OPT_BASE64, OPT_HEX,
Packit c4476c
    OPT_R_ENUM
Packit c4476c
} OPTION_CHOICE;
Packit c4476c
Packit c4476c
const OPTIONS rand_options[] = {
Packit c4476c
    {OPT_HELP_STR, 1, '-', "Usage: %s [flags] num\n"},
Packit c4476c
    {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
Packit c4476c
    {"help", OPT_HELP, '-', "Display this summary"},
Packit c4476c
    {"out", OPT_OUT, '>', "Output file"},
Packit c4476c
    OPT_R_OPTIONS,
Packit c4476c
    {"base64", OPT_BASE64, '-', "Base64 encode output"},
Packit c4476c
    {"hex", OPT_HEX, '-', "Hex encode output"},
Packit c4476c
#ifndef OPENSSL_NO_ENGINE
Packit c4476c
    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
Packit c4476c
#endif
Packit c4476c
    {NULL}
Packit c4476c
};
Packit c4476c
Packit c4476c
int rand_main(int argc, char **argv)
Packit c4476c
{
Packit c4476c
    ENGINE *e = NULL;
Packit c4476c
    BIO *out = NULL;
Packit c4476c
    char *outfile = NULL, *prog;
Packit c4476c
    OPTION_CHOICE o;
Packit c4476c
    int format = FORMAT_BINARY, i, num = -1, r, ret = 1;
Packit c4476c
Packit c4476c
    prog = opt_init(argc, argv, rand_options);
Packit c4476c
    while ((o = opt_next()) != OPT_EOF) {
Packit c4476c
        switch (o) {
Packit c4476c
        case OPT_EOF:
Packit c4476c
        case OPT_ERR:
Packit c4476c
 opthelp:
Packit c4476c
            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
Packit c4476c
            goto end;
Packit c4476c
        case OPT_HELP:
Packit c4476c
            opt_help(rand_options);
Packit c4476c
            ret = 0;
Packit c4476c
            goto end;
Packit c4476c
        case OPT_OUT:
Packit c4476c
            outfile = opt_arg();
Packit c4476c
            break;
Packit c4476c
        case OPT_ENGINE:
Packit c4476c
            e = setup_engine(opt_arg(), 0);
Packit c4476c
            break;
Packit c4476c
        case OPT_R_CASES:
Packit c4476c
            if (!opt_rand(o))
Packit c4476c
                goto end;
Packit c4476c
            break;
Packit c4476c
        case OPT_BASE64:
Packit c4476c
            format = FORMAT_BASE64;
Packit c4476c
            break;
Packit c4476c
        case OPT_HEX:
Packit c4476c
            format = FORMAT_TEXT;
Packit c4476c
            break;
Packit c4476c
        }
Packit c4476c
    }
Packit c4476c
    argc = opt_num_rest();
Packit c4476c
    argv = opt_rest();
Packit c4476c
    if (argc == 1) {
Packit c4476c
        if (!opt_int(argv[0], &num) || num <= 0)
Packit c4476c
            goto end;
Packit c4476c
    } else if (argc > 0) {
Packit c4476c
        BIO_printf(bio_err, "Extra arguments given.\n");
Packit c4476c
        goto opthelp;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    out = bio_open_default(outfile, 'w', format);
Packit c4476c
    if (out == NULL)
Packit c4476c
        goto end;
Packit c4476c
Packit c4476c
    if (format == FORMAT_BASE64) {
Packit c4476c
        BIO *b64 = BIO_new(BIO_f_base64());
Packit c4476c
        if (b64 == NULL)
Packit c4476c
            goto end;
Packit c4476c
        out = BIO_push(b64, out);
Packit c4476c
    }
Packit c4476c
Packit c4476c
    while (num > 0) {
Packit c4476c
        unsigned char buf[4096];
Packit c4476c
        int chunk;
Packit c4476c
Packit c4476c
        chunk = num;
Packit c4476c
        if (chunk > (int)sizeof(buf))
Packit c4476c
            chunk = sizeof(buf);
Packit c4476c
        r = RAND_bytes(buf, chunk);
Packit c4476c
        if (r <= 0)
Packit c4476c
            goto end;
Packit c4476c
        if (format != FORMAT_TEXT) {
Packit c4476c
            if (BIO_write(out, buf, chunk) != chunk)
Packit c4476c
                goto end;
Packit c4476c
        } else {
Packit c4476c
            for (i = 0; i < chunk; i++)
Packit c4476c
                if (BIO_printf(out, "%02x", buf[i]) != 2)
Packit c4476c
                    goto end;
Packit c4476c
        }
Packit c4476c
        num -= chunk;
Packit c4476c
    }
Packit c4476c
    if (format == FORMAT_TEXT)
Packit c4476c
        BIO_puts(out, "\n");
Packit c4476c
    if (BIO_flush(out) <= 0)
Packit c4476c
        goto end;
Packit c4476c
Packit c4476c
    ret = 0;
Packit c4476c
Packit c4476c
 end:
Packit c4476c
    if (ret != 0)
Packit c4476c
        ERR_print_errors(bio_err);
Packit c4476c
    release_engine(e);
Packit c4476c
    BIO_free_all(out);
Packit c4476c
    return ret;
Packit c4476c
}