Initial commit of visual studio 9 git build superproject.
[git-build-vc9.git] / curl / docs / examples / curlx.c
blobbd192865f78517a9b9725efeb2bca61846be815c
1 /*
2 curlx.c Authors: Peter Sylvester, Jean-Paul Merlin
4 This is a little program to demonstrate the usage of
6 - an ssl initialisation callback setting a user key and trustbases
7 coming from a pkcs12 file
8 - using an ssl application callback to find a URI in the
9 certificate presented during ssl session establishment.
15 * Copyright (c) 2003 The OpenEvidence Project. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions, the following disclaimer,
23 * and the original OpenSSL and SSLeay Licences below.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions, the following disclaimer
27 * and the original OpenSSL and SSLeay Licences below in
28 * the documentation and/or other materials provided with the
29 * distribution.
31 * 3. All advertising materials mentioning features or use of this
32 * software must display the following acknowledgments:
33 * "This product includes software developed by the Openevidence Project
34 * for use in the OpenEvidence Toolkit. (http://www.openevidence.org/)"
35 * This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37 * This product includes cryptographic software written by Eric Young
38 * (eay@cryptsoft.com). This product includes software written by Tim
39 * Hudson (tjh@cryptsoft.com)."
41 * 4. The names "OpenEvidence Toolkit" and "OpenEvidence Project" must not be
42 * used to endorse or promote products derived from this software without
43 * prior written permission. For written permission, please contact
44 * openevidence-core@openevidence.org.
46 * 5. Products derived from this software may not be called "OpenEvidence"
47 * nor may "OpenEvidence" appear in their names without prior written
48 * permission of the OpenEvidence Project.
50 * 6. Redistributions of any form whatsoever must retain the following
51 * acknowledgments:
52 * "This product includes software developed by the OpenEvidence Project
53 * for use in the OpenEvidence Toolkit (http://www.openevidence.org/)
54 * This product includes software developed by the OpenSSL Project
55 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
56 * This product includes cryptographic software written by Eric Young
57 * (eay@cryptsoft.com). This product includes software written by Tim
58 * Hudson (tjh@cryptsoft.com)."
60 * THIS SOFTWARE IS PROVIDED BY THE OpenEvidence PROJECT ``AS IS'' AND ANY
61 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
63 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenEvidence PROJECT OR
64 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
65 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
66 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
67 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
69 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
70 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
71 * OF THE POSSIBILITY OF SUCH DAMAGE.
72 * ====================================================================
74 * This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit (http://www.openssl.org/)
76 * This product includes cryptographic software written by Eric Young
77 * (eay@cryptsoft.com). This product includes software written by Tim
78 * Hudson (tjh@cryptsoft.com).
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <curl/curl.h>
86 #include <openssl/x509v3.h>
87 #include <openssl/x509_vfy.h>
88 #include <openssl/crypto.h>
89 #include <openssl/lhash.h>
90 #include <openssl/objects.h>
91 #include <openssl/err.h>
92 #include <openssl/evp.h>
93 #include <openssl/x509.h>
94 #include <openssl/pkcs12.h>
95 #include <openssl/bio.h>
96 #include <openssl/ssl.h>
98 static const char *curlx_usage[]={
99 "usage: curlx args\n",
100 " -p12 arg - tia file ",
101 " -envpass arg - environement variable which content the tia private key password",
102 " -out arg - output file (response)- default stdout",
103 " -in arg - input file (request)- default stdin",
104 " -connect arg - URL of the server for the connection ex: www.openevidence.org",
105 " -mimetype arg - MIME type for data in ex : application/timestamp-query or application/dvcs -default application/timestamp-query",
106 " -acceptmime arg - MIME type acceptable for the response ex : application/timestamp-response or application/dvcs -default none",
107 " -accesstype arg - an Object identifier in an AIA/SIA method, e.g. AD_DVCS or ad_timestamping",
108 NULL
113 ./curlx -p12 psy.p12 -envpass XX -in request -verbose -accesstype AD_DVCS
114 -mimetype application/dvcs -acceptmime application/dvcs -out response
119 * We use this ZERO_NULL to avoid picky compiler warnings,
120 * when assigning a NULL pointer to a function pointer var.
123 #define ZERO_NULL 0
125 /* This is a context that we pass to all callbacks */
127 typedef struct sslctxparm_st {
128 unsigned char * p12file ;
129 const char * pst ;
130 PKCS12 * p12 ;
131 EVP_PKEY * pkey ;
132 X509 * usercert ;
133 STACK_OF(X509) * ca ;
134 CURL * curl;
135 BIO * errorbio;
136 int accesstype ;
137 int verbose;
139 } sslctxparm;
141 /* some helper function. */
143 static char *i2s_ASN1_IA5STRING( ASN1_IA5STRING *ia5)
145 char *tmp;
146 if(!ia5 || !ia5->length)
147 return NULL;
148 tmp = OPENSSL_malloc(ia5->length + 1);
149 memcpy(tmp, ia5->data, ia5->length);
150 tmp[ia5->length] = 0;
151 return tmp;
154 /* A conveniance routine to get an access URI. */
156 static unsigned char *my_get_ext(X509 * cert, const int type, int extensiontype) {
158 int i;
159 STACK_OF(ACCESS_DESCRIPTION) * accessinfo ;
160 accessinfo = X509_get_ext_d2i(cert, extensiontype, NULL, NULL) ;
162 if (!sk_ACCESS_DESCRIPTION_num(accessinfo))
163 return NULL;
164 for (i = 0; i < sk_ACCESS_DESCRIPTION_num(accessinfo); i++) {
165 ACCESS_DESCRIPTION * ad = sk_ACCESS_DESCRIPTION_value(accessinfo, i);
166 if (OBJ_obj2nid(ad->method) == type) {
167 if (ad->location->type == GEN_URI) {
168 return i2s_ASN1_IA5STRING(ad->location->d.ia5);
170 return NULL;
173 return NULL;
176 /* This is an application verification call back, it does not
177 perform any addition verification but tries to find a URL
178 in the presented certificat. If found, this will become
179 the URL to be used in the POST.
182 static int ssl_app_verify_callback(X509_STORE_CTX *ctx, void *arg)
184 sslctxparm * p = (sslctxparm *) arg;
185 int ok;
187 if (p->verbose > 2)
188 BIO_printf(p->errorbio,"entering ssl_app_verify_callback\n");
190 if ((ok= X509_verify_cert(ctx)) && ctx->cert) {
191 unsigned char * accessinfo ;
192 if (p->verbose > 1)
193 X509_print_ex(p->errorbio,ctx->cert,0,0);
195 if (accessinfo = my_get_ext(ctx->cert,p->accesstype ,NID_sinfo_access)) {
196 if (p->verbose)
197 BIO_printf(p->errorbio,"Setting URL from SIA to: %s\n", accessinfo);
199 curl_easy_setopt(p->curl, CURLOPT_URL,accessinfo);
201 else if (accessinfo = my_get_ext(ctx->cert,p->accesstype,
202 NID_info_access)) {
203 if (p->verbose)
204 BIO_printf(p->errorbio,"Setting URL from AIA to: %s\n", accessinfo);
206 curl_easy_setopt(p->curl, CURLOPT_URL,accessinfo);
209 if (p->verbose > 2)
210 BIO_printf(p->errorbio,"leaving ssl_app_verify_callback with %d\n", ok);
211 return(ok);
215 /* This is an example of an curl SSL initialisation call back. The callback sets:
216 - a private key and certificate
217 - a trusted ca certificate
218 - a preferred cipherlist
219 - an application verification callback (the function above)
222 static CURLcode sslctxfun(CURL * curl, void * sslctx, void * parm) {
224 sslctxparm * p = (sslctxparm *) parm;
225 SSL_CTX * ctx = (SSL_CTX *) sslctx ;
227 if (!SSL_CTX_use_certificate(ctx,p->usercert)) {
228 BIO_printf(p->errorbio, "SSL_CTX_use_certificate problem\n"); goto err;
230 if (!SSL_CTX_use_PrivateKey(ctx,p->pkey)) {
231 BIO_printf(p->errorbio, "SSL_CTX_use_PrivateKey\n"); goto err;
234 if (!SSL_CTX_check_private_key(ctx)) {
235 BIO_printf(p->errorbio, "SSL_CTX_check_private_key\n"); goto err;
238 SSL_CTX_set_quiet_shutdown(ctx,1);
239 SSL_CTX_set_cipher_list(ctx,"RC4-MD5");
240 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
242 X509_STORE_add_cert(ctx->cert_store,sk_X509_value(p->ca,
243 sk_X509_num(p->ca)-1));
245 SSL_CTX_set_verify_depth(ctx,2);
247 SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER,ZERO_NULL);
249 SSL_CTX_set_cert_verify_callback(ctx, ssl_app_verify_callback, parm);
252 return CURLE_OK ;
253 err:
254 ERR_print_errors(p->errorbio);
255 return CURLE_SSL_CERTPROBLEM;
259 int main(int argc, char **argv) {
261 BIO* in=NULL;
262 BIO* out=NULL;
264 char * outfile = NULL;
265 char * infile = NULL ;
267 int tabLength=100;
268 char *binaryptr;
269 char* mimetype;
270 char* mimetypeaccept=NULL;
271 char* contenttype;
272 const char** pp;
273 unsigned char* hostporturl = NULL;
274 BIO * p12bio ;
275 char **args = argv + 1;
276 unsigned char * serverurl;
277 sslctxparm p;
278 char *response;
280 CURLcode res;
281 struct curl_slist * headers=NULL;
282 int badarg=0;
284 binaryptr=(char*)malloc(tabLength);
286 p.verbose = 0;
287 p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE);
289 curl_global_init(CURL_GLOBAL_DEFAULT);
291 /* we need some more for the P12 decoding */
293 OpenSSL_add_all_ciphers();
294 OpenSSL_add_all_digests();
295 ERR_load_crypto_strings();
299 while (*args && *args[0] == '-') {
300 if (!strcmp (*args, "-in")) {
301 if (args[1]) {
302 infile=*(++args);
303 } else badarg=1;
304 } else if (!strcmp (*args, "-out")) {
305 if (args[1]) {
306 outfile=*(++args);
307 } else badarg=1;
308 } else if (!strcmp (*args, "-p12")) {
309 if (args[1]) {
310 p.p12file = *(++args);
311 } else badarg=1;
312 } else if (strcmp(*args,"-envpass") == 0) {
313 if (args[1]) {
314 p.pst = getenv(*(++args));
315 } else badarg=1;
316 } else if (strcmp(*args,"-connect") == 0) {
317 if (args[1]) {
318 hostporturl = *(++args);
319 } else badarg=1;
320 } else if (strcmp(*args,"-mimetype") == 0) {
321 if (args[1]) {
322 mimetype = *(++args);
323 } else badarg=1;
324 } else if (strcmp(*args,"-acceptmime") == 0) {
325 if (args[1]) {
326 mimetypeaccept = *(++args);
327 } else badarg=1;
328 } else if (strcmp(*args,"-accesstype") == 0) {
329 if (args[1]) {
330 if ((p.accesstype = OBJ_obj2nid(OBJ_txt2obj(*++args,0))) == 0) badarg=1;
331 } else badarg=1;
332 } else if (strcmp(*args,"-verbose") == 0) {
333 p.verbose++;
334 } else badarg=1;
335 args++;
338 if (mimetype==NULL || mimetypeaccept == NULL) badarg = 1;
340 if (badarg) {
341 for (pp=curlx_usage; (*pp != NULL); pp++)
342 BIO_printf(p.errorbio,"%s\n",*pp);
343 BIO_printf(p.errorbio,"\n");
344 goto err;
349 /* set input */
351 if ((in=BIO_new(BIO_s_file())) == NULL) {
352 BIO_printf(p.errorbio, "Error setting input bio\n");
353 goto err;
354 } else if (infile == NULL)
355 BIO_set_fp(in,stdin,BIO_NOCLOSE|BIO_FP_TEXT);
356 else if (BIO_read_filename(in,infile) <= 0) {
357 BIO_printf(p.errorbio, "Error opening input file %s\n", infile);
358 BIO_free(in);
359 goto err;
362 /* set output */
364 if ((out=BIO_new(BIO_s_file())) == NULL) {
365 BIO_printf(p.errorbio, "Error setting output bio.\n");
366 goto err;
367 } else if (outfile == NULL)
368 BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
369 else if (BIO_write_filename(out,outfile) <= 0) {
370 BIO_printf(p.errorbio, "Error opening output file %s\n", outfile);
371 BIO_free(out);
372 goto err;
376 p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE);
378 if (!(p.curl = curl_easy_init())) {
379 BIO_printf(p.errorbio, "Cannot init curl lib\n");
380 goto err;
385 if (!(p12bio = BIO_new_file(p.p12file , "rb"))) {
386 BIO_printf(p.errorbio, "Error opening P12 file %s\n", p.p12file); goto err;
388 if (!(p.p12 = d2i_PKCS12_bio (p12bio, NULL))) {
389 BIO_printf(p.errorbio, "Cannot decode P12 structure %s\n", p.p12file); goto err;
392 p.ca= NULL;
393 if (!(PKCS12_parse (p.p12, p.pst, &(p.pkey), &(p.usercert), &(p.ca) ) )) {
394 BIO_printf(p.errorbio,"Invalid P12 structure in %s\n", p.p12file); goto err;
397 if (sk_X509_num(p.ca) <= 0) {
398 BIO_printf(p.errorbio,"No trustworthy CA given.%s\n", p.p12file); goto err;
401 if (p.verbose > 1)
402 X509_print_ex(p.errorbio,p.usercert,0,0);
404 /* determine URL to go */
406 if (hostporturl) {
407 serverurl=(char*) malloc(9+strlen(hostporturl));
408 sprintf(serverurl,"https://%s",hostporturl);
410 else if (p.accesstype != 0) { /* see whether we can find an AIA or SIA for a given access type */
411 if (!(serverurl = my_get_ext(p.usercert,p.accesstype,NID_info_access))) {
412 int j=0;
413 BIO_printf(p.errorbio,"no service URL in user cert "
414 "cherching in others certificats\n");
415 for (j=0;j<sk_X509_num(p.ca);j++) {
416 if ((serverurl = my_get_ext(sk_X509_value(p.ca,j),p.accesstype,
417 NID_info_access)))
418 break;
419 if ((serverurl = my_get_ext(sk_X509_value(p.ca,j),p.accesstype,
420 NID_sinfo_access)))
421 break;
426 if (!serverurl) {
427 BIO_printf(p.errorbio, "no service URL in certificats,"
428 " check '-accesstype (AD_DVCS | ad_timestamping)'"
429 " or use '-connect'\n");
430 goto err;
433 if (p.verbose)
434 BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
436 curl_easy_setopt(p.curl, CURLOPT_URL, serverurl);
438 /* Now specify the POST binary data */
440 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
441 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE,(long)tabLength);
443 /* pass our list of custom made headers */
445 contenttype=(char*) malloc(15+strlen(mimetype));
446 sprintf(contenttype,"Content-type: %s",mimetype);
447 headers = curl_slist_append(headers,contenttype);
448 curl_easy_setopt(p.curl, CURLOPT_HTTPHEADER, headers);
450 if (p.verbose)
451 BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
454 FILE *outfp;
455 BIO_get_fp(out,&outfp);
456 curl_easy_setopt(p.curl, CURLOPT_FILE,outfp);
459 res = curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun) ;
461 if (res != CURLE_OK)
462 BIO_printf(p.errorbio,"%d %s=%d %d\n", __LINE__, "CURLOPT_SSL_CTX_FUNCTION",CURLOPT_SSL_CTX_FUNCTION,res);
464 curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_DATA, &p);
467 int lu; int i=0;
468 while ((lu = BIO_read (in,&binaryptr[i],tabLength-i)) >0 ) {
469 i+=lu;
470 if (i== tabLength) {
471 tabLength+=100;
472 binaryptr=(char*)realloc(binaryptr,tabLength); /* should be more careful */
475 tabLength = i;
477 /* Now specify the POST binary data */
479 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
480 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE,(long)tabLength);
483 /* Perform the request, res will get the return code */
485 BIO_printf(p.errorbio,"%d %s %d\n", __LINE__, "curl_easy_perform",
486 res = curl_easy_perform(p.curl));
488 int result =curl_easy_getinfo(p.curl,CURLINFO_CONTENT_TYPE,&response);
489 if( mimetypeaccept && p.verbose)
490 if(!strcmp(mimetypeaccept,response))
491 BIO_printf(p.errorbio,"the response has a correct mimetype : %s\n",
492 response);
493 else
494 BIO_printf(p.errorbio,"the reponse doesn\'t has an acceptable "
495 "mime type, it is %s instead of %s\n",
496 response,mimetypeaccept);
499 /*** code d'erreur si accept mime ***, egalement code return HTTP != 200 ***/
501 /* free the header list*/
503 curl_slist_free_all(headers);
505 /* always cleanup */
506 curl_easy_cleanup(p.curl);
508 BIO_free(in);
509 BIO_free(out);
510 return (EXIT_SUCCESS);
512 err: BIO_printf(p.errorbio,"error");
513 exit(1);