1 | /*
|
---|
2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License 2.0 (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
|
---|
8 | */
|
---|
9 |
|
---|
10 | #if defined (__TANDEM) && defined (_SPT_MODEL_)
|
---|
11 | /*
|
---|
12 | * These definitions have to come first in SPT due to scoping of the
|
---|
13 | * declarations in c99 associated with SPT use of stat.
|
---|
14 | */
|
---|
15 | # include <sys/types.h>
|
---|
16 | # include <sys/stat.h>
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | #include "internal/cryptlib.h"
|
---|
20 |
|
---|
21 | #include <errno.h>
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <stdlib.h>
|
---|
24 | #include <string.h>
|
---|
25 |
|
---|
26 | #include <openssl/crypto.h>
|
---|
27 | #include <openssl/rand.h>
|
---|
28 | #include <openssl/buffer.h>
|
---|
29 |
|
---|
30 | #ifdef OPENSSL_SYS_VMS
|
---|
31 | # include <unixio.h>
|
---|
32 | #endif
|
---|
33 | #include <sys/types.h>
|
---|
34 | #ifndef OPENSSL_NO_POSIX_IO
|
---|
35 | # include <sys/stat.h>
|
---|
36 | # include <fcntl.h>
|
---|
37 | # if defined(_WIN32) && !defined(_WIN32_WCE)
|
---|
38 | # ifdef VBOX
|
---|
39 | # include <iprt/win/windows.h>
|
---|
40 | # else
|
---|
41 | # include <windows.h>
|
---|
42 | # endif
|
---|
43 | # include <io.h>
|
---|
44 | # define stat _stat
|
---|
45 | # define chmod _chmod
|
---|
46 | # define open _open
|
---|
47 | # define fdopen _fdopen
|
---|
48 | # define fstat _fstat
|
---|
49 | # define fileno _fileno
|
---|
50 | # endif
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Following should not be needed, and we could have been stricter
|
---|
55 | * and demand S_IS*. But some systems just don't comply... Formally
|
---|
56 | * below macros are "anatomically incorrect", because normally they
|
---|
57 | * would look like ((m) & MASK == TYPE), but since MASK availability
|
---|
58 | * is as questionable, we settle for this poor-man fallback...
|
---|
59 | */
|
---|
60 | # if !defined(S_ISREG)
|
---|
61 | # define S_ISREG(m) ((m) & S_IFREG)
|
---|
62 | # endif
|
---|
63 |
|
---|
64 | #define RAND_BUF_SIZE 1024
|
---|
65 | #define RFILE ".rnd"
|
---|
66 |
|
---|
67 | #ifdef OPENSSL_SYS_VMS
|
---|
68 | /*
|
---|
69 | * __FILE_ptr32 is a type provided by DEC C headers (types.h specifically)
|
---|
70 | * to make sure the FILE* is a 32-bit pointer no matter what. We know that
|
---|
71 | * stdio functions return this type (a study of stdio.h proves it).
|
---|
72 | *
|
---|
73 | * This declaration is a nasty hack to get around vms' extension to fopen for
|
---|
74 | * passing in sharing options being disabled by /STANDARD=ANSI89
|
---|
75 | */
|
---|
76 | static __FILE_ptr32 (*const vms_fopen)(const char *, const char *, ...) =
|
---|
77 | (__FILE_ptr32 (*)(const char *, const char *, ...))fopen;
|
---|
78 | # define VMS_OPEN_ATTRS \
|
---|
79 | "shr=get,put,upd,del","ctx=bin,stm","rfm=stm","rat=none","mrs=0"
|
---|
80 | # define openssl_fopen(fname, mode) vms_fopen((fname), (mode), VMS_OPEN_ATTRS)
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Note that these functions are intended for seed files only. Entropy
|
---|
85 | * devices and EGD sockets are handled in rand_unix.c If |bytes| is
|
---|
86 | * -1 read the complete file; otherwise read the specified amount.
|
---|
87 | */
|
---|
88 | int RAND_load_file(const char *file, long bytes)
|
---|
89 | {
|
---|
90 | /*
|
---|
91 | * The load buffer size exceeds the chunk size by the comfortable amount
|
---|
92 | * of 'RAND_DRBG_STRENGTH' bytes (not bits!). This is done on purpose
|
---|
93 | * to avoid calling RAND_add() with a small final chunk. Instead, such
|
---|
94 | * a small final chunk will be added together with the previous chunk
|
---|
95 | * (unless it's the only one).
|
---|
96 | */
|
---|
97 | #define RAND_LOAD_BUF_SIZE (RAND_BUF_SIZE + RAND_DRBG_STRENGTH)
|
---|
98 | unsigned char buf[RAND_LOAD_BUF_SIZE];
|
---|
99 |
|
---|
100 | #ifndef OPENSSL_NO_POSIX_IO
|
---|
101 | struct stat sb;
|
---|
102 | #endif
|
---|
103 | int i, n, ret = 0;
|
---|
104 | FILE *in;
|
---|
105 |
|
---|
106 | if (bytes == 0)
|
---|
107 | return 0;
|
---|
108 |
|
---|
109 | if ((in = openssl_fopen(file, "rb")) == NULL) {
|
---|
110 | ERR_raise_data(ERR_LIB_RAND, RAND_R_CANNOT_OPEN_FILE,
|
---|
111 | "Filename=%s", file);
|
---|
112 | return -1;
|
---|
113 | }
|
---|
114 |
|
---|
115 | #ifndef OPENSSL_NO_POSIX_IO
|
---|
116 | if (fstat(fileno(in), &sb) < 0) {
|
---|
117 | ERR_raise_data(ERR_LIB_RAND, RAND_R_INTERNAL_ERROR,
|
---|
118 | "Filename=%s", file);
|
---|
119 | fclose(in);
|
---|
120 | return -1;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (bytes < 0) {
|
---|
124 | if (S_ISREG(sb.st_mode))
|
---|
125 | bytes = sb.st_size;
|
---|
126 | else
|
---|
127 | bytes = RAND_DRBG_STRENGTH;
|
---|
128 | }
|
---|
129 | #endif
|
---|
130 | /*
|
---|
131 | * On VMS, setbuf() will only take 32-bit pointers, and a compilation
|
---|
132 | * with /POINTER_SIZE=64 will give off a MAYLOSEDATA2 warning here.
|
---|
133 | * However, we trust that the C RTL will never give us a FILE pointer
|
---|
134 | * above the first 4 GB of memory, so we simply turn off the warning
|
---|
135 | * temporarily.
|
---|
136 | */
|
---|
137 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
|
---|
138 | # pragma environment save
|
---|
139 | # pragma message disable maylosedata2
|
---|
140 | #endif
|
---|
141 | /*
|
---|
142 | * Don't buffer, because even if |file| is regular file, we have
|
---|
143 | * no control over the buffer, so why would we want a copy of its
|
---|
144 | * contents lying around?
|
---|
145 | */
|
---|
146 | setbuf(in, NULL);
|
---|
147 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
|
---|
148 | # pragma environment restore
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | for ( ; ; ) {
|
---|
152 | if (bytes > 0)
|
---|
153 | n = (bytes <= RAND_LOAD_BUF_SIZE) ? (int)bytes : RAND_BUF_SIZE;
|
---|
154 | else
|
---|
155 | n = RAND_LOAD_BUF_SIZE;
|
---|
156 | i = fread(buf, 1, n, in);
|
---|
157 | #ifdef EINTR
|
---|
158 | if (ferror(in) && errno == EINTR){
|
---|
159 | clearerr(in);
|
---|
160 | if (i == 0)
|
---|
161 | continue;
|
---|
162 | }
|
---|
163 | #endif
|
---|
164 | if (i == 0)
|
---|
165 | break;
|
---|
166 |
|
---|
167 | RAND_add(buf, i, (double)i);
|
---|
168 | ret += i;
|
---|
169 |
|
---|
170 | /* If given a bytecount, and we did it, break. */
|
---|
171 | if (bytes > 0 && (bytes -= i) <= 0)
|
---|
172 | break;
|
---|
173 | }
|
---|
174 |
|
---|
175 | OPENSSL_cleanse(buf, sizeof(buf));
|
---|
176 | fclose(in);
|
---|
177 | if (!RAND_status()) {
|
---|
178 | ERR_raise_data(ERR_LIB_RAND, RAND_R_RESEED_ERROR, "Filename=%s", file);
|
---|
179 | return -1;
|
---|
180 | }
|
---|
181 |
|
---|
182 | return ret;
|
---|
183 | }
|
---|
184 |
|
---|
185 | int RAND_write_file(const char *file)
|
---|
186 | {
|
---|
187 | unsigned char buf[RAND_BUF_SIZE];
|
---|
188 | int ret = -1;
|
---|
189 | FILE *out = NULL;
|
---|
190 | #ifndef OPENSSL_NO_POSIX_IO
|
---|
191 | struct stat sb;
|
---|
192 |
|
---|
193 | if (stat(file, &sb) >= 0 && !S_ISREG(sb.st_mode)) {
|
---|
194 | ERR_raise_data(ERR_LIB_RAND, RAND_R_NOT_A_REGULAR_FILE,
|
---|
195 | "Filename=%s", file);
|
---|
196 | return -1;
|
---|
197 | }
|
---|
198 | #endif
|
---|
199 |
|
---|
200 | /* Collect enough random data. */
|
---|
201 | if (RAND_priv_bytes(buf, (int)sizeof(buf)) != 1)
|
---|
202 | return -1;
|
---|
203 |
|
---|
204 | #if defined(O_CREAT) && !defined(OPENSSL_NO_POSIX_IO) && \
|
---|
205 | !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WINDOWS)
|
---|
206 | {
|
---|
207 | # ifndef O_BINARY
|
---|
208 | # define O_BINARY 0
|
---|
209 | # endif
|
---|
210 | /*
|
---|
211 | * chmod(..., 0600) is too late to protect the file, permissions
|
---|
212 | * should be restrictive from the start
|
---|
213 | */
|
---|
214 | int fd = open(file, O_WRONLY | O_CREAT | O_BINARY, 0600);
|
---|
215 | if (fd != -1)
|
---|
216 | out = fdopen(fd, "wb");
|
---|
217 | }
|
---|
218 | #endif
|
---|
219 |
|
---|
220 | #ifdef OPENSSL_SYS_VMS
|
---|
221 | /*
|
---|
222 | * VMS NOTE: Prior versions of this routine created a _new_ version of
|
---|
223 | * the rand file for each call into this routine, then deleted all
|
---|
224 | * existing versions named ;-1, and finally renamed the current version
|
---|
225 | * as ';1'. Under concurrent usage, this resulted in an RMS race
|
---|
226 | * condition in rename() which could orphan files (see vms message help
|
---|
227 | * for RMS$_REENT). With the fopen() calls below, openssl/VMS now shares
|
---|
228 | * the top-level version of the rand file. Note that there may still be
|
---|
229 | * conditions where the top-level rand file is locked. If so, this code
|
---|
230 | * will then create a new version of the rand file. Without the delete
|
---|
231 | * and rename code, this can result in ascending file versions that stop
|
---|
232 | * at version 32767, and this routine will then return an error. The
|
---|
233 | * remedy for this is to recode the calling application to avoid
|
---|
234 | * concurrent use of the rand file, or synchronize usage at the
|
---|
235 | * application level. Also consider whether or not you NEED a persistent
|
---|
236 | * rand file in a concurrent use situation.
|
---|
237 | */
|
---|
238 | out = openssl_fopen(file, "rb+");
|
---|
239 | #endif
|
---|
240 |
|
---|
241 | if (out == NULL)
|
---|
242 | out = openssl_fopen(file, "wb");
|
---|
243 | if (out == NULL) {
|
---|
244 | ERR_raise_data(ERR_LIB_RAND, RAND_R_CANNOT_OPEN_FILE,
|
---|
245 | "Filename=%s", file);
|
---|
246 | return -1;
|
---|
247 | }
|
---|
248 |
|
---|
249 | #if !defined(NO_CHMOD) && !defined(OPENSSL_NO_POSIX_IO)
|
---|
250 | /*
|
---|
251 | * Yes it's late to do this (see above comment), but better than nothing.
|
---|
252 | */
|
---|
253 | chmod(file, 0600);
|
---|
254 | #endif
|
---|
255 |
|
---|
256 | ret = fwrite(buf, 1, RAND_BUF_SIZE, out);
|
---|
257 | fclose(out);
|
---|
258 | OPENSSL_cleanse(buf, RAND_BUF_SIZE);
|
---|
259 | return ret;
|
---|
260 | }
|
---|
261 |
|
---|
262 | const char *RAND_file_name(char *buf, size_t size)
|
---|
263 | {
|
---|
264 | char *s = NULL;
|
---|
265 | size_t len;
|
---|
266 | int use_randfile = 1;
|
---|
267 |
|
---|
268 | #if defined(_WIN32) && defined(CP_UTF8) && !defined(_WIN32_WCE)
|
---|
269 | DWORD envlen;
|
---|
270 | WCHAR *var;
|
---|
271 |
|
---|
272 | /* Look up various environment variables. */
|
---|
273 | if ((envlen = GetEnvironmentVariableW(var = L"RANDFILE", NULL, 0)) == 0) {
|
---|
274 | use_randfile = 0;
|
---|
275 | if ((envlen = GetEnvironmentVariableW(var = L"HOME", NULL, 0)) == 0
|
---|
276 | && (envlen = GetEnvironmentVariableW(var = L"USERPROFILE",
|
---|
277 | NULL, 0)) == 0)
|
---|
278 | envlen = GetEnvironmentVariableW(var = L"SYSTEMROOT", NULL, 0);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /* If we got a value, allocate space to hold it and then get it. */
|
---|
282 | if (envlen != 0) {
|
---|
283 | int sz;
|
---|
284 | WCHAR *val = _alloca(envlen * sizeof(WCHAR));
|
---|
285 |
|
---|
286 | if (GetEnvironmentVariableW(var, val, envlen) < envlen
|
---|
287 | && (sz = WideCharToMultiByte(CP_UTF8, 0, val, -1, NULL, 0,
|
---|
288 | NULL, NULL)) != 0) {
|
---|
289 | s = _alloca(sz);
|
---|
290 | if (WideCharToMultiByte(CP_UTF8, 0, val, -1, s, sz,
|
---|
291 | NULL, NULL) == 0)
|
---|
292 | s = NULL;
|
---|
293 | }
|
---|
294 | }
|
---|
295 | #else
|
---|
296 | if ((s = ossl_safe_getenv("RANDFILE")) == NULL || *s == '\0') {
|
---|
297 | use_randfile = 0;
|
---|
298 | s = ossl_safe_getenv("HOME");
|
---|
299 | }
|
---|
300 | #endif
|
---|
301 |
|
---|
302 | #ifdef DEFAULT_HOME
|
---|
303 | if (!use_randfile && s == NULL)
|
---|
304 | s = DEFAULT_HOME;
|
---|
305 | #endif
|
---|
306 | if (s == NULL || *s == '\0')
|
---|
307 | return NULL;
|
---|
308 |
|
---|
309 | len = strlen(s);
|
---|
310 | if (use_randfile) {
|
---|
311 | if (len + 1 >= size)
|
---|
312 | return NULL;
|
---|
313 | strcpy(buf, s);
|
---|
314 | } else {
|
---|
315 | if (len + 1 + strlen(RFILE) + 1 >= size)
|
---|
316 | return NULL;
|
---|
317 | strcpy(buf, s);
|
---|
318 | #ifndef OPENSSL_SYS_VMS
|
---|
319 | strcat(buf, "/");
|
---|
320 | #endif
|
---|
321 | strcat(buf, RFILE);
|
---|
322 | }
|
---|
323 |
|
---|
324 | return buf;
|
---|
325 | }
|
---|