2
0

x509.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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_id(pkey) == EVP_PKEY_RSA) {
  721. const BIGNUM *n;
  722. RSA_get0_key(EVP_PKEY_get0_RSA(pkey), &n, NULL, NULL);
  723. BN_print(out, n);
  724. } else
  725. #endif
  726. #ifndef OPENSSL_NO_DSA
  727. if (EVP_PKEY_id(pkey) == EVP_PKEY_DSA) {
  728. const BIGNUM *dsapub = NULL;
  729. DSA_get0_key(EVP_PKEY_get0_DSA(pkey), &dsapub, NULL);
  730. BN_print(out, dsapub);
  731. } else
  732. #endif
  733. {
  734. BIO_printf(out, "Wrong Algorithm type");
  735. }
  736. BIO_printf(out, "\n");
  737. } else if (pubkey == i) {
  738. EVP_PKEY *pkey;
  739. pkey = X509_get0_pubkey(x);
  740. if (pkey == NULL) {
  741. BIO_printf(bio_err, "Error getting public key\n");
  742. ERR_print_errors(bio_err);
  743. goto end;
  744. }
  745. PEM_write_bio_PUBKEY(out, pkey);
  746. } else if (text == i) {
  747. X509_print_ex(out, x, get_nameopt(), certflag);
  748. } else if (startdate == i) {
  749. BIO_puts(out, "notBefore=");
  750. ASN1_TIME_print(out, X509_get0_notBefore(x));
  751. BIO_puts(out, "\n");
  752. } else if (enddate == i) {
  753. BIO_puts(out, "notAfter=");
  754. ASN1_TIME_print(out, X509_get0_notAfter(x));
  755. BIO_puts(out, "\n");
  756. } else if (fingerprint == i) {
  757. int j;
  758. unsigned int n;
  759. unsigned char md[EVP_MAX_MD_SIZE];
  760. const EVP_MD *fdig = digest;
  761. if (fdig == NULL)
  762. fdig = EVP_sha1();
  763. if (!X509_digest(x, fdig, md, &n)) {
  764. BIO_printf(bio_err, "out of memory\n");
  765. goto end;
  766. }
  767. BIO_printf(out, "%s Fingerprint=",
  768. OBJ_nid2sn(EVP_MD_type(fdig)));
  769. for (j = 0; j < (int)n; j++) {
  770. BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n)
  771. ? '\n' : ':');
  772. }
  773. }
  774. /* should be in the library */
  775. else if (sign_flag == i && x509req == 0) {
  776. BIO_printf(bio_err, "Getting Private key\n");
  777. if (Upkey == NULL) {
  778. Upkey = load_key(keyfile, keyformat, 0,
  779. passin, e, "private key");
  780. if (Upkey == NULL)
  781. goto end;
  782. }
  783. if (!sign(x, Upkey, fkey, sigopts, days, clrext, digest,
  784. extconf, extsect, preserve_dates))
  785. goto end;
  786. } else if (CA_flag == i) {
  787. BIO_printf(bio_err, "Getting CA Private Key\n");
  788. if (CAkeyfile != NULL) {
  789. CApkey = load_key(CAkeyfile, CAkeyformat,
  790. 0, passin, e, "CA private key");
  791. if (CApkey == NULL)
  792. goto end;
  793. }
  794. if (!x509_certify(ctx, CAfile, digest, x, xca,
  795. CApkey, sigopts,
  796. CAserial, CA_createserial, days, clrext,
  797. extconf, extsect, sno, reqfile, preserve_dates))
  798. goto end;
  799. } else if (x509req == i) {
  800. EVP_PKEY *pk;
  801. BIO_printf(bio_err, "Getting request Private Key\n");
  802. if (keyfile == NULL) {
  803. BIO_printf(bio_err, "no request key file specified\n");
  804. goto end;
  805. } else {
  806. pk = load_key(keyfile, keyformat, 0,
  807. passin, e, "request key");
  808. if (pk == NULL)
  809. goto end;
  810. }
  811. BIO_printf(bio_err, "Generating certificate request\n");
  812. rq = X509_to_X509_REQ(x, pk, digest);
  813. EVP_PKEY_free(pk);
  814. if (rq == NULL) {
  815. ERR_print_errors(bio_err);
  816. goto end;
  817. }
  818. if (!noout) {
  819. X509_REQ_print_ex(out, rq, get_nameopt(), X509_FLAG_COMPAT);
  820. PEM_write_bio_X509_REQ(out, rq);
  821. }
  822. noout = 1;
  823. } else if (ocspid == i) {
  824. X509_ocspid_print(out, x);
  825. } else if (ext == i) {
  826. print_x509v3_exts(out, x, exts);
  827. }
  828. }
  829. }
  830. if (checkend) {
  831. time_t tcheck = time(NULL) + checkoffset;
  832. if (X509_cmp_time(X509_get0_notAfter(x), &tcheck) < 0) {
  833. BIO_printf(out, "Certificate will expire\n");
  834. ret = 1;
  835. } else {
  836. BIO_printf(out, "Certificate will not expire\n");
  837. ret = 0;
  838. }
  839. goto end;
  840. }
  841. print_cert_checks(out, x, checkhost, checkemail, checkip);
  842. if (noout || nocert) {
  843. ret = 0;
  844. goto end;
  845. }
  846. if (outformat == FORMAT_ASN1) {
  847. i = i2d_X509_bio(out, x);
  848. } else if (outformat == FORMAT_PEM) {
  849. if (trustout)
  850. i = PEM_write_bio_X509_AUX(out, x);
  851. else
  852. i = PEM_write_bio_X509(out, x);
  853. } else {
  854. BIO_printf(bio_err, "bad output format specified for outfile\n");
  855. goto end;
  856. }
  857. if (!i) {
  858. BIO_printf(bio_err, "unable to write certificate\n");
  859. ERR_print_errors(bio_err);
  860. goto end;
  861. }
  862. ret = 0;
  863. end:
  864. NCONF_free(extconf);
  865. BIO_free_all(out);
  866. X509_STORE_free(ctx);
  867. X509_NAME_free(fsubj);
  868. X509_REQ_free(req);
  869. X509_free(x);
  870. X509_free(xca);
  871. EVP_PKEY_free(Upkey);
  872. EVP_PKEY_free(CApkey);
  873. EVP_PKEY_free(fkey);
  874. sk_OPENSSL_STRING_free(sigopts);
  875. sk_OPENSSL_STRING_free(vfyopts);
  876. X509_REQ_free(rq);
  877. ASN1_INTEGER_free(sno);
  878. sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
  879. sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
  880. ASN1_OBJECT_free(objtmp);
  881. release_engine(e);
  882. clear_free(passin);
  883. return ret;
  884. }
  885. static ASN1_INTEGER *x509_load_serial(const char *CAfile,
  886. const char *serialfile, int create)
  887. {
  888. char *buf = NULL;
  889. ASN1_INTEGER *bs = NULL;
  890. BIGNUM *serial = NULL;
  891. if (serialfile == NULL) {
  892. const char *p = strrchr(CAfile, '.');
  893. size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
  894. buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");
  895. memcpy(buf, CAfile, len);
  896. memcpy(buf + len, POSTFIX, sizeof(POSTFIX));
  897. serialfile = buf;
  898. }
  899. serial = load_serial(serialfile, create, NULL);
  900. if (serial == NULL)
  901. goto end;
  902. if (!BN_add_word(serial, 1)) {
  903. BIO_printf(bio_err, "add_word failure\n");
  904. goto end;
  905. }
  906. if (!save_serial(serialfile, NULL, serial, &bs))
  907. goto end;
  908. end:
  909. OPENSSL_free(buf);
  910. BN_free(serial);
  911. return bs;
  912. }
  913. static int x509_certify(X509_STORE *ctx, const char *CAfile, const EVP_MD *digest,
  914. X509 *x, X509 *xca, EVP_PKEY *pkey,
  915. STACK_OF(OPENSSL_STRING) *sigopts,
  916. const char *serialfile, int create,
  917. int days, int clrext, CONF *conf, const char *section,
  918. ASN1_INTEGER *sno, int reqfile, int preserve_dates)
  919. {
  920. int ret = 0;
  921. ASN1_INTEGER *bs = NULL;
  922. X509_STORE_CTX *xsc = NULL;
  923. EVP_PKEY *upkey;
  924. upkey = X509_get0_pubkey(xca);
  925. if (upkey == NULL) {
  926. BIO_printf(bio_err, "Error obtaining CA X509 public key\n");
  927. goto end;
  928. }
  929. EVP_PKEY_copy_parameters(upkey, pkey);
  930. xsc = X509_STORE_CTX_new();
  931. if (xsc == NULL || !X509_STORE_CTX_init(xsc, ctx, x, NULL)) {
  932. BIO_printf(bio_err, "Error initialising X509 store\n");
  933. goto end;
  934. }
  935. if (sno)
  936. bs = sno;
  937. else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL)
  938. goto end;
  939. /*
  940. * NOTE: this certificate can/should be self-signed, unless it was a
  941. * certificate request in which case it is not.
  942. */
  943. X509_STORE_CTX_set_cert(xsc, x);
  944. X509_STORE_CTX_set_flags(xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
  945. if (!reqfile && X509_verify_cert(xsc) <= 0)
  946. goto end;
  947. if (!X509_check_private_key(xca, pkey)) {
  948. BIO_printf(bio_err,
  949. "CA certificate and CA private key do not match\n");
  950. goto end;
  951. }
  952. if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
  953. goto end;
  954. if (!X509_set_serialNumber(x, bs))
  955. goto end;
  956. if (!preserve_dates && !set_cert_times(x, NULL, NULL, days))
  957. goto end;
  958. if (clrext) {
  959. while (X509_get_ext_count(x) > 0)
  960. X509_delete_ext(x, 0);
  961. }
  962. if (conf != NULL) {
  963. X509V3_CTX ctx2;
  964. X509_set_version(x, 2); /* version 3 certificate */
  965. X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
  966. X509V3_set_nconf(&ctx2, conf);
  967. if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))
  968. goto end;
  969. }
  970. if (!do_X509_sign(x, pkey, digest, sigopts))
  971. goto end;
  972. ret = 1;
  973. end:
  974. X509_STORE_CTX_free(xsc);
  975. if (!ret)
  976. ERR_print_errors(bio_err);
  977. if (!sno)
  978. ASN1_INTEGER_free(bs);
  979. return ret;
  980. }
  981. static int callb(int ok, X509_STORE_CTX *ctx)
  982. {
  983. int err;
  984. X509 *err_cert;
  985. /*
  986. * It is ok to use a self-signed certificate. This case will catch both
  987. * the initial ok == 0 and the final ok == 1 calls to this function.
  988. */
  989. err = X509_STORE_CTX_get_error(ctx);
  990. if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
  991. return 1;
  992. /*
  993. * BAD we should have gotten an error. Normally if everything worked
  994. * X509_STORE_CTX_get_error(ctx) will still be set to
  995. * DEPTH_ZERO_SELF_....
  996. */
  997. if (ok) {
  998. BIO_printf(bio_err,
  999. "error with certificate to be certified - should be self-signed\n");
  1000. return 0;
  1001. } else {
  1002. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  1003. print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
  1004. BIO_printf(bio_err,
  1005. "error with certificate - error %d at depth %d\n%s\n", err,
  1006. X509_STORE_CTX_get_error_depth(ctx),
  1007. X509_verify_cert_error_string(err));
  1008. return 1;
  1009. }
  1010. }
  1011. /* self-issue; self-sign unless a forced public key (fkey) is given */
  1012. static int sign(X509 *x, EVP_PKEY *pkey, EVP_PKEY *fkey,
  1013. STACK_OF(OPENSSL_STRING) *sigopts,
  1014. int days, int clrext,
  1015. const EVP_MD *digest, CONF *conf, const char *section,
  1016. int preserve_dates)
  1017. {
  1018. if (!X509_set_issuer_name(x, X509_get_subject_name(x)))
  1019. goto err;
  1020. if (!preserve_dates && !set_cert_times(x, NULL, NULL, days))
  1021. goto err;
  1022. if (fkey == NULL && !X509_set_pubkey(x, pkey))
  1023. goto err;
  1024. if (clrext) {
  1025. while (X509_get_ext_count(x) > 0)
  1026. X509_delete_ext(x, 0);
  1027. }
  1028. if (conf != NULL) {
  1029. X509V3_CTX ctx;
  1030. X509_set_version(x, 2); /* version 3 certificate */
  1031. X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
  1032. X509V3_set_nconf(&ctx, conf);
  1033. if (!X509V3_EXT_add_nconf(conf, &ctx, section, x))
  1034. goto err;
  1035. }
  1036. if (!do_X509_sign(x, pkey, digest, sigopts))
  1037. goto err;
  1038. return 1;
  1039. err:
  1040. ERR_print_errors(bio_err);
  1041. return 0;
  1042. }
  1043. static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
  1044. {
  1045. int id, i, idret;
  1046. const char *pname;
  1047. id = X509_PURPOSE_get_id(pt);
  1048. pname = X509_PURPOSE_get0_name(pt);
  1049. for (i = 0; i < 2; i++) {
  1050. idret = X509_check_purpose(cert, id, i);
  1051. BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
  1052. if (idret == 1)
  1053. BIO_printf(bio, "Yes\n");
  1054. else if (idret == 0)
  1055. BIO_printf(bio, "No\n");
  1056. else
  1057. BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
  1058. }
  1059. return 1;
  1060. }
  1061. static int parse_ext_names(char *names, const char **result)
  1062. {
  1063. char *p, *q;
  1064. int cnt = 0, len = 0;
  1065. p = q = names;
  1066. len = strlen(names);
  1067. while (q - names <= len) {
  1068. if (*q != ',' && *q != '\0') {
  1069. q++;
  1070. continue;
  1071. }
  1072. if (p != q) {
  1073. /* found */
  1074. if (result != NULL) {
  1075. result[cnt] = p;
  1076. *q = '\0';
  1077. }
  1078. cnt++;
  1079. }
  1080. p = ++q;
  1081. }
  1082. return cnt;
  1083. }
  1084. static int print_x509v3_exts(BIO *bio, X509 *x, const char *ext_names)
  1085. {
  1086. const STACK_OF(X509_EXTENSION) *exts = NULL;
  1087. STACK_OF(X509_EXTENSION) *exts2 = NULL;
  1088. X509_EXTENSION *ext = NULL;
  1089. ASN1_OBJECT *obj;
  1090. int i, j, ret = 0, num, nn = 0;
  1091. const char *sn, **names = NULL;
  1092. char *tmp_ext_names = NULL;
  1093. exts = X509_get0_extensions(x);
  1094. if ((num = sk_X509_EXTENSION_num(exts)) <= 0) {
  1095. BIO_printf(bio, "No extensions in certificate\n");
  1096. ret = 1;
  1097. goto end;
  1098. }
  1099. /* parse comma separated ext name string */
  1100. if ((tmp_ext_names = OPENSSL_strdup(ext_names)) == NULL)
  1101. goto end;
  1102. if ((nn = parse_ext_names(tmp_ext_names, NULL)) == 0) {
  1103. BIO_printf(bio, "Invalid extension names: %s\n", ext_names);
  1104. goto end;
  1105. }
  1106. if ((names = OPENSSL_malloc(sizeof(char *) * nn)) == NULL)
  1107. goto end;
  1108. parse_ext_names(tmp_ext_names, names);
  1109. for (i = 0; i < num; i++) {
  1110. ext = sk_X509_EXTENSION_value(exts, i);
  1111. /* check if this ext is what we want */
  1112. obj = X509_EXTENSION_get_object(ext);
  1113. sn = OBJ_nid2sn(OBJ_obj2nid(obj));
  1114. if (sn == NULL || strcmp(sn, "UNDEF") == 0)
  1115. continue;
  1116. for (j = 0; j < nn; j++) {
  1117. if (strcmp(sn, names[j]) == 0) {
  1118. /* push the extension into a new stack */
  1119. if (exts2 == NULL
  1120. && (exts2 = sk_X509_EXTENSION_new_null()) == NULL)
  1121. goto end;
  1122. if (!sk_X509_EXTENSION_push(exts2, ext))
  1123. goto end;
  1124. }
  1125. }
  1126. }
  1127. if (!sk_X509_EXTENSION_num(exts2)) {
  1128. BIO_printf(bio, "No extensions matched with %s\n", ext_names);
  1129. ret = 1;
  1130. goto end;
  1131. }
  1132. ret = X509V3_extensions_print(bio, NULL, exts2, 0, 0);
  1133. end:
  1134. sk_X509_EXTENSION_free(exts2);
  1135. OPENSSL_free(names);
  1136. OPENSSL_free(tmp_ext_names);
  1137. return ret;
  1138. }