Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / simplessl.c
blob7a479fbe8b23a1706c17c540264d0ea7755070b8
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: simplessl.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
9 */
11 #include <stdio.h>
13 #include <curl/curl.h>
14 #include <curl/types.h>
15 #include <curl/easy.h>
18 /* some requirements for this to work:
19 1. set pCertFile to the file with the client certificate
20 2. if the key is passphrase protected, set pPassphrase to the
21 passphrase you use
22 3. if you are using a crypto engine:
23 3.1. set a #define USE_ENGINE
24 3.2. set pEngine to the name of the crypto engine you use
25 3.3. set pKeyName to the key identifier you want to use
26 4. if you don't use a crypto engine:
27 4.1. set pKeyName to the file name of your client key
28 4.2. if the format of the key file is DER, set pKeyType to "DER"
30 !! verify of the server certificate is not implemented here !!
32 **** This example only works with libcurl 7.9.3 and later! ****
36 int main(int argc, char **argv)
38 CURL *curl;
39 CURLcode res;
40 FILE *headerfile;
41 const char *pPassphrase = NULL;
43 static const char *pCertFile = "testcert.pem";
44 static const char *pCACertFile="cacert.pem";
46 const char *pKeyName;
47 const char *pKeyType;
49 const char *pEngine;
51 #if USE_ENGINE
52 pKeyName = "rsa_test";
53 pKeyType = "ENG";
54 pEngine = "chil"; /* for nChiper HSM... */
55 #else
56 pKeyName = "testkey.pem";
57 pKeyType = "PEM";
58 pEngine = NULL;
59 #endif
61 headerfile = fopen("dumpit", "w");
63 curl_global_init(CURL_GLOBAL_DEFAULT);
65 curl = curl_easy_init();
66 if(curl) {
67 /* what call to write: */
68 curl_easy_setopt(curl, CURLOPT_URL, "HTTPS://your.favourite.ssl.site");
69 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, headerfile);
71 while(1) /* do some ugly short cut... */
73 if (pEngine) /* use crypto engine */
75 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE,pEngine) != CURLE_OK)
76 { /* load the crypto engine */
77 fprintf(stderr,"can't set crypto engine\n");
78 break;
80 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT,1L) != CURLE_OK)
81 { /* set the crypto engine as default */
82 /* only needed for the first time you load
83 a engine in a curl object... */
84 fprintf(stderr,"can't set crypto engine as default\n");
85 break;
88 /* cert is stored PEM coded in file... */
89 /* since PEM is default, we needn't set it for PEM */
90 curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
92 /* set the cert for client authentication */
93 curl_easy_setopt(curl,CURLOPT_SSLCERT,pCertFile);
95 /* sorry, for engine we must set the passphrase
96 (if the key has one...) */
97 if (pPassphrase)
98 curl_easy_setopt(curl,CURLOPT_KEYPASSWD,pPassphrase);
100 /* if we use a key stored in a crypto engine,
101 we must set the key type to "ENG" */
102 curl_easy_setopt(curl,CURLOPT_SSLKEYTYPE,pKeyType);
104 /* set the private key (file or ID in engine) */
105 curl_easy_setopt(curl,CURLOPT_SSLKEY,pKeyName);
107 /* set the file with the certs vaildating the server */
108 curl_easy_setopt(curl,CURLOPT_CAINFO,pCACertFile);
110 /* disconnect if we can't validate server's cert */
111 curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,1L);
113 res = curl_easy_perform(curl);
114 break; /* we are done... */
116 /* always cleanup */
117 curl_easy_cleanup(curl);
120 curl_global_cleanup();
122 return 0;