2
0

x509.c 40 KB

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