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