os/ossrv/ssl/libcrypto/src/crypto/rand/randfile.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/rand/randfile.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,296 @@
     1.4 +/* crypto/rand/randfile.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +/*
    1.62 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.63 + */
    1.64 +/* We need to define this to get macros like S_IFBLK and S_IFCHR */
    1.65 +#define _XOPEN_SOURCE 500
    1.66 +
    1.67 +
    1.68 +
    1.69 +#include <errno.h>
    1.70 +#include <stdio.h>
    1.71 +#include <stdlib.h>
    1.72 +#include <string.h>
    1.73 +
    1.74 +
    1.75 +#include "e_os.h"
    1.76 +#include <openssl/crypto.h>
    1.77 +#include <openssl/rand.h>
    1.78 +#include <openssl/buffer.h>
    1.79 +
    1.80 +# include <sys/types.h>
    1.81 +
    1.82 +#ifdef OPENSSL_SYS_VMS
    1.83 +#include <unixio.h>
    1.84 +#endif
    1.85 +#ifndef NO_SYS_TYPES_H
    1.86 +# include <sys/types.h>
    1.87 +#endif
    1.88 +#ifdef MAC_OS_pre_X
    1.89 +# include <stat.h>
    1.90 +#else
    1.91 +# include <sys/stat.h>
    1.92 +#endif
    1.93 +
    1.94 +#undef BUFSIZE
    1.95 +#ifndef SYMBIAN
    1.96 +#define BUFSIZE	1024
    1.97 +#define RAND_DATA 1024
    1.98 +#else
    1.99 +#define BUFSIZE	512
   1.100 +#define RAND_DATA 512
   1.101 +#endif
   1.102 +
   1.103 +
   1.104 +/* #define RFILE ".rnd" - defined in ../../e_os.h */
   1.105 +
   1.106 +/* Note that these functions are intended for seed files only.
   1.107 + * Entropy devices and EGD sockets are handled in rand_unix.c */
   1.108 +
   1.109 +EXPORT_C int RAND_load_file(const char *file, long bytes)
   1.110 +	{
   1.111 +	/* If bytes >= 0, read up to 'bytes' bytes.
   1.112 +	 * if bytes == -1, read complete file. */
   1.113 +
   1.114 +	MS_STATIC unsigned char buf[BUFSIZE];
   1.115 +	struct stat sb;
   1.116 +	int i,ret=0,n;
   1.117 +	FILE *in;
   1.118 +
   1.119 +	if (file == NULL) return(0);
   1.120 +
   1.121 +	if (stat(file,&sb) < 0) return(0);
   1.122 +	RAND_add(&sb,sizeof(sb),0.0);
   1.123 +	if (bytes == 0) return(ret);
   1.124 +
   1.125 +	in=fopen(file,"rb");
   1.126 +	if (in == NULL) goto err;
   1.127 +#if defined(S_IFBLK) && defined(S_IFCHR)
   1.128 +	if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
   1.129 +	  /* this file is a device. we don't want read an infinite number
   1.130 +	   * of bytes from a random device, nor do we want to use buffered
   1.131 +	   * I/O because we will waste system entropy. 
   1.132 +	   */
   1.133 +	  bytes = (bytes == -1) ? 2048 : bytes; /* ok, is 2048 enough? */
   1.134 +	  setvbuf(in, NULL, _IONBF, 0); /* don't do buffered reads */
   1.135 +	}
   1.136 +#endif
   1.137 +	for (;;)
   1.138 +		{
   1.139 +		if (bytes > 0)
   1.140 +			n = (bytes < BUFSIZE)?(int)bytes:BUFSIZE;
   1.141 +		else
   1.142 +			n = BUFSIZE;
   1.143 +		i=fread(buf,1,n,in);
   1.144 +		if (i <= 0) break;
   1.145 +		/* even if n != i, use the full array */
   1.146 +		RAND_add(buf,n,(double)i);
   1.147 +		ret+=i;
   1.148 +		if (bytes > 0)
   1.149 +			{
   1.150 +			bytes-=n;
   1.151 +			if (bytes <= 0) break;
   1.152 +			}
   1.153 +		}
   1.154 +	fclose(in);
   1.155 +	OPENSSL_cleanse(buf,BUFSIZE);
   1.156 +err:
   1.157 +	return(ret);
   1.158 +	}
   1.159 +
   1.160 +EXPORT_C int RAND_write_file(const char *file)
   1.161 +	{
   1.162 +	unsigned char buf[BUFSIZE];
   1.163 +	int i,ret=0,rand_err=0;
   1.164 +	FILE *out = NULL;
   1.165 +	int n;
   1.166 +	struct stat sb;
   1.167 +	
   1.168 +	i=stat(file,&sb);
   1.169 +	if (i != -1) { 
   1.170 +#if defined(S_IFBLK) && defined(S_IFCHR)
   1.171 +	  if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
   1.172 +	    /* this file is a device. we don't write back to it. 
   1.173 +	     * we "succeed" on the assumption this is some sort 
   1.174 +	     * of random device. Otherwise attempting to write to 
   1.175 +	     * and chmod the device causes problems.
   1.176 +	     */
   1.177 +	    return(1); 
   1.178 +	  }
   1.179 +#endif
   1.180 +	}
   1.181 +
   1.182 +#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32)
   1.183 +	{
   1.184 +	/* For some reason Win32 can't write to files created this way */
   1.185 +	
   1.186 +	/* chmod(..., 0600) is too late to protect the file,
   1.187 +	 * permissions should be restrictive from the start */
   1.188 +	int fd = open(file, O_CREAT, 0600);
   1.189 +	if (fd != -1)
   1.190 +		out = fdopen(fd, "wb");
   1.191 +	}
   1.192 +#endif
   1.193 +	if (out == NULL)
   1.194 +		out = fopen(file,"wb");
   1.195 +	if (out == NULL) goto err;
   1.196 +
   1.197 +#ifndef NO_CHMOD
   1.198 +	chmod(file,0600);
   1.199 +#endif
   1.200 +	n=RAND_DATA;
   1.201 +	for (;;)
   1.202 +		{
   1.203 +		i=(n > BUFSIZE)?BUFSIZE:n;
   1.204 +		n-=BUFSIZE;
   1.205 +		if (RAND_bytes(buf,i) <= 0)
   1.206 +			rand_err=1;
   1.207 +		i=fwrite(buf,1,i,out);
   1.208 +		if (i <= 0)
   1.209 +			{
   1.210 +			ret=0;
   1.211 +			break;
   1.212 +			}
   1.213 +		ret+=i;
   1.214 +		if (n <= 0) break;
   1.215 +                }
   1.216 +#ifdef OPENSSL_SYS_VMS
   1.217 +	/* Try to delete older versions of the file, until there aren't
   1.218 +	   any */
   1.219 +	{
   1.220 +	char *tmpf;
   1.221 +
   1.222 +	tmpf = OPENSSL_malloc(strlen(file) + 4);  /* to add ";-1" and a nul */
   1.223 +	if (tmpf)
   1.224 +		{
   1.225 +		strcpy(tmpf, file);
   1.226 +		strcat(tmpf, ";-1");
   1.227 +		while(delete(tmpf) == 0)
   1.228 +			;
   1.229 +		rename(file,";1"); /* Make sure it's version 1, or we
   1.230 +				      will reach the limit (32767) at
   1.231 +				      some point... */
   1.232 +		}
   1.233 +	}
   1.234 +#endif /* OPENSSL_SYS_VMS */
   1.235 +
   1.236 +	fclose(out);
   1.237 +	OPENSSL_cleanse(buf,BUFSIZE);
   1.238 +err:
   1.239 +	return (rand_err ? -1 : ret);
   1.240 +	}
   1.241 +
   1.242 +EXPORT_C const char *RAND_file_name(char *buf, size_t size)
   1.243 +	{
   1.244 +	char *s=NULL;
   1.245 +	int ok = 0;
   1.246 +#ifdef __OpenBSD__
   1.247 +	struct stat sb;
   1.248 +#endif
   1.249 +
   1.250 +	if (OPENSSL_issetugid() == 0)
   1.251 +		s=getenv("RANDFILE");
   1.252 +	if (s != NULL && *s && strlen(s) + 1 < size)
   1.253 +		{
   1.254 +		if (BUF_strlcpy(buf,s,size) >= size)
   1.255 +			return NULL;
   1.256 +		}
   1.257 +	else
   1.258 +		{
   1.259 +		if (OPENSSL_issetugid() == 0)
   1.260 +			s=getenv("HOME");
   1.261 +#ifdef DEFAULT_HOME
   1.262 +		if (s == NULL)
   1.263 +			{
   1.264 +			s = DEFAULT_HOME;
   1.265 +			}
   1.266 +#endif
   1.267 +		if (s && *s && strlen(s)+strlen(RFILE)+2 < size)
   1.268 +			{
   1.269 +			BUF_strlcpy(buf,s,size);
   1.270 +#ifndef OPENSSL_SYS_VMS
   1.271 +			BUF_strlcat(buf,"/",size);
   1.272 +#endif
   1.273 +			BUF_strlcat(buf,RFILE,size);
   1.274 +			ok = 1;
   1.275 +			}
   1.276 +		else
   1.277 +		  	buf[0] = '\0'; /* no file name */
   1.278 +		}
   1.279 +
   1.280 +#ifdef __OpenBSD__
   1.281 +	/* given that all random loads just fail if the file can't be 
   1.282 +	 * seen on a stat, we stat the file we're returning, if it
   1.283 +	 * fails, use /dev/arandom instead. this allows the user to 
   1.284 +	 * use their own source for good random data, but defaults
   1.285 +	 * to something hopefully decent if that isn't available. 
   1.286 +	 */
   1.287 +
   1.288 +	if (!ok)
   1.289 +		if (BUF_strlcpy(buf,"/dev/arandom",size) >= size) {
   1.290 +			return(NULL);
   1.291 +		}	
   1.292 +	if (stat(buf,&sb) == -1)
   1.293 +		if (BUF_strlcpy(buf,"/dev/arandom",size) >= size) {
   1.294 +			return(NULL);
   1.295 +		}	
   1.296 +
   1.297 +#endif
   1.298 +	return(buf);
   1.299 +	}