1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel (at) 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 http://curl.haxx.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 <stdio.h> 23 24 #include <curl/curl.h> 25 26 /* some requirements for this to work: 27 1. set pCertFile to the file with the client certificate 28 2. if the key is passphrase protected, set pPassphrase to the 29 passphrase you use 30 3. if you are using a crypto engine: 31 3.1. set a #define USE_ENGINE 32 3.2. set pEngine to the name of the crypto engine you use 33 3.3. set pKeyName to the key identifier you want to use 34 4. if you don't use a crypto engine: 35 4.1. set pKeyName to the file name of your client key 36 4.2. if the format of the key file is DER, set pKeyType to "DER" 37 38 !! verify of the server certificate is not implemented here !! 39 40 **** This example only works with libcurl 7.9.3 and later! **** 41 42 */ 43 44 int main(void) 45 { 46 int i; 47 CURL *curl; 48 CURLcode res; 49 FILE *headerfile; 50 const char *pPassphrase = NULL; 51 52 static const char *pCertFile = "testcert.pem"; 53 static const char *pCACertFile="cacert.pem"; 54 55 const char *pKeyName; 56 const char *pKeyType; 57 58 const char *pEngine; 59 60 #ifdef USE_ENGINE 61 pKeyName = "rsa_test"; 62 pKeyType = "ENG"; 63 pEngine = "chil"; /* for nChiper HSM... */ 64 #else 65 pKeyName = "testkey.pem"; 66 pKeyType = "PEM"; 67 pEngine = NULL; 68 #endif 69 70 headerfile = fopen("dumpit", "w"); 71 72 curl_global_init(CURL_GLOBAL_DEFAULT); 73 74 curl = curl_easy_init(); 75 if(curl) { 76 /* what call to write: */ 77 curl_easy_setopt(curl, CURLOPT_URL, "HTTPS://your.favourite.ssl.site"); 78 curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile); 79 80 for(i = 0; i < 1; i++) /* single-iteration loop, just to break out from */ 81 { 82 if (pEngine) /* use crypto engine */ 83 { 84 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE,pEngine) != CURLE_OK) 85 { /* load the crypto engine */ 86 fprintf(stderr,"can't set crypto engine\n"); 87 break; 88 } 89 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT,1L) != CURLE_OK) 90 { /* set the crypto engine as default */ 91 /* only needed for the first time you load 92 a engine in a curl object... */ 93 fprintf(stderr,"can't set crypto engine as default\n"); 94 break; 95 } 96 } 97 /* cert is stored PEM coded in file... */ 98 /* since PEM is default, we needn't set it for PEM */ 99 curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM"); 100 101 /* set the cert for client authentication */ 102 curl_easy_setopt(curl,CURLOPT_SSLCERT,pCertFile); 103 104 /* sorry, for engine we must set the passphrase 105 (if the key has one...) */ 106 if (pPassphrase) 107 curl_easy_setopt(curl,CURLOPT_KEYPASSWD,pPassphrase); 108 109 /* if we use a key stored in a crypto engine, 110 we must set the key type to "ENG" */ 111 curl_easy_setopt(curl,CURLOPT_SSLKEYTYPE,pKeyType); 112 113 /* set the private key (file or ID in engine) */ 114 curl_easy_setopt(curl,CURLOPT_SSLKEY,pKeyName); 115 116 /* set the file with the certs vaildating the server */ 117 curl_easy_setopt(curl,CURLOPT_CAINFO,pCACertFile); 118 119 /* disconnect if we can't validate server's cert */ 120 curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,1L); 121 122 /* Perform the request, res will get the return code */ 123 res = curl_easy_perform(curl); 124 /* Check for errors */ 125 if(res != CURLE_OK) 126 fprintf(stderr, "curl_easy_perform() failed: %s\n", 127 curl_easy_strerror(res)); 128 129 /* we are done... */ 130 } 131 /* always cleanup */ 132 curl_easy_cleanup(curl); 133 } 134 135 curl_global_cleanup(); 136 137 return 0; 138 } 139