x509.c 42 KB

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