blob: 2aa10aa75ea4ecd9799853a0141b115e4574d8db [file] [log] [blame]
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001/*
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*/
Alex Deymo8f1a2142016-06-28 14:49:26 -070012/* <DESC>
13 * demonstrates use of SSL context callback, requires OpenSSL
14 * </DESC>
15 */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070016
17/*
18 * Copyright (c) 2003 The OpenEvidence Project. All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions, the following disclaimer,
26 * and the original OpenSSL and SSLeay Licences below.
27 *
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions, the following disclaimer
30 * and the original OpenSSL and SSLeay Licences below in
31 * the documentation and/or other materials provided with the
32 * distribution.
33 *
34 * 3. All advertising materials mentioning features or use of this
35 * software must display the following acknowledgments:
36 * "This product includes software developed by the Openevidence Project
37 * for use in the OpenEvidence Toolkit. (http://www.openevidence.org/)"
38 * This product includes software developed by the OpenSSL Project
Alex Deymo8f1a2142016-06-28 14:49:26 -070039 * for use in the OpenSSL Toolkit (https://www.openssl.org/)"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070040 * This product includes cryptographic software written by Eric Young
41 * (eay@cryptsoft.com). This product includes software written by Tim
42 * Hudson (tjh@cryptsoft.com)."
43 *
44 * 4. The names "OpenEvidence Toolkit" and "OpenEvidence Project" must not be
45 * used to endorse or promote products derived from this software without
46 * prior written permission. For written permission, please contact
47 * openevidence-core@openevidence.org.
48 *
49 * 5. Products derived from this software may not be called "OpenEvidence"
50 * nor may "OpenEvidence" appear in their names without prior written
51 * permission of the OpenEvidence Project.
52 *
53 * 6. Redistributions of any form whatsoever must retain the following
54 * acknowledgments:
55 * "This product includes software developed by the OpenEvidence Project
56 * for use in the OpenEvidence Toolkit (http://www.openevidence.org/)
57 * This product includes software developed by the OpenSSL Project
Alex Deymo8f1a2142016-06-28 14:49:26 -070058 * for use in the OpenSSL Toolkit (https://www.openssl.org/)"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070059 * This product includes cryptographic software written by Eric Young
60 * (eay@cryptsoft.com). This product includes software written by Tim
61 * Hudson (tjh@cryptsoft.com)."
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE OpenEvidence PROJECT ``AS IS'' AND ANY
64 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
66 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenEvidence PROJECT OR
67 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
68 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
69 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
70 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
72 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
73 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
74 * OF THE POSSIBILITY OF SUCH DAMAGE.
75 * ====================================================================
76 *
77 * This product includes software developed by the OpenSSL Project
Alex Deymo8f1a2142016-06-28 14:49:26 -070078 * for use in the OpenSSL Toolkit (https://www.openssl.org/)
Lucas Eckels9bd90e62012-08-06 15:07:02 -070079 * This product includes cryptographic software written by Eric Young
80 * (eay@cryptsoft.com). This product includes software written by Tim
81 * Hudson (tjh@cryptsoft.com).
82 *
83 */
84
85#include <stdio.h>
86#include <stdlib.h>
87#include <string.h>
88#include <curl/curl.h>
89#include <openssl/x509v3.h>
90#include <openssl/x509_vfy.h>
91#include <openssl/crypto.h>
92#include <openssl/lhash.h>
93#include <openssl/objects.h>
94#include <openssl/err.h>
95#include <openssl/evp.h>
96#include <openssl/x509.h>
97#include <openssl/pkcs12.h>
98#include <openssl/bio.h>
99#include <openssl/ssl.h>
100
101static const char *curlx_usage[]={
102 "usage: curlx args\n",
103 " -p12 arg - tia file ",
Elliott Hughes82be86d2017-09-20 17:00:17 -0700104 " -envpass arg - environment variable which content the tia private"
Alex Deymo8f1a2142016-06-28 14:49:26 -0700105 " key password",
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700106 " -out arg - output file (response)- default stdout",
107 " -in arg - input file (request)- default stdin",
Alex Deymo8f1a2142016-06-28 14:49:26 -0700108 " -connect arg - URL of the server for the connection ex:"
109 " www.openevidence.org",
110 " -mimetype arg - MIME type for data in ex : application/timestamp-query"
111 " or application/dvcs -default application/timestamp-query",
112 " -acceptmime arg - MIME type acceptable for the response ex : "
113 "application/timestamp-response or application/dvcs -default none",
114 " -accesstype arg - an Object identifier in an AIA/SIA method, e.g."
115 " AD_DVCS or ad_timestamping",
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700116 NULL
117};
118
119/*
120
121./curlx -p12 psy.p12 -envpass XX -in request -verbose -accesstype AD_DVCS
122-mimetype application/dvcs -acceptmime application/dvcs -out response
123
124*/
125
126/*
127 * We use this ZERO_NULL to avoid picky compiler warnings,
128 * when assigning a NULL pointer to a function pointer var.
129 */
130
131#define ZERO_NULL 0
132
133/* This is a context that we pass to all callbacks */
134
135typedef struct sslctxparm_st {
Elliott Hughescee03382017-06-23 12:17:18 -0700136 unsigned char *p12file;
137 const char *pst;
138 PKCS12 *p12;
139 EVP_PKEY *pkey;
140 X509 *usercert;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700141 STACK_OF(X509) * ca;
Elliott Hughescee03382017-06-23 12:17:18 -0700142 CURL *curl;
143 BIO *errorbio;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700144 int accesstype;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700145 int verbose;
146
147} sslctxparm;
148
149/* some helper function. */
150
Alex Deymo8f1a2142016-06-28 14:49:26 -0700151static char *ia5string(ASN1_IA5STRING *ia5)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700152{
153 char *tmp;
154 if(!ia5 || !ia5->length)
155 return NULL;
156 tmp = OPENSSL_malloc(ia5->length + 1);
157 memcpy(tmp, ia5->data, ia5->length);
158 tmp[ia5->length] = 0;
159 return tmp;
160}
161
162/* A conveniance routine to get an access URI. */
Alex Deymo8f1a2142016-06-28 14:49:26 -0700163static unsigned char *my_get_ext(X509 *cert, const int type,
164 int extensiontype)
165{
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700166 int i;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700167 STACK_OF(ACCESS_DESCRIPTION) * accessinfo;
168 accessinfo = X509_get_ext_d2i(cert, extensiontype, NULL, NULL);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700169
Alex Deymo8f1a2142016-06-28 14:49:26 -0700170 if(!sk_ACCESS_DESCRIPTION_num(accessinfo))
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700171 return NULL;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700172 for(i = 0; i < sk_ACCESS_DESCRIPTION_num(accessinfo); i++) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700173 ACCESS_DESCRIPTION * ad = sk_ACCESS_DESCRIPTION_value(accessinfo, i);
Alex Deymo8f1a2142016-06-28 14:49:26 -0700174 if(OBJ_obj2nid(ad->method) == type) {
175 if(ad->location->type == GEN_URI) {
176 return ia5string(ad->location->d.ia5);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700177 }
178 return NULL;
179 }
180 }
181 return NULL;
182}
183
184/* This is an application verification call back, it does not
185 perform any addition verification but tries to find a URL
186 in the presented certificat. If found, this will become
187 the URL to be used in the POST.
188*/
189
190static int ssl_app_verify_callback(X509_STORE_CTX *ctx, void *arg)
191{
192 sslctxparm * p = (sslctxparm *) arg;
193 int ok;
194
Alex Deymo8f1a2142016-06-28 14:49:26 -0700195 if(p->verbose > 2)
196 BIO_printf(p->errorbio, "entering ssl_app_verify_callback\n");
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700197
Alex Deymo486467e2017-12-19 19:04:07 +0100198 ok = X509_verify_cert(ctx);
199 if(ok && ctx->cert) {
Elliott Hughescee03382017-06-23 12:17:18 -0700200 unsigned char *accessinfo;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700201 if(p->verbose > 1)
202 X509_print_ex(p->errorbio, ctx->cert, 0, 0);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700203
Elliott Hughes82be86d2017-09-20 17:00:17 -0700204 accessinfo = my_get_ext(ctx->cert, p->accesstype, NID_sinfo_access);
205 if(accessinfo) {
Alex Deymo8f1a2142016-06-28 14:49:26 -0700206 if(p->verbose)
207 BIO_printf(p->errorbio, "Setting URL from SIA to: %s\n", accessinfo);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700208
Alex Deymo8f1a2142016-06-28 14:49:26 -0700209 curl_easy_setopt(p->curl, CURLOPT_URL, accessinfo);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700210 }
Alex Deymo8f1a2142016-06-28 14:49:26 -0700211 else if(accessinfo = my_get_ext(ctx->cert, p->accesstype,
212 NID_info_access)) {
213 if(p->verbose)
214 BIO_printf(p->errorbio, "Setting URL from AIA to: %s\n", accessinfo);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700215
Alex Deymo8f1a2142016-06-28 14:49:26 -0700216 curl_easy_setopt(p->curl, CURLOPT_URL, accessinfo);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700217 }
218 }
Alex Deymo8f1a2142016-06-28 14:49:26 -0700219 if(p->verbose > 2)
220 BIO_printf(p->errorbio, "leaving ssl_app_verify_callback with %d\n", ok);
221
222 return ok;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700223}
224
225
Alex Deymo8f1a2142016-06-28 14:49:26 -0700226/* The SSL initialisation callback. The callback sets:
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700227 - a private key and certificate
228 - a trusted ca certificate
229 - a preferred cipherlist
230 - an application verification callback (the function above)
231*/
232
Elliott Hughescee03382017-06-23 12:17:18 -0700233static CURLcode sslctxfun(CURL *curl, void *sslctx, void *parm)
Alex Deymo8f1a2142016-06-28 14:49:26 -0700234{
Elliott Hughescee03382017-06-23 12:17:18 -0700235 sslctxparm *p = (sslctxparm *) parm;
236 SSL_CTX *ctx = (SSL_CTX *) sslctx;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700237
Alex Deymo8f1a2142016-06-28 14:49:26 -0700238 if(!SSL_CTX_use_certificate(ctx, p->usercert)) {
239 BIO_printf(p->errorbio, "SSL_CTX_use_certificate problem\n");
240 goto err;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700241 }
Alex Deymo8f1a2142016-06-28 14:49:26 -0700242 if(!SSL_CTX_use_PrivateKey(ctx, p->pkey)) {
243 BIO_printf(p->errorbio, "SSL_CTX_use_PrivateKey\n");
244 goto err;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700245 }
246
Alex Deymo8f1a2142016-06-28 14:49:26 -0700247 if(!SSL_CTX_check_private_key(ctx)) {
248 BIO_printf(p->errorbio, "SSL_CTX_check_private_key\n");
249 goto err;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700250 }
251
Alex Deymo8f1a2142016-06-28 14:49:26 -0700252 SSL_CTX_set_quiet_shutdown(ctx, 1);
253 SSL_CTX_set_cipher_list(ctx, "RC4-MD5");
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700254 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
255
Alex Deymo8f1a2142016-06-28 14:49:26 -0700256 X509_STORE_add_cert(SSL_CTX_get_cert_store(ctx),
257 sk_X509_value(p->ca, sk_X509_num(p->ca)-1));
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700258
Alex Deymo8f1a2142016-06-28 14:49:26 -0700259 SSL_CTX_set_verify_depth(ctx, 2);
260 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, ZERO_NULL);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700261 SSL_CTX_set_cert_verify_callback(ctx, ssl_app_verify_callback, parm);
262
Alex Deymo8f1a2142016-06-28 14:49:26 -0700263 return CURLE_OK;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700264 err:
265 ERR_print_errors(p->errorbio);
266 return CURLE_SSL_CERTPROBLEM;
267
268}
269
Alex Deymo8f1a2142016-06-28 14:49:26 -0700270int main(int argc, char **argv)
271{
Alex Deymo486467e2017-12-19 19:04:07 +0100272 BIO* in = NULL;
273 BIO* out = NULL;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700274
Elliott Hughescee03382017-06-23 12:17:18 -0700275 char *outfile = NULL;
276 char *infile = NULL;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700277
Alex Deymo486467e2017-12-19 19:04:07 +0100278 int tabLength = 100;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700279 char *binaryptr;
Elliott Hughescee03382017-06-23 12:17:18 -0700280 char *mimetype;
Alex Deymo486467e2017-12-19 19:04:07 +0100281 char *mimetypeaccept = NULL;
Elliott Hughescee03382017-06-23 12:17:18 -0700282 char *contenttype;
283 const char **pp;
284 unsigned char *hostporturl = NULL;
285 BIO *p12bio;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700286 char **args = argv + 1;
Elliott Hughescee03382017-06-23 12:17:18 -0700287 unsigned char *serverurl;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700288 sslctxparm p;
289 char *response;
290
291 CURLcode res;
Alex Deymo486467e2017-12-19 19:04:07 +0100292 struct curl_slist *headers = NULL;
293 int badarg = 0;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700294
295 binaryptr = malloc(tabLength);
296
297 p.verbose = 0;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700298 p.errorbio = BIO_new_fp(stderr, BIO_NOCLOSE);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700299
300 curl_global_init(CURL_GLOBAL_DEFAULT);
301
302 /* we need some more for the P12 decoding */
303
304 OpenSSL_add_all_ciphers();
305 OpenSSL_add_all_digests();
306 ERR_load_crypto_strings();
307
Alex Deymo8f1a2142016-06-28 14:49:26 -0700308 while(*args && *args[0] == '-') {
309 if(!strcmp (*args, "-in")) {
310 if(args[1]) {
Alex Deymo486467e2017-12-19 19:04:07 +0100311 infile = *(++args);
Alex Deymo8f1a2142016-06-28 14:49:26 -0700312 }
313 else
Alex Deymo486467e2017-12-19 19:04:07 +0100314 badarg = 1;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700315 }
316 else if(!strcmp (*args, "-out")) {
317 if(args[1]) {
Alex Deymo486467e2017-12-19 19:04:07 +0100318 outfile = *(++args);
Alex Deymo8f1a2142016-06-28 14:49:26 -0700319 }
320 else
Alex Deymo486467e2017-12-19 19:04:07 +0100321 badarg = 1;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700322 }
323 else if(!strcmp (*args, "-p12")) {
324 if(args[1]) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700325 p.p12file = *(++args);
Alex Deymo8f1a2142016-06-28 14:49:26 -0700326 }
327 else
Alex Deymo486467e2017-12-19 19:04:07 +0100328 badarg = 1;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700329 }
330 else if(strcmp(*args, "-envpass") == 0) {
331 if(args[1]) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700332 p.pst = getenv(*(++args));
Alex Deymo8f1a2142016-06-28 14:49:26 -0700333 }
334 else
Alex Deymo486467e2017-12-19 19:04:07 +0100335 badarg = 1;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700336 }
337 else if(strcmp(*args, "-connect") == 0) {
338 if(args[1]) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700339 hostporturl = *(++args);
Alex Deymo8f1a2142016-06-28 14:49:26 -0700340 }
341 else
Alex Deymo486467e2017-12-19 19:04:07 +0100342 badarg = 1;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700343 }
344 else if(strcmp(*args, "-mimetype") == 0) {
345 if(args[1]) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700346 mimetype = *(++args);
Alex Deymo8f1a2142016-06-28 14:49:26 -0700347 }
348 else
Alex Deymo486467e2017-12-19 19:04:07 +0100349 badarg = 1;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700350 }
351 else if(strcmp(*args, "-acceptmime") == 0) {
352 if(args[1]) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700353 mimetypeaccept = *(++args);
Alex Deymo8f1a2142016-06-28 14:49:26 -0700354 }
355 else
Alex Deymo486467e2017-12-19 19:04:07 +0100356 badarg = 1;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700357 }
358 else if(strcmp(*args, "-accesstype") == 0) {
359 if(args[1]) {
Elliott Hughes82be86d2017-09-20 17:00:17 -0700360 p.accesstype = OBJ_obj2nid(OBJ_txt2obj(*++args, 0));
361 if(p.accesstype == 0)
Alex Deymo486467e2017-12-19 19:04:07 +0100362 badarg = 1;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700363 }
364 else
Alex Deymo486467e2017-12-19 19:04:07 +0100365 badarg = 1;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700366 }
367 else if(strcmp(*args, "-verbose") == 0) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700368 p.verbose++;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700369 }
370 else
Alex Deymo486467e2017-12-19 19:04:07 +0100371 badarg = 1;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700372 args++;
373 }
374
Alex Deymo486467e2017-12-19 19:04:07 +0100375 if(mimetype == NULL || mimetypeaccept == NULL)
Alex Deymo8f1a2142016-06-28 14:49:26 -0700376 badarg = 1;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700377
Alex Deymo8f1a2142016-06-28 14:49:26 -0700378 if(badarg) {
Alex Deymo486467e2017-12-19 19:04:07 +0100379 for(pp = curlx_usage; (*pp != NULL); pp++)
Alex Deymo8f1a2142016-06-28 14:49:26 -0700380 BIO_printf(p.errorbio, "%s\n", *pp);
381 BIO_printf(p.errorbio, "\n");
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700382 goto err;
383 }
384
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700385 /* set input */
386
Alex Deymo486467e2017-12-19 19:04:07 +0100387 in = BIO_new(BIO_s_file());
388 if(in == NULL) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700389 BIO_printf(p.errorbio, "Error setting input bio\n");
390 goto err;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700391 }
392 else if(infile == NULL)
393 BIO_set_fp(in, stdin, BIO_NOCLOSE|BIO_FP_TEXT);
394 else if(BIO_read_filename(in, infile) <= 0) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700395 BIO_printf(p.errorbio, "Error opening input file %s\n", infile);
396 BIO_free(in);
397 goto err;
398 }
399
400 /* set output */
401
Alex Deymo486467e2017-12-19 19:04:07 +0100402 out = BIO_new(BIO_s_file());
403 if(out == NULL) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700404 BIO_printf(p.errorbio, "Error setting output bio.\n");
405 goto err;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700406 }
407 else if(outfile == NULL)
408 BIO_set_fp(out, stdout, BIO_NOCLOSE|BIO_FP_TEXT);
409 else if(BIO_write_filename(out, outfile) <= 0) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700410 BIO_printf(p.errorbio, "Error opening output file %s\n", outfile);
411 BIO_free(out);
412 goto err;
413 }
414
415
Elliott Hughes82be86d2017-09-20 17:00:17 -0700416 p.errorbio = BIO_new_fp(stderr, BIO_NOCLOSE);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700417
Elliott Hughes82be86d2017-09-20 17:00:17 -0700418 p.curl = curl_easy_init();
419 if(!p.curl) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700420 BIO_printf(p.errorbio, "Cannot init curl lib\n");
421 goto err;
422 }
423
Elliott Hughes82be86d2017-09-20 17:00:17 -0700424 p12bio = BIO_new_file(p.p12file, "rb");
425 if(!p12bio) {
Alex Deymo8f1a2142016-06-28 14:49:26 -0700426 BIO_printf(p.errorbio, "Error opening P12 file %s\n", p.p12file);
427 goto err;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700428 }
Elliott Hughes82be86d2017-09-20 17:00:17 -0700429 p.p12 = d2i_PKCS12_bio(p12bio, NULL);
430 if(!p.p12) {
Alex Deymo8f1a2142016-06-28 14:49:26 -0700431 BIO_printf(p.errorbio, "Cannot decode P12 structure %s\n", p.p12file);
432 goto err;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700433 }
434
Alex Deymo486467e2017-12-19 19:04:07 +0100435 p.ca = NULL;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700436 if(!(PKCS12_parse (p.p12, p.pst, &(p.pkey), &(p.usercert), &(p.ca) ) )) {
437 BIO_printf(p.errorbio, "Invalid P12 structure in %s\n", p.p12file);
438 goto err;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700439 }
440
Alex Deymo8f1a2142016-06-28 14:49:26 -0700441 if(sk_X509_num(p.ca) <= 0) {
442 BIO_printf(p.errorbio, "No trustworthy CA given.%s\n", p.p12file);
443 goto err;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700444 }
445
Alex Deymo8f1a2142016-06-28 14:49:26 -0700446 if(p.verbose > 1)
447 X509_print_ex(p.errorbio, p.usercert, 0, 0);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700448
449 /* determine URL to go */
450
Alex Deymo8f1a2142016-06-28 14:49:26 -0700451 if(hostporturl) {
452 size_t len = strlen(hostporturl) + 9;
453 serverurl = malloc(len);
454 snprintf(serverurl, len, "https://%s", hostporturl);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700455 }
Alex Deymo8f1a2142016-06-28 14:49:26 -0700456 else if(p.accesstype != 0) { /* see whether we can find an AIA or SIA for a
457 given access type */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700458 serverurl = my_get_ext(p.usercert, p.accesstype, NID_info_access);
459 if(!serverurl) {
Alex Deymo486467e2017-12-19 19:04:07 +0100460 int j = 0;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700461 BIO_printf(p.errorbio, "no service URL in user cert "
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700462 "cherching in others certificats\n");
Alex Deymo486467e2017-12-19 19:04:07 +0100463 for(j = 0; j<sk_X509_num(p.ca); j++) {
Elliott Hughes82be86d2017-09-20 17:00:17 -0700464 serverurl = my_get_ext(sk_X509_value(p.ca, j), p.accesstype,
465 NID_info_access);
466 if(serverurl)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700467 break;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700468 serverurl = my_get_ext(sk_X509_value(p.ca, j), p.accesstype,
469 NID_sinfo_access);
470 if(serverurl)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700471 break;
472 }
473 }
474 }
475
Alex Deymo8f1a2142016-06-28 14:49:26 -0700476 if(!serverurl) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700477 BIO_printf(p.errorbio, "no service URL in certificats,"
478 " check '-accesstype (AD_DVCS | ad_timestamping)'"
479 " or use '-connect'\n");
480 goto err;
481 }
482
Alex Deymo8f1a2142016-06-28 14:49:26 -0700483 if(p.verbose)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700484 BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
485
486 curl_easy_setopt(p.curl, CURLOPT_URL, serverurl);
487
488 /* Now specify the POST binary data */
489
490 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
Alex Deymo8f1a2142016-06-28 14:49:26 -0700491 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE, (long)tabLength);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700492
493 /* pass our list of custom made headers */
494
Alex Deymo486467e2017-12-19 19:04:07 +0100495 contenttype = malloc(15 + strlen(mimetype));
496 snprintf(contenttype, 15 + strlen(mimetype), "Content-type: %s", mimetype);
Alex Deymo8f1a2142016-06-28 14:49:26 -0700497 headers = curl_slist_append(headers, contenttype);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700498 curl_easy_setopt(p.curl, CURLOPT_HTTPHEADER, headers);
499
Alex Deymo8f1a2142016-06-28 14:49:26 -0700500 if(p.verbose)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700501 BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
502
503 {
504 FILE *outfp;
Alex Deymo8f1a2142016-06-28 14:49:26 -0700505 BIO_get_fp(out, &outfp);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700506 curl_easy_setopt(p.curl, CURLOPT_WRITEDATA, outfp);
507 }
508
Alex Deymo8f1a2142016-06-28 14:49:26 -0700509 res = curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700510
Alex Deymo8f1a2142016-06-28 14:49:26 -0700511 if(res != CURLE_OK)
512 BIO_printf(p.errorbio, "%d %s=%d %d\n", __LINE__,
513 "CURLOPT_SSL_CTX_FUNCTION", CURLOPT_SSL_CTX_FUNCTION, res);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700514
515 curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_DATA, &p);
516
517 {
Alex Deymo486467e2017-12-19 19:04:07 +0100518 int lu; int i = 0;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700519 while((lu = BIO_read(in, &binaryptr[i], tabLength-i)) >0) {
Alex Deymo486467e2017-12-19 19:04:07 +0100520 i += lu;
521 if(i == tabLength) {
522 tabLength += 100;
523 binaryptr = realloc(binaryptr, tabLength); /* should be more careful */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700524 }
525 }
526 tabLength = i;
527 }
528 /* Now specify the POST binary data */
529
530 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
Alex Deymo8f1a2142016-06-28 14:49:26 -0700531 curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE, (long)tabLength);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700532
533
534 /* Perform the request, res will get the return code */
535
Alex Deymo8f1a2142016-06-28 14:49:26 -0700536 BIO_printf(p.errorbio, "%d %s %d\n", __LINE__, "curl_easy_perform",
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700537 res = curl_easy_perform(p.curl));
538 {
Alex Deymo486467e2017-12-19 19:04:07 +0100539 int result = curl_easy_getinfo(p.curl, CURLINFO_CONTENT_TYPE, &response);
540 if(mimetypeaccept && p.verbose) {
Alex Deymo8f1a2142016-06-28 14:49:26 -0700541 if(!strcmp(mimetypeaccept, response))
542 BIO_printf(p.errorbio, "the response has a correct mimetype : %s\n",
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700543 response);
544 else
Alex Deymo8f1a2142016-06-28 14:49:26 -0700545 BIO_printf(p.errorbio, "the response doesn\'t have an acceptable "
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700546 "mime type, it is %s instead of %s\n",
Alex Deymo8f1a2142016-06-28 14:49:26 -0700547 response, mimetypeaccept);
Alex Deymo486467e2017-12-19 19:04:07 +0100548 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700549 }
550
551 /*** code d'erreur si accept mime ***, egalement code return HTTP != 200 ***/
552
553/* free the header list*/
554
555 curl_slist_free_all(headers);
556
557 /* always cleanup */
558 curl_easy_cleanup(p.curl);
559
560 BIO_free(in);
561 BIO_free(out);
562 return (EXIT_SUCCESS);
563
Alex Deymo8f1a2142016-06-28 14:49:26 -0700564 err: BIO_printf(p.errorbio, "error");
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700565 exit(1);
566}