x509.c 43 KB

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