rand_egd.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. # if defined(OPENSSL_SYS_TANDEM)
  49. /*
  50. * HPNS:
  51. *
  52. * Our current MQ 5.3 EGD requies compatability-mode sockets
  53. * This code forces the mode to compatibility if required
  54. * and then restores the mode.
  55. *
  56. * Needs review:
  57. *
  58. * The better long-term solution is to either run two EGD's each in one of
  59. * the two modes or revise the EGD code to listen on two different sockets
  60. * (each in one of the two modes).
  61. */
  62. _variable
  63. int hpns_socket(int family,
  64. int type,
  65. int protocol,
  66. char* transport)
  67. {
  68. int socket_rc;
  69. char current_transport[20];
  70. # define AF_UNIX_PORTABILITY "$ZAFN2"
  71. # define AF_UNIX_COMPATIBILITY "$ZPLS"
  72. if (!_arg_present(transport) || transport != NULL || transport[0] == '\0')
  73. return socket(family, type, protocol);
  74. socket_transport_name_get(AF_UNIX, current_transport, 20);
  75. if (strcmp(current_transport,transport) == 0)
  76. return socket(family, type, protocol);
  77. /* set the requested socket transport */
  78. if (socket_transport_name_set(AF_UNIX, transport))
  79. return -1;
  80. socket_rc = socket(family,type,protocol);
  81. /* set mode back to what it was */
  82. if (socket_transport_name_set(AF_UNIX, current_transport))
  83. return -1;
  84. return socket_rc;
  85. }
  86. /*#define socket(a,b,c,...) hpns_socket(a,b,c,__VA_ARGS__) */
  87. static int hpns_connect_attempt = 0;
  88. # endif /* defined(OPENSSL_SYS_HPNS) */
  89. int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
  90. {
  91. FILE *fp = NULL;
  92. struct sockaddr_un addr;
  93. int mybuffer, ret = -1, i, numbytes, fd;
  94. unsigned char tempbuf[255];
  95. if (bytes > (int)sizeof(tempbuf))
  96. return -1;
  97. /* Make socket. */
  98. memset(&addr, 0, sizeof(addr));
  99. addr.sun_family = AF_UNIX;
  100. if (strlen(path) >= sizeof(addr.sun_path))
  101. return -1;
  102. strcpy(addr.sun_path, path);
  103. i = offsetof(struct sockaddr_un, sun_path) + strlen(path);
  104. #if defined(OPENSSL_SYS_TANDEM)
  105. fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_COMPATIBILITY);
  106. #else
  107. fd = socket(AF_UNIX, SOCK_STREAM, 0);
  108. #endif
  109. if (fd == -1 || (fp = fdopen(fd, "r+")) == NULL)
  110. return -1;
  111. setbuf(fp, NULL);
  112. /* Try to connect */
  113. for ( ; ; ) {
  114. if (connect(fd, (struct sockaddr *)&addr, i) == 0)
  115. break;
  116. # ifdef EISCONN
  117. if (errno == EISCONN)
  118. break;
  119. # endif
  120. switch (errno) {
  121. # ifdef EINTR
  122. case EINTR:
  123. # endif
  124. # ifdef EAGAIN
  125. case EAGAIN:
  126. # endif
  127. # ifdef EINPROGRESS
  128. case EINPROGRESS:
  129. # endif
  130. # ifdef EALREADY
  131. case EALREADY:
  132. # endif
  133. /* No error, try again */
  134. break;
  135. default:
  136. # if defined(OPENSSL_SYS_TANDEM)
  137. if (hpns_connect_attempt == 0) {
  138. /* try the other kind of AF_UNIX socket */
  139. close(fd);
  140. fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_PORTABILITY);
  141. if (fd == -1)
  142. return -1;
  143. ++hpns_connect_attempt;
  144. break; /* try the connect again */
  145. }
  146. # endif
  147. ret = -1;
  148. goto err;
  149. }
  150. }
  151. /* Make request, see how many bytes we can get back. */
  152. tempbuf[0] = 1;
  153. tempbuf[1] = bytes;
  154. if (fwrite(tempbuf, sizeof(char), 2, fp) != 2 || fflush(fp) == EOF)
  155. goto err;
  156. if (fread(tempbuf, sizeof(char), 1, fp) != 1 || tempbuf[0] == 0)
  157. goto err;
  158. numbytes = tempbuf[0];
  159. /* Which buffer are we using? */
  160. mybuffer = buf == NULL;
  161. if (mybuffer)
  162. buf = tempbuf;
  163. /* Read bytes. */
  164. i = fread(buf, sizeof(char), numbytes, fp);
  165. if (i < numbytes)
  166. goto err;
  167. ret = numbytes;
  168. if (mybuffer)
  169. RAND_add(tempbuf, i, i);
  170. err:
  171. if (fp != NULL)
  172. fclose(fp);
  173. return ret;
  174. }
  175. int RAND_egd_bytes(const char *path, int bytes)
  176. {
  177. int num;
  178. num = RAND_query_egd_bytes(path, NULL, bytes);
  179. if (num < 0)
  180. return -1;
  181. if (RAND_status() != 1)
  182. return -1;
  183. return num;
  184. }
  185. int RAND_egd(const char *path)
  186. {
  187. return RAND_egd_bytes(path, 255);
  188. }
  189. #endif