1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/topenssl/src/rand.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,230 @@
1.4 +/* apps/rand.c */
1.5 +/* ====================================================================
1.6 + * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
1.7 + *
1.8 + * Redistribution and use in source and binary forms, with or without
1.9 + * modification, are permitted provided that the following conditions
1.10 + * are met:
1.11 + *
1.12 + * 1. Redistributions of source code must retain the above copyright
1.13 + * notice, this list of conditions and the following disclaimer.
1.14 + *
1.15 + * 2. Redistributions in binary form must reproduce the above copyright
1.16 + * notice, this list of conditions and the following disclaimer in
1.17 + * the documentation and/or other materials provided with the
1.18 + * distribution.
1.19 + *
1.20 + * 3. All advertising materials mentioning features or use of this
1.21 + * software must display the following acknowledgment:
1.22 + * "This product includes software developed by the OpenSSL Project
1.23 + * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
1.24 + *
1.25 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.26 + * endorse or promote products derived from this software without
1.27 + * prior written permission. For written permission, please contact
1.28 + * openssl-core@openssl.org.
1.29 + *
1.30 + * 5. Products derived from this software may not be called "OpenSSL"
1.31 + * nor may "OpenSSL" appear in their names without prior written
1.32 + * permission of the OpenSSL Project.
1.33 + *
1.34 + * 6. Redistributions of any form whatsoever must retain the following
1.35 + * acknowledgment:
1.36 + * "This product includes software developed by the OpenSSL Project
1.37 + * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
1.38 + *
1.39 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.40 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.41 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.42 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.43 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.44 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.45 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.46 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.47 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.48 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.49 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.50 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.51 + * ====================================================================
1.52 + *
1.53 + * This product includes cryptographic software written by Eric Young
1.54 + * (eay@cryptsoft.com). This product includes software written by Tim
1.55 + * Hudson (tjh@cryptsoft.com).
1.56 + *
1.57 + */
1.58 +
1.59 +#include "apps.h"
1.60 +
1.61 +#include <ctype.h>
1.62 +#include <stdio.h>
1.63 +#include <string.h>
1.64 +
1.65 +#include <openssl/bio.h>
1.66 +#include <openssl/err.h>
1.67 +#include <openssl/rand.h>
1.68 +
1.69 +#undef PROG
1.70 +#define PROG rand_main
1.71 +
1.72 +/* -out file - write to file
1.73 + * -rand file:file - PRNG seed files
1.74 + * -base64 - encode output
1.75 + * num - write 'num' bytes
1.76 + */
1.77 +
1.78 +
1.79 +int MAIN(int, char **);
1.80 +
1.81 +int MAIN(int argc, char **argv)
1.82 + {
1.83 +#ifndef OPENSSL_NO_ENGINE
1.84 + ENGINE *e = NULL;
1.85 +#endif
1.86 + int i, r, ret = 1;
1.87 + int badopt;
1.88 + char *outfile = NULL;
1.89 + char *inrand = NULL;
1.90 + int base64 = 0;
1.91 + BIO *out = NULL;
1.92 + int num = -1;
1.93 +#ifndef OPENSSL_NO_ENGINE
1.94 + char *engine=NULL;
1.95 +#endif
1.96 +
1.97 + apps_startup();
1.98 +
1.99 + if (bio_err == NULL)
1.100 + if ((bio_err = BIO_new(BIO_s_file())) != NULL)
1.101 + BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
1.102 +
1.103 +
1.104 + if (!load_config(bio_err, NULL))
1.105 + goto err;
1.106 +
1.107 + badopt = 0;
1.108 + i = 0;
1.109 + while (!badopt && argv[++i] != NULL)
1.110 + {
1.111 + if (strcmp(argv[i], "-out") == 0)
1.112 + {
1.113 + if ((argv[i+1] != NULL) && (outfile == NULL))
1.114 + outfile = argv[++i];
1.115 + else
1.116 + badopt = 1;
1.117 + }
1.118 +#ifndef OPENSSL_NO_ENGINE
1.119 + else if (strcmp(argv[i], "-engine") == 0)
1.120 + {
1.121 + if ((argv[i+1] != NULL) && (engine == NULL))
1.122 + engine = argv[++i];
1.123 + else
1.124 + badopt = 1;
1.125 + }
1.126 +#endif
1.127 + else if (strcmp(argv[i], "-rand") == 0)
1.128 + {
1.129 + if ((argv[i+1] != NULL) && (inrand == NULL))
1.130 + inrand = argv[++i];
1.131 + else
1.132 + badopt = 1;
1.133 + }
1.134 + else if (strcmp(argv[i], "-base64") == 0)
1.135 + {
1.136 + if (!base64)
1.137 + base64 = 1;
1.138 + else
1.139 + badopt = 1;
1.140 + }
1.141 + else if (isdigit((unsigned char)argv[i][0]))
1.142 + {
1.143 + if (num < 0)
1.144 + {
1.145 + r = sscanf(argv[i], "%d", &num);
1.146 + if (r == 0 || num < 0)
1.147 + badopt = 1;
1.148 + }
1.149 + else
1.150 + badopt = 1;
1.151 + }
1.152 + else
1.153 + badopt = 1;
1.154 + }
1.155 +
1.156 + if (num < 0)
1.157 + badopt = 1;
1.158 +
1.159 + if (badopt)
1.160 + {
1.161 + BIO_printf(bio_err, "Usage: rand [options] num\n");
1.162 + BIO_printf(bio_err, "where options are\n");
1.163 + BIO_printf(bio_err, "-out file - write to file\n");
1.164 +#ifndef OPENSSL_NO_ENGINE
1.165 + BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n");
1.166 +#endif
1.167 + BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
1.168 + BIO_printf(bio_err, "-base64 - encode output\n");
1.169 + goto err;
1.170 + }
1.171 +
1.172 +#ifndef OPENSSL_NO_ENGINE
1.173 + e = setup_engine(bio_err, engine, 0);
1.174 +#endif
1.175 +
1.176 + app_RAND_load_file(NULL, bio_err, (inrand != NULL));
1.177 + if (inrand != NULL)
1.178 + BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
1.179 + app_RAND_load_files(inrand));
1.180 +
1.181 + out = BIO_new(BIO_s_file());
1.182 + if (out == NULL)
1.183 + goto err;
1.184 + if (outfile != NULL)
1.185 + r = BIO_write_filename(out, outfile);
1.186 + else
1.187 + {
1.188 + r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
1.189 +
1.190 +#ifdef OPENSSL_SYS_VMS
1.191 + {
1.192 + BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1.193 + out = BIO_push(tmpbio, out);
1.194 + }
1.195 +#endif
1.196 + }
1.197 + if (r <= 0)
1.198 + goto err;
1.199 +
1.200 + if (base64)
1.201 + {
1.202 + BIO *b64 = BIO_new(BIO_f_base64());
1.203 + if (b64 == NULL)
1.204 + goto err;
1.205 + out = BIO_push(b64, out);
1.206 + }
1.207 +
1.208 + while (num > 0)
1.209 + {
1.210 + unsigned char buf[4096];
1.211 + int chunk;
1.212 +
1.213 + chunk = num;
1.214 + if (chunk > (int)sizeof(buf))
1.215 + chunk = sizeof buf;
1.216 + r = RAND_bytes(buf, chunk);
1.217 + if (r <= 0)
1.218 + goto err;
1.219 + BIO_write(out, buf, chunk);
1.220 + num -= chunk;
1.221 + }
1.222 + (void)BIO_flush(out);
1.223 +
1.224 + app_RAND_write_file(NULL, bio_err);
1.225 + ret = 0;
1.226 +
1.227 +err:
1.228 + ERR_print_errors(bio_err);
1.229 + if (out)
1.230 + BIO_free_all(out);
1.231 + apps_shutdown();
1.232 + OPENSSL_EXIT(ret);
1.233 + }