2020-07-13 09:17:56 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2020-07-13 09:17:56 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2020-07-13 09:17:56 +08:00
|
|
|
***************************************************************************/
|
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
#include "testutil.h"
|
|
|
|
#include "warnless.h"
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
|
|
|
static int loadfile(const char *filename, void **filedata, size_t *filesize)
|
|
|
|
{
|
|
|
|
size_t datasize = 0;
|
|
|
|
void *data = NULL;
|
|
|
|
if(filename) {
|
|
|
|
FILE *fInCert = fopen(filename, "rb");
|
|
|
|
|
|
|
|
if(fInCert) {
|
|
|
|
long cert_tell = 0;
|
|
|
|
bool continue_reading = fseek(fInCert, 0, SEEK_END) == 0;
|
|
|
|
if(continue_reading)
|
|
|
|
cert_tell = ftell(fInCert);
|
|
|
|
if(cert_tell < 0)
|
|
|
|
continue_reading = FALSE;
|
|
|
|
else
|
|
|
|
datasize = (size_t)cert_tell;
|
|
|
|
if(continue_reading)
|
2023-04-29 00:07:33 +08:00
|
|
|
continue_reading = fseek(fInCert, 0, SEEK_SET) == 0;
|
2020-07-13 09:17:56 +08:00
|
|
|
if(continue_reading)
|
2023-04-29 00:07:33 +08:00
|
|
|
data = malloc(datasize + 1);
|
2020-07-13 09:17:56 +08:00
|
|
|
if((!data) ||
|
2023-04-29 00:07:33 +08:00
|
|
|
((int)fread(data, datasize, 1, fInCert) != 1))
|
|
|
|
continue_reading = FALSE;
|
2020-07-13 09:17:56 +08:00
|
|
|
fclose(fInCert);
|
|
|
|
if(!continue_reading) {
|
|
|
|
free(data);
|
|
|
|
datasize = 0;
|
|
|
|
data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*filesize = datasize;
|
|
|
|
*filedata = data;
|
|
|
|
return data ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2024-05-12 03:36:05 +08:00
|
|
|
static CURLcode test_cert_blob(const char *url, const char *cafile)
|
2020-07-13 09:17:56 +08:00
|
|
|
{
|
|
|
|
CURLcode code = CURLE_OUT_OF_MEMORY;
|
|
|
|
CURL *curl;
|
|
|
|
struct curl_blob blob;
|
|
|
|
size_t certsize;
|
|
|
|
void *certdata;
|
|
|
|
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if(!curl) {
|
|
|
|
fprintf(stderr, "curl_easy_init() failed\n");
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(loadfile(cafile, &certdata, &certsize)) {
|
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "CURLOPT_CAINFO_BLOB");
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS,
|
|
|
|
CURLSSLOPT_REVOKE_BEST_EFFORT);
|
|
|
|
|
|
|
|
blob.data = certdata;
|
|
|
|
blob.len = certsize;
|
|
|
|
blob.flags = CURL_BLOB_COPY;
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CAINFO_BLOB, &blob);
|
|
|
|
free(certdata);
|
|
|
|
code = curl_easy_perform(curl);
|
|
|
|
}
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
2024-05-12 03:36:05 +08:00
|
|
|
return code;
|
2020-07-13 09:17:56 +08:00
|
|
|
}
|
|
|
|
|
2024-05-12 03:36:05 +08:00
|
|
|
CURLcode test(char *URL)
|
2020-07-13 09:17:56 +08:00
|
|
|
{
|
2024-05-12 03:36:05 +08:00
|
|
|
CURLcode res = CURLE_OK;
|
2020-07-13 09:17:56 +08:00
|
|
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
|
|
|
if(!strcmp("check", URL)) {
|
|
|
|
CURL *e;
|
|
|
|
CURLcode w = CURLE_OK;
|
|
|
|
struct curl_blob blob = {0};
|
|
|
|
e = curl_easy_init();
|
|
|
|
if(e) {
|
|
|
|
w = curl_easy_setopt(e, CURLOPT_CAINFO_BLOB, &blob);
|
|
|
|
if(w)
|
|
|
|
printf("CURLOPT_CAINFO_BLOB is not supported\n");
|
|
|
|
curl_easy_cleanup(e);
|
|
|
|
}
|
2024-05-12 03:36:05 +08:00
|
|
|
res = w;
|
2020-07-13 09:17:56 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
res = test_cert_blob(URL, libtest_arg2);
|
|
|
|
|
|
|
|
curl_global_cleanup();
|
|
|
|
return res;
|
|
|
|
}
|