diff options
Diffstat (limited to 'src/usr.bin/openssl/rand.c')
| -rw-r--r-- | src/usr.bin/openssl/rand.c | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/src/usr.bin/openssl/rand.c b/src/usr.bin/openssl/rand.c new file mode 100644 index 0000000000..0800157a35 --- /dev/null +++ b/src/usr.bin/openssl/rand.c | |||
| @@ -0,0 +1,194 @@ | |||
| 1 | /* $OpenBSD: rand.c,v 1.1 2014/08/26 17:47:25 jsing Exp $ */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * openssl-core@openssl.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | * This product includes cryptographic software written by Eric Young | ||
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 52 | * Hudson (tjh@cryptsoft.com). | ||
| 53 | * | ||
| 54 | */ | ||
| 55 | |||
| 56 | #include <ctype.h> | ||
| 57 | #include <stdio.h> | ||
| 58 | #include <string.h> | ||
| 59 | |||
| 60 | #include "apps.h" | ||
| 61 | |||
| 62 | #include <openssl/bio.h> | ||
| 63 | #include <openssl/err.h> | ||
| 64 | #include <openssl/rand.h> | ||
| 65 | |||
| 66 | /* -out file - write to file | ||
| 67 | * -base64 - base64 encode output | ||
| 68 | * -hex - hex encode output | ||
| 69 | * num - write 'num' bytes | ||
| 70 | */ | ||
| 71 | |||
| 72 | int rand_main(int, char **); | ||
| 73 | |||
| 74 | int | ||
| 75 | rand_main(int argc, char **argv) | ||
| 76 | { | ||
| 77 | int i, r, ret = 1; | ||
| 78 | int badopt; | ||
| 79 | char *outfile = NULL; | ||
| 80 | int base64 = 0; | ||
| 81 | int hex = 0; | ||
| 82 | BIO *out = NULL; | ||
| 83 | int num = -1; | ||
| 84 | #ifndef OPENSSL_NO_ENGINE | ||
| 85 | char *engine = NULL; | ||
| 86 | #endif | ||
| 87 | |||
| 88 | badopt = 0; | ||
| 89 | i = 0; | ||
| 90 | while (!badopt && argv[++i] != NULL) { | ||
| 91 | if (strcmp(argv[i], "-out") == 0) { | ||
| 92 | if ((argv[i + 1] != NULL) && (outfile == NULL)) | ||
| 93 | outfile = argv[++i]; | ||
| 94 | else | ||
| 95 | badopt = 1; | ||
| 96 | } | ||
| 97 | #ifndef OPENSSL_NO_ENGINE | ||
| 98 | else if (strcmp(argv[i], "-engine") == 0) { | ||
| 99 | if ((argv[i + 1] != NULL) && (engine == NULL)) | ||
| 100 | engine = argv[++i]; | ||
| 101 | else | ||
| 102 | badopt = 1; | ||
| 103 | } | ||
| 104 | #endif | ||
| 105 | else if (strcmp(argv[i], "-base64") == 0) { | ||
| 106 | if (!base64) | ||
| 107 | base64 = 1; | ||
| 108 | else | ||
| 109 | badopt = 1; | ||
| 110 | } else if (strcmp(argv[i], "-hex") == 0) { | ||
| 111 | if (!hex) | ||
| 112 | hex = 1; | ||
| 113 | else | ||
| 114 | badopt = 1; | ||
| 115 | } else if (isdigit((unsigned char) argv[i][0])) { | ||
| 116 | if (num < 0) { | ||
| 117 | r = sscanf(argv[i], "%d", &num); | ||
| 118 | if (r == 0 || num < 0) | ||
| 119 | badopt = 1; | ||
| 120 | } else | ||
| 121 | badopt = 1; | ||
| 122 | } else | ||
| 123 | badopt = 1; | ||
| 124 | } | ||
| 125 | |||
| 126 | if (hex && base64) | ||
| 127 | badopt = 1; | ||
| 128 | |||
| 129 | if (num < 0) | ||
| 130 | badopt = 1; | ||
| 131 | |||
| 132 | if (badopt) { | ||
| 133 | BIO_printf(bio_err, "Usage: rand [options] num\n"); | ||
| 134 | BIO_printf(bio_err, "where options are\n"); | ||
| 135 | BIO_printf(bio_err, "-out file - write to file\n"); | ||
| 136 | #ifndef OPENSSL_NO_ENGINE | ||
| 137 | BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n"); | ||
| 138 | #endif | ||
| 139 | BIO_printf(bio_err, "-base64 - base64 encode output\n"); | ||
| 140 | BIO_printf(bio_err, "-hex - hex encode output\n"); | ||
| 141 | goto err; | ||
| 142 | } | ||
| 143 | #ifndef OPENSSL_NO_ENGINE | ||
| 144 | setup_engine(bio_err, engine, 0); | ||
| 145 | #endif | ||
| 146 | |||
| 147 | out = BIO_new(BIO_s_file()); | ||
| 148 | if (out == NULL) | ||
| 149 | goto err; | ||
| 150 | if (outfile != NULL) | ||
| 151 | r = BIO_write_filename(out, outfile); | ||
| 152 | else { | ||
| 153 | r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT); | ||
| 154 | } | ||
| 155 | if (r <= 0) | ||
| 156 | goto err; | ||
| 157 | |||
| 158 | if (base64) { | ||
| 159 | BIO *b64 = BIO_new(BIO_f_base64()); | ||
| 160 | if (b64 == NULL) | ||
| 161 | goto err; | ||
| 162 | out = BIO_push(b64, out); | ||
| 163 | } | ||
| 164 | while (num > 0) { | ||
| 165 | unsigned char buf[4096]; | ||
| 166 | int chunk; | ||
| 167 | |||
| 168 | chunk = num; | ||
| 169 | if (chunk > (int) sizeof(buf)) | ||
| 170 | chunk = sizeof buf; | ||
| 171 | r = RAND_bytes(buf, chunk); | ||
| 172 | if (r <= 0) | ||
| 173 | goto err; | ||
| 174 | if (!hex) | ||
| 175 | BIO_write(out, buf, chunk); | ||
| 176 | else { | ||
| 177 | for (i = 0; i < chunk; i++) | ||
| 178 | BIO_printf(out, "%02x", buf[i]); | ||
| 179 | } | ||
| 180 | num -= chunk; | ||
| 181 | } | ||
| 182 | if (hex) | ||
| 183 | BIO_puts(out, "\n"); | ||
| 184 | (void) BIO_flush(out); | ||
| 185 | |||
| 186 | ret = 0; | ||
| 187 | |||
| 188 | err: | ||
| 189 | ERR_print_errors(bio_err); | ||
| 190 | if (out) | ||
| 191 | BIO_free_all(out); | ||
| 192 | |||
| 193 | return (ret); | ||
| 194 | } | ||
