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