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