]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
6738bf14 | 2 | * Copyright 1998-2018 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 RS |
21 | typedef enum OPTION_choice { |
22 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
3ee1eac2 RS |
23 | OPT_OUT, OPT_ENGINE, OPT_BASE64, OPT_HEX, |
24 | OPT_R_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, | |
92de469f RS |
42 | |
43 | OPT_PARAMETERS(), | |
44 | {"num", 0, 0, "Number of bytes to generate"}, | |
7e1b7485 RS |
45 | {NULL} |
46 | }; | |
27b78273 | 47 | |
7e1b7485 RS |
48 | int rand_main(int argc, char **argv) |
49 | { | |
dd1abd44 | 50 | ENGINE *e = NULL; |
7e1b7485 | 51 | BIO *out = NULL; |
3ee1eac2 | 52 | char *outfile = NULL, *prog; |
7e1b7485 | 53 | OPTION_CHOICE o; |
bdd58d98 | 54 | int format = FORMAT_BINARY, i, num = -1, r, ret = 1; |
7e1b7485 RS |
55 | |
56 | prog = opt_init(argc, argv, rand_options); | |
57 | while ((o = opt_next()) != OPT_EOF) { | |
58 | switch (o) { | |
59 | case OPT_EOF: | |
60 | case OPT_ERR: | |
61 | opthelp: | |
62 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
63 | goto end; | |
64 | case OPT_HELP: | |
65 | opt_help(rand_options); | |
66 | ret = 0; | |
67 | goto end; | |
68 | case OPT_OUT: | |
69 | outfile = opt_arg(); | |
70 | break; | |
71 | case OPT_ENGINE: | |
dd1abd44 | 72 | e = setup_engine(opt_arg(), 0); |
7e1b7485 | 73 | break; |
3ee1eac2 RS |
74 | case OPT_R_CASES: |
75 | if (!opt_rand(o)) | |
76 | goto end; | |
7e1b7485 RS |
77 | break; |
78 | case OPT_BASE64: | |
bdd58d98 | 79 | format = FORMAT_BASE64; |
7e1b7485 RS |
80 | break; |
81 | case OPT_HEX: | |
bdd58d98 | 82 | format = FORMAT_TEXT; |
7e1b7485 | 83 | break; |
0f113f3e | 84 | } |
0f113f3e | 85 | } |
7e1b7485 RS |
86 | argc = opt_num_rest(); |
87 | argv = opt_rest(); | |
c27363f5 RS |
88 | if (argc == 1) { |
89 | if (!opt_int(argv[0], &num) || num <= 0) | |
90 | goto end; | |
91 | } else if (argc > 0) { | |
92 | BIO_printf(bio_err, "Extra arguments given.\n"); | |
7e1b7485 | 93 | goto opthelp; |
c27363f5 | 94 | } |
0f113f3e | 95 | |
bdd58d98 | 96 | out = bio_open_default(outfile, 'w', format); |
0f113f3e | 97 | if (out == NULL) |
7e1b7485 | 98 | goto end; |
0f113f3e | 99 | |
bdd58d98 | 100 | if (format == FORMAT_BASE64) { |
0f113f3e MC |
101 | BIO *b64 = BIO_new(BIO_f_base64()); |
102 | if (b64 == NULL) | |
7e1b7485 | 103 | goto end; |
0f113f3e MC |
104 | out = BIO_push(b64, out); |
105 | } | |
106 | ||
107 | while (num > 0) { | |
108 | unsigned char buf[4096]; | |
109 | int chunk; | |
110 | ||
111 | chunk = num; | |
112 | if (chunk > (int)sizeof(buf)) | |
cbe29648 | 113 | chunk = sizeof(buf); |
0f113f3e MC |
114 | r = RAND_bytes(buf, chunk); |
115 | if (r <= 0) | |
7e1b7485 | 116 | goto end; |
0f91e1df RS |
117 | if (format != FORMAT_TEXT) { |
118 | if (BIO_write(out, buf, chunk) != chunk) | |
119 | goto end; | |
120 | } else { | |
0f113f3e | 121 | for (i = 0; i < chunk; i++) |
0f91e1df RS |
122 | if (BIO_printf(out, "%02x", buf[i]) != 2) |
123 | goto end; | |
0f113f3e MC |
124 | } |
125 | num -= chunk; | |
126 | } | |
bdd58d98 | 127 | if (format == FORMAT_TEXT) |
0f113f3e | 128 | BIO_puts(out, "\n"); |
3ee1eac2 | 129 | if (BIO_flush(out) <= 0) |
0f91e1df | 130 | goto end; |
0f113f3e | 131 | |
0f113f3e MC |
132 | ret = 0; |
133 | ||
7e1b7485 | 134 | end: |
0f91e1df RS |
135 | if (ret != 0) |
136 | ERR_print_errors(bio_err); | |
dd1abd44 | 137 | release_engine(e); |
ca3a82c3 | 138 | BIO_free_all(out); |
26a7d938 | 139 | return ret; |
0f113f3e | 140 | } |