x509.c 44 KB

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