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
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
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
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).
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",
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.
125 /* This is a context that we pass to all callbacks */
127 typedef struct sslctxparm_st {
128 unsigned char * p12file ;
133 STACK_OF(X509) * ca ;
141 /* some helper function. */
143 static char *i2s_ASN1_IA5STRING( ASN1_IA5STRING *ia5)
146 if(!ia5 || !ia5->length)
148 tmp = OPENSSL_malloc(ia5->length + 1);
149 memcpy(tmp, ia5->data, ia5->length);
150 tmp[ia5->length] = 0;
154 /* A conveniance routine to get an access URI. */
156 static unsigned char *my_get_ext(X509 * cert, const int type, int extensiontype) {
159 STACK_OF(ACCESS_DESCRIPTION) * accessinfo ;
160 accessinfo = X509_get_ext_d2i(cert, extensiontype, NULL, NULL) ;
162 if (!sk_ACCESS_DESCRIPTION_num(accessinfo))
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);
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;
188 BIO_printf(p->errorbio,"entering ssl_app_verify_callback\n");
190 if ((ok= X509_verify_cert(ctx)) && ctx->cert) {
191 unsigned char * accessinfo ;
193 X509_print_ex(p->errorbio,ctx->cert,0,0);
195 if (accessinfo = my_get_ext(ctx->cert,p->accesstype ,NID_sinfo_access)) {
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,
204 BIO_printf(p->errorbio,"Setting URL from AIA to: %s\n", accessinfo);
206 curl_easy_setopt(p->curl, CURLOPT_URL,accessinfo);
210 BIO_printf(p->errorbio,"leaving ssl_app_verify_callback with %d\n", 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(SSL_CTX_get_cert_store(ctx), sk_X509_value(p->ca, sk_X509_num(p->ca)-1));
244 SSL_CTX_set_verify_depth(ctx,2);
246 SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER,ZERO_NULL);
248 SSL_CTX_set_cert_verify_callback(ctx, ssl_app_verify_callback, parm);
253 ERR_print_errors(p->errorbio);
254 return CURLE_SSL_CERTPROBLEM;
258 int main(int argc, char **argv) {
263 char * outfile = NULL;
264 char * infile = NULL ;
269 char* mimetypeaccept=NULL;
272 unsigned char* hostporturl = NULL;
274 char **args = argv + 1;
275 unsigned char * serverurl;
280 struct curl_slist * headers=NULL;
283 binaryptr = malloc(tabLength);
286 p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE);
288 curl_global_init(CURL_GLOBAL_DEFAULT);
290 /* we need some more for the P12 decoding */
292 OpenSSL_add_all_ciphers();
293 OpenSSL_add_all_digests();
294 ERR_load_crypto_strings();
298 while (*args && *args[0] == '-') {
299 if (!strcmp (*args, "-in")) {
303 } else if (!strcmp (*args, "-out")) {
307 } else if (!strcmp (*args, "-p12")) {
309 p.p12file = *(++args);
311 } else if (strcmp(*args,"-envpass") == 0) {
313 p.pst = getenv(*(++args));
315 } else if (strcmp(*args,"-connect") == 0) {
317 hostporturl = *(++args);
319 } else if (strcmp(*args,"-mimetype") == 0) {
321 mimetype = *(++args);
323 } else if (strcmp(*args,"-acceptmime") == 0) {
325 mimetypeaccept = *(++args);
327 } else if (strcmp(*args,"-accesstype") == 0) {
329 if ((p.accesstype = OBJ_obj2nid(OBJ_txt2obj(*++args,0))) == 0) badarg=1;
331 } else if (strcmp(*args,"-verbose") == 0) {
337 if (mimetype==NULL || mimetypeaccept == NULL) badarg = 1;
340 for (pp=curlx_usage; (*pp != NULL); pp++)
341 BIO_printf(p.errorbio,"%s\n",*pp);
342 BIO_printf(p.errorbio,"\n");
350 if ((in=BIO_new(BIO_s_file())) == NULL) {
351 BIO_printf(p.errorbio, "Error setting input bio\n");
353 } else if (infile == NULL)
354 BIO_set_fp(in,stdin,BIO_NOCLOSE|BIO_FP_TEXT);
355 else if (BIO_read_filename(in,infile) <= 0) {
356 BIO_printf(p.errorbio, "Error opening input file %s\n", infile);
363 if ((out=BIO_new(BIO_s_file())) == NULL) {
364 BIO_printf(p.errorbio, "Error setting output bio.\n");
366 } else if (outfile == NULL)
367 BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
368 else if (BIO_write_filename(out,outfile) <= 0) {
369 BIO_printf(p.errorbio, "Error opening output file %s\n", outfile);
375 p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE);
377 if (!(p.curl = curl_easy_init())) {
378 BIO_printf(p.errorbio, "Cannot init curl lib\n");
384 if (!(p12bio = BIO_new_file(p.p12file , "rb"))) {
385 BIO_printf(p.errorbio, "Error opening P12 file %s\n", p.p12file); goto err;
387 if (!(p.p12 = d2i_PKCS12_bio (p12bio, NULL))) {
388 BIO_printf(p.errorbio, "Cannot decode P12 structure %s\n", p.p12file); goto err;
392 if (!(PKCS12_parse (p.p12, p.pst, &(p.pkey), &(p.usercert), &(p.ca) ) )) {
393 BIO_printf(p.errorbio,"Invalid P12 structure in %s\n", p.p12file); goto err;
396 if (sk_X509_num(p.ca) <= 0) {
397 BIO_printf(p.errorbio,"No trustworthy CA given.%s\n", p.p12file); goto err;
401 X509_print_ex(p.errorbio,p.usercert,0,0);
403 /* determine URL to go */
406 serverurl = malloc(9+strlen(hostporturl));
407 sprintf(serverurl,"https://%s",hostporturl);
409 else if (p.accesstype != 0) { /* see whether we can find an AIA or SIA for a given access type */
410 if (!(serverurl = my_get_ext(p.usercert,p.accesstype,NID_info_access))) {
412 BIO_printf(p.errorbio,"no service URL in user cert "
413 "cherching in others certificats\n");
414 for (j=0;j<sk_X509_num(p.ca);j++) {
415 if ((serverurl = my_get_ext(sk_X509_value(p.ca,j),p.accesstype,
418 if ((serverurl = my_get_ext(sk_X509_value(p.ca,j),p.accesstype,
426 BIO_printf(p.errorbio, "no service URL in certificats,"
427 " check '-accesstype (AD_DVCS | ad_timestamping)'"
428 " or use '-connect'\n");
433 BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
435 curl_easy_setopt(p.curl, CURLOPT_URL, serverurl);
437 /* Now specify the POST binary data */
439 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
440 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE,(long)tabLength);
442 /* pass our list of custom made headers */
444 contenttype = malloc(15+strlen(mimetype));
445 sprintf(contenttype,"Content-type: %s",mimetype);
446 headers = curl_slist_append(headers,contenttype);
447 curl_easy_setopt(p.curl, CURLOPT_HTTPHEADER, headers);
450 BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
454 BIO_get_fp(out,&outfp);
455 curl_easy_setopt(p.curl, CURLOPT_WRITEDATA, outfp);
458 res = curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun) ;
461 BIO_printf(p.errorbio,"%d %s=%d %d\n", __LINE__, "CURLOPT_SSL_CTX_FUNCTION",CURLOPT_SSL_CTX_FUNCTION,res);
463 curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_DATA, &p);
467 while ((lu = BIO_read (in,&binaryptr[i],tabLength-i)) >0 ) {
471 binaryptr=realloc(binaryptr,tabLength); /* should be more careful */
476 /* Now specify the POST binary data */
478 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
479 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE,(long)tabLength);
482 /* Perform the request, res will get the return code */
484 BIO_printf(p.errorbio,"%d %s %d\n", __LINE__, "curl_easy_perform",
485 res = curl_easy_perform(p.curl));
487 int result =curl_easy_getinfo(p.curl,CURLINFO_CONTENT_TYPE,&response);
488 if( mimetypeaccept && p.verbose)
489 if(!strcmp(mimetypeaccept,response))
490 BIO_printf(p.errorbio,"the response has a correct mimetype : %s\n",
493 BIO_printf(p.errorbio,"the reponse doesn\'t has an acceptable "
494 "mime type, it is %s instead of %s\n",
495 response,mimetypeaccept);
498 /*** code d'erreur si accept mime ***, egalement code return HTTP != 200 ***/
500 /* free the header list*/
502 curl_slist_free_all(headers);
505 curl_easy_cleanup(p.curl);
509 return (EXIT_SUCCESS);
511 err: BIO_printf(p.errorbio,"error");