]> git.proxmox.com Git - efi-boot-shim.git/blob - Cryptlib/OpenSSL/crypto/bio/bss_fd.c
New upstream version 15+1533136590.3beb971
[efi-boot-shim.git] / Cryptlib / OpenSSL / crypto / bio / bss_fd.c
1 /*
2 * Copyright 1995-2016 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 <stdio.h>
11 #include <errno.h>
12
13 #include "bio_lcl.h"
14
15 #if defined(OPENSSL_NO_POSIX_IO)
16 /*
17 * Dummy placeholder for BIO_s_fd...
18 */
19 BIO *BIO_new_fd(int fd, int close_flag)
20 {
21 return NULL;
22 }
23
24 int BIO_fd_non_fatal_error(int err)
25 {
26 return 0;
27 }
28
29 int BIO_fd_should_retry(int i)
30 {
31 return 0;
32 }
33
34 const BIO_METHOD *BIO_s_fd(void)
35 {
36 return NULL;
37 }
38 #else
39 /*
40 * As for unconditional usage of "UPLINK" interface in this module.
41 * Trouble is that unlike Unix file descriptors [which are indexes
42 * in kernel-side per-process table], corresponding descriptors on
43 * platforms which require "UPLINK" interface seem to be indexes
44 * in a user-land, non-global table. Well, in fact they are indexes
45 * in stdio _iob[], and recall that _iob[] was the very reason why
46 * "UPLINK" interface was introduced in first place. But one way on
47 * another. Neither libcrypto or libssl use this BIO meaning that
48 * file descriptors can only be provided by application. Therefore
49 * "UPLINK" calls are due...
50 */
51 static int fd_write(BIO *h, const char *buf, int num);
52 static int fd_read(BIO *h, char *buf, int size);
53 static int fd_puts(BIO *h, const char *str);
54 static int fd_gets(BIO *h, char *buf, int size);
55 static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);
56 static int fd_new(BIO *h);
57 static int fd_free(BIO *data);
58 int BIO_fd_should_retry(int s);
59
60 static const BIO_METHOD methods_fdp = {
61 BIO_TYPE_FD, "file descriptor",
62 fd_write,
63 fd_read,
64 fd_puts,
65 fd_gets,
66 fd_ctrl,
67 fd_new,
68 fd_free,
69 NULL,
70 };
71
72 const BIO_METHOD *BIO_s_fd(void)
73 {
74 return (&methods_fdp);
75 }
76
77 BIO *BIO_new_fd(int fd, int close_flag)
78 {
79 BIO *ret;
80 ret = BIO_new(BIO_s_fd());
81 if (ret == NULL)
82 return (NULL);
83 BIO_set_fd(ret, fd, close_flag);
84 return (ret);
85 }
86
87 static int fd_new(BIO *bi)
88 {
89 bi->init = 0;
90 bi->num = -1;
91 bi->ptr = NULL;
92 bi->flags = BIO_FLAGS_UPLINK; /* essentially redundant */
93 return (1);
94 }
95
96 static int fd_free(BIO *a)
97 {
98 if (a == NULL)
99 return (0);
100 if (a->shutdown) {
101 if (a->init) {
102 UP_close(a->num);
103 }
104 a->init = 0;
105 a->flags = BIO_FLAGS_UPLINK;
106 }
107 return (1);
108 }
109
110 static int fd_read(BIO *b, char *out, int outl)
111 {
112 int ret = 0;
113
114 if (out != NULL) {
115 clear_sys_error();
116 ret = UP_read(b->num, out, outl);
117 BIO_clear_retry_flags(b);
118 if (ret <= 0) {
119 if (BIO_fd_should_retry(ret))
120 BIO_set_retry_read(b);
121 }
122 }
123 return (ret);
124 }
125
126 static int fd_write(BIO *b, const char *in, int inl)
127 {
128 int ret;
129 clear_sys_error();
130 ret = UP_write(b->num, in, inl);
131 BIO_clear_retry_flags(b);
132 if (ret <= 0) {
133 if (BIO_fd_should_retry(ret))
134 BIO_set_retry_write(b);
135 }
136 return (ret);
137 }
138
139 static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
140 {
141 long ret = 1;
142 int *ip;
143
144 switch (cmd) {
145 case BIO_CTRL_RESET:
146 num = 0;
147 case BIO_C_FILE_SEEK:
148 ret = (long)UP_lseek(b->num, num, 0);
149 break;
150 case BIO_C_FILE_TELL:
151 case BIO_CTRL_INFO:
152 ret = (long)UP_lseek(b->num, 0, 1);
153 break;
154 case BIO_C_SET_FD:
155 fd_free(b);
156 b->num = *((int *)ptr);
157 b->shutdown = (int)num;
158 b->init = 1;
159 break;
160 case BIO_C_GET_FD:
161 if (b->init) {
162 ip = (int *)ptr;
163 if (ip != NULL)
164 *ip = b->num;
165 ret = b->num;
166 } else
167 ret = -1;
168 break;
169 case BIO_CTRL_GET_CLOSE:
170 ret = b->shutdown;
171 break;
172 case BIO_CTRL_SET_CLOSE:
173 b->shutdown = (int)num;
174 break;
175 case BIO_CTRL_PENDING:
176 case BIO_CTRL_WPENDING:
177 ret = 0;
178 break;
179 case BIO_CTRL_DUP:
180 case BIO_CTRL_FLUSH:
181 ret = 1;
182 break;
183 default:
184 ret = 0;
185 break;
186 }
187 return (ret);
188 }
189
190 static int fd_puts(BIO *bp, const char *str)
191 {
192 int n, ret;
193
194 n = strlen(str);
195 ret = fd_write(bp, str, n);
196 return (ret);
197 }
198
199 static int fd_gets(BIO *bp, char *buf, int size)
200 {
201 int ret = 0;
202 char *ptr = buf;
203 char *end = buf + size - 1;
204
205 while ((ptr < end) && (fd_read(bp, ptr, 1) > 0) && (ptr[0] != '\n'))
206 ptr++;
207
208 ptr[0] = '\0';
209
210 if (buf[0] != '\0')
211 ret = strlen(buf);
212 return (ret);
213 }
214
215 int BIO_fd_should_retry(int i)
216 {
217 int err;
218
219 if ((i == 0) || (i == -1)) {
220 err = get_last_sys_error();
221
222 return (BIO_fd_non_fatal_error(err));
223 }
224 return (0);
225 }
226
227 int BIO_fd_non_fatal_error(int err)
228 {
229 switch (err) {
230
231 # ifdef EWOULDBLOCK
232 # ifdef WSAEWOULDBLOCK
233 # if WSAEWOULDBLOCK != EWOULDBLOCK
234 case EWOULDBLOCK:
235 # endif
236 # else
237 case EWOULDBLOCK:
238 # endif
239 # endif
240
241 # if defined(ENOTCONN)
242 case ENOTCONN:
243 # endif
244
245 # ifdef EINTR
246 case EINTR:
247 # endif
248
249 # ifdef EAGAIN
250 # if EWOULDBLOCK != EAGAIN
251 case EAGAIN:
252 # endif
253 # endif
254
255 # ifdef EPROTO
256 case EPROTO:
257 # endif
258
259 # ifdef EINPROGRESS
260 case EINPROGRESS:
261 # endif
262
263 # ifdef EALREADY
264 case EALREADY:
265 # endif
266 return (1);
267 /* break; */
268 default:
269 break;
270 }
271 return (0);
272 }
273 #endif