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 * Use an in-memory user certificate and RSA key and retrieve an https page. 26 * </DESC> 27 */ 28/* Written by Ishan SinghLevett, based on Theo Borm's cacertinmem.c. 29 * Note that to maintain simplicity this example does not use a CA certificate 30 * for peer verification. However, some form of peer verification 31 * must be used in real circumstances when a secure connection is required. 32 */ 33 34#include <openssl/ssl.h> 35#include <openssl/x509.h> 36#include <openssl/pem.h> 37#include <curl/curl.h> 38#include <stdio.h> 39 40static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream) 41{ 42 fwrite(ptr, size, nmemb, stream); 43 return (nmemb*size); 44} 45 46static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm) 47{ 48 X509 *cert = NULL; 49 BIO *bio = NULL; 50 BIO *kbio = NULL; 51 RSA *rsa = NULL; 52 int ret; 53 54 const char *mypem = /* www.cacert.org */ 55 "-----BEGIN CERTIFICATE-----\n"\ 56 "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"\ 57 "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"\ 58 "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BC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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\ 107 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\ 108 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\ 109 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\ 110 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\ 111 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\ 112 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\ 113 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\ 114 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\ 115 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\ 116 "-----END RSA PRIVATE KEY-----\n"; 117 118 (void)curl; /* avoid warnings */ 119 (void)parm; /* avoid warnings */ 120 121 /* get a BIO */ 122 bio = BIO_new_mem_buf((char *)mypem, -1); 123 124 if(!bio) { 125 printf("BIO_new_mem_buf failed\n"); 126 } 127 128 /* use it to read the PEM formatted certificate from memory into an X509 129 * structure that SSL can use 130 */ 131 cert = PEM_read_bio_X509(bio, NULL, 0, NULL); 132 if(!cert) { 133 printf("PEM_read_bio_X509 failed...\n"); 134 } 135 136 /* tell SSL to use the X509 certificate */ 137 ret = SSL_CTX_use_certificate((SSL_CTX*)sslctx, cert); 138 if(ret != 1) { 139 printf("Use certificate failed\n"); 140 } 141 142 /* create a bio for the RSA key */ 143 kbio = BIO_new_mem_buf((char *)mykey, -1); 144 if(!kbio) { 145 printf("BIO_new_mem_buf failed\n"); 146 } 147 148 /* read the key bio into an RSA object */ 149 rsa = PEM_read_bio_RSAPrivateKey(kbio, NULL, 0, NULL); 150 if(!rsa) { 151 printf("Failed to create key bio\n"); 152 } 153 154 /* tell SSL to use the RSA key from memory */ 155 ret = SSL_CTX_use_RSAPrivateKey((SSL_CTX*)sslctx, rsa); 156 if(ret != 1) { 157 printf("Use Key failed\n"); 158 } 159 160 /* free resources that have been allocated by openssl functions */ 161 if(bio) 162 BIO_free(bio); 163 164 if(kbio) 165 BIO_free(kbio); 166 167 if(rsa) 168 RSA_free(rsa); 169 170 if(cert) 171 X509_free(cert); 172 173 /* all set to go */ 174 return CURLE_OK; 175} 176 177int main(void) 178{ 179 CURL *ch; 180 CURLcode rv; 181 182 curl_global_init(CURL_GLOBAL_ALL); 183 ch = curl_easy_init(); 184 curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L); 185 curl_easy_setopt(ch, CURLOPT_HEADER, 0L); 186 curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L); 187 curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L); 188 curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, writefunction); 189 curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout); 190 curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, writefunction); 191 curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr); 192 curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM"); 193 194 /* both VERIFYPEER and VERIFYHOST are set to 0 in this case because there is 195 no CA certificate */ 196 197 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L); 198 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L); 199 curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/"); 200 curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, "PEM"); 201 202 /* first try: retrieve page without user certificate and key -> will fail 203 */ 204 rv = curl_easy_perform(ch); 205 if(rv == CURLE_OK) { 206 printf("*** transfer succeeded ***\n"); 207 } 208 else { 209 printf("*** transfer failed ***\n"); 210 } 211 212 /* second try: retrieve page using user certificate and key -> will succeed 213 * load the certificate and key by installing a function doing the necessary 214 * "modifications" to the SSL CONTEXT just before link init 215 */ 216 curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function); 217 rv = curl_easy_perform(ch); 218 if(rv == CURLE_OK) { 219 printf("*** transfer succeeded ***\n"); 220 } 221 else { 222 printf("*** transfer failed ***\n"); 223 } 224 225 curl_easy_cleanup(ch); 226 curl_global_cleanup(); 227 return rv; 228} 229