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