rand_egd.c 3.4 KB

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