2021-11-04 12:45:19 +01:00
|
|
|
/*
|
|
|
|
* Test dynamic loading of libmbed*
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mbedtls/build_info.h"
|
|
|
|
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
#endif
|
|
|
|
|
2021-11-12 14:30:22 +01:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#define SO_SUFFIX ".dylib"
|
|
|
|
#else
|
|
|
|
#define SO_SUFFIX ".so"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define CRYPTO_SO_FILENAME "libmbedcrypto" SO_SUFFIX
|
|
|
|
#define X509_SO_FILENAME "libmbedx509" SO_SUFFIX
|
|
|
|
#define TLS_SO_FILENAME "libmbedtls" SO_SUFFIX
|
2021-11-04 12:45:19 +01:00
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define CHECK_DLERROR(function, argument) \
|
2021-11-04 12:45:19 +01:00
|
|
|
do \
|
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
char *CHECK_DLERROR_error = dlerror(); \
|
|
|
|
if (CHECK_DLERROR_error != NULL) \
|
2021-11-04 12:45:19 +01:00
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
fprintf(stderr, "Dynamic loading error for %s(%s): %s\n", \
|
|
|
|
function, argument, CHECK_DLERROR_error); \
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
|
2021-11-04 12:45:19 +01:00
|
|
|
} \
|
|
|
|
} \
|
2023-01-11 14:50:10 +01:00
|
|
|
while (0)
|
2021-11-04 12:45:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(void)
|
2021-11-04 12:45:19 +01:00
|
|
|
{
|
2021-11-10 19:11:32 +01:00
|
|
|
#if defined(MBEDTLS_MD_C) || defined(MBEDTLS_SSL_TLS_C)
|
2021-11-04 12:45:19 +01:00
|
|
|
unsigned n;
|
2021-11-10 19:11:32 +01:00
|
|
|
#endif
|
2021-11-04 12:45:19 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_TLS_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
void *tls_so = dlopen(TLS_SO_FILENAME, RTLD_NOW);
|
|
|
|
CHECK_DLERROR("dlopen", TLS_SO_FILENAME);
|
|
|
|
const int *(*ssl_list_ciphersuites)(void) =
|
|
|
|
dlsym(tls_so, "mbedtls_ssl_list_ciphersuites");
|
|
|
|
CHECK_DLERROR("dlsym", "mbedtls_ssl_list_ciphersuites");
|
|
|
|
const int *ciphersuites = ssl_list_ciphersuites();
|
|
|
|
for (n = 0; ciphersuites[n] != 0; n++) {/* nothing to do, we're just counting */
|
|
|
|
;
|
|
|
|
}
|
|
|
|
mbedtls_printf("dlopen(%s): %u ciphersuites\n",
|
|
|
|
TLS_SO_FILENAME, n);
|
|
|
|
dlclose(tls_so);
|
|
|
|
CHECK_DLERROR("dlclose", TLS_SO_FILENAME);
|
2021-11-04 12:45:19 +01:00
|
|
|
#endif /* MBEDTLS_SSL_TLS_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
void *x509_so = dlopen(X509_SO_FILENAME, RTLD_NOW);
|
|
|
|
CHECK_DLERROR("dlopen", X509_SO_FILENAME);
|
2021-11-04 12:45:19 +01:00
|
|
|
const mbedtls_x509_crt_profile *profile =
|
2023-01-11 14:50:10 +01:00
|
|
|
dlsym(x509_so, "mbedtls_x509_crt_profile_default");
|
|
|
|
CHECK_DLERROR("dlsym", "mbedtls_x509_crt_profile_default");
|
|
|
|
mbedtls_printf("dlopen(%s): Allowed md mask: %08x\n",
|
|
|
|
X509_SO_FILENAME, (unsigned) profile->allowed_mds);
|
|
|
|
dlclose(x509_so);
|
|
|
|
CHECK_DLERROR("dlclose", X509_SO_FILENAME);
|
2021-11-04 12:45:19 +01:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MD_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
void *crypto_so = dlopen(CRYPTO_SO_FILENAME, RTLD_NOW);
|
|
|
|
CHECK_DLERROR("dlopen", CRYPTO_SO_FILENAME);
|
|
|
|
const int *(*md_list)(void) =
|
|
|
|
dlsym(crypto_so, "mbedtls_md_list");
|
|
|
|
CHECK_DLERROR("dlsym", "mbedtls_md_list");
|
|
|
|
const int *mds = md_list();
|
|
|
|
for (n = 0; mds[n] != 0; n++) {/* nothing to do, we're just counting */
|
|
|
|
;
|
|
|
|
}
|
|
|
|
mbedtls_printf("dlopen(%s): %u hashes\n",
|
|
|
|
CRYPTO_SO_FILENAME, n);
|
|
|
|
dlclose(crypto_so);
|
|
|
|
CHECK_DLERROR("dlclose", CRYPTO_SO_FILENAME);
|
2021-11-04 12:45:19 +01:00
|
|
|
#endif /* MBEDTLS_MD_C */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-11-04 12:45:19 +01:00
|
|
|
}
|