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 #ifndef NO_SYS_TYPES_H
25 # include <sys/types.h>
27 #ifndef OPENSSL_NO_POSIX_IO
28 # include <sys/stat.h>
31 * Following should not be needed, and we could have been stricter
32 * and demand S_IS*. But some systems just don't comply... Formally
33 * below macros are "anatomically incorrect", because normally they
34 * would look like ((m) & MASK == TYPE), but since MASK availability
35 * is as questionable, we settle for this poor-man fallback...
37 # if !defined(S_ISBLK)
38 # if defined(_S_IFBLK)
39 # define S_ISBLK(m) ((m) & _S_IFBLK)
40 # elif defined(S_IFBLK)
41 # define S_ISBLK(m) ((m) & S_IFBLK)
42 # elif defined(_WIN32)
43 # define S_ISBLK(m) 0 /* no concept of block devices on Windows */
46 # if !defined(S_ISCHR)
47 # if defined(_S_IFCHR)
48 # define S_ISCHR(m) ((m) & _S_IFCHR)
49 # elif defined(S_IFCHR)
50 # define S_ISCHR(m) ((m) & S_IFCHR)
59 # define fdopen _fdopen
61 # define fileno _fileno
66 #define RAND_DATA 1024
68 #ifdef OPENSSL_SYS_VMS
70 * Misc hacks needed for specific cases.
72 * __FILE_ptr32 is a type provided by DEC C headers (types.h specifically)
73 * to make sure the FILE* is a 32-bit pointer no matter what. We know that
74 * stdio function return this type (a study of stdio.h proves it).
75 * Additionally, we create a similar char pointer type for the sake of
78 # if __INITIAL_POINTER_SIZE == 64
79 # pragma pointer_size save
80 # pragma pointer_size 32
81 typedef char *char_ptr32;
82 # pragma pointer_size restore
84 * On VMS, setbuf() will only take 32-bit pointers, and a compilation
85 * with /POINTER_SIZE=64 will give off a MAYLOSEDATA2 warning here.
86 * Since we know that the FILE* really is a 32-bit pointer expanded to
87 * 64 bits, we also know it's safe to convert it back to a 32-bit pointer.
88 * As for the buffer parameter, we only use NULL here, so that passes as
91 # define setbuf(fp,buf) (setbuf)((__FILE_ptr32)(fp), (char_ptr32)(buf))
95 * This declaration is a nasty hack to get around vms' extension to fopen for
96 * passing in sharing options being disabled by /STANDARD=ANSI89
98 static __FILE_ptr32 (*const vms_fopen)(const char *, const char *, ...) =
99 (__FILE_ptr32 (*)(const char *, const char *, ...))fopen;
100 # define VMS_OPEN_ATTRS "shr=get,put,upd,del","ctx=bin,stm","rfm=stm","rat=none","mrs=0"
102 # define openssl_fopen(fname,mode) vms_fopen((fname), (mode), VMS_OPEN_ATTRS)
108 * Note that these functions are intended for seed files only. Entropy
109 * devices and EGD sockets are handled in rand_unix.c
112 int RAND_load_file(const char *file, long bytes)
115 * If bytes >= 0, read up to 'bytes' bytes.
116 * if bytes == -1, read complete file.
119 unsigned char buf[BUFSIZE];
120 #ifndef OPENSSL_NO_POSIX_IO
132 in = openssl_fopen(file, "rb");
136 #ifndef OPENSSL_NO_POSIX_IO
138 * struct stat can have padding and unused fields that may not be
139 * initialized in the call to stat(). We need to clear the entire
140 * structure before calling RAND_add() to avoid complaints from
141 * applications such as Valgrind.
143 memset(&sb, 0, sizeof(sb));
144 if (fstat(fileno(in), &sb) < 0)
146 RAND_add(&sb, sizeof(sb), 0.0);
151 n = (bytes < BUFSIZE) ? (int)bytes : BUFSIZE;
154 i = fread(buf, 1, n, in);
158 RAND_add(buf, i, (double)i);
166 OPENSSL_cleanse(buf, BUFSIZE);
173 int RAND_write_file(const char *file)
175 unsigned char buf[BUFSIZE];
176 int i, ret = 0, rand_err = 0;
179 #ifndef OPENSSL_NO_POSIX_IO
181 # if defined(S_ISBLK) && defined(S_ISCHR)
184 * Check for |file| being a driver as "ASCII-safe" on Windows,
185 * because driver paths are always ASCII.
191 #if defined(O_CREAT) && !defined(OPENSSL_NO_POSIX_IO) && \
192 !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WINDOWS)
198 * chmod(..., 0600) is too late to protect the file, permissions
199 * should be restrictive from the start
201 int fd = open(file, O_WRONLY | O_CREAT | O_BINARY, 0600);
203 out = fdopen(fd, "wb");
207 #ifdef OPENSSL_SYS_VMS
209 * VMS NOTE: Prior versions of this routine created a _new_ version of
210 * the rand file for each call into this routine, then deleted all
211 * existing versions named ;-1, and finally renamed the current version
212 * as ';1'. Under concurrent usage, this resulted in an RMS race
213 * condition in rename() which could orphan files (see vms message help
214 * for RMS$_REENT). With the fopen() calls below, openssl/VMS now shares
215 * the top-level version of the rand file. Note that there may still be
216 * conditions where the top-level rand file is locked. If so, this code
217 * will then create a new version of the rand file. Without the delete
218 * and rename code, this can result in ascending file versions that stop
219 * at version 32767, and this routine will then return an error. The
220 * remedy for this is to recode the calling application to avoid
221 * concurrent use of the rand file, or synchronize usage at the
222 * application level. Also consider whether or not you NEED a persistent
223 * rand file in a concurrent use situation.
226 out = openssl_fopen(file, "rb+");
229 out = openssl_fopen(file, "wb");
233 #if !defined(NO_CHMOD) && !defined(OPENSSL_NO_POSIX_IO)
238 i = (n > BUFSIZE) ? BUFSIZE : n;
240 if (RAND_bytes(buf, i) <= 0)
242 i = fwrite(buf, 1, i, out);
253 OPENSSL_cleanse(buf, BUFSIZE);
255 return (rand_err ? -1 : ret);
258 const char *RAND_file_name(char *buf, size_t size)
261 int use_randfile = 1;
263 #if defined(_WIN32) && defined(CP_UTF8)
267 if ((var = L"RANDFILE",
268 len = GetEnvironmentVariableW(var, NULL, 0)) == 0
269 && (var = L"HOME", use_randfile = 0,
270 len = GetEnvironmentVariableW(var, NULL, 0)) == 0
271 && (var = L"USERPROFILE",
272 len = GetEnvironmentVariableW(var, NULL, 0)) == 0) {
274 len = GetEnvironmentVariableW(var, NULL, 0);
280 val = _alloca(len * sizeof(WCHAR));
282 if (GetEnvironmentVariableW(var, val, len) < len
283 && (sz = WideCharToMultiByte(CP_UTF8, 0, val, -1, NULL, 0,
286 if (WideCharToMultiByte(CP_UTF8, 0, val, -1, s, sz,
292 if (OPENSSL_issetugid() != 0) {
295 s = getenv("RANDFILE");
296 if (s == NULL || *s == '\0') {
303 if (!use_randfile && s == NULL) {
307 if (s != NULL && *s) {
308 size_t len = strlen(s);
310 if (use_randfile && len + 1 < size) {
311 if (OPENSSL_strlcpy(buf, s, size) >= size)
313 } else if (len + strlen(RFILE) + 2 < size) {
314 OPENSSL_strlcpy(buf, s, size);
315 #ifndef OPENSSL_SYS_VMS
316 OPENSSL_strlcat(buf, "/", size);
318 OPENSSL_strlcat(buf, RFILE, size);
321 buf[0] = '\0'; /* no file name */
324 return buf[0] ? buf : NULL;