aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrent Cook <busterb@gmail.com>2014-11-20 00:26:55 -0600
committerBrent Cook <bcook@openbsd.org>2014-12-03 17:02:29 -0600
commit58fcd3c39c73d83eb9782a8ef935498b18b5ae73 (patch)
tree523e55877fe8f38cc99ce8fdb633d9db9fe513e9
parent7f0646f612f61792bc3f922233c85bd205130216 (diff)
downloadportable-58fcd3c39c73d83eb9782a8ef935498b18b5ae73.tar.gz
portable-58fcd3c39c73d83eb9782a8ef935498b18b5ae73.tar.bz2
portable-58fcd3c39c73d83eb9782a8ef935498b18b5ae73.zip
Add conditional compilation for windows and posix functions.
This adds a Windows-specific versions of several symbols from libcrypto and openssl(1).
-rw-r--r--apps/apps_win.c29
-rw-r--r--crypto/compat/b_win.c54
-rw-r--r--crypto/compat/ui_openssl_win.c367
-rwxr-xr-xupdate.sh49
4 files changed, 492 insertions, 7 deletions
diff --git a/apps/apps_win.c b/apps/apps_win.c
new file mode 100644
index 0000000..496ac03
--- /dev/null
+++ b/apps/apps_win.c
@@ -0,0 +1,29 @@
1/*
2 * Public domain
3 *
4 * Dongsheng Song <dongsheng.song@gmail.com>
5 * Brent Cook <bcook@openbsd.org>
6 */
7
8#include <windows.h>
9
10#include "apps.h"
11
12double
13app_tminterval(int stop, int usertime)
14{
15 static unsigned __int64 tmstart;
16 union {
17 unsigned __int64 u64;
18 FILETIME ft;
19 } ct, et, kt, ut;
20
21 GetProcessTimes(GetCurrentProcess(), &ct.ft, &et.ft, &kt.ft, &ut.ft);
22
23 if (stop == TM_START) {
24 tmstart = ut.u64 + kt.u64;
25 } else {
26 return (ut.u64 + kt.u64 - tmstart) / (double) 10000000;
27 }
28 return 0;
29}
diff --git a/crypto/compat/b_win.c b/crypto/compat/b_win.c
new file mode 100644
index 0000000..b8d01ae
--- /dev/null
+++ b/crypto/compat/b_win.c
@@ -0,0 +1,54 @@
1/*
2 * Public domain
3 *
4 * Dongsheng Song <dongsheng.song@gmail.com>
5 * Brent Cook <bcook@openbsd.org>
6 */
7
8#include <ws2tcpip.h>
9
10#include <openssl/bio.h>
11#include <openssl/err.h>
12
13int
14BIO_sock_init(void)
15{
16 /*
17 * WSAStartup loads the winsock .dll and initializes the networking
18 * stack on Windows, or simply increases the reference count.
19 */
20 static struct WSAData wsa_state = {0};
21 WORD version_requested = MAKEWORD(2, 2);
22 static int wsa_init_done = 0;
23 if (!wsa_init_done) {
24 if (WSAStartup(version_requested, &wsa_state) != 0) {
25 int err = WSAGetLastError();
26 SYSerr(SYS_F_WSASTARTUP, err);
27 BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP);
28 return (-1);
29 }
30 wsa_init_done = 1;
31 }
32 return (1);
33}
34
35void
36BIO_sock_cleanup(void)
37{
38 /*
39 * We could call WSACleanup here, but it is easy to get it wrong. Since
40 * this API provides no way to even tell if it failed, there is no safe
41 * way to expose that functionality here.
42 *
43 * The cost of leaving the networking DLLs loaded may have been large
44 * during the Windows 3.1/win32s era, but it is small in modern
45 * contexts, so don't bother.
46 */
47}
48
49int
50BIO_socket_nbio(int s, int mode)
51{
52 u_long value = mode;
53 return ioctlsocket(s, FIONBIO, &value) != SOCKET_ERROR;
54}
diff --git a/crypto/compat/ui_openssl_win.c b/crypto/compat/ui_openssl_win.c
new file mode 100644
index 0000000..ecf7376
--- /dev/null
+++ b/crypto/compat/ui_openssl_win.c
@@ -0,0 +1,367 @@
1/* $OpenBSD: ui_openssl.c,v 1.22 2014/07/11 08:44:49 jsing Exp $ */
2/* Written by Richard Levitte (richard@levitte.org) and others
3 * for the OpenSSL project 2001.
4 */
5/* ====================================================================
6 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59/* The lowest level part of this file was previously in crypto/des/read_pwd.c,
60 * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
61 * All rights reserved.
62 *
63 * This package is an SSL implementation written
64 * by Eric Young (eay@cryptsoft.com).
65 * The implementation was written so as to conform with Netscapes SSL.
66 *
67 * This library is free for commercial and non-commercial use as long as
68 * the following conditions are aheared to. The following conditions
69 * apply to all code found in this distribution, be it the RC4, RSA,
70 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
71 * included with this distribution is covered by the same copyright terms
72 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
73 *
74 * Copyright remains Eric Young's, and as such any Copyright notices in
75 * the code are not to be removed.
76 * If this package is used in a product, Eric Young should be given attribution
77 * as the author of the parts of the library used.
78 * This can be in the form of a textual message at program startup or
79 * in documentation (online or textual) provided with the package.
80 *
81 * Redistribution and use in source and binary forms, with or without
82 * modification, are permitted provided that the following conditions
83 * are met:
84 * 1. Redistributions of source code must retain the copyright
85 * notice, this list of conditions and the following disclaimer.
86 * 2. Redistributions in binary form must reproduce the above copyright
87 * notice, this list of conditions and the following disclaimer in the
88 * documentation and/or other materials provided with the distribution.
89 * 3. All advertising materials mentioning features or use of this software
90 * must display the following acknowledgement:
91 * "This product includes cryptographic software written by
92 * Eric Young (eay@cryptsoft.com)"
93 * The word 'cryptographic' can be left out if the rouines from the library
94 * being used are not cryptographic related :-).
95 * 4. If you include any Windows specific code (or a derivative thereof) from
96 * the apps directory (application code) you must include an acknowledgement:
97 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
98 *
99 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
100 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
102 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
103 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
104 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
105 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
106 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
107 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
108 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
109 * SUCH DAMAGE.
110 *
111 * The licence and distribution terms for any publically available version or
112 * derivative of this code cannot be changed. i.e. this code cannot simply be
113 * copied and put under another distribution licence
114 * [including the GNU Public Licence.]
115 */
116
117#include <sys/ioctl.h>
118
119#include <openssl/opensslconf.h>
120
121#include <errno.h>
122#include <signal.h>
123#include <stdio.h>
124#include <string.h>
125#include <unistd.h>
126
127#include "ui_locl.h"
128
129#ifndef NX509_SIG
130#define NX509_SIG 32
131#endif
132
133/* Define globals. They are protected by a lock */
134static void (*savsig[NX509_SIG])(int );
135
136static FILE *tty_in, *tty_out;
137static int is_a_tty;
138
139/* Declare static functions */
140static int read_till_nl(FILE *);
141static void recsig(int);
142static void pushsig(void);
143static void popsig(void);
144static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
145
146static int read_string(UI *ui, UI_STRING *uis);
147static int write_string(UI *ui, UI_STRING *uis);
148
149static int open_console(UI *ui);
150static int echo_console(UI *ui);
151static int noecho_console(UI *ui);
152static int close_console(UI *ui);
153
154static UI_METHOD ui_openssl = {
155 .name = "OpenSSL default user interface",
156 .ui_open_session = open_console,
157 .ui_write_string = write_string,
158 .ui_read_string = read_string,
159 .ui_close_session = close_console,
160};
161
162/* The method with all the built-in thingies */
163UI_METHOD *
164UI_OpenSSL(void)
165{
166 return &ui_openssl;
167}
168
169/* The following function makes sure that info and error strings are printed
170 before any prompt. */
171static int
172write_string(UI *ui, UI_STRING *uis)
173{
174 switch (UI_get_string_type(uis)) {
175 case UIT_ERROR:
176 case UIT_INFO:
177 fputs(UI_get0_output_string(uis), tty_out);
178 fflush(tty_out);
179 break;
180 default:
181 break;
182 }
183 return 1;
184}
185
186static int
187read_string(UI *ui, UI_STRING *uis)
188{
189 int ok = 0;
190
191 switch (UI_get_string_type(uis)) {
192 case UIT_BOOLEAN:
193 fputs(UI_get0_output_string(uis), tty_out);
194 fputs(UI_get0_action_string(uis), tty_out);
195 fflush(tty_out);
196 return read_string_inner(ui, uis,
197 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0);
198 case UIT_PROMPT:
199 fputs(UI_get0_output_string(uis), tty_out);
200 fflush(tty_out);
201 return read_string_inner(ui, uis,
202 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1);
203 case UIT_VERIFY:
204 fprintf(tty_out, "Verifying - %s",
205 UI_get0_output_string(uis));
206 fflush(tty_out);
207 if ((ok = read_string_inner(ui, uis, UI_get_input_flags(uis) &
208 UI_INPUT_FLAG_ECHO, 1)) <= 0)
209 return ok;
210 if (strcmp(UI_get0_result_string(uis),
211 UI_get0_test_string(uis)) != 0) {
212 fprintf(tty_out, "Verify failure\n");
213 fflush(tty_out);
214 return 0;
215 }
216 break;
217 default:
218 break;
219 }
220 return 1;
221}
222
223
224/* Internal functions to read a string without echoing */
225static int
226read_till_nl(FILE *in)
227{
228#define SIZE 4
229 char buf[SIZE + 1];
230
231 do {
232 if (!fgets(buf, SIZE, in))
233 return 0;
234 } while (strchr(buf, '\n') == NULL);
235 return 1;
236}
237
238static volatile sig_atomic_t intr_signal;
239
240static int
241read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
242{
243 static int ps;
244 int ok;
245 char result[BUFSIZ];
246 int maxsize = BUFSIZ - 1;
247 char *p;
248
249 intr_signal = 0;
250 ok = 0;
251 ps = 0;
252
253 pushsig();
254
255 ps = 1;
256
257 if (!echo && !noecho_console(ui))
258 goto error;
259 ps = 2;
260
261 result[0] = '\0';
262 p = fgets(result, maxsize, tty_in);
263 if (!p)
264 goto error;
265 if (feof(tty_in))
266 goto error;
267 if (ferror(tty_in))
268 goto error;
269 if ((p = strchr(result, '\n')) != NULL) {
270 if (strip_nl)
271 *p = '\0';
272 } else if (!read_till_nl(tty_in))
273 goto error;
274 if (UI_set_result(ui, uis, result) >= 0)
275 ok = 1;
276
277error:
278 if (intr_signal == SIGINT)
279 ok = -1;
280 if (!echo)
281 fprintf(tty_out, "\n");
282 if (ps >= 2 && !echo && !echo_console(ui))
283 ok = 0;
284
285 if (ps >= 1)
286 popsig();
287
288 OPENSSL_cleanse(result, BUFSIZ);
289 return ok;
290}
291
292
293/* Internal functions to open, handle and close a channel to the console. */
294static int
295open_console(UI *ui)
296{
297 CRYPTO_w_lock(CRYPTO_LOCK_UI);
298 is_a_tty = 1;
299
300 tty_in = stdin;
301 tty_out = stderr;
302
303 return 1;
304}
305
306static int
307noecho_console(UI *ui)
308{
309 DWORD mode = 0;
310 HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
311 if (handle != INVALID_HANDLE_VALUE && handle != handle) {
312 return GetConsoleMode(handle, &mode) && SetConsoleMode(handle, mode & (~ENABLE_ECHO_INPUT));
313 }
314 return 0;
315}
316
317static int
318echo_console(UI *ui)
319{
320 DWORD mode = 0;
321 HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
322 if (handle != INVALID_HANDLE_VALUE && handle != handle) {
323 return GetConsoleMode(handle, &mode) && SetConsoleMode(handle, mode | ENABLE_ECHO_INPUT);
324 }
325 return 0;
326}
327
328static int
329close_console(UI *ui)
330{
331 if (tty_in != stdin)
332 fclose(tty_in);
333 if (tty_out != stderr)
334 fclose(tty_out);
335 CRYPTO_w_unlock(CRYPTO_LOCK_UI);
336
337 return 1;
338}
339
340/* Internal functions to handle signals and act on them */
341static void
342pushsig(void)
343{
344 savsig[SIGABRT] = signal(SIGABRT, recsig);
345 savsig[SIGFPE] = signal(SIGFPE, recsig);
346 savsig[SIGILL] = signal(SIGILL, recsig);
347 savsig[SIGINT] = signal(SIGINT, recsig);
348 savsig[SIGSEGV] = signal(SIGSEGV, recsig);
349 savsig[SIGTERM] = signal(SIGTERM, recsig);
350}
351
352static void
353popsig(void)
354{
355 signal(SIGABRT, savsig[SIGABRT]);
356 signal(SIGFPE, savsig[SIGFPE]);
357 signal(SIGILL, savsig[SIGILL]);
358 signal(SIGINT, savsig[SIGINT]);
359 signal(SIGSEGV, savsig[SIGSEGV]);
360 signal(SIGTERM, savsig[SIGTERM]);
361}
362
363static void
364recsig(int i)
365{
366 intr_signal = i;
367}
diff --git a/update.sh b/update.sh
index 3a0edbb..80842cf 100755
--- a/update.sh
+++ b/update.sh
@@ -38,7 +38,11 @@ copy_src() {
38 mkdir -p $1 38 mkdir -p $1
39 rm -f $1/*.c 39 rm -f $1/*.c
40 for file in $3; do 40 for file in $3; do
41 $CP $2/src/$1/$file $1 41 if [ -e $2/src/$1/$file ]; then
42 $CP $2/src/$1/$file $1
43 else
44 $CP crypto/compat/$file $1
45 fi
42 done 46 done
43} 47}
44 48
@@ -128,7 +132,8 @@ copy_crypto bf "bf_skey.c bf_ecb.c bf_enc.c bf_cfb64.c bf_ofb64.c bf_locl.h bf_p
128 132
129copy_crypto bio "bio_lib.c bio_cb.c bio_err.c bss_mem.c bss_null.c bss_fd.c 133copy_crypto bio "bio_lib.c bio_cb.c bio_err.c bss_mem.c bss_null.c bss_fd.c
130 bss_file.c bss_sock.c bss_conn.c bf_null.c bf_buff.c b_dump.c 134 bss_file.c bss_sock.c bss_conn.c bf_null.c bf_buff.c b_dump.c
131 b_sock.c bss_acpt.c bf_nbio.c bss_log.c bss_bio.c bss_dgram.c b_print.c" 135 b_sock.c bss_acpt.c bf_nbio.c bss_log.c bss_bio.c bss_dgram.c b_print.c
136 b_posix.c b_win.c"
132 137
133copy_crypto bn "bn_add.c bn_asm.c bn_div.c bn_exp.c bn_lib.c bn_ctx.c bn_mul.c 138copy_crypto bn "bn_add.c bn_asm.c bn_div.c bn_exp.c bn_lib.c bn_ctx.c bn_mul.c
134 bn_mod.c bn_print.c bn_rand.c bn_shift.c bn_word.c bn_blind.c bn_kron.c 139 bn_mod.c bn_print.c bn_rand.c bn_shift.c bn_word.c bn_blind.c bn_kron.c
@@ -262,7 +267,7 @@ copy_crypto ts "ts_err.c ts_req_utils.c ts_req_print.c ts_rsp_utils.c
262 267
263copy_crypto txt_db "txt_db.c" 268copy_crypto txt_db "txt_db.c"
264 269
265copy_crypto ui "ui_err.c ui_lib.c ui_openssl.c ui_util.c ui_locl.h" 270copy_crypto ui "ui_err.c ui_lib.c ui_openssl.c ui_openssl_win.c ui_util.c ui_locl.h"
266 271
267copy_crypto whrlpool "wp_block.c wp_dgst.c wp_locl.h" 272copy_crypto whrlpool "wp_block.c wp_dgst.c wp_locl.h"
268 273
@@ -373,6 +378,14 @@ crypto_excludes=(
373 chacha/chacha-merged.c 378 chacha/chacha-merged.c
374 poly1305/poly1305-donna.c 379 poly1305/poly1305-donna.c
375 ) 380 )
381crypto_posix_only=(
382 bio/b_posix.c
383 ui/ui_openssl.c
384 )
385crypto_win32_only=(
386 bio/b_win.c
387 ui/ui_openssl_win.c
388 )
376(cd crypto 389(cd crypto
377 sed -e "s/libcrypto-version/${libcrypto_version}/" Makefile.am.tpl > Makefile.am 390 sed -e "s/libcrypto-version/${libcrypto_version}/" Makefile.am.tpl > Makefile.am
378 for i in `ls -1 *.c|sort`; do 391 for i in `ls -1 *.c|sort`; do
@@ -385,10 +398,18 @@ crypto_excludes=(
385 echo >> Makefile.am 398 echo >> Makefile.am
386 echo "# ${subdir}" >> Makefile.am 399 echo "# ${subdir}" >> Makefile.am
387 for i in `ls -1 $subdir/*.c|sort`; do 400 for i in `ls -1 $subdir/*.c|sort`; do
388 if ! [[ ${crypto_excludes[*]} =~ $i ]]; then 401 if [[ ${crypto_excludes[*]} =~ $i ]]; then
389 echo "libcrypto_la_SOURCES += $i" >> Makefile.am
390 else
391 echo "EXTRA_libcrypto_la_SOURCES += $i" >> Makefile.am 402 echo "EXTRA_libcrypto_la_SOURCES += $i" >> Makefile.am
403 elif [[ ${crypto_posix_only[*]} =~ $i ]]; then
404 echo "if !HOST_WIN" >> Makefile.am
405 echo "libcrypto_la_SOURCES += ${i}" >> Makefile.am
406 echo "endif" >> Makefile.am
407 elif [[ ${crypto_win32_only[*]} =~ $i ]]; then
408 echo "if HOST_WIN" >> Makefile.am
409 echo "libcrypto_la_SOURCES += ${i}" >> Makefile.am
410 echo "endif" >> Makefile.am
411 else
412 echo "libcrypto_la_SOURCES += $i" >> Makefile.am
392 fi 413 fi
393 done 414 done
394 headers=`ls -1 $subdir/*.h 2>/dev/null |sort` 415 headers=`ls -1 $subdir/*.h 2>/dev/null |sort`
@@ -405,10 +426,24 @@ $CP $libc_src/stdlib/strtonum.c apps/
405apps_excludes=( 426apps_excludes=(
406 strtonum.c 427 strtonum.c
407 ) 428 )
429apps_posix_only=(
430 apps_posix.c
431 )
432apps_win32_only=(
433 apps_win.c
434 )
408(cd apps 435(cd apps
409 $CP Makefile.am.tpl Makefile.am 436 $CP Makefile.am.tpl Makefile.am
410 for i in `ls -1 *.c|sort`; do 437 for i in `ls -1 *.c|sort`; do
411 if ! [[ ${apps_excludes[*]} =~ $i ]]; then 438 if [[ ${apps_posix_only[*]} =~ $i ]]; then
439 echo "if !HOST_WIN" >> Makefile.am
440 echo "openssl_SOURCES += ${i}" >> Makefile.am
441 echo "endif" >> Makefile.am
442 elif [[ ${apps_win32_only[*]} =~ $i ]]; then
443 echo "if HOST_WIN" >> Makefile.am
444 echo "openssl_SOURCES += ${i}" >> Makefile.am
445 echo "endif" >> Makefile.am
446 elif ! [[ ${apps_excludes[*]} =~ $i ]]; then
412 echo "openssl_SOURCES += $i" >> Makefile.am 447 echo "openssl_SOURCES += $i" >> Makefile.am
413 fi 448 fi
414 done 449 done