xref: /curl/tests/libtest/lib678.c (revision 25cbc2f7)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "test.h"
25 
26 #include "testutil.h"
27 #include "warnless.h"
28 #include "memdebug.h"
29 
loadfile(const char * filename,void ** filedata,size_t * filesize)30 static int loadfile(const char *filename, void **filedata, size_t *filesize)
31 {
32   size_t datasize = 0;
33   void *data = NULL;
34   if(filename) {
35     FILE *fInCert = fopen(filename, "rb");
36 
37     if(fInCert) {
38       long cert_tell = 0;
39       bool continue_reading = fseek(fInCert, 0, SEEK_END) == 0;
40       if(continue_reading)
41         cert_tell = ftell(fInCert);
42       if(cert_tell < 0)
43         continue_reading = FALSE;
44       else
45         datasize = (size_t)cert_tell;
46       if(continue_reading)
47         continue_reading = fseek(fInCert, 0, SEEK_SET) == 0;
48       if(continue_reading)
49         data = malloc(datasize + 1);
50       if((!data) ||
51          ((int)fread(data, datasize, 1, fInCert) != 1))
52         continue_reading = FALSE;
53       fclose(fInCert);
54       if(!continue_reading) {
55         free(data);
56         datasize = 0;
57         data = NULL;
58       }
59    }
60   }
61   *filesize = datasize;
62   *filedata = data;
63   return data ? 1 : 0;
64 }
65 
test_cert_blob(const char * url,const char * cafile)66 static CURLcode test_cert_blob(const char *url, const char *cafile)
67 {
68   CURLcode code = CURLE_OUT_OF_MEMORY;
69   CURL *curl;
70   struct curl_blob blob;
71   size_t certsize;
72   void *certdata;
73 
74   curl = curl_easy_init();
75   if(!curl) {
76     fprintf(stderr, "curl_easy_init() failed\n");
77     return CURLE_FAILED_INIT;
78   }
79 
80   if(loadfile(cafile, &certdata, &certsize)) {
81     curl_easy_setopt(curl, CURLOPT_VERBOSE,     1L);
82     curl_easy_setopt(curl, CURLOPT_HEADER,      1L);
83     curl_easy_setopt(curl, CURLOPT_URL,         url);
84     curl_easy_setopt(curl, CURLOPT_USERAGENT,   "CURLOPT_CAINFO_BLOB");
85     curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS,
86                      CURLSSLOPT_REVOKE_BEST_EFFORT);
87 
88     blob.data = certdata;
89     blob.len = certsize;
90     blob.flags = CURL_BLOB_COPY;
91     curl_easy_setopt(curl, CURLOPT_CAINFO_BLOB, &blob);
92     free(certdata);
93     code = curl_easy_perform(curl);
94   }
95   curl_easy_cleanup(curl);
96 
97   return code;
98 }
99 
test(char * URL)100 CURLcode test(char *URL)
101 {
102   CURLcode res = CURLE_OK;
103   curl_global_init(CURL_GLOBAL_DEFAULT);
104   if(!strcmp("check", URL)) {
105     CURL *e;
106     CURLcode w = CURLE_OK;
107     struct curl_blob blob = {0};
108     e = curl_easy_init();
109     if(e) {
110       w = curl_easy_setopt(e, CURLOPT_CAINFO_BLOB, &blob);
111       if(w)
112         printf("CURLOPT_CAINFO_BLOB is not supported\n");
113       curl_easy_cleanup(e);
114     }
115     res = w;
116   }
117   else
118     res = test_cert_blob(URL, libtest_arg2);
119 
120   curl_global_cleanup();
121   return res;
122 }
123