rand_egd.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2000-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 <openssl/opensslconf.h>
  10. #ifdef OPENSSL_NO_EGD
  11. NON_EMPTY_TRANSLATION_UNIT
  12. #else
  13. # include <openssl/crypto.h>
  14. # include <openssl/e_os2.h>
  15. # include <openssl/rand.h>
  16. /*-
  17. * Query the EGD <URL: http://www.lothar.com/tech/crypto/>.
  18. *
  19. * This module supplies three routines:
  20. *
  21. * RAND_query_egd_bytes(path, buf, bytes)
  22. * will actually query "bytes" bytes of entropy form the egd-socket located
  23. * at path and will write them to buf (if supplied) or will directly feed
  24. * it to RAND_seed() if buf==NULL.
  25. * The number of bytes is not limited by the maximum chunk size of EGD,
  26. * which is 255 bytes. If more than 255 bytes are wanted, several chunks
  27. * of entropy bytes are requested. The connection is left open until the
  28. * query is competed.
  29. * RAND_query_egd_bytes() returns with
  30. * -1 if an error occurred during connection or communication.
  31. * num the number of bytes read from the EGD socket. This number is either
  32. * the number of bytes requested or smaller, if the EGD pool is
  33. * drained and the daemon signals that the pool is empty.
  34. * This routine does not touch any RAND_status(). This is necessary, since
  35. * PRNG functions may call it during initialization.
  36. *
  37. * RAND_egd_bytes(path, bytes) will query "bytes" bytes and have them
  38. * used to seed the PRNG.
  39. * RAND_egd_bytes() is a wrapper for RAND_query_egd_bytes() with buf=NULL.
  40. * Unlike RAND_query_egd_bytes(), RAND_status() is used to test the
  41. * seed status so that the return value can reflect the seed state:
  42. * -1 if an error occurred during connection or communication _or_
  43. * if the PRNG has still not received the required seeding.
  44. * num the number of bytes read from the EGD socket. This number is either
  45. * the number of bytes requested or smaller, if the EGD pool is
  46. * drained and the daemon signals that the pool is empty.
  47. *
  48. * RAND_egd(path) will query 255 bytes and use the bytes retrieved to seed
  49. * the PRNG.
  50. * RAND_egd() is a wrapper for RAND_egd_bytes() with numbytes=255.
  51. */
  52. # if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_VOS) || defined(OPENSSL_SYS_UEFI)
  53. int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
  54. {
  55. return (-1);
  56. }
  57. int RAND_egd(const char *path)
  58. {
  59. return (-1);
  60. }
  61. int RAND_egd_bytes(const char *path, int bytes)
  62. {
  63. return (-1);
  64. }
  65. # else
  66. # include <openssl/opensslconf.h>
  67. # include OPENSSL_UNISTD
  68. # include <stddef.h>
  69. # include <sys/types.h>
  70. # include <sys/socket.h>
  71. # ifndef NO_SYS_UN_H
  72. # ifdef OPENSSL_SYS_VXWORKS
  73. # include <streams/un.h>
  74. # else
  75. # include <sys/un.h>
  76. # endif
  77. # else
  78. struct sockaddr_un {
  79. short sun_family; /* AF_UNIX */
  80. char sun_path[108]; /* path name (gag) */
  81. };
  82. # endif /* NO_SYS_UN_H */
  83. # include <string.h>
  84. # include <errno.h>
  85. int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
  86. {
  87. int ret = 0;
  88. struct sockaddr_un addr;
  89. int len, num, numbytes;
  90. int fd = -1;
  91. int success;
  92. unsigned char egdbuf[2], tempbuf[255], *retrievebuf;
  93. memset(&addr, 0, sizeof(addr));
  94. addr.sun_family = AF_UNIX;
  95. if (strlen(path) >= sizeof(addr.sun_path))
  96. return (-1);
  97. OPENSSL_strlcpy(addr.sun_path, path, sizeof addr.sun_path);
  98. len = offsetof(struct sockaddr_un, sun_path) + strlen(path);
  99. fd = socket(AF_UNIX, SOCK_STREAM, 0);
  100. if (fd == -1)
  101. return (-1);
  102. success = 0;
  103. while (!success) {
  104. if (connect(fd, (struct sockaddr *)&addr, len) == 0)
  105. success = 1;
  106. else {
  107. switch (errno) {
  108. # ifdef EINTR
  109. case EINTR:
  110. # endif
  111. # ifdef EAGAIN
  112. case EAGAIN:
  113. # endif
  114. # ifdef EINPROGRESS
  115. case EINPROGRESS:
  116. # endif
  117. # ifdef EALREADY
  118. case EALREADY:
  119. # endif
  120. /* No error, try again */
  121. break;
  122. # ifdef EISCONN
  123. case EISCONN:
  124. success = 1;
  125. break;
  126. # endif
  127. default:
  128. ret = -1;
  129. goto err; /* failure */
  130. }
  131. }
  132. }
  133. while (bytes > 0) {
  134. egdbuf[0] = 1;
  135. egdbuf[1] = bytes < 255 ? bytes : 255;
  136. numbytes = 0;
  137. while (numbytes != 2) {
  138. num = write(fd, egdbuf + numbytes, 2 - numbytes);
  139. if (num >= 0)
  140. numbytes += num;
  141. else {
  142. switch (errno) {
  143. # ifdef EINTR
  144. case EINTR:
  145. # endif
  146. # ifdef EAGAIN
  147. case EAGAIN:
  148. # endif
  149. /* No error, try again */
  150. break;
  151. default:
  152. ret = -1;
  153. goto err; /* failure */
  154. }
  155. }
  156. }
  157. numbytes = 0;
  158. while (numbytes != 1) {
  159. num = read(fd, egdbuf, 1);
  160. if (num == 0)
  161. goto err; /* descriptor closed */
  162. else if (num > 0)
  163. numbytes += num;
  164. else {
  165. switch (errno) {
  166. # ifdef EINTR
  167. case EINTR:
  168. # endif
  169. # ifdef EAGAIN
  170. case EAGAIN:
  171. # endif
  172. /* No error, try again */
  173. break;
  174. default:
  175. ret = -1;
  176. goto err; /* failure */
  177. }
  178. }
  179. }
  180. if (egdbuf[0] == 0)
  181. goto err;
  182. if (buf)
  183. retrievebuf = buf + ret;
  184. else
  185. retrievebuf = tempbuf;
  186. numbytes = 0;
  187. while (numbytes != egdbuf[0]) {
  188. num = read(fd, retrievebuf + numbytes, egdbuf[0] - numbytes);
  189. if (num == 0)
  190. goto err; /* descriptor closed */
  191. else if (num > 0)
  192. numbytes += num;
  193. else {
  194. switch (errno) {
  195. # ifdef EINTR
  196. case EINTR:
  197. # endif
  198. # ifdef EAGAIN
  199. case EAGAIN:
  200. # endif
  201. /* No error, try again */
  202. break;
  203. default:
  204. ret = -1;
  205. goto err; /* failure */
  206. }
  207. }
  208. }
  209. ret += egdbuf[0];
  210. bytes -= egdbuf[0];
  211. if (!buf)
  212. RAND_seed(tempbuf, egdbuf[0]);
  213. }
  214. err:
  215. if (fd != -1)
  216. close(fd);
  217. return (ret);
  218. }
  219. int RAND_egd_bytes(const char *path, int bytes)
  220. {
  221. int num, ret = -1;
  222. num = RAND_query_egd_bytes(path, NULL, bytes);
  223. if (num < 0)
  224. goto err;
  225. if (RAND_status() == 1)
  226. ret = num;
  227. err:
  228. return (ret);
  229. }
  230. int RAND_egd(const char *path)
  231. {
  232. return (RAND_egd_bytes(path, 255));
  233. }
  234. # endif
  235. #endif