x509.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /*
  2. * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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/asn1.h>
  16. #include <openssl/err.h>
  17. #include <openssl/bn.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/x509.h>
  20. #include <openssl/x509v3.h>
  21. #include <openssl/objects.h>
  22. #include <openssl/pem.h>
  23. #include <openssl/rsa.h>
  24. #ifndef OPENSSL_NO_DSA
  25. # include <openssl/dsa.h>
  26. #endif
  27. #include "internal/e_os.h" /* For isatty() */
  28. #undef POSTFIX
  29. #define POSTFIX ".srl"
  30. #define DEFAULT_DAYS 30 /* default certificate validity period in days */
  31. #define UNSET_DAYS -2 /* -1 may be used for testing expiration checks */
  32. #define EXT_COPY_UNSET -1
  33. static int callb(int ok, X509_STORE_CTX *ctx);
  34. static ASN1_INTEGER *x509_load_serial(const char *CAfile,
  35. const char *serialfile, int create);
  36. static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
  37. static int print_x509v3_exts(BIO *bio, X509 *x, const char *ext_names);
  38. typedef enum OPTION_choice {
  39. OPT_COMMON,
  40. OPT_INFORM, OPT_OUTFORM, OPT_KEYFORM, OPT_REQ, OPT_CAFORM,
  41. OPT_CAKEYFORM, OPT_VFYOPT, OPT_SIGOPT, OPT_DAYS, OPT_PASSIN, OPT_EXTFILE,
  42. OPT_EXTENSIONS, OPT_IN, OPT_OUT, OPT_KEY, OPT_SIGNKEY, OPT_CA, OPT_CAKEY,
  43. OPT_CASERIAL, OPT_SET_SERIAL, OPT_NEW, OPT_FORCE_PUBKEY, OPT_ISSU, OPT_SUBJ,
  44. OPT_ADDTRUST, OPT_ADDREJECT, OPT_SETALIAS, OPT_CERTOPT, OPT_DATEOPT, OPT_NAMEOPT,
  45. OPT_EMAIL, OPT_OCSP_URI, OPT_SERIAL, OPT_NEXT_SERIAL,
  46. OPT_MODULUS, OPT_PUBKEY, OPT_X509TOREQ, OPT_TEXT, OPT_HASH,
  47. OPT_ISSUER_HASH, OPT_SUBJECT, OPT_ISSUER, OPT_FINGERPRINT, OPT_DATES,
  48. OPT_PURPOSE, OPT_STARTDATE, OPT_ENDDATE, OPT_CHECKEND, OPT_CHECKHOST,
  49. OPT_CHECKEMAIL, OPT_CHECKIP, OPT_NOOUT, OPT_TRUSTOUT, OPT_CLRTRUST,
  50. OPT_CLRREJECT, OPT_ALIAS, OPT_CACREATESERIAL, OPT_CLREXT, OPT_OCSPID,
  51. OPT_SUBJECT_HASH_OLD, OPT_ISSUER_HASH_OLD, OPT_COPY_EXTENSIONS,
  52. OPT_BADSIG, OPT_MD, OPT_ENGINE, OPT_NOCERT, OPT_PRESERVE_DATES,
  53. OPT_NOT_BEFORE, OPT_NOT_AFTER,
  54. OPT_R_ENUM, OPT_PROV_ENUM, OPT_EXT
  55. } OPTION_CHOICE;
  56. const OPTIONS x509_options[] = {
  57. OPT_SECTION("General"),
  58. {"help", OPT_HELP, '-', "Display this summary"},
  59. {"in", OPT_IN, '<',
  60. "Certificate input, or CSR input file with -req (default stdin)"},
  61. {"passin", OPT_PASSIN, 's', "Private key and cert file pass-phrase source"},
  62. {"new", OPT_NEW, '-', "Generate a certificate from scratch"},
  63. {"x509toreq", OPT_X509TOREQ, '-',
  64. "Output a certification request (rather than a certificate)"},
  65. {"req", OPT_REQ, '-', "Input is a CSR file (rather than a certificate)"},
  66. {"copy_extensions", OPT_COPY_EXTENSIONS, 's',
  67. "copy extensions when converting from CSR to x509 or vice versa"},
  68. {"inform", OPT_INFORM, 'f',
  69. "CSR input format to use (PEM or DER; by default try PEM first)"},
  70. {"vfyopt", OPT_VFYOPT, 's', "CSR verification parameter in n:v form"},
  71. {"key", OPT_KEY, 's',
  72. "Key for signing, and to include unless using -force_pubkey"},
  73. {"signkey", OPT_SIGNKEY, 's',
  74. "Same as -key"},
  75. {"keyform", OPT_KEYFORM, 'E',
  76. "Key input format (ENGINE, other values ignored)"},
  77. {"out", OPT_OUT, '>', "Output file - default stdout"},
  78. {"outform", OPT_OUTFORM, 'f',
  79. "Output format (DER or PEM) - default PEM"},
  80. {"nocert", OPT_NOCERT, '-',
  81. "No cert output (except for requested printing)"},
  82. {"noout", OPT_NOOUT, '-', "No output (except for requested printing)"},
  83. OPT_SECTION("Certificate printing"),
  84. {"text", OPT_TEXT, '-', "Print the certificate in text form"},
  85. {"dateopt", OPT_DATEOPT, 's',
  86. "Datetime format used for printing. (rfc_822/iso_8601). Default is rfc_822."},
  87. {"certopt", OPT_CERTOPT, 's', "Various certificate text printing options"},
  88. {"fingerprint", OPT_FINGERPRINT, '-', "Print the certificate fingerprint"},
  89. {"alias", OPT_ALIAS, '-', "Print certificate alias"},
  90. {"serial", OPT_SERIAL, '-', "Print serial number value"},
  91. {"startdate", OPT_STARTDATE, '-', "Print the notBefore field"},
  92. {"enddate", OPT_ENDDATE, '-', "Print the notAfter field"},
  93. {"dates", OPT_DATES, '-', "Print both notBefore and notAfter fields"},
  94. {"subject", OPT_SUBJECT, '-', "Print subject DN"},
  95. {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
  96. {"nameopt", OPT_NAMEOPT, 's',
  97. "Certificate subject/issuer name printing options"},
  98. {"email", OPT_EMAIL, '-', "Print email address(es)"},
  99. {"hash", OPT_HASH, '-', "Synonym for -subject_hash (for backward compat)"},
  100. {"subject_hash", OPT_HASH, '-', "Print subject hash value"},
  101. #ifndef OPENSSL_NO_MD5
  102. {"subject_hash_old", OPT_SUBJECT_HASH_OLD, '-',
  103. "Print old-style (MD5) subject hash value"},
  104. #endif
  105. {"issuer_hash", OPT_ISSUER_HASH, '-', "Print issuer hash value"},
  106. #ifndef OPENSSL_NO_MD5
  107. {"issuer_hash_old", OPT_ISSUER_HASH_OLD, '-',
  108. "Print old-style (MD5) issuer hash value"},
  109. #endif
  110. {"ext", OPT_EXT, 's',
  111. "Restrict which X.509 extensions to print and/or copy"},
  112. {"ocspid", OPT_OCSPID, '-',
  113. "Print OCSP hash values for the subject name and public key"},
  114. {"ocsp_uri", OPT_OCSP_URI, '-', "Print OCSP Responder URL(s)"},
  115. {"purpose", OPT_PURPOSE, '-', "Print out certificate purposes"},
  116. {"pubkey", OPT_PUBKEY, '-', "Print the public key in PEM format"},
  117. {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
  118. OPT_SECTION("Certificate checking"),
  119. {"checkend", OPT_CHECKEND, 'M',
  120. "Check whether cert expires in the next arg seconds"},
  121. {OPT_MORE_STR, 1, 1, "Exit 1 (failure) if so, 0 if not"},
  122. {"checkhost", OPT_CHECKHOST, 's', "Check certificate matches host"},
  123. {"checkemail", OPT_CHECKEMAIL, 's', "Check certificate matches email"},
  124. {"checkip", OPT_CHECKIP, 's', "Check certificate matches ipaddr"},
  125. OPT_SECTION("Certificate output"),
  126. {"set_serial", OPT_SET_SERIAL, 's',
  127. "Serial number to use, overrides -CAserial"},
  128. {"next_serial", OPT_NEXT_SERIAL, '-',
  129. "Increment current certificate serial number"},
  130. {"not_before", OPT_NOT_BEFORE, 's',
  131. "[CC]YYMMDDHHMMSSZ value for notBefore certificate field"},
  132. {"not_after", OPT_NOT_AFTER, 's',
  133. "[CC]YYMMDDHHMMSSZ value for notAfter certificate field, overrides -days"},
  134. {"days", OPT_DAYS, 'n',
  135. "Number of days until newly generated certificate expires - default 30"},
  136. {"preserve_dates", OPT_PRESERVE_DATES, '-',
  137. "Preserve existing validity dates"},
  138. {"set_issuer", OPT_ISSU, 's', "Set or override certificate issuer"},
  139. {"set_subject", OPT_SUBJ, 's', "Set or override certificate subject (and issuer)"},
  140. {"subj", OPT_SUBJ, 's', "Alias for -set_subject"},
  141. {"force_pubkey", OPT_FORCE_PUBKEY, '<',
  142. "Key to be placed in new certificate or certificate request"},
  143. {"clrext", OPT_CLREXT, '-',
  144. "Do not take over any extensions from the source certificate or request"},
  145. {"extfile", OPT_EXTFILE, '<', "Config file with X509V3 extensions to add"},
  146. {"extensions", OPT_EXTENSIONS, 's',
  147. "Section of extfile to use - default: unnamed section"},
  148. {"sigopt", OPT_SIGOPT, 's', "Signature parameter, in n:v form"},
  149. {"badsig", OPT_BADSIG, '-',
  150. "Corrupt last byte of certificate signature (for test)"},
  151. {"", OPT_MD, '-', "Any supported digest, used for signing and printing"},
  152. OPT_SECTION("Micro-CA"),
  153. {"CA", OPT_CA, '<',
  154. "Use the given CA certificate, conflicts with -key"},
  155. {"CAform", OPT_CAFORM, 'F', "CA cert format (PEM/DER/P12); has no effect"},
  156. {"CAkey", OPT_CAKEY, 's', "The corresponding CA key; default is -CA arg"},
  157. {"CAkeyform", OPT_CAKEYFORM, 'E',
  158. "CA key format (ENGINE, other values ignored)"},
  159. {"CAserial", OPT_CASERIAL, 's',
  160. "File that keeps track of CA-generated serial number"},
  161. {"CAcreateserial", OPT_CACREATESERIAL, '-',
  162. "Create CA serial number file if it does not exist"},
  163. OPT_SECTION("Certificate trust output"),
  164. {"trustout", OPT_TRUSTOUT, '-', "Mark certificate PEM output as trusted"},
  165. {"setalias", OPT_SETALIAS, 's', "Set certificate alias (nickname)"},
  166. {"clrtrust", OPT_CLRTRUST, '-', "Clear all trusted purposes"},
  167. {"addtrust", OPT_ADDTRUST, 's', "Trust certificate for a given purpose"},
  168. {"clrreject", OPT_CLRREJECT, '-',
  169. "Clears all the prohibited or rejected uses of the certificate"},
  170. {"addreject", OPT_ADDREJECT, 's',
  171. "Reject certificate for a given purpose"},
  172. OPT_R_OPTIONS,
  173. #ifndef OPENSSL_NO_ENGINE
  174. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  175. #endif
  176. OPT_PROV_OPTIONS,
  177. {NULL}
  178. };
  179. static void warn_copying(ASN1_OBJECT *excluded, const char *names)
  180. {
  181. const char *sn = OBJ_nid2sn(OBJ_obj2nid(excluded));
  182. if (names != NULL && strstr(names, sn) != NULL)
  183. BIO_printf(bio_err,
  184. "Warning: -ext should not specify copying %s extension to CSR; ignoring this\n",
  185. sn);
  186. }
  187. static X509_REQ *x509_to_req(X509 *cert, int ext_copy, const char *names)
  188. {
  189. const STACK_OF(X509_EXTENSION) *cert_exts = X509_get0_extensions(cert);
  190. int i, n = sk_X509_EXTENSION_num(cert_exts /* may be NULL */);
  191. ASN1_OBJECT *skid = OBJ_nid2obj(NID_subject_key_identifier);
  192. ASN1_OBJECT *akid = OBJ_nid2obj(NID_authority_key_identifier);
  193. STACK_OF(X509_EXTENSION) *exts;
  194. X509_REQ *req = X509_to_X509_REQ(cert, NULL, NULL);
  195. if (req == NULL)
  196. return NULL;
  197. /*
  198. * Filter out SKID and AKID extensions, which make no sense in a CSR.
  199. * If names is not NULL, copy only those extensions listed there.
  200. */
  201. warn_copying(skid, names);
  202. warn_copying(akid, names);
  203. if ((exts = sk_X509_EXTENSION_new_reserve(NULL, n)) == NULL)
  204. goto err;
  205. for (i = 0; i < n; i++) {
  206. X509_EXTENSION *ex = sk_X509_EXTENSION_value(cert_exts, i);
  207. ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex);
  208. if (OBJ_cmp(obj, skid) != 0 && OBJ_cmp(obj, akid) != 0
  209. && !sk_X509_EXTENSION_push(exts, ex))
  210. goto err;
  211. }
  212. if (sk_X509_EXTENSION_num(exts) > 0) {
  213. if (ext_copy != EXT_COPY_UNSET && ext_copy != EXT_COPY_NONE
  214. && !X509_REQ_add_extensions(req, exts)) {
  215. BIO_printf(bio_err, "Error copying extensions from certificate\n");
  216. goto err;
  217. }
  218. }
  219. sk_X509_EXTENSION_free(exts);
  220. return req;
  221. err:
  222. sk_X509_EXTENSION_free(exts);
  223. X509_REQ_free(req);
  224. return NULL;
  225. }
  226. static int self_signed(X509_STORE *ctx, X509 *cert)
  227. {
  228. X509_STORE_CTX *xsc = X509_STORE_CTX_new();
  229. int ret = 0;
  230. if (xsc == NULL || !X509_STORE_CTX_init(xsc, ctx, cert, NULL)) {
  231. BIO_printf(bio_err, "Error initialising X509 store\n");
  232. } else {
  233. X509_STORE_CTX_set_flags(xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
  234. ret = X509_verify_cert(xsc) > 0;
  235. }
  236. X509_STORE_CTX_free(xsc);
  237. return ret;
  238. }
  239. int x509_main(int argc, char **argv)
  240. {
  241. ASN1_INTEGER *sno = NULL;
  242. ASN1_OBJECT *objtmp = NULL;
  243. BIO *out = NULL;
  244. CONF *extconf = NULL;
  245. int ext_copy = EXT_COPY_UNSET;
  246. X509V3_CTX ext_ctx;
  247. EVP_PKEY *privkey = NULL, *CAkey = NULL, *pubkey = NULL;
  248. EVP_PKEY *pkey;
  249. int newcert = 0;
  250. char *issu = NULL, *subj = NULL, *digest = NULL;
  251. X509_NAME *fissu = NULL, *fsubj = NULL;
  252. const unsigned long chtype = MBSTRING_ASC;
  253. const int multirdn = 1;
  254. STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
  255. STACK_OF(OPENSSL_STRING) *sigopts = NULL, *vfyopts = NULL;
  256. X509 *x = NULL, *xca = NULL, *issuer_cert;
  257. X509_REQ *req = NULL, *rq = NULL;
  258. X509_STORE *ctx = NULL;
  259. char *CAkeyfile = NULL, *CAserial = NULL, *pubkeyfile = NULL, *alias = NULL;
  260. char *checkhost = NULL, *checkemail = NULL, *checkip = NULL;
  261. char *ext_names = NULL;
  262. char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL;
  263. char *infile = NULL, *outfile = NULL, *privkeyfile = NULL, *CAfile = NULL;
  264. char *prog, *not_before = NULL, *not_after = NULL;
  265. int days = UNSET_DAYS; /* not explicitly set */
  266. int x509toreq = 0, modulus = 0, print_pubkey = 0, pprint = 0;
  267. int CAformat = FORMAT_UNDEF, CAkeyformat = FORMAT_UNDEF;
  268. unsigned long dateopt = ASN1_DTFLGS_RFC822;
  269. int fingerprint = 0, reqfile = 0, checkend = 0;
  270. int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, keyformat = FORMAT_UNDEF;
  271. int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0;
  272. int noout = 0, CA_createserial = 0, email = 0;
  273. int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0;
  274. int ret = 1, i, j, num = 0, badsig = 0, clrext = 0, nocert = 0;
  275. int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0, ext = 0;
  276. int enddate = 0;
  277. time_t checkoffset = 0;
  278. unsigned long certflag = 0;
  279. int preserve_dates = 0;
  280. OPTION_CHOICE o;
  281. ENGINE *e = NULL;
  282. #ifndef OPENSSL_NO_MD5
  283. int subject_hash_old = 0, issuer_hash_old = 0;
  284. #endif
  285. ctx = X509_STORE_new();
  286. if (ctx == NULL)
  287. goto err;
  288. X509_STORE_set_verify_cb(ctx, callb);
  289. opt_set_unknown_name("digest");
  290. prog = opt_init(argc, argv, x509_options);
  291. while ((o = opt_next()) != OPT_EOF) {
  292. switch (o) {
  293. case OPT_EOF:
  294. case OPT_ERR:
  295. opthelp:
  296. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  297. goto err;
  298. case OPT_HELP:
  299. opt_help(x509_options);
  300. ret = 0;
  301. goto end;
  302. case OPT_INFORM:
  303. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  304. goto opthelp;
  305. break;
  306. case OPT_IN:
  307. infile = opt_arg();
  308. break;
  309. case OPT_OUTFORM:
  310. if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
  311. goto opthelp;
  312. break;
  313. case OPT_KEYFORM:
  314. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
  315. goto opthelp;
  316. break;
  317. case OPT_CAFORM:
  318. if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAformat))
  319. goto opthelp;
  320. break;
  321. case OPT_CAKEYFORM:
  322. if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAkeyformat))
  323. goto opthelp;
  324. break;
  325. case OPT_OUT:
  326. outfile = opt_arg();
  327. break;
  328. case OPT_REQ:
  329. reqfile = 1;
  330. break;
  331. case OPT_DATEOPT:
  332. if (!set_dateopt(&dateopt, opt_arg())) {
  333. BIO_printf(bio_err,
  334. "Invalid date format: %s\n", opt_arg());
  335. goto err;
  336. }
  337. break;
  338. case OPT_COPY_EXTENSIONS:
  339. if (!set_ext_copy(&ext_copy, opt_arg())) {
  340. BIO_printf(bio_err,
  341. "Invalid extension copy option: %s\n", opt_arg());
  342. goto err;
  343. }
  344. break;
  345. case OPT_SIGOPT:
  346. if (!sigopts)
  347. sigopts = sk_OPENSSL_STRING_new_null();
  348. if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
  349. goto opthelp;
  350. break;
  351. case OPT_VFYOPT:
  352. if (!vfyopts)
  353. vfyopts = sk_OPENSSL_STRING_new_null();
  354. if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg()))
  355. goto opthelp;
  356. break;
  357. case OPT_NOT_BEFORE:
  358. not_before = opt_arg();
  359. break;
  360. case OPT_NOT_AFTER:
  361. not_after = opt_arg();
  362. break;
  363. case OPT_DAYS:
  364. days = atoi(opt_arg());
  365. if (days <= UNSET_DAYS) {
  366. BIO_printf(bio_err, "%s: -days parameter arg must be >= -1\n",
  367. prog);
  368. goto err;
  369. }
  370. break;
  371. case OPT_PASSIN:
  372. passinarg = opt_arg();
  373. break;
  374. case OPT_EXTFILE:
  375. extfile = opt_arg();
  376. break;
  377. case OPT_R_CASES:
  378. if (!opt_rand(o))
  379. goto end;
  380. break;
  381. case OPT_PROV_CASES:
  382. if (!opt_provider(o))
  383. goto end;
  384. break;
  385. case OPT_EXTENSIONS:
  386. extsect = opt_arg();
  387. break;
  388. case OPT_KEY:
  389. case OPT_SIGNKEY:
  390. privkeyfile = opt_arg();
  391. break;
  392. case OPT_CA:
  393. CAfile = opt_arg();
  394. break;
  395. case OPT_CAKEY:
  396. CAkeyfile = opt_arg();
  397. break;
  398. case OPT_CASERIAL:
  399. CAserial = opt_arg();
  400. break;
  401. case OPT_SET_SERIAL:
  402. if (sno != NULL) {
  403. BIO_printf(bio_err, "Serial number supplied twice\n");
  404. goto opthelp;
  405. }
  406. if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)
  407. goto opthelp;
  408. break;
  409. case OPT_NEW:
  410. newcert = 1;
  411. break;
  412. case OPT_FORCE_PUBKEY:
  413. pubkeyfile = opt_arg();
  414. break;
  415. case OPT_ISSU:
  416. issu = opt_arg();
  417. break;
  418. case OPT_SUBJ:
  419. subj = opt_arg();
  420. break;
  421. case OPT_ADDTRUST:
  422. if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)
  423. goto end;
  424. if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
  425. BIO_printf(bio_err, "%s: Invalid trust object value %s\n",
  426. prog, opt_arg());
  427. goto opthelp;
  428. }
  429. sk_ASN1_OBJECT_push(trust, objtmp);
  430. trustout = 1;
  431. break;
  432. case OPT_ADDREJECT:
  433. if (reject == NULL && (reject = sk_ASN1_OBJECT_new_null()) == NULL)
  434. goto end;
  435. if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
  436. BIO_printf(bio_err, "%s: Invalid reject object value %s\n",
  437. prog, opt_arg());
  438. goto opthelp;
  439. }
  440. sk_ASN1_OBJECT_push(reject, objtmp);
  441. trustout = 1;
  442. break;
  443. case OPT_SETALIAS:
  444. alias = opt_arg();
  445. trustout = 1;
  446. break;
  447. case OPT_CERTOPT:
  448. if (!set_cert_ex(&certflag, opt_arg()))
  449. goto opthelp;
  450. break;
  451. case OPT_NAMEOPT:
  452. if (!set_nameopt(opt_arg()))
  453. goto opthelp;
  454. break;
  455. case OPT_ENGINE:
  456. e = setup_engine(opt_arg(), 0);
  457. break;
  458. case OPT_EMAIL:
  459. email = ++num;
  460. break;
  461. case OPT_OCSP_URI:
  462. ocsp_uri = ++num;
  463. break;
  464. case OPT_SERIAL:
  465. serial = ++num;
  466. break;
  467. case OPT_NEXT_SERIAL:
  468. next_serial = ++num;
  469. break;
  470. case OPT_MODULUS:
  471. modulus = ++num;
  472. break;
  473. case OPT_PUBKEY:
  474. print_pubkey = ++num;
  475. break;
  476. case OPT_X509TOREQ:
  477. x509toreq = 1;
  478. break;
  479. case OPT_TEXT:
  480. text = ++num;
  481. break;
  482. case OPT_SUBJECT:
  483. subject = ++num;
  484. break;
  485. case OPT_ISSUER:
  486. issuer = ++num;
  487. break;
  488. case OPT_FINGERPRINT:
  489. fingerprint = ++num;
  490. break;
  491. case OPT_HASH:
  492. subject_hash = ++num;
  493. break;
  494. case OPT_ISSUER_HASH:
  495. issuer_hash = ++num;
  496. break;
  497. case OPT_PURPOSE:
  498. pprint = ++num;
  499. break;
  500. case OPT_STARTDATE:
  501. startdate = ++num;
  502. break;
  503. case OPT_ENDDATE:
  504. enddate = ++num;
  505. break;
  506. case OPT_NOOUT:
  507. noout = ++num;
  508. break;
  509. case OPT_EXT:
  510. ext = ++num;
  511. ext_names = opt_arg();
  512. break;
  513. case OPT_NOCERT:
  514. nocert = 1;
  515. break;
  516. case OPT_TRUSTOUT:
  517. trustout = 1;
  518. break;
  519. case OPT_CLRTRUST:
  520. clrtrust = ++num;
  521. break;
  522. case OPT_CLRREJECT:
  523. clrreject = ++num;
  524. break;
  525. case OPT_ALIAS:
  526. aliasout = ++num;
  527. break;
  528. case OPT_CACREATESERIAL:
  529. CA_createserial = 1;
  530. break;
  531. case OPT_CLREXT:
  532. clrext = 1;
  533. break;
  534. case OPT_OCSPID:
  535. ocspid = ++num;
  536. break;
  537. case OPT_BADSIG:
  538. badsig = 1;
  539. break;
  540. #ifndef OPENSSL_NO_MD5
  541. case OPT_SUBJECT_HASH_OLD:
  542. subject_hash_old = ++num;
  543. break;
  544. case OPT_ISSUER_HASH_OLD:
  545. issuer_hash_old = ++num;
  546. break;
  547. #else
  548. case OPT_SUBJECT_HASH_OLD:
  549. case OPT_ISSUER_HASH_OLD:
  550. break;
  551. #endif
  552. case OPT_DATES:
  553. startdate = ++num;
  554. enddate = ++num;
  555. break;
  556. case OPT_CHECKEND:
  557. checkend = 1;
  558. {
  559. ossl_intmax_t temp = 0;
  560. if (!opt_intmax(opt_arg(), &temp))
  561. goto opthelp;
  562. checkoffset = (time_t)temp;
  563. if ((ossl_intmax_t)checkoffset != temp) {
  564. BIO_printf(bio_err, "%s: Checkend time out of range %s\n",
  565. prog, opt_arg());
  566. goto opthelp;
  567. }
  568. }
  569. break;
  570. case OPT_CHECKHOST:
  571. checkhost = opt_arg();
  572. break;
  573. case OPT_CHECKEMAIL:
  574. checkemail = opt_arg();
  575. break;
  576. case OPT_CHECKIP:
  577. checkip = opt_arg();
  578. break;
  579. case OPT_PRESERVE_DATES:
  580. preserve_dates = 1;
  581. break;
  582. case OPT_MD:
  583. digest = opt_unknown();
  584. break;
  585. }
  586. }
  587. /* No extra arguments. */
  588. if (!opt_check_rest_arg(NULL))
  589. goto opthelp;
  590. if (!app_RAND_load())
  591. goto end;
  592. if (!opt_check_md(digest))
  593. goto opthelp;
  594. if (preserve_dates && not_before != NULL) {
  595. BIO_printf(bio_err, "Cannot use -preserve_dates with -not_before option\n");
  596. goto err;
  597. }
  598. if (preserve_dates && not_after != NULL) {
  599. BIO_printf(bio_err, "Cannot use -preserve_dates with -not_after option\n");
  600. goto err;
  601. }
  602. if (preserve_dates && days != UNSET_DAYS) {
  603. BIO_printf(bio_err, "Cannot use -preserve_dates with -days option\n");
  604. goto err;
  605. }
  606. if (days == UNSET_DAYS)
  607. days = DEFAULT_DAYS;
  608. else if (not_after != NULL)
  609. BIO_printf(bio_err, "Warning: -not_after option overriding -days option\n");
  610. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  611. BIO_printf(bio_err, "Error getting password\n");
  612. goto err;
  613. }
  614. if (!X509_STORE_set_default_paths_ex(ctx, app_get0_libctx(),
  615. app_get0_propq()))
  616. goto end;
  617. if (newcert && infile != NULL) {
  618. BIO_printf(bio_err, "The -in option cannot be used with -new\n");
  619. goto err;
  620. }
  621. if (newcert && reqfile) {
  622. BIO_printf(bio_err, "The -req option cannot be used with -new\n");
  623. goto err;
  624. }
  625. if (privkeyfile != NULL) {
  626. privkey = load_key(privkeyfile, keyformat, 0, passin, e, "private key");
  627. if (privkey == NULL)
  628. goto end;
  629. }
  630. if (pubkeyfile != NULL) {
  631. if ((pubkey = load_pubkey(pubkeyfile, keyformat, 0, NULL, e,
  632. "explicitly set public key")) == NULL)
  633. goto end;
  634. }
  635. if (newcert) {
  636. if (subj == NULL) {
  637. BIO_printf(bio_err,
  638. "The -new option requires a subject to be set using -subj\n");
  639. goto err;
  640. }
  641. if (privkeyfile == NULL && pubkeyfile == NULL) {
  642. BIO_printf(bio_err,
  643. "The -new option requires using the -key or -force_pubkey option\n");
  644. goto err;
  645. }
  646. }
  647. if (issu != NULL
  648. && (fissu = parse_name(issu, chtype, multirdn, "issuer")) == NULL)
  649. goto end;
  650. if (subj != NULL
  651. && (fsubj = parse_name(subj, chtype, multirdn, "subject")) == NULL)
  652. goto end;
  653. if (CAkeyfile == NULL)
  654. CAkeyfile = CAfile;
  655. if (CAfile != NULL) {
  656. if (privkeyfile != NULL) {
  657. BIO_printf(bio_err, "Cannot use both -key/-signkey and -CA option\n");
  658. goto err;
  659. }
  660. } else {
  661. #define WARN_NO_CA(opt) BIO_printf(bio_err, \
  662. "Warning: ignoring " opt " option since -CA option is not given\n");
  663. if (CAkeyfile != NULL)
  664. WARN_NO_CA("-CAkey");
  665. if (CAkeyformat != FORMAT_UNDEF)
  666. WARN_NO_CA("-CAkeyform");
  667. if (CAformat != FORMAT_UNDEF)
  668. WARN_NO_CA("-CAform");
  669. if (CAserial != NULL)
  670. WARN_NO_CA("-CAserial");
  671. if (CA_createserial)
  672. WARN_NO_CA("-CAcreateserial");
  673. }
  674. if (extfile == NULL) {
  675. if (extsect != NULL)
  676. BIO_printf(bio_err,
  677. "Warning: ignoring -extensions option without -extfile\n");
  678. } else {
  679. X509V3_CTX ctx2;
  680. if ((extconf = app_load_config(extfile)) == NULL)
  681. goto end;
  682. if (extsect == NULL) {
  683. extsect = app_conf_try_string(extconf, "default", "extensions");
  684. if (extsect == NULL)
  685. extsect = "default";
  686. }
  687. X509V3_set_ctx_test(&ctx2);
  688. X509V3_set_nconf(&ctx2, extconf);
  689. if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
  690. BIO_printf(bio_err,
  691. "Error checking extension section %s\n", extsect);
  692. goto err;
  693. }
  694. }
  695. if (reqfile) {
  696. if (infile == NULL && isatty(fileno_stdin()))
  697. BIO_printf(bio_err,
  698. "Warning: Reading cert request from stdin since no -in option is given\n");
  699. req = load_csr_autofmt(infile, informat, vfyopts,
  700. "certificate request input");
  701. if (req == NULL)
  702. goto end;
  703. if ((pkey = X509_REQ_get0_pubkey(req)) == NULL) {
  704. BIO_printf(bio_err, "Error unpacking public key from CSR\n");
  705. goto err;
  706. }
  707. i = do_X509_REQ_verify(req, pkey, vfyopts);
  708. if (i <= 0) {
  709. BIO_printf(bio_err, i < 0
  710. ? "Error while verifying certificate request self-signature\n"
  711. : "Certificate request self-signature did not match the contents\n");
  712. goto err;
  713. }
  714. BIO_printf(bio_err, "Certificate request self-signature ok\n");
  715. print_name(bio_err, "subject=", X509_REQ_get_subject_name(req));
  716. } else if (!x509toreq && ext_copy != EXT_COPY_UNSET) {
  717. BIO_printf(bio_err, "Warning: ignoring -copy_extensions since neither -x509toreq nor -req is given\n");
  718. }
  719. if (reqfile || newcert) {
  720. if (preserve_dates)
  721. BIO_printf(bio_err,
  722. "Warning: ignoring -preserve_dates option with -req or -new\n");
  723. preserve_dates = 0;
  724. if (privkeyfile == NULL && CAkeyfile == NULL) {
  725. BIO_printf(bio_err,
  726. "We need a private key to sign with, use -key or -CAkey or -CA with private key\n");
  727. goto err;
  728. }
  729. if ((x = X509_new_ex(app_get0_libctx(), app_get0_propq())) == NULL)
  730. goto end;
  731. if (CAfile == NULL && sno == NULL) {
  732. sno = ASN1_INTEGER_new();
  733. if (sno == NULL || !rand_serial(NULL, sno))
  734. goto end;
  735. }
  736. if (req != NULL && ext_copy != EXT_COPY_UNSET) {
  737. if (clrext && ext_copy != EXT_COPY_NONE) {
  738. BIO_printf(bio_err, "Must not use -clrext together with -copy_extensions\n");
  739. goto err;
  740. } else if (!copy_extensions(x, req, ext_copy)) {
  741. BIO_printf(bio_err, "Error copying extensions from request\n");
  742. goto err;
  743. }
  744. }
  745. } else {
  746. if (infile == NULL && isatty(fileno_stdin()))
  747. BIO_printf(bio_err,
  748. "Warning: Reading certificate from stdin since no -in or -new option is given\n");
  749. x = load_cert_pass(infile, informat, 1, passin, "certificate");
  750. if (x == NULL)
  751. goto end;
  752. }
  753. if ((fsubj != NULL || req != NULL)
  754. && !X509_set_subject_name(x, fsubj != NULL ? fsubj :
  755. X509_REQ_get_subject_name(req)))
  756. goto end;
  757. if ((pubkey != NULL || privkey != NULL || req != NULL)
  758. && !X509_set_pubkey(x, pubkey != NULL ? pubkey :
  759. privkey != NULL ? privkey :
  760. X509_REQ_get0_pubkey(req)))
  761. goto end;
  762. if (CAfile != NULL) {
  763. xca = load_cert_pass(CAfile, CAformat, 1, passin, "CA certificate");
  764. if (xca == NULL)
  765. goto end;
  766. }
  767. out = bio_open_default(outfile, 'w', outformat);
  768. if (out == NULL)
  769. goto end;
  770. if (alias)
  771. X509_alias_set1(x, (unsigned char *)alias, -1);
  772. if (clrtrust)
  773. X509_trust_clear(x);
  774. if (clrreject)
  775. X509_reject_clear(x);
  776. if (trust != NULL) {
  777. for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++)
  778. X509_add1_trust_object(x, sk_ASN1_OBJECT_value(trust, i));
  779. }
  780. if (reject != NULL) {
  781. for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++)
  782. X509_add1_reject_object(x, sk_ASN1_OBJECT_value(reject, i));
  783. }
  784. if (clrext && ext_names != NULL)
  785. BIO_printf(bio_err, "Warning: Ignoring -ext since -clrext is given\n");
  786. for (i = X509_get_ext_count(x) - 1; i >= 0; i--) {
  787. X509_EXTENSION *ex = X509_get_ext(x, i);
  788. const char *sn = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ex)));
  789. if (clrext || (ext_names != NULL && strstr(ext_names, sn) == NULL))
  790. X509_EXTENSION_free(X509_delete_ext(x, i));
  791. }
  792. issuer_cert = x;
  793. if (CAfile != NULL) {
  794. issuer_cert = xca;
  795. if (sno == NULL)
  796. sno = x509_load_serial(CAfile, CAserial, CA_createserial);
  797. if (sno == NULL)
  798. goto end;
  799. if (!x509toreq && !reqfile && !newcert && !self_signed(ctx, x))
  800. goto end;
  801. } else {
  802. if (privkey != NULL && !cert_matches_key(x, privkey))
  803. BIO_printf(bio_err,
  804. "Warning: Signature key and public key of cert do not match\n");
  805. }
  806. if (sno != NULL && !X509_set_serialNumber(x, sno))
  807. goto end;
  808. if (reqfile || newcert || privkey != NULL || CAfile != NULL) {
  809. if (!preserve_dates && !set_cert_times(x, not_before, not_after, days, 1))
  810. goto end;
  811. if (fissu != NULL) {
  812. if (!X509_set_issuer_name(x, fissu))
  813. goto end;
  814. } else {
  815. if (!X509_set_issuer_name(x, X509_get_subject_name(issuer_cert)))
  816. goto end;
  817. }
  818. }
  819. X509V3_set_ctx(&ext_ctx, issuer_cert, x, NULL, NULL, X509V3_CTX_REPLACE);
  820. /* prepare fallback for AKID, but only if issuer cert equals subject cert */
  821. if (CAfile == NULL) {
  822. if (!X509V3_set_issuer_pkey(&ext_ctx, privkey))
  823. goto end;
  824. }
  825. if (extconf != NULL && !x509toreq) {
  826. X509V3_set_nconf(&ext_ctx, extconf);
  827. if (!X509V3_EXT_add_nconf(extconf, &ext_ctx, extsect, x)) {
  828. BIO_printf(bio_err,
  829. "Error adding extensions from section %s\n", extsect);
  830. goto err;
  831. }
  832. }
  833. /* At this point the contents of the certificate x have been finished. */
  834. pkey = X509_get0_pubkey(x);
  835. if ((print_pubkey != 0 || modulus != 0) && pkey == NULL) {
  836. BIO_printf(bio_err, "Error getting public key\n");
  837. goto err;
  838. }
  839. if (x509toreq) { /* also works in conjunction with -req */
  840. if (privkey == NULL) {
  841. BIO_printf(bio_err, "Must specify request signing key using -key\n");
  842. goto err;
  843. }
  844. if (clrext && ext_copy != EXT_COPY_NONE) {
  845. BIO_printf(bio_err, "Must not use -clrext together with -copy_extensions\n");
  846. goto err;
  847. }
  848. if ((rq = x509_to_req(x, ext_copy, ext_names)) == NULL)
  849. goto end;
  850. if (extconf != NULL) {
  851. X509V3_set_nconf(&ext_ctx, extconf);
  852. if (!X509V3_EXT_REQ_add_nconf(extconf, &ext_ctx, extsect, rq)) {
  853. BIO_printf(bio_err,
  854. "Error adding request extensions from section %s\n", extsect);
  855. goto err;
  856. }
  857. }
  858. if (!do_X509_REQ_sign(rq, privkey, digest, sigopts))
  859. goto end;
  860. if (!noout) {
  861. if (outformat == FORMAT_ASN1) {
  862. X509_REQ_print_ex(out, rq, get_nameopt(), X509_FLAG_COMPAT);
  863. i = i2d_X509_bio(out, x);
  864. } else {
  865. i = PEM_write_bio_X509_REQ(out, rq);
  866. }
  867. if (!i) {
  868. BIO_printf(bio_err,
  869. "Unable to write certificate request\n");
  870. goto err;
  871. }
  872. }
  873. noout = 1;
  874. } else if (CAfile != NULL) {
  875. if ((CAkey = load_key(CAkeyfile, CAkeyformat,
  876. 0, passin, e, "CA private key")) == NULL)
  877. goto end;
  878. if (!X509_check_private_key(xca, CAkey)) {
  879. BIO_printf(bio_err,
  880. "CA certificate and CA private key do not match\n");
  881. goto err;
  882. }
  883. if (!do_X509_sign(x, 0, CAkey, digest, sigopts, &ext_ctx))
  884. goto end;
  885. } else if (privkey != NULL) {
  886. if (!do_X509_sign(x, 0, privkey, digest, sigopts, &ext_ctx))
  887. goto end;
  888. }
  889. if (badsig) {
  890. const ASN1_BIT_STRING *signature;
  891. X509_get0_signature(&signature, NULL, x);
  892. corrupt_signature(signature);
  893. }
  894. /* Process print options in the given order, as indicated by index i */
  895. for (i = 1; i <= num; i++) {
  896. if (i == issuer) {
  897. print_name(out, "issuer=", X509_get_issuer_name(x));
  898. } else if (i == subject) {
  899. print_name(out, "subject=", X509_get_subject_name(x));
  900. } else if (i == serial) {
  901. BIO_printf(out, "serial=");
  902. i2a_ASN1_INTEGER(out, X509_get0_serialNumber(x));
  903. BIO_printf(out, "\n");
  904. } else if (i == next_serial) {
  905. ASN1_INTEGER *ser;
  906. BIGNUM *bnser = ASN1_INTEGER_to_BN(X509_get0_serialNumber(x), NULL);
  907. if (bnser == NULL)
  908. goto end;
  909. if (!BN_add_word(bnser, 1)
  910. || (ser = BN_to_ASN1_INTEGER(bnser, NULL)) == NULL) {
  911. BN_free(bnser);
  912. goto end;
  913. }
  914. BN_free(bnser);
  915. i2a_ASN1_INTEGER(out, ser);
  916. ASN1_INTEGER_free(ser);
  917. BIO_puts(out, "\n");
  918. } else if (i == email || i == ocsp_uri) {
  919. STACK_OF(OPENSSL_STRING) *emlst =
  920. i == email ? X509_get1_email(x) : X509_get1_ocsp(x);
  921. for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
  922. BIO_printf(out, "%s\n", sk_OPENSSL_STRING_value(emlst, j));
  923. X509_email_free(emlst);
  924. } else if (i == aliasout) {
  925. unsigned char *alstr = X509_alias_get0(x, NULL);
  926. if (alstr)
  927. BIO_printf(out, "%s\n", alstr);
  928. else
  929. BIO_puts(out, "<No Alias>\n");
  930. } else if (i == subject_hash) {
  931. BIO_printf(out, "%08lx\n", X509_subject_name_hash(x));
  932. #ifndef OPENSSL_NO_MD5
  933. } else if (i == subject_hash_old) {
  934. BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x));
  935. #endif
  936. } else if (i == issuer_hash) {
  937. BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x));
  938. #ifndef OPENSSL_NO_MD5
  939. } else if (i == issuer_hash_old) {
  940. BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x));
  941. #endif
  942. } else if (i == pprint) {
  943. BIO_printf(out, "Certificate purposes:\n");
  944. for (j = 0; j < X509_PURPOSE_get_count(); j++)
  945. purpose_print(out, x, X509_PURPOSE_get0(j));
  946. } else if (i == modulus) {
  947. BIO_printf(out, "Modulus=");
  948. if (EVP_PKEY_is_a(pkey, "RSA") || EVP_PKEY_is_a(pkey, "RSA-PSS")) {
  949. BIGNUM *n = NULL;
  950. /* Every RSA key has an 'n' */
  951. EVP_PKEY_get_bn_param(pkey, "n", &n);
  952. BN_print(out, n);
  953. BN_free(n);
  954. } else if (EVP_PKEY_is_a(pkey, "DSA")) {
  955. BIGNUM *dsapub = NULL;
  956. /* Every DSA key has a 'pub' */
  957. EVP_PKEY_get_bn_param(pkey, "pub", &dsapub);
  958. BN_print(out, dsapub);
  959. BN_free(dsapub);
  960. } else {
  961. BIO_printf(out, "No modulus for this public key type");
  962. }
  963. BIO_printf(out, "\n");
  964. } else if (i == print_pubkey) {
  965. PEM_write_bio_PUBKEY(out, pkey);
  966. } else if (i == text) {
  967. X509_print_ex(out, x, get_nameopt(), certflag);
  968. } else if (i == startdate) {
  969. BIO_puts(out, "notBefore=");
  970. ASN1_TIME_print_ex(out, X509_get0_notBefore(x), dateopt);
  971. BIO_puts(out, "\n");
  972. } else if (i == enddate) {
  973. BIO_puts(out, "notAfter=");
  974. ASN1_TIME_print_ex(out, X509_get0_notAfter(x), dateopt);
  975. BIO_puts(out, "\n");
  976. } else if (i == fingerprint) {
  977. unsigned int n;
  978. unsigned char md[EVP_MAX_MD_SIZE];
  979. const char *fdigname = digest;
  980. EVP_MD *fdig;
  981. int digres;
  982. if (fdigname == NULL)
  983. fdigname = "SHA1";
  984. if ((fdig = EVP_MD_fetch(app_get0_libctx(), fdigname,
  985. app_get0_propq())) == NULL) {
  986. BIO_printf(bio_err, "Unknown digest\n");
  987. goto err;
  988. }
  989. digres = X509_digest(x, fdig, md, &n);
  990. EVP_MD_free(fdig);
  991. if (!digres) {
  992. BIO_printf(bio_err, "Out of memory\n");
  993. goto err;
  994. }
  995. BIO_printf(out, "%s Fingerprint=", fdigname);
  996. for (j = 0; j < (int)n; j++)
  997. BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n) ? '\n' : ':');
  998. } else if (i == ocspid) {
  999. X509_ocspid_print(out, x);
  1000. } else if (i == ext) {
  1001. print_x509v3_exts(out, x, ext_names);
  1002. }
  1003. }
  1004. if (checkend) {
  1005. time_t tcheck = time(NULL) + checkoffset;
  1006. ret = X509_cmp_time(X509_get0_notAfter(x), &tcheck) < 0;
  1007. if (ret)
  1008. BIO_printf(out, "Certificate will expire\n");
  1009. else
  1010. BIO_printf(out, "Certificate will not expire\n");
  1011. goto end;
  1012. }
  1013. if (!check_cert_attributes(out, x, checkhost, checkemail, checkip, 1))
  1014. goto err;
  1015. if (noout || nocert) {
  1016. ret = 0;
  1017. goto end;
  1018. }
  1019. if (outformat == FORMAT_ASN1) {
  1020. i = i2d_X509_bio(out, x);
  1021. } else if (outformat == FORMAT_PEM) {
  1022. if (trustout)
  1023. i = PEM_write_bio_X509_AUX(out, x);
  1024. else
  1025. i = PEM_write_bio_X509(out, x);
  1026. } else {
  1027. BIO_printf(bio_err, "Bad output format specified for outfile\n");
  1028. goto err;
  1029. }
  1030. if (!i) {
  1031. BIO_printf(bio_err, "Unable to write certificate\n");
  1032. goto err;
  1033. }
  1034. ret = 0;
  1035. goto end;
  1036. err:
  1037. ERR_print_errors(bio_err);
  1038. end:
  1039. NCONF_free(extconf);
  1040. BIO_free_all(out);
  1041. X509_STORE_free(ctx);
  1042. X509_NAME_free(fissu);
  1043. X509_NAME_free(fsubj);
  1044. X509_REQ_free(req);
  1045. X509_free(x);
  1046. X509_free(xca);
  1047. EVP_PKEY_free(privkey);
  1048. EVP_PKEY_free(CAkey);
  1049. EVP_PKEY_free(pubkey);
  1050. sk_OPENSSL_STRING_free(sigopts);
  1051. sk_OPENSSL_STRING_free(vfyopts);
  1052. X509_REQ_free(rq);
  1053. ASN1_INTEGER_free(sno);
  1054. sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
  1055. sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
  1056. release_engine(e);
  1057. clear_free(passin);
  1058. return ret;
  1059. }
  1060. static ASN1_INTEGER *x509_load_serial(const char *CAfile,
  1061. const char *serialfile, int create)
  1062. {
  1063. char *buf = NULL;
  1064. ASN1_INTEGER *bs = NULL;
  1065. BIGNUM *serial = NULL;
  1066. int defaultfile = 0, file_exists;
  1067. if (serialfile == NULL) {
  1068. const char *p = strrchr(CAfile, '.');
  1069. size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
  1070. buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");
  1071. memcpy(buf, CAfile, len);
  1072. memcpy(buf + len, POSTFIX, sizeof(POSTFIX));
  1073. serialfile = buf;
  1074. defaultfile = 1;
  1075. }
  1076. serial = load_serial(serialfile, &file_exists, create || defaultfile, NULL);
  1077. if (serial == NULL)
  1078. goto end;
  1079. if (!BN_add_word(serial, 1)) {
  1080. BIO_printf(bio_err, "Serial number increment failure\n");
  1081. goto end;
  1082. }
  1083. if (file_exists || create)
  1084. save_serial(serialfile, NULL, serial, &bs);
  1085. else
  1086. bs = BN_to_ASN1_INTEGER(serial, NULL);
  1087. end:
  1088. OPENSSL_free(buf);
  1089. BN_free(serial);
  1090. return bs;
  1091. }
  1092. static int callb(int ok, X509_STORE_CTX *ctx)
  1093. {
  1094. int err;
  1095. X509 *err_cert;
  1096. /*
  1097. * It is ok to use a self-signed certificate. This case will catch both
  1098. * the initial ok == 0 and the final ok == 1 calls to this function.
  1099. */
  1100. err = X509_STORE_CTX_get_error(ctx);
  1101. if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
  1102. return 1;
  1103. if (!ok) {
  1104. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  1105. print_name(bio_err, "subject=", X509_get_subject_name(err_cert));
  1106. BIO_printf(bio_err,
  1107. "Error with certificate - error %d at depth %d\n%s\n", err,
  1108. X509_STORE_CTX_get_error_depth(ctx),
  1109. X509_verify_cert_error_string(err));
  1110. return 1;
  1111. }
  1112. return 1;
  1113. }
  1114. static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
  1115. {
  1116. int id, i, idret;
  1117. const char *pname;
  1118. id = X509_PURPOSE_get_id(pt);
  1119. pname = X509_PURPOSE_get0_name(pt);
  1120. for (i = 0; i < 2; i++) {
  1121. idret = X509_check_purpose(cert, id, i);
  1122. BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
  1123. if (idret == 1)
  1124. BIO_printf(bio, "Yes\n");
  1125. else if (idret == 0)
  1126. BIO_printf(bio, "No\n");
  1127. else
  1128. BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
  1129. }
  1130. return 1;
  1131. }
  1132. static int parse_ext_names(char *names, const char **result)
  1133. {
  1134. char *p, *q;
  1135. int cnt = 0, len = 0;
  1136. p = q = names;
  1137. len = strlen(names);
  1138. while (q - names <= len) {
  1139. if (*q != ',' && *q != '\0') {
  1140. q++;
  1141. continue;
  1142. }
  1143. if (p != q) {
  1144. /* found */
  1145. if (result != NULL) {
  1146. result[cnt] = p;
  1147. *q = '\0';
  1148. }
  1149. cnt++;
  1150. }
  1151. p = ++q;
  1152. }
  1153. return cnt;
  1154. }
  1155. static int print_x509v3_exts(BIO *bio, X509 *x, const char *ext_names)
  1156. {
  1157. const STACK_OF(X509_EXTENSION) *exts = NULL;
  1158. STACK_OF(X509_EXTENSION) *exts2 = NULL;
  1159. X509_EXTENSION *ext = NULL;
  1160. ASN1_OBJECT *obj;
  1161. int i, j, ret = 0, num, nn = 0;
  1162. const char *sn, **names = NULL;
  1163. char *tmp_ext_names = NULL;
  1164. exts = X509_get0_extensions(x);
  1165. if ((num = sk_X509_EXTENSION_num(exts)) <= 0) {
  1166. BIO_printf(bio_err, "No extensions in certificate\n");
  1167. ret = 1;
  1168. goto end;
  1169. }
  1170. /* parse comma separated ext name string */
  1171. if ((tmp_ext_names = OPENSSL_strdup(ext_names)) == NULL)
  1172. goto end;
  1173. if ((nn = parse_ext_names(tmp_ext_names, NULL)) == 0) {
  1174. BIO_printf(bio, "Invalid extension names: %s\n", ext_names);
  1175. goto end;
  1176. }
  1177. if ((names = OPENSSL_malloc(sizeof(char *) * nn)) == NULL)
  1178. goto end;
  1179. parse_ext_names(tmp_ext_names, names);
  1180. for (i = 0; i < num; i++) {
  1181. ext = sk_X509_EXTENSION_value(exts, i);
  1182. /* check if this ext is what we want */
  1183. obj = X509_EXTENSION_get_object(ext);
  1184. sn = OBJ_nid2sn(OBJ_obj2nid(obj));
  1185. if (sn == NULL || strcmp(sn, "UNDEF") == 0)
  1186. continue;
  1187. for (j = 0; j < nn; j++) {
  1188. if (strcmp(sn, names[j]) == 0) {
  1189. /* push the extension into a new stack */
  1190. if (exts2 == NULL
  1191. && (exts2 = sk_X509_EXTENSION_new_null()) == NULL)
  1192. goto end;
  1193. if (!sk_X509_EXTENSION_push(exts2, ext))
  1194. goto end;
  1195. }
  1196. }
  1197. }
  1198. if (!sk_X509_EXTENSION_num(exts2)) {
  1199. BIO_printf(bio, "No extensions matched with %s\n", ext_names);
  1200. ret = 1;
  1201. goto end;
  1202. }
  1203. ret = X509V3_extensions_print(bio, NULL, exts2, 0, 0);
  1204. end:
  1205. sk_X509_EXTENSION_free(exts2);
  1206. OPENSSL_free(names);
  1207. OPENSSL_free(tmp_ext_names);
  1208. return ret;
  1209. }