2
0

x509.c 41 KB

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