2
0

x509.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * Copyright 1995-2022 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 end;
  279. X509_STORE_set_verify_cb(ctx, callb);
  280. prog = opt_init(argc, argv, x509_options);
  281. while ((o = opt_next()) != OPT_EOF) {
  282. switch (o) {
  283. case OPT_EOF:
  284. case OPT_ERR:
  285. opthelp:
  286. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  287. goto end;
  288. case OPT_HELP:
  289. opt_help(x509_options);
  290. ret = 0;
  291. goto end;
  292. case OPT_INFORM:
  293. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  294. goto opthelp;
  295. break;
  296. case OPT_IN:
  297. infile = opt_arg();
  298. break;
  299. case OPT_OUTFORM:
  300. if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
  301. goto opthelp;
  302. break;
  303. case OPT_KEYFORM:
  304. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
  305. goto opthelp;
  306. break;
  307. case OPT_CAFORM:
  308. if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAformat))
  309. goto opthelp;
  310. break;
  311. case OPT_CAKEYFORM:
  312. if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAkeyformat))
  313. goto opthelp;
  314. break;
  315. case OPT_OUT:
  316. outfile = opt_arg();
  317. break;
  318. case OPT_REQ:
  319. reqfile = 1;
  320. break;
  321. case OPT_DATEOPT:
  322. if (!set_dateopt(&dateopt, opt_arg())) {
  323. BIO_printf(bio_err,
  324. "Invalid date format: %s\n", opt_arg());
  325. goto end;
  326. }
  327. break;
  328. case OPT_COPY_EXTENSIONS:
  329. if (!set_ext_copy(&ext_copy, opt_arg())) {
  330. BIO_printf(bio_err,
  331. "Invalid extension copy option: %s\n", opt_arg());
  332. goto end;
  333. }
  334. break;
  335. case OPT_SIGOPT:
  336. if (!sigopts)
  337. sigopts = sk_OPENSSL_STRING_new_null();
  338. if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
  339. goto opthelp;
  340. break;
  341. case OPT_VFYOPT:
  342. if (!vfyopts)
  343. vfyopts = sk_OPENSSL_STRING_new_null();
  344. if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg()))
  345. goto opthelp;
  346. break;
  347. case OPT_DAYS:
  348. days = atoi(opt_arg());
  349. if (days < -1) {
  350. BIO_printf(bio_err, "%s: -days parameter arg must be >= -1\n",
  351. prog);
  352. goto end;
  353. }
  354. break;
  355. case OPT_PASSIN:
  356. passinarg = opt_arg();
  357. break;
  358. case OPT_EXTFILE:
  359. extfile = opt_arg();
  360. break;
  361. case OPT_R_CASES:
  362. if (!opt_rand(o))
  363. goto end;
  364. break;
  365. case OPT_PROV_CASES:
  366. if (!opt_provider(o))
  367. goto end;
  368. break;
  369. case OPT_EXTENSIONS:
  370. extsect = opt_arg();
  371. break;
  372. case OPT_KEY:
  373. case OPT_SIGNKEY:
  374. privkeyfile = opt_arg();
  375. break;
  376. case OPT_CA:
  377. CAfile = opt_arg();
  378. break;
  379. case OPT_CAKEY:
  380. CAkeyfile = opt_arg();
  381. break;
  382. case OPT_CASERIAL:
  383. CAserial = opt_arg();
  384. break;
  385. case OPT_SET_SERIAL:
  386. if (sno != NULL) {
  387. BIO_printf(bio_err, "Serial number supplied twice\n");
  388. goto opthelp;
  389. }
  390. if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)
  391. goto opthelp;
  392. break;
  393. case OPT_NEW:
  394. newcert = 1;
  395. break;
  396. case OPT_FORCE_PUBKEY:
  397. pubkeyfile = opt_arg();
  398. break;
  399. case OPT_SUBJ:
  400. subj = opt_arg();
  401. break;
  402. case OPT_ADDTRUST:
  403. if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)
  404. goto end;
  405. if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
  406. BIO_printf(bio_err, "%s: Invalid trust object value %s\n",
  407. prog, opt_arg());
  408. goto opthelp;
  409. }
  410. sk_ASN1_OBJECT_push(trust, objtmp);
  411. trustout = 1;
  412. break;
  413. case OPT_ADDREJECT:
  414. if (reject == NULL && (reject = sk_ASN1_OBJECT_new_null()) == NULL)
  415. goto end;
  416. if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
  417. BIO_printf(bio_err, "%s: Invalid reject object value %s\n",
  418. prog, opt_arg());
  419. goto opthelp;
  420. }
  421. sk_ASN1_OBJECT_push(reject, objtmp);
  422. trustout = 1;
  423. break;
  424. case OPT_SETALIAS:
  425. alias = opt_arg();
  426. trustout = 1;
  427. break;
  428. case OPT_CERTOPT:
  429. if (!set_cert_ex(&certflag, opt_arg()))
  430. goto opthelp;
  431. break;
  432. case OPT_NAMEOPT:
  433. if (!set_nameopt(opt_arg()))
  434. goto opthelp;
  435. break;
  436. case OPT_ENGINE:
  437. e = setup_engine(opt_arg(), 0);
  438. break;
  439. case OPT_EMAIL:
  440. email = ++num;
  441. break;
  442. case OPT_OCSP_URI:
  443. ocsp_uri = ++num;
  444. break;
  445. case OPT_SERIAL:
  446. serial = ++num;
  447. break;
  448. case OPT_NEXT_SERIAL:
  449. next_serial = ++num;
  450. break;
  451. case OPT_MODULUS:
  452. modulus = ++num;
  453. break;
  454. case OPT_PUBKEY:
  455. print_pubkey = ++num;
  456. break;
  457. case OPT_X509TOREQ:
  458. x509toreq = 1;
  459. break;
  460. case OPT_TEXT:
  461. text = ++num;
  462. break;
  463. case OPT_SUBJECT:
  464. subject = ++num;
  465. break;
  466. case OPT_ISSUER:
  467. issuer = ++num;
  468. break;
  469. case OPT_FINGERPRINT:
  470. fingerprint = ++num;
  471. break;
  472. case OPT_HASH:
  473. subject_hash = ++num;
  474. break;
  475. case OPT_ISSUER_HASH:
  476. issuer_hash = ++num;
  477. break;
  478. case OPT_PURPOSE:
  479. pprint = ++num;
  480. break;
  481. case OPT_STARTDATE:
  482. startdate = ++num;
  483. break;
  484. case OPT_ENDDATE:
  485. enddate = ++num;
  486. break;
  487. case OPT_NOOUT:
  488. noout = ++num;
  489. break;
  490. case OPT_EXT:
  491. ext = ++num;
  492. ext_names = opt_arg();
  493. break;
  494. case OPT_NOCERT:
  495. nocert = 1;
  496. break;
  497. case OPT_TRUSTOUT:
  498. trustout = 1;
  499. break;
  500. case OPT_CLRTRUST:
  501. clrtrust = ++num;
  502. break;
  503. case OPT_CLRREJECT:
  504. clrreject = ++num;
  505. break;
  506. case OPT_ALIAS:
  507. aliasout = ++num;
  508. break;
  509. case OPT_CACREATESERIAL:
  510. CA_createserial = 1;
  511. break;
  512. case OPT_CLREXT:
  513. clrext = 1;
  514. break;
  515. case OPT_OCSPID:
  516. ocspid = ++num;
  517. break;
  518. case OPT_BADSIG:
  519. badsig = 1;
  520. break;
  521. #ifndef OPENSSL_NO_MD5
  522. case OPT_SUBJECT_HASH_OLD:
  523. subject_hash_old = ++num;
  524. break;
  525. case OPT_ISSUER_HASH_OLD:
  526. issuer_hash_old = ++num;
  527. break;
  528. #else
  529. case OPT_SUBJECT_HASH_OLD:
  530. case OPT_ISSUER_HASH_OLD:
  531. break;
  532. #endif
  533. case OPT_DATES:
  534. startdate = ++num;
  535. enddate = ++num;
  536. break;
  537. case OPT_CHECKEND:
  538. checkend = 1;
  539. {
  540. ossl_intmax_t temp = 0;
  541. if (!opt_intmax(opt_arg(), &temp))
  542. goto opthelp;
  543. checkoffset = (time_t)temp;
  544. if ((ossl_intmax_t)checkoffset != temp) {
  545. BIO_printf(bio_err, "%s: Checkend time out of range %s\n",
  546. prog, opt_arg());
  547. goto opthelp;
  548. }
  549. }
  550. break;
  551. case OPT_CHECKHOST:
  552. checkhost = opt_arg();
  553. break;
  554. case OPT_CHECKEMAIL:
  555. checkemail = opt_arg();
  556. break;
  557. case OPT_CHECKIP:
  558. checkip = opt_arg();
  559. break;
  560. case OPT_PRESERVE_DATES:
  561. preserve_dates = 1;
  562. break;
  563. case OPT_MD:
  564. digest = opt_unknown();
  565. break;
  566. }
  567. }
  568. /* No extra arguments. */
  569. argc = opt_num_rest();
  570. if (argc != 0)
  571. goto opthelp;
  572. if (!app_RAND_load())
  573. goto end;
  574. if (preserve_dates && days != UNSET_DAYS) {
  575. BIO_printf(bio_err, "Cannot use -preserve_dates with -days option\n");
  576. goto end;
  577. }
  578. if (days == UNSET_DAYS)
  579. days = DEFAULT_DAYS;
  580. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  581. BIO_printf(bio_err, "Error getting password\n");
  582. goto end;
  583. }
  584. if (!X509_STORE_set_default_paths_ex(ctx, app_get0_libctx(),
  585. app_get0_propq()))
  586. goto end;
  587. if (newcert && infile != NULL) {
  588. BIO_printf(bio_err, "The -in option cannot be used with -new\n");
  589. goto end;
  590. }
  591. if (newcert && reqfile) {
  592. BIO_printf(bio_err,
  593. "The -req option cannot be used with -new\n");
  594. goto end;
  595. }
  596. if (privkeyfile != NULL) {
  597. privkey = load_key(privkeyfile, keyformat, 0, passin, e, "private key");
  598. if (privkey == NULL)
  599. goto end;
  600. }
  601. if (pubkeyfile != NULL) {
  602. if ((pubkey = load_pubkey(pubkeyfile, keyformat, 0, NULL, e,
  603. "explicitly set public key")) == NULL)
  604. goto end;
  605. }
  606. if (newcert) {
  607. if (subj == NULL) {
  608. BIO_printf(bio_err,
  609. "The -new option requires a subject to be set using -subj\n");
  610. goto end;
  611. }
  612. if (privkeyfile == NULL && pubkeyfile == NULL) {
  613. BIO_printf(bio_err,
  614. "The -new option requires using the -key or -force_pubkey option\n");
  615. goto end;
  616. }
  617. }
  618. if (subj != NULL
  619. && (fsubj = parse_name(subj, chtype, multirdn, "subject")) == NULL)
  620. goto end;
  621. if (CAkeyfile == NULL)
  622. CAkeyfile = CAfile;
  623. if (CAfile != NULL) {
  624. if (privkeyfile != NULL) {
  625. BIO_printf(bio_err, "Cannot use both -key/-signkey and -CA option\n");
  626. goto end;
  627. }
  628. } else {
  629. #define WARN_NO_CA(opt) BIO_printf(bio_err, \
  630. "Warning: ignoring " opt " option since -CA option is not given\n");
  631. if (CAkeyfile != NULL)
  632. WARN_NO_CA("-CAkey");
  633. if (CAkeyformat != FORMAT_UNDEF)
  634. WARN_NO_CA("-CAkeyform");
  635. if (CAformat != FORMAT_UNDEF)
  636. WARN_NO_CA("-CAform");
  637. if (CAserial != NULL)
  638. WARN_NO_CA("-CAserial");
  639. if (CA_createserial)
  640. WARN_NO_CA("-CAcreateserial");
  641. }
  642. if (extfile == NULL) {
  643. if (extsect != NULL)
  644. BIO_printf(bio_err,
  645. "Warning: ignoring -extensions option without -extfile\n");
  646. } else {
  647. X509V3_CTX ctx2;
  648. if ((extconf = app_load_config(extfile)) == NULL)
  649. goto end;
  650. if (extsect == NULL) {
  651. extsect = NCONF_get_string(extconf, "default", "extensions");
  652. if (extsect == NULL) {
  653. ERR_clear_error();
  654. extsect = "default";
  655. }
  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 end;
  663. }
  664. }
  665. if (reqfile) {
  666. req = load_csr(infile, informat, "certificate request input");
  667. if (req == NULL)
  668. goto end;
  669. if ((pkey = X509_REQ_get0_pubkey(req)) == NULL) {
  670. BIO_printf(bio_err, "Error unpacking public key from CSR\n");
  671. goto end;
  672. }
  673. i = do_X509_REQ_verify(req, pkey, vfyopts);
  674. if (i <= 0) {
  675. BIO_printf(bio_err, i < 0
  676. ? "Error while verifying certificate request self-signature\n"
  677. : "Certificate request self-signature did not match the contents\n");
  678. goto end;
  679. }
  680. BIO_printf(bio_err, "Certificate request self-signature ok\n");
  681. print_name(bio_err, "subject=", X509_REQ_get_subject_name(req));
  682. } else if (!x509toreq && ext_copy != EXT_COPY_UNSET) {
  683. BIO_printf(bio_err, "Warning: ignoring -copy_extensions since neither -x509toreq nor -req is given\n");
  684. }
  685. if (reqfile || newcert) {
  686. if (preserve_dates)
  687. BIO_printf(bio_err,
  688. "Warning: ignoring -preserve_dates option with -req or -new\n");
  689. preserve_dates = 0;
  690. if (privkeyfile == NULL && CAkeyfile == NULL) {
  691. BIO_printf(bio_err,
  692. "We need a private key to sign with, use -key or -CAkey or -CA with private key\n");
  693. goto end;
  694. }
  695. if ((x = X509_new_ex(app_get0_libctx(), app_get0_propq())) == NULL)
  696. goto end;
  697. if (CAfile == NULL && sno == NULL) {
  698. sno = ASN1_INTEGER_new();
  699. if (sno == NULL || !rand_serial(NULL, sno))
  700. goto end;
  701. }
  702. if (req != NULL && ext_copy != EXT_COPY_UNSET) {
  703. if (clrext && ext_copy != EXT_COPY_NONE) {
  704. BIO_printf(bio_err, "Must not use -clrext together with -copy_extensions\n");
  705. goto end;
  706. } else if (!copy_extensions(x, req, ext_copy)) {
  707. BIO_printf(bio_err, "Error copying extensions from request\n");
  708. goto end;
  709. }
  710. }
  711. } else {
  712. x = load_cert_pass(infile, informat, 1, passin, "certificate");
  713. if (x == NULL)
  714. goto end;
  715. }
  716. if ((fsubj != NULL || req != NULL)
  717. && !X509_set_subject_name(x, fsubj != NULL ? fsubj :
  718. X509_REQ_get_subject_name(req)))
  719. goto end;
  720. if ((pubkey != NULL || privkey != NULL || req != NULL)
  721. && !X509_set_pubkey(x, pubkey != NULL ? pubkey :
  722. privkey != NULL ? privkey :
  723. X509_REQ_get0_pubkey(req)))
  724. goto end;
  725. if (CAfile != NULL) {
  726. xca = load_cert_pass(CAfile, CAformat, 1, passin, "CA certificate");
  727. if (xca == NULL)
  728. goto end;
  729. }
  730. out = bio_open_default(outfile, 'w', outformat);
  731. if (out == NULL)
  732. goto end;
  733. if (!noout || text || next_serial)
  734. OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
  735. if (alias)
  736. X509_alias_set1(x, (unsigned char *)alias, -1);
  737. if (clrtrust)
  738. X509_trust_clear(x);
  739. if (clrreject)
  740. X509_reject_clear(x);
  741. if (trust != NULL) {
  742. for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++)
  743. X509_add1_trust_object(x, sk_ASN1_OBJECT_value(trust, i));
  744. }
  745. if (reject != NULL) {
  746. for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++)
  747. X509_add1_reject_object(x, sk_ASN1_OBJECT_value(reject, i));
  748. }
  749. if (clrext && ext_names != NULL)
  750. BIO_printf(bio_err, "Warning: Ignoring -ext since -clrext is given\n");
  751. for (i = X509_get_ext_count(x) - 1; i >= 0; i--) {
  752. X509_EXTENSION *ex = X509_get_ext(x, i);
  753. const char *sn = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ex)));
  754. if (clrext || (ext_names != NULL && strstr(ext_names, sn) == NULL))
  755. X509_EXTENSION_free(X509_delete_ext(x, i));
  756. }
  757. issuer_cert = x;
  758. if (CAfile != NULL) {
  759. issuer_cert = xca;
  760. if (sno == NULL)
  761. sno = x509_load_serial(CAfile, CAserial, CA_createserial);
  762. if (sno == NULL)
  763. goto end;
  764. if (!x509toreq && !reqfile && !newcert && !self_signed(ctx, x))
  765. goto end;
  766. }
  767. if (sno != NULL && !X509_set_serialNumber(x, sno))
  768. goto end;
  769. if (reqfile || newcert || privkey != NULL || CAfile != NULL) {
  770. if (!preserve_dates && !set_cert_times(x, NULL, NULL, days))
  771. goto end;
  772. if (!X509_set_issuer_name(x, X509_get_subject_name(issuer_cert)))
  773. goto end;
  774. }
  775. X509V3_set_ctx(&ext_ctx, issuer_cert, x, NULL, NULL, X509V3_CTX_REPLACE);
  776. /* prepare fallback for AKID, but only if issuer cert equals subject cert */
  777. if (CAfile == NULL) {
  778. if (!X509V3_set_issuer_pkey(&ext_ctx, privkey))
  779. goto end;
  780. }
  781. if (extconf != NULL && !x509toreq) {
  782. X509V3_set_nconf(&ext_ctx, extconf);
  783. if (!X509V3_EXT_add_nconf(extconf, &ext_ctx, extsect, x)) {
  784. BIO_printf(bio_err,
  785. "Error adding extensions from section %s\n", extsect);
  786. goto end;
  787. }
  788. }
  789. /* At this point the contents of the certificate x have been finished. */
  790. pkey = X509_get0_pubkey(x);
  791. if ((print_pubkey != 0 || modulus != 0) && pkey == NULL) {
  792. BIO_printf(bio_err, "Error getting public key\n");
  793. goto end;
  794. }
  795. if (x509toreq) { /* also works in conjunction with -req */
  796. if (privkey == NULL) {
  797. BIO_printf(bio_err, "Must specify request signing key using -key\n");
  798. goto end;
  799. }
  800. if (clrext && ext_copy != EXT_COPY_NONE) {
  801. BIO_printf(bio_err, "Must not use -clrext together with -copy_extensions\n");
  802. goto end;
  803. }
  804. if ((rq = x509_to_req(x, ext_copy, ext_names)) == NULL)
  805. goto end;
  806. if (extconf != NULL) {
  807. X509V3_set_nconf(&ext_ctx, extconf);
  808. if (!X509V3_EXT_REQ_add_nconf(extconf, &ext_ctx, extsect, rq)) {
  809. BIO_printf(bio_err,
  810. "Error adding request extensions from section %s\n", extsect);
  811. goto end;
  812. }
  813. }
  814. if (!do_X509_REQ_sign(rq, privkey, digest, sigopts))
  815. goto end;
  816. if (!noout) {
  817. if (outformat == FORMAT_ASN1) {
  818. X509_REQ_print_ex(out, rq, get_nameopt(), X509_FLAG_COMPAT);
  819. i = i2d_X509_bio(out, x);
  820. } else {
  821. i = PEM_write_bio_X509_REQ(out, rq);
  822. }
  823. if (!i) {
  824. BIO_printf(bio_err,
  825. "Unable to write certificate request\n");
  826. goto end;
  827. }
  828. }
  829. noout = 1;
  830. } else if (privkey != NULL) {
  831. if (!do_X509_sign(x, privkey, digest, sigopts, &ext_ctx))
  832. goto end;
  833. } else if (CAfile != NULL) {
  834. if ((CAkey = load_key(CAkeyfile, CAkeyformat,
  835. 0, passin, e, "CA private key")) == NULL)
  836. goto end;
  837. if (!X509_check_private_key(xca, CAkey)) {
  838. BIO_printf(bio_err,
  839. "CA certificate and CA private key do not match\n");
  840. goto end;
  841. }
  842. if (!do_X509_sign(x, CAkey, digest, sigopts, &ext_ctx))
  843. goto end;
  844. }
  845. if (badsig) {
  846. const ASN1_BIT_STRING *signature;
  847. X509_get0_signature(&signature, NULL, x);
  848. corrupt_signature(signature);
  849. }
  850. /* Process print options in the given order, as indicated by index i */
  851. for (i = 1; i <= num; i++) {
  852. if (i == issuer) {
  853. print_name(out, "issuer=", X509_get_issuer_name(x));
  854. } else if (i == subject) {
  855. print_name(out, "subject=", X509_get_subject_name(x));
  856. } else if (i == serial) {
  857. BIO_printf(out, "serial=");
  858. i2a_ASN1_INTEGER(out, X509_get0_serialNumber(x));
  859. BIO_printf(out, "\n");
  860. } else if (i == next_serial) {
  861. ASN1_INTEGER *ser;
  862. BIGNUM *bnser = ASN1_INTEGER_to_BN(X509_get0_serialNumber(x), NULL);
  863. if (bnser == NULL)
  864. goto end;
  865. if (!BN_add_word(bnser, 1)
  866. || (ser = BN_to_ASN1_INTEGER(bnser, NULL)) == NULL) {
  867. BN_free(bnser);
  868. goto end;
  869. }
  870. BN_free(bnser);
  871. i2a_ASN1_INTEGER(out, ser);
  872. ASN1_INTEGER_free(ser);
  873. BIO_puts(out, "\n");
  874. } else if (i == email || i == ocsp_uri) {
  875. STACK_OF(OPENSSL_STRING) *emlst =
  876. i == email ? X509_get1_email(x) : X509_get1_ocsp(x);
  877. for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
  878. BIO_printf(out, "%s\n", sk_OPENSSL_STRING_value(emlst, j));
  879. X509_email_free(emlst);
  880. } else if (i == aliasout) {
  881. unsigned char *alstr = X509_alias_get0(x, NULL);
  882. if (alstr)
  883. BIO_printf(out, "%s\n", alstr);
  884. else
  885. BIO_puts(out, "<No Alias>\n");
  886. } else if (i == subject_hash) {
  887. BIO_printf(out, "%08lx\n", X509_subject_name_hash(x));
  888. #ifndef OPENSSL_NO_MD5
  889. } else if (i == subject_hash_old) {
  890. BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x));
  891. #endif
  892. } else if (i == issuer_hash) {
  893. BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x));
  894. #ifndef OPENSSL_NO_MD5
  895. } else if (i == issuer_hash_old) {
  896. BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x));
  897. #endif
  898. } else if (i == pprint) {
  899. BIO_printf(out, "Certificate purposes:\n");
  900. for (j = 0; j < X509_PURPOSE_get_count(); j++)
  901. purpose_print(out, x, X509_PURPOSE_get0(j));
  902. } else if (i == modulus) {
  903. BIO_printf(out, "Modulus=");
  904. if (EVP_PKEY_is_a(pkey, "RSA") || EVP_PKEY_is_a(pkey, "RSA-PSS")) {
  905. BIGNUM *n = NULL;
  906. /* Every RSA key has an 'n' */
  907. EVP_PKEY_get_bn_param(pkey, "n", &n);
  908. BN_print(out, n);
  909. BN_free(n);
  910. } else if (EVP_PKEY_is_a(pkey, "DSA")) {
  911. BIGNUM *dsapub = NULL;
  912. /* Every DSA key has a 'pub' */
  913. EVP_PKEY_get_bn_param(pkey, "pub", &dsapub);
  914. BN_print(out, dsapub);
  915. BN_free(dsapub);
  916. } else {
  917. BIO_printf(out, "No modulus for this public key type");
  918. }
  919. BIO_printf(out, "\n");
  920. } else if (i == print_pubkey) {
  921. PEM_write_bio_PUBKEY(out, pkey);
  922. } else if (i == text) {
  923. X509_print_ex(out, x, get_nameopt(), certflag);
  924. } else if (i == startdate) {
  925. BIO_puts(out, "notBefore=");
  926. ASN1_TIME_print_ex(out, X509_get0_notBefore(x), dateopt);
  927. BIO_puts(out, "\n");
  928. } else if (i == enddate) {
  929. BIO_puts(out, "notAfter=");
  930. ASN1_TIME_print_ex(out, X509_get0_notAfter(x), dateopt);
  931. BIO_puts(out, "\n");
  932. } else if (i == fingerprint) {
  933. unsigned int n;
  934. unsigned char md[EVP_MAX_MD_SIZE];
  935. const char *fdigname = digest;
  936. EVP_MD *fdig;
  937. int digres;
  938. if (fdigname == NULL)
  939. fdigname = "SHA1";
  940. if ((fdig = EVP_MD_fetch(app_get0_libctx(), fdigname,
  941. app_get0_propq())) == NULL) {
  942. BIO_printf(bio_err, "Unknown digest\n");
  943. goto end;
  944. }
  945. digres = X509_digest(x, fdig, md, &n);
  946. EVP_MD_free(fdig);
  947. if (!digres) {
  948. BIO_printf(bio_err, "Out of memory\n");
  949. goto end;
  950. }
  951. BIO_printf(out, "%s Fingerprint=", fdigname);
  952. for (j = 0; j < (int)n; j++)
  953. BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n) ? '\n' : ':');
  954. } else if (i == ocspid) {
  955. X509_ocspid_print(out, x);
  956. } else if (i == ext) {
  957. print_x509v3_exts(out, x, ext_names);
  958. }
  959. }
  960. if (checkend) {
  961. time_t tcheck = time(NULL) + checkoffset;
  962. ret = X509_cmp_time(X509_get0_notAfter(x), &tcheck) < 0;
  963. if (ret)
  964. BIO_printf(out, "Certificate will expire\n");
  965. else
  966. BIO_printf(out, "Certificate will not expire\n");
  967. goto end;
  968. }
  969. print_cert_checks(out, x, checkhost, checkemail, checkip);
  970. if (noout || nocert) {
  971. ret = 0;
  972. goto end;
  973. }
  974. if (outformat == FORMAT_ASN1) {
  975. i = i2d_X509_bio(out, x);
  976. } else if (outformat == FORMAT_PEM) {
  977. if (trustout)
  978. i = PEM_write_bio_X509_AUX(out, x);
  979. else
  980. i = PEM_write_bio_X509(out, x);
  981. } else {
  982. BIO_printf(bio_err, "Bad output format specified for outfile\n");
  983. goto end;
  984. }
  985. if (!i) {
  986. BIO_printf(bio_err, "Unable to write certificate\n");
  987. goto end;
  988. }
  989. ret = 0;
  990. end:
  991. if (ret != 0)
  992. ERR_print_errors(bio_err);
  993. NCONF_free(extconf);
  994. BIO_free_all(out);
  995. X509_STORE_free(ctx);
  996. X509_NAME_free(fsubj);
  997. X509_REQ_free(req);
  998. X509_free(x);
  999. X509_free(xca);
  1000. EVP_PKEY_free(privkey);
  1001. EVP_PKEY_free(CAkey);
  1002. EVP_PKEY_free(pubkey);
  1003. sk_OPENSSL_STRING_free(sigopts);
  1004. sk_OPENSSL_STRING_free(vfyopts);
  1005. X509_REQ_free(rq);
  1006. ASN1_INTEGER_free(sno);
  1007. sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
  1008. sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
  1009. release_engine(e);
  1010. clear_free(passin);
  1011. return ret;
  1012. }
  1013. static ASN1_INTEGER *x509_load_serial(const char *CAfile,
  1014. const char *serialfile, int create)
  1015. {
  1016. char *buf = NULL;
  1017. ASN1_INTEGER *bs = NULL;
  1018. BIGNUM *serial = NULL;
  1019. int defaultfile = 0, file_exists;
  1020. if (serialfile == NULL) {
  1021. const char *p = strrchr(CAfile, '.');
  1022. size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
  1023. buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");
  1024. memcpy(buf, CAfile, len);
  1025. memcpy(buf + len, POSTFIX, sizeof(POSTFIX));
  1026. serialfile = buf;
  1027. defaultfile = 1;
  1028. }
  1029. serial = load_serial(serialfile, &file_exists, create || defaultfile, NULL);
  1030. if (serial == NULL)
  1031. goto end;
  1032. if (!BN_add_word(serial, 1)) {
  1033. BIO_printf(bio_err, "Serial number increment failure\n");
  1034. goto end;
  1035. }
  1036. if (file_exists || create)
  1037. save_serial(serialfile, NULL, serial, &bs);
  1038. else
  1039. bs = BN_to_ASN1_INTEGER(serial, NULL);
  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. }