x509.c 42 KB

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