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>
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",
125 STACK_OF(X509) * ca ;
135 static char *i2s_ASN1_IA5STRING( ASN1_IA5STRING *ia5)
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;
147 static unsigned char *my_get_ext(X509 * cert,
const int type,
int extensiontype) {
150 STACK_OF(ACCESS_DESCRIPTION) * accessinfo ;
151 accessinfo = X509_get_ext_d2i(cert, extensiontype,
NULL,
NULL) ;
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);
172 static int ssl_app_verify_callback(X509_STORE_CTX *ctx,
void *arg) {
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);
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);
188 if (p->verbose > 2) BIO_printf(p->errorbio,
"leaving ssl_app_verify_callback with %d\n",ok);
200 static CURLcode sslctxfun(
CURL * curl,
void * sslctx,
void * parm) {
203 SSL_CTX * ctx = (SSL_CTX *) sslctx ;
205 if (!SSL_CTX_use_certificate(ctx,p->
usercert)) {
206 BIO_printf(p->errorbio,
"SSL_CTX_use_certificate problem\n");
goto err;
208 if (!SSL_CTX_use_PrivateKey(ctx,p->
pkey)) {
209 BIO_printf(p->errorbio,
"SSL_CTX_use_PrivateKey\n");
goto err;
212 if (!SSL_CTX_check_private_key(ctx)) {
213 BIO_printf(p->errorbio,
"SSL_CTX_check_private_key\n");
goto err;
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);
220 X509_STORE_add_cert(ctx->cert_store,sk_X509_value(p->ca,sk_X509_num(p->ca)-1));
222 SSL_CTX_set_verify_depth(ctx,2);
224 SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER,
NULL);
226 SSL_CTX_set_cert_verify_callback(ctx, ssl_app_verify_callback, parm);
231 ERR_print_errors(p->errorbio);
236 int main(
int argc,
char **argv) {
242 char * outfile =
NULL;
243 char * infile =
NULL ;
248 char* mimetypeaccept=
NULL;
251 unsigned char* hostporturl =
NULL;
252 binaryptr=(
char*)malloc(tabLength);
254 char **args = argv + 1;
255 unsigned char * serverurl;
263 p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE);
269 OpenSSL_add_all_ciphers();
270 OpenSSL_add_all_digests();
271 ERR_load_crypto_strings();
276 while (*args && *args[0] ==
'-') {
277 if (!
strcmp (*args,
"-in")) {
281 }
else if (!
strcmp (*args,
"-out")) {
285 }
else if (!
strcmp (*args,
"-p12")) {
289 }
else if (
strcmp(*args,
"-envpass") == 0) {
291 p.
pst = getenv(*(++args));
293 }
else if (
strcmp(*args,
"-connect") == 0) {
295 hostporturl = *(++args);
297 }
else if (
strcmp(*args,
"-mimetype") == 0) {
299 mimetype = *(++args);
301 }
else if (
strcmp(*args,
"-acceptmime") == 0) {
303 mimetypeaccept = *(++args);
305 }
else if (
strcmp(*args,
"-accesstype") == 0) {
307 if ((p.accesstype = OBJ_obj2nid(OBJ_txt2obj(*++args,0))) == 0) badarg=1;
309 }
else if (
strcmp(*args,
"-verbose") == 0) {
315 if (mimetype==
NULL || mimetypeaccept ==
NULL) badarg = 1;
318 for (pp=curlx_usage; (*pp !=
NULL); pp++)
319 BIO_printf(p.errorbio,
"%s\n",*pp);
320 BIO_printf(p.errorbio,
"\n");
328 if ((in=BIO_new(BIO_s_file())) ==
NULL) {
329 BIO_printf(p.errorbio,
"Error setting input bio\n");
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);
341 if ((out=BIO_new(BIO_s_file())) ==
NULL) {
342 BIO_printf(p.errorbio,
"Error setting output bio.\n");
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);
353 p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE);
356 BIO_printf(p.errorbio,
"Cannot init curl lib\n");
362 if (!(p12bio = BIO_new_file(p.
p12file ,
"rb"))) {
363 BIO_printf(p.errorbio,
"Error opening P12 file %s\n", p.
p12file);
goto err;
365 if (!(p.
p12 = d2i_PKCS12_bio (p12bio,
NULL))) {
366 BIO_printf(p.errorbio,
"Cannot decode P12 structure %s\n", p.
p12file);
goto err;
371 BIO_printf(p.errorbio,
"Invalid P12 structure in %s\n", p.
p12file);
goto err;
374 if (sk_X509_num(p.ca) <= 0) {
375 BIO_printf(p.errorbio,
"No trustworthy CA given.%s\n", p.
p12file);
goto err;
378 if (p.verbose > 1) X509_print_ex(p.errorbio,p.
usercert,0,0);
383 serverurl=(
char*) malloc(9+strlen(hostporturl));
384 sprintf(serverurl,
"https://%s",hostporturl);
385 }
else if (p.accesstype != 0) {
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");
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;
398 BIO_printf(p.errorbio,
"no service URL in certificats, check '-accesstype (AD_DVCS | ad_timestamping)' or use '-connect'\n");
goto err;
401 if (p.verbose) BIO_printf(p.errorbio,
"Service URL: <%s>\n", serverurl);
411 contenttype=(
char*) malloc(15+strlen(mimetype));
412 sprintf(contenttype,
"Content-type: %s",mimetype);
416 if (p.verbose) BIO_printf(p.errorbio,
"Service URL: <%s>\n", serverurl);
420 BIO_get_fp(out,&outfp);
427 BIO_printf(p.errorbio,
"%d %s=%d %d\n", __LINE__,
"CURLOPT_SSL_CTX_FUNCTION",CURLOPT_SSL_CTX_FUNCTION,res);
433 while ((lu = BIO_read (in,&binaryptr[i],tabLength-i)) >0 ) {
437 binaryptr=(
char*)realloc(binaryptr,tabLength);
450 BIO_printf(p.errorbio,
"%d %s %d\n", __LINE__,
"curl_easy_perform", res =
curl_easy_perform(p.curl));
453 if( mimetypeaccept && p.verbose)
454 if(!
strcmp(mimetypeaccept,response))
455 BIO_printf(p.errorbio,
"the response has a correct mimetype : %s\n",response);
457 BIO_printf(p.errorbio,
"the reponse doesn\'t has an acceptable mime type, it is %s instead of %s\n",response,mimetypeaccept);
471 return (EXIT_SUCCESS);
474 err: BIO_printf(p.errorbio,
"error");
CURLcode curl_global_init(long flags)
Globally initializes cURL given a bitwise set of the different features to initialize.
CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info,...)
CURLcode curl_easy_perform(CURL *curl)
GLuint GLuint GLsizei GLenum type
CURLcode curl_easy_setopt(CURL *curl, CURLoption option,...)
#define CURL_GLOBAL_DEFAULT
int main(int argc, char **argv)
static WindowRef ValidModeCallbackProc inCallback OSStatus err
struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
void curl_slist_free_all(struct curl_slist *)
void curl_easy_cleanup(CURL *curl)
int sprintf(idStr &string, const char *fmt,...)
CURL * curl_easy_init(void)
struct sslctxparm_st sslctxparm