x509.c 41 KB

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