rand_egd.c 3.5 KB

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