x509.c 43 KB

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