Test the size_t constant time functions
[openssl.git] / test / shlibloadtest.c
1 /*
2  * Copyright 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 <string.h>
12 #include <stdlib.h>
13 #include <openssl/opensslv.h>
14
15 #define SSL_CTX_NEW "SSL_CTX_new"
16 #define SSL_CTX_FREE "SSL_CTX_free"
17 #define TLS_METHOD "TLS_method"
18
19 #define ERR_GET_ERROR "ERR_get_error"
20 #define OPENSSL_VERSION_NUM_FUNC "OpenSSL_version_num"
21
22 typedef struct ssl_ctx_st SSL_CTX;
23 typedef struct ssl_method_st SSL_METHOD;
24 typedef const SSL_METHOD * (*TLS_method_t)(void);
25 typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
26 typedef void (*SSL_CTX_free_t)(SSL_CTX *);
27
28 typedef unsigned long (*ERR_get_error_t)(void);
29 typedef unsigned long (*OpenSSL_version_num_t)(void);
30
31 static TLS_method_t TLS_method;
32 static SSL_CTX_new_t SSL_CTX_new;
33 static SSL_CTX_free_t SSL_CTX_free;
34
35 static ERR_get_error_t ERR_get_error;
36 static OpenSSL_version_num_t OpenSSL_version_num;
37
38
39 #ifdef DSO_DLFCN
40
41 # include <dlfcn.h>
42
43 typedef void * SHLIB;
44 typedef void * SHLIB_SYM;
45 # define SHLIB_INIT NULL
46
47 static int shlib_load(const char *filename, SHLIB *lib)
48 {
49     *lib = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
50
51     if (*lib == NULL)
52         return 0;
53
54     return 1;
55 }
56
57 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
58 {
59     *sym = dlsym(lib, symname);
60
61     return *sym != NULL;
62 }
63
64 static int shlib_close(SHLIB lib)
65 {
66     if (dlclose(lib) != 0)
67         return 0;
68
69     return 1;
70 }
71
72 #elif defined(DSO_WIN32)
73
74 # include <windows.h>
75
76 typedef HINSTANCE SHLIB;
77 typedef void * SHLIB_SYM;
78 # define SHLIB_INIT 0
79
80 static int shlib_load(const char *filename, SHLIB *lib)
81 {
82     *lib = LoadLibraryA(filename);
83     if (*lib == NULL)
84         return 0;
85
86     return 1;
87 }
88
89 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
90 {
91     *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
92
93     return *sym != NULL;
94 }
95
96 static int shlib_close(SHLIB lib)
97 {
98     if (FreeLibrary(lib) == 0)
99         return 0;
100
101     return 1;
102 }
103
104 #endif
105
106 /* The test is only currently implemented for DSO_DLFCN and DSO_WIN32 */
107 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
108
109 # define CRYPTO_FIRST_OPT    "-crypto_first"
110 # define SSL_FIRST_OPT       "-ssl_first"
111 # define JUST_CRYPTO_OPT     "-just_crypto"
112
113 enum test_types_en {
114     CRYPTO_FIRST,
115     SSL_FIRST,
116     JUST_CRYPTO
117 };
118
119 int main(int argc, char **argv)
120 {
121     SHLIB ssllib = SHLIB_INIT, cryptolib = SHLIB_INIT;
122     SSL_CTX *ctx;
123     union {
124         void (*func) (void);
125         SHLIB_SYM sym;
126     } tls_method_sym, ssl_ctx_new_sym, ssl_ctx_free_sym, err_get_error_sym,
127     openssl_version_num_sym;
128     enum test_types_en test_type;
129     int i;
130
131     if (argc != 4) {
132         printf("Unexpected number of arguments\n");
133         return 1;
134     }
135
136     if (strcmp(argv[1], CRYPTO_FIRST_OPT) == 0) {
137         test_type = CRYPTO_FIRST;
138     } else if (strcmp(argv[1], SSL_FIRST_OPT) == 0) {
139             test_type = SSL_FIRST;
140     } else if (strcmp(argv[1], JUST_CRYPTO_OPT) == 0) {
141             test_type = JUST_CRYPTO;
142     } else {
143         printf("Unrecognised argument\n");
144         return 1;
145     }
146
147     for (i = 0; i < 2; i++) {
148         if ((i == 0 && (test_type == CRYPTO_FIRST
149                        || test_type == JUST_CRYPTO))
150                || (i == 1 && test_type == SSL_FIRST)) {
151             if (!shlib_load(argv[2], &cryptolib)) {
152                 printf("Unable to load libcrypto\n");
153                 return 1;
154             }
155         }
156         if ((i == 0 && test_type == SSL_FIRST)
157                 || (i == 1 && test_type == CRYPTO_FIRST)) {
158             if (!shlib_load(argv[3], &ssllib)) {
159                 printf("Unable to load libssl\n");
160                 return 1;
161             }
162         }
163     }
164
165     if (test_type != JUST_CRYPTO) {
166         if (!shlib_sym(ssllib, TLS_METHOD, &tls_method_sym.sym)
167                 || !shlib_sym(ssllib, SSL_CTX_NEW, &ssl_ctx_new_sym.sym)
168                 || !shlib_sym(ssllib, SSL_CTX_FREE, &ssl_ctx_free_sym.sym)) {
169             printf("Unable to load ssl symbols\n");
170             return 1;
171         }
172
173         TLS_method = (TLS_method_t)tls_method_sym.func;
174         SSL_CTX_new = (SSL_CTX_new_t)ssl_ctx_new_sym.func;
175         SSL_CTX_free = (SSL_CTX_free_t)ssl_ctx_free_sym.func;
176
177         ctx = SSL_CTX_new(TLS_method());
178         if (ctx == NULL) {
179             printf("Unable to create SSL_CTX\n");
180             return 1;
181         }
182         SSL_CTX_free(ctx);
183     }
184
185     if (!shlib_sym(cryptolib, ERR_GET_ERROR, &err_get_error_sym.sym)
186             || !shlib_sym(cryptolib, OPENSSL_VERSION_NUM_FUNC,
187                           &openssl_version_num_sym.sym)) {
188         printf("Unable to load crypto symbols\n");
189         return 1;
190     }
191
192     ERR_get_error = (ERR_get_error_t)err_get_error_sym.func;
193     OpenSSL_version_num = (OpenSSL_version_num_t)openssl_version_num_sym.func;
194
195     if (ERR_get_error() != 0) {
196         printf("Unexpected error in error queue\n");
197         return 1;
198     }
199
200     if (OpenSSL_version_num() != OPENSSL_VERSION_NUMBER) {
201         printf("Unexpected library version loaded\n");
202         return 1;
203     }
204
205     for (i = 0; i < 2; i++) {
206         if ((i == 0 && test_type == CRYPTO_FIRST)
207                 || (i == 1 && test_type == SSL_FIRST)) {
208             if (!shlib_close(ssllib)) {
209                 printf("Unable to close libssl\n");
210                 return 1;
211             }
212         }
213         if ((i == 0 && (test_type == SSL_FIRST
214                        || test_type == JUST_CRYPTO))
215                 || (i == 1 && test_type == CRYPTO_FIRST)) {
216             if (!shlib_close(cryptolib)) {
217                 printf("Unable to close libcrypto\n");
218                 return 1;
219             }
220         }
221     }
222
223     printf("Success\n");
224     return 0;
225 }
226 #else
227 int main(void)
228 {
229     printf("Test not implemented on this platform\n");
230     return 0;
231 }
232 #endif