doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
simplessl.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * $Id: simplessl.c,v 1.5 2003/05/20 12:44:55 bagder Exp $
9  */
10 
11 #include <stdio.h>
12 
13 #include <curl/curl.h>
14 #include <curl/types.h>
15 #include <curl/easy.h>
16 
17 
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"
29 
30  !! verify of the server certificate is not implemented here !!
31 
32  **** This example only works with libcurl 7.9.3 and later! ****
33 
34 */
35 
36 int main(int argc, char **argv)
37 {
38  CURL *curl;
39  CURLcode res;
40  FILE *headerfile;
41 
42  const char *pCertFile = "testcert.pem";
43  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  const char *pPassphrase = NULL;
61 
62  headerfile = fopen("dumpit", "w");
63 
65 
66  curl = curl_easy_init();
67  if(curl) {
68  /* what call to write: */
69  curl_easy_setopt(curl, CURLOPT_URL, "HTTPS://your.favourite.ssl.site");
70  curl_easy_setopt(curl, CURLOPT_WRITEHEADER, headerfile);
71 
72  while(1) /* do some ugly short cut... */
73  {
74  if (pEngine) /* use crypto engine */
75  {
76  if (curl_easy_setopt(curl, CURLOPT_SSLENGINE,pEngine) != CURLE_OK)
77  { /* load the crypto engine */
78  fprintf(stderr,"can't set crypto engine\n");
79  break;
80  }
81  if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT,1) != CURLE_OK)
82  { /* set the crypto engine as default */
83  /* only needed for the first time you load
84  a engine in a curl object... */
85  fprintf(stderr,"can't set crypto engine as default\n");
86  break;
87  }
88  }
89  /* cert is stored PEM coded in file... */
90  /* since PEM is default, we needn't set it for PEM */
91  curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
92  /* set the cert for client authentication */
93  curl_easy_setopt(curl,CURLOPT_SSLCERT,pCertFile);
94  /* sorry, for engine we must set the passphrase
95  (if the key has one...) */
96  if (pPassphrase)
97  curl_easy_setopt(curl,CURLOPT_SSLKEYPASSWD,pPassphrase);
98  /* if we use a key stored in a crypto engine,
99  we must set the key type to "ENG" */
100  curl_easy_setopt(curl,CURLOPT_SSLKEYTYPE,pKeyType);
101  /* set the private key (file or ID in engine) */
102  curl_easy_setopt(curl,CURLOPT_SSLKEY,pKeyName);
103  /* set the file with the certs vaildating the server */
104  curl_easy_setopt(curl,CURLOPT_CAINFO,pCACertFile);
105  /* disconnect if we can't validate server's cert */
106  curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,1);
107 
108  res = curl_easy_perform(curl);
109  break; /* we are done... */
110  }
111  /* always cleanup */
112  curl_easy_cleanup(curl);
113  }
114 
116 
117  return 0;
118 }
CURLcode curl_global_init(long flags)
Globally initializes cURL given a bitwise set of the different features to initialize.
Definition: easy.c:147
CURLcode curl_easy_perform(CURL *curl)
Definition: easy.c:260
CURLcode
Definition: curl.h:209
CURLcode curl_easy_setopt(CURL *curl, CURLoption option,...)
Definition: easy.c:217
void curl_global_cleanup(void)
Globally cleanup cURL, uses the value of "init_flags" to determine what needs to be cleaned up and ...
Definition: easy.c:174
#define NULL
Definition: Lib.h:88
#define CURL_GLOBAL_DEFAULT
Definition: curl.h:1155
int main(int argc, char **argv)
Definition: simplessl.c:36
Definition: curl.h:210
void CURL
Definition: types.h:25
GLuint res
Definition: glext.h:5385
void curl_easy_cleanup(CURL *curl)
Definition: easy.c:288
CURL * curl_easy_init(void)
Definition: easy.c:195