#ifndef LIBCURL_CURLX_HPP
#define LIBCURL_CURLX_HPP
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"curl/curl.h"
#include"openssl/x509v3.h"
#include"openssl/x509_vfy.h"
#include"openssl/crypto.h"
#include"openssl/lhash.h"
#include"openssl/objects.h"
#include"openssl/err.h"
#include"openssl/evp.h"
#include"openssl/x509.h"
#include"openssl/pkcs12.h"
#include"openssl/bio.h"
#include"openssl/ssl.h"
static const char *curlx_usage[]={
"usage: curlx args\n",
" -p12 arg - tia file ",
" -envpass arg - environment variable which content the tia private"
" key password",
" -out arg - output file (response)- default stdout",
" -in arg - input file (request)- default stdin",
" -connect arg - URL of the server for the connection ex:"
" www.openevidence.org",
" -mimetype arg - MIME type for data in ex : application/timestamp-query"
" or application/dvcs -default application/timestamp-query",
" -acceptmime arg - MIME type acceptable for the response ex : "
"application/timestamp-response or application/dvcs -default none",
" -accesstype arg - an Object identifier in an AIA/SIA method, e.g."
" AD_DVCS or ad_timestamping",
NULL
};
#define ZERO_NULL 0
//this is a context that we pass to all callbacks
typedef struct sslctxparm_st{
unsigned char *p12file;
const char *pst;
PKCS12 *p12;
EVP_PKEY *pKey;
X509 *pUsercert;
STACK_OF(X509) *pCa;
CURL *pCurl;
BIO *pErrorbio;
int accesstype;
int verbose;
}sslctxparm;
//some helper function
static char *is5string(ASN1_IA5STRING *ia5){
char *pTmp;
if(!ia5 || !ia5->length)
return NULL;
pTmp =(char *)OPENSSL_malloc(ia5->length +1);
memcpy(pTmp,ia5->data,ia5->length);
pTmp[ia5->length] = 0;
return pTmp;
}
//A convenience routine to get an access URI.
static unsigned char *my_get_ext(X509 *cert,const int type,int extensiontype){
int i;
STACK_OF(ACCESS_DESCRIPTION) *accessinfo;
accessinfo = (STACK_OF(ACCESS_DESCRIPTION) *)X509_get_ext_d2i(cert,extensiontype,NULL,NULL);
if(!sk_ACCESS_DESCRIPTION_num(accessinfo))
return NULL;
for(i=0;i<sk_ACCESS_DESCRIPTION_num(accessinfo);i++){
ACCESS_DESCRIPTION * ad = sk_ACCESS_DESCRIPTION_value(accessinfo,i);
if(ad->location->type == GEN_URI){
return (unsigned char*)is5string(ad->location->d.ia5);
}
return NULL;
}
return NULL;
}
//this is an application verification callback it does not perform any addition verification but tries to find a URL in the presented certificated. IF found, this wille becom the URL to be
//used in the POST
static int ssl_app_verify_callback(X509_STORE_CTX *ctx,void *arg){
sslctxparm *pSslctxparm = (sslctxparm *)arg;
int ok;
if(pSslctxparm->verbose >2){
BIO_printf(pSslctxparm->pErrorbio,"Entering ssl_app_verify_callback\n");
}
ok = X509_verify_cert(ctx);
if(ok && ctx->cert){
unsigned char *accessinfo;
if(pSslctxparm->verbose > 1)
X509_print_ex(pSslctxparm->pErrorbio,ctx->cert,0,0);
accessinfo = my_get_ext(ctx->cert,pSslctxparm->accesstype,NID_sinfo_access);
if(accessinfo){
if(pSslctxparm->verbose)
BIO_Printf(pSslctxparm->pErrorbio,ctx->cert,0,0);
accessinfo = my_get_ext(ctx->cert,pSslctxparm->accesstype,NID_sinfo_access);
}
if(accessinfo){
if(pSslctxparm->verbose)
BIO_printf(pSslctxparm->pErrorbio,"Setting URL from SIA to :%s\n",accessinfo);
curl_easy_setopt(pSslctxparm->pCurl,CURLOPT_URL,accessinfo);
}else if(accessinfo == my_get_ext(ctx->cert,pSslctxparm->accesstype,NID_info_access)){
if(pSslctxparm->verbose)
BIO_printf(pSslctxparm->pErrorbio,"Setting URL from AIA to :%s\n",accessinfo);
curl_easy_setopt(pSslctxparm->pCurl,CURLOPT_URL,accessinfo);
}
}
if(pSslctxparm->verbose > 2)
BIO_printf(pSslctxparm->pErrorbio,"leaving ssl_app_verify_callback with %d\n",ok);
return ok;
}
/**The SSL initialisation callback. The callback sets:
*a private key and cerificate
* a trusted ca cerificate
* a preferred cipherlist
* an application verification callback (the function above)
**/
static CURLcode sslctxfun(CURL *curl,void *sslctx,void *parm){
sslctxparm *p = (sslctxparm*)parm;
SSL_CTX *pCtx = (SSL_CTX*)sslctx;
if(!SSL_CTX_use_certificate(pCtx,p->pUsercert)){
BIO_printf(p->pErrorbio,"SSL_CTX_use_certificate problem\n");
goto err;
}
if(!SSL_CTX_use_PrivateKey(pCtx,p->pKey)){
BIO_printf(p->pErrorbio,"SSL_CTX_use_PrivateKey\n");
goto err;
}
if(!SSL_CTX_check_private_key(pCtx)){
BIO_printf(p->pErrorbio,"SSL_CTX_check_private_key\n");
goto err;
}
SSL_CTX_set_quiet_shutdown(pCtx,1);
SSL_CTX_set_cipher_list(pCtx,"RC4-MD5");
SSL_CTX_set_mode(pCtx,SSL_MODE_AUTO_RETRY);
X509_STORE_add_cert(SSL_CTX_get_cert_store(pCtx),sk_X509_value(p->pCa,sk_X509_num(p->pCa)-1));
SSL_CTX_set_verify_depth(pCtx,2);
SSL_CTX_set_verify(pCtx,SSL_VERIFY_PEER,ZERO_NULL);
SSL_CTX_set_cert_verify_callback(pCtx,ssl_app_verify_callback,parm);
return CURLE_OK;
err:
ERR_print_errors(p->pErrorbio);
return CURLE_SSL_CERTPROBLEM;
}
int test_curlx(int argc,char**argv){
BIO * in = NULL;
BIO *out = NULL;
char *outfile = NULL;
char *infile = NULL;
int tabLength = 100;
char *binaryptr;
char *mimetype = NULL;
char *mimetypeaccept = NULL;
char *contenttype;
const char **pp;
unsigned char *hostporturl = NULL;
BIO *p12bio;
char **args = argv+1;
unsigned char *serverurl;
sslctxparm p;
char *response;
CURLcode res;
struct curl_slist *pHeaders = NULL;
int badarg = 0;
binaryptr = (char*)malloc(tabLength);
memset(&p,'\0',sizeof(p));
p.pErrorbio = BIO_new_fp(stderr,BIO_NOCLOSE);
curl_global_init(CURL_GLOBAL_DEFAULT);
//we need some more for the P12 decoding
OpenSSL_add_all_ciphers();
OpenSSL_add_all_digests();
ERR_load_crypto_strings();
while (*args && *args[0] == '-') {
if(!strcmp (*args,"-in")){
if(args[1]){
infile = *(++args);
}else
badarg = 1;
}else if(!strcmp(*args,"-out")){
if(args[1]){
outfile = *(++args);
}else
badarg = 1;
}else if(strcmp(*args,"-envpass") == 0){
if(args[1]){
p.pst = getenv(*(++args));
}else
badarg = 1;
}else if(strcmp(*args,"-connect") == 0){
if(args[1]){
hostporturl = (unsigned char*)(++args);
}else
badarg =1;
}else if(strcmp(*args,"-accpetmime") == 0){
if(args[1]){
mimetypeaccept = *(++args);
}else
badarg = 1;
}else if(strcmp(*args,"-accesstype") == 0){
if(args[1]){
p.accesstype = OBJ_obj2nid(OBJ_txt2obj(*++args,0));
if(0 == p.accesstype)
badarg = 1;
}else
badarg = 1;
}else if(strcmp(*args,"-verbose") == 0){
p.verbose++;
}else
badarg = 1;
args++;
}
if(mimetype == NULL||mimetypeaccept == NULL||p.p12file ==NULL)
badarg = 1;
if(badarg){
for(pp = curlx_usage;(*pp!=NULL);pp++)
BIO_printf(p.pErrorbio,"%s\n",*pp);
BIO_printf(p.pErrorbio,"\n");
goto err;
}
//set input
in = BIO_new(BIO_s_file());
if(NULL == in){
BIO_printf(p.pErrorbio,"Error setting input bio\n");
goto err;
}else if(NULL == infile){
BIO_set_fp(in,stdin,BIO_NOCLOSE|BIO_FP_TEXT);
}else if(BIO_read_filename(in,infile) <=0){
BIO_printf(p.pErrorbio,"Error opening input file %s\n",infile);
BIO_free(in);
goto err;
}
//set ouput
out = BIO_new(BIO_s_file());
if(NULL == out){
BIO_printf(p.pErrorbio,"Error setting output bio.\n");
goto err;
}else if(NULL == outfile){
BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
}else if(BIO_write_filename(out,outfile) <= 0){
BIO_printf(p.pErrorbio,"Error opening output file %s\n",outfile);
BIO_free(out);
goto err;
}
p.pErrorbio = BIO_new_fp(stderr,BIO_NOCLOSE);
p.pCurl = curl_easy_init();
if(!p.pCurl){
BIO_printf(p.pErrorbio,"Cannot init curl lib\n");
goto err;
}
p12bio = BIO_new_file(p.p12file,"rb");
if(!p12bio){
BIO_printf(p.pErrorbio,"Error opening P12 file %s\n",p.p12file);
goto err;
}
p.p12 = d2i_PKCS12_bio(p12bio,NULL);
if(!p.p12){
BIO_printf(p.pErrorbio,"Can not decode P12 structure %s\n",p.p12file);
goto err;
}
p.pCa = NULL;
if(!(PKCS12_parse(p.p12,p.pst,&p.pKey,&(p.pUsercert),&(p.pCa))){
BIO_printf(p.pErrorbio,"Invalid P12 structuire in %s\n",p.p12file);
goto err;
}
if(sk_X509_num(p.pCa) <=0){
BIO_printf(p.pErrorbio,"No treustworthy CA given.%s\n",p.p12file);
goto err;
}
if(p.verbose > 1){
X509_print_ex(p.pErrorbio,p.pUsercert,0,0);
}
//determine URL to go
if(hostporturl){
size_t len = strlen(hostporturl)+9;
serverurl = (unsigned char*)malloc(len);
sprintf(serverurl,len,"https://%s",hostporturl);
}else if(p.accesstype != 0){ //see whether we can find an AI or SIA for a given access type
serverurl = my_get_ext(p.pUsercert,p.accesstype,NID_info_access);
if(!serverurl){
int j = 0;
BIO_printf(p.pErrorbio,"no service URL in user cert "
"searching in others ertificates\n");
for(j=0;j<sk_X509_num(p.pCa);j++){
serverurl = my_get_ext(sk_X509_value(p.pCa,j),p.accesstype,NID_info_access);
if(serverurl)
break;
serverurl = my_get_ext(sk_X509_value(p.pCa,j),p.accesstype,NID_sinfo_access);
if(serverurl)
break;
}
}
}
if(!serverurl){
BIO_printf(p.pErrorbio,"no service URL in certificates,"
"check '-accesstype (AD_DVCS |ad_timestamping)'"
"or use '-connect'\n");
goto err;
}
if(p.verbose)
BIO_printf(p.pErrorbio,"Service URL:<%s>",serverurl);
curl_easy_setopt(p.pCurl,CURLOPT_URL,serverurl);
//Now specify the POST binary data
curl_easy_setopt(p.pCurl,CURLOPT_POSTFIELDS,binaryptr);
curl_easy_setopt(p.pCurl,CURLOPT_COPYPOSTFIELDS,(long)tabLength);
//pass out list of custom made headers
contenttype = (char*)malloc(15+strlen(mimetype));
snprintf(contenttype,15+strlen(mimetype),"Content-type:%s",mimetype);
pHeaders = curl_slist_append(pHeaders,contenttype);
curl_easy_setopt(p.pCurl,CURLOPT_HTTPHEADER,pHeaders);
if(p.verbose)
BIO_printf(p.pErrorbio,"Service URL:<%s>\n",serverurl);
{
FILE *outfp;
BIO_get_fp(out,&outfp);
curl_easy_setopt(p.pCurl,CURLOPT_WRITEDATA,outfp);
}
res = curl_easy_setopt(p.pCurl,CURLOPT_SSL_CTX_FUNCTION,sslctxfun);
if(res !=CURLE_OK)
BIO_printf(p.pErrorbio,"%d %s=%d %d\n",513,"CURLOPT_SSL_CTX_FUNCTION",CURLOPT_SSL_CTX_FUNCTION,res);
curl_easy_setopt(p.pCurl,CURLOPT_SSL_CTX_DATA,&p);
{
char *ptr;
int lu;
int i = 0;
while ((lu =BIO_read(in,&binaryptr[i],tabLength-i)) > 0) {
i +=lu;
if(i == tabLength){
tabLength += 100;
ptr = (char*)realloc(binaryptr,tabLength); //should be more careful
if(!ptr){
//out of memory
BIO_printf(p.pErrorbio,"out of memory (realloc returned NULL)\n");
goto fail;
}
binaryptr = ptr;
ptr = NULL;
}
}
tabLength = i;
}
//Now specify the post binary data
curl_easy_setopt(p.pCurl,CURLOPT_POSTFIELDS,binaryptr);
curl_easy_setopt(p.pCurl,CURLOPT_POSTFIELDSIZE,(long)tabLength);
//perform the request ,res will get the return code
BIO_printf(p.pErrorbio,"%d %s %d\n",545,"curl_easy_perform",res = curl_easy_perform(p.pCurl));
{
curl_easy_getinfo(p.pCurl,CURLINFO_CONTENT_TYPE,&response);
if(mimetypeaccept && p.verbose){
if(!strcmp(mimetypeaccept,response))
BIO_printf(p.pErrorbio,"the response has a correct mimetype :%s\n",response);
else
BIO_printf(p.pErrorbio,"the response doesn\'t has acceptable "
"mime type, it is %s instead of %s\n",response,mimetypeaccept);
}
}
//code d'erreur si accept mime ***, egalement code return HTTP != 200
//free the header list
fail:
curl_slist_free_all(pHeaders);
curl_easy_cleanup(p.pCurl);
BIO_free(in);
BIO_free(out);
return EXIT_SUCCESS;
err:
BIO_printf(p.pErrorbio,"error");
exit(1);
}
#endif // LIBCURL_CURLX_HPP
代碼編輯過程中存在多處錯誤,以及數據結構不完整等現象