x509.c 41 KB

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