curlx.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. curlx.c Authors: Peter Sylvester, Jean-Paul Merlin
  3. This is a little program to demonstrate the usage of
  4. - an SSL initialization callback setting a user key and trustbases
  5. coming from a pkcs12 file
  6. - using an ssl application callback to find a URI in the
  7. certificate presented during ssl session establishment.
  8. */
  9. /* <DESC>
  10. * demonstrates use of SSL context callback, requires OpenSSL
  11. * </DESC>
  12. */
  13. /*
  14. * Copyright (c) 2003 - 2022 The OpenEvidence Project. All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. *
  20. * 1. Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions, the following disclaimer,
  22. * and the original OpenSSL and SSLeay Licences below.
  23. *
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions, the following disclaimer
  26. * and the original OpenSSL and SSLeay Licences below in
  27. * the documentation and/or other materials provided with the
  28. * distribution.
  29. *
  30. * 3. All advertising materials mentioning features or use of this
  31. * software must display the following acknowledgments:
  32. * "This product includes software developed by the Openevidence Project
  33. * for use in the OpenEvidence Toolkit. (http://www.openevidence.org/)"
  34. * This product includes software developed by the OpenSSL Project
  35. * for use in the OpenSSL Toolkit (https://www.openssl.org/)"
  36. * This product includes cryptographic software written by Eric Young
  37. * (eay@cryptsoft.com). This product includes software written by Tim
  38. * Hudson (tjh@cryptsoft.com)."
  39. *
  40. * 4. The names "OpenEvidence Toolkit" and "OpenEvidence Project" must not be
  41. * used to endorse or promote products derived from this software without
  42. * prior written permission. For written permission, please contact
  43. * openevidence-core@openevidence.org.
  44. *
  45. * 5. Products derived from this software may not be called "OpenEvidence"
  46. * nor may "OpenEvidence" appear in their names without prior written
  47. * permission of the OpenEvidence Project.
  48. *
  49. * 6. Redistributions of any form whatsoever must retain the following
  50. * acknowledgments:
  51. * "This product includes software developed by the OpenEvidence Project
  52. * for use in the OpenEvidence Toolkit (http://www.openevidence.org/)
  53. * This product includes software developed by the OpenSSL Project
  54. * for use in the OpenSSL Toolkit (https://www.openssl.org/)"
  55. * This product includes cryptographic software written by Eric Young
  56. * (eay@cryptsoft.com). This product includes software written by Tim
  57. * Hudson (tjh@cryptsoft.com)."
  58. *
  59. * THIS SOFTWARE IS PROVIDED BY THE OpenEvidence PROJECT ``AS IS'' AND ANY
  60. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  62. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenEvidence PROJECT OR
  63. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  64. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  65. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  66. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  67. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  68. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  69. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  70. * OF THE POSSIBILITY OF SUCH DAMAGE.
  71. * ====================================================================
  72. *
  73. * This product includes software developed by the OpenSSL Project
  74. * for use in the OpenSSL Toolkit (https://www.openssl.org/)
  75. * This product includes cryptographic software written by Eric Young
  76. * (eay@cryptsoft.com). This product includes software written by Tim
  77. * Hudson (tjh@cryptsoft.com).
  78. *
  79. */
  80. #include <stdio.h>
  81. #include <stdlib.h>
  82. #include <string.h>
  83. #include <curl/curl.h>
  84. #include <openssl/x509v3.h>
  85. #include <openssl/x509_vfy.h>
  86. #include <openssl/crypto.h>
  87. #include <openssl/lhash.h>
  88. #include <openssl/objects.h>
  89. #include <openssl/err.h>
  90. #include <openssl/evp.h>
  91. #include <openssl/x509.h>
  92. #include <openssl/pkcs12.h>
  93. #include <openssl/bio.h>
  94. #include <openssl/ssl.h>
  95. static const char *curlx_usage[]={
  96. "usage: curlx args\n",
  97. " -p12 arg - tia file ",
  98. " -envpass arg - environment variable which content the tia private"
  99. " key password",
  100. " -out arg - output file (response)- default stdout",
  101. " -in arg - input file (request)- default stdin",
  102. " -connect arg - URL of the server for the connection ex:"
  103. " www.openevidence.org",
  104. " -mimetype arg - MIME type for data in ex : application/timestamp-query"
  105. " or application/dvcs -default application/timestamp-query",
  106. " -acceptmime arg - MIME type acceptable for the response ex : "
  107. "application/timestamp-response or application/dvcs -default none",
  108. " -accesstype arg - an Object identifier in an AIA/SIA method, e.g."
  109. " AD_DVCS or ad_timestamping",
  110. NULL
  111. };
  112. /*
  113. ./curlx -p12 psy.p12 -envpass XX -in request -verbose -accesstype AD_DVCS
  114. -mimetype application/dvcs -acceptmime application/dvcs -out response
  115. */
  116. /*
  117. * We use this ZERO_NULL to avoid picky compiler warnings,
  118. * when assigning a NULL pointer to a function pointer var.
  119. */
  120. #define ZERO_NULL 0
  121. /* This is a context that we pass to all callbacks */
  122. typedef struct sslctxparm_st {
  123. unsigned char *p12file;
  124. const char *pst;
  125. PKCS12 *p12;
  126. EVP_PKEY *pkey;
  127. X509 *usercert;
  128. STACK_OF(X509) * ca;
  129. CURL *curl;
  130. BIO *errorbio;
  131. int accesstype;
  132. int verbose;
  133. } sslctxparm;
  134. /* some helper function. */
  135. static char *ia5string(ASN1_IA5STRING *ia5)
  136. {
  137. char *tmp;
  138. if(!ia5 || !ia5->length)
  139. return NULL;
  140. tmp = OPENSSL_malloc(ia5->length + 1);
  141. memcpy(tmp, ia5->data, ia5->length);
  142. tmp[ia5->length] = 0;
  143. return tmp;
  144. }
  145. /* A convenience routine to get an access URI. */
  146. static unsigned char *my_get_ext(X509 *cert, const int type,
  147. int extensiontype)
  148. {
  149. int i;
  150. STACK_OF(ACCESS_DESCRIPTION) * accessinfo;
  151. accessinfo = X509_get_ext_d2i(cert, extensiontype, NULL, NULL);
  152. if(!sk_ACCESS_DESCRIPTION_num(accessinfo))
  153. 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 ia5string(ad->location->d.ia5);
  159. }
  160. return NULL;
  161. }
  162. }
  163. return NULL;
  164. }
  165. /* This is an application verification call back, it does not
  166. perform any addition verification but tries to find a URL
  167. in the presented certificate. If found, this will become
  168. the URL to be used in the POST.
  169. */
  170. static int ssl_app_verify_callback(X509_STORE_CTX *ctx, void *arg)
  171. {
  172. sslctxparm * p = (sslctxparm *) arg;
  173. int ok;
  174. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  175. X509 *cert = X509_STORE_CTX_get0_cert(ctx);
  176. #else
  177. X509 *cert = ctx->cert;
  178. #endif
  179. if(p->verbose > 2)
  180. BIO_printf(p->errorbio, "entering ssl_app_verify_callback\n");
  181. ok = X509_verify_cert(ctx);
  182. if(ok && cert) {
  183. unsigned char *accessinfo;
  184. if(p->verbose > 1)
  185. X509_print_ex(p->errorbio, cert, 0, 0);
  186. accessinfo = my_get_ext(cert, p->accesstype, NID_sinfo_access);
  187. if(accessinfo) {
  188. if(p->verbose)
  189. BIO_printf(p->errorbio, "Setting URL from SIA to: %s\n", accessinfo);
  190. curl_easy_setopt(p->curl, CURLOPT_URL, accessinfo);
  191. }
  192. else if(accessinfo = my_get_ext(cert, p->accesstype,
  193. NID_info_access)) {
  194. if(p->verbose)
  195. BIO_printf(p->errorbio, "Setting URL from AIA to: %s\n", accessinfo);
  196. curl_easy_setopt(p->curl, CURLOPT_URL, accessinfo);
  197. }
  198. }
  199. if(p->verbose > 2)
  200. BIO_printf(p->errorbio, "leaving ssl_app_verify_callback with %d\n", ok);
  201. return ok;
  202. }
  203. /* The SSL initialization callback. The callback sets:
  204. - a private key and certificate
  205. - a trusted ca certificate
  206. - a preferred cipherlist
  207. - an application verification callback (the function above)
  208. */
  209. static CURLcode sslctxfun(CURL *curl, void *sslctx, void *parm)
  210. {
  211. sslctxparm *p = (sslctxparm *) parm;
  212. SSL_CTX *ctx = (SSL_CTX *) sslctx;
  213. if(!SSL_CTX_use_certificate(ctx, p->usercert)) {
  214. BIO_printf(p->errorbio, "SSL_CTX_use_certificate problem\n");
  215. goto err;
  216. }
  217. if(!SSL_CTX_use_PrivateKey(ctx, p->pkey)) {
  218. BIO_printf(p->errorbio, "SSL_CTX_use_PrivateKey\n");
  219. goto err;
  220. }
  221. if(!SSL_CTX_check_private_key(ctx)) {
  222. BIO_printf(p->errorbio, "SSL_CTX_check_private_key\n");
  223. goto err;
  224. }
  225. SSL_CTX_set_quiet_shutdown(ctx, 1);
  226. SSL_CTX_set_cipher_list(ctx, "RC4-MD5");
  227. SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
  228. X509_STORE_add_cert(SSL_CTX_get_cert_store(ctx),
  229. sk_X509_value(p->ca, sk_X509_num(p->ca)-1));
  230. SSL_CTX_set_verify_depth(ctx, 2);
  231. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, ZERO_NULL);
  232. SSL_CTX_set_cert_verify_callback(ctx, ssl_app_verify_callback, parm);
  233. return CURLE_OK;
  234. err:
  235. ERR_print_errors(p->errorbio);
  236. return CURLE_SSL_CERTPROBLEM;
  237. }
  238. int main(int argc, char **argv)
  239. {
  240. BIO* in = NULL;
  241. BIO* out = NULL;
  242. char *outfile = NULL;
  243. char *infile = NULL;
  244. int tabLength = 100;
  245. char *binaryptr;
  246. char *mimetype = NULL;
  247. char *mimetypeaccept = NULL;
  248. char *contenttype;
  249. const char **pp;
  250. unsigned char *hostporturl = NULL;
  251. BIO *p12bio;
  252. char **args = argv + 1;
  253. unsigned char *serverurl;
  254. sslctxparm p;
  255. char *response;
  256. CURLcode res;
  257. struct curl_slist *headers = NULL;
  258. int badarg = 0;
  259. binaryptr = malloc(tabLength);
  260. memset(&p, '\0', sizeof(p));
  261. p.errorbio = BIO_new_fp(stderr, BIO_NOCLOSE);
  262. curl_global_init(CURL_GLOBAL_DEFAULT);
  263. /* we need some more for the P12 decoding */
  264. OpenSSL_add_all_ciphers();
  265. OpenSSL_add_all_digests();
  266. ERR_load_crypto_strings();
  267. while(*args && *args[0] == '-') {
  268. if(!strcmp (*args, "-in")) {
  269. if(args[1]) {
  270. infile = *(++args);
  271. }
  272. else
  273. badarg = 1;
  274. }
  275. else if(!strcmp (*args, "-out")) {
  276. if(args[1]) {
  277. outfile = *(++args);
  278. }
  279. else
  280. badarg = 1;
  281. }
  282. else if(!strcmp (*args, "-p12")) {
  283. if(args[1]) {
  284. p.p12file = *(++args);
  285. }
  286. else
  287. badarg = 1;
  288. }
  289. else if(strcmp(*args, "-envpass") == 0) {
  290. if(args[1]) {
  291. p.pst = getenv(*(++args));
  292. }
  293. else
  294. badarg = 1;
  295. }
  296. else if(strcmp(*args, "-connect") == 0) {
  297. if(args[1]) {
  298. hostporturl = *(++args);
  299. }
  300. else
  301. badarg = 1;
  302. }
  303. else if(strcmp(*args, "-mimetype") == 0) {
  304. if(args[1]) {
  305. mimetype = *(++args);
  306. }
  307. else
  308. badarg = 1;
  309. }
  310. else if(strcmp(*args, "-acceptmime") == 0) {
  311. if(args[1]) {
  312. mimetypeaccept = *(++args);
  313. }
  314. else
  315. badarg = 1;
  316. }
  317. else if(strcmp(*args, "-accesstype") == 0) {
  318. if(args[1]) {
  319. p.accesstype = OBJ_obj2nid(OBJ_txt2obj(*++args, 0));
  320. if(p.accesstype == 0)
  321. badarg = 1;
  322. }
  323. else
  324. badarg = 1;
  325. }
  326. else if(strcmp(*args, "-verbose") == 0) {
  327. p.verbose++;
  328. }
  329. else
  330. badarg = 1;
  331. args++;
  332. }
  333. if(!mimetype || !mimetypeaccept || !p.p12file)
  334. badarg = 1;
  335. if(badarg) {
  336. for(pp = curlx_usage; (*pp != NULL); pp++)
  337. BIO_printf(p.errorbio, "%s\n", *pp);
  338. BIO_printf(p.errorbio, "\n");
  339. goto err;
  340. }
  341. /* set input */
  342. in = BIO_new(BIO_s_file());
  343. if(!in) {
  344. BIO_printf(p.errorbio, "Error setting input bio\n");
  345. goto err;
  346. }
  347. else if(!infile)
  348. BIO_set_fp(in, stdin, BIO_NOCLOSE|BIO_FP_TEXT);
  349. else if(BIO_read_filename(in, infile) <= 0) {
  350. BIO_printf(p.errorbio, "Error opening input file %s\n", infile);
  351. BIO_free(in);
  352. goto err;
  353. }
  354. /* set output */
  355. out = BIO_new(BIO_s_file());
  356. if(!out) {
  357. BIO_printf(p.errorbio, "Error setting output bio.\n");
  358. goto err;
  359. }
  360. else if(!outfile)
  361. BIO_set_fp(out, stdout, BIO_NOCLOSE|BIO_FP_TEXT);
  362. else if(BIO_write_filename(out, outfile) <= 0) {
  363. BIO_printf(p.errorbio, "Error opening output file %s\n", outfile);
  364. BIO_free(out);
  365. goto err;
  366. }
  367. p.errorbio = BIO_new_fp(stderr, BIO_NOCLOSE);
  368. p.curl = curl_easy_init();
  369. if(!p.curl) {
  370. BIO_printf(p.errorbio, "Cannot init curl lib\n");
  371. goto err;
  372. }
  373. p12bio = BIO_new_file(p.p12file, "rb");
  374. if(!p12bio) {
  375. BIO_printf(p.errorbio, "Error opening P12 file %s\n", p.p12file);
  376. goto err;
  377. }
  378. p.p12 = d2i_PKCS12_bio(p12bio, NULL);
  379. if(!p.p12) {
  380. BIO_printf(p.errorbio, "Cannot decode P12 structure %s\n", p.p12file);
  381. goto err;
  382. }
  383. p.ca = NULL;
  384. if(!(PKCS12_parse (p.p12, p.pst, &(p.pkey), &(p.usercert), &(p.ca) ) )) {
  385. BIO_printf(p.errorbio, "Invalid P12 structure in %s\n", p.p12file);
  386. goto err;
  387. }
  388. if(sk_X509_num(p.ca) <= 0) {
  389. BIO_printf(p.errorbio, "No trustworthy CA given.%s\n", p.p12file);
  390. goto err;
  391. }
  392. if(p.verbose > 1)
  393. X509_print_ex(p.errorbio, p.usercert, 0, 0);
  394. /* determine URL to go */
  395. if(hostporturl) {
  396. size_t len = strlen(hostporturl) + 9;
  397. serverurl = malloc(len);
  398. snprintf(serverurl, len, "https://%s", hostporturl);
  399. }
  400. else if(p.accesstype) { /* see whether we can find an AIA or SIA for a
  401. given access type */
  402. serverurl = my_get_ext(p.usercert, p.accesstype, NID_info_access);
  403. if(!serverurl) {
  404. int j = 0;
  405. BIO_printf(p.errorbio, "no service URL in user cert "
  406. "searching in others certificates\n");
  407. for(j = 0; j<sk_X509_num(p.ca); j++) {
  408. serverurl = my_get_ext(sk_X509_value(p.ca, j), p.accesstype,
  409. NID_info_access);
  410. if(serverurl)
  411. break;
  412. serverurl = my_get_ext(sk_X509_value(p.ca, j), p.accesstype,
  413. NID_sinfo_access);
  414. if(serverurl)
  415. break;
  416. }
  417. }
  418. }
  419. if(!serverurl) {
  420. BIO_printf(p.errorbio, "no service URL in certificates,"
  421. " check '-accesstype (AD_DVCS | ad_timestamping)'"
  422. " or use '-connect'\n");
  423. goto err;
  424. }
  425. if(p.verbose)
  426. BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
  427. curl_easy_setopt(p.curl, CURLOPT_URL, serverurl);
  428. /* Now specify the POST binary data */
  429. curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
  430. curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE, (long)tabLength);
  431. /* pass our list of custom made headers */
  432. contenttype = malloc(15 + strlen(mimetype));
  433. snprintf(contenttype, 15 + strlen(mimetype), "Content-type: %s", mimetype);
  434. headers = curl_slist_append(headers, contenttype);
  435. curl_easy_setopt(p.curl, CURLOPT_HTTPHEADER, headers);
  436. if(p.verbose)
  437. BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
  438. {
  439. FILE *outfp;
  440. BIO_get_fp(out, &outfp);
  441. curl_easy_setopt(p.curl, CURLOPT_WRITEDATA, outfp);
  442. }
  443. res = curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun);
  444. if(res != CURLE_OK)
  445. BIO_printf(p.errorbio, "%d %s=%d %d\n", __LINE__,
  446. "CURLOPT_SSL_CTX_FUNCTION", CURLOPT_SSL_CTX_FUNCTION, res);
  447. curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_DATA, &p);
  448. {
  449. char *ptr;
  450. int lu; int i = 0;
  451. while((lu = BIO_read(in, &binaryptr[i], tabLength-i)) >0) {
  452. i += lu;
  453. if(i == tabLength) {
  454. tabLength += 100;
  455. ptr = realloc(binaryptr, tabLength); /* should be more careful */
  456. if(!ptr) {
  457. /* out of memory */
  458. BIO_printf(p.errorbio, "out of memory (realloc returned NULL)\n");
  459. goto fail;
  460. }
  461. binaryptr = ptr;
  462. ptr = NULL;
  463. }
  464. }
  465. tabLength = i;
  466. }
  467. /* Now specify the POST binary data */
  468. curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
  469. curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE, (long)tabLength);
  470. /* Perform the request, res will get the return code */
  471. BIO_printf(p.errorbio, "%d %s %d\n", __LINE__, "curl_easy_perform",
  472. res = curl_easy_perform(p.curl));
  473. {
  474. curl_easy_getinfo(p.curl, CURLINFO_CONTENT_TYPE, &response);
  475. if(mimetypeaccept && p.verbose) {
  476. if(!strcmp(mimetypeaccept, response))
  477. BIO_printf(p.errorbio, "the response has a correct mimetype : %s\n",
  478. response);
  479. else
  480. BIO_printf(p.errorbio, "the response doesn\'t have an acceptable "
  481. "mime type, it is %s instead of %s\n",
  482. response, mimetypeaccept);
  483. }
  484. }
  485. /*** code d'erreur si accept mime ***, egalement code return HTTP != 200 ***/
  486. /* free the header list*/
  487. fail:
  488. curl_slist_free_all(headers);
  489. /* always cleanup */
  490. curl_easy_cleanup(p.curl);
  491. BIO_free(in);
  492. BIO_free(out);
  493. return (EXIT_SUCCESS);
  494. err: BIO_printf(p.errorbio, "error");
  495. exit(1);
  496. }