xref: /dflybsd-src/crypto/libressl/apps/openssl/apps_posix.c (revision 72c3367655e64985522b7a48ddfab613e869dc68)
1f5b1c8a1SJohn Marino /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2f5b1c8a1SJohn Marino  * All rights reserved.
3f5b1c8a1SJohn Marino  *
4f5b1c8a1SJohn Marino  * This package is an SSL implementation written
5f5b1c8a1SJohn Marino  * by Eric Young (eay@cryptsoft.com).
6f5b1c8a1SJohn Marino  * The implementation was written so as to conform with Netscapes SSL.
7f5b1c8a1SJohn Marino  *
8f5b1c8a1SJohn Marino  * This library is free for commercial and non-commercial use as long as
9f5b1c8a1SJohn Marino  * the following conditions are aheared to.  The following conditions
10f5b1c8a1SJohn Marino  * apply to all code found in this distribution, be it the RC4, RSA,
11f5b1c8a1SJohn Marino  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12f5b1c8a1SJohn Marino  * included with this distribution is covered by the same copyright terms
13f5b1c8a1SJohn Marino  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14f5b1c8a1SJohn Marino  *
15f5b1c8a1SJohn Marino  * Copyright remains Eric Young's, and as such any Copyright notices in
16f5b1c8a1SJohn Marino  * the code are not to be removed.
17f5b1c8a1SJohn Marino  * If this package is used in a product, Eric Young should be given attribution
18f5b1c8a1SJohn Marino  * as the author of the parts of the library used.
19f5b1c8a1SJohn Marino  * This can be in the form of a textual message at program startup or
20f5b1c8a1SJohn Marino  * in documentation (online or textual) provided with the package.
21f5b1c8a1SJohn Marino  *
22f5b1c8a1SJohn Marino  * Redistribution and use in source and binary forms, with or without
23f5b1c8a1SJohn Marino  * modification, are permitted provided that the following conditions
24f5b1c8a1SJohn Marino  * are met:
25f5b1c8a1SJohn Marino  * 1. Redistributions of source code must retain the copyright
26f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer.
27f5b1c8a1SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
28f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
29f5b1c8a1SJohn Marino  *    documentation and/or other materials provided with the distribution.
30f5b1c8a1SJohn Marino  * 3. All advertising materials mentioning features or use of this software
31f5b1c8a1SJohn Marino  *    must display the following acknowledgement:
32f5b1c8a1SJohn Marino  *    "This product includes cryptographic software written by
33f5b1c8a1SJohn Marino  *     Eric Young (eay@cryptsoft.com)"
34f5b1c8a1SJohn Marino  *    The word 'cryptographic' can be left out if the rouines from the library
35f5b1c8a1SJohn Marino  *    being used are not cryptographic related :-).
36f5b1c8a1SJohn Marino  * 4. If you include any Windows specific code (or a derivative thereof) from
37f5b1c8a1SJohn Marino  *    the apps directory (application code) you must include an acknowledgement:
38f5b1c8a1SJohn Marino  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39f5b1c8a1SJohn Marino  *
40f5b1c8a1SJohn Marino  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41f5b1c8a1SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42f5b1c8a1SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43f5b1c8a1SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44f5b1c8a1SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45f5b1c8a1SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46f5b1c8a1SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47f5b1c8a1SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48f5b1c8a1SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49f5b1c8a1SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50f5b1c8a1SJohn Marino  * SUCH DAMAGE.
51f5b1c8a1SJohn Marino  *
52f5b1c8a1SJohn Marino  * The licence and distribution terms for any publically available version or
53f5b1c8a1SJohn Marino  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54f5b1c8a1SJohn Marino  * copied and put under another distribution licence
55f5b1c8a1SJohn Marino  * [including the GNU Public Licence.]
56f5b1c8a1SJohn Marino  */
57f5b1c8a1SJohn Marino /* ====================================================================
58f5b1c8a1SJohn Marino  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
59f5b1c8a1SJohn Marino  *
60f5b1c8a1SJohn Marino  * Redistribution and use in source and binary forms, with or without
61f5b1c8a1SJohn Marino  * modification, are permitted provided that the following conditions
62f5b1c8a1SJohn Marino  * are met:
63f5b1c8a1SJohn Marino  *
64f5b1c8a1SJohn Marino  * 1. Redistributions of source code must retain the above copyright
65f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer.
66f5b1c8a1SJohn Marino  *
67f5b1c8a1SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
68f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer in
69f5b1c8a1SJohn Marino  *    the documentation and/or other materials provided with the
70f5b1c8a1SJohn Marino  *    distribution.
71f5b1c8a1SJohn Marino  *
72f5b1c8a1SJohn Marino  * 3. All advertising materials mentioning features or use of this
73f5b1c8a1SJohn Marino  *    software must display the following acknowledgment:
74f5b1c8a1SJohn Marino  *    "This product includes software developed by the OpenSSL Project
75f5b1c8a1SJohn Marino  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76f5b1c8a1SJohn Marino  *
77f5b1c8a1SJohn Marino  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78f5b1c8a1SJohn Marino  *    endorse or promote products derived from this software without
79f5b1c8a1SJohn Marino  *    prior written permission. For written permission, please contact
80f5b1c8a1SJohn Marino  *    openssl-core@openssl.org.
81f5b1c8a1SJohn Marino  *
82f5b1c8a1SJohn Marino  * 5. Products derived from this software may not be called "OpenSSL"
83f5b1c8a1SJohn Marino  *    nor may "OpenSSL" appear in their names without prior written
84f5b1c8a1SJohn Marino  *    permission of the OpenSSL Project.
85f5b1c8a1SJohn Marino  *
86f5b1c8a1SJohn Marino  * 5. Products derived from this software may not be called "OpenSSL"
87f5b1c8a1SJohn Marino  *    nor may "OpenSSL" appear in their names without prior written
88f5b1c8a1SJohn Marino  *    permission of the OpenSSL Project.
89f5b1c8a1SJohn Marino  *
90f5b1c8a1SJohn Marino  * 6. Redistributions of any form whatsoever must retain the following
91f5b1c8a1SJohn Marino  *    acknowledgment:
92f5b1c8a1SJohn Marino  *    "This product includes software developed by the OpenSSL Project
93f5b1c8a1SJohn Marino  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
94f5b1c8a1SJohn Marino  *
95f5b1c8a1SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
96f5b1c8a1SJohn Marino  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
97f5b1c8a1SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
98f5b1c8a1SJohn Marino  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
99f5b1c8a1SJohn Marino  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
100f5b1c8a1SJohn Marino  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
101f5b1c8a1SJohn Marino  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
102f5b1c8a1SJohn Marino  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
103f5b1c8a1SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
104f5b1c8a1SJohn Marino  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
105f5b1c8a1SJohn Marino  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
106f5b1c8a1SJohn Marino  * OF THE POSSIBILITY OF SUCH DAMAGE.
107f5b1c8a1SJohn Marino  * ====================================================================
108f5b1c8a1SJohn Marino  *
109f5b1c8a1SJohn Marino  * This product includes cryptographic software written by Eric Young
110f5b1c8a1SJohn Marino  * (eay@cryptsoft.com).  This product includes software written by Tim
111f5b1c8a1SJohn Marino  * Hudson (tjh@cryptsoft.com).
112f5b1c8a1SJohn Marino  *
113f5b1c8a1SJohn Marino  */
114f5b1c8a1SJohn Marino 
115f5b1c8a1SJohn Marino /*
116f5b1c8a1SJohn Marino  * Functions that need to be overridden by non-POSIX operating systems.
117f5b1c8a1SJohn Marino  */
118f5b1c8a1SJohn Marino 
119*72c33676SMaxim Ag #include <sys/resource.h>
120*72c33676SMaxim Ag #include <sys/time.h>
121f5b1c8a1SJohn Marino 
122*72c33676SMaxim Ag #include <time.h>
123f5b1c8a1SJohn Marino 
124f5b1c8a1SJohn Marino #include "apps.h"
125f5b1c8a1SJohn Marino 
126f5b1c8a1SJohn Marino #define timespecsub(tsp, usp, vsp)					\
127*72c33676SMaxim Ag 	do {								\
128f5b1c8a1SJohn Marino 		(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;		\
129*72c33676SMaxim Ag 		(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;	\
130*72c33676SMaxim Ag 		if ((vsp)->tv_nsec < 0) {				\
131f5b1c8a1SJohn Marino 			(vsp)->tv_sec--;				\
132*72c33676SMaxim Ag 			(vsp)->tv_nsec += 1000000000L;			\
133*72c33676SMaxim Ag 		}							\
134*72c33676SMaxim Ag 	} while (0)
135*72c33676SMaxim Ag 
136*72c33676SMaxim Ag double
app_timer_real(int get)137*72c33676SMaxim Ag app_timer_real(int get)
138*72c33676SMaxim Ag {
139f5b1c8a1SJohn Marino 	static struct timespec start;
140f5b1c8a1SJohn Marino 	struct timespec elapsed, now;
141*72c33676SMaxim Ag 
142*72c33676SMaxim Ag 	clock_gettime(CLOCK_MONOTONIC, &now);
143*72c33676SMaxim Ag 	if (get) {
144*72c33676SMaxim Ag 		timespecsub(&now, &start, &elapsed);
145*72c33676SMaxim Ag 		return elapsed.tv_sec + elapsed.tv_nsec / 1000000000.0;
146*72c33676SMaxim Ag 	}
147*72c33676SMaxim Ag 	start = now;
148*72c33676SMaxim Ag 	return 0.0;
149*72c33676SMaxim Ag }
150*72c33676SMaxim Ag 
151*72c33676SMaxim Ag double
app_timer_user(int get)152*72c33676SMaxim Ag app_timer_user(int get)
153*72c33676SMaxim Ag {
154*72c33676SMaxim Ag 	static struct timeval start;
155f5b1c8a1SJohn Marino 	struct timeval elapsed;
156f5b1c8a1SJohn Marino 	struct rusage now;
157f5b1c8a1SJohn Marino 
158f5b1c8a1SJohn Marino 	getrusage(RUSAGE_SELF, &now);
159f5b1c8a1SJohn Marino 	if (get) {
160f5b1c8a1SJohn Marino 		timersub(&now.ru_utime, &start, &elapsed);
161f5b1c8a1SJohn Marino 		return elapsed.tv_sec + elapsed.tv_usec / 1000000.0;
162f5b1c8a1SJohn Marino 	}
163f5b1c8a1SJohn Marino 	start = now.ru_utime;
164f5b1c8a1SJohn Marino 	return 0.0;
165f5b1c8a1SJohn Marino }
166f5b1c8a1SJohn Marino 
167f5b1c8a1SJohn Marino int
setup_ui(void)168f5b1c8a1SJohn Marino setup_ui(void)
169f5b1c8a1SJohn Marino {
170f5b1c8a1SJohn Marino 	ui_method = UI_create_method("OpenSSL application user interface");
171f5b1c8a1SJohn Marino 	UI_method_set_opener(ui_method, ui_open);
172f5b1c8a1SJohn Marino 	UI_method_set_reader(ui_method, ui_read);
173f5b1c8a1SJohn Marino 	UI_method_set_writer(ui_method, ui_write);
174f5b1c8a1SJohn Marino 	UI_method_set_closer(ui_method, ui_close);
175f5b1c8a1SJohn Marino 	return 0;
176 }
177 
178 void
destroy_ui(void)179 destroy_ui(void)
180 {
181 	if (ui_method) {
182 		UI_destroy_method(ui_method);
183 		ui_method = NULL;
184 	}
185 }
186