xref: /onnv-gate/usr/src/common/openssl/apps/rand.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* apps/rand.c */
20Sstevel@tonic-gate /* ====================================================================
30Sstevel@tonic-gate  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
60Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
70Sstevel@tonic-gate  * are met:
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
100Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
140Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
150Sstevel@tonic-gate  *    distribution.
160Sstevel@tonic-gate  *
170Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
180Sstevel@tonic-gate  *    software must display the following acknowledgment:
190Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
200Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
210Sstevel@tonic-gate  *
220Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
230Sstevel@tonic-gate  *    endorse or promote products derived from this software without
240Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
250Sstevel@tonic-gate  *    openssl-core@openssl.org.
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
280Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
290Sstevel@tonic-gate  *    permission of the OpenSSL Project.
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
320Sstevel@tonic-gate  *    acknowledgment:
330Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
340Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
370Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
380Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
390Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
400Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
410Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
420Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
430Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
440Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
450Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
460Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
470Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
480Sstevel@tonic-gate  * ====================================================================
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
510Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
520Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include "apps.h"
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <ctype.h>
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <string.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #include <openssl/bio.h>
630Sstevel@tonic-gate #include <openssl/err.h>
640Sstevel@tonic-gate #include <openssl/rand.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #undef PROG
670Sstevel@tonic-gate #define PROG rand_main
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /* -out file         - write to file
700Sstevel@tonic-gate  * -rand file:file   - PRNG seed files
710Sstevel@tonic-gate  * -base64           - encode output
720Sstevel@tonic-gate  * num               - write 'num' bytes
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate int MAIN(int, char **);
760Sstevel@tonic-gate 
MAIN(int argc,char ** argv)770Sstevel@tonic-gate int MAIN(int argc, char **argv)
780Sstevel@tonic-gate 	{
790Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
800Sstevel@tonic-gate 	ENGINE *e = NULL;
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate 	int i, r, ret = 1;
830Sstevel@tonic-gate 	int badopt;
840Sstevel@tonic-gate 	char *outfile = NULL;
850Sstevel@tonic-gate 	char *inrand = NULL;
860Sstevel@tonic-gate 	int base64 = 0;
870Sstevel@tonic-gate 	BIO *out = NULL;
880Sstevel@tonic-gate 	int num = -1;
890Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
900Sstevel@tonic-gate 	char *engine=NULL;
910Sstevel@tonic-gate #endif
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	apps_startup();
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if (bio_err == NULL)
960Sstevel@tonic-gate 		if ((bio_err = BIO_new(BIO_s_file())) != NULL)
970Sstevel@tonic-gate 			BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if (!load_config(bio_err, NULL))
1000Sstevel@tonic-gate 		goto err;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	badopt = 0;
1030Sstevel@tonic-gate 	i = 0;
1040Sstevel@tonic-gate 	while (!badopt && argv[++i] != NULL)
1050Sstevel@tonic-gate 		{
1060Sstevel@tonic-gate 		if (strcmp(argv[i], "-out") == 0)
1070Sstevel@tonic-gate 			{
1080Sstevel@tonic-gate 			if ((argv[i+1] != NULL) && (outfile == NULL))
1090Sstevel@tonic-gate 				outfile = argv[++i];
1100Sstevel@tonic-gate 			else
1110Sstevel@tonic-gate 				badopt = 1;
1120Sstevel@tonic-gate 			}
1130Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1140Sstevel@tonic-gate 		else if (strcmp(argv[i], "-engine") == 0)
1150Sstevel@tonic-gate 			{
1160Sstevel@tonic-gate 			if ((argv[i+1] != NULL) && (engine == NULL))
1170Sstevel@tonic-gate 				engine = argv[++i];
1180Sstevel@tonic-gate 			else
1190Sstevel@tonic-gate 				badopt = 1;
1200Sstevel@tonic-gate 			}
1210Sstevel@tonic-gate #endif
1220Sstevel@tonic-gate 		else if (strcmp(argv[i], "-rand") == 0)
1230Sstevel@tonic-gate 			{
1240Sstevel@tonic-gate 			if ((argv[i+1] != NULL) && (inrand == NULL))
1250Sstevel@tonic-gate 				inrand = argv[++i];
1260Sstevel@tonic-gate 			else
1270Sstevel@tonic-gate 				badopt = 1;
1280Sstevel@tonic-gate 			}
1290Sstevel@tonic-gate 		else if (strcmp(argv[i], "-base64") == 0)
1300Sstevel@tonic-gate 			{
1310Sstevel@tonic-gate 			if (!base64)
1320Sstevel@tonic-gate 				base64 = 1;
1330Sstevel@tonic-gate 			else
1340Sstevel@tonic-gate 				badopt = 1;
1350Sstevel@tonic-gate 			}
1360Sstevel@tonic-gate 		else if (isdigit((unsigned char)argv[i][0]))
1370Sstevel@tonic-gate 			{
1380Sstevel@tonic-gate 			if (num < 0)
1390Sstevel@tonic-gate 				{
1400Sstevel@tonic-gate 				r = sscanf(argv[i], "%d", &num);
1410Sstevel@tonic-gate 				if (r == 0 || num < 0)
1420Sstevel@tonic-gate 					badopt = 1;
1430Sstevel@tonic-gate 				}
1440Sstevel@tonic-gate 			else
1450Sstevel@tonic-gate 				badopt = 1;
1460Sstevel@tonic-gate 			}
1470Sstevel@tonic-gate 		else
1480Sstevel@tonic-gate 			badopt = 1;
1490Sstevel@tonic-gate 		}
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (num < 0)
1520Sstevel@tonic-gate 		badopt = 1;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (badopt)
1550Sstevel@tonic-gate 		{
1560Sstevel@tonic-gate 		BIO_printf(bio_err, "Usage: rand [options] num\n");
1570Sstevel@tonic-gate 		BIO_printf(bio_err, "where options are\n");
1580Sstevel@tonic-gate 		BIO_printf(bio_err, "-out file             - write to file\n");
1590Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1600Sstevel@tonic-gate 		BIO_printf(bio_err, "-engine e             - use engine e, possibly a hardware device.\n");
1610Sstevel@tonic-gate #endif
1620Sstevel@tonic-gate 		BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
1630Sstevel@tonic-gate 		BIO_printf(bio_err, "-base64               - encode output\n");
1640Sstevel@tonic-gate 		goto err;
1650Sstevel@tonic-gate 		}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1680Sstevel@tonic-gate         e = setup_engine(bio_err, engine, 0);
1690Sstevel@tonic-gate #endif
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	app_RAND_load_file(NULL, bio_err, (inrand != NULL));
1720Sstevel@tonic-gate 	if (inrand != NULL)
1730Sstevel@tonic-gate 		BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
1740Sstevel@tonic-gate 			app_RAND_load_files(inrand));
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	out = BIO_new(BIO_s_file());
1770Sstevel@tonic-gate 	if (out == NULL)
1780Sstevel@tonic-gate 		goto err;
1790Sstevel@tonic-gate 	if (outfile != NULL)
1800Sstevel@tonic-gate 		r = BIO_write_filename(out, outfile);
1810Sstevel@tonic-gate 	else
1820Sstevel@tonic-gate 		{
1830Sstevel@tonic-gate 		r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
1840Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
1850Sstevel@tonic-gate 		{
1860Sstevel@tonic-gate 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1870Sstevel@tonic-gate 		out = BIO_push(tmpbio, out);
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate #endif
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 	if (r <= 0)
1920Sstevel@tonic-gate 		goto err;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if (base64)
1950Sstevel@tonic-gate 		{
1960Sstevel@tonic-gate 		BIO *b64 = BIO_new(BIO_f_base64());
1970Sstevel@tonic-gate 		if (b64 == NULL)
1980Sstevel@tonic-gate 			goto err;
1990Sstevel@tonic-gate 		out = BIO_push(b64, out);
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	while (num > 0)
2030Sstevel@tonic-gate 		{
2040Sstevel@tonic-gate 		unsigned char buf[4096];
2050Sstevel@tonic-gate 		int chunk;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		chunk = num;
208*2139Sjp161948 		if (chunk > (int)sizeof(buf))
2090Sstevel@tonic-gate 			chunk = sizeof buf;
2100Sstevel@tonic-gate 		r = RAND_bytes(buf, chunk);
2110Sstevel@tonic-gate 		if (r <= 0)
2120Sstevel@tonic-gate 			goto err;
2130Sstevel@tonic-gate 		BIO_write(out, buf, chunk);
2140Sstevel@tonic-gate 		num -= chunk;
2150Sstevel@tonic-gate 		}
2160Sstevel@tonic-gate 	BIO_flush(out);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	app_RAND_write_file(NULL, bio_err);
2190Sstevel@tonic-gate 	ret = 0;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate err:
2220Sstevel@tonic-gate 	ERR_print_errors(bio_err);
2230Sstevel@tonic-gate 	if (out)
2240Sstevel@tonic-gate 		BIO_free_all(out);
2250Sstevel@tonic-gate 	apps_shutdown();
2260Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
2270Sstevel@tonic-gate 	}
228