A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
8.3 KiB

  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include "e_os.h"
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <openssl/crypto.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/buffer.h>
  17. #ifdef OPENSSL_SYS_VMS
  18. # include <unixio.h>
  19. #endif
  20. #ifndef NO_SYS_TYPES_H
  21. # include <sys/types.h>
  22. #endif
  23. #ifndef OPENSSL_NO_POSIX_IO
  24. # include <sys/stat.h>
  25. # include <fcntl.h>
  26. /*
  27. * Following should not be needed, and we could have been stricter
  28. * and demand S_IS*. But some systems just don't comply... Formally
  29. * below macros are "anatomically incorrect", because normally they
  30. * would look like ((m) & MASK == TYPE), but since MASK availability
  31. * is as questionable, we settle for this poor-man fallback...
  32. */
  33. # if !defined(S_ISBLK)
  34. # if defined(_S_IFBLK)
  35. # define S_ISBLK(m) ((m) & _S_IFBLK)
  36. # elif defined(S_IFBLK)
  37. # define S_ISBLK(m) ((m) & S_IFBLK)
  38. # elif defined(_WIN32)
  39. # define S_ISBLK(m) 0 /* no concept of block devices on Windows */
  40. # endif
  41. # endif
  42. # if !defined(S_ISCHR)
  43. # if defined(_S_IFCHR)
  44. # define S_ISCHR(m) ((m) & _S_IFCHR)
  45. # elif defined(S_IFCHR)
  46. # define S_ISCHR(m) ((m) & S_IFCHR)
  47. # endif
  48. # endif
  49. #endif
  50. #ifdef _WIN32
  51. # define stat _stat
  52. # define chmod _chmod
  53. # define open _open
  54. # define fdopen _fdopen
  55. #endif
  56. #undef BUFSIZE
  57. #define BUFSIZE 1024
  58. #define RAND_DATA 1024
  59. #ifdef OPENSSL_SYS_VMS
  60. /*
  61. * This declaration is a nasty hack to get around vms' extension to fopen for
  62. * passing in sharing options being disabled by our /STANDARD=ANSI89
  63. */
  64. static FILE *(*const vms_fopen)(const char *, const char *, ...) =
  65. (FILE *(*)(const char *, const char *, ...))fopen;
  66. # define VMS_OPEN_ATTRS "shr=get,put,upd,del","ctx=bin,stm","rfm=stm","rat=none","mrs=0"
  67. #endif
  68. #define RFILE ".rnd"
  69. /*
  70. * Note that these functions are intended for seed files only. Entropy
  71. * devices and EGD sockets are handled in rand_unix.c
  72. */
  73. int RAND_load_file(const char *file, long bytes)
  74. {
  75. /*-
  76. * If bytes >= 0, read up to 'bytes' bytes.
  77. * if bytes == -1, read complete file.
  78. */
  79. unsigned char buf[BUFSIZE];
  80. #ifndef OPENSSL_NO_POSIX_IO
  81. struct stat sb;
  82. #endif
  83. int i, ret = 0, n;
  84. FILE *in;
  85. if (file == NULL)
  86. return (0);
  87. #ifndef OPENSSL_NO_POSIX_IO
  88. /*
  89. * struct stat can have padding and unused fields that may not be
  90. * initialized in the call to stat(). We need to clear the entire
  91. * structure before calling RAND_add() to avoid complaints from
  92. * applications such as Valgrind.
  93. */
  94. memset(&sb, 0, sizeof(sb));
  95. if (stat(file, &sb) < 0)
  96. return (0);
  97. RAND_add(&sb, sizeof(sb), 0.0);
  98. #endif
  99. if (bytes == 0)
  100. return (ret);
  101. #ifdef OPENSSL_SYS_VMS
  102. in = vms_fopen(file, "rb", VMS_OPEN_ATTRS);
  103. #else
  104. in = fopen(file, "rb");
  105. #endif
  106. if (in == NULL)
  107. goto err;
  108. #if defined(S_ISBLK) && defined(S_ISCHR) && !defined(OPENSSL_NO_POSIX_IO)
  109. if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
  110. /*
  111. * this file is a device. we don't want read an infinite number of
  112. * bytes from a random device, nor do we want to use buffered I/O
  113. * because we will waste system entropy.
  114. */
  115. bytes = (bytes == -1) ? 2048 : bytes; /* ok, is 2048 enough? */
  116. setbuf(in, NULL); /* don't do buffered reads */
  117. }
  118. #endif
  119. for (;;) {
  120. if (bytes > 0)
  121. n = (bytes < BUFSIZE) ? (int)bytes : BUFSIZE;
  122. else
  123. n = BUFSIZE;
  124. i = fread(buf, 1, n, in);
  125. if (i <= 0)
  126. break;
  127. RAND_add(buf, i, (double)i);
  128. ret += i;
  129. if (bytes > 0) {
  130. bytes -= n;
  131. if (bytes <= 0)
  132. break;
  133. }
  134. }
  135. fclose(in);
  136. OPENSSL_cleanse(buf, BUFSIZE);
  137. err:
  138. return (ret);
  139. }
  140. int RAND_write_file(const char *file)
  141. {
  142. unsigned char buf[BUFSIZE];
  143. int i, ret = 0, rand_err = 0;
  144. FILE *out = NULL;
  145. int n;
  146. #ifndef OPENSSL_NO_POSIX_IO
  147. struct stat sb;
  148. i = stat(file, &sb);
  149. if (i != -1) {
  150. # if defined(S_ISBLK) && defined(S_ISCHR)
  151. if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
  152. /*
  153. * this file is a device. we don't write back to it. we
  154. * "succeed" on the assumption this is some sort of random
  155. * device. Otherwise attempting to write to and chmod the device
  156. * causes problems.
  157. */
  158. return (1);
  159. }
  160. # endif
  161. }
  162. #endif
  163. #if defined(O_CREAT) && !defined(OPENSSL_NO_POSIX_IO) && !defined(OPENSSL_SYS_VMS)
  164. {
  165. # ifndef O_BINARY
  166. # define O_BINARY 0
  167. # endif
  168. /*
  169. * chmod(..., 0600) is too late to protect the file, permissions
  170. * should be restrictive from the start
  171. */
  172. int fd = open(file, O_WRONLY | O_CREAT | O_BINARY, 0600);
  173. if (fd != -1)
  174. out = fdopen(fd, "wb");
  175. }
  176. #endif
  177. #ifdef OPENSSL_SYS_VMS
  178. /*
  179. * VMS NOTE: Prior versions of this routine created a _new_ version of
  180. * the rand file for each call into this routine, then deleted all
  181. * existing versions named ;-1, and finally renamed the current version
  182. * as ';1'. Under concurrent usage, this resulted in an RMS race
  183. * condition in rename() which could orphan files (see vms message help
  184. * for RMS$_REENT). With the fopen() calls below, openssl/VMS now shares
  185. * the top-level version of the rand file. Note that there may still be
  186. * conditions where the top-level rand file is locked. If so, this code
  187. * will then create a new version of the rand file. Without the delete
  188. * and rename code, this can result in ascending file versions that stop
  189. * at version 32767, and this routine will then return an error. The
  190. * remedy for this is to recode the calling application to avoid
  191. * concurrent use of the rand file, or synchronize usage at the
  192. * application level. Also consider whether or not you NEED a persistent
  193. * rand file in a concurrent use situation.
  194. */
  195. out = vms_fopen(file, "rb+", VMS_OPEN_ATTRS);
  196. if (out == NULL)
  197. out = vms_fopen(file, "wb", VMS_OPEN_ATTRS);
  198. #else
  199. if (out == NULL)
  200. out = fopen(file, "wb");
  201. #endif
  202. if (out == NULL)
  203. goto err;
  204. #if !defined(NO_CHMOD) && !defined(OPENSSL_NO_POSIX_IO)
  205. chmod(file, 0600);
  206. #endif
  207. n = RAND_DATA;
  208. for (;;) {
  209. i = (n > BUFSIZE) ? BUFSIZE : n;
  210. n -= BUFSIZE;
  211. if (RAND_bytes(buf, i) <= 0)
  212. rand_err = 1;
  213. i = fwrite(buf, 1, i, out);
  214. if (i <= 0) {
  215. ret = 0;
  216. break;
  217. }
  218. ret += i;
  219. if (n <= 0)
  220. break;
  221. }
  222. fclose(out);
  223. OPENSSL_cleanse(buf, BUFSIZE);
  224. err:
  225. return (rand_err ? -1 : ret);
  226. }
  227. const char *RAND_file_name(char *buf, size_t size)
  228. {
  229. char *s = NULL;
  230. #ifdef __OpenBSD__
  231. struct stat sb;
  232. #endif
  233. if (OPENSSL_issetugid() == 0)
  234. s = getenv("RANDFILE");
  235. if (s != NULL && *s && strlen(s) + 1 < size) {
  236. if (OPENSSL_strlcpy(buf, s, size) >= size)
  237. return NULL;
  238. } else {
  239. if (OPENSSL_issetugid() == 0)
  240. s = getenv("HOME");
  241. #ifdef DEFAULT_HOME
  242. if (s == NULL) {
  243. s = DEFAULT_HOME;
  244. }
  245. #endif
  246. if (s && *s && strlen(s) + strlen(RFILE) + 2 < size) {
  247. OPENSSL_strlcpy(buf, s, size);
  248. #ifndef OPENSSL_SYS_VMS
  249. OPENSSL_strlcat(buf, "/", size);
  250. #endif
  251. OPENSSL_strlcat(buf, RFILE, size);
  252. } else
  253. buf[0] = '\0'; /* no file name */
  254. }
  255. #ifdef __OpenBSD__
  256. /*
  257. * given that all random loads just fail if the file can't be seen on a
  258. * stat, we stat the file we're returning, if it fails, use /dev/arandom
  259. * instead. this allows the user to use their own source for good random
  260. * data, but defaults to something hopefully decent if that isn't
  261. * available.
  262. */
  263. if (!buf[0])
  264. if (OPENSSL_strlcpy(buf, "/dev/arandom", size) >= size) {
  265. return (NULL);
  266. }
  267. if (stat(buf, &sb) == -1)
  268. if (OPENSSL_strlcpy(buf, "/dev/arandom", size) >= size) {
  269. return (NULL);
  270. }
  271. #endif
  272. return (buf);
  273. }