doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
curlx.c
Go to the documentation of this file.
1 /*
2  curlx.c Authors: Peter Sylvester, Jean-Paul Merlin
3 
4  This is a little program to demonstrate the usage of
5 
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.
10 
11 */
12 
13 
14 /*
15  * Copyright (c) 2003 The OpenEvidence Project. All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
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.
24  *
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.
30  *
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)."
40  *
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.
45  *
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.
49  *
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)."
59  *
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  * ====================================================================
73  *
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).
79  *
80  */
81 
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <curl/curl.h>
85 #include <openssl/x509v3.h>
86 #include <openssl/x509_vfy.h>
87 #include <openssl/crypto.h>
88 #include <openssl/lhash.h>
89 #include <openssl/objects.h>
90 #include <openssl/err.h>
91 #include <openssl/evp.h>
92 #include <openssl/x509.h>
93 #include <openssl/pkcs12.h>
94 #include <openssl/bio.h>
95 #include <openssl/ssl.h>
96 
97 static char *curlx_usage[]={
98 "usage: curlx args\n",
99 " -p12 arg - tia file ",
100 " -envpass arg - environement variable which content the tia private key password",
101 " -out arg - output file (response)- default stdout",
102 " -in arg - input file (request)- default stdin",
103 " -connect arg - URL of the server for the connection ex: www.openevidenve.org",
104 " -mimetype arg - MIME type for data in ex : application/timestamp-query or application/dvcs -default application/timestamp-query",
105 " -acceptmime arg - MIME type acceptable for the response ex : application/timestamp-response or application/dvcs -default none",
106 " -accesstype arg - an Object identifier in an AIA/SIA method, e.g. AD_DVCS or ad_timestamping",
107 NULL
108 };
109 
110 /*
111 
112  ./curlx -p12 psy.p12 -envpass XX -in request -verbose -accesstype AD_DVCS
113  -mimetype application/dvcs -acceptmime application/dvcs -out response
114 
115 */
116 
117 /* This is a context that we pass to all callbacks */
118 
119 typedef struct sslctxparm_st {
120  unsigned char * p12file ;
121  const char * pst ;
122  PKCS12 * p12 ;
123  EVP_PKEY * pkey ;
124  X509 * usercert ;
125  STACK_OF(X509) * ca ;
126  CURL * curl;
127  BIO * errorbio;
128  int accesstype ;
129  int verbose;
130 
131 } sslctxparm;
132 
133 /* some helper function. */
134 
135 static char *i2s_ASN1_IA5STRING( ASN1_IA5STRING *ia5)
136 {
137  char *tmp;
138  if(!ia5 || !ia5->length) return NULL;
139  tmp = OPENSSL_malloc(ia5->length + 1);
140  memcpy(tmp, ia5->data, ia5->length);
141  tmp[ia5->length] = 0;
142  return tmp;
143 }
144 
145 /* A conveniance routine to get an access URI. */
146 
147 static unsigned char *my_get_ext(X509 * cert, const int type, int extensiontype) {
148 
149  int i;
150  STACK_OF(ACCESS_DESCRIPTION) * accessinfo ;
151  accessinfo = X509_get_ext_d2i(cert, extensiontype, NULL, NULL) ;
152 
153  if (!sk_ACCESS_DESCRIPTION_num(accessinfo)) return NULL;
154  for (i = 0; i < sk_ACCESS_DESCRIPTION_num(accessinfo); i++) {
155  ACCESS_DESCRIPTION * ad = sk_ACCESS_DESCRIPTION_value(accessinfo, i);
156  if (OBJ_obj2nid(ad->method) == type) {
157  if (ad->location->type == GEN_URI) {
158  return i2s_ASN1_IA5STRING(ad->location->d.ia5);
159  }
160  return NULL;
161  }
162  }
163  return NULL;
164 }
165 
166 /* This is an application verification call back, it does not
167  perform any addition verification but tries to find a URL
168  in the presented certificat. If found, this will become
169  the URL to be used in the POST.
170 */
171 
172 static int ssl_app_verify_callback(X509_STORE_CTX *ctx, void *arg) {
173  sslctxparm * p = (sslctxparm *) arg;
174  int ok;
175 
176  if (p->verbose > 2) BIO_printf(p->errorbio,"entering ssl_app_verify_callback\n");
177  if ((ok= X509_verify_cert(ctx)) && ctx->cert) {
178  unsigned char * accessinfo ;
179  if (p->verbose > 1) X509_print_ex(p->errorbio,ctx->cert,0,0);
180  if (accessinfo = my_get_ext(ctx->cert,p->accesstype ,NID_sinfo_access)) {
181  if (p->verbose) BIO_printf(p->errorbio,"Setting URL from SIA to: %s\n",accessinfo);
182  curl_easy_setopt(p->curl, CURLOPT_URL,accessinfo);
183  } else if (accessinfo = my_get_ext(ctx->cert,p->accesstype ,NID_info_access)) {
184  if (p->verbose) BIO_printf(p->errorbio,"Setting URL from AIA to: %s\n",accessinfo);
185  curl_easy_setopt(p->curl, CURLOPT_URL,accessinfo);
186  }
187  }
188  if (p->verbose > 2) BIO_printf(p->errorbio,"leaving ssl_app_verify_callback with %d\n",ok);
189  return(ok);
190 }
191 
192 
193 /* This is an example of an curl SSL initialisation call back. The callback sets:
194  - a private key and certificate
195  - a trusted ca certificate
196  - a preferred cipherlist
197  - an application verification callback (the function above)
198 */
199 
200 static CURLcode sslctxfun(CURL * curl, void * sslctx, void * parm) {
201 
202  sslctxparm * p = (sslctxparm *) parm;
203  SSL_CTX * ctx = (SSL_CTX *) sslctx ;
204 
205  if (!SSL_CTX_use_certificate(ctx,p->usercert)) {
206  BIO_printf(p->errorbio, "SSL_CTX_use_certificate problem\n"); goto err;
207  }
208  if (!SSL_CTX_use_PrivateKey(ctx,p->pkey)) {
209  BIO_printf(p->errorbio, "SSL_CTX_use_PrivateKey\n"); goto err;
210  }
211 
212  if (!SSL_CTX_check_private_key(ctx)) {
213  BIO_printf(p->errorbio, "SSL_CTX_check_private_key\n"); goto err;
214  }
215 
216  SSL_CTX_set_quiet_shutdown(ctx,1);
217  SSL_CTX_set_cipher_list(ctx,"RC4-MD5");
218  SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
219 
220  X509_STORE_add_cert(ctx->cert_store,sk_X509_value(p->ca,sk_X509_num(p->ca)-1));
221 
222  SSL_CTX_set_verify_depth(ctx,2);
223 
224  SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER,NULL);
225 
226  SSL_CTX_set_cert_verify_callback(ctx, ssl_app_verify_callback, parm);
227 
228 
229  return CURLE_OK ;
230 err:
231  ERR_print_errors(p->errorbio);
232  return CURLE_SSL_CERTPROBLEM;
233 
234 }
235 
236 int main(int argc, char **argv) {
237 
238  BIO* in=NULL;
239  BIO* out=NULL;
240 
241 
242  char * outfile = NULL;
243  char * infile = NULL ;
244 
245  int tabLength=100;
246  char *binaryptr;
247  char* mimetype;
248  char* mimetypeaccept=NULL;
249  char* contenttype;
250  char** pp;
251  unsigned char* hostporturl = NULL;
252  binaryptr=(char*)malloc(tabLength);
253  BIO * p12bio ;
254  char **args = argv + 1;
255  unsigned char * serverurl;
256  sslctxparm p;
257  char *response;
258  p.verbose = 0;
259 
260  CURLcode res;
261  struct curl_slist * headers=NULL;
262 
263  p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE);
264 
266 
267 /* we need some more for the P12 decoding */
268 
269  OpenSSL_add_all_ciphers();
270  OpenSSL_add_all_digests();
271  ERR_load_crypto_strings();
272 
273 
274  int badarg=0;
275 
276  while (*args && *args[0] == '-') {
277  if (!strcmp (*args, "-in")) {
278  if (args[1]) {
279  infile=*(++args);
280  } else badarg=1;
281  } else if (!strcmp (*args, "-out")) {
282  if (args[1]) {
283  outfile=*(++args);
284  } else badarg=1;
285  } else if (!strcmp (*args, "-p12")) {
286  if (args[1]) {
287  p.p12file = *(++args);
288  } else badarg=1;
289  } else if (strcmp(*args,"-envpass") == 0) {
290  if (args[1]) {
291  p.pst = getenv(*(++args));
292  } else badarg=1;
293  } else if (strcmp(*args,"-connect") == 0) {
294  if (args[1]) {
295  hostporturl = *(++args);
296  } else badarg=1;
297  } else if (strcmp(*args,"-mimetype") == 0) {
298  if (args[1]) {
299  mimetype = *(++args);
300  } else badarg=1;
301  } else if (strcmp(*args,"-acceptmime") == 0) {
302  if (args[1]) {
303  mimetypeaccept = *(++args);
304  } else badarg=1;
305  } else if (strcmp(*args,"-accesstype") == 0) {
306  if (args[1]) {
307  if ((p.accesstype = OBJ_obj2nid(OBJ_txt2obj(*++args,0))) == 0) badarg=1;
308  } else badarg=1;
309  } else if (strcmp(*args,"-verbose") == 0) {
310  p.verbose++;
311  } else badarg=1;
312  args++;
313  }
314 
315  if (mimetype==NULL || mimetypeaccept == NULL) badarg = 1;
316 
317  if (badarg) {
318  for (pp=curlx_usage; (*pp != NULL); pp++)
319  BIO_printf(p.errorbio,"%s\n",*pp);
320  BIO_printf(p.errorbio,"\n");
321  goto err;
322  }
323 
324 
325 
326  /* set input */
327 
328  if ((in=BIO_new(BIO_s_file())) == NULL) {
329  BIO_printf(p.errorbio, "Error setting input bio\n");
330  goto err;
331  } else if (infile == NULL)
332  BIO_set_fp(in,stdin,BIO_NOCLOSE|BIO_FP_TEXT);
333  else if (BIO_read_filename(in,infile) <= 0) {
334  BIO_printf(p.errorbio, "Error opening input file %s\n", infile);
335  BIO_free(in);
336  goto err;
337  }
338 
339  /* set output */
340 
341  if ((out=BIO_new(BIO_s_file())) == NULL) {
342  BIO_printf(p.errorbio, "Error setting output bio.\n");
343  goto err;
344  } else if (outfile == NULL)
345  BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
346  else if (BIO_write_filename(out,outfile) <= 0) {
347  BIO_printf(p.errorbio, "Error opening output file %s\n", outfile);
348  BIO_free(out);
349  goto err;
350  }
351 
352 
353  p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE);
354 
355  if (!(p.curl = curl_easy_init())) {
356  BIO_printf(p.errorbio, "Cannot init curl lib\n");
357  goto err;
358  }
359 
360 
361 
362  if (!(p12bio = BIO_new_file(p.p12file , "rb"))) {
363  BIO_printf(p.errorbio, "Error opening P12 file %s\n", p.p12file); goto err;
364  }
365  if (!(p.p12 = d2i_PKCS12_bio (p12bio, NULL))) {
366  BIO_printf(p.errorbio, "Cannot decode P12 structure %s\n", p.p12file); goto err;
367  }
368 
369  p.ca= NULL;
370  if (!(PKCS12_parse (p.p12, p.pst, &(p.pkey), &(p.usercert), &(p.ca) ) )) {
371  BIO_printf(p.errorbio,"Invalid P12 structure in %s\n", p.p12file); goto err;
372  }
373 
374  if (sk_X509_num(p.ca) <= 0) {
375  BIO_printf(p.errorbio,"No trustworthy CA given.%s\n", p.p12file); goto err;
376  }
377 
378  if (p.verbose > 1) X509_print_ex(p.errorbio,p.usercert,0,0);
379 
380  /* determine URL to go */
381 
382  if (hostporturl) {
383  serverurl=(char*) malloc(9+strlen(hostporturl));
384  sprintf(serverurl,"https://%s",hostporturl);
385  } else if (p.accesstype != 0) { /* see whether we can find an AIA or SIA for a given access type */
386  if (!(serverurl = my_get_ext(p.usercert,p.accesstype,NID_info_access))) {
387  BIO_printf(p.errorbio,"no service URL in user cert cherching in others certificats\n");
388  int j=0;
389  int find=0;
390  for (j=0;j<sk_X509_num(p.ca);j++) {
391  if ((serverurl = my_get_ext(sk_X509_value(p.ca,j),p.accesstype,NID_info_access))) break;
392  if ((serverurl = my_get_ext(sk_X509_value(p.ca,j),p.accesstype,NID_sinfo_access))) break;
393  }
394  }
395  }
396 
397  if (!serverurl) {
398  BIO_printf(p.errorbio, "no service URL in certificats, check '-accesstype (AD_DVCS | ad_timestamping)' or use '-connect'\n"); goto err;
399  }
400 
401  if (p.verbose) BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
402  curl_easy_setopt(p.curl, CURLOPT_URL, serverurl);
403 
404  /* Now specify the POST binary data */
405 
406  curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
407  curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE,tabLength);
408 
409  /* pass our list of custom made headers */
410 
411  contenttype=(char*) malloc(15+strlen(mimetype));
412  sprintf(contenttype,"Content-type: %s",mimetype);
413  headers = curl_slist_append(headers,contenttype);
414  curl_easy_setopt(p.curl, CURLOPT_HTTPHEADER, headers);
415 
416  if (p.verbose) BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
417 
418  {
419  FILE *outfp;
420  BIO_get_fp(out,&outfp);
421  curl_easy_setopt(p.curl, CURLOPT_FILE,outfp);
422  }
423 
424  res = curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun) ;
425 
426  if (res != CURLE_OK)
427  BIO_printf(p.errorbio,"%d %s=%d %d\n", __LINE__, "CURLOPT_SSL_CTX_FUNCTION",CURLOPT_SSL_CTX_FUNCTION,res);
428 
429  curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_DATA, &p);
430 
431  {
432  int lu; int i=0;
433  while ((lu = BIO_read (in,&binaryptr[i],tabLength-i)) >0 ) {
434  i+=lu;
435  if (i== tabLength) {
436  tabLength+=100;
437  binaryptr=(char*)realloc(binaryptr,tabLength); /* should be more careful */
438  }
439  }
440  tabLength = i;
441  }
442  /* Now specify the POST binary data */
443 
444  curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
445  curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE,tabLength);
446 
447 
448  /* Perform the request, res will get the return code */
449 
450  BIO_printf(p.errorbio,"%d %s %d\n", __LINE__, "curl_easy_perform", res = curl_easy_perform(p.curl));
451  {
452  int result =curl_easy_getinfo(p.curl,CURLINFO_CONTENT_TYPE,&response);
453  if( mimetypeaccept && p.verbose)
454  if(!strcmp(mimetypeaccept,response))
455  BIO_printf(p.errorbio,"the response has a correct mimetype : %s\n",response);
456  else
457  BIO_printf(p.errorbio,"the reponse doesn\'t has an acceptable mime type, it is %s instead of %s\n",response,mimetypeaccept);
458  }
459 
460  /*** code d'erreur si accept mime ***, egalement code return HTTP != 200 ***/
461 
462 /* free the header list*/
463 
464  curl_slist_free_all(headers);
465 
466  /* always cleanup */
467  curl_easy_cleanup(p.curl);
468 
469  BIO_free(in);
470  BIO_free(out);
471  return (EXIT_SUCCESS);
472 
473 
474  err: BIO_printf(p.errorbio,"error");
475  exit(1);
476 }
477 
478 
479 
480 
CURLcode curl_global_init(long flags)
Globally initializes cURL given a bitwise set of the different features to initialize.
Definition: easy.c:147
PKCS12 * p12
Definition: curlx.c:122
#define strcmp
Definition: Str.h:41
CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info,...)
Definition: easy.c:299
EVP_PKEY * pkey
Definition: curlx.c:123
CURLcode curl_easy_perform(CURL *curl)
Definition: easy.c:260
CURLcode
Definition: curl.h:209
GLuint GLuint GLsizei GLenum type
Definition: glext.h:2845
CURLcode curl_easy_setopt(CURL *curl, CURLoption option,...)
Definition: easy.c:217
int i
Definition: process.py:33
Boolean result
#define NULL
Definition: Lib.h:88
#define CURL_GLOBAL_DEFAULT
Definition: curl.h:1155
unsigned char * p12file
Definition: curlx.c:120
Definition: curl.h:210
int main(int argc, char **argv)
Definition: curlx.c:236
static WindowRef ValidModeCallbackProc inCallback OSStatus err
const char * pst
Definition: curlx.c:121
GLuint in
Definition: glext.h:5388
struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: sendf.c:82
X509 * usercert
Definition: curlx.c:124
void CURL
Definition: types.h:25
void curl_slist_free_all(struct curl_slist *)
Definition: sendf.c:108
GLuint res
Definition: glext.h:5385
GLint j
Definition: qgl.h:264
GLfloat GLfloat p
Definition: glext.h:4674
void curl_easy_cleanup(CURL *curl)
Definition: easy.c:288
int sprintf(idStr &string, const char *fmt,...)
Definition: Str.cpp:1528
CURL * curl_easy_init(void)
Definition: easy.c:195
struct sslctxparm_st sslctxparm