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