1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2013 - 2017, 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.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 /* <DESC>
23 * Use an in-memory user certificate and RSA key and retrieve an https page.
24 * </DESC>
25 */
26 /* Written by Ishan SinghLevett, based on Theo Borm's cacertinmem.c.
27 * Note that to maintain simplicity this example does not use a CA certificate
28 * for peer verification. However, some form of peer verification
29 * must be used in real circumstances when a secure connection is required.
30 */
31
32 #include <openssl/ssl.h>
33 #include <openssl/x509.h>
34 #include <openssl/pem.h>
35 #include <curl/curl.h>
36 #include <stdio.h>
37
writefunction(void * ptr,size_t size,size_t nmemb,void * stream)38 static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
39 {
40 fwrite(ptr, size, nmemb, stream);
41 return (nmemb*size);
42 }
43
sslctx_function(CURL * curl,void * sslctx,void * parm)44 static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
45 {
46 X509 *cert = NULL;
47 BIO *bio = NULL;
48 BIO *kbio = NULL;
49 RSA *rsa = NULL;
50 int ret;
51
52 const char *mypem = /* www.cacert.org */
53 "-----BEGIN CERTIFICATE-----\n"\
54 "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"\
55 "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"\
56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elE5tDlG+1sSPypZt90Pf6DBl\n"\
86 "Jzt7u0NDY8RD97LsaMzhGY4i+5jhe1o+ATc7iwiwovOVThrLm82asduycPAtStvY\n"\
87 "sONvRUgzEv/+PDIqVPfE94rwiCPCR/5kenHA0R6mY7AHfqQv0wGP3J8rtsYIqQ+T\n"\
88 "SCX8Ev2fQtzzxD72V7DX3WnRBnc0CkvSyqD/HMaMyRa+xMwyN2hzXwj7UfdJUzYF\n"\
89 "CpUCTPJ5GhD22Dp1nPMd8aINcGeGG7MW9S/lpOt5hvk9C8JzC6WZrG/8Z7jlLwum\n"\
90 "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\n"\
91 "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\n"\
92 "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\n"\
93 "-----END CERTIFICATE-----\n";
94
95 /*replace the XXX with the actual RSA key*/
96 const char *mykey =
97 "-----BEGIN RSA PRIVATE KEY-----\n"\
98 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
99 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
100 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
101 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
102 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
103 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
104 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
105 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
106 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
107 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
108 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
109 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
110 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
111 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
112 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
113 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
114 "-----END RSA PRIVATE KEY-----\n";
115
116 (void)curl; /* avoid warnings */
117 (void)parm; /* avoid warnings */
118
119 /* get a BIO */
120 bio = BIO_new_mem_buf((char *)mypem, -1);
121
122 if(bio == NULL) {
123 printf("BIO_new_mem_buf failed\n");
124 }
125
126 /* use it to read the PEM formatted certificate from memory into an X509
127 * structure that SSL can use
128 */
129 cert = PEM_read_bio_X509(bio, NULL, 0, NULL);
130 if(cert == NULL) {
131 printf("PEM_read_bio_X509 failed...\n");
132 }
133
134 /*tell SSL to use the X509 certificate*/
135 ret = SSL_CTX_use_certificate((SSL_CTX*)sslctx, cert);
136 if(ret != 1) {
137 printf("Use certificate failed\n");
138 }
139
140 /*create a bio for the RSA key*/
141 kbio = BIO_new_mem_buf((char *)mykey, -1);
142 if(kbio == NULL) {
143 printf("BIO_new_mem_buf failed\n");
144 }
145
146 /*read the key bio into an RSA object*/
147 rsa = PEM_read_bio_RSAPrivateKey(kbio, NULL, 0, NULL);
148 if(rsa == NULL) {
149 printf("Failed to create key bio\n");
150 }
151
152 /*tell SSL to use the RSA key from memory*/
153 ret = SSL_CTX_use_RSAPrivateKey((SSL_CTX*)sslctx, rsa);
154 if(ret != 1) {
155 printf("Use Key failed\n");
156 }
157
158 /* free resources that have been allocated by openssl functions */
159 if(bio)
160 BIO_free(bio);
161
162 if(kbio)
163 BIO_free(kbio);
164
165 if(rsa)
166 RSA_free(rsa);
167
168 if(cert)
169 X509_free(cert);
170
171 /* all set to go */
172 return CURLE_OK;
173 }
174
main(void)175 int main(void)
176 {
177 CURL *ch;
178 CURLcode rv;
179
180 curl_global_init(CURL_GLOBAL_ALL);
181 ch = curl_easy_init();
182 curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
183 curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
184 curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
185 curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
186 curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, writefunction);
187 curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
188 curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, writefunction);
189 curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
190 curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
191
192 /* both VERIFYPEER and VERIFYHOST are set to 0 in this case because there is
193 no CA certificate*/
194
195 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L);
196 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L);
197 curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
198 curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, "PEM");
199
200 /* first try: retrieve page without user certificate and key -> will fail
201 */
202 rv = curl_easy_perform(ch);
203 if(rv == CURLE_OK) {
204 printf("*** transfer succeeded ***\n");
205 }
206 else {
207 printf("*** transfer failed ***\n");
208 }
209
210 /* second try: retrieve page using user certificate and key -> will succeed
211 * load the certificate and key by installing a function doing the necessary
212 * "modifications" to the SSL CONTEXT just before link init
213 */
214 curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
215 rv = curl_easy_perform(ch);
216 if(rv == CURLE_OK) {
217 printf("*** transfer succeeded ***\n");
218 }
219 else {
220 printf("*** transfer failed ***\n");
221 }
222
223 curl_easy_cleanup(ch);
224 curl_global_cleanup();
225 return rv;
226 }
227