os/ossrv/ssl/libcrypto/src/crypto/rand/randfile.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* crypto/rand/randfile.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
/*
sl@0
    59
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
/* We need to define this to get macros like S_IFBLK and S_IFCHR */
sl@0
    62
#define _XOPEN_SOURCE 500
sl@0
    63
sl@0
    64
sl@0
    65
sl@0
    66
#include <errno.h>
sl@0
    67
#include <stdio.h>
sl@0
    68
#include <stdlib.h>
sl@0
    69
#include <string.h>
sl@0
    70
sl@0
    71
sl@0
    72
#include "e_os.h"
sl@0
    73
#include <openssl/crypto.h>
sl@0
    74
#include <openssl/rand.h>
sl@0
    75
#include <openssl/buffer.h>
sl@0
    76
sl@0
    77
# include <sys/types.h>
sl@0
    78
sl@0
    79
#ifdef OPENSSL_SYS_VMS
sl@0
    80
#include <unixio.h>
sl@0
    81
#endif
sl@0
    82
#ifndef NO_SYS_TYPES_H
sl@0
    83
# include <sys/types.h>
sl@0
    84
#endif
sl@0
    85
#ifdef MAC_OS_pre_X
sl@0
    86
# include <stat.h>
sl@0
    87
#else
sl@0
    88
# include <sys/stat.h>
sl@0
    89
#endif
sl@0
    90
sl@0
    91
#undef BUFSIZE
sl@0
    92
#ifndef SYMBIAN
sl@0
    93
#define BUFSIZE	1024
sl@0
    94
#define RAND_DATA 1024
sl@0
    95
#else
sl@0
    96
#define BUFSIZE	512
sl@0
    97
#define RAND_DATA 512
sl@0
    98
#endif
sl@0
    99
sl@0
   100
sl@0
   101
/* #define RFILE ".rnd" - defined in ../../e_os.h */
sl@0
   102
sl@0
   103
/* Note that these functions are intended for seed files only.
sl@0
   104
 * Entropy devices and EGD sockets are handled in rand_unix.c */
sl@0
   105
sl@0
   106
EXPORT_C int RAND_load_file(const char *file, long bytes)
sl@0
   107
	{
sl@0
   108
	/* If bytes >= 0, read up to 'bytes' bytes.
sl@0
   109
	 * if bytes == -1, read complete file. */
sl@0
   110
sl@0
   111
	MS_STATIC unsigned char buf[BUFSIZE];
sl@0
   112
	struct stat sb;
sl@0
   113
	int i,ret=0,n;
sl@0
   114
	FILE *in;
sl@0
   115
sl@0
   116
	if (file == NULL) return(0);
sl@0
   117
sl@0
   118
	if (stat(file,&sb) < 0) return(0);
sl@0
   119
	RAND_add(&sb,sizeof(sb),0.0);
sl@0
   120
	if (bytes == 0) return(ret);
sl@0
   121
sl@0
   122
	in=fopen(file,"rb");
sl@0
   123
	if (in == NULL) goto err;
sl@0
   124
#if defined(S_IFBLK) && defined(S_IFCHR)
sl@0
   125
	if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
sl@0
   126
	  /* this file is a device. we don't want read an infinite number
sl@0
   127
	   * of bytes from a random device, nor do we want to use buffered
sl@0
   128
	   * I/O because we will waste system entropy. 
sl@0
   129
	   */
sl@0
   130
	  bytes = (bytes == -1) ? 2048 : bytes; /* ok, is 2048 enough? */
sl@0
   131
	  setvbuf(in, NULL, _IONBF, 0); /* don't do buffered reads */
sl@0
   132
	}
sl@0
   133
#endif
sl@0
   134
	for (;;)
sl@0
   135
		{
sl@0
   136
		if (bytes > 0)
sl@0
   137
			n = (bytes < BUFSIZE)?(int)bytes:BUFSIZE;
sl@0
   138
		else
sl@0
   139
			n = BUFSIZE;
sl@0
   140
		i=fread(buf,1,n,in);
sl@0
   141
		if (i <= 0) break;
sl@0
   142
		/* even if n != i, use the full array */
sl@0
   143
		RAND_add(buf,n,(double)i);
sl@0
   144
		ret+=i;
sl@0
   145
		if (bytes > 0)
sl@0
   146
			{
sl@0
   147
			bytes-=n;
sl@0
   148
			if (bytes <= 0) break;
sl@0
   149
			}
sl@0
   150
		}
sl@0
   151
	fclose(in);
sl@0
   152
	OPENSSL_cleanse(buf,BUFSIZE);
sl@0
   153
err:
sl@0
   154
	return(ret);
sl@0
   155
	}
sl@0
   156
sl@0
   157
EXPORT_C int RAND_write_file(const char *file)
sl@0
   158
	{
sl@0
   159
	unsigned char buf[BUFSIZE];
sl@0
   160
	int i,ret=0,rand_err=0;
sl@0
   161
	FILE *out = NULL;
sl@0
   162
	int n;
sl@0
   163
	struct stat sb;
sl@0
   164
	
sl@0
   165
	i=stat(file,&sb);
sl@0
   166
	if (i != -1) { 
sl@0
   167
#if defined(S_IFBLK) && defined(S_IFCHR)
sl@0
   168
	  if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
sl@0
   169
	    /* this file is a device. we don't write back to it. 
sl@0
   170
	     * we "succeed" on the assumption this is some sort 
sl@0
   171
	     * of random device. Otherwise attempting to write to 
sl@0
   172
	     * and chmod the device causes problems.
sl@0
   173
	     */
sl@0
   174
	    return(1); 
sl@0
   175
	  }
sl@0
   176
#endif
sl@0
   177
	}
sl@0
   178
sl@0
   179
#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32)
sl@0
   180
	{
sl@0
   181
	/* For some reason Win32 can't write to files created this way */
sl@0
   182
	
sl@0
   183
	/* chmod(..., 0600) is too late to protect the file,
sl@0
   184
	 * permissions should be restrictive from the start */
sl@0
   185
	int fd = open(file, O_CREAT, 0600);
sl@0
   186
	if (fd != -1)
sl@0
   187
		out = fdopen(fd, "wb");
sl@0
   188
	}
sl@0
   189
#endif
sl@0
   190
	if (out == NULL)
sl@0
   191
		out = fopen(file,"wb");
sl@0
   192
	if (out == NULL) goto err;
sl@0
   193
sl@0
   194
#ifndef NO_CHMOD
sl@0
   195
	chmod(file,0600);
sl@0
   196
#endif
sl@0
   197
	n=RAND_DATA;
sl@0
   198
	for (;;)
sl@0
   199
		{
sl@0
   200
		i=(n > BUFSIZE)?BUFSIZE:n;
sl@0
   201
		n-=BUFSIZE;
sl@0
   202
		if (RAND_bytes(buf,i) <= 0)
sl@0
   203
			rand_err=1;
sl@0
   204
		i=fwrite(buf,1,i,out);
sl@0
   205
		if (i <= 0)
sl@0
   206
			{
sl@0
   207
			ret=0;
sl@0
   208
			break;
sl@0
   209
			}
sl@0
   210
		ret+=i;
sl@0
   211
		if (n <= 0) break;
sl@0
   212
                }
sl@0
   213
#ifdef OPENSSL_SYS_VMS
sl@0
   214
	/* Try to delete older versions of the file, until there aren't
sl@0
   215
	   any */
sl@0
   216
	{
sl@0
   217
	char *tmpf;
sl@0
   218
sl@0
   219
	tmpf = OPENSSL_malloc(strlen(file) + 4);  /* to add ";-1" and a nul */
sl@0
   220
	if (tmpf)
sl@0
   221
		{
sl@0
   222
		strcpy(tmpf, file);
sl@0
   223
		strcat(tmpf, ";-1");
sl@0
   224
		while(delete(tmpf) == 0)
sl@0
   225
			;
sl@0
   226
		rename(file,";1"); /* Make sure it's version 1, or we
sl@0
   227
				      will reach the limit (32767) at
sl@0
   228
				      some point... */
sl@0
   229
		}
sl@0
   230
	}
sl@0
   231
#endif /* OPENSSL_SYS_VMS */
sl@0
   232
sl@0
   233
	fclose(out);
sl@0
   234
	OPENSSL_cleanse(buf,BUFSIZE);
sl@0
   235
err:
sl@0
   236
	return (rand_err ? -1 : ret);
sl@0
   237
	}
sl@0
   238
sl@0
   239
EXPORT_C const char *RAND_file_name(char *buf, size_t size)
sl@0
   240
	{
sl@0
   241
	char *s=NULL;
sl@0
   242
	int ok = 0;
sl@0
   243
#ifdef __OpenBSD__
sl@0
   244
	struct stat sb;
sl@0
   245
#endif
sl@0
   246
sl@0
   247
	if (OPENSSL_issetugid() == 0)
sl@0
   248
		s=getenv("RANDFILE");
sl@0
   249
	if (s != NULL && *s && strlen(s) + 1 < size)
sl@0
   250
		{
sl@0
   251
		if (BUF_strlcpy(buf,s,size) >= size)
sl@0
   252
			return NULL;
sl@0
   253
		}
sl@0
   254
	else
sl@0
   255
		{
sl@0
   256
		if (OPENSSL_issetugid() == 0)
sl@0
   257
			s=getenv("HOME");
sl@0
   258
#ifdef DEFAULT_HOME
sl@0
   259
		if (s == NULL)
sl@0
   260
			{
sl@0
   261
			s = DEFAULT_HOME;
sl@0
   262
			}
sl@0
   263
#endif
sl@0
   264
		if (s && *s && strlen(s)+strlen(RFILE)+2 < size)
sl@0
   265
			{
sl@0
   266
			BUF_strlcpy(buf,s,size);
sl@0
   267
#ifndef OPENSSL_SYS_VMS
sl@0
   268
			BUF_strlcat(buf,"/",size);
sl@0
   269
#endif
sl@0
   270
			BUF_strlcat(buf,RFILE,size);
sl@0
   271
			ok = 1;
sl@0
   272
			}
sl@0
   273
		else
sl@0
   274
		  	buf[0] = '\0'; /* no file name */
sl@0
   275
		}
sl@0
   276
sl@0
   277
#ifdef __OpenBSD__
sl@0
   278
	/* given that all random loads just fail if the file can't be 
sl@0
   279
	 * seen on a stat, we stat the file we're returning, if it
sl@0
   280
	 * fails, use /dev/arandom instead. this allows the user to 
sl@0
   281
	 * use their own source for good random data, but defaults
sl@0
   282
	 * to something hopefully decent if that isn't available. 
sl@0
   283
	 */
sl@0
   284
sl@0
   285
	if (!ok)
sl@0
   286
		if (BUF_strlcpy(buf,"/dev/arandom",size) >= size) {
sl@0
   287
			return(NULL);
sl@0
   288
		}	
sl@0
   289
	if (stat(buf,&sb) == -1)
sl@0
   290
		if (BUF_strlcpy(buf,"/dev/arandom",size) >= size) {
sl@0
   291
			return(NULL);
sl@0
   292
		}	
sl@0
   293
sl@0
   294
#endif
sl@0
   295
	return(buf);
sl@0
   296
	}