2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
17 #include <openssl/crypto.h>
18 #include <openssl/rand.h>
19 #include <openssl/buffer.h>
21 #ifdef OPENSSL_SYS_VMS
24 #include <sys/types.h>
25 #ifndef OPENSSL_NO_POSIX_IO
26 # include <sys/stat.h>
34 # define fdopen _fdopen
36 # define fileno _fileno
41 * Following should not be needed, and we could have been stricter
42 * and demand S_IS*. But some systems just don't comply... Formally
43 * below macros are "anatomically incorrect", because normally they
44 * would look like ((m) & MASK == TYPE), but since MASK availability
45 * is as questionable, we settle for this poor-man fallback...
47 # if !defined(S_ISREG)
48 # define S_ISREG(m) ((m) & S_IFREG)
51 #define RAND_FILE_SIZE 1024
54 #ifdef OPENSSL_SYS_VMS
56 * __FILE_ptr32 is a type provided by DEC C headers (types.h specifically)
57 * to make sure the FILE* is a 32-bit pointer no matter what. We know that
58 * stdio functions return this type (a study of stdio.h proves it).
60 * This declaration is a nasty hack to get around vms' extension to fopen for
61 * passing in sharing options being disabled by /STANDARD=ANSI89
63 static __FILE_ptr32 (*const vms_fopen)(const char *, const char *, ...) =
64 (__FILE_ptr32 (*)(const char *, const char *, ...))fopen;
65 # define VMS_OPEN_ATTRS \
66 "shr=get,put,upd,del","ctx=bin,stm","rfm=stm","rat=none","mrs=0"
67 # define openssl_fopen(fname, mode) vms_fopen((fname), (mode), VMS_OPEN_ATTRS)
71 * Note that these functions are intended for seed files only. Entropy
72 * devices and EGD sockets are handled in rand_unix.c If |bytes| is
73 * -1 read the complete file; otherwise read the specified amount.
75 int RAND_load_file(const char *file, long bytes)
77 unsigned char buf[RAND_FILE_SIZE];
78 #ifndef OPENSSL_NO_POSIX_IO
87 if ((in = openssl_fopen(file, "rb")) == NULL) {
88 RANDerr(RAND_F_RAND_LOAD_FILE, RAND_R_CANNOT_OPEN_FILE);
89 ERR_add_error_data(2, "Filename=", file);
93 #ifndef OPENSSL_NO_POSIX_IO
94 if (fstat(fileno(in), &sb) < 0) {
95 RANDerr(RAND_F_RAND_LOAD_FILE, RAND_R_INTERNAL_ERROR);
96 ERR_add_error_data(2, "Filename=", file);
100 if (!S_ISREG(sb.st_mode) && bytes < 0)
104 * Don't buffer, because even if |file| is regular file, we have
105 * no control over the buffer, so why would we want a copy of its
106 * contents lying around?
112 n = (bytes < RAND_FILE_SIZE) ? (int)bytes : RAND_FILE_SIZE;
115 i = fread(buf, 1, n, in);
117 if (ferror(in) && errno == EINTR){
126 RAND_add(buf, i, (double)i);
129 /* If given a bytecount, and we did it, break. */
130 if (bytes > 0 && (bytes -= i) <= 0)
134 OPENSSL_cleanse(buf, sizeof(buf));
139 int RAND_write_file(const char *file)
141 unsigned char buf[RAND_FILE_SIZE];
144 #ifndef OPENSSL_NO_POSIX_IO
147 if (stat(file, &sb) >= 0 && !S_ISREG(sb.st_mode)) {
148 RANDerr(RAND_F_RAND_WRITE_FILE, RAND_R_NOT_A_REGULAR_FILE);
149 ERR_add_error_data(2, "Filename=", file);
154 /* Collect enough random data. */
155 if (RAND_bytes(buf, (int)sizeof(buf)) != 1)
158 #if defined(O_CREAT) && !defined(OPENSSL_NO_POSIX_IO) && \
159 !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WINDOWS)
165 * chmod(..., 0600) is too late to protect the file, permissions
166 * should be restrictive from the start
168 int fd = open(file, O_WRONLY | O_CREAT | O_BINARY, 0600);
170 out = fdopen(fd, "wb");
174 #ifdef OPENSSL_SYS_VMS
176 * VMS NOTE: Prior versions of this routine created a _new_ version of
177 * the rand file for each call into this routine, then deleted all
178 * existing versions named ;-1, and finally renamed the current version
179 * as ';1'. Under concurrent usage, this resulted in an RMS race
180 * condition in rename() which could orphan files (see vms message help
181 * for RMS$_REENT). With the fopen() calls below, openssl/VMS now shares
182 * the top-level version of the rand file. Note that there may still be
183 * conditions where the top-level rand file is locked. If so, this code
184 * will then create a new version of the rand file. Without the delete
185 * and rename code, this can result in ascending file versions that stop
186 * at version 32767, and this routine will then return an error. The
187 * remedy for this is to recode the calling application to avoid
188 * concurrent use of the rand file, or synchronize usage at the
189 * application level. Also consider whether or not you NEED a persistent
190 * rand file in a concurrent use situation.
192 out = openssl_fopen(file, "rb+");
196 out = openssl_fopen(file, "wb");
198 RANDerr(RAND_F_RAND_WRITE_FILE, RAND_R_CANNOT_OPEN_FILE);
199 ERR_add_error_data(2, "Filename=", file);
203 #if !defined(NO_CHMOD) && !defined(OPENSSL_NO_POSIX_IO)
205 * Yes it's late to do this (see above comment), but better than nothing.
210 ret = fwrite(buf, 1, RAND_FILE_SIZE, out);
212 OPENSSL_cleanse(buf, RAND_FILE_SIZE);
216 const char *RAND_file_name(char *buf, size_t size)
220 int use_randfile = 1;
222 #if defined(_WIN32) && defined(CP_UTF8)
226 /* Look up various environment variables. */
227 if ((envlen = GetEnvironmentVariableW(var = L"RANDFILE", NULL, 0)) == 0) {
229 if ((envlen = GetEnvironmentVariableW(var = L"HOME", NULL, 0)) == 0
230 && (envlen = GetEnvironmentVariableW(var = L"USERPROFILE",
232 envlen = GetEnvironmentVariableW(var = L"SYSTEMROOT", NULL, 0);
235 /* If we got a value, allocate space to hold it and then get it. */
238 WCHAR *val = _alloca(envlen * sizeof(WCHAR));
240 if (GetEnvironmentVariableW(var, val, envlen) < envlen
241 && (sz = WideCharToMultiByte(CP_UTF8, 0, val, -1, NULL, 0,
244 if (WideCharToMultiByte(CP_UTF8, 0, val, -1, s, sz,
250 if (OPENSSL_issetugid() != 0) {
252 } else if ((s = getenv("RANDFILE")) == NULL || *s == '\0') {
259 if (!use_randfile && s == NULL)
262 if (s == NULL || *s == '\0')
271 if (len + 1 + strlen(RFILE) + 1 >= size)
274 #ifndef OPENSSL_SYS_VMS