x509.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include "apps.h"
  61. #include <openssl/bio.h>
  62. #include <openssl/asn1.h>
  63. #include <openssl/err.h>
  64. #include <openssl/bn.h>
  65. #include <openssl/evp.h>
  66. #include <openssl/x509.h>
  67. #include <openssl/x509v3.h>
  68. #include <openssl/objects.h>
  69. #include <openssl/pem.h>
  70. #ifndef OPENSSL_NO_RSA
  71. # include <openssl/rsa.h>
  72. #endif
  73. #ifndef OPENSSL_NO_DSA
  74. # include <openssl/dsa.h>
  75. #endif
  76. #undef POSTFIX
  77. #define POSTFIX ".srl"
  78. #define DEF_DAYS 30
  79. static int callb(int ok, X509_STORE_CTX *ctx);
  80. static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
  81. const EVP_MD *digest, CONF *conf, char *section);
  82. static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
  83. X509 *x, X509 *xca, EVP_PKEY *pkey,
  84. STACK_OF(OPENSSL_STRING) *sigopts, char *serial,
  85. int create, int days, int clrext, CONF *conf,
  86. char *section, ASN1_INTEGER *sno, int reqfile);
  87. static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
  88. #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  89. static int force_version = 2;
  90. #endif
  91. typedef enum OPTION_choice {
  92. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  93. OPT_INFORM, OPT_OUTFORM, OPT_KEYFORM, OPT_REQ, OPT_CAFORM,
  94. OPT_CAKEYFORM, OPT_SIGOPT, OPT_DAYS, OPT_PASSIN, OPT_EXTFILE,
  95. OPT_EXTENSIONS, OPT_IN, OPT_OUT, OPT_SIGNKEY, OPT_CA,
  96. OPT_CAKEY, OPT_CASERIAL, OPT_SET_SERIAL, OPT_FORCE_PUBKEY,
  97. OPT_ADDTRUST, OPT_ADDREJECT, OPT_SETALIAS, OPT_CERTOPT, OPT_NAMEOPT,
  98. OPT_C, OPT_EMAIL, OPT_OCSP_URI, OPT_SERIAL, OPT_NEXT_SERIAL,
  99. OPT_MODULUS, OPT_PUBKEY, OPT_X509TOREQ, OPT_TEXT, OPT_HASH,
  100. OPT_ISSUER_HASH, OPT_SUBJECT, OPT_ISSUER, OPT_FINGERPRINT, OPT_DATES,
  101. OPT_PURPOSE, OPT_STARTDATE, OPT_ENDDATE, OPT_CHECKEND, OPT_CHECKHOST,
  102. OPT_CHECKEMAIL, OPT_CHECKIP, OPT_NOOUT, OPT_TRUSTOUT, OPT_CLRTRUST,
  103. OPT_CLRREJECT, OPT_ALIAS, OPT_CACREATESERIAL, OPT_CLREXT, OPT_OCSPID,
  104. OPT_SUBJECT_HASH_OLD,
  105. OPT_ISSUER_HASH_OLD,
  106. OPT_FORCE_VERSION,
  107. OPT_BADSIG, OPT_MD, OPT_ENGINE, OPT_NOCERT
  108. } OPTION_CHOICE;
  109. OPTIONS x509_options[] = {
  110. {"help", OPT_HELP, '-', "Display this summary"},
  111. {"inform", OPT_INFORM, 'f',
  112. "Input format - default PEM (one of DER, NET or PEM)"},
  113. {"in", OPT_IN, '<', "Input file - default stdin"},
  114. {"outform", OPT_OUTFORM, 'f',
  115. "Output format - default PEM (one of DER, NET or PEM)"},
  116. {"out", OPT_OUT, '>', "Output file - default stdout"},
  117. {"keyform", OPT_KEYFORM, 'F', "Private key format - default PEM"},
  118. {"passin", OPT_PASSIN, 's', "Private key password source"},
  119. {"serial", OPT_SERIAL, '-', "Print serial number value"},
  120. {"subject_hash", OPT_HASH, '-', "Print subject hash value"},
  121. {"issuer_hash", OPT_ISSUER_HASH, '-', "Print issuer hash value"},
  122. {"hash", OPT_HASH, '-', "Synonym for -subject_hash"},
  123. {"subject", OPT_SUBJECT, '-', "Print subject DN"},
  124. {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
  125. {"email", OPT_EMAIL, '-', "Print email address(es)"},
  126. {"startdate", OPT_STARTDATE, '-', "Set notBefore field"},
  127. {"enddate", OPT_ENDDATE, '-', "Set notAfter field"},
  128. {"purpose", OPT_PURPOSE, '-', "Print out certificate purposes"},
  129. {"dates", OPT_DATES, '-', "Both Before and After dates"},
  130. {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
  131. {"pubkey", OPT_PUBKEY, '-', "Output the public key"},
  132. {"fingerprint", OPT_FINGERPRINT, '-',
  133. "Print the certificate fingerprint"},
  134. {"alias", OPT_ALIAS, '-', "Output certificate alias"},
  135. {"noout", OPT_NOOUT, '-', "No output, just status"},
  136. {"nocert", OPT_NOCERT, '-', "No certificate output"},
  137. {"ocspid", OPT_OCSPID, '-',
  138. "Print OCSP hash values for the subject name and public key"},
  139. {"ocsp_uri", OPT_OCSP_URI, '-', "Print OCSP Responder URL(s)"},
  140. {"trustout", OPT_TRUSTOUT, '-', "Output a trusted certificate"},
  141. {"clrtrust", OPT_CLRTRUST, '-', "Clear all trusted purposes"},
  142. {"clrext", OPT_CLREXT, '-', "Clear all rejected purposes"},
  143. {"addtrust", OPT_ADDTRUST, 's', "Trust certificate for a given purpose"},
  144. {"addreject", OPT_ADDREJECT, 's',
  145. "Reject certificate for a given purpose"},
  146. {"setalias", OPT_SETALIAS, 's', "Set certificate alias"},
  147. {"days", OPT_DAYS, 'n',
  148. "How long till expiry of a signed certificate - def 30 days"},
  149. {"checkend", OPT_CHECKEND, 'p',
  150. "Check whether the cert expires in the next arg seconds"},
  151. {OPT_MORE_STR, 1, 1, "Exit 1 if so, 0 if not"},
  152. {"signkey", OPT_SIGNKEY, '<', "Self sign cert with arg"},
  153. {"x509toreq", OPT_X509TOREQ, '-',
  154. "Output a certification request object"},
  155. {"req", OPT_REQ, '-', "Input is a certificate request, sign and output"},
  156. {"CA", OPT_CA, '<', "Set the CA certificate, must be PEM format"},
  157. {"CAkey", OPT_CAKEY, '<',
  158. "The CA key, must be PEM format; if not in CAfile"},
  159. {"CAcreateserial", OPT_CACREATESERIAL, '-',
  160. "Create serial number file if it does not exist"},
  161. {"CAserial", OPT_CASERIAL, 's', "Serial file"},
  162. {"set_serial", OPT_SET_SERIAL, 's', "Serial number to use"},
  163. {"text", OPT_TEXT, '-', "Print the certificate in text form"},
  164. {"C", OPT_C, '-', "Print out C code forms"},
  165. {"extfile", OPT_EXTFILE, '<', "File with X509V3 extensions to add"},
  166. {"extensions", OPT_EXTENSIONS, 's', "Section from config file to use"},
  167. {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
  168. {"certopt", OPT_CERTOPT, 's', "Various certificate text options"},
  169. {"checkhost", OPT_CHECKHOST, 's', "Check certificate matches host"},
  170. {"checkemail", OPT_CHECKEMAIL, 's', "Check certificate matches email"},
  171. {"checkip", OPT_CHECKIP, 's', "Check certificate matches ipaddr"},
  172. {"CAform", OPT_CAFORM, 'F', "CA format - default PEM"},
  173. {"CAkeyform", OPT_CAKEYFORM, 'F', "CA key format - default PEM"},
  174. {"sigopt", OPT_SIGOPT, 's'},
  175. {"force_pubkey", OPT_FORCE_PUBKEY, '<'},
  176. {"next_serial", OPT_NEXT_SERIAL, '-'},
  177. {"clrreject", OPT_CLRREJECT, '-'},
  178. {"badsig", OPT_BADSIG, '-'},
  179. {"", OPT_MD, '-', "Any supported digest"},
  180. #ifndef OPENSSL_NO_MD5
  181. {"subject_hash_old", OPT_SUBJECT_HASH_OLD, '-',
  182. "Print old-style (MD5) issuer hash value"},
  183. {"issuer_hash_old", OPT_ISSUER_HASH_OLD, '-',
  184. "Print old-style (MD5) subject hash value"},
  185. #endif
  186. #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  187. {"force_version", OPT_FORCE_VERSION, 'p'},
  188. #endif
  189. #ifndef OPENSSL_NO_ENGINE
  190. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  191. #endif
  192. {NULL}
  193. };
  194. int x509_main(int argc, char **argv)
  195. {
  196. ASN1_INTEGER *sno = NULL;
  197. ASN1_OBJECT *objtmp;
  198. BIO *out = NULL;
  199. CONF *extconf = NULL;
  200. EVP_PKEY *Upkey = NULL, *CApkey = NULL, *fkey = NULL;
  201. STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
  202. STACK_OF(OPENSSL_STRING) *sigopts = NULL;
  203. X509 *x = NULL, *xca = NULL;
  204. X509_REQ *req = NULL, *rq = NULL;
  205. X509_STORE *ctx = NULL;
  206. const EVP_MD *digest = NULL;
  207. char *CAkeyfile = NULL, *CAserial = NULL, *fkeyfile = NULL, *alias = NULL;
  208. char *checkhost = NULL, *checkemail = NULL, *checkip = NULL;
  209. char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL;
  210. char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL;
  211. char buf[256], *prog;
  212. int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0, pprint = 0;
  213. int C = 0, CAformat = FORMAT_PEM, CAkeyformat = FORMAT_PEM;
  214. int fingerprint = 0, reqfile = 0, need_rand = 0, checkend = 0;
  215. int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
  216. int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0;
  217. int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0, email = 0;
  218. int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0;
  219. int ret = 1, i, num = 0, badsig = 0, clrext = 0, nocert = 0;
  220. int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0;
  221. int checkoffset = 0, enddate = 0;
  222. unsigned long nmflag = 0, certflag = 0;
  223. char nmflag_set = 0;
  224. OPTION_CHOICE o;
  225. ENGINE *e = NULL;
  226. #ifndef OPENSSL_NO_MD5
  227. int subject_hash_old = 0, issuer_hash_old = 0;
  228. #endif
  229. ctx = X509_STORE_new();
  230. if (ctx == NULL)
  231. goto end;
  232. X509_STORE_set_verify_cb(ctx, callb);
  233. prog = opt_init(argc, argv, x509_options);
  234. while ((o = opt_next()) != OPT_EOF) {
  235. switch (o) {
  236. case OPT_EOF:
  237. case OPT_ERR:
  238. opthelp:
  239. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  240. goto end;
  241. case OPT_HELP:
  242. opt_help(x509_options);
  243. ret = 0;
  244. goto end;
  245. case OPT_INFORM:
  246. if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
  247. goto opthelp;
  248. break;
  249. case OPT_IN:
  250. infile = opt_arg();
  251. break;
  252. case OPT_OUTFORM:
  253. if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
  254. goto opthelp;
  255. break;
  256. case OPT_KEYFORM:
  257. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
  258. goto opthelp;
  259. break;
  260. case OPT_CAFORM:
  261. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAformat))
  262. goto opthelp;
  263. break;
  264. case OPT_CAKEYFORM:
  265. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAkeyformat))
  266. goto opthelp;
  267. break;
  268. case OPT_OUT:
  269. outfile = opt_arg();
  270. break;
  271. case OPT_REQ:
  272. reqfile = need_rand = 1;
  273. break;
  274. case OPT_SIGOPT:
  275. if (!sigopts)
  276. sigopts = sk_OPENSSL_STRING_new_null();
  277. if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
  278. goto opthelp;
  279. break;
  280. case OPT_FORCE_VERSION:
  281. #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  282. force_version = atoi(opt_arg()) - 1;
  283. #endif
  284. break;
  285. case OPT_DAYS:
  286. days = atoi(opt_arg());
  287. break;
  288. case OPT_PASSIN:
  289. passinarg = opt_arg();
  290. break;
  291. case OPT_EXTFILE:
  292. extfile = opt_arg();
  293. break;
  294. case OPT_EXTENSIONS:
  295. extsect = opt_arg();
  296. break;
  297. case OPT_SIGNKEY:
  298. keyfile = opt_arg();
  299. sign_flag = ++num;
  300. need_rand = 1;
  301. break;
  302. case OPT_CA:
  303. CAfile = opt_arg();
  304. CA_flag = ++num;
  305. need_rand = 1;
  306. break;
  307. case OPT_CAKEY:
  308. CAkeyfile = opt_arg();
  309. break;
  310. case OPT_CASERIAL:
  311. CAserial = opt_arg();
  312. break;
  313. case OPT_SET_SERIAL:
  314. if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)
  315. goto opthelp;
  316. break;
  317. case OPT_FORCE_PUBKEY:
  318. fkeyfile = opt_arg();
  319. break;
  320. case OPT_ADDTRUST:
  321. if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
  322. BIO_printf(bio_err,
  323. "%s: Invalid trust object value %s\n",
  324. prog, opt_arg());
  325. goto opthelp;
  326. }
  327. if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)
  328. goto end;
  329. sk_ASN1_OBJECT_push(trust, objtmp);
  330. trustout = 1;
  331. break;
  332. case OPT_ADDREJECT:
  333. if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
  334. BIO_printf(bio_err,
  335. "%s: Invalid reject object value %s\n",
  336. prog, opt_arg());
  337. goto opthelp;
  338. }
  339. if (reject == NULL
  340. && (reject = sk_ASN1_OBJECT_new_null()) == NULL)
  341. goto end;
  342. sk_ASN1_OBJECT_push(reject, objtmp);
  343. trustout = 1;
  344. break;
  345. case OPT_SETALIAS:
  346. alias = opt_arg();
  347. trustout = 1;
  348. break;
  349. case OPT_CERTOPT:
  350. if (!set_cert_ex(&certflag, opt_arg()))
  351. goto opthelp;
  352. break;
  353. case OPT_NAMEOPT:
  354. nmflag_set = 1;
  355. if (!set_name_ex(&nmflag, opt_arg()))
  356. goto opthelp;
  357. break;
  358. case OPT_ENGINE:
  359. e = setup_engine(opt_arg(), 0);
  360. break;
  361. case OPT_C:
  362. C = ++num;
  363. break;
  364. case OPT_EMAIL:
  365. email = ++num;
  366. break;
  367. case OPT_OCSP_URI:
  368. ocsp_uri = ++num;
  369. break;
  370. case OPT_SERIAL:
  371. serial = ++num;
  372. break;
  373. case OPT_NEXT_SERIAL:
  374. next_serial = ++num;
  375. break;
  376. case OPT_MODULUS:
  377. modulus = ++num;
  378. break;
  379. case OPT_PUBKEY:
  380. pubkey = ++num;
  381. break;
  382. case OPT_X509TOREQ:
  383. x509req = ++num;
  384. break;
  385. case OPT_TEXT:
  386. text = ++num;
  387. break;
  388. case OPT_SUBJECT:
  389. subject = ++num;
  390. break;
  391. case OPT_ISSUER:
  392. issuer = ++num;
  393. break;
  394. case OPT_FINGERPRINT:
  395. fingerprint = ++num;
  396. break;
  397. case OPT_HASH:
  398. subject_hash = ++num;
  399. break;
  400. case OPT_ISSUER_HASH:
  401. issuer_hash = ++num;
  402. break;
  403. case OPT_PURPOSE:
  404. pprint = ++num;
  405. break;
  406. case OPT_STARTDATE:
  407. startdate = ++num;
  408. break;
  409. case OPT_ENDDATE:
  410. enddate = ++num;
  411. break;
  412. case OPT_NOOUT:
  413. noout = ++num;
  414. break;
  415. case OPT_NOCERT:
  416. nocert = 1;
  417. break;
  418. case OPT_TRUSTOUT:
  419. trustout = 1;
  420. break;
  421. case OPT_CLRTRUST:
  422. clrtrust = ++num;
  423. break;
  424. case OPT_CLRREJECT:
  425. clrreject = ++num;
  426. break;
  427. case OPT_ALIAS:
  428. aliasout = ++num;
  429. break;
  430. case OPT_CACREATESERIAL:
  431. CA_createserial = ++num;
  432. break;
  433. case OPT_CLREXT:
  434. clrext = 1;
  435. break;
  436. case OPT_OCSPID:
  437. ocspid = ++num;
  438. break;
  439. case OPT_BADSIG:
  440. badsig = 1;
  441. break;
  442. #ifndef OPENSSL_NO_MD5
  443. case OPT_SUBJECT_HASH_OLD:
  444. subject_hash_old = ++num;
  445. break;
  446. case OPT_ISSUER_HASH_OLD:
  447. issuer_hash_old = ++num;
  448. break;
  449. #else
  450. case OPT_SUBJECT_HASH_OLD:
  451. case OPT_ISSUER_HASH_OLD:
  452. break;
  453. #endif
  454. case OPT_DATES:
  455. startdate = ++num;
  456. enddate = ++num;
  457. break;
  458. case OPT_CHECKEND:
  459. checkoffset = atoi(opt_arg());
  460. checkend = 1;
  461. break;
  462. case OPT_CHECKHOST:
  463. checkhost = opt_arg();
  464. break;
  465. case OPT_CHECKEMAIL:
  466. checkemail = opt_arg();
  467. break;
  468. case OPT_CHECKIP:
  469. checkip = opt_arg();
  470. break;
  471. case OPT_MD:
  472. if (!opt_md(opt_unknown(), &digest))
  473. goto opthelp;
  474. }
  475. }
  476. argc = opt_num_rest();
  477. argv = opt_rest();
  478. if (argc != 0) {
  479. BIO_printf(bio_err, "%s: Unknown parameter %s\n", prog, argv[0]);
  480. goto opthelp;
  481. }
  482. if (!nmflag_set)
  483. nmflag = XN_FLAG_ONELINE;
  484. out = bio_open_default(outfile, 'w', outformat);
  485. if (out == NULL)
  486. goto end;
  487. if (need_rand)
  488. app_RAND_load_file(NULL, 0);
  489. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  490. BIO_printf(bio_err, "Error getting password\n");
  491. goto end;
  492. }
  493. if (!X509_STORE_set_default_paths(ctx)) {
  494. ERR_print_errors(bio_err);
  495. goto end;
  496. }
  497. if (fkeyfile) {
  498. fkey = load_pubkey(fkeyfile, keyformat, 0, NULL, e, "Forced key");
  499. if (fkey == NULL)
  500. goto end;
  501. }
  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. }
  509. if (extfile) {
  510. X509V3_CTX ctx2;
  511. if ((extconf = app_load_config(extfile)) == NULL)
  512. goto end;
  513. if (!extsect) {
  514. extsect = NCONF_get_string(extconf, "default", "extensions");
  515. if (!extsect) {
  516. ERR_clear_error();
  517. extsect = "default";
  518. }
  519. }
  520. X509V3_set_ctx_test(&ctx2);
  521. X509V3_set_nconf(&ctx2, extconf);
  522. if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
  523. BIO_printf(bio_err,
  524. "Error Loading extension section %s\n", extsect);
  525. ERR_print_errors(bio_err);
  526. goto end;
  527. }
  528. }
  529. if (reqfile) {
  530. EVP_PKEY *pkey;
  531. BIO *in;
  532. if (!sign_flag && !CA_flag) {
  533. BIO_printf(bio_err, "We need a private key to sign with\n");
  534. goto end;
  535. }
  536. in = bio_open_default(infile, 'r', informat);
  537. if (in == NULL)
  538. goto end;
  539. req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
  540. BIO_free(in);
  541. if (req == NULL) {
  542. ERR_print_errors(bio_err);
  543. goto end;
  544. }
  545. if ((pkey = X509_REQ_get_pubkey(req)) == NULL) {
  546. BIO_printf(bio_err, "error unpacking public key\n");
  547. goto end;
  548. }
  549. i = X509_REQ_verify(req, pkey);
  550. EVP_PKEY_free(pkey);
  551. if (i < 0) {
  552. BIO_printf(bio_err, "Signature verification error\n");
  553. ERR_print_errors(bio_err);
  554. goto end;
  555. }
  556. if (i == 0) {
  557. BIO_printf(bio_err,
  558. "Signature did not match the certificate request\n");
  559. goto end;
  560. } else
  561. BIO_printf(bio_err, "Signature ok\n");
  562. print_name(bio_err, "subject=", X509_REQ_get_subject_name(req),
  563. nmflag);
  564. if ((x = X509_new()) == NULL)
  565. goto end;
  566. if (sno == NULL) {
  567. sno = ASN1_INTEGER_new();
  568. if (sno == NULL || !rand_serial(NULL, sno))
  569. goto end;
  570. if (!X509_set_serialNumber(x, sno))
  571. goto end;
  572. ASN1_INTEGER_free(sno);
  573. sno = NULL;
  574. } else if (!X509_set_serialNumber(x, sno))
  575. goto end;
  576. if (!X509_set_issuer_name(x, X509_REQ_get_subject_name(req)))
  577. goto end;
  578. if (!X509_set_subject_name(x, X509_REQ_get_subject_name(req)))
  579. goto end;
  580. X509_gmtime_adj(X509_get_notBefore(x), 0);
  581. X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL);
  582. if (fkey)
  583. X509_set_pubkey(x, fkey);
  584. else {
  585. pkey = X509_REQ_get_pubkey(req);
  586. X509_set_pubkey(x, pkey);
  587. EVP_PKEY_free(pkey);
  588. }
  589. } else
  590. x = load_cert(infile, informat, NULL, e, "Certificate");
  591. if (x == NULL)
  592. goto end;
  593. if (CA_flag) {
  594. xca = load_cert(CAfile, CAformat, NULL, e, "CA Certificate");
  595. if (xca == NULL)
  596. goto end;
  597. }
  598. if (!noout || text || next_serial) {
  599. OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
  600. }
  601. if (alias)
  602. X509_alias_set1(x, (unsigned char *)alias, -1);
  603. if (clrtrust)
  604. X509_trust_clear(x);
  605. if (clrreject)
  606. X509_reject_clear(x);
  607. if (trust) {
  608. for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
  609. objtmp = sk_ASN1_OBJECT_value(trust, i);
  610. X509_add1_trust_object(x, objtmp);
  611. }
  612. }
  613. if (reject) {
  614. for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
  615. objtmp = sk_ASN1_OBJECT_value(reject, i);
  616. X509_add1_reject_object(x, objtmp);
  617. }
  618. }
  619. if (num) {
  620. for (i = 1; i <= num; i++) {
  621. if (issuer == i) {
  622. print_name(out, "issuer= ", X509_get_issuer_name(x), nmflag);
  623. } else if (subject == i) {
  624. print_name(out, "subject= ",
  625. X509_get_subject_name(x), nmflag);
  626. } else if (serial == i) {
  627. BIO_printf(out, "serial=");
  628. i2a_ASN1_INTEGER(out, X509_get_serialNumber(x));
  629. BIO_printf(out, "\n");
  630. } else if (next_serial == i) {
  631. BIGNUM *bnser;
  632. ASN1_INTEGER *ser;
  633. ser = X509_get_serialNumber(x);
  634. bnser = ASN1_INTEGER_to_BN(ser, NULL);
  635. if (!bnser)
  636. goto end;
  637. if (!BN_add_word(bnser, 1))
  638. goto end;
  639. ser = BN_to_ASN1_INTEGER(bnser, NULL);
  640. if (!ser)
  641. goto end;
  642. BN_free(bnser);
  643. i2a_ASN1_INTEGER(out, ser);
  644. ASN1_INTEGER_free(ser);
  645. BIO_puts(out, "\n");
  646. } else if ((email == i) || (ocsp_uri == i)) {
  647. int j;
  648. STACK_OF(OPENSSL_STRING) *emlst;
  649. if (email == i)
  650. emlst = X509_get1_email(x);
  651. else
  652. emlst = X509_get1_ocsp(x);
  653. for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
  654. BIO_printf(out, "%s\n",
  655. sk_OPENSSL_STRING_value(emlst, j));
  656. X509_email_free(emlst);
  657. } else if (aliasout == i) {
  658. unsigned char *alstr;
  659. alstr = X509_alias_get0(x, NULL);
  660. if (alstr)
  661. BIO_printf(out, "%s\n", alstr);
  662. else
  663. BIO_puts(out, "<No Alias>\n");
  664. } else if (subject_hash == i) {
  665. BIO_printf(out, "%08lx\n", X509_subject_name_hash(x));
  666. }
  667. #ifndef OPENSSL_NO_MD5
  668. else if (subject_hash_old == i) {
  669. BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x));
  670. }
  671. #endif
  672. else if (issuer_hash == i) {
  673. BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x));
  674. }
  675. #ifndef OPENSSL_NO_MD5
  676. else if (issuer_hash_old == i) {
  677. BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x));
  678. }
  679. #endif
  680. else if (pprint == i) {
  681. X509_PURPOSE *ptmp;
  682. int j;
  683. BIO_printf(out, "Certificate purposes:\n");
  684. for (j = 0; j < X509_PURPOSE_get_count(); j++) {
  685. ptmp = X509_PURPOSE_get0(j);
  686. purpose_print(out, x, ptmp);
  687. }
  688. } else if (modulus == i) {
  689. EVP_PKEY *pkey;
  690. pkey = X509_get0_pubkey(x);
  691. if (pkey == NULL) {
  692. BIO_printf(bio_err, "Modulus=unavailable\n");
  693. ERR_print_errors(bio_err);
  694. goto end;
  695. }
  696. BIO_printf(out, "Modulus=");
  697. #ifndef OPENSSL_NO_RSA
  698. if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA)
  699. BN_print(out, EVP_PKEY_get0_RSA(pkey)->n);
  700. else
  701. #endif
  702. #ifndef OPENSSL_NO_DSA
  703. if (EVP_PKEY_id(pkey) == EVP_PKEY_DSA)
  704. BN_print(out, EVP_PKEY_get0_DSA(pkey)->pub_key);
  705. else
  706. #endif
  707. BIO_printf(out, "Wrong Algorithm type");
  708. BIO_printf(out, "\n");
  709. } else if (pubkey == i) {
  710. EVP_PKEY *pkey;
  711. pkey = X509_get0_pubkey(x);
  712. if (pkey == NULL) {
  713. BIO_printf(bio_err, "Error getting public key\n");
  714. ERR_print_errors(bio_err);
  715. goto end;
  716. }
  717. PEM_write_bio_PUBKEY(out, pkey);
  718. } else if (C == i) {
  719. unsigned char *d;
  720. char *m;
  721. int len;
  722. X509_NAME_oneline(X509_get_subject_name(x), buf, sizeof buf);
  723. BIO_printf(out, "/*\n"
  724. " * Subject: %s\n", buf);
  725. m = X509_NAME_oneline(X509_get_issuer_name(x), buf, sizeof buf);
  726. BIO_printf(out, " * Issuer: %s\n"
  727. " */\n", buf);
  728. len = i2d_X509(x, NULL);
  729. m = app_malloc(len, "x509 name buffer");
  730. d = (unsigned char *)m;
  731. len = i2d_X509_NAME(X509_get_subject_name(x), &d);
  732. print_array(out, "the_subject_name", len, (unsigned char *)m);
  733. d = (unsigned char *)m;
  734. len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d);
  735. print_array(out, "the_public_key", len, (unsigned char *)m);
  736. d = (unsigned char *)m;
  737. len = i2d_X509(x, &d);
  738. print_array(out, "the_certificate", len, (unsigned char *)m);
  739. OPENSSL_free(m);
  740. } else if (text == i) {
  741. X509_print_ex(out, x, nmflag, certflag);
  742. } else if (startdate == i) {
  743. BIO_puts(out, "notBefore=");
  744. ASN1_TIME_print(out, X509_get_notBefore(x));
  745. BIO_puts(out, "\n");
  746. } else if (enddate == i) {
  747. BIO_puts(out, "notAfter=");
  748. ASN1_TIME_print(out, X509_get_notAfter(x));
  749. BIO_puts(out, "\n");
  750. } else if (fingerprint == i) {
  751. int j;
  752. unsigned int n;
  753. unsigned char md[EVP_MAX_MD_SIZE];
  754. const EVP_MD *fdig = digest;
  755. if (!fdig)
  756. fdig = EVP_sha1();
  757. if (!X509_digest(x, fdig, md, &n)) {
  758. BIO_printf(bio_err, "out of memory\n");
  759. goto end;
  760. }
  761. BIO_printf(out, "%s Fingerprint=",
  762. OBJ_nid2sn(EVP_MD_type(fdig)));
  763. for (j = 0; j < (int)n; j++) {
  764. BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n)
  765. ? '\n' : ':');
  766. }
  767. }
  768. /* should be in the library */
  769. else if ((sign_flag == i) && (x509req == 0)) {
  770. BIO_printf(bio_err, "Getting Private key\n");
  771. if (Upkey == NULL) {
  772. Upkey = load_key(keyfile, keyformat, 0,
  773. passin, e, "Private key");
  774. if (Upkey == NULL)
  775. goto end;
  776. }
  777. assert(need_rand);
  778. if (!sign(x, Upkey, days, clrext, digest, extconf, extsect))
  779. goto end;
  780. } else if (CA_flag == i) {
  781. BIO_printf(bio_err, "Getting CA Private Key\n");
  782. if (CAkeyfile != NULL) {
  783. CApkey = load_key(CAkeyfile, CAkeyformat,
  784. 0, passin, e, "CA Private Key");
  785. if (CApkey == NULL)
  786. goto end;
  787. }
  788. assert(need_rand);
  789. if (!x509_certify(ctx, CAfile, digest, x, xca,
  790. CApkey, sigopts,
  791. CAserial, CA_createserial, days, clrext,
  792. extconf, extsect, sno, reqfile))
  793. goto end;
  794. } else if (x509req == i) {
  795. EVP_PKEY *pk;
  796. BIO_printf(bio_err, "Getting request Private Key\n");
  797. if (keyfile == NULL) {
  798. BIO_printf(bio_err, "no request key file specified\n");
  799. goto end;
  800. } else {
  801. pk = load_key(keyfile, keyformat, 0,
  802. passin, e, "request key");
  803. if (pk == NULL)
  804. goto end;
  805. }
  806. BIO_printf(bio_err, "Generating certificate request\n");
  807. rq = X509_to_X509_REQ(x, pk, digest);
  808. EVP_PKEY_free(pk);
  809. if (rq == NULL) {
  810. ERR_print_errors(bio_err);
  811. goto end;
  812. }
  813. if (!noout) {
  814. X509_REQ_print(out, rq);
  815. PEM_write_bio_X509_REQ(out, rq);
  816. }
  817. noout = 1;
  818. } else if (ocspid == i) {
  819. X509_ocspid_print(out, x);
  820. }
  821. }
  822. }
  823. if (checkend) {
  824. time_t tcheck = time(NULL) + checkoffset;
  825. if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) {
  826. BIO_printf(out, "Certificate will expire\n");
  827. ret = 1;
  828. } else {
  829. BIO_printf(out, "Certificate will not expire\n");
  830. ret = 0;
  831. }
  832. goto end;
  833. }
  834. print_cert_checks(out, x, checkhost, checkemail, checkip);
  835. if (noout || nocert) {
  836. ret = 0;
  837. goto end;
  838. }
  839. if (badsig) {
  840. ASN1_BIT_STRING *signature;
  841. unsigned char *s;
  842. X509_get0_signature(&signature, NULL, x);
  843. s = ASN1_STRING_data(signature);
  844. s[ASN1_STRING_length(signature) - 1] ^= 0x1;
  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. if (need_rand)
  865. app_RAND_write_file(NULL);
  866. OBJ_cleanup();
  867. NCONF_free(extconf);
  868. BIO_free_all(out);
  869. X509_STORE_free(ctx);
  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. X509_REQ_free(rq);
  878. ASN1_INTEGER_free(sno);
  879. sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
  880. sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
  881. OPENSSL_free(passin);
  882. return (ret);
  883. }
  884. static ASN1_INTEGER *x509_load_serial(char *CAfile, char *serialfile,
  885. int create)
  886. {
  887. char *buf = NULL, *p;
  888. ASN1_INTEGER *bs = NULL;
  889. BIGNUM *serial = NULL;
  890. size_t len;
  891. len = ((serialfile == NULL)
  892. ? (strlen(CAfile) + strlen(POSTFIX) + 1)
  893. : (strlen(serialfile))) + 1;
  894. buf = app_malloc(len, "serial# buffer");
  895. if (serialfile == NULL) {
  896. OPENSSL_strlcpy(buf, CAfile, len);
  897. for (p = buf; *p; p++)
  898. if (*p == '.') {
  899. *p = '\0';
  900. break;
  901. }
  902. OPENSSL_strlcat(buf, POSTFIX, len);
  903. } else
  904. OPENSSL_strlcpy(buf, serialfile, len);
  905. serial = load_serial(buf, create, NULL);
  906. if (serial == NULL)
  907. goto end;
  908. if (!BN_add_word(serial, 1)) {
  909. BIO_printf(bio_err, "add_word failure\n");
  910. goto end;
  911. }
  912. if (!save_serial(buf, NULL, serial, &bs))
  913. goto end;
  914. end:
  915. OPENSSL_free(buf);
  916. BN_free(serial);
  917. return bs;
  918. }
  919. static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
  920. X509 *x, X509 *xca, EVP_PKEY *pkey,
  921. STACK_OF(OPENSSL_STRING) *sigopts,
  922. char *serialfile, int create,
  923. int days, int clrext, CONF *conf, char *section,
  924. ASN1_INTEGER *sno, int reqfile)
  925. {
  926. int ret = 0;
  927. ASN1_INTEGER *bs = NULL;
  928. X509_STORE_CTX xsc;
  929. EVP_PKEY *upkey;
  930. upkey = X509_get0_pubkey(xca);
  931. EVP_PKEY_copy_parameters(upkey, pkey);
  932. if (!X509_STORE_CTX_init(&xsc, ctx, x, NULL)) {
  933. BIO_printf(bio_err, "Error initialising X509 store\n");
  934. goto end;
  935. }
  936. if (sno)
  937. bs = sno;
  938. else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL)
  939. goto end;
  940. /*
  941. * NOTE: this certificate can/should be self signed, unless it was a
  942. * certificate request in which case it is not.
  943. */
  944. X509_STORE_CTX_set_cert(&xsc, x);
  945. X509_STORE_CTX_set_flags(&xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
  946. if (!reqfile && X509_verify_cert(&xsc) <= 0)
  947. goto end;
  948. if (!X509_check_private_key(xca, pkey)) {
  949. BIO_printf(bio_err,
  950. "CA certificate and CA private key do not match\n");
  951. goto end;
  952. }
  953. if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
  954. goto end;
  955. if (!X509_set_serialNumber(x, bs))
  956. goto end;
  957. if (X509_gmtime_adj(X509_get_notBefore(x), 0L) == NULL)
  958. goto end;
  959. /* hardwired expired */
  960. if (X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL) == NULL)
  961. goto end;
  962. if (clrext) {
  963. while (X509_get_ext_count(x) > 0)
  964. X509_delete_ext(x, 0);
  965. }
  966. if (conf) {
  967. X509V3_CTX ctx2;
  968. #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  969. X509_set_version(x, force_version);
  970. #else
  971. X509_set_version(x, 2); /* version 3 certificate */
  972. #endif
  973. X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
  974. X509V3_set_nconf(&ctx2, conf);
  975. if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))
  976. goto end;
  977. }
  978. if (!do_X509_sign(x, pkey, digest, sigopts))
  979. goto end;
  980. ret = 1;
  981. end:
  982. X509_STORE_CTX_cleanup(&xsc);
  983. if (!ret)
  984. ERR_print_errors(bio_err);
  985. if (!sno)
  986. ASN1_INTEGER_free(bs);
  987. return ret;
  988. }
  989. static int callb(int ok, X509_STORE_CTX *ctx)
  990. {
  991. int err;
  992. X509 *err_cert;
  993. /*
  994. * it is ok to use a self signed certificate This case will catch both
  995. * the initial ok == 0 and the final ok == 1 calls to this function
  996. */
  997. err = X509_STORE_CTX_get_error(ctx);
  998. if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
  999. return 1;
  1000. /*
  1001. * BAD we should have gotten an error. Normally if everything worked
  1002. * X509_STORE_CTX_get_error(ctx) will still be set to
  1003. * DEPTH_ZERO_SELF_....
  1004. */
  1005. if (ok) {
  1006. BIO_printf(bio_err,
  1007. "error with certificate to be certified - should be self signed\n");
  1008. return 0;
  1009. } else {
  1010. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  1011. print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
  1012. BIO_printf(bio_err,
  1013. "error with certificate - error %d at depth %d\n%s\n", err,
  1014. X509_STORE_CTX_get_error_depth(ctx),
  1015. X509_verify_cert_error_string(err));
  1016. return 1;
  1017. }
  1018. }
  1019. /* self sign */
  1020. static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
  1021. const EVP_MD *digest, CONF *conf, char *section)
  1022. {
  1023. if (!X509_set_issuer_name(x, X509_get_subject_name(x)))
  1024. goto err;
  1025. if (X509_gmtime_adj(X509_get_notBefore(x), 0) == NULL)
  1026. goto err;
  1027. if (X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL) == NULL)
  1028. goto err;
  1029. if (!X509_set_pubkey(x, pkey))
  1030. goto err;
  1031. if (clrext) {
  1032. while (X509_get_ext_count(x) > 0)
  1033. X509_delete_ext(x, 0);
  1034. }
  1035. if (conf) {
  1036. X509V3_CTX ctx;
  1037. #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  1038. X509_set_version(x, force_version);
  1039. #else
  1040. X509_set_version(x, 2); /* version 3 certificate */
  1041. #endif
  1042. X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
  1043. X509V3_set_nconf(&ctx, conf);
  1044. if (!X509V3_EXT_add_nconf(conf, &ctx, section, x))
  1045. goto err;
  1046. }
  1047. if (!X509_sign(x, pkey, digest))
  1048. goto err;
  1049. return 1;
  1050. err:
  1051. ERR_print_errors(bio_err);
  1052. return 0;
  1053. }
  1054. static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
  1055. {
  1056. int id, i, idret;
  1057. char *pname;
  1058. id = X509_PURPOSE_get_id(pt);
  1059. pname = X509_PURPOSE_get0_name(pt);
  1060. for (i = 0; i < 2; i++) {
  1061. idret = X509_check_purpose(cert, id, i);
  1062. BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
  1063. if (idret == 1)
  1064. BIO_printf(bio, "Yes\n");
  1065. else if (idret == 0)
  1066. BIO_printf(bio, "No\n");
  1067. else
  1068. BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
  1069. }
  1070. return 1;
  1071. }