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