]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
a28d06f3 | 2 | * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved. |
a661b653 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
a661b653 | 8 | */ |
27b78273 BM |
9 | |
10 | #include "apps.h" | |
dab2cd68 | 11 | #include "progs.h" |
27b78273 BM |
12 | |
13 | #include <ctype.h> | |
14 | #include <stdio.h> | |
8824ec7c | 15 | #include <string.h> |
27b78273 BM |
16 | |
17 | #include <openssl/bio.h> | |
18 | #include <openssl/err.h> | |
19 | #include <openssl/rand.h> | |
20 | ||
7e1b7485 | 21 | typedef enum OPTION_choice { |
b0f96018 | 22 | OPT_COMMON, |
3ee1eac2 | 23 | OPT_OUT, OPT_ENGINE, OPT_BASE64, OPT_HEX, |
6bd4e3f2 | 24 | OPT_R_ENUM, OPT_PROV_ENUM |
7e1b7485 RS |
25 | } OPTION_CHOICE; |
26 | ||
44c83ebd | 27 | const OPTIONS rand_options[] = { |
92de469f | 28 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] num\n"}, |
5388f986 RS |
29 | |
30 | OPT_SECTION("General"), | |
7e1b7485 | 31 | {"help", OPT_HELP, '-', "Display this summary"}, |
0b13e9f0 | 32 | #ifndef OPENSSL_NO_ENGINE |
7e1b7485 | 33 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
0b13e9f0 | 34 | #endif |
5388f986 RS |
35 | |
36 | OPT_SECTION("Output"), | |
37 | {"out", OPT_OUT, '>', "Output file"}, | |
38 | {"base64", OPT_BASE64, '-', "Base64 encode output"}, | |
39 | {"hex", OPT_HEX, '-', "Hex encode output"}, | |
40 | ||
41 | OPT_R_OPTIONS, | |
6bd4e3f2 | 42 | OPT_PROV_OPTIONS, |
92de469f RS |
43 | |
44 | OPT_PARAMETERS(), | |
45 | {"num", 0, 0, "Number of bytes to generate"}, | |
7e1b7485 RS |
46 | {NULL} |
47 | }; | |
27b78273 | 48 | |
7e1b7485 RS |
49 | int rand_main(int argc, char **argv) |
50 | { | |
dd1abd44 | 51 | ENGINE *e = NULL; |
7e1b7485 | 52 | BIO *out = NULL; |
3ee1eac2 | 53 | char *outfile = NULL, *prog; |
7e1b7485 | 54 | OPTION_CHOICE o; |
2aa645bc KB |
55 | int format = FORMAT_BINARY, r, i, ret = 1, buflen = 131072; |
56 | long num = -1; | |
57 | uint8_t *buf = NULL; | |
7e1b7485 RS |
58 | |
59 | prog = opt_init(argc, argv, rand_options); | |
60 | while ((o = opt_next()) != OPT_EOF) { | |
61 | switch (o) { | |
62 | case OPT_EOF: | |
63 | case OPT_ERR: | |
64 | opthelp: | |
65 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
66 | goto end; | |
67 | case OPT_HELP: | |
68 | opt_help(rand_options); | |
69 | ret = 0; | |
70 | goto end; | |
71 | case OPT_OUT: | |
72 | outfile = opt_arg(); | |
73 | break; | |
74 | case OPT_ENGINE: | |
dd1abd44 | 75 | e = setup_engine(opt_arg(), 0); |
7e1b7485 | 76 | break; |
3ee1eac2 RS |
77 | case OPT_R_CASES: |
78 | if (!opt_rand(o)) | |
79 | goto end; | |
7e1b7485 RS |
80 | break; |
81 | case OPT_BASE64: | |
bdd58d98 | 82 | format = FORMAT_BASE64; |
7e1b7485 RS |
83 | break; |
84 | case OPT_HEX: | |
bdd58d98 | 85 | format = FORMAT_TEXT; |
7e1b7485 | 86 | break; |
6bd4e3f2 P |
87 | case OPT_PROV_CASES: |
88 | if (!opt_provider(o)) | |
89 | goto end; | |
90 | break; | |
0f113f3e | 91 | } |
0f113f3e | 92 | } |
021410ea RS |
93 | |
94 | /* Optional argument is number of bytes to generate. */ | |
7e1b7485 RS |
95 | argc = opt_num_rest(); |
96 | argv = opt_rest(); | |
c27363f5 | 97 | if (argc == 1) { |
2aa645bc | 98 | if (!opt_long(argv[0], &num) || num <= 0) |
021410ea | 99 | goto opthelp; |
d9f07357 | 100 | } else if (!opt_check_rest_arg(NULL)) { |
7e1b7485 | 101 | goto opthelp; |
c27363f5 | 102 | } |
0f113f3e | 103 | |
3ad60309 DDO |
104 | if (!app_RAND_load()) |
105 | goto end; | |
106 | ||
bdd58d98 | 107 | out = bio_open_default(outfile, 'w', format); |
0f113f3e | 108 | if (out == NULL) |
7e1b7485 | 109 | goto end; |
0f113f3e | 110 | |
bdd58d98 | 111 | if (format == FORMAT_BASE64) { |
0f113f3e MC |
112 | BIO *b64 = BIO_new(BIO_f_base64()); |
113 | if (b64 == NULL) | |
7e1b7485 | 114 | goto end; |
0f113f3e MC |
115 | out = BIO_push(b64, out); |
116 | } | |
117 | ||
2aa645bc | 118 | buf = app_malloc(buflen, "buffer for output file"); |
0f113f3e | 119 | while (num > 0) { |
2aa645bc | 120 | long chunk; |
0f113f3e | 121 | |
2aa645bc | 122 | chunk = (num > buflen) ? buflen : num; |
0f113f3e MC |
123 | r = RAND_bytes(buf, chunk); |
124 | if (r <= 0) | |
7e1b7485 | 125 | goto end; |
0f91e1df RS |
126 | if (format != FORMAT_TEXT) { |
127 | if (BIO_write(out, buf, chunk) != chunk) | |
128 | goto end; | |
129 | } else { | |
0f113f3e | 130 | for (i = 0; i < chunk; i++) |
0f91e1df RS |
131 | if (BIO_printf(out, "%02x", buf[i]) != 2) |
132 | goto end; | |
0f113f3e MC |
133 | } |
134 | num -= chunk; | |
135 | } | |
bdd58d98 | 136 | if (format == FORMAT_TEXT) |
0f113f3e | 137 | BIO_puts(out, "\n"); |
3ee1eac2 | 138 | if (BIO_flush(out) <= 0) |
0f91e1df | 139 | goto end; |
0f113f3e | 140 | |
0f113f3e MC |
141 | ret = 0; |
142 | ||
7e1b7485 | 143 | end: |
0f91e1df RS |
144 | if (ret != 0) |
145 | ERR_print_errors(bio_err); | |
2aa645bc | 146 | OPENSSL_free(buf); |
dd1abd44 | 147 | release_engine(e); |
ca3a82c3 | 148 | BIO_free_all(out); |
26a7d938 | 149 | return ret; |
0f113f3e | 150 | } |