crl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "apps.h"
  13. #include "progs.h"
  14. #include <openssl/bio.h>
  15. #include <openssl/err.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/x509v3.h>
  18. #include <openssl/pem.h>
  19. typedef enum OPTION_choice {
  20. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  21. OPT_INFORM, OPT_IN, OPT_OUTFORM, OPT_OUT, OPT_KEYFORM, OPT_KEY,
  22. OPT_ISSUER, OPT_LASTUPDATE, OPT_NEXTUPDATE, OPT_FINGERPRINT,
  23. OPT_CRLNUMBER, OPT_BADSIG, OPT_GENDELTA, OPT_CAPATH, OPT_CAFILE,
  24. OPT_NOCAPATH, OPT_NOCAFILE, OPT_VERIFY, OPT_TEXT, OPT_HASH, OPT_HASH_OLD,
  25. OPT_NOOUT, OPT_NAMEOPT, OPT_MD
  26. } OPTION_CHOICE;
  27. const OPTIONS crl_options[] = {
  28. {"help", OPT_HELP, '-', "Display this summary"},
  29. {"inform", OPT_INFORM, 'F', "Input format; default PEM"},
  30. {"in", OPT_IN, '<', "Input file - default stdin"},
  31. {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
  32. {"out", OPT_OUT, '>', "output file - default stdout"},
  33. {"keyform", OPT_KEYFORM, 'F', "Private key file format (PEM or ENGINE)"},
  34. {"key", OPT_KEY, '<', "CRL signing Private key to use"},
  35. {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
  36. {"lastupdate", OPT_LASTUPDATE, '-', "Set lastUpdate field"},
  37. {"nextupdate", OPT_NEXTUPDATE, '-', "Set nextUpdate field"},
  38. {"noout", OPT_NOOUT, '-', "No CRL output"},
  39. {"fingerprint", OPT_FINGERPRINT, '-', "Print the crl fingerprint"},
  40. {"crlnumber", OPT_CRLNUMBER, '-', "Print CRL number"},
  41. {"badsig", OPT_BADSIG, '-', "Corrupt last byte of loaded CRL signature (for test)" },
  42. {"gendelta", OPT_GENDELTA, '<', "Other CRL to compare/diff to the Input one"},
  43. {"CApath", OPT_CAPATH, '/', "Verify CRL using certificates in dir"},
  44. {"CAfile", OPT_CAFILE, '<', "Verify CRL using certificates in file name"},
  45. {"no-CAfile", OPT_NOCAFILE, '-',
  46. "Do not load the default certificates file"},
  47. {"no-CApath", OPT_NOCAPATH, '-',
  48. "Do not load certificates from the default certificates directory"},
  49. {"verify", OPT_VERIFY, '-', "Verify CRL signature"},
  50. {"text", OPT_TEXT, '-', "Print out a text format version"},
  51. {"hash", OPT_HASH, '-', "Print hash value"},
  52. {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
  53. {"", OPT_MD, '-', "Any supported digest"},
  54. #ifndef OPENSSL_NO_MD5
  55. {"hash_old", OPT_HASH_OLD, '-', "Print old-style (MD5) hash value"},
  56. #endif
  57. {NULL}
  58. };
  59. int crl_main(int argc, char **argv)
  60. {
  61. X509_CRL *x = NULL;
  62. BIO *out = NULL;
  63. X509_STORE *store = NULL;
  64. X509_STORE_CTX *ctx = NULL;
  65. X509_LOOKUP *lookup = NULL;
  66. X509_OBJECT *xobj = NULL;
  67. EVP_PKEY *pkey;
  68. const EVP_MD *digest = EVP_sha1();
  69. char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL;
  70. const char *CAfile = NULL, *CApath = NULL, *prog;
  71. OPTION_CHOICE o;
  72. int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout = 0;
  73. int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
  74. int ret = 1, num = 0, badsig = 0, fingerprint = 0, crlnumber = 0;
  75. int text = 0, do_ver = 0, noCAfile = 0, noCApath = 0;
  76. int i;
  77. #ifndef OPENSSL_NO_MD5
  78. int hash_old = 0;
  79. #endif
  80. prog = opt_init(argc, argv, crl_options);
  81. while ((o = opt_next()) != OPT_EOF) {
  82. switch (o) {
  83. case OPT_EOF:
  84. case OPT_ERR:
  85. opthelp:
  86. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  87. goto end;
  88. case OPT_HELP:
  89. opt_help(crl_options);
  90. ret = 0;
  91. goto end;
  92. case OPT_INFORM:
  93. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  94. goto opthelp;
  95. break;
  96. case OPT_IN:
  97. infile = opt_arg();
  98. break;
  99. case OPT_OUTFORM:
  100. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  101. goto opthelp;
  102. break;
  103. case OPT_OUT:
  104. outfile = opt_arg();
  105. break;
  106. case OPT_KEYFORM:
  107. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
  108. goto opthelp;
  109. break;
  110. case OPT_KEY:
  111. keyfile = opt_arg();
  112. break;
  113. case OPT_GENDELTA:
  114. crldiff = opt_arg();
  115. break;
  116. case OPT_CAPATH:
  117. CApath = opt_arg();
  118. do_ver = 1;
  119. break;
  120. case OPT_CAFILE:
  121. CAfile = opt_arg();
  122. do_ver = 1;
  123. break;
  124. case OPT_NOCAPATH:
  125. noCApath = 1;
  126. break;
  127. case OPT_NOCAFILE:
  128. noCAfile = 1;
  129. break;
  130. case OPT_HASH_OLD:
  131. #ifndef OPENSSL_NO_MD5
  132. hash_old = ++num;
  133. #endif
  134. break;
  135. case OPT_VERIFY:
  136. do_ver = 1;
  137. break;
  138. case OPT_TEXT:
  139. text = 1;
  140. break;
  141. case OPT_HASH:
  142. hash = ++num;
  143. break;
  144. case OPT_ISSUER:
  145. issuer = ++num;
  146. break;
  147. case OPT_LASTUPDATE:
  148. lastupdate = ++num;
  149. break;
  150. case OPT_NEXTUPDATE:
  151. nextupdate = ++num;
  152. break;
  153. case OPT_NOOUT:
  154. noout = ++num;
  155. break;
  156. case OPT_FINGERPRINT:
  157. fingerprint = ++num;
  158. break;
  159. case OPT_CRLNUMBER:
  160. crlnumber = ++num;
  161. break;
  162. case OPT_BADSIG:
  163. badsig = 1;
  164. break;
  165. case OPT_NAMEOPT:
  166. if (!set_nameopt(opt_arg()))
  167. goto opthelp;
  168. break;
  169. case OPT_MD:
  170. if (!opt_md(opt_unknown(), &digest))
  171. goto opthelp;
  172. }
  173. }
  174. argc = opt_num_rest();
  175. if (argc != 0)
  176. goto opthelp;
  177. x = load_crl(infile, informat);
  178. if (x == NULL)
  179. goto end;
  180. if (do_ver) {
  181. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
  182. goto end;
  183. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  184. if (lookup == NULL)
  185. goto end;
  186. ctx = X509_STORE_CTX_new();
  187. if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) {
  188. BIO_printf(bio_err, "Error initialising X509 store\n");
  189. goto end;
  190. }
  191. xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509,
  192. X509_CRL_get_issuer(x));
  193. if (xobj == NULL) {
  194. BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
  195. goto end;
  196. }
  197. pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj));
  198. X509_OBJECT_free(xobj);
  199. if (!pkey) {
  200. BIO_printf(bio_err, "Error getting CRL issuer public key\n");
  201. goto end;
  202. }
  203. i = X509_CRL_verify(x, pkey);
  204. EVP_PKEY_free(pkey);
  205. if (i < 0)
  206. goto end;
  207. if (i == 0)
  208. BIO_printf(bio_err, "verify failure\n");
  209. else
  210. BIO_printf(bio_err, "verify OK\n");
  211. }
  212. if (crldiff) {
  213. X509_CRL *newcrl, *delta;
  214. if (!keyfile) {
  215. BIO_puts(bio_err, "Missing CRL signing key\n");
  216. goto end;
  217. }
  218. newcrl = load_crl(crldiff, informat);
  219. if (!newcrl)
  220. goto end;
  221. pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
  222. if (!pkey) {
  223. X509_CRL_free(newcrl);
  224. goto end;
  225. }
  226. delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
  227. X509_CRL_free(newcrl);
  228. EVP_PKEY_free(pkey);
  229. if (delta) {
  230. X509_CRL_free(x);
  231. x = delta;
  232. } else {
  233. BIO_puts(bio_err, "Error creating delta CRL\n");
  234. goto end;
  235. }
  236. }
  237. if (badsig) {
  238. const ASN1_BIT_STRING *sig;
  239. X509_CRL_get0_signature(x, &sig, NULL);
  240. corrupt_signature(sig);
  241. }
  242. if (num) {
  243. for (i = 1; i <= num; i++) {
  244. if (issuer == i) {
  245. print_name(bio_out, "issuer=", X509_CRL_get_issuer(x),
  246. get_nameopt());
  247. }
  248. if (crlnumber == i) {
  249. ASN1_INTEGER *crlnum;
  250. crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
  251. BIO_printf(bio_out, "crlNumber=");
  252. if (crlnum) {
  253. i2a_ASN1_INTEGER(bio_out, crlnum);
  254. ASN1_INTEGER_free(crlnum);
  255. } else
  256. BIO_puts(bio_out, "<NONE>");
  257. BIO_printf(bio_out, "\n");
  258. }
  259. if (hash == i) {
  260. BIO_printf(bio_out, "%08lx\n",
  261. X509_NAME_hash(X509_CRL_get_issuer(x)));
  262. }
  263. #ifndef OPENSSL_NO_MD5
  264. if (hash_old == i) {
  265. BIO_printf(bio_out, "%08lx\n",
  266. X509_NAME_hash_old(X509_CRL_get_issuer(x)));
  267. }
  268. #endif
  269. if (lastupdate == i) {
  270. BIO_printf(bio_out, "lastUpdate=");
  271. ASN1_TIME_print(bio_out, X509_CRL_get0_lastUpdate(x));
  272. BIO_printf(bio_out, "\n");
  273. }
  274. if (nextupdate == i) {
  275. BIO_printf(bio_out, "nextUpdate=");
  276. if (X509_CRL_get0_nextUpdate(x))
  277. ASN1_TIME_print(bio_out, X509_CRL_get0_nextUpdate(x));
  278. else
  279. BIO_printf(bio_out, "NONE");
  280. BIO_printf(bio_out, "\n");
  281. }
  282. if (fingerprint == i) {
  283. int j;
  284. unsigned int n;
  285. unsigned char md[EVP_MAX_MD_SIZE];
  286. if (!X509_CRL_digest(x, digest, md, &n)) {
  287. BIO_printf(bio_err, "out of memory\n");
  288. goto end;
  289. }
  290. BIO_printf(bio_out, "%s Fingerprint=",
  291. OBJ_nid2sn(EVP_MD_type(digest)));
  292. for (j = 0; j < (int)n; j++) {
  293. BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
  294. ? '\n' : ':');
  295. }
  296. }
  297. }
  298. }
  299. out = bio_open_default(outfile, 'w', outformat);
  300. if (out == NULL)
  301. goto end;
  302. if (text)
  303. X509_CRL_print_ex(out, x, get_nameopt());
  304. if (noout) {
  305. ret = 0;
  306. goto end;
  307. }
  308. if (outformat == FORMAT_ASN1)
  309. i = (int)i2d_X509_CRL_bio(out, x);
  310. else
  311. i = PEM_write_bio_X509_CRL(out, x);
  312. if (!i) {
  313. BIO_printf(bio_err, "unable to write CRL\n");
  314. goto end;
  315. }
  316. ret = 0;
  317. end:
  318. if (ret != 0)
  319. ERR_print_errors(bio_err);
  320. BIO_free_all(out);
  321. X509_CRL_free(x);
  322. X509_STORE_CTX_free(ctx);
  323. X509_STORE_free(store);
  324. return ret;
  325. }