Test an overlong ChaCha20-Poly1305 nonce
[openssl.git] / test / shlibloadtest.c
1 /*
2  * Copyright 2016-2018 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 /* The test is only currently implemented for DSO_DLFCN and DSO_WIN32 */
16 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
17
18 #define SSL_CTX_NEW "SSL_CTX_new"
19 #define SSL_CTX_FREE "SSL_CTX_free"
20 #define TLS_METHOD "TLS_method"
21
22 #define ERR_GET_ERROR "ERR_get_error"
23 #define OPENSSL_VERSION_NUM_FUNC "OpenSSL_version_num"
24
25 typedef struct ssl_ctx_st SSL_CTX;
26 typedef struct ssl_method_st SSL_METHOD;
27 typedef const SSL_METHOD * (*TLS_method_t)(void);
28 typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
29 typedef void (*SSL_CTX_free_t)(SSL_CTX *);
30
31 typedef unsigned long (*ERR_get_error_t)(void);
32 typedef unsigned long (*OpenSSL_version_num_t)(void);
33
34 static TLS_method_t TLS_method;
35 static SSL_CTX_new_t SSL_CTX_new;
36 static SSL_CTX_free_t SSL_CTX_free;
37
38 static ERR_get_error_t ERR_get_error;
39 static OpenSSL_version_num_t OpenSSL_version_num;
40
41 #ifdef DSO_DLFCN
42
43 # define DSO_DSOBYADDR "DSO_dsobyaddr"
44 # define DSO_FREE "DSO_free"
45
46 typedef void DSO;
47 typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
48 typedef int (*DSO_free_t)(DSO *dso);
49
50 static DSO_dsobyaddr_t DSO_dsobyaddr;
51 static DSO_free_t DSO_free;
52
53 # include <dlfcn.h>
54
55 typedef void * SHLIB;
56 typedef void * SHLIB_SYM;
57 # define SHLIB_INIT NULL
58
59 static int shlib_load(const char *filename, SHLIB *lib)
60 {
61     *lib = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
62
63     if (*lib == NULL)
64         return 0;
65
66     return 1;
67 }
68
69 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
70 {
71     *sym = dlsym(lib, symname);
72
73     return *sym != NULL;
74 }
75
76 static int shlib_close(SHLIB lib)
77 {
78     if (dlclose(lib) != 0)
79         return 0;
80
81     return 1;
82 }
83
84 #elif defined(DSO_WIN32)
85
86 # include <windows.h>
87
88 typedef HINSTANCE SHLIB;
89 typedef void * SHLIB_SYM;
90 # define SHLIB_INIT 0
91
92 static int shlib_load(const char *filename, SHLIB *lib)
93 {
94     *lib = LoadLibraryA(filename);
95     if (*lib == NULL)
96         return 0;
97
98     return 1;
99 }
100
101 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
102 {
103     *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
104
105     return *sym != NULL;
106 }
107
108 static int shlib_close(SHLIB lib)
109 {
110     if (FreeLibrary(lib) == 0)
111         return 0;
112
113     return 1;
114 }
115
116 #endif
117
118 # define CRYPTO_FIRST_OPT    "-crypto_first"
119 # define SSL_FIRST_OPT       "-ssl_first"
120 # define JUST_CRYPTO_OPT     "-just_crypto"
121 # define DSO_REFTEST_OPT     "-dso_ref"
122
123 enum test_types_en {
124     CRYPTO_FIRST,
125     SSL_FIRST,
126     JUST_CRYPTO,
127     DSO_REFTEST
128 };
129
130 int main(int argc, char **argv)
131 {
132     SHLIB ssllib = SHLIB_INIT, cryptolib = SHLIB_INIT;
133     SSL_CTX *ctx;
134     union {
135         void (*func) (void);
136         SHLIB_SYM sym;
137     } tls_method_sym, ssl_ctx_new_sym, ssl_ctx_free_sym, err_get_error_sym,
138     openssl_version_num_sym, dso_dsobyaddr_sym, dso_free_sym;
139     enum test_types_en test_type;
140     int i;
141
142     if (argc != 4) {
143         printf("Unexpected number of arguments\n");
144         return 1;
145     }
146
147     if (strcmp(argv[1], CRYPTO_FIRST_OPT) == 0) {
148         test_type = CRYPTO_FIRST;
149     } else if (strcmp(argv[1], SSL_FIRST_OPT) == 0) {
150             test_type = SSL_FIRST;
151     } else if (strcmp(argv[1], JUST_CRYPTO_OPT) == 0) {
152             test_type = JUST_CRYPTO;
153     } else if (strcmp(argv[1], DSO_REFTEST_OPT) == 0) {
154             test_type = DSO_REFTEST;
155     } else {
156         printf("Unrecognised argument\n");
157         return 1;
158     }
159
160     for (i = 0; i < 2; i++) {
161         if ((i == 0 && (test_type == CRYPTO_FIRST
162                        || test_type == JUST_CRYPTO
163                        || test_type == DSO_REFTEST))
164                || (i == 1 && test_type == SSL_FIRST)) {
165             if (!shlib_load(argv[2], &cryptolib)) {
166                 printf("Unable to load libcrypto\n");
167                 return 1;
168             }
169         }
170         if ((i == 0 && test_type == SSL_FIRST)
171                 || (i == 1 && test_type == CRYPTO_FIRST)) {
172             if (!shlib_load(argv[3], &ssllib)) {
173                 printf("Unable to load libssl\n");
174                 return 1;
175             }
176         }
177     }
178
179     if (test_type != JUST_CRYPTO && test_type != DSO_REFTEST) {
180         if (!shlib_sym(ssllib, TLS_METHOD, &tls_method_sym.sym)
181                 || !shlib_sym(ssllib, SSL_CTX_NEW, &ssl_ctx_new_sym.sym)
182                 || !shlib_sym(ssllib, SSL_CTX_FREE, &ssl_ctx_free_sym.sym)) {
183             printf("Unable to load ssl symbols\n");
184             return 1;
185         }
186
187         TLS_method = (TLS_method_t)tls_method_sym.func;
188         SSL_CTX_new = (SSL_CTX_new_t)ssl_ctx_new_sym.func;
189         SSL_CTX_free = (SSL_CTX_free_t)ssl_ctx_free_sym.func;
190
191         ctx = SSL_CTX_new(TLS_method());
192         if (ctx == NULL) {
193             printf("Unable to create SSL_CTX\n");
194             return 1;
195         }
196         SSL_CTX_free(ctx);
197     }
198
199     if (!shlib_sym(cryptolib, ERR_GET_ERROR, &err_get_error_sym.sym)
200             || !shlib_sym(cryptolib, OPENSSL_VERSION_NUM_FUNC,
201                           &openssl_version_num_sym.sym)) {
202         printf("Unable to load crypto symbols\n");
203         return 1;
204     }
205
206     ERR_get_error = (ERR_get_error_t)err_get_error_sym.func;
207     OpenSSL_version_num = (OpenSSL_version_num_t)openssl_version_num_sym.func;
208
209     if (ERR_get_error() != 0) {
210         printf("Unexpected error in error queue\n");
211         return 1;
212     }
213
214     /*
215      * The bits that COMPATIBILITY_MASK lets through MUST be the same in
216      * the library and in the application.
217      * The bits that are masked away MUST be a larger or equal number in
218      * the library compared to the application.
219      */
220 # define COMPATIBILITY_MASK 0xfff00000L
221     if ((OpenSSL_version_num() & COMPATIBILITY_MASK)
222         != (OPENSSL_VERSION_NUMBER & COMPATIBILITY_MASK)) {
223         printf("Unexpected library version loaded\n");
224         return 1;
225     }
226
227     if ((OpenSSL_version_num() & ~COMPATIBILITY_MASK)
228         < (OPENSSL_VERSION_NUMBER & ~COMPATIBILITY_MASK)) {
229         printf("Unexpected library version loaded\n");
230         return 1;
231     }
232
233     if (test_type == DSO_REFTEST) {
234 # ifdef DSO_DLFCN
235         /*
236          * This is resembling the code used in ossl_init_base() and
237          * OPENSSL_atexit() to block unloading the library after dlclose().
238          * We are not testing this on Windows, because it is done there in a
239          * completely different way. Especially as a call to DSO_dsobyaddr()
240          * will always return an error, because DSO_pathbyaddr() is not
241          * implemented there.
242          */
243         if (!shlib_sym(cryptolib, DSO_DSOBYADDR, &dso_dsobyaddr_sym.sym)
244             || !shlib_sym(cryptolib, DSO_FREE, &dso_free_sym.sym)) {
245             printf("Unable to load crypto dso symbols\n");
246             return 1;
247         }
248
249         DSO_dsobyaddr = (DSO_dsobyaddr_t)dso_dsobyaddr_sym.func;
250         DSO_free = (DSO_free_t)dso_free_sym.func;
251
252         {
253             DSO *hndl;
254             /* use known symbol from crypto module */
255             if ((hndl = DSO_dsobyaddr((void (*)(void))ERR_get_error, 0)) != NULL) {
256                 DSO_free(hndl);
257             } else {
258                 printf("Unable to obtain DSO reference from crypto symbol\n");
259                 return 1;
260             }
261         }
262 # endif /* DSO_DLFCN */
263     }
264
265     for (i = 0; i < 2; i++) {
266         if ((i == 0 && test_type == CRYPTO_FIRST)
267                 || (i == 1 && test_type == SSL_FIRST)) {
268             if (!shlib_close(ssllib)) {
269                 printf("Unable to close libssl\n");
270                 return 1;
271             }
272         }
273         if ((i == 0 && (test_type == SSL_FIRST
274                        || test_type == JUST_CRYPTO
275                        || test_type == DSO_REFTEST))
276                 || (i == 1 && test_type == CRYPTO_FIRST)) {
277             if (!shlib_close(cryptolib)) {
278                 printf("Unable to close libcrypto\n");
279                 return 1;
280             }
281         }
282     }
283
284     printf("Success\n");
285     return 0;
286 }
287 #else
288 int main(void)
289 {
290     printf("Test not implemented on this platform\n");
291     return 0;
292 }
293 #endif