rand_egd.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2000-2017 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 an EGD
  18. */
  19. # 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)
  20. int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
  21. {
  22. return -1;
  23. }
  24. int RAND_egd(const char *path)
  25. {
  26. return -1;
  27. }
  28. int RAND_egd_bytes(const char *path, int bytes)
  29. {
  30. return -1;
  31. }
  32. # else
  33. # include <openssl/opensslconf.h>
  34. # include OPENSSL_UNISTD
  35. # include <stddef.h>
  36. # include <sys/types.h>
  37. # include <sys/socket.h>
  38. # ifndef NO_SYS_UN_H
  39. # ifdef OPENSSL_SYS_VXWORKS
  40. # include <streams/un.h>
  41. # else
  42. # include <sys/un.h>
  43. # endif
  44. # else
  45. struct sockaddr_un {
  46. short sun_family; /* AF_UNIX */
  47. char sun_path[108]; /* path name (gag) */
  48. };
  49. # endif /* NO_SYS_UN_H */
  50. # include <string.h>
  51. # include <errno.h>
  52. int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
  53. {
  54. FILE *fp = NULL;
  55. struct sockaddr_un addr;
  56. int mybuffer, ret = -1, i, numbytes, fd;
  57. unsigned char tempbuf[255];
  58. if (bytes > (int)sizeof(tempbuf))
  59. return -1;
  60. /* Make socket. */
  61. memset(&addr, 0, sizeof(addr));
  62. addr.sun_family = AF_UNIX;
  63. if (strlen(path) >= sizeof(addr.sun_path))
  64. return -1;
  65. strcpy(addr.sun_path, path);
  66. i = offsetof(struct sockaddr_un, sun_path) + strlen(path);
  67. fd = socket(AF_UNIX, SOCK_STREAM, 0);
  68. if (fd == -1 || (fp = fdopen(fd, "r+")) == NULL)
  69. return -1;
  70. setbuf(fp, NULL);
  71. /* Try to connect */
  72. for ( ; ; ) {
  73. if (connect(fd, (struct sockaddr *)&addr, i) == 0)
  74. break;
  75. # ifdef EISCONN
  76. if (errno == EISCONN)
  77. break;
  78. # endif
  79. switch (errno) {
  80. # ifdef EINTR
  81. case EINTR:
  82. # endif
  83. # ifdef EAGAIN
  84. case EAGAIN:
  85. # endif
  86. # ifdef EINPROGRESS
  87. case EINPROGRESS:
  88. # endif
  89. # ifdef EALREADY
  90. case EALREADY:
  91. # endif
  92. /* No error, try again */
  93. break;
  94. default:
  95. ret = -1;
  96. goto err;
  97. }
  98. }
  99. /* Make request, see how many bytes we can get back. */
  100. tempbuf[0] = 1;
  101. tempbuf[1] = bytes;
  102. if (fwrite(tempbuf, sizeof(char), 2, fp) != 2 || fflush(fp) == EOF)
  103. goto err;
  104. if (fread(tempbuf, sizeof(char), 1, fp) != 1 || tempbuf[0] == 0)
  105. goto err;
  106. numbytes = tempbuf[0];
  107. /* Which buffer are we using? */
  108. mybuffer = buf == NULL;
  109. if (mybuffer)
  110. buf = tempbuf;
  111. /* Read bytes. */
  112. i = fread(buf, sizeof(char), numbytes, fp);
  113. if (i < numbytes)
  114. goto err;
  115. ret = numbytes;
  116. if (mybuffer)
  117. RAND_add(tempbuf, i, i);
  118. err:
  119. if (fp != NULL)
  120. fclose(fp);
  121. return ret;
  122. }
  123. int RAND_egd_bytes(const char *path, int bytes)
  124. {
  125. int num;
  126. num = RAND_query_egd_bytes(path, NULL, bytes);
  127. if (num < 0)
  128. return -1;
  129. if (RAND_status() != 1)
  130. return -1;
  131. return num;
  132. }
  133. int RAND_egd(const char *path)
  134. {
  135. return RAND_egd_bytes(path, 255);
  136. }
  137. # endif
  138. #endif