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