x509.c 43 KB

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