113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci/* <DESC> 2513498266Sopenharmony_ci * CA cert in memory with OpenSSL to get an HTTPS page. 2613498266Sopenharmony_ci * </DESC> 2713498266Sopenharmony_ci */ 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#include <openssl/err.h> 3013498266Sopenharmony_ci#include <openssl/ssl.h> 3113498266Sopenharmony_ci#include <curl/curl.h> 3213498266Sopenharmony_ci#include <stdio.h> 3313498266Sopenharmony_ci 3413498266Sopenharmony_cistatic size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream) 3513498266Sopenharmony_ci{ 3613498266Sopenharmony_ci fwrite(ptr, size, nmemb, (FILE *)stream); 3713498266Sopenharmony_ci return (nmemb*size); 3813498266Sopenharmony_ci} 3913498266Sopenharmony_ci 4013498266Sopenharmony_cistatic CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm) 4113498266Sopenharmony_ci{ 4213498266Sopenharmony_ci CURLcode rv = CURLE_ABORTED_BY_CALLBACK; 4313498266Sopenharmony_ci 4413498266Sopenharmony_ci /** This example uses two (fake) certificates **/ 4513498266Sopenharmony_ci static const char mypem[] = 4613498266Sopenharmony_ci "-----BEGIN CERTIFICATE-----\n" 4713498266Sopenharmony_ci "MIIH0zCCBbugAwIBAgIIXsO3pkN/pOAwDQYJKoZIhvcNAQEFBQAwQjESMBAGA1UE\n" 4813498266Sopenharmony_ci "AwwJQUNDVlJBSVoxMRAwDgYDVQQLDAdQS0lBQ0NWMQ0wCwYDVQQKDARBQ0NWMQsw\n" 4913498266Sopenharmony_ci "CQYDVQQGEwJFUzAeFw0xMTA1MDUwOTM3MzdaFw0zMDEyMzEwOTM3MzdaMEIxEjAQ\n" 5013498266Sopenharmony_ci "BgNVBAMMCUFDQ1ZSQUlaMTEQMA4GA1UECwwHUEtJQUNDVjENMAsGA1UECgwEQUND\n" 5113498266Sopenharmony_ci "VjELMAkGA1UEBhMCRVMwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCb\n" 5213498266Sopenharmony_ci "qau/YUqXry+XZpp0X9DZlv3P4uRm7x8fRzPCRKPfmt4ftVTdFXxpNRFvu8gMjmoY\n" 5313498266Sopenharmony_ci "HtiP2Ra8EEg2XPBjs5BaXCQ316PWywlxufEBcoSwfdtNgM3802/J+Nq2DoLSRYWo\n" 5413498266Sopenharmony_ci "G2ioPej0RGy9ocLLA76MPhMAhN9KSMDjIgro6TenGEyxCQ0jVn8ETdkXhBilyNpA\n" 5513498266Sopenharmony_ci "0KIV9VMJcRz/RROE5iZe+OCIHAr8Fraocwa48GOEAqDGWuzndN9wrqODJerWx5eH\n" 5613498266Sopenharmony_ci "k6fGioozl2A3ED6XPm4pFdahD9GILBKfb6qkxkLrQaLjlUPTAYVtjrs78yM2x/47\n" 5713498266Sopenharmony_ci "JyCpZET/LtZ1qmxNYEAZSUNUY9rizLpm5U9EelvZaoErQNV/+QEnWCzI7UiRfD+m\n" 5813498266Sopenharmony_ci "AM/EKXMRNt6GGT6d7hmKG9Ww7Y49nCrADdg9ZuM8Db3VlFzi4qc1GwQA9j9ajepD\n" 5913498266Sopenharmony_ci "vV+JHanBsMyZ4k0ACtrJJ1vnE5Bc5PUzolVt3OAJTS+xJlsndQAJxGJ3KQhfnlms\n" 6013498266Sopenharmony_ci "tn6tn1QwIgPBHnFk/vk4CpYY3QIUrCPLBhwepH2NDd4nQeit2hW3sCPdK6jT2iWH\n" 6113498266Sopenharmony_ci "7ehVRE2I9DZ+hJp4rPcOVkkO1jMl1oRQQmwgEh0q1b688nCBpHBgvgW1m54ERL5h\n" 6213498266Sopenharmony_ci "I6zppSSMEYCUWqKiuUnSwdzRp+0xESyeGabu4VXhwOrPDYTkF7eifKXeVSUG7szA\n" 6313498266Sopenharmony_ci "h1xA2syVP1XgNce4hL60Xc16gwFy7ofmXx2utYXGJt/mwZrpHgJHnyqobalbz+xF\n" 6413498266Sopenharmony_ci "d3+YJ5oyXSrjhO7FmGYvliAd3djDJ9ew+f7Zfc3Qn48LFFhRny+Lwzgt3uiP1o2H\n" 6513498266Sopenharmony_ci "pPVWQxaZLPSkVrQ0uGE3ycJYgBugl6H8WY3pEfbRD0tVNEYqi4Y7\n" 6613498266Sopenharmony_ci "-----END CERTIFICATE-----\n" 6713498266Sopenharmony_ci "-----BEGIN CERTIFICATE-----\n" 6813498266Sopenharmony_ci "MIIFtTCCA52gAwIBAgIIYY3HhjsBggUwDQYJKoZIhvcNAQEFBQAwRDEWMBQGA1UE\n" 6913498266Sopenharmony_ci "AwwNQUNFRElDT00gUm9vdDEMMAoGA1UECwwDUEtJMQ8wDQYDVQQKDAZFRElDT00x\n" 7013498266Sopenharmony_ci "CzAJBgNVBAYTAkVTMB4XDTA4MDQxODE2MjQyMloXDTI4MDQxMzE2MjQyMlowRDEW\n" 7113498266Sopenharmony_ci "MBQGA1UEAwwNQUNFRElDT00gUm9vdDEMMAoGA1UECwwDUEtJMQ8wDQYDVQQKDAZF\n" 7213498266Sopenharmony_ci "RElDT00xCzAJBgNVBAYTAkVTMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKC\n" 7313498266Sopenharmony_ci "AgEA/5KV4WgGdrQsyFhIyv2AVClVYyT/kGWbEHV7w2rbYgIB8hiGtXxaOLHkWLn7\n" 7413498266Sopenharmony_ci "09gtn70yN78sFW2+tfQh0hOR2QetAQXW8713zl9CgQr5auODAKgrLlUTY4HKRxx7\n" 7513498266Sopenharmony_ci "XBZXehuDYAQ6PmXDzQHe3qTWDLqO3tkE7hdWIpuPY/1NFgu3e3eM+SW10W2ZEi5P\n" 7613498266Sopenharmony_ci "gvoFNTPhNahXwOf9jU8/kzJPeGYDdwdY6ZXIfj7QeQCM8htRM5u8lOk6e25SLTKe\n" 7713498266Sopenharmony_ci "I6RF+7YuE7CLGLHdztUdp0J/Vb77W7tH1PwkzQSulgUV1qzOMPPKC8W64iLgpq0i\n" 7813498266Sopenharmony_ci "5ALudBF/TP94HTXa5gI06xgSYXcGCRZj6hitoocf8seACQl1ThCojz2GuHURwCRi\n" 7913498266Sopenharmony_ci "ipZ7SkXp7FnFvmuD5uHorLUwHv4FB4D54SMNUI8FmP8sX+g7tq3PgbUhh8oIKiMn\n" 8013498266Sopenharmony_ci "MCArz+2UW6yyetLHKKGKC5tNSixthT8Jcjxn4tncB7rrZXtaAWPWkFtPF2Y9fwsZ\n" 8113498266Sopenharmony_ci "o5NjEFIqnxQWWOLcpfShFosOkYuByptZ+thrkQdlVV9SH686+5DdaaVbnG0OLLb6\n" 8213498266Sopenharmony_ci "zqylfDJKZ0DcMDQj3dcEI2bw/FWAp/tmGYI1Z2JwOV5vx+qQQEQIHriy1tvuWacN\n" 8313498266Sopenharmony_ci "GHk0vFQYXlPKNFHtRQrmjseCNj6nOGOpMCwXEGCSn1WHElkQwg9naRHMTh5+Spqt\n" 8413498266Sopenharmony_ci "r0CodaxWkHS4oJyleW/c6RrIaQXpuvoDs3zk4E7Czp3otkYNbn5XOmeUwssfnHdK\n" 8513498266Sopenharmony_ci "Z05phkOTOPu220+DkdRgfks+KzgHVZhepA==\n" 8613498266Sopenharmony_ci "-----END CERTIFICATE-----\n"; 8713498266Sopenharmony_ci 8813498266Sopenharmony_ci BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem)); 8913498266Sopenharmony_ci X509_STORE *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx); 9013498266Sopenharmony_ci int i; 9113498266Sopenharmony_ci STACK_OF(X509_INFO) *inf; 9213498266Sopenharmony_ci (void)curl; 9313498266Sopenharmony_ci (void)parm; 9413498266Sopenharmony_ci 9513498266Sopenharmony_ci if(!cts || !cbio) { 9613498266Sopenharmony_ci return rv; 9713498266Sopenharmony_ci } 9813498266Sopenharmony_ci 9913498266Sopenharmony_ci inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL); 10013498266Sopenharmony_ci 10113498266Sopenharmony_ci if(!inf) { 10213498266Sopenharmony_ci BIO_free(cbio); 10313498266Sopenharmony_ci return rv; 10413498266Sopenharmony_ci } 10513498266Sopenharmony_ci 10613498266Sopenharmony_ci for(i = 0; i < sk_X509_INFO_num(inf); i++) { 10713498266Sopenharmony_ci X509_INFO *itmp = sk_X509_INFO_value(inf, i); 10813498266Sopenharmony_ci if(itmp->x509) { 10913498266Sopenharmony_ci X509_STORE_add_cert(cts, itmp->x509); 11013498266Sopenharmony_ci } 11113498266Sopenharmony_ci if(itmp->crl) { 11213498266Sopenharmony_ci X509_STORE_add_crl(cts, itmp->crl); 11313498266Sopenharmony_ci } 11413498266Sopenharmony_ci } 11513498266Sopenharmony_ci 11613498266Sopenharmony_ci sk_X509_INFO_pop_free(inf, X509_INFO_free); 11713498266Sopenharmony_ci BIO_free(cbio); 11813498266Sopenharmony_ci 11913498266Sopenharmony_ci rv = CURLE_OK; 12013498266Sopenharmony_ci return rv; 12113498266Sopenharmony_ci} 12213498266Sopenharmony_ci 12313498266Sopenharmony_ciint main(void) 12413498266Sopenharmony_ci{ 12513498266Sopenharmony_ci CURL *ch; 12613498266Sopenharmony_ci CURLcode rv; 12713498266Sopenharmony_ci 12813498266Sopenharmony_ci curl_global_init(CURL_GLOBAL_ALL); 12913498266Sopenharmony_ci ch = curl_easy_init(); 13013498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L); 13113498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_HEADER, 0L); 13213498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L); 13313498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L); 13413498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, writefunction); 13513498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout); 13613498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, writefunction); 13713498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr); 13813498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM"); 13913498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L); 14013498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/"); 14113498266Sopenharmony_ci 14213498266Sopenharmony_ci /* Turn off the default CA locations, otherwise libcurl will load CA 14313498266Sopenharmony_ci * certificates from the locations that were detected/specified at 14413498266Sopenharmony_ci * build-time 14513498266Sopenharmony_ci */ 14613498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_CAINFO, NULL); 14713498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_CAPATH, NULL); 14813498266Sopenharmony_ci 14913498266Sopenharmony_ci /* first try: retrieve page without ca certificates -> should fail 15013498266Sopenharmony_ci * unless libcurl was built --with-ca-fallback enabled at build-time 15113498266Sopenharmony_ci */ 15213498266Sopenharmony_ci rv = curl_easy_perform(ch); 15313498266Sopenharmony_ci if(rv == CURLE_OK) 15413498266Sopenharmony_ci printf("*** transfer succeeded ***\n"); 15513498266Sopenharmony_ci else 15613498266Sopenharmony_ci printf("*** transfer failed ***\n"); 15713498266Sopenharmony_ci 15813498266Sopenharmony_ci /* use a fresh connection (optional) 15913498266Sopenharmony_ci * this option seriously impacts performance of multiple transfers but 16013498266Sopenharmony_ci * it is necessary order to demonstrate this example. recall that the 16113498266Sopenharmony_ci * ssl ctx callback is only called _before_ an SSL connection is 16213498266Sopenharmony_ci * established, therefore it will not affect existing verified SSL 16313498266Sopenharmony_ci * connections already in the connection cache associated with this 16413498266Sopenharmony_ci * handle. normally you would set the ssl ctx function before making 16513498266Sopenharmony_ci * any transfers, and not use this option. 16613498266Sopenharmony_ci */ 16713498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L); 16813498266Sopenharmony_ci 16913498266Sopenharmony_ci /* second try: retrieve page using cacerts' certificate -> will succeed 17013498266Sopenharmony_ci * load the certificate by installing a function doing the necessary 17113498266Sopenharmony_ci * "modifications" to the SSL CONTEXT just before link init 17213498266Sopenharmony_ci */ 17313498266Sopenharmony_ci curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function); 17413498266Sopenharmony_ci rv = curl_easy_perform(ch); 17513498266Sopenharmony_ci if(rv == CURLE_OK) 17613498266Sopenharmony_ci printf("*** transfer succeeded ***\n"); 17713498266Sopenharmony_ci else 17813498266Sopenharmony_ci printf("*** transfer failed ***\n"); 17913498266Sopenharmony_ci 18013498266Sopenharmony_ci curl_easy_cleanup(ch); 18113498266Sopenharmony_ci curl_global_cleanup(); 18213498266Sopenharmony_ci return rv; 18313498266Sopenharmony_ci} 184