2018-09-13 19:18:07 +00:00
|
|
|
/*
|
2023-03-01 04:21:31 +00:00
|
|
|
* Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2003-01-28 21:43:22 +00:00
|
|
|
*
|
2023-03-01 04:21:31 +00:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2018-09-13 19:18:07 +00:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2003-01-28 21:43:22 +00:00
|
|
|
*/
|
2000-04-13 06:33:22 +00:00
|
|
|
|
|
|
|
#include "apps.h"
|
2018-09-13 19:18:07 +00:00
|
|
|
#include "progs.h"
|
2000-04-13 06:33:22 +00:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
2018-09-13 19:18:07 +00:00
|
|
|
typedef enum OPTION_choice {
|
2023-03-01 04:21:31 +00:00
|
|
|
OPT_COMMON,
|
2018-09-13 19:18:07 +00:00
|
|
|
OPT_OUT, OPT_ENGINE, OPT_BASE64, OPT_HEX,
|
2023-03-01 04:21:31 +00:00
|
|
|
OPT_R_ENUM, OPT_PROV_ENUM
|
2018-09-13 19:18:07 +00:00
|
|
|
} OPTION_CHOICE;
|
|
|
|
|
|
|
|
const OPTIONS rand_options[] = {
|
2023-03-01 04:21:31 +00:00
|
|
|
{OPT_HELP_STR, 1, '-', "Usage: %s [options] num\n"},
|
|
|
|
|
|
|
|
OPT_SECTION("General"),
|
2018-09-13 19:18:07 +00:00
|
|
|
{"help", OPT_HELP, '-', "Display this summary"},
|
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
|
|
|
{"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
|
|
|
|
#endif
|
2023-03-01 04:21:31 +00:00
|
|
|
|
|
|
|
OPT_SECTION("Output"),
|
|
|
|
{"out", OPT_OUT, '>', "Output file"},
|
|
|
|
{"base64", OPT_BASE64, '-', "Base64 encode output"},
|
|
|
|
{"hex", OPT_HEX, '-', "Hex encode output"},
|
|
|
|
|
|
|
|
OPT_R_OPTIONS,
|
|
|
|
OPT_PROV_OPTIONS,
|
|
|
|
|
|
|
|
OPT_PARAMETERS(),
|
|
|
|
{"num", 0, 0, "Number of bytes to generate"},
|
2018-09-13 19:18:07 +00:00
|
|
|
{NULL}
|
|
|
|
};
|
2000-04-13 06:33:22 +00:00
|
|
|
|
2018-09-13 19:18:07 +00:00
|
|
|
int rand_main(int argc, char **argv)
|
2015-03-20 15:28:40 +00:00
|
|
|
{
|
2017-01-26 18:32:12 +00:00
|
|
|
ENGINE *e = NULL;
|
2018-09-13 19:18:07 +00:00
|
|
|
BIO *out = NULL;
|
|
|
|
char *outfile = NULL, *prog;
|
|
|
|
OPTION_CHOICE o;
|
|
|
|
int format = FORMAT_BINARY, i, num = -1, r, ret = 1;
|
|
|
|
|
|
|
|
prog = opt_init(argc, argv, rand_options);
|
|
|
|
while ((o = opt_next()) != OPT_EOF) {
|
|
|
|
switch (o) {
|
|
|
|
case OPT_EOF:
|
|
|
|
case OPT_ERR:
|
|
|
|
opthelp:
|
|
|
|
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
|
|
|
|
goto end;
|
|
|
|
case OPT_HELP:
|
|
|
|
opt_help(rand_options);
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
|
|
|
case OPT_OUT:
|
|
|
|
outfile = opt_arg();
|
|
|
|
break;
|
|
|
|
case OPT_ENGINE:
|
|
|
|
e = setup_engine(opt_arg(), 0);
|
|
|
|
break;
|
|
|
|
case OPT_R_CASES:
|
|
|
|
if (!opt_rand(o))
|
|
|
|
goto end;
|
|
|
|
break;
|
|
|
|
case OPT_BASE64:
|
|
|
|
format = FORMAT_BASE64;
|
|
|
|
break;
|
|
|
|
case OPT_HEX:
|
|
|
|
format = FORMAT_TEXT;
|
|
|
|
break;
|
2023-03-01 04:21:31 +00:00
|
|
|
case OPT_PROV_CASES:
|
|
|
|
if (!opt_provider(o))
|
|
|
|
goto end;
|
|
|
|
break;
|
2015-03-20 15:28:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-01 04:21:31 +00:00
|
|
|
|
|
|
|
/* Optional argument is number of bytes to generate. */
|
2018-09-13 19:18:07 +00:00
|
|
|
argc = opt_num_rest();
|
|
|
|
argv = opt_rest();
|
|
|
|
if (argc == 1) {
|
|
|
|
if (!opt_int(argv[0], &num) || num <= 0)
|
2023-03-01 04:21:31 +00:00
|
|
|
goto opthelp;
|
|
|
|
} else if (argc != 0) {
|
2018-09-13 19:18:07 +00:00
|
|
|
goto opthelp;
|
2015-03-20 15:28:40 +00:00
|
|
|
}
|
2003-01-28 21:43:22 +00:00
|
|
|
|
2023-03-01 04:21:31 +00:00
|
|
|
if (!app_RAND_load())
|
|
|
|
goto end;
|
|
|
|
|
2018-09-13 19:18:07 +00:00
|
|
|
out = bio_open_default(outfile, 'w', format);
|
2015-03-20 15:28:40 +00:00
|
|
|
if (out == NULL)
|
2018-09-13 19:18:07 +00:00
|
|
|
goto end;
|
2015-03-20 15:28:40 +00:00
|
|
|
|
2018-09-13 19:18:07 +00:00
|
|
|
if (format == FORMAT_BASE64) {
|
2015-03-20 15:28:40 +00:00
|
|
|
BIO *b64 = BIO_new(BIO_f_base64());
|
|
|
|
if (b64 == NULL)
|
2018-09-13 19:18:07 +00:00
|
|
|
goto end;
|
2015-03-20 15:28:40 +00:00
|
|
|
out = BIO_push(b64, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (num > 0) {
|
|
|
|
unsigned char buf[4096];
|
|
|
|
int chunk;
|
|
|
|
|
|
|
|
chunk = num;
|
|
|
|
if (chunk > (int)sizeof(buf))
|
2018-03-27 17:03:01 +00:00
|
|
|
chunk = sizeof(buf);
|
2015-03-20 15:28:40 +00:00
|
|
|
r = RAND_bytes(buf, chunk);
|
|
|
|
if (r <= 0)
|
2018-09-13 19:18:07 +00:00
|
|
|
goto end;
|
|
|
|
if (format != FORMAT_TEXT) {
|
|
|
|
if (BIO_write(out, buf, chunk) != chunk)
|
|
|
|
goto end;
|
|
|
|
} else {
|
2015-03-20 15:28:40 +00:00
|
|
|
for (i = 0; i < chunk; i++)
|
2018-09-13 19:18:07 +00:00
|
|
|
if (BIO_printf(out, "%02x", buf[i]) != 2)
|
|
|
|
goto end;
|
2015-03-20 15:28:40 +00:00
|
|
|
}
|
|
|
|
num -= chunk;
|
|
|
|
}
|
2018-09-13 19:18:07 +00:00
|
|
|
if (format == FORMAT_TEXT)
|
2015-03-20 15:28:40 +00:00
|
|
|
BIO_puts(out, "\n");
|
2018-09-13 19:18:07 +00:00
|
|
|
if (BIO_flush(out) <= 0)
|
|
|
|
goto end;
|
2015-03-20 15:28:40 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2018-09-13 19:18:07 +00:00
|
|
|
end:
|
|
|
|
if (ret != 0)
|
|
|
|
ERR_print_errors(bio_err);
|
2017-01-26 18:32:12 +00:00
|
|
|
release_engine(e);
|
2018-09-13 19:18:07 +00:00
|
|
|
BIO_free_all(out);
|
|
|
|
return ret;
|
2015-03-20 15:28:40 +00:00
|
|
|
}
|