]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved. | |
a661b653 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (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 | |
a661b653 | 8 | */ |
27b78273 BM |
9 | |
10 | #include "apps.h" | |
11 | ||
12 | #include <ctype.h> | |
13 | #include <stdio.h> | |
8824ec7c | 14 | #include <string.h> |
27b78273 BM |
15 | |
16 | #include <openssl/bio.h> | |
17 | #include <openssl/err.h> | |
18 | #include <openssl/rand.h> | |
19 | ||
7e1b7485 RS |
20 | typedef enum OPTION_choice { |
21 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
22 | OPT_OUT, OPT_ENGINE, OPT_RAND, OPT_BASE64, OPT_HEX | |
23 | } OPTION_CHOICE; | |
24 | ||
44c83ebd | 25 | const OPTIONS rand_options[] = { |
7e1b7485 RS |
26 | {OPT_HELP_STR, 1, '-', "Usage: %s [flags] num\n"}, |
27 | {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, | |
28 | {"help", OPT_HELP, '-', "Display this summary"}, | |
29 | {"out", OPT_OUT, '>', "Output file"}, | |
30 | {"rand", OPT_RAND, 's', | |
31 | "Load the file(s) into the random number generator"}, | |
32 | {"base64", OPT_BASE64, '-', "Base64 encode output"}, | |
33 | {"hex", OPT_HEX, '-', "Hex encode output"}, | |
0b13e9f0 | 34 | #ifndef OPENSSL_NO_ENGINE |
7e1b7485 | 35 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
0b13e9f0 | 36 | #endif |
7e1b7485 RS |
37 | {NULL} |
38 | }; | |
27b78273 | 39 | |
7e1b7485 RS |
40 | int rand_main(int argc, char **argv) |
41 | { | |
dd1abd44 | 42 | ENGINE *e = NULL; |
7e1b7485 | 43 | BIO *out = NULL; |
333b070e | 44 | char *inrand = NULL, *outfile = NULL, *prog; |
7e1b7485 | 45 | OPTION_CHOICE o; |
bdd58d98 | 46 | int format = FORMAT_BINARY, i, num = -1, r, ret = 1; |
7e1b7485 RS |
47 | |
48 | prog = opt_init(argc, argv, rand_options); | |
49 | while ((o = opt_next()) != OPT_EOF) { | |
50 | switch (o) { | |
51 | case OPT_EOF: | |
52 | case OPT_ERR: | |
53 | opthelp: | |
54 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
55 | goto end; | |
56 | case OPT_HELP: | |
57 | opt_help(rand_options); | |
58 | ret = 0; | |
59 | goto end; | |
60 | case OPT_OUT: | |
61 | outfile = opt_arg(); | |
62 | break; | |
63 | case OPT_ENGINE: | |
dd1abd44 | 64 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
65 | break; |
66 | case OPT_RAND: | |
67 | inrand = opt_arg(); | |
68 | break; | |
69 | case OPT_BASE64: | |
bdd58d98 | 70 | format = FORMAT_BASE64; |
7e1b7485 RS |
71 | break; |
72 | case OPT_HEX: | |
bdd58d98 | 73 | format = FORMAT_TEXT; |
7e1b7485 | 74 | break; |
0f113f3e | 75 | } |
0f113f3e | 76 | } |
7e1b7485 RS |
77 | argc = opt_num_rest(); |
78 | argv = opt_rest(); | |
0f113f3e | 79 | |
bd4850df | 80 | if (argc != 1 || !opt_int(argv[0], &num) || num < 0) |
7e1b7485 | 81 | goto opthelp; |
0f113f3e | 82 | |
7e1b7485 | 83 | app_RAND_load_file(NULL, (inrand != NULL)); |
0f113f3e MC |
84 | if (inrand != NULL) |
85 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", | |
86 | app_RAND_load_files(inrand)); | |
87 | ||
bdd58d98 | 88 | out = bio_open_default(outfile, 'w', format); |
0f113f3e | 89 | if (out == NULL) |
7e1b7485 | 90 | goto end; |
0f113f3e | 91 | |
bdd58d98 | 92 | if (format == FORMAT_BASE64) { |
0f113f3e MC |
93 | BIO *b64 = BIO_new(BIO_f_base64()); |
94 | if (b64 == NULL) | |
7e1b7485 | 95 | goto end; |
0f113f3e MC |
96 | out = BIO_push(b64, out); |
97 | } | |
98 | ||
99 | while (num > 0) { | |
100 | unsigned char buf[4096]; | |
101 | int chunk; | |
102 | ||
103 | chunk = num; | |
104 | if (chunk > (int)sizeof(buf)) | |
105 | chunk = sizeof buf; | |
106 | r = RAND_bytes(buf, chunk); | |
107 | if (r <= 0) | |
7e1b7485 | 108 | goto end; |
0f91e1df RS |
109 | if (format != FORMAT_TEXT) { |
110 | if (BIO_write(out, buf, chunk) != chunk) | |
111 | goto end; | |
112 | } else { | |
0f113f3e | 113 | for (i = 0; i < chunk; i++) |
0f91e1df RS |
114 | if (BIO_printf(out, "%02x", buf[i]) != 2) |
115 | goto end; | |
0f113f3e MC |
116 | } |
117 | num -= chunk; | |
118 | } | |
bdd58d98 | 119 | if (format == FORMAT_TEXT) |
0f113f3e | 120 | BIO_puts(out, "\n"); |
0f91e1df RS |
121 | if (BIO_flush(out) <= 0 || !app_RAND_write_file(NULL)) |
122 | goto end; | |
0f113f3e | 123 | |
0f113f3e MC |
124 | ret = 0; |
125 | ||
7e1b7485 | 126 | end: |
0f91e1df RS |
127 | if (ret != 0) |
128 | ERR_print_errors(bio_err); | |
dd1abd44 | 129 | release_engine(e); |
ca3a82c3 | 130 | BIO_free_all(out); |
7e1b7485 | 131 | return (ret); |
0f113f3e | 132 | } |