quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

cacertinmem.c (7433B)


      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 /* <DESC>
     25  * CA cert in memory with OpenSSL to get an HTTPS page.
     26  * </DESC>
     27  */
     28 
     29 #include <openssl/err.h>
     30 #include <openssl/ssl.h>
     31 #include <curl/curl.h>
     32 #include <stdio.h>
     33 
     34 #if defined(__GNUC__) || defined(__clang__)
     35 #pragma GCC diagnostic ignored "-Woverlength-strings"
     36 #endif
     37 
     38 static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
     39 {
     40   fwrite(ptr, size, nmemb, (FILE *)stream);
     41   return nmemb * size;
     42 }
     43 
     44 static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
     45 {
     46   CURLcode rv = CURLE_ABORTED_BY_CALLBACK;
     47 
     48   /** This example uses two (fake) certificates **/
     49   static const char mypem[] =
     50     "-----BEGIN CERTIFICATE-----\n"
     51     "MIIH0zCCBbugAwIBAgIIXsO3pkN/pOAwDQYJKoZIhvcNAQEFBQAwQjESMBAGA1UE\n"
     52     "AwwJQUNDVlJBSVoxMRAwDgYDVQQLDAdQS0lBQ0NWMQ0wCwYDVQQKDARBQ0NWMQsw\n"
     53     "CQYDVQQGEwJFUzAeFw0xMTA1MDUwOTM3MzdaFw0zMDEyMzEwOTM3MzdaMEIxEjAQ\n"
     54     "BgNVBAMMCUFDQ1ZSQUlaMTEQMA4GA1UECwwHUEtJQUNDVjENMAsGA1UECgwEQUND\n"
     55     "VjELMAkGA1UEBhMCRVMwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCb\n"
     56     "qau/YUqXry+XZpp0X9DZlv3P4uRm7x8fRzPCRKPfmt4ftVTdFXxpNRFvu8gMjmoY\n"
     57     "HtiP2Ra8EEg2XPBjs5BaXCQ316PWywlxufEBcoSwfdtNgM3802/J+Nq2DoLSRYWo\n"
     58     "G2ioPej0RGy9ocLLA76MPhMAhN9KSMDjIgro6TenGEyxCQ0jVn8ETdkXhBilyNpA\n"
     59     "0KIV9VMJcRz/RROE5iZe+OCIHAr8Fraocwa48GOEAqDGWuzndN9wrqODJerWx5eH\n"
     60     "k6fGioozl2A3ED6XPm4pFdahD9GILBKfb6qkxkLrQaLjlUPTAYVtjrs78yM2x/47\n"
     61     "JyCpZET/LtZ1qmxNYEAZSUNUY9rizLpm5U9EelvZaoErQNV/+QEnWCzI7UiRfD+m\n"
     62     "AM/EKXMRNt6GGT6d7hmKG9Ww7Y49nCrADdg9ZuM8Db3VlFzi4qc1GwQA9j9ajepD\n"
     63     "vV+JHanBsMyZ4k0ACtrJJ1vnE5Bc5PUzolVt3OAJTS+xJlsndQAJxGJ3KQhfnlms\n"
     64     "tn6tn1QwIgPBHnFk/vk4CpYY3QIUrCPLBhwepH2NDd4nQeit2hW3sCPdK6jT2iWH\n"
     65     "7ehVRE2I9DZ+hJp4rPcOVkkO1jMl1oRQQmwgEh0q1b688nCBpHBgvgW1m54ERL5h\n"
     66     "I6zppSSMEYCUWqKiuUnSwdzRp+0xESyeGabu4VXhwOrPDYTkF7eifKXeVSUG7szA\n"
     67     "h1xA2syVP1XgNce4hL60Xc16gwFy7ofmXx2utYXGJt/mwZrpHgJHnyqobalbz+xF\n"
     68     "d3+YJ5oyXSrjhO7FmGYvliAd3djDJ9ew+f7Zfc3Qn48LFFhRny+Lwzgt3uiP1o2H\n"
     69     "pPVWQxaZLPSkVrQ0uGE3ycJYgBugl6H8WY3pEfbRD0tVNEYqi4Y7\n"
     70     "-----END CERTIFICATE-----\n"
     71     "-----BEGIN CERTIFICATE-----\n"
     72     "MIIFtTCCA52gAwIBAgIIYY3HhjsBggUwDQYJKoZIhvcNAQEFBQAwRDEWMBQGA1UE\n"
     73     "AwwNQUNFRElDT00gUm9vdDEMMAoGA1UECwwDUEtJMQ8wDQYDVQQKDAZFRElDT00x\n"
     74     "CzAJBgNVBAYTAkVTMB4XDTA4MDQxODE2MjQyMloXDTI4MDQxMzE2MjQyMlowRDEW\n"
     75     "MBQGA1UEAwwNQUNFRElDT00gUm9vdDEMMAoGA1UECwwDUEtJMQ8wDQYDVQQKDAZF\n"
     76     "RElDT00xCzAJBgNVBAYTAkVTMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKC\n"
     77     "AgEA/5KV4WgGdrQsyFhIyv2AVClVYyT/kGWbEHV7w2rbYgIB8hiGtXxaOLHkWLn7\n"
     78     "09gtn70yN78sFW2+tfQh0hOR2QetAQXW8713zl9CgQr5auODAKgrLlUTY4HKRxx7\n"
     79     "XBZXehuDYAQ6PmXDzQHe3qTWDLqO3tkE7hdWIpuPY/1NFgu3e3eM+SW10W2ZEi5P\n"
     80     "gvoFNTPhNahXwOf9jU8/kzJPeGYDdwdY6ZXIfj7QeQCM8htRM5u8lOk6e25SLTKe\n"
     81     "I6RF+7YuE7CLGLHdztUdp0J/Vb77W7tH1PwkzQSulgUV1qzOMPPKC8W64iLgpq0i\n"
     82     "5ALudBF/TP94HTXa5gI06xgSYXcGCRZj6hitoocf8seACQl1ThCojz2GuHURwCRi\n"
     83     "ipZ7SkXp7FnFvmuD5uHorLUwHv4FB4D54SMNUI8FmP8sX+g7tq3PgbUhh8oIKiMn\n"
     84     "MCArz+2UW6yyetLHKKGKC5tNSixthT8Jcjxn4tncB7rrZXtaAWPWkFtPF2Y9fwsZ\n"
     85     "o5NjEFIqnxQWWOLcpfShFosOkYuByptZ+thrkQdlVV9SH686+5DdaaVbnG0OLLb6\n"
     86     "zqylfDJKZ0DcMDQj3dcEI2bw/FWAp/tmGYI1Z2JwOV5vx+qQQEQIHriy1tvuWacN\n"
     87     "GHk0vFQYXlPKNFHtRQrmjseCNj6nOGOpMCwXEGCSn1WHElkQwg9naRHMTh5+Spqt\n"
     88     "r0CodaxWkHS4oJyleW/c6RrIaQXpuvoDs3zk4E7Czp3otkYNbn5XOmeUwssfnHdK\n"
     89     "Z05phkOTOPu220+DkdRgfks+KzgHVZhepA==\n"
     90     "-----END CERTIFICATE-----\n";
     91 
     92   BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem));
     93   X509_STORE  *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
     94   int i;
     95   STACK_OF(X509_INFO) *inf;
     96   (void)curl;
     97   (void)parm;
     98 
     99   if(!cts || !cbio) {
    100     return rv;
    101   }
    102 
    103   inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
    104 
    105   if(!inf) {
    106     BIO_free(cbio);
    107     return rv;
    108   }
    109 
    110   for(i = 0; i < sk_X509_INFO_num(inf); i++) {
    111     X509_INFO *itmp = sk_X509_INFO_value(inf, i);
    112     if(itmp->x509) {
    113       X509_STORE_add_cert(cts, itmp->x509);
    114     }
    115     if(itmp->crl) {
    116       X509_STORE_add_crl(cts, itmp->crl);
    117     }
    118   }
    119 
    120   sk_X509_INFO_pop_free(inf, X509_INFO_free);
    121   BIO_free(cbio);
    122 
    123   rv = CURLE_OK;
    124   return rv;
    125 }
    126 
    127 int main(void)
    128 {
    129   CURL *ch;
    130   CURLcode rv;
    131 
    132   curl_global_init(CURL_GLOBAL_ALL);
    133   ch = curl_easy_init();
    134   curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
    135   curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
    136   curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
    137   curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
    138   curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, writefunction);
    139   curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
    140   curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, writefunction);
    141   curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
    142   curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
    143   curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
    144   curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
    145 
    146   /* Turn off the default CA locations, otherwise libcurl loads CA
    147    * certificates from the locations that were detected/specified at
    148    * build-time
    149    */
    150   curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
    151   curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
    152 
    153   /* first try: retrieve page without ca certificates -> should fail
    154    * unless libcurl was built --with-ca-fallback enabled at build-time
    155    */
    156   rv = curl_easy_perform(ch);
    157   if(rv == CURLE_OK)
    158     printf("*** transfer succeeded ***\n");
    159   else
    160     printf("*** transfer failed ***\n");
    161 
    162   /* use a fresh connection (optional) this option seriously impacts
    163    * performance of multiple transfers but it is necessary order to
    164    * demonstrate this example. recall that the ssl ctx callback is only called
    165    * _before_ an SSL connection is established, therefore it does not affect
    166    * existing verified SSL connections already in the connection cache
    167    * associated with this handle. normally you would set the ssl ctx function
    168    * before making any transfers, and not use this option.
    169    */
    170   curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
    171 
    172   /* second try: retrieve page using cacerts' certificate -> succeeds to load
    173    * the certificate by installing a function doing the necessary
    174    * "modifications" to the SSL CONTEXT just before link init
    175    */
    176   curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function);
    177   rv = curl_easy_perform(ch);
    178   if(rv == CURLE_OK)
    179     printf("*** transfer succeeded ***\n");
    180   else
    181     printf("*** transfer failed ***\n");
    182 
    183   curl_easy_cleanup(ch);
    184   curl_global_cleanup();
    185   return (int)rv;
    186 }