2004-10-06 03:50:18 -04:00
|
|
|
/*
|
2003-10-03 09:46:27 -04:00
|
|
|
curlx.c Authors: Peter Sylvester, Jean-Paul Merlin
|
|
|
|
|
|
|
|
This is a little program to demonstrate the usage of
|
|
|
|
|
|
|
|
- an ssl initialisation callback setting a user key and trustbases
|
2004-11-22 08:48:35 -05:00
|
|
|
coming from a pkcs12 file
|
2003-10-03 09:46:27 -04:00
|
|
|
- using an ssl application callback to find a URI in the
|
2004-11-22 08:48:35 -05:00
|
|
|
certificate presented during ssl session establishment.
|
2003-10-03 09:46:27 -04:00
|
|
|
|
|
|
|
*/
|
2015-06-18 05:38:54 -04:00
|
|
|
/* <DESC>
|
|
|
|
* demonstrates use of SSL context callback, requires OpenSSL
|
|
|
|
* </DESC>
|
|
|
|
*/
|
2003-10-03 09:46:27 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2003 The OpenEvidence Project. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions, the following disclaimer,
|
2004-10-06 03:50:18 -04:00
|
|
|
* and the original OpenSSL and SSLeay Licences below.
|
2003-10-03 09:46:27 -04:00
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
2004-10-06 03:50:18 -04:00
|
|
|
* notice, this list of conditions, the following disclaimer
|
2003-10-03 09:46:27 -04:00
|
|
|
* and the original OpenSSL and SSLeay Licences below in
|
|
|
|
* the documentation and/or other materials provided with the
|
2004-10-06 03:50:18 -04:00
|
|
|
* distribution.
|
2003-10-03 09:46:27 -04:00
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgments:
|
|
|
|
* "This product includes software developed by the Openevidence Project
|
|
|
|
* for use in the OpenEvidence Toolkit. (http://www.openevidence.org/)"
|
|
|
|
* This product includes software developed by the OpenSSL Project
|
2016-02-02 22:16:52 -05:00
|
|
|
* for use in the OpenSSL Toolkit (https://www.openssl.org/)"
|
2003-10-03 09:46:27 -04:00
|
|
|
* This product includes cryptographic software written by Eric Young
|
|
|
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
* Hudson (tjh@cryptsoft.com)."
|
|
|
|
*
|
|
|
|
* 4. The names "OpenEvidence Toolkit" and "OpenEvidence Project" must not be
|
|
|
|
* used to endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please contact
|
|
|
|
* openevidence-core@openevidence.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "OpenEvidence"
|
|
|
|
* nor may "OpenEvidence" appear in their names without prior written
|
|
|
|
* permission of the OpenEvidence Project.
|
|
|
|
*
|
|
|
|
* 6. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgments:
|
|
|
|
* "This product includes software developed by the OpenEvidence Project
|
|
|
|
* for use in the OpenEvidence Toolkit (http://www.openevidence.org/)
|
|
|
|
* This product includes software developed by the OpenSSL Project
|
2016-02-02 22:16:52 -05:00
|
|
|
* for use in the OpenSSL Toolkit (https://www.openssl.org/)"
|
2003-10-03 09:46:27 -04:00
|
|
|
* This product includes cryptographic software written by Eric Young
|
|
|
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
* Hudson (tjh@cryptsoft.com)."
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE OpenEvidence PROJECT ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenEvidence PROJECT OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This product includes software developed by the OpenSSL Project
|
2016-02-02 22:16:52 -05:00
|
|
|
* for use in the OpenSSL Toolkit (https://www.openssl.org/)
|
2003-10-03 09:46:27 -04:00
|
|
|
* This product includes cryptographic software written by Eric Young
|
|
|
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
* Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2007-07-12 17:11:10 -04:00
|
|
|
#include <string.h>
|
2003-10-03 09:46:27 -04:00
|
|
|
#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>
|
|
|
|
|
2007-07-12 17:11:10 -04:00
|
|
|
static const char *curlx_usage[]={
|
2004-11-22 08:48:35 -05:00
|
|
|
"usage: curlx args\n",
|
|
|
|
" -p12 arg - tia file ",
|
2017-03-26 11:02:22 -04:00
|
|
|
" -envpass arg - environment variable which content the tia private"
|
2016-02-11 03:42:38 -05:00
|
|
|
" key password",
|
2004-11-22 08:48:35 -05:00
|
|
|
" -out arg - output file (response)- default stdout",
|
|
|
|
" -in arg - input file (request)- default stdin",
|
2016-02-11 03:42:38 -05:00
|
|
|
" -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",
|
2004-11-22 08:48:35 -05:00
|
|
|
NULL
|
2003-10-03 09:46:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
2004-11-22 08:48:35 -05:00
|
|
|
./curlx -p12 psy.p12 -envpass XX -in request -verbose -accesstype AD_DVCS
|
|
|
|
-mimetype application/dvcs -acceptmime application/dvcs -out response
|
2003-10-03 09:46:27 -04:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-02-14 14:40:18 -05:00
|
|
|
/*
|
2006-10-29 16:19:23 -05:00
|
|
|
* We use this ZERO_NULL to avoid picky compiler warnings,
|
|
|
|
* when assigning a NULL pointer to a function pointer var.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ZERO_NULL 0
|
|
|
|
|
2003-10-03 09:46:27 -04:00
|
|
|
/* This is a context that we pass to all callbacks */
|
|
|
|
|
|
|
|
typedef struct sslctxparm_st {
|
2016-11-23 01:53:24 -05:00
|
|
|
unsigned char *p12file;
|
|
|
|
const char *pst;
|
|
|
|
PKCS12 *p12;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
X509 *usercert;
|
2016-02-11 03:42:38 -05:00
|
|
|
STACK_OF(X509) * ca;
|
2016-11-23 01:53:24 -05:00
|
|
|
CURL *curl;
|
|
|
|
BIO *errorbio;
|
2016-02-11 03:42:38 -05:00
|
|
|
int accesstype;
|
2004-11-22 08:48:35 -05:00
|
|
|
int verbose;
|
2003-10-03 09:46:27 -04:00
|
|
|
|
|
|
|
} sslctxparm;
|
|
|
|
|
|
|
|
/* some helper function. */
|
|
|
|
|
2016-02-17 08:51:31 -05:00
|
|
|
static char *ia5string(ASN1_IA5STRING *ia5)
|
2003-10-03 09:46:27 -04:00
|
|
|
{
|
2004-11-22 08:48:35 -05:00
|
|
|
char *tmp;
|
|
|
|
if(!ia5 || !ia5->length)
|
|
|
|
return NULL;
|
|
|
|
tmp = OPENSSL_malloc(ia5->length + 1);
|
|
|
|
memcpy(tmp, ia5->data, ia5->length);
|
|
|
|
tmp[ia5->length] = 0;
|
|
|
|
return tmp;
|
2003-10-03 09:46:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* A conveniance routine to get an access URI. */
|
2016-02-17 08:51:31 -05:00
|
|
|
static unsigned char *my_get_ext(X509 *cert, const int type,
|
2016-02-11 03:42:38 -05:00
|
|
|
int extensiontype)
|
|
|
|
{
|
2004-11-22 08:48:35 -05:00
|
|
|
int i;
|
2016-02-11 03:42:38 -05:00
|
|
|
STACK_OF(ACCESS_DESCRIPTION) * accessinfo;
|
|
|
|
accessinfo = X509_get_ext_d2i(cert, extensiontype, NULL, NULL);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(!sk_ACCESS_DESCRIPTION_num(accessinfo))
|
2004-11-22 08:48:35 -05:00
|
|
|
return NULL;
|
2016-02-11 03:42:38 -05:00
|
|
|
for(i = 0; i < sk_ACCESS_DESCRIPTION_num(accessinfo); i++) {
|
2004-11-22 08:48:35 -05:00
|
|
|
ACCESS_DESCRIPTION * ad = sk_ACCESS_DESCRIPTION_value(accessinfo, i);
|
2016-02-11 03:42:38 -05:00
|
|
|
if(OBJ_obj2nid(ad->method) == type) {
|
|
|
|
if(ad->location->type == GEN_URI) {
|
2016-02-17 08:51:31 -05:00
|
|
|
return ia5string(ad->location->d.ia5);
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2003-10-03 09:46:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is an application verification call back, it does not
|
|
|
|
perform any addition verification but tries to find a URL
|
|
|
|
in the presented certificat. If found, this will become
|
|
|
|
the URL to be used in the POST.
|
|
|
|
*/
|
|
|
|
|
2004-11-22 08:48:35 -05:00
|
|
|
static int ssl_app_verify_callback(X509_STORE_CTX *ctx, void *arg)
|
|
|
|
{
|
|
|
|
sslctxparm * p = (sslctxparm *) arg;
|
|
|
|
int ok;
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(p->verbose > 2)
|
|
|
|
BIO_printf(p->errorbio, "entering ssl_app_verify_callback\n");
|
2004-11-22 08:48:35 -05:00
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
ok = X509_verify_cert(ctx);
|
|
|
|
if(ok && ctx->cert) {
|
2016-11-23 01:53:24 -05:00
|
|
|
unsigned char *accessinfo;
|
2016-02-11 03:42:38 -05:00
|
|
|
if(p->verbose > 1)
|
|
|
|
X509_print_ex(p->errorbio, ctx->cert, 0, 0);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
2016-12-13 19:29:44 -05:00
|
|
|
accessinfo = my_get_ext(ctx->cert, p->accesstype, NID_sinfo_access);
|
|
|
|
if(accessinfo) {
|
2016-02-11 03:42:38 -05:00
|
|
|
if(p->verbose)
|
|
|
|
BIO_printf(p->errorbio, "Setting URL from SIA to: %s\n", accessinfo);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
curl_easy_setopt(p->curl, CURLOPT_URL, accessinfo);
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
2016-02-11 03:42:38 -05:00
|
|
|
else if(accessinfo = my_get_ext(ctx->cert, p->accesstype,
|
|
|
|
NID_info_access)) {
|
|
|
|
if(p->verbose)
|
|
|
|
BIO_printf(p->errorbio, "Setting URL from AIA to: %s\n", accessinfo);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
curl_easy_setopt(p->curl, CURLOPT_URL, accessinfo);
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
|
|
|
}
|
2016-02-11 03:42:38 -05:00
|
|
|
if(p->verbose > 2)
|
|
|
|
BIO_printf(p->errorbio, "leaving ssl_app_verify_callback with %d\n", ok);
|
|
|
|
|
|
|
|
return ok;
|
2003-10-03 09:46:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
/* The SSL initialisation callback. The callback sets:
|
2004-11-22 08:48:35 -05:00
|
|
|
- a private key and certificate
|
|
|
|
- a trusted ca certificate
|
|
|
|
- a preferred cipherlist
|
|
|
|
- an application verification callback (the function above)
|
2003-10-03 09:46:27 -04:00
|
|
|
*/
|
|
|
|
|
2016-11-23 01:53:24 -05:00
|
|
|
static CURLcode sslctxfun(CURL *curl, void *sslctx, void *parm)
|
2016-03-06 10:54:58 -05:00
|
|
|
{
|
2016-11-23 01:53:24 -05:00
|
|
|
sslctxparm *p = (sslctxparm *) parm;
|
|
|
|
SSL_CTX *ctx = (SSL_CTX *) sslctx;
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(!SSL_CTX_use_certificate(ctx, p->usercert)) {
|
|
|
|
BIO_printf(p->errorbio, "SSL_CTX_use_certificate problem\n");
|
|
|
|
goto err;
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
2016-02-11 03:42:38 -05:00
|
|
|
if(!SSL_CTX_use_PrivateKey(ctx, p->pkey)) {
|
|
|
|
BIO_printf(p->errorbio, "SSL_CTX_use_PrivateKey\n");
|
|
|
|
goto err;
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(!SSL_CTX_check_private_key(ctx)) {
|
|
|
|
BIO_printf(p->errorbio, "SSL_CTX_check_private_key\n");
|
|
|
|
goto err;
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
SSL_CTX_set_quiet_shutdown(ctx, 1);
|
|
|
|
SSL_CTX_set_cipher_list(ctx, "RC4-MD5");
|
2004-11-22 08:48:35 -05:00
|
|
|
SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
X509_STORE_add_cert(SSL_CTX_get_cert_store(ctx),
|
|
|
|
sk_X509_value(p->ca, sk_X509_num(p->ca)-1));
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
SSL_CTX_set_verify_depth(ctx, 2);
|
|
|
|
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, ZERO_NULL);
|
2004-11-22 08:48:35 -05:00
|
|
|
SSL_CTX_set_cert_verify_callback(ctx, ssl_app_verify_callback, parm);
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
return CURLE_OK;
|
2004-11-22 08:48:35 -05:00
|
|
|
err:
|
|
|
|
ERR_print_errors(p->errorbio);
|
|
|
|
return CURLE_SSL_CERTPROBLEM;
|
2003-10-03 09:46:27 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-06 10:54:58 -05:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2017-09-09 17:09:06 -04:00
|
|
|
BIO* in = NULL;
|
|
|
|
BIO* out = NULL;
|
2004-11-22 08:48:35 -05:00
|
|
|
|
2016-11-23 01:53:24 -05:00
|
|
|
char *outfile = NULL;
|
|
|
|
char *infile = NULL;
|
2004-11-22 08:48:35 -05:00
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
int tabLength = 100;
|
2004-11-22 08:48:35 -05:00
|
|
|
char *binaryptr;
|
2016-11-23 01:53:24 -05:00
|
|
|
char *mimetype;
|
2017-09-09 17:09:06 -04:00
|
|
|
char *mimetypeaccept = NULL;
|
2016-11-23 01:53:24 -05:00
|
|
|
char *contenttype;
|
|
|
|
const char **pp;
|
|
|
|
unsigned char *hostporturl = NULL;
|
|
|
|
BIO *p12bio;
|
2004-11-22 08:48:35 -05:00
|
|
|
char **args = argv + 1;
|
2016-11-23 01:53:24 -05:00
|
|
|
unsigned char *serverurl;
|
2004-11-22 08:48:35 -05:00
|
|
|
sslctxparm p;
|
|
|
|
char *response;
|
|
|
|
|
|
|
|
CURLcode res;
|
2017-09-09 17:09:06 -04:00
|
|
|
struct curl_slist *headers = NULL;
|
|
|
|
int badarg = 0;
|
2007-07-12 17:11:10 -04:00
|
|
|
|
2008-09-06 01:29:05 -04:00
|
|
|
binaryptr = malloc(tabLength);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
2007-07-12 17:11:10 -04:00
|
|
|
p.verbose = 0;
|
2016-12-13 17:34:59 -05:00
|
|
|
p.errorbio = BIO_new_fp(stderr, BIO_NOCLOSE);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
while(*args && *args[0] == '-') {
|
|
|
|
if(!strcmp (*args, "-in")) {
|
|
|
|
if(args[1]) {
|
2017-09-09 17:09:06 -04:00
|
|
|
infile = *(++args);
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else
|
2017-09-09 17:09:06 -04:00
|
|
|
badarg = 1;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else if(!strcmp (*args, "-out")) {
|
|
|
|
if(args[1]) {
|
2017-09-09 17:09:06 -04:00
|
|
|
outfile = *(++args);
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else
|
2017-09-09 17:09:06 -04:00
|
|
|
badarg = 1;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else if(!strcmp (*args, "-p12")) {
|
|
|
|
if(args[1]) {
|
2004-11-22 08:48:35 -05:00
|
|
|
p.p12file = *(++args);
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else
|
2017-09-09 17:09:06 -04:00
|
|
|
badarg = 1;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else if(strcmp(*args, "-envpass") == 0) {
|
|
|
|
if(args[1]) {
|
2004-11-22 08:48:35 -05:00
|
|
|
p.pst = getenv(*(++args));
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else
|
2017-09-09 17:09:06 -04:00
|
|
|
badarg = 1;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else if(strcmp(*args, "-connect") == 0) {
|
|
|
|
if(args[1]) {
|
2004-11-22 08:48:35 -05:00
|
|
|
hostporturl = *(++args);
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else
|
2017-09-09 17:09:06 -04:00
|
|
|
badarg = 1;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else if(strcmp(*args, "-mimetype") == 0) {
|
|
|
|
if(args[1]) {
|
2004-11-22 08:48:35 -05:00
|
|
|
mimetype = *(++args);
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else
|
2017-09-09 17:09:06 -04:00
|
|
|
badarg = 1;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else if(strcmp(*args, "-acceptmime") == 0) {
|
|
|
|
if(args[1]) {
|
2004-11-22 08:48:35 -05:00
|
|
|
mimetypeaccept = *(++args);
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else
|
2017-09-09 17:09:06 -04:00
|
|
|
badarg = 1;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else if(strcmp(*args, "-accesstype") == 0) {
|
|
|
|
if(args[1]) {
|
2016-12-13 19:29:44 -05:00
|
|
|
p.accesstype = OBJ_obj2nid(OBJ_txt2obj(*++args, 0));
|
|
|
|
if(p.accesstype == 0)
|
2017-09-09 17:09:06 -04:00
|
|
|
badarg = 1;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else
|
2017-09-09 17:09:06 -04:00
|
|
|
badarg = 1;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else if(strcmp(*args, "-verbose") == 0) {
|
2004-11-22 08:48:35 -05:00
|
|
|
p.verbose++;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else
|
2017-09-09 17:09:06 -04:00
|
|
|
badarg = 1;
|
2004-11-22 08:48:35 -05:00
|
|
|
args++;
|
|
|
|
}
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
if(mimetype == NULL || mimetypeaccept == NULL)
|
2016-02-11 03:42:38 -05:00
|
|
|
badarg = 1;
|
2004-11-22 08:48:35 -05:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(badarg) {
|
2017-09-09 17:09:06 -04:00
|
|
|
for(pp = curlx_usage; (*pp != NULL); pp++)
|
2016-02-11 03:42:38 -05:00
|
|
|
BIO_printf(p.errorbio, "%s\n", *pp);
|
|
|
|
BIO_printf(p.errorbio, "\n");
|
2004-11-22 08:48:35 -05:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set input */
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
in = BIO_new(BIO_s_file());
|
|
|
|
if(in == NULL) {
|
2004-11-22 08:48:35 -05:00
|
|
|
BIO_printf(p.errorbio, "Error setting input bio\n");
|
|
|
|
goto err;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else if(infile == NULL)
|
|
|
|
BIO_set_fp(in, stdin, BIO_NOCLOSE|BIO_FP_TEXT);
|
|
|
|
else if(BIO_read_filename(in, infile) <= 0) {
|
2004-11-22 08:48:35 -05:00
|
|
|
BIO_printf(p.errorbio, "Error opening input file %s\n", infile);
|
2003-10-03 09:46:27 -04:00
|
|
|
BIO_free(in);
|
2004-11-22 08:48:35 -05:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set output */
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
out = BIO_new(BIO_s_file());
|
|
|
|
if(out == NULL) {
|
2004-11-22 08:48:35 -05:00
|
|
|
BIO_printf(p.errorbio, "Error setting output bio.\n");
|
|
|
|
goto err;
|
2016-02-11 03:42:38 -05:00
|
|
|
}
|
|
|
|
else if(outfile == NULL)
|
|
|
|
BIO_set_fp(out, stdout, BIO_NOCLOSE|BIO_FP_TEXT);
|
|
|
|
else if(BIO_write_filename(out, outfile) <= 0) {
|
2004-11-22 08:48:35 -05:00
|
|
|
BIO_printf(p.errorbio, "Error opening output file %s\n", outfile);
|
2003-10-03 09:46:27 -04:00
|
|
|
BIO_free(out);
|
2004-11-22 08:48:35 -05:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-13 17:34:59 -05:00
|
|
|
p.errorbio = BIO_new_fp(stderr, BIO_NOCLOSE);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
2016-12-13 19:29:44 -05:00
|
|
|
p.curl = curl_easy_init();
|
|
|
|
if(!p.curl) {
|
2004-11-22 08:48:35 -05:00
|
|
|
BIO_printf(p.errorbio, "Cannot init curl lib\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-12-13 19:29:44 -05:00
|
|
|
p12bio = BIO_new_file(p.p12file, "rb");
|
|
|
|
if(!p12bio) {
|
2016-02-11 03:42:38 -05:00
|
|
|
BIO_printf(p.errorbio, "Error opening P12 file %s\n", p.p12file);
|
|
|
|
goto err;
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
2016-12-13 19:29:44 -05:00
|
|
|
p.p12 = d2i_PKCS12_bio(p12bio, NULL);
|
|
|
|
if(!p.p12) {
|
2016-02-11 03:42:38 -05:00
|
|
|
BIO_printf(p.errorbio, "Cannot decode P12 structure %s\n", p.p12file);
|
|
|
|
goto err;
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
p.ca = NULL;
|
2016-02-11 03:42:38 -05:00
|
|
|
if(!(PKCS12_parse (p.p12, p.pst, &(p.pkey), &(p.usercert), &(p.ca) ) )) {
|
|
|
|
BIO_printf(p.errorbio, "Invalid P12 structure in %s\n", p.p12file);
|
|
|
|
goto err;
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(sk_X509_num(p.ca) <= 0) {
|
|
|
|
BIO_printf(p.errorbio, "No trustworthy CA given.%s\n", p.p12file);
|
|
|
|
goto err;
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(p.verbose > 1)
|
|
|
|
X509_print_ex(p.errorbio, p.usercert, 0, 0);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
|
|
|
/* determine URL to go */
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(hostporturl) {
|
|
|
|
size_t len = strlen(hostporturl) + 9;
|
|
|
|
serverurl = malloc(len);
|
|
|
|
snprintf(serverurl, len, "https://%s", hostporturl);
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
2016-02-11 03:42:38 -05:00
|
|
|
else if(p.accesstype != 0) { /* see whether we can find an AIA or SIA for a
|
|
|
|
given access type */
|
2016-12-13 19:29:44 -05:00
|
|
|
serverurl = my_get_ext(p.usercert, p.accesstype, NID_info_access);
|
|
|
|
if(!serverurl) {
|
2017-09-09 17:09:06 -04:00
|
|
|
int j = 0;
|
2016-02-11 03:42:38 -05:00
|
|
|
BIO_printf(p.errorbio, "no service URL in user cert "
|
2004-11-22 08:48:35 -05:00
|
|
|
"cherching in others certificats\n");
|
2017-09-09 17:09:06 -04:00
|
|
|
for(j = 0; j<sk_X509_num(p.ca); j++) {
|
2016-12-13 19:29:44 -05:00
|
|
|
serverurl = my_get_ext(sk_X509_value(p.ca, j), p.accesstype,
|
|
|
|
NID_info_access);
|
|
|
|
if(serverurl)
|
2004-11-22 08:48:35 -05:00
|
|
|
break;
|
2016-12-13 19:29:44 -05:00
|
|
|
serverurl = my_get_ext(sk_X509_value(p.ca, j), p.accesstype,
|
|
|
|
NID_sinfo_access);
|
|
|
|
if(serverurl)
|
2004-11-22 08:48:35 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(!serverurl) {
|
2004-11-22 08:48:35 -05:00
|
|
|
BIO_printf(p.errorbio, "no service URL in certificats,"
|
|
|
|
" check '-accesstype (AD_DVCS | ad_timestamping)'"
|
|
|
|
" or use '-connect'\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(p.verbose)
|
2004-11-22 08:48:35 -05:00
|
|
|
BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
|
|
|
|
|
|
|
|
curl_easy_setopt(p.curl, CURLOPT_URL, serverurl);
|
|
|
|
|
|
|
|
/* Now specify the POST binary data */
|
|
|
|
|
|
|
|
curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
|
2016-02-11 03:42:38 -05:00
|
|
|
curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE, (long)tabLength);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
|
|
|
/* pass our list of custom made headers */
|
|
|
|
|
2017-09-09 17:55:08 -04:00
|
|
|
contenttype = malloc(15 + strlen(mimetype));
|
|
|
|
snprintf(contenttype, 15 + strlen(mimetype), "Content-type: %s", mimetype);
|
2016-02-11 03:42:38 -05:00
|
|
|
headers = curl_slist_append(headers, contenttype);
|
2004-11-22 08:48:35 -05:00
|
|
|
curl_easy_setopt(p.curl, CURLOPT_HTTPHEADER, headers);
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(p.verbose)
|
2004-11-22 08:48:35 -05:00
|
|
|
BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
|
|
|
|
|
|
|
|
{
|
|
|
|
FILE *outfp;
|
2016-02-11 03:42:38 -05:00
|
|
|
BIO_get_fp(out, &outfp);
|
2010-01-04 13:43:29 -05:00
|
|
|
curl_easy_setopt(p.curl, CURLOPT_WRITEDATA, outfp);
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
res = curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(res != CURLE_OK)
|
|
|
|
BIO_printf(p.errorbio, "%d %s=%d %d\n", __LINE__,
|
|
|
|
"CURLOPT_SSL_CTX_FUNCTION", CURLOPT_SSL_CTX_FUNCTION, res);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
|
|
|
curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_DATA, &p);
|
|
|
|
|
|
|
|
{
|
2017-09-09 17:09:06 -04:00
|
|
|
int lu; int i = 0;
|
2016-12-13 17:34:59 -05:00
|
|
|
while((lu = BIO_read(in, &binaryptr[i], tabLength-i)) >0) {
|
2017-09-09 17:09:06 -04:00
|
|
|
i += lu;
|
|
|
|
if(i == tabLength) {
|
|
|
|
tabLength += 100;
|
|
|
|
binaryptr = realloc(binaryptr, tabLength); /* should be more careful */
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tabLength = i;
|
|
|
|
}
|
|
|
|
/* Now specify the POST binary data */
|
|
|
|
|
|
|
|
curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
|
2016-02-11 03:42:38 -05:00
|
|
|
curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE, (long)tabLength);
|
2004-11-22 08:48:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* Perform the request, res will get the return code */
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
BIO_printf(p.errorbio, "%d %s %d\n", __LINE__, "curl_easy_perform",
|
2004-11-22 08:48:35 -05:00
|
|
|
res = curl_easy_perform(p.curl));
|
|
|
|
{
|
2017-09-09 17:09:06 -04:00
|
|
|
int result = curl_easy_getinfo(p.curl, CURLINFO_CONTENT_TYPE, &response);
|
2017-11-18 13:06:35 -05:00
|
|
|
if(mimetypeaccept && p.verbose) {
|
2016-02-11 03:42:38 -05:00
|
|
|
if(!strcmp(mimetypeaccept, response))
|
|
|
|
BIO_printf(p.errorbio, "the response has a correct mimetype : %s\n",
|
2004-11-22 08:48:35 -05:00
|
|
|
response);
|
|
|
|
else
|
2016-02-11 03:42:38 -05:00
|
|
|
BIO_printf(p.errorbio, "the response doesn\'t have an acceptable "
|
2004-11-22 08:48:35 -05:00
|
|
|
"mime type, it is %s instead of %s\n",
|
2016-02-11 03:42:38 -05:00
|
|
|
response, mimetypeaccept);
|
2017-11-18 13:06:35 -05:00
|
|
|
}
|
2004-11-22 08:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*** code d'erreur si accept mime ***, egalement code return HTTP != 200 ***/
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2004-11-22 08:48:35 -05:00
|
|
|
/* free the header list*/
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2004-11-22 08:48:35 -05:00
|
|
|
curl_slist_free_all(headers);
|
2003-10-03 09:46:27 -04:00
|
|
|
|
2004-11-22 08:48:35 -05:00
|
|
|
/* always cleanup */
|
|
|
|
curl_easy_cleanup(p.curl);
|
2003-10-03 09:46:27 -04:00
|
|
|
|
2004-11-22 08:48:35 -05:00
|
|
|
BIO_free(in);
|
|
|
|
BIO_free(out);
|
|
|
|
return (EXIT_SUCCESS);
|
2003-10-03 09:46:27 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
err: BIO_printf(p.errorbio, "error");
|
2004-11-22 08:48:35 -05:00
|
|
|
exit(1);
|
|
|
|
}
|