randfile.c revision 109998
155714Skris/* crypto/rand/randfile.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <errno.h> 6055714Skris#include <stdio.h> 6155714Skris#include <stdlib.h> 6255714Skris#include <string.h> 6355714Skris 64109998Smarkm#include "e_os.h" 65109998Smarkm#include <openssl/crypto.h> 66109998Smarkm#include <openssl/rand.h> 67109998Smarkm 68109998Smarkm#ifdef OPENSSL_SYS_VMS 6959191Skris#include <unixio.h> 7059191Skris#endif 7159191Skris#ifndef NO_SYS_TYPES_H 7259191Skris# include <sys/types.h> 7359191Skris#endif 7459191Skris#ifdef MAC_OS_pre_X 7559191Skris# include <stat.h> 7659191Skris#else 7759191Skris# include <sys/stat.h> 7859191Skris#endif 7959191Skris 8055714Skris#undef BUFSIZE 8155714Skris#define BUFSIZE 1024 8255714Skris#define RAND_DATA 1024 8355714Skris 8459191Skris/* #define RFILE ".rnd" - defined in ../../e_os.h */ 8555714Skris 8672613Skris/* Note that these functions are intended for seed files only. 8772613Skris * Entropy devices and EGD sockets are handled in rand_unix.c */ 8872613Skris 8955714Skrisint RAND_load_file(const char *file, long bytes) 9055714Skris { 9159191Skris /* If bytes >= 0, read up to 'bytes' bytes. 9259191Skris * if bytes == -1, read complete file. */ 9359191Skris 9455714Skris MS_STATIC unsigned char buf[BUFSIZE]; 9555714Skris struct stat sb; 9655714Skris int i,ret=0,n; 9755714Skris FILE *in; 9855714Skris 9955714Skris if (file == NULL) return(0); 10055714Skris 10155714Skris i=stat(file,&sb); 10255714Skris /* If the state fails, put some crap in anyway */ 10359191Skris RAND_add(&sb,sizeof(sb),0); 10455714Skris if (i < 0) return(0); 10559191Skris if (bytes == 0) return(ret); 10655714Skris 10755714Skris in=fopen(file,"rb"); 10855714Skris if (in == NULL) goto err; 10955714Skris for (;;) 11055714Skris { 11159191Skris if (bytes > 0) 11259191Skris n = (bytes < BUFSIZE)?(int)bytes:BUFSIZE; 11359191Skris else 11459191Skris n = BUFSIZE; 11555714Skris i=fread(buf,1,n,in); 11655714Skris if (i <= 0) break; 11755714Skris /* even if n != i, use the full array */ 11859191Skris RAND_add(buf,n,i); 11955714Skris ret+=i; 12059191Skris if (bytes > 0) 12159191Skris { 12259191Skris bytes-=n; 12372613Skris if (bytes <= 0) break; 12459191Skris } 12555714Skris } 12655714Skris fclose(in); 127109998Smarkm OPENSSL_cleanse(buf,BUFSIZE); 12855714Skriserr: 12955714Skris return(ret); 13055714Skris } 13155714Skris 13255714Skrisint RAND_write_file(const char *file) 13355714Skris { 13455714Skris unsigned char buf[BUFSIZE]; 13568651Skris int i,ret=0,rand_err=0; 13659191Skris FILE *out = NULL; 13755714Skris int n; 13859191Skris 139109998Smarkm#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32) 14059191Skris /* For some reason Win32 can't write to files created this way */ 14168651Skris 14268651Skris /* chmod(..., 0600) is too late to protect the file, 14368651Skris * permissions should be restrictive from the start */ 14468651Skris int fd = open(file, O_CREAT, 0600); 14568651Skris if (fd != -1) 14668651Skris out = fdopen(fd, "wb"); 14759191Skris#endif 14868651Skris if (out == NULL) 14968651Skris out = fopen(file,"wb"); 15068651Skris if (out == NULL) goto err; 15159191Skris 15259191Skris#ifndef NO_CHMOD 15355714Skris chmod(file,0600); 15459191Skris#endif 15555714Skris n=RAND_DATA; 15655714Skris for (;;) 15755714Skris { 15855714Skris i=(n > BUFSIZE)?BUFSIZE:n; 15955714Skris n-=BUFSIZE; 16059191Skris if (RAND_bytes(buf,i) <= 0) 16168651Skris rand_err=1; 16255714Skris i=fwrite(buf,1,i,out); 16355714Skris if (i <= 0) 16455714Skris { 16555714Skris ret=0; 16655714Skris break; 16755714Skris } 16855714Skris ret+=i; 16955714Skris if (n <= 0) break; 17059191Skris } 171109998Smarkm#ifdef OPENSSL_SYS_VMS 17259191Skris /* Try to delete older versions of the file, until there aren't 17359191Skris any */ 17459191Skris { 17559191Skris char *tmpf; 17659191Skris 17768651Skris tmpf = OPENSSL_malloc(strlen(file) + 4); /* to add ";-1" and a nul */ 17859191Skris if (tmpf) 17959191Skris { 18059191Skris strcpy(tmpf, file); 18159191Skris strcat(tmpf, ";-1"); 18259191Skris while(delete(tmpf) == 0) 18359191Skris ; 18459191Skris rename(file,";1"); /* Make sure it's version 1, or we 18559191Skris will reach the limit (32767) at 18659191Skris some point... */ 18755714Skris } 18859191Skris } 189109998Smarkm#endif /* OPENSSL_SYS_VMS */ 19059191Skris 19155714Skris fclose(out); 192109998Smarkm OPENSSL_cleanse(buf,BUFSIZE); 19355714Skriserr: 19468651Skris return (rand_err ? -1 : ret); 19555714Skris } 19655714Skris 19776866Skrisconst char *RAND_file_name(char *buf, size_t size) 19855714Skris { 19976866Skris char *s=NULL; 20055714Skris char *ret=NULL; 20155714Skris 20276866Skris if (OPENSSL_issetugid() == 0) 20376866Skris s=getenv("RANDFILE"); 20455714Skris if (s != NULL) 20555714Skris { 206109998Smarkm if(strlen(s) >= size) 207109998Smarkm return NULL; 208109998Smarkm strcpy(buf,s); 20955714Skris ret=buf; 21055714Skris } 21155714Skris else 21255714Skris { 21376866Skris if (OPENSSL_issetugid() == 0) 21476866Skris s=getenv("HOME"); 21579998Skris#ifdef DEFAULT_HOME 21679998Skris if (s == NULL) 21779998Skris { 21879998Skris s = DEFAULT_HOME; 21979998Skris } 22079998Skris#endif 22176866Skris if (s != NULL && (strlen(s)+strlen(RFILE)+2 < size)) 22276866Skris { 22376866Skris strcpy(buf,s); 224109998Smarkm#ifndef OPENSSL_SYS_VMS 22576866Skris strcat(buf,"/"); 22655714Skris#endif 22776866Skris strcat(buf,RFILE); 22876866Skris ret=buf; 22976866Skris } 23079998Skris else 23176866Skris buf[0] = '\0'; /* no file name */ 23255714Skris } 23355714Skris return(ret); 23455714Skris } 235