| 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al. |
| 9 | * |
| 10 | * This software is licensed as described in the file COPYING, which |
| 11 | * you should have received as part of this distribution. The terms |
| 12 | * are also available at https://curl.se/docs/copyright.html. |
| 13 | * |
| 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 | * copies of the Software, and permit persons to whom the Software is |
| 16 | * furnished to do so, under the terms of the COPYING file. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ***************************************************************************/ |
| 22 | #include "test.h" |
| 23 | |
| 24 | #include "testutil.h" |
| 25 | #include "warnless.h" |
| 26 | #include "memdebug.h" |
| 27 | |
| 28 | static int loadfile(const char *filename, void **filedata, size_t *filesize) |
| 29 | { |
| 30 | size_t datasize = 0; |
| 31 | void *data = NULL; |
| 32 | if(filename) { |
| 33 | FILE *fInCert = fopen(filename, "rb" ); |
| 34 | |
| 35 | if(fInCert) { |
| 36 | long cert_tell = 0; |
| 37 | bool continue_reading = fseek(fInCert, 0, SEEK_END) == 0; |
| 38 | if(continue_reading) |
| 39 | cert_tell = ftell(fInCert); |
| 40 | if(cert_tell < 0) |
| 41 | continue_reading = FALSE; |
| 42 | else |
| 43 | datasize = (size_t)cert_tell; |
| 44 | if(continue_reading) |
| 45 | continue_reading = fseek(fInCert, 0, SEEK_SET) == 0; |
| 46 | if(continue_reading) |
| 47 | data = malloc(datasize + 1); |
| 48 | if((!data) || |
| 49 | ((int)fread(data, datasize, 1, fInCert) != 1)) |
| 50 | continue_reading = FALSE; |
| 51 | fclose(fInCert); |
| 52 | if(!continue_reading) { |
| 53 | free(data); |
| 54 | datasize = 0; |
| 55 | data = NULL; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | *filesize = datasize; |
| 60 | *filedata = data; |
| 61 | return data ? 1 : 0; |
| 62 | } |
| 63 | |
| 64 | static int test_cert_blob(const char *url, const char *cafile) |
| 65 | { |
| 66 | CURLcode code = CURLE_OUT_OF_MEMORY; |
| 67 | CURL *curl; |
| 68 | struct curl_blob blob; |
| 69 | size_t certsize; |
| 70 | void *certdata; |
| 71 | |
| 72 | curl = curl_easy_init(); |
| 73 | if(!curl) { |
| 74 | fprintf(stderr, "curl_easy_init() failed\n" ); |
| 75 | return CURLE_FAILED_INIT; |
| 76 | } |
| 77 | |
| 78 | if(loadfile(cafile, &certdata, &certsize)) { |
| 79 | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
| 80 | curl_easy_setopt(curl, CURLOPT_HEADER, 1L); |
| 81 | curl_easy_setopt(curl, CURLOPT_URL, url); |
| 82 | curl_easy_setopt(curl, CURLOPT_USERAGENT, "CURLOPT_CAINFO_BLOB" ); |
| 83 | curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, |
| 84 | CURLSSLOPT_REVOKE_BEST_EFFORT); |
| 85 | |
| 86 | blob.data = certdata; |
| 87 | blob.len = certsize; |
| 88 | blob.flags = CURL_BLOB_COPY; |
| 89 | curl_easy_setopt(curl, CURLOPT_CAINFO_BLOB, &blob); |
| 90 | free(certdata); |
| 91 | code = curl_easy_perform(curl); |
| 92 | } |
| 93 | curl_easy_cleanup(curl); |
| 94 | |
| 95 | return (int)code; |
| 96 | } |
| 97 | |
| 98 | int test(char *URL) |
| 99 | { |
| 100 | int res = 0; |
| 101 | curl_global_init(CURL_GLOBAL_DEFAULT); |
| 102 | if(!strcmp("check" , URL)) { |
| 103 | CURL *e; |
| 104 | CURLcode w = CURLE_OK; |
| 105 | struct curl_blob blob = {0}; |
| 106 | e = curl_easy_init(); |
| 107 | if(e) { |
| 108 | w = curl_easy_setopt(e, CURLOPT_CAINFO_BLOB, &blob); |
| 109 | if(w) |
| 110 | printf("CURLOPT_CAINFO_BLOB is not supported\n" ); |
| 111 | curl_easy_cleanup(e); |
| 112 | } |
| 113 | res = (int)w; |
| 114 | } |
| 115 | else |
| 116 | res = test_cert_blob(URL, libtest_arg2); |
| 117 | |
| 118 | curl_global_cleanup(); |
| 119 | return res; |
| 120 | } |
| 121 | |