x509.c 43 KB

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