]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/rand.c
Add "sections" to -help output
[thirdparty/openssl.git] / apps / rand.c
1 /*
2 * Copyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
8 */
9
10 #include "apps.h"
11 #include "progs.h"
12
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <string.h>
16
17 #include <openssl/bio.h>
18 #include <openssl/err.h>
19 #include <openssl/rand.h>
20
21 typedef enum OPTION_choice {
22 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
23 OPT_OUT, OPT_ENGINE, OPT_BASE64, OPT_HEX,
24 OPT_R_ENUM
25 } OPTION_CHOICE;
26
27 const OPTIONS rand_options[] = {
28 {OPT_HELP_STR, 1, '-', "Usage: %s [flags] num\n"},
29
30 OPT_SECTION("General"),
31 {"help", OPT_HELP, '-', "Display this summary"},
32 #ifndef OPENSSL_NO_ENGINE
33 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
34 #endif
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,
42 {NULL}
43 };
44
45 int rand_main(int argc, char **argv)
46 {
47 ENGINE *e = NULL;
48 BIO *out = NULL;
49 char *outfile = NULL, *prog;
50 OPTION_CHOICE o;
51 int format = FORMAT_BINARY, i, num = -1, r, ret = 1;
52
53 prog = opt_init(argc, argv, rand_options);
54 while ((o = opt_next()) != OPT_EOF) {
55 switch (o) {
56 case OPT_EOF:
57 case OPT_ERR:
58 opthelp:
59 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
60 goto end;
61 case OPT_HELP:
62 opt_help(rand_options);
63 ret = 0;
64 goto end;
65 case OPT_OUT:
66 outfile = opt_arg();
67 break;
68 case OPT_ENGINE:
69 e = setup_engine(opt_arg(), 0);
70 break;
71 case OPT_R_CASES:
72 if (!opt_rand(o))
73 goto end;
74 break;
75 case OPT_BASE64:
76 format = FORMAT_BASE64;
77 break;
78 case OPT_HEX:
79 format = FORMAT_TEXT;
80 break;
81 }
82 }
83 argc = opt_num_rest();
84 argv = opt_rest();
85 if (argc == 1) {
86 if (!opt_int(argv[0], &num) || num <= 0)
87 goto end;
88 } else if (argc > 0) {
89 BIO_printf(bio_err, "Extra arguments given.\n");
90 goto opthelp;
91 }
92
93 out = bio_open_default(outfile, 'w', format);
94 if (out == NULL)
95 goto end;
96
97 if (format == FORMAT_BASE64) {
98 BIO *b64 = BIO_new(BIO_f_base64());
99 if (b64 == NULL)
100 goto end;
101 out = BIO_push(b64, out);
102 }
103
104 while (num > 0) {
105 unsigned char buf[4096];
106 int chunk;
107
108 chunk = num;
109 if (chunk > (int)sizeof(buf))
110 chunk = sizeof(buf);
111 r = RAND_bytes(buf, chunk);
112 if (r <= 0)
113 goto end;
114 if (format != FORMAT_TEXT) {
115 if (BIO_write(out, buf, chunk) != chunk)
116 goto end;
117 } else {
118 for (i = 0; i < chunk; i++)
119 if (BIO_printf(out, "%02x", buf[i]) != 2)
120 goto end;
121 }
122 num -= chunk;
123 }
124 if (format == FORMAT_TEXT)
125 BIO_puts(out, "\n");
126 if (BIO_flush(out) <= 0)
127 goto end;
128
129 ret = 0;
130
131 end:
132 if (ret != 0)
133 ERR_print_errors(bio_err);
134 release_engine(e);
135 BIO_free_all(out);
136 return ret;
137 }