ca.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <ctype.h>
  13. #include <sys/types.h>
  14. #include <openssl/conf.h>
  15. #include <openssl/bio.h>
  16. #include <openssl/err.h>
  17. #include <openssl/bn.h>
  18. #include <openssl/txt_db.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/x509.h>
  21. #include <openssl/x509v3.h>
  22. #include <openssl/objects.h>
  23. #include <openssl/ocsp.h>
  24. #include <openssl/pem.h>
  25. #ifndef W_OK
  26. # ifdef OPENSSL_SYS_VMS
  27. # include <unistd.h>
  28. # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS)
  29. # include <sys/file.h>
  30. # endif
  31. #endif
  32. #include "apps.h"
  33. #include "progs.h"
  34. #ifndef W_OK
  35. # define F_OK 0
  36. # define W_OK 2
  37. # define R_OK 4
  38. #endif
  39. #ifndef PATH_MAX
  40. # define PATH_MAX 4096
  41. #endif
  42. #define BASE_SECTION "ca"
  43. #define ENV_DEFAULT_CA "default_ca"
  44. #define STRING_MASK "string_mask"
  45. #define UTF8_IN "utf8"
  46. #define ENV_NEW_CERTS_DIR "new_certs_dir"
  47. #define ENV_CERTIFICATE "certificate"
  48. #define ENV_SERIAL "serial"
  49. #define ENV_RAND_SERIAL "rand_serial"
  50. #define ENV_CRLNUMBER "crlnumber"
  51. #define ENV_PRIVATE_KEY "private_key"
  52. #define ENV_DEFAULT_DAYS "default_days"
  53. #define ENV_DEFAULT_STARTDATE "default_startdate"
  54. #define ENV_DEFAULT_ENDDATE "default_enddate"
  55. #define ENV_DEFAULT_CRL_DAYS "default_crl_days"
  56. #define ENV_DEFAULT_CRL_HOURS "default_crl_hours"
  57. #define ENV_DEFAULT_MD "default_md"
  58. #define ENV_DEFAULT_EMAIL_DN "email_in_dn"
  59. #define ENV_PRESERVE "preserve"
  60. #define ENV_POLICY "policy"
  61. #define ENV_EXTENSIONS "x509_extensions"
  62. #define ENV_CRLEXT "crl_extensions"
  63. #define ENV_MSIE_HACK "msie_hack"
  64. #define ENV_NAMEOPT "name_opt"
  65. #define ENV_CERTOPT "cert_opt"
  66. #define ENV_EXTCOPY "copy_extensions"
  67. #define ENV_UNIQUE_SUBJECT "unique_subject"
  68. #define ENV_DATABASE "database"
  69. /* Additional revocation information types */
  70. typedef enum {
  71. REV_VALID = -1, /* Valid (not-revoked) status */
  72. REV_NONE = 0, /* No additional information */
  73. REV_CRL_REASON = 1, /* Value is CRL reason code */
  74. REV_HOLD = 2, /* Value is hold instruction */
  75. REV_KEY_COMPROMISE = 3, /* Value is cert key compromise time */
  76. REV_CA_COMPROMISE = 4 /* Value is CA key compromise time */
  77. } REVINFO_TYPE;
  78. static char *lookup_conf(const CONF *conf, const char *group, const char *tag);
  79. static int certify(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509,
  80. const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
  81. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  82. BIGNUM *serial, const char *subj, unsigned long chtype,
  83. int multirdn, int email_dn, const char *startdate,
  84. const char *enddate,
  85. long days, int batch, const char *ext_sect, CONF *conf,
  86. int verbose, unsigned long certopt, unsigned long nameopt,
  87. int default_op, int ext_copy, int selfsign);
  88. static int certify_cert(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509,
  89. const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
  90. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  91. BIGNUM *serial, const char *subj, unsigned long chtype,
  92. int multirdn, int email_dn, const char *startdate,
  93. const char *enddate, long days, int batch, const char *ext_sect,
  94. CONF *conf, int verbose, unsigned long certopt,
  95. unsigned long nameopt, int default_op, int ext_copy);
  96. static int certify_spkac(X509 **xret, const char *infile, EVP_PKEY *pkey,
  97. X509 *x509, const EVP_MD *dgst,
  98. STACK_OF(OPENSSL_STRING) *sigopts,
  99. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  100. BIGNUM *serial, const char *subj, unsigned long chtype,
  101. int multirdn, int email_dn, const char *startdate,
  102. const char *enddate, long days, const char *ext_sect, CONF *conf,
  103. int verbose, unsigned long certopt,
  104. unsigned long nameopt, int default_op, int ext_copy);
  105. static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
  106. const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
  107. STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,
  108. const char *subj, unsigned long chtype, int multirdn,
  109. int email_dn, const char *startdate, const char *enddate, long days,
  110. int batch, int verbose, X509_REQ *req, const char *ext_sect,
  111. CONF *conf, unsigned long certopt, unsigned long nameopt,
  112. int default_op, int ext_copy, int selfsign);
  113. static int get_certificate_status(const char *ser_status, CA_DB *db);
  114. static int do_updatedb(CA_DB *db);
  115. static int check_time_format(const char *str);
  116. static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type,
  117. const char *extval);
  118. static char *make_revocation_str(REVINFO_TYPE rev_type, const char *rev_arg);
  119. static int make_revoked(X509_REVOKED *rev, const char *str);
  120. static int old_entry_print(const ASN1_OBJECT *obj, const ASN1_STRING *str);
  121. static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext);
  122. static CONF *extconf = NULL;
  123. static int preserve = 0;
  124. static int msie_hack = 0;
  125. typedef enum OPTION_choice {
  126. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  127. OPT_ENGINE, OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SUBJ, OPT_UTF8,
  128. OPT_CREATE_SERIAL, OPT_MULTIVALUE_RDN, OPT_STARTDATE, OPT_ENDDATE,
  129. OPT_DAYS, OPT_MD, OPT_POLICY, OPT_KEYFILE, OPT_KEYFORM, OPT_PASSIN,
  130. OPT_KEY, OPT_CERT, OPT_SELFSIGN, OPT_IN, OPT_OUT, OPT_OUTDIR,
  131. OPT_SIGOPT, OPT_NOTEXT, OPT_BATCH, OPT_PRESERVEDN, OPT_NOEMAILDN,
  132. OPT_GENCRL, OPT_MSIE_HACK, OPT_CRLDAYS, OPT_CRLHOURS, OPT_CRLSEC,
  133. OPT_INFILES, OPT_SS_CERT, OPT_SPKAC, OPT_REVOKE, OPT_VALID,
  134. OPT_EXTENSIONS, OPT_EXTFILE, OPT_STATUS, OPT_UPDATEDB, OPT_CRLEXTS,
  135. OPT_RAND_SERIAL,
  136. OPT_R_ENUM,
  137. /* Do not change the order here; see related case statements below */
  138. OPT_CRL_REASON, OPT_CRL_HOLD, OPT_CRL_COMPROMISE, OPT_CRL_CA_COMPROMISE
  139. } OPTION_CHOICE;
  140. const OPTIONS ca_options[] = {
  141. {"help", OPT_HELP, '-', "Display this summary"},
  142. {"verbose", OPT_VERBOSE, '-', "Verbose output during processing"},
  143. {"config", OPT_CONFIG, 's', "A config file"},
  144. {"name", OPT_NAME, 's', "The particular CA definition to use"},
  145. {"subj", OPT_SUBJ, 's', "Use arg instead of request's subject"},
  146. {"utf8", OPT_UTF8, '-', "Input characters are UTF8 (default ASCII)"},
  147. {"create_serial", OPT_CREATE_SERIAL, '-',
  148. "If reading serial fails, create a new random serial"},
  149. {"rand_serial", OPT_RAND_SERIAL, '-',
  150. "Always create a random serial; do not store it"},
  151. {"multivalue-rdn", OPT_MULTIVALUE_RDN, '-',
  152. "Enable support for multivalued RDNs"},
  153. {"startdate", OPT_STARTDATE, 's', "Cert notBefore, YYMMDDHHMMSSZ"},
  154. {"enddate", OPT_ENDDATE, 's',
  155. "YYMMDDHHMMSSZ cert notAfter (overrides -days)"},
  156. {"days", OPT_DAYS, 'p', "Number of days to certify the cert for"},
  157. {"md", OPT_MD, 's', "md to use; one of md2, md5, sha or sha1"},
  158. {"policy", OPT_POLICY, 's', "The CA 'policy' to support"},
  159. {"keyfile", OPT_KEYFILE, 's', "Private key"},
  160. {"keyform", OPT_KEYFORM, 'f', "Private key file format (PEM or ENGINE)"},
  161. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  162. {"key", OPT_KEY, 's', "Key to decode the private key if it is encrypted"},
  163. {"cert", OPT_CERT, '<', "The CA cert"},
  164. {"selfsign", OPT_SELFSIGN, '-',
  165. "Sign a cert with the key associated with it"},
  166. {"in", OPT_IN, '<', "The input PEM encoded cert request(s)"},
  167. {"out", OPT_OUT, '>', "Where to put the output file(s)"},
  168. {"outdir", OPT_OUTDIR, '/', "Where to put output cert"},
  169. {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"},
  170. {"notext", OPT_NOTEXT, '-', "Do not print the generated certificate"},
  171. {"batch", OPT_BATCH, '-', "Don't ask questions"},
  172. {"preserveDN", OPT_PRESERVEDN, '-', "Don't re-order the DN"},
  173. {"noemailDN", OPT_NOEMAILDN, '-', "Don't add the EMAIL field to the DN"},
  174. {"gencrl", OPT_GENCRL, '-', "Generate a new CRL"},
  175. {"msie_hack", OPT_MSIE_HACK, '-',
  176. "msie modifications to handle all those universal strings"},
  177. {"crldays", OPT_CRLDAYS, 'p', "Days until the next CRL is due"},
  178. {"crlhours", OPT_CRLHOURS, 'p', "Hours until the next CRL is due"},
  179. {"crlsec", OPT_CRLSEC, 'p', "Seconds until the next CRL is due"},
  180. {"infiles", OPT_INFILES, '-', "The last argument, requests to process"},
  181. {"ss_cert", OPT_SS_CERT, '<', "File contains a self signed cert to sign"},
  182. {"spkac", OPT_SPKAC, '<',
  183. "File contains DN and signed public key and challenge"},
  184. {"revoke", OPT_REVOKE, '<', "Revoke a cert (given in file)"},
  185. {"valid", OPT_VALID, 's',
  186. "Add a Valid(not-revoked) DB entry about a cert (given in file)"},
  187. {"extensions", OPT_EXTENSIONS, 's',
  188. "Extension section (override value in config file)"},
  189. {"extfile", OPT_EXTFILE, '<',
  190. "Configuration file with X509v3 extensions to add"},
  191. {"status", OPT_STATUS, 's', "Shows cert status given the serial number"},
  192. {"updatedb", OPT_UPDATEDB, '-', "Updates db for expired cert"},
  193. {"crlexts", OPT_CRLEXTS, 's',
  194. "CRL extension section (override value in config file)"},
  195. {"crl_reason", OPT_CRL_REASON, 's', "revocation reason"},
  196. {"crl_hold", OPT_CRL_HOLD, 's',
  197. "the hold instruction, an OID. Sets revocation reason to certificateHold"},
  198. {"crl_compromise", OPT_CRL_COMPROMISE, 's',
  199. "sets compromise time to val and the revocation reason to keyCompromise"},
  200. {"crl_CA_compromise", OPT_CRL_CA_COMPROMISE, 's',
  201. "sets compromise time to val and the revocation reason to CACompromise"},
  202. OPT_R_OPTIONS,
  203. #ifndef OPENSSL_NO_ENGINE
  204. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  205. #endif
  206. {NULL}
  207. };
  208. int ca_main(int argc, char **argv)
  209. {
  210. CONF *conf = NULL;
  211. ENGINE *e = NULL;
  212. BIGNUM *crlnumber = NULL, *serial = NULL;
  213. EVP_PKEY *pkey = NULL;
  214. BIO *in = NULL, *out = NULL, *Sout = NULL;
  215. ASN1_INTEGER *tmpser;
  216. ASN1_TIME *tmptm;
  217. CA_DB *db = NULL;
  218. DB_ATTR db_attr;
  219. STACK_OF(CONF_VALUE) *attribs = NULL;
  220. STACK_OF(OPENSSL_STRING) *sigopts = NULL;
  221. STACK_OF(X509) *cert_sk = NULL;
  222. X509_CRL *crl = NULL;
  223. const EVP_MD *dgst = NULL;
  224. char *configfile = default_config_file, *section = NULL;
  225. char *md = NULL, *policy = NULL, *keyfile = NULL;
  226. char *certfile = NULL, *crl_ext = NULL, *crlnumberfile = NULL, *key = NULL;
  227. const char *infile = NULL, *spkac_file = NULL, *ss_cert_file = NULL;
  228. const char *extensions = NULL, *extfile = NULL, *passinarg = NULL;
  229. char *outdir = NULL, *outfile = NULL, *rev_arg = NULL, *ser_status = NULL;
  230. const char *serialfile = NULL, *subj = NULL;
  231. char *prog, *startdate = NULL, *enddate = NULL;
  232. char *dbfile = NULL, *f;
  233. char new_cert[PATH_MAX];
  234. char tmp[10 + 1] = "\0";
  235. char *const *pp;
  236. const char *p;
  237. size_t outdirlen = 0;
  238. int create_ser = 0, free_key = 0, total = 0, total_done = 0;
  239. int batch = 0, default_op = 1, doupdatedb = 0, ext_copy = EXT_COPY_NONE;
  240. int keyformat = FORMAT_PEM, multirdn = 0, notext = 0, output_der = 0;
  241. int ret = 1, email_dn = 1, req = 0, verbose = 0, gencrl = 0, dorevoke = 0;
  242. int rand_ser = 0, i, j, selfsign = 0;
  243. long crldays = 0, crlhours = 0, crlsec = 0, days = 0;
  244. unsigned long chtype = MBSTRING_ASC, certopt = 0;
  245. X509 *x509 = NULL, *x509p = NULL, *x = NULL;
  246. REVINFO_TYPE rev_type = REV_NONE;
  247. X509_REVOKED *r = NULL;
  248. OPTION_CHOICE o;
  249. prog = opt_init(argc, argv, ca_options);
  250. while ((o = opt_next()) != OPT_EOF) {
  251. switch (o) {
  252. case OPT_EOF:
  253. case OPT_ERR:
  254. opthelp:
  255. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  256. goto end;
  257. case OPT_HELP:
  258. opt_help(ca_options);
  259. ret = 0;
  260. goto end;
  261. case OPT_IN:
  262. req = 1;
  263. infile = opt_arg();
  264. break;
  265. case OPT_OUT:
  266. outfile = opt_arg();
  267. break;
  268. case OPT_VERBOSE:
  269. verbose = 1;
  270. break;
  271. case OPT_CONFIG:
  272. configfile = opt_arg();
  273. break;
  274. case OPT_NAME:
  275. section = opt_arg();
  276. break;
  277. case OPT_SUBJ:
  278. subj = opt_arg();
  279. /* preserve=1; */
  280. break;
  281. case OPT_UTF8:
  282. chtype = MBSTRING_UTF8;
  283. break;
  284. case OPT_RAND_SERIAL:
  285. rand_ser = 1;
  286. break;
  287. case OPT_CREATE_SERIAL:
  288. create_ser = 1;
  289. break;
  290. case OPT_MULTIVALUE_RDN:
  291. multirdn = 1;
  292. break;
  293. case OPT_STARTDATE:
  294. startdate = opt_arg();
  295. break;
  296. case OPT_ENDDATE:
  297. enddate = opt_arg();
  298. break;
  299. case OPT_DAYS:
  300. days = atoi(opt_arg());
  301. break;
  302. case OPT_MD:
  303. md = opt_arg();
  304. break;
  305. case OPT_POLICY:
  306. policy = opt_arg();
  307. break;
  308. case OPT_KEYFILE:
  309. keyfile = opt_arg();
  310. break;
  311. case OPT_KEYFORM:
  312. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
  313. goto opthelp;
  314. break;
  315. case OPT_PASSIN:
  316. passinarg = opt_arg();
  317. break;
  318. case OPT_R_CASES:
  319. if (!opt_rand(o))
  320. goto end;
  321. break;
  322. case OPT_KEY:
  323. key = opt_arg();
  324. break;
  325. case OPT_CERT:
  326. certfile = opt_arg();
  327. break;
  328. case OPT_SELFSIGN:
  329. selfsign = 1;
  330. break;
  331. case OPT_OUTDIR:
  332. outdir = opt_arg();
  333. break;
  334. case OPT_SIGOPT:
  335. if (sigopts == NULL)
  336. sigopts = sk_OPENSSL_STRING_new_null();
  337. if (sigopts == NULL || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
  338. goto end;
  339. break;
  340. case OPT_NOTEXT:
  341. notext = 1;
  342. break;
  343. case OPT_BATCH:
  344. batch = 1;
  345. break;
  346. case OPT_PRESERVEDN:
  347. preserve = 1;
  348. break;
  349. case OPT_NOEMAILDN:
  350. email_dn = 0;
  351. break;
  352. case OPT_GENCRL:
  353. gencrl = 1;
  354. break;
  355. case OPT_MSIE_HACK:
  356. msie_hack = 1;
  357. break;
  358. case OPT_CRLDAYS:
  359. crldays = atol(opt_arg());
  360. break;
  361. case OPT_CRLHOURS:
  362. crlhours = atol(opt_arg());
  363. break;
  364. case OPT_CRLSEC:
  365. crlsec = atol(opt_arg());
  366. break;
  367. case OPT_INFILES:
  368. req = 1;
  369. goto end_of_options;
  370. case OPT_SS_CERT:
  371. ss_cert_file = opt_arg();
  372. req = 1;
  373. break;
  374. case OPT_SPKAC:
  375. spkac_file = opt_arg();
  376. req = 1;
  377. break;
  378. case OPT_REVOKE:
  379. infile = opt_arg();
  380. dorevoke = 1;
  381. break;
  382. case OPT_VALID:
  383. infile = opt_arg();
  384. dorevoke = 2;
  385. break;
  386. case OPT_EXTENSIONS:
  387. extensions = opt_arg();
  388. break;
  389. case OPT_EXTFILE:
  390. extfile = opt_arg();
  391. break;
  392. case OPT_STATUS:
  393. ser_status = opt_arg();
  394. break;
  395. case OPT_UPDATEDB:
  396. doupdatedb = 1;
  397. break;
  398. case OPT_CRLEXTS:
  399. crl_ext = opt_arg();
  400. break;
  401. case OPT_CRL_REASON: /* := REV_CRL_REASON */
  402. case OPT_CRL_HOLD:
  403. case OPT_CRL_COMPROMISE:
  404. case OPT_CRL_CA_COMPROMISE:
  405. rev_arg = opt_arg();
  406. rev_type = (o - OPT_CRL_REASON) + REV_CRL_REASON;
  407. break;
  408. case OPT_ENGINE:
  409. e = setup_engine(opt_arg(), 0);
  410. break;
  411. }
  412. }
  413. end_of_options:
  414. argc = opt_num_rest();
  415. argv = opt_rest();
  416. BIO_printf(bio_err, "Using configuration from %s\n", configfile);
  417. if ((conf = app_load_config(configfile)) == NULL)
  418. goto end;
  419. if (configfile != default_config_file && !app_load_modules(conf))
  420. goto end;
  421. /* Lets get the config section we are using */
  422. if (section == NULL
  423. && (section = lookup_conf(conf, BASE_SECTION, ENV_DEFAULT_CA)) == NULL)
  424. goto end;
  425. p = NCONF_get_string(conf, NULL, "oid_file");
  426. if (p == NULL)
  427. ERR_clear_error();
  428. if (p != NULL) {
  429. BIO *oid_bio = BIO_new_file(p, "r");
  430. if (oid_bio == NULL) {
  431. ERR_clear_error();
  432. } else {
  433. OBJ_create_objects(oid_bio);
  434. BIO_free(oid_bio);
  435. }
  436. }
  437. if (!add_oid_section(conf)) {
  438. ERR_print_errors(bio_err);
  439. goto end;
  440. }
  441. app_RAND_load_conf(conf, BASE_SECTION);
  442. f = NCONF_get_string(conf, section, STRING_MASK);
  443. if (f == NULL)
  444. ERR_clear_error();
  445. if (f != NULL && !ASN1_STRING_set_default_mask_asc(f)) {
  446. BIO_printf(bio_err, "Invalid global string mask setting %s\n", f);
  447. goto end;
  448. }
  449. if (chtype != MBSTRING_UTF8) {
  450. f = NCONF_get_string(conf, section, UTF8_IN);
  451. if (f == NULL)
  452. ERR_clear_error();
  453. else if (strcmp(f, "yes") == 0)
  454. chtype = MBSTRING_UTF8;
  455. }
  456. db_attr.unique_subject = 1;
  457. p = NCONF_get_string(conf, section, ENV_UNIQUE_SUBJECT);
  458. if (p != NULL)
  459. db_attr.unique_subject = parse_yesno(p, 1);
  460. else
  461. ERR_clear_error();
  462. /*****************************************************************/
  463. /* report status of cert with serial number given on command line */
  464. if (ser_status) {
  465. dbfile = lookup_conf(conf, section, ENV_DATABASE);
  466. if (dbfile == NULL)
  467. goto end;
  468. db = load_index(dbfile, &db_attr);
  469. if (db == NULL)
  470. goto end;
  471. if (index_index(db) <= 0)
  472. goto end;
  473. if (get_certificate_status(ser_status, db) != 1)
  474. BIO_printf(bio_err, "Error verifying serial %s!\n", ser_status);
  475. goto end;
  476. }
  477. /*****************************************************************/
  478. /* we definitely need a private key, so let's get it */
  479. if (keyfile == NULL
  480. && (keyfile = lookup_conf(conf, section, ENV_PRIVATE_KEY)) == NULL)
  481. goto end;
  482. if (key == NULL) {
  483. free_key = 1;
  484. if (!app_passwd(passinarg, NULL, &key, NULL)) {
  485. BIO_printf(bio_err, "Error getting password\n");
  486. goto end;
  487. }
  488. }
  489. pkey = load_key(keyfile, keyformat, 0, key, e, "CA private key");
  490. if (key != NULL)
  491. OPENSSL_cleanse(key, strlen(key));
  492. if (pkey == NULL)
  493. /* load_key() has already printed an appropriate message */
  494. goto end;
  495. /*****************************************************************/
  496. /* we need a certificate */
  497. if (!selfsign || spkac_file || ss_cert_file || gencrl) {
  498. if (certfile == NULL
  499. && (certfile = lookup_conf(conf, section, ENV_CERTIFICATE)) == NULL)
  500. goto end;
  501. x509 = load_cert(certfile, FORMAT_PEM, "CA certificate");
  502. if (x509 == NULL)
  503. goto end;
  504. if (!X509_check_private_key(x509, pkey)) {
  505. BIO_printf(bio_err,
  506. "CA certificate and CA private key do not match\n");
  507. goto end;
  508. }
  509. }
  510. if (!selfsign)
  511. x509p = x509;
  512. f = NCONF_get_string(conf, BASE_SECTION, ENV_PRESERVE);
  513. if (f == NULL)
  514. ERR_clear_error();
  515. if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
  516. preserve = 1;
  517. f = NCONF_get_string(conf, BASE_SECTION, ENV_MSIE_HACK);
  518. if (f == NULL)
  519. ERR_clear_error();
  520. if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
  521. msie_hack = 1;
  522. f = NCONF_get_string(conf, section, ENV_NAMEOPT);
  523. if (f != NULL) {
  524. if (!set_nameopt(f)) {
  525. BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f);
  526. goto end;
  527. }
  528. default_op = 0;
  529. }
  530. f = NCONF_get_string(conf, section, ENV_CERTOPT);
  531. if (f != NULL) {
  532. if (!set_cert_ex(&certopt, f)) {
  533. BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f);
  534. goto end;
  535. }
  536. default_op = 0;
  537. } else {
  538. ERR_clear_error();
  539. }
  540. f = NCONF_get_string(conf, section, ENV_EXTCOPY);
  541. if (f != NULL) {
  542. if (!set_ext_copy(&ext_copy, f)) {
  543. BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f);
  544. goto end;
  545. }
  546. } else {
  547. ERR_clear_error();
  548. }
  549. /*****************************************************************/
  550. /* lookup where to write new certificates */
  551. if ((outdir == NULL) && (req)) {
  552. outdir = NCONF_get_string(conf, section, ENV_NEW_CERTS_DIR);
  553. if (outdir == NULL) {
  554. BIO_printf(bio_err,
  555. "there needs to be defined a directory for new certificate to be placed in\n");
  556. goto end;
  557. }
  558. #ifndef OPENSSL_SYS_VMS
  559. /*
  560. * outdir is a directory spec, but access() for VMS demands a
  561. * filename. We could use the DEC C routine to convert the
  562. * directory syntax to Unixly, and give that to app_isdir,
  563. * but for now the fopen will catch the error if it's not a
  564. * directory
  565. */
  566. if (app_isdir(outdir) <= 0) {
  567. BIO_printf(bio_err, "%s: %s is not a directory\n", prog, outdir);
  568. perror(outdir);
  569. goto end;
  570. }
  571. #endif
  572. }
  573. /*****************************************************************/
  574. /* we need to load the database file */
  575. dbfile = lookup_conf(conf, section, ENV_DATABASE);
  576. if (dbfile == NULL)
  577. goto end;
  578. db = load_index(dbfile, &db_attr);
  579. if (db == NULL)
  580. goto end;
  581. /* Lets check some fields */
  582. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  583. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  584. if ((pp[DB_type][0] != DB_TYPE_REV) && (pp[DB_rev_date][0] != '\0')) {
  585. BIO_printf(bio_err,
  586. "entry %d: not revoked yet, but has a revocation date\n",
  587. i + 1);
  588. goto end;
  589. }
  590. if ((pp[DB_type][0] == DB_TYPE_REV) &&
  591. !make_revoked(NULL, pp[DB_rev_date])) {
  592. BIO_printf(bio_err, " in entry %d\n", i + 1);
  593. goto end;
  594. }
  595. if (!check_time_format((char *)pp[DB_exp_date])) {
  596. BIO_printf(bio_err, "entry %d: invalid expiry date\n", i + 1);
  597. goto end;
  598. }
  599. p = pp[DB_serial];
  600. j = strlen(p);
  601. if (*p == '-') {
  602. p++;
  603. j--;
  604. }
  605. if ((j & 1) || (j < 2)) {
  606. BIO_printf(bio_err, "entry %d: bad serial number length (%d)\n",
  607. i + 1, j);
  608. goto end;
  609. }
  610. for ( ; *p; p++) {
  611. if (!isxdigit(_UC(*p))) {
  612. BIO_printf(bio_err,
  613. "entry %d: bad char 0%o '%c' in serial number\n",
  614. i + 1, *p, *p);
  615. goto end;
  616. }
  617. }
  618. }
  619. if (verbose) {
  620. TXT_DB_write(bio_out, db->db);
  621. BIO_printf(bio_err, "%d entries loaded from the database\n",
  622. sk_OPENSSL_PSTRING_num(db->db->data));
  623. BIO_printf(bio_err, "generating index\n");
  624. }
  625. if (index_index(db) <= 0)
  626. goto end;
  627. /*****************************************************************/
  628. /* Update the db file for expired certificates */
  629. if (doupdatedb) {
  630. if (verbose)
  631. BIO_printf(bio_err, "Updating %s ...\n", dbfile);
  632. i = do_updatedb(db);
  633. if (i == -1) {
  634. BIO_printf(bio_err, "Malloc failure\n");
  635. goto end;
  636. } else if (i == 0) {
  637. if (verbose)
  638. BIO_printf(bio_err, "No entries found to mark expired\n");
  639. } else {
  640. if (!save_index(dbfile, "new", db))
  641. goto end;
  642. if (!rotate_index(dbfile, "new", "old"))
  643. goto end;
  644. if (verbose)
  645. BIO_printf(bio_err, "Done. %d entries marked as expired\n", i);
  646. }
  647. }
  648. /*****************************************************************/
  649. /* Read extensions config file */
  650. if (extfile) {
  651. if ((extconf = app_load_config(extfile)) == NULL) {
  652. ret = 1;
  653. goto end;
  654. }
  655. if (verbose)
  656. BIO_printf(bio_err, "Successfully loaded extensions file %s\n",
  657. extfile);
  658. /* We can have sections in the ext file */
  659. if (extensions == NULL) {
  660. extensions = NCONF_get_string(extconf, "default", "extensions");
  661. if (extensions == NULL)
  662. extensions = "default";
  663. }
  664. }
  665. /*****************************************************************/
  666. if (req || gencrl) {
  667. if (spkac_file != NULL) {
  668. output_der = 1;
  669. batch = 1;
  670. }
  671. Sout = bio_open_default(outfile, 'w',
  672. output_der ? FORMAT_ASN1 : FORMAT_TEXT);
  673. if (Sout == NULL)
  674. goto end;
  675. }
  676. if (md == NULL && (md = lookup_conf(conf, section, ENV_DEFAULT_MD)) == NULL)
  677. goto end;
  678. if (strcmp(md, "null") == 0) {
  679. dgst = EVP_md_null();
  680. } else {
  681. if (strcmp(md, "default") == 0) {
  682. int def_nid;
  683. if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0) {
  684. BIO_puts(bio_err, "no default digest\n");
  685. goto end;
  686. }
  687. md = (char *)OBJ_nid2sn(def_nid);
  688. }
  689. if (!opt_md(md, &dgst)) {
  690. goto end;
  691. }
  692. }
  693. if (req) {
  694. if (email_dn == 1) {
  695. char *tmp_email_dn = NULL;
  696. tmp_email_dn = NCONF_get_string(conf, section, ENV_DEFAULT_EMAIL_DN);
  697. if (tmp_email_dn != NULL && strcmp(tmp_email_dn, "no") == 0)
  698. email_dn = 0;
  699. }
  700. if (verbose)
  701. BIO_printf(bio_err, "message digest is %s\n",
  702. OBJ_nid2ln(EVP_MD_type(dgst)));
  703. if (policy == NULL
  704. && (policy = lookup_conf(conf, section, ENV_POLICY)) == NULL)
  705. goto end;
  706. if (verbose)
  707. BIO_printf(bio_err, "policy is %s\n", policy);
  708. if (NCONF_get_string(conf, section, ENV_RAND_SERIAL) != NULL) {
  709. rand_ser = 1;
  710. } else {
  711. serialfile = lookup_conf(conf, section, ENV_SERIAL);
  712. if (serialfile == NULL)
  713. goto end;
  714. }
  715. if (extconf == NULL) {
  716. /*
  717. * no '-extfile' option, so we look for extensions in the main
  718. * configuration file
  719. */
  720. if (extensions == NULL) {
  721. extensions = NCONF_get_string(conf, section, ENV_EXTENSIONS);
  722. if (extensions == NULL)
  723. ERR_clear_error();
  724. }
  725. if (extensions != NULL) {
  726. /* Check syntax of file */
  727. X509V3_CTX ctx;
  728. X509V3_set_ctx_test(&ctx);
  729. X509V3_set_nconf(&ctx, conf);
  730. if (!X509V3_EXT_add_nconf(conf, &ctx, extensions, NULL)) {
  731. BIO_printf(bio_err,
  732. "Error Loading extension section %s\n",
  733. extensions);
  734. ret = 1;
  735. goto end;
  736. }
  737. }
  738. }
  739. if (startdate == NULL) {
  740. startdate = NCONF_get_string(conf, section, ENV_DEFAULT_STARTDATE);
  741. if (startdate == NULL)
  742. ERR_clear_error();
  743. }
  744. if (startdate != NULL && !ASN1_TIME_set_string_X509(NULL, startdate)) {
  745. BIO_printf(bio_err,
  746. "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
  747. goto end;
  748. }
  749. if (startdate == NULL)
  750. startdate = "today";
  751. if (enddate == NULL) {
  752. enddate = NCONF_get_string(conf, section, ENV_DEFAULT_ENDDATE);
  753. if (enddate == NULL)
  754. ERR_clear_error();
  755. }
  756. if (enddate != NULL && !ASN1_TIME_set_string_X509(NULL, enddate)) {
  757. BIO_printf(bio_err,
  758. "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
  759. goto end;
  760. }
  761. if (days == 0) {
  762. if (!NCONF_get_number(conf, section, ENV_DEFAULT_DAYS, &days))
  763. days = 0;
  764. }
  765. if (enddate == NULL && days == 0) {
  766. BIO_printf(bio_err, "cannot lookup how many days to certify for\n");
  767. goto end;
  768. }
  769. if (rand_ser) {
  770. if ((serial = BN_new()) == NULL || !rand_serial(serial, NULL)) {
  771. BIO_printf(bio_err, "error generating serial number\n");
  772. goto end;
  773. }
  774. } else {
  775. if ((serial = load_serial(serialfile, create_ser, NULL)) == NULL) {
  776. BIO_printf(bio_err, "error while loading serial number\n");
  777. goto end;
  778. }
  779. if (verbose) {
  780. if (BN_is_zero(serial)) {
  781. BIO_printf(bio_err, "next serial number is 00\n");
  782. } else {
  783. if ((f = BN_bn2hex(serial)) == NULL)
  784. goto end;
  785. BIO_printf(bio_err, "next serial number is %s\n", f);
  786. OPENSSL_free(f);
  787. }
  788. }
  789. }
  790. if ((attribs = NCONF_get_section(conf, policy)) == NULL) {
  791. BIO_printf(bio_err, "unable to find 'section' for %s\n", policy);
  792. goto end;
  793. }
  794. if ((cert_sk = sk_X509_new_null()) == NULL) {
  795. BIO_printf(bio_err, "Memory allocation failure\n");
  796. goto end;
  797. }
  798. if (spkac_file != NULL) {
  799. total++;
  800. j = certify_spkac(&x, spkac_file, pkey, x509, dgst, sigopts,
  801. attribs, db, serial, subj, chtype, multirdn,
  802. email_dn, startdate, enddate, days, extensions,
  803. conf, verbose, certopt, get_nameopt(), default_op,
  804. ext_copy);
  805. if (j < 0)
  806. goto end;
  807. if (j > 0) {
  808. total_done++;
  809. BIO_printf(bio_err, "\n");
  810. if (!BN_add_word(serial, 1))
  811. goto end;
  812. if (!sk_X509_push(cert_sk, x)) {
  813. BIO_printf(bio_err, "Memory allocation failure\n");
  814. goto end;
  815. }
  816. }
  817. }
  818. if (ss_cert_file != NULL) {
  819. total++;
  820. j = certify_cert(&x, ss_cert_file, pkey, x509, dgst, sigopts,
  821. attribs,
  822. db, serial, subj, chtype, multirdn, email_dn,
  823. startdate, enddate, days, batch, extensions,
  824. conf, verbose, certopt, get_nameopt(), default_op,
  825. ext_copy);
  826. if (j < 0)
  827. goto end;
  828. if (j > 0) {
  829. total_done++;
  830. BIO_printf(bio_err, "\n");
  831. if (!BN_add_word(serial, 1))
  832. goto end;
  833. if (!sk_X509_push(cert_sk, x)) {
  834. BIO_printf(bio_err, "Memory allocation failure\n");
  835. goto end;
  836. }
  837. }
  838. }
  839. if (infile != NULL) {
  840. total++;
  841. j = certify(&x, infile, pkey, x509p, dgst, sigopts, attribs, db,
  842. serial, subj, chtype, multirdn, email_dn, startdate,
  843. enddate, days, batch, extensions, conf, verbose,
  844. certopt, get_nameopt(), default_op, ext_copy, selfsign);
  845. if (j < 0)
  846. goto end;
  847. if (j > 0) {
  848. total_done++;
  849. BIO_printf(bio_err, "\n");
  850. if (!BN_add_word(serial, 1))
  851. goto end;
  852. if (!sk_X509_push(cert_sk, x)) {
  853. BIO_printf(bio_err, "Memory allocation failure\n");
  854. goto end;
  855. }
  856. }
  857. }
  858. for (i = 0; i < argc; i++) {
  859. total++;
  860. j = certify(&x, argv[i], pkey, x509p, dgst, sigopts, attribs, db,
  861. serial, subj, chtype, multirdn, email_dn, startdate,
  862. enddate, days, batch, extensions, conf, verbose,
  863. certopt, get_nameopt(), default_op, ext_copy, selfsign);
  864. if (j < 0)
  865. goto end;
  866. if (j > 0) {
  867. total_done++;
  868. BIO_printf(bio_err, "\n");
  869. if (!BN_add_word(serial, 1)) {
  870. X509_free(x);
  871. goto end;
  872. }
  873. if (!sk_X509_push(cert_sk, x)) {
  874. BIO_printf(bio_err, "Memory allocation failure\n");
  875. X509_free(x);
  876. goto end;
  877. }
  878. }
  879. }
  880. /*
  881. * we have a stack of newly certified certificates and a data base
  882. * and serial number that need updating
  883. */
  884. if (sk_X509_num(cert_sk) > 0) {
  885. if (!batch) {
  886. BIO_printf(bio_err,
  887. "\n%d out of %d certificate requests certified, commit? [y/n]",
  888. total_done, total);
  889. (void)BIO_flush(bio_err);
  890. tmp[0] = '\0';
  891. if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
  892. BIO_printf(bio_err, "CERTIFICATION CANCELED: I/O error\n");
  893. ret = 0;
  894. goto end;
  895. }
  896. if (tmp[0] != 'y' && tmp[0] != 'Y') {
  897. BIO_printf(bio_err, "CERTIFICATION CANCELED\n");
  898. ret = 0;
  899. goto end;
  900. }
  901. }
  902. BIO_printf(bio_err, "Write out database with %d new entries\n",
  903. sk_X509_num(cert_sk));
  904. if (!rand_ser
  905. && !save_serial(serialfile, "new", serial, NULL))
  906. goto end;
  907. if (!save_index(dbfile, "new", db))
  908. goto end;
  909. }
  910. outdirlen = OPENSSL_strlcpy(new_cert, outdir, sizeof(new_cert));
  911. #ifndef OPENSSL_SYS_VMS
  912. outdirlen = OPENSSL_strlcat(new_cert, "/", sizeof(new_cert));
  913. #endif
  914. if (verbose)
  915. BIO_printf(bio_err, "writing new certificates\n");
  916. for (i = 0; i < sk_X509_num(cert_sk); i++) {
  917. BIO *Cout = NULL;
  918. X509 *xi = sk_X509_value(cert_sk, i);
  919. ASN1_INTEGER *serialNumber = X509_get_serialNumber(xi);
  920. const unsigned char *psn = ASN1_STRING_get0_data(serialNumber);
  921. const int snl = ASN1_STRING_length(serialNumber);
  922. const int filen_len = 2 * (snl > 0 ? snl : 1) + sizeof(".pem");
  923. char *n = new_cert + outdirlen;
  924. if (outdirlen + filen_len > PATH_MAX) {
  925. BIO_printf(bio_err, "certificate file name too long\n");
  926. goto end;
  927. }
  928. if (snl > 0) {
  929. static const char HEX_DIGITS[] = "0123456789ABCDEF";
  930. for (j = 0; j < snl; j++, psn++) {
  931. *n++ = HEX_DIGITS[*psn >> 4];
  932. *n++ = HEX_DIGITS[*psn & 0x0F];
  933. }
  934. } else {
  935. *(n++) = '0';
  936. *(n++) = '0';
  937. }
  938. *(n++) = '.';
  939. *(n++) = 'p';
  940. *(n++) = 'e';
  941. *(n++) = 'm';
  942. *n = '\0'; /* closing new_cert */
  943. if (verbose)
  944. BIO_printf(bio_err, "writing %s\n", new_cert);
  945. Cout = BIO_new_file(new_cert, "w");
  946. if (Cout == NULL) {
  947. perror(new_cert);
  948. goto end;
  949. }
  950. write_new_certificate(Cout, xi, 0, notext);
  951. write_new_certificate(Sout, xi, output_der, notext);
  952. BIO_free_all(Cout);
  953. }
  954. if (sk_X509_num(cert_sk)) {
  955. /* Rename the database and the serial file */
  956. if (!rotate_serial(serialfile, "new", "old"))
  957. goto end;
  958. if (!rotate_index(dbfile, "new", "old"))
  959. goto end;
  960. BIO_printf(bio_err, "Data Base Updated\n");
  961. }
  962. }
  963. /*****************************************************************/
  964. if (gencrl) {
  965. int crl_v2 = 0;
  966. if (crl_ext == NULL) {
  967. crl_ext = NCONF_get_string(conf, section, ENV_CRLEXT);
  968. if (crl_ext == NULL)
  969. ERR_clear_error();
  970. }
  971. if (crl_ext != NULL) {
  972. /* Check syntax of file */
  973. X509V3_CTX ctx;
  974. X509V3_set_ctx_test(&ctx);
  975. X509V3_set_nconf(&ctx, conf);
  976. if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) {
  977. BIO_printf(bio_err,
  978. "Error Loading CRL extension section %s\n", crl_ext);
  979. ret = 1;
  980. goto end;
  981. }
  982. }
  983. if ((crlnumberfile = NCONF_get_string(conf, section, ENV_CRLNUMBER))
  984. != NULL)
  985. if ((crlnumber = load_serial(crlnumberfile, 0, NULL)) == NULL) {
  986. BIO_printf(bio_err, "error while loading CRL number\n");
  987. goto end;
  988. }
  989. if (!crldays && !crlhours && !crlsec) {
  990. if (!NCONF_get_number(conf, section,
  991. ENV_DEFAULT_CRL_DAYS, &crldays))
  992. crldays = 0;
  993. if (!NCONF_get_number(conf, section,
  994. ENV_DEFAULT_CRL_HOURS, &crlhours))
  995. crlhours = 0;
  996. ERR_clear_error();
  997. }
  998. if ((crldays == 0) && (crlhours == 0) && (crlsec == 0)) {
  999. BIO_printf(bio_err,
  1000. "cannot lookup how long until the next CRL is issued\n");
  1001. goto end;
  1002. }
  1003. if (verbose)
  1004. BIO_printf(bio_err, "making CRL\n");
  1005. if ((crl = X509_CRL_new()) == NULL)
  1006. goto end;
  1007. if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509)))
  1008. goto end;
  1009. tmptm = ASN1_TIME_new();
  1010. if (tmptm == NULL
  1011. || X509_gmtime_adj(tmptm, 0) == NULL
  1012. || !X509_CRL_set1_lastUpdate(crl, tmptm)
  1013. || X509_time_adj_ex(tmptm, crldays, crlhours * 60 * 60 + crlsec,
  1014. NULL) == NULL) {
  1015. BIO_puts(bio_err, "error setting CRL nextUpdate\n");
  1016. ASN1_TIME_free(tmptm);
  1017. goto end;
  1018. }
  1019. X509_CRL_set1_nextUpdate(crl, tmptm);
  1020. ASN1_TIME_free(tmptm);
  1021. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  1022. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  1023. if (pp[DB_type][0] == DB_TYPE_REV) {
  1024. if ((r = X509_REVOKED_new()) == NULL)
  1025. goto end;
  1026. j = make_revoked(r, pp[DB_rev_date]);
  1027. if (!j)
  1028. goto end;
  1029. if (j == 2)
  1030. crl_v2 = 1;
  1031. if (!BN_hex2bn(&serial, pp[DB_serial]))
  1032. goto end;
  1033. tmpser = BN_to_ASN1_INTEGER(serial, NULL);
  1034. BN_free(serial);
  1035. serial = NULL;
  1036. if (!tmpser)
  1037. goto end;
  1038. X509_REVOKED_set_serialNumber(r, tmpser);
  1039. ASN1_INTEGER_free(tmpser);
  1040. X509_CRL_add0_revoked(crl, r);
  1041. }
  1042. }
  1043. /*
  1044. * sort the data so it will be written in serial number order
  1045. */
  1046. X509_CRL_sort(crl);
  1047. /* we now have a CRL */
  1048. if (verbose)
  1049. BIO_printf(bio_err, "signing CRL\n");
  1050. /* Add any extensions asked for */
  1051. if (crl_ext != NULL || crlnumberfile != NULL) {
  1052. X509V3_CTX crlctx;
  1053. X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
  1054. X509V3_set_nconf(&crlctx, conf);
  1055. if (crl_ext != NULL)
  1056. if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx, crl_ext, crl))
  1057. goto end;
  1058. if (crlnumberfile != NULL) {
  1059. tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
  1060. if (!tmpser)
  1061. goto end;
  1062. X509_CRL_add1_ext_i2d(crl, NID_crl_number, tmpser, 0, 0);
  1063. ASN1_INTEGER_free(tmpser);
  1064. crl_v2 = 1;
  1065. if (!BN_add_word(crlnumber, 1))
  1066. goto end;
  1067. }
  1068. }
  1069. if (crl_ext != NULL || crl_v2) {
  1070. if (!X509_CRL_set_version(crl, 1))
  1071. goto end; /* version 2 CRL */
  1072. }
  1073. /* we have a CRL number that need updating */
  1074. if (crlnumberfile != NULL)
  1075. if (!rand_ser
  1076. && !save_serial(crlnumberfile, "new", crlnumber, NULL))
  1077. goto end;
  1078. BN_free(crlnumber);
  1079. crlnumber = NULL;
  1080. if (!do_X509_CRL_sign(crl, pkey, dgst, sigopts))
  1081. goto end;
  1082. PEM_write_bio_X509_CRL(Sout, crl);
  1083. if (crlnumberfile != NULL) /* Rename the crlnumber file */
  1084. if (!rotate_serial(crlnumberfile, "new", "old"))
  1085. goto end;
  1086. }
  1087. /*****************************************************************/
  1088. if (dorevoke) {
  1089. if (infile == NULL) {
  1090. BIO_printf(bio_err, "no input files\n");
  1091. goto end;
  1092. } else {
  1093. X509 *revcert;
  1094. revcert = load_cert(infile, FORMAT_PEM, infile);
  1095. if (revcert == NULL)
  1096. goto end;
  1097. if (dorevoke == 2)
  1098. rev_type = REV_VALID;
  1099. j = do_revoke(revcert, db, rev_type, rev_arg);
  1100. if (j <= 0)
  1101. goto end;
  1102. X509_free(revcert);
  1103. if (!save_index(dbfile, "new", db))
  1104. goto end;
  1105. if (!rotate_index(dbfile, "new", "old"))
  1106. goto end;
  1107. BIO_printf(bio_err, "Data Base Updated\n");
  1108. }
  1109. }
  1110. ret = 0;
  1111. end:
  1112. if (ret)
  1113. ERR_print_errors(bio_err);
  1114. BIO_free_all(Sout);
  1115. BIO_free_all(out);
  1116. BIO_free_all(in);
  1117. sk_X509_pop_free(cert_sk, X509_free);
  1118. if (free_key)
  1119. OPENSSL_free(key);
  1120. BN_free(serial);
  1121. BN_free(crlnumber);
  1122. free_index(db);
  1123. sk_OPENSSL_STRING_free(sigopts);
  1124. EVP_PKEY_free(pkey);
  1125. X509_free(x509);
  1126. X509_CRL_free(crl);
  1127. NCONF_free(conf);
  1128. NCONF_free(extconf);
  1129. release_engine(e);
  1130. return ret;
  1131. }
  1132. static char *lookup_conf(const CONF *conf, const char *section, const char *tag)
  1133. {
  1134. char *entry = NCONF_get_string(conf, section, tag);
  1135. if (entry == NULL)
  1136. BIO_printf(bio_err, "variable lookup failed for %s::%s\n", section, tag);
  1137. return entry;
  1138. }
  1139. static int certify(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509,
  1140. const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
  1141. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1142. BIGNUM *serial, const char *subj, unsigned long chtype,
  1143. int multirdn, int email_dn, const char *startdate,
  1144. const char *enddate,
  1145. long days, int batch, const char *ext_sect, CONF *lconf,
  1146. int verbose, unsigned long certopt, unsigned long nameopt,
  1147. int default_op, int ext_copy, int selfsign)
  1148. {
  1149. X509_REQ *req = NULL;
  1150. BIO *in = NULL;
  1151. EVP_PKEY *pktmp = NULL;
  1152. int ok = -1, i;
  1153. in = BIO_new_file(infile, "r");
  1154. if (in == NULL) {
  1155. ERR_print_errors(bio_err);
  1156. goto end;
  1157. }
  1158. if ((req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL)) == NULL) {
  1159. BIO_printf(bio_err, "Error reading certificate request in %s\n",
  1160. infile);
  1161. goto end;
  1162. }
  1163. if (verbose)
  1164. X509_REQ_print_ex(bio_err, req, nameopt, X509_FLAG_COMPAT);
  1165. BIO_printf(bio_err, "Check that the request matches the signature\n");
  1166. if (selfsign && !X509_REQ_check_private_key(req, pkey)) {
  1167. BIO_printf(bio_err,
  1168. "Certificate request and CA private key do not match\n");
  1169. ok = 0;
  1170. goto end;
  1171. }
  1172. if ((pktmp = X509_REQ_get0_pubkey(req)) == NULL) {
  1173. BIO_printf(bio_err, "error unpacking public key\n");
  1174. goto end;
  1175. }
  1176. i = X509_REQ_verify(req, pktmp);
  1177. pktmp = NULL;
  1178. if (i < 0) {
  1179. ok = 0;
  1180. BIO_printf(bio_err, "Signature verification problems....\n");
  1181. ERR_print_errors(bio_err);
  1182. goto end;
  1183. }
  1184. if (i == 0) {
  1185. ok = 0;
  1186. BIO_printf(bio_err,
  1187. "Signature did not match the certificate request\n");
  1188. ERR_print_errors(bio_err);
  1189. goto end;
  1190. } else {
  1191. BIO_printf(bio_err, "Signature ok\n");
  1192. }
  1193. ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
  1194. chtype, multirdn, email_dn, startdate, enddate, days, batch,
  1195. verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
  1196. ext_copy, selfsign);
  1197. end:
  1198. X509_REQ_free(req);
  1199. BIO_free(in);
  1200. return ok;
  1201. }
  1202. static int certify_cert(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509,
  1203. const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
  1204. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1205. BIGNUM *serial, const char *subj, unsigned long chtype,
  1206. int multirdn, int email_dn, const char *startdate,
  1207. const char *enddate, long days, int batch, const char *ext_sect,
  1208. CONF *lconf, int verbose, unsigned long certopt,
  1209. unsigned long nameopt, int default_op, int ext_copy)
  1210. {
  1211. X509 *req = NULL;
  1212. X509_REQ *rreq = NULL;
  1213. EVP_PKEY *pktmp = NULL;
  1214. int ok = -1, i;
  1215. if ((req = load_cert(infile, FORMAT_PEM, infile)) == NULL)
  1216. goto end;
  1217. if (verbose)
  1218. X509_print(bio_err, req);
  1219. BIO_printf(bio_err, "Check that the request matches the signature\n");
  1220. if ((pktmp = X509_get0_pubkey(req)) == NULL) {
  1221. BIO_printf(bio_err, "error unpacking public key\n");
  1222. goto end;
  1223. }
  1224. i = X509_verify(req, pktmp);
  1225. if (i < 0) {
  1226. ok = 0;
  1227. BIO_printf(bio_err, "Signature verification problems....\n");
  1228. goto end;
  1229. }
  1230. if (i == 0) {
  1231. ok = 0;
  1232. BIO_printf(bio_err, "Signature did not match the certificate\n");
  1233. goto end;
  1234. } else {
  1235. BIO_printf(bio_err, "Signature ok\n");
  1236. }
  1237. if ((rreq = X509_to_X509_REQ(req, NULL, NULL)) == NULL)
  1238. goto end;
  1239. ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
  1240. chtype, multirdn, email_dn, startdate, enddate, days, batch,
  1241. verbose, rreq, ext_sect, lconf, certopt, nameopt, default_op,
  1242. ext_copy, 0);
  1243. end:
  1244. X509_REQ_free(rreq);
  1245. X509_free(req);
  1246. return ok;
  1247. }
  1248. static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
  1249. const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
  1250. STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,
  1251. const char *subj, unsigned long chtype, int multirdn,
  1252. int email_dn, const char *startdate, const char *enddate, long days,
  1253. int batch, int verbose, X509_REQ *req, const char *ext_sect,
  1254. CONF *lconf, unsigned long certopt, unsigned long nameopt,
  1255. int default_op, int ext_copy, int selfsign)
  1256. {
  1257. X509_NAME *name = NULL, *CAname = NULL, *subject = NULL;
  1258. const ASN1_TIME *tm;
  1259. ASN1_STRING *str, *str2;
  1260. ASN1_OBJECT *obj;
  1261. X509 *ret = NULL;
  1262. X509_NAME_ENTRY *ne, *tne;
  1263. EVP_PKEY *pktmp;
  1264. int ok = -1, i, j, last, nid;
  1265. const char *p;
  1266. CONF_VALUE *cv;
  1267. OPENSSL_STRING row[DB_NUMBER];
  1268. OPENSSL_STRING *irow = NULL;
  1269. OPENSSL_STRING *rrow = NULL;
  1270. char buf[25];
  1271. for (i = 0; i < DB_NUMBER; i++)
  1272. row[i] = NULL;
  1273. if (subj) {
  1274. X509_NAME *n = parse_name(subj, chtype, multirdn);
  1275. if (!n) {
  1276. ERR_print_errors(bio_err);
  1277. goto end;
  1278. }
  1279. X509_REQ_set_subject_name(req, n);
  1280. X509_NAME_free(n);
  1281. }
  1282. if (default_op)
  1283. BIO_printf(bio_err, "The Subject's Distinguished Name is as follows\n");
  1284. name = X509_REQ_get_subject_name(req);
  1285. for (i = 0; i < X509_NAME_entry_count(name); i++) {
  1286. ne = X509_NAME_get_entry(name, i);
  1287. str = X509_NAME_ENTRY_get_data(ne);
  1288. obj = X509_NAME_ENTRY_get_object(ne);
  1289. nid = OBJ_obj2nid(obj);
  1290. if (msie_hack) {
  1291. /* assume all type should be strings */
  1292. if (str->type == V_ASN1_UNIVERSALSTRING)
  1293. ASN1_UNIVERSALSTRING_to_string(str);
  1294. if (str->type == V_ASN1_IA5STRING && nid != NID_pkcs9_emailAddress)
  1295. str->type = V_ASN1_T61STRING;
  1296. if (nid == NID_pkcs9_emailAddress
  1297. && str->type == V_ASN1_PRINTABLESTRING)
  1298. str->type = V_ASN1_IA5STRING;
  1299. }
  1300. /* If no EMAIL is wanted in the subject */
  1301. if (nid == NID_pkcs9_emailAddress && !email_dn)
  1302. continue;
  1303. /* check some things */
  1304. if (nid == NID_pkcs9_emailAddress && str->type != V_ASN1_IA5STRING) {
  1305. BIO_printf(bio_err,
  1306. "\nemailAddress type needs to be of type IA5STRING\n");
  1307. goto end;
  1308. }
  1309. if (str->type != V_ASN1_BMPSTRING && str->type != V_ASN1_UTF8STRING) {
  1310. j = ASN1_PRINTABLE_type(str->data, str->length);
  1311. if ((j == V_ASN1_T61STRING && str->type != V_ASN1_T61STRING) ||
  1312. (j == V_ASN1_IA5STRING && str->type == V_ASN1_PRINTABLESTRING))
  1313. {
  1314. BIO_printf(bio_err,
  1315. "\nThe string contains characters that are illegal for the ASN.1 type\n");
  1316. goto end;
  1317. }
  1318. }
  1319. if (default_op)
  1320. old_entry_print(obj, str);
  1321. }
  1322. /* Ok, now we check the 'policy' stuff. */
  1323. if ((subject = X509_NAME_new()) == NULL) {
  1324. BIO_printf(bio_err, "Memory allocation failure\n");
  1325. goto end;
  1326. }
  1327. /* take a copy of the issuer name before we mess with it. */
  1328. if (selfsign)
  1329. CAname = X509_NAME_dup(name);
  1330. else
  1331. CAname = X509_NAME_dup(X509_get_subject_name(x509));
  1332. if (CAname == NULL)
  1333. goto end;
  1334. str = str2 = NULL;
  1335. for (i = 0; i < sk_CONF_VALUE_num(policy); i++) {
  1336. cv = sk_CONF_VALUE_value(policy, i); /* get the object id */
  1337. if ((j = OBJ_txt2nid(cv->name)) == NID_undef) {
  1338. BIO_printf(bio_err,
  1339. "%s:unknown object type in 'policy' configuration\n",
  1340. cv->name);
  1341. goto end;
  1342. }
  1343. obj = OBJ_nid2obj(j);
  1344. last = -1;
  1345. for (;;) {
  1346. X509_NAME_ENTRY *push = NULL;
  1347. /* lookup the object in the supplied name list */
  1348. j = X509_NAME_get_index_by_OBJ(name, obj, last);
  1349. if (j < 0) {
  1350. if (last != -1)
  1351. break;
  1352. tne = NULL;
  1353. } else {
  1354. tne = X509_NAME_get_entry(name, j);
  1355. }
  1356. last = j;
  1357. /* depending on the 'policy', decide what to do. */
  1358. if (strcmp(cv->value, "optional") == 0) {
  1359. if (tne != NULL)
  1360. push = tne;
  1361. } else if (strcmp(cv->value, "supplied") == 0) {
  1362. if (tne == NULL) {
  1363. BIO_printf(bio_err,
  1364. "The %s field needed to be supplied and was missing\n",
  1365. cv->name);
  1366. goto end;
  1367. } else {
  1368. push = tne;
  1369. }
  1370. } else if (strcmp(cv->value, "match") == 0) {
  1371. int last2;
  1372. if (tne == NULL) {
  1373. BIO_printf(bio_err,
  1374. "The mandatory %s field was missing\n",
  1375. cv->name);
  1376. goto end;
  1377. }
  1378. last2 = -1;
  1379. again2:
  1380. j = X509_NAME_get_index_by_OBJ(CAname, obj, last2);
  1381. if ((j < 0) && (last2 == -1)) {
  1382. BIO_printf(bio_err,
  1383. "The %s field does not exist in the CA certificate,\n"
  1384. "the 'policy' is misconfigured\n", cv->name);
  1385. goto end;
  1386. }
  1387. if (j >= 0) {
  1388. push = X509_NAME_get_entry(CAname, j);
  1389. str = X509_NAME_ENTRY_get_data(tne);
  1390. str2 = X509_NAME_ENTRY_get_data(push);
  1391. last2 = j;
  1392. if (ASN1_STRING_cmp(str, str2) != 0)
  1393. goto again2;
  1394. }
  1395. if (j < 0) {
  1396. BIO_printf(bio_err,
  1397. "The %s field is different between\n"
  1398. "CA certificate (%s) and the request (%s)\n",
  1399. cv->name,
  1400. ((str2 == NULL) ? "NULL" : (char *)str2->data),
  1401. ((str == NULL) ? "NULL" : (char *)str->data));
  1402. goto end;
  1403. }
  1404. } else {
  1405. BIO_printf(bio_err,
  1406. "%s:invalid type in 'policy' configuration\n",
  1407. cv->value);
  1408. goto end;
  1409. }
  1410. if (push != NULL) {
  1411. if (!X509_NAME_add_entry(subject, push, -1, 0)) {
  1412. BIO_printf(bio_err, "Memory allocation failure\n");
  1413. goto end;
  1414. }
  1415. }
  1416. if (j < 0)
  1417. break;
  1418. }
  1419. }
  1420. if (preserve) {
  1421. X509_NAME_free(subject);
  1422. /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
  1423. subject = X509_NAME_dup(name);
  1424. if (subject == NULL)
  1425. goto end;
  1426. }
  1427. /* We are now totally happy, lets make and sign the certificate */
  1428. if (verbose)
  1429. BIO_printf(bio_err,
  1430. "Everything appears to be ok, creating and signing the certificate\n");
  1431. if ((ret = X509_new()) == NULL)
  1432. goto end;
  1433. #ifdef X509_V3
  1434. /* Make it an X509 v3 certificate. */
  1435. if (!X509_set_version(ret, 2))
  1436. goto end;
  1437. #endif
  1438. if (BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(ret)) == NULL)
  1439. goto end;
  1440. if (selfsign) {
  1441. if (!X509_set_issuer_name(ret, subject))
  1442. goto end;
  1443. } else {
  1444. if (!X509_set_issuer_name(ret, X509_get_subject_name(x509)))
  1445. goto end;
  1446. }
  1447. if (!set_cert_times(ret, startdate, enddate, days))
  1448. goto end;
  1449. if (enddate != NULL) {
  1450. int tdays;
  1451. if (!ASN1_TIME_diff(&tdays, NULL, NULL, X509_get0_notAfter(ret)))
  1452. goto end;
  1453. days = tdays;
  1454. }
  1455. if (!X509_set_subject_name(ret, subject))
  1456. goto end;
  1457. pktmp = X509_REQ_get0_pubkey(req);
  1458. i = X509_set_pubkey(ret, pktmp);
  1459. if (!i)
  1460. goto end;
  1461. /* Lets add the extensions, if there are any */
  1462. if (ext_sect) {
  1463. X509V3_CTX ctx;
  1464. /* Initialize the context structure */
  1465. if (selfsign)
  1466. X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);
  1467. else
  1468. X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
  1469. if (extconf != NULL) {
  1470. if (verbose)
  1471. BIO_printf(bio_err, "Extra configuration file found\n");
  1472. /* Use the extconf configuration db LHASH */
  1473. X509V3_set_nconf(&ctx, extconf);
  1474. /* Test the structure (needed?) */
  1475. /* X509V3_set_ctx_test(&ctx); */
  1476. /* Adds exts contained in the configuration file */
  1477. if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect, ret)) {
  1478. BIO_printf(bio_err,
  1479. "ERROR: adding extensions in section %s\n",
  1480. ext_sect);
  1481. ERR_print_errors(bio_err);
  1482. goto end;
  1483. }
  1484. if (verbose)
  1485. BIO_printf(bio_err,
  1486. "Successfully added extensions from file.\n");
  1487. } else if (ext_sect) {
  1488. /* We found extensions to be set from config file */
  1489. X509V3_set_nconf(&ctx, lconf);
  1490. if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) {
  1491. BIO_printf(bio_err,
  1492. "ERROR: adding extensions in section %s\n",
  1493. ext_sect);
  1494. ERR_print_errors(bio_err);
  1495. goto end;
  1496. }
  1497. if (verbose)
  1498. BIO_printf(bio_err,
  1499. "Successfully added extensions from config\n");
  1500. }
  1501. }
  1502. /* Copy extensions from request (if any) */
  1503. if (!copy_extensions(ret, req, ext_copy)) {
  1504. BIO_printf(bio_err, "ERROR: adding extensions from request\n");
  1505. ERR_print_errors(bio_err);
  1506. goto end;
  1507. }
  1508. {
  1509. const STACK_OF(X509_EXTENSION) *exts = X509_get0_extensions(ret);
  1510. if (exts != NULL && sk_X509_EXTENSION_num(exts) > 0)
  1511. /* Make it an X509 v3 certificate. */
  1512. if (!X509_set_version(ret, 2))
  1513. goto end;
  1514. }
  1515. if (verbose)
  1516. BIO_printf(bio_err,
  1517. "The subject name appears to be ok, checking data base for clashes\n");
  1518. /* Build the correct Subject if no e-mail is wanted in the subject. */
  1519. if (!email_dn) {
  1520. X509_NAME_ENTRY *tmpne;
  1521. X509_NAME *dn_subject;
  1522. /*
  1523. * Its best to dup the subject DN and then delete any email addresses
  1524. * because this retains its structure.
  1525. */
  1526. if ((dn_subject = X509_NAME_dup(subject)) == NULL) {
  1527. BIO_printf(bio_err, "Memory allocation failure\n");
  1528. goto end;
  1529. }
  1530. while ((i = X509_NAME_get_index_by_NID(dn_subject,
  1531. NID_pkcs9_emailAddress,
  1532. -1)) >= 0) {
  1533. tmpne = X509_NAME_get_entry(dn_subject, i);
  1534. X509_NAME_delete_entry(dn_subject, i);
  1535. X509_NAME_ENTRY_free(tmpne);
  1536. }
  1537. if (!X509_set_subject_name(ret, dn_subject)) {
  1538. X509_NAME_free(dn_subject);
  1539. goto end;
  1540. }
  1541. X509_NAME_free(dn_subject);
  1542. }
  1543. row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0);
  1544. if (row[DB_name] == NULL) {
  1545. BIO_printf(bio_err, "Memory allocation failure\n");
  1546. goto end;
  1547. }
  1548. if (BN_is_zero(serial))
  1549. row[DB_serial] = OPENSSL_strdup("00");
  1550. else
  1551. row[DB_serial] = BN_bn2hex(serial);
  1552. if (row[DB_serial] == NULL) {
  1553. BIO_printf(bio_err, "Memory allocation failure\n");
  1554. goto end;
  1555. }
  1556. if (row[DB_name][0] == '\0') {
  1557. /*
  1558. * An empty subject! We'll use the serial number instead. If
  1559. * unique_subject is in use then we don't want different entries with
  1560. * empty subjects matching each other.
  1561. */
  1562. OPENSSL_free(row[DB_name]);
  1563. row[DB_name] = OPENSSL_strdup(row[DB_serial]);
  1564. if (row[DB_name] == NULL) {
  1565. BIO_printf(bio_err, "Memory allocation failure\n");
  1566. goto end;
  1567. }
  1568. }
  1569. if (db->attributes.unique_subject) {
  1570. OPENSSL_STRING *crow = row;
  1571. rrow = TXT_DB_get_by_index(db->db, DB_name, crow);
  1572. if (rrow != NULL) {
  1573. BIO_printf(bio_err,
  1574. "ERROR:There is already a certificate for %s\n",
  1575. row[DB_name]);
  1576. }
  1577. }
  1578. if (rrow == NULL) {
  1579. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1580. if (rrow != NULL) {
  1581. BIO_printf(bio_err,
  1582. "ERROR:Serial number %s has already been issued,\n",
  1583. row[DB_serial]);
  1584. BIO_printf(bio_err,
  1585. " check the database/serial_file for corruption\n");
  1586. }
  1587. }
  1588. if (rrow != NULL) {
  1589. BIO_printf(bio_err, "The matching entry has the following details\n");
  1590. if (rrow[DB_type][0] == DB_TYPE_EXP)
  1591. p = "Expired";
  1592. else if (rrow[DB_type][0] == DB_TYPE_REV)
  1593. p = "Revoked";
  1594. else if (rrow[DB_type][0] == DB_TYPE_VAL)
  1595. p = "Valid";
  1596. else
  1597. p = "\ninvalid type, Data base error\n";
  1598. BIO_printf(bio_err, "Type :%s\n", p);;
  1599. if (rrow[DB_type][0] == DB_TYPE_REV) {
  1600. p = rrow[DB_exp_date];
  1601. if (p == NULL)
  1602. p = "undef";
  1603. BIO_printf(bio_err, "Was revoked on:%s\n", p);
  1604. }
  1605. p = rrow[DB_exp_date];
  1606. if (p == NULL)
  1607. p = "undef";
  1608. BIO_printf(bio_err, "Expires on :%s\n", p);
  1609. p = rrow[DB_serial];
  1610. if (p == NULL)
  1611. p = "undef";
  1612. BIO_printf(bio_err, "Serial Number :%s\n", p);
  1613. p = rrow[DB_file];
  1614. if (p == NULL)
  1615. p = "undef";
  1616. BIO_printf(bio_err, "File name :%s\n", p);
  1617. p = rrow[DB_name];
  1618. if (p == NULL)
  1619. p = "undef";
  1620. BIO_printf(bio_err, "Subject Name :%s\n", p);
  1621. ok = -1; /* This is now a 'bad' error. */
  1622. goto end;
  1623. }
  1624. if (!default_op) {
  1625. BIO_printf(bio_err, "Certificate Details:\n");
  1626. /*
  1627. * Never print signature details because signature not present
  1628. */
  1629. certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
  1630. X509_print_ex(bio_err, ret, nameopt, certopt);
  1631. }
  1632. BIO_printf(bio_err, "Certificate is to be certified until ");
  1633. ASN1_TIME_print(bio_err, X509_get0_notAfter(ret));
  1634. if (days)
  1635. BIO_printf(bio_err, " (%ld days)", days);
  1636. BIO_printf(bio_err, "\n");
  1637. if (!batch) {
  1638. BIO_printf(bio_err, "Sign the certificate? [y/n]:");
  1639. (void)BIO_flush(bio_err);
  1640. buf[0] = '\0';
  1641. if (fgets(buf, sizeof(buf), stdin) == NULL) {
  1642. BIO_printf(bio_err,
  1643. "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
  1644. ok = 0;
  1645. goto end;
  1646. }
  1647. if (!(buf[0] == 'y' || buf[0] == 'Y')) {
  1648. BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\n");
  1649. ok = 0;
  1650. goto end;
  1651. }
  1652. }
  1653. pktmp = X509_get0_pubkey(ret);
  1654. if (EVP_PKEY_missing_parameters(pktmp) &&
  1655. !EVP_PKEY_missing_parameters(pkey))
  1656. EVP_PKEY_copy_parameters(pktmp, pkey);
  1657. if (!do_X509_sign(ret, pkey, dgst, sigopts))
  1658. goto end;
  1659. /* We now just add it to the database as DB_TYPE_VAL('V') */
  1660. row[DB_type] = OPENSSL_strdup("V");
  1661. tm = X509_get0_notAfter(ret);
  1662. row[DB_exp_date] = app_malloc(tm->length + 1, "row expdate");
  1663. memcpy(row[DB_exp_date], tm->data, tm->length);
  1664. row[DB_exp_date][tm->length] = '\0';
  1665. row[DB_rev_date] = NULL;
  1666. row[DB_file] = OPENSSL_strdup("unknown");
  1667. if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
  1668. (row[DB_file] == NULL) || (row[DB_name] == NULL)) {
  1669. BIO_printf(bio_err, "Memory allocation failure\n");
  1670. goto end;
  1671. }
  1672. irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row space");
  1673. for (i = 0; i < DB_NUMBER; i++)
  1674. irow[i] = row[i];
  1675. irow[DB_NUMBER] = NULL;
  1676. if (!TXT_DB_insert(db->db, irow)) {
  1677. BIO_printf(bio_err, "failed to update database\n");
  1678. BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
  1679. goto end;
  1680. }
  1681. irow = NULL;
  1682. ok = 1;
  1683. end:
  1684. if (ok != 1) {
  1685. for (i = 0; i < DB_NUMBER; i++)
  1686. OPENSSL_free(row[i]);
  1687. }
  1688. OPENSSL_free(irow);
  1689. X509_NAME_free(CAname);
  1690. X509_NAME_free(subject);
  1691. if (ok <= 0)
  1692. X509_free(ret);
  1693. else
  1694. *xret = ret;
  1695. return ok;
  1696. }
  1697. static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext)
  1698. {
  1699. if (output_der) {
  1700. (void)i2d_X509_bio(bp, x);
  1701. return;
  1702. }
  1703. if (!notext)
  1704. X509_print(bp, x);
  1705. PEM_write_bio_X509(bp, x);
  1706. }
  1707. static int certify_spkac(X509 **xret, const char *infile, EVP_PKEY *pkey,
  1708. X509 *x509, const EVP_MD *dgst,
  1709. STACK_OF(OPENSSL_STRING) *sigopts,
  1710. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1711. BIGNUM *serial, const char *subj, unsigned long chtype,
  1712. int multirdn, int email_dn, const char *startdate,
  1713. const char *enddate, long days, const char *ext_sect,
  1714. CONF *lconf, int verbose, unsigned long certopt,
  1715. unsigned long nameopt, int default_op, int ext_copy)
  1716. {
  1717. STACK_OF(CONF_VALUE) *sk = NULL;
  1718. LHASH_OF(CONF_VALUE) *parms = NULL;
  1719. X509_REQ *req = NULL;
  1720. CONF_VALUE *cv = NULL;
  1721. NETSCAPE_SPKI *spki = NULL;
  1722. char *type, *buf;
  1723. EVP_PKEY *pktmp = NULL;
  1724. X509_NAME *n = NULL;
  1725. X509_NAME_ENTRY *ne = NULL;
  1726. int ok = -1, i, j;
  1727. long errline;
  1728. int nid;
  1729. /*
  1730. * Load input file into a hash table. (This is just an easy
  1731. * way to read and parse the file, then put it into a convenient
  1732. * STACK format).
  1733. */
  1734. parms = CONF_load(NULL, infile, &errline);
  1735. if (parms == NULL) {
  1736. BIO_printf(bio_err, "error on line %ld of %s\n", errline, infile);
  1737. ERR_print_errors(bio_err);
  1738. goto end;
  1739. }
  1740. sk = CONF_get_section(parms, "default");
  1741. if (sk_CONF_VALUE_num(sk) == 0) {
  1742. BIO_printf(bio_err, "no name/value pairs found in %s\n", infile);
  1743. goto end;
  1744. }
  1745. /*
  1746. * Now create a dummy X509 request structure. We don't actually
  1747. * have an X509 request, but we have many of the components
  1748. * (a public key, various DN components). The idea is that we
  1749. * put these components into the right X509 request structure
  1750. * and we can use the same code as if you had a real X509 request.
  1751. */
  1752. req = X509_REQ_new();
  1753. if (req == NULL) {
  1754. ERR_print_errors(bio_err);
  1755. goto end;
  1756. }
  1757. /*
  1758. * Build up the subject name set.
  1759. */
  1760. n = X509_REQ_get_subject_name(req);
  1761. for (i = 0;; i++) {
  1762. if (sk_CONF_VALUE_num(sk) <= i)
  1763. break;
  1764. cv = sk_CONF_VALUE_value(sk, i);
  1765. type = cv->name;
  1766. /*
  1767. * Skip past any leading X. X: X, etc to allow for multiple instances
  1768. */
  1769. for (buf = cv->name; *buf; buf++)
  1770. if ((*buf == ':') || (*buf == ',') || (*buf == '.')) {
  1771. buf++;
  1772. if (*buf)
  1773. type = buf;
  1774. break;
  1775. }
  1776. buf = cv->value;
  1777. if ((nid = OBJ_txt2nid(type)) == NID_undef) {
  1778. if (strcmp(type, "SPKAC") == 0) {
  1779. spki = NETSCAPE_SPKI_b64_decode(cv->value, -1);
  1780. if (spki == NULL) {
  1781. BIO_printf(bio_err,
  1782. "unable to load Netscape SPKAC structure\n");
  1783. ERR_print_errors(bio_err);
  1784. goto end;
  1785. }
  1786. }
  1787. continue;
  1788. }
  1789. if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
  1790. (unsigned char *)buf, -1, -1, 0))
  1791. goto end;
  1792. }
  1793. if (spki == NULL) {
  1794. BIO_printf(bio_err, "Netscape SPKAC structure not found in %s\n",
  1795. infile);
  1796. goto end;
  1797. }
  1798. /*
  1799. * Now extract the key from the SPKI structure.
  1800. */
  1801. BIO_printf(bio_err, "Check that the SPKAC request matches the signature\n");
  1802. if ((pktmp = NETSCAPE_SPKI_get_pubkey(spki)) == NULL) {
  1803. BIO_printf(bio_err, "error unpacking SPKAC public key\n");
  1804. goto end;
  1805. }
  1806. j = NETSCAPE_SPKI_verify(spki, pktmp);
  1807. if (j <= 0) {
  1808. EVP_PKEY_free(pktmp);
  1809. BIO_printf(bio_err,
  1810. "signature verification failed on SPKAC public key\n");
  1811. goto end;
  1812. }
  1813. BIO_printf(bio_err, "Signature ok\n");
  1814. X509_REQ_set_pubkey(req, pktmp);
  1815. EVP_PKEY_free(pktmp);
  1816. ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
  1817. chtype, multirdn, email_dn, startdate, enddate, days, 1,
  1818. verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
  1819. ext_copy, 0);
  1820. end:
  1821. X509_REQ_free(req);
  1822. CONF_free(parms);
  1823. NETSCAPE_SPKI_free(spki);
  1824. X509_NAME_ENTRY_free(ne);
  1825. return ok;
  1826. }
  1827. static int check_time_format(const char *str)
  1828. {
  1829. return ASN1_TIME_set_string(NULL, str);
  1830. }
  1831. static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type,
  1832. const char *value)
  1833. {
  1834. const ASN1_TIME *tm = NULL;
  1835. char *row[DB_NUMBER], **rrow, **irow;
  1836. char *rev_str = NULL;
  1837. BIGNUM *bn = NULL;
  1838. int ok = -1, i;
  1839. for (i = 0; i < DB_NUMBER; i++)
  1840. row[i] = NULL;
  1841. row[DB_name] = X509_NAME_oneline(X509_get_subject_name(x509), NULL, 0);
  1842. bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509), NULL);
  1843. if (!bn)
  1844. goto end;
  1845. if (BN_is_zero(bn))
  1846. row[DB_serial] = OPENSSL_strdup("00");
  1847. else
  1848. row[DB_serial] = BN_bn2hex(bn);
  1849. BN_free(bn);
  1850. if (row[DB_name] != NULL && row[DB_name][0] == '\0') {
  1851. /* Entries with empty Subjects actually use the serial number instead */
  1852. OPENSSL_free(row[DB_name]);
  1853. row[DB_name] = OPENSSL_strdup(row[DB_serial]);
  1854. }
  1855. if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) {
  1856. BIO_printf(bio_err, "Memory allocation failure\n");
  1857. goto end;
  1858. }
  1859. /*
  1860. * We have to lookup by serial number because name lookup skips revoked
  1861. * certs
  1862. */
  1863. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1864. if (rrow == NULL) {
  1865. BIO_printf(bio_err,
  1866. "Adding Entry with serial number %s to DB for %s\n",
  1867. row[DB_serial], row[DB_name]);
  1868. /* We now just add it to the database as DB_TYPE_REV('V') */
  1869. row[DB_type] = OPENSSL_strdup("V");
  1870. tm = X509_get0_notAfter(x509);
  1871. row[DB_exp_date] = app_malloc(tm->length + 1, "row exp_data");
  1872. memcpy(row[DB_exp_date], tm->data, tm->length);
  1873. row[DB_exp_date][tm->length] = '\0';
  1874. row[DB_rev_date] = NULL;
  1875. row[DB_file] = OPENSSL_strdup("unknown");
  1876. if (row[DB_type] == NULL || row[DB_file] == NULL) {
  1877. BIO_printf(bio_err, "Memory allocation failure\n");
  1878. goto end;
  1879. }
  1880. irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row ptr");
  1881. for (i = 0; i < DB_NUMBER; i++)
  1882. irow[i] = row[i];
  1883. irow[DB_NUMBER] = NULL;
  1884. if (!TXT_DB_insert(db->db, irow)) {
  1885. BIO_printf(bio_err, "failed to update database\n");
  1886. BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
  1887. OPENSSL_free(irow);
  1888. goto end;
  1889. }
  1890. for (i = 0; i < DB_NUMBER; i++)
  1891. row[i] = NULL;
  1892. /* Revoke Certificate */
  1893. if (rev_type == REV_VALID)
  1894. ok = 1;
  1895. else
  1896. /* Retry revocation after DB insertion */
  1897. ok = do_revoke(x509, db, rev_type, value);
  1898. goto end;
  1899. } else if (index_name_cmp_noconst(row, rrow)) {
  1900. BIO_printf(bio_err, "ERROR:name does not match %s\n", row[DB_name]);
  1901. goto end;
  1902. } else if (rev_type == REV_VALID) {
  1903. BIO_printf(bio_err, "ERROR:Already present, serial number %s\n",
  1904. row[DB_serial]);
  1905. goto end;
  1906. } else if (rrow[DB_type][0] == DB_TYPE_REV) {
  1907. BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n",
  1908. row[DB_serial]);
  1909. goto end;
  1910. } else {
  1911. BIO_printf(bio_err, "Revoking Certificate %s.\n", rrow[DB_serial]);
  1912. rev_str = make_revocation_str(rev_type, value);
  1913. if (!rev_str) {
  1914. BIO_printf(bio_err, "Error in revocation arguments\n");
  1915. goto end;
  1916. }
  1917. rrow[DB_type][0] = DB_TYPE_REV;
  1918. rrow[DB_type][1] = '\0';
  1919. rrow[DB_rev_date] = rev_str;
  1920. }
  1921. ok = 1;
  1922. end:
  1923. for (i = 0; i < DB_NUMBER; i++)
  1924. OPENSSL_free(row[i]);
  1925. return ok;
  1926. }
  1927. static int get_certificate_status(const char *serial, CA_DB *db)
  1928. {
  1929. char *row[DB_NUMBER], **rrow;
  1930. int ok = -1, i;
  1931. size_t serial_len = strlen(serial);
  1932. /* Free Resources */
  1933. for (i = 0; i < DB_NUMBER; i++)
  1934. row[i] = NULL;
  1935. /* Malloc needed char spaces */
  1936. row[DB_serial] = app_malloc(serial_len + 2, "row serial#");
  1937. if (serial_len % 2) {
  1938. /*
  1939. * Set the first char to 0
  1940. */
  1941. row[DB_serial][0] = '0';
  1942. /* Copy String from serial to row[DB_serial] */
  1943. memcpy(row[DB_serial] + 1, serial, serial_len);
  1944. row[DB_serial][serial_len + 1] = '\0';
  1945. } else {
  1946. /* Copy String from serial to row[DB_serial] */
  1947. memcpy(row[DB_serial], serial, serial_len);
  1948. row[DB_serial][serial_len] = '\0';
  1949. }
  1950. /* Make it Upper Case */
  1951. make_uppercase(row[DB_serial]);
  1952. ok = 1;
  1953. /* Search for the certificate */
  1954. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1955. if (rrow == NULL) {
  1956. BIO_printf(bio_err, "Serial %s not present in db.\n", row[DB_serial]);
  1957. ok = -1;
  1958. goto end;
  1959. } else if (rrow[DB_type][0] == DB_TYPE_VAL) {
  1960. BIO_printf(bio_err, "%s=Valid (%c)\n",
  1961. row[DB_serial], rrow[DB_type][0]);
  1962. goto end;
  1963. } else if (rrow[DB_type][0] == DB_TYPE_REV) {
  1964. BIO_printf(bio_err, "%s=Revoked (%c)\n",
  1965. row[DB_serial], rrow[DB_type][0]);
  1966. goto end;
  1967. } else if (rrow[DB_type][0] == DB_TYPE_EXP) {
  1968. BIO_printf(bio_err, "%s=Expired (%c)\n",
  1969. row[DB_serial], rrow[DB_type][0]);
  1970. goto end;
  1971. } else if (rrow[DB_type][0] == DB_TYPE_SUSP) {
  1972. BIO_printf(bio_err, "%s=Suspended (%c)\n",
  1973. row[DB_serial], rrow[DB_type][0]);
  1974. goto end;
  1975. } else {
  1976. BIO_printf(bio_err, "%s=Unknown (%c).\n",
  1977. row[DB_serial], rrow[DB_type][0]);
  1978. ok = -1;
  1979. }
  1980. end:
  1981. for (i = 0; i < DB_NUMBER; i++) {
  1982. OPENSSL_free(row[i]);
  1983. }
  1984. return ok;
  1985. }
  1986. static int do_updatedb(CA_DB *db)
  1987. {
  1988. ASN1_UTCTIME *a_tm = NULL;
  1989. int i, cnt = 0;
  1990. int db_y2k, a_y2k; /* flags = 1 if y >= 2000 */
  1991. char **rrow, *a_tm_s;
  1992. a_tm = ASN1_UTCTIME_new();
  1993. if (a_tm == NULL)
  1994. return -1;
  1995. /* get actual time and make a string */
  1996. if (X509_gmtime_adj(a_tm, 0) == NULL) {
  1997. ASN1_UTCTIME_free(a_tm);
  1998. return -1;
  1999. }
  2000. a_tm_s = app_malloc(a_tm->length + 1, "time string");
  2001. memcpy(a_tm_s, a_tm->data, a_tm->length);
  2002. a_tm_s[a_tm->length] = '\0';
  2003. if (strncmp(a_tm_s, "49", 2) <= 0)
  2004. a_y2k = 1;
  2005. else
  2006. a_y2k = 0;
  2007. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  2008. rrow = sk_OPENSSL_PSTRING_value(db->db->data, i);
  2009. if (rrow[DB_type][0] == DB_TYPE_VAL) {
  2010. /* ignore entries that are not valid */
  2011. if (strncmp(rrow[DB_exp_date], "49", 2) <= 0)
  2012. db_y2k = 1;
  2013. else
  2014. db_y2k = 0;
  2015. if (db_y2k == a_y2k) {
  2016. /* all on the same y2k side */
  2017. if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0) {
  2018. rrow[DB_type][0] = DB_TYPE_EXP;
  2019. rrow[DB_type][1] = '\0';
  2020. cnt++;
  2021. BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
  2022. }
  2023. } else if (db_y2k < a_y2k) {
  2024. rrow[DB_type][0] = DB_TYPE_EXP;
  2025. rrow[DB_type][1] = '\0';
  2026. cnt++;
  2027. BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
  2028. }
  2029. }
  2030. }
  2031. ASN1_UTCTIME_free(a_tm);
  2032. OPENSSL_free(a_tm_s);
  2033. return cnt;
  2034. }
  2035. static const char *crl_reasons[] = {
  2036. /* CRL reason strings */
  2037. "unspecified",
  2038. "keyCompromise",
  2039. "CACompromise",
  2040. "affiliationChanged",
  2041. "superseded",
  2042. "cessationOfOperation",
  2043. "certificateHold",
  2044. "removeFromCRL",
  2045. /* Additional pseudo reasons */
  2046. "holdInstruction",
  2047. "keyTime",
  2048. "CAkeyTime"
  2049. };
  2050. #define NUM_REASONS OSSL_NELEM(crl_reasons)
  2051. /*
  2052. * Given revocation information convert to a DB string. The format of the
  2053. * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time
  2054. * (the current time). 'reason' is the optional CRL reason and 'extra' is any
  2055. * additional argument
  2056. */
  2057. static char *make_revocation_str(REVINFO_TYPE rev_type, const char *rev_arg)
  2058. {
  2059. char *str;
  2060. const char *reason = NULL, *other = NULL;
  2061. ASN1_OBJECT *otmp;
  2062. ASN1_UTCTIME *revtm = NULL;
  2063. int i;
  2064. switch (rev_type) {
  2065. case REV_NONE:
  2066. case REV_VALID:
  2067. break;
  2068. case REV_CRL_REASON:
  2069. for (i = 0; i < 8; i++) {
  2070. if (strcasecmp(rev_arg, crl_reasons[i]) == 0) {
  2071. reason = crl_reasons[i];
  2072. break;
  2073. }
  2074. }
  2075. if (reason == NULL) {
  2076. BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
  2077. return NULL;
  2078. }
  2079. break;
  2080. case REV_HOLD:
  2081. /* Argument is an OID */
  2082. otmp = OBJ_txt2obj(rev_arg, 0);
  2083. ASN1_OBJECT_free(otmp);
  2084. if (otmp == NULL) {
  2085. BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg);
  2086. return NULL;
  2087. }
  2088. reason = "holdInstruction";
  2089. other = rev_arg;
  2090. break;
  2091. case REV_KEY_COMPROMISE:
  2092. case REV_CA_COMPROMISE:
  2093. /* Argument is the key compromise time */
  2094. if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) {
  2095. BIO_printf(bio_err,
  2096. "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
  2097. rev_arg);
  2098. return NULL;
  2099. }
  2100. other = rev_arg;
  2101. if (rev_type == REV_KEY_COMPROMISE)
  2102. reason = "keyTime";
  2103. else
  2104. reason = "CAkeyTime";
  2105. break;
  2106. }
  2107. revtm = X509_gmtime_adj(NULL, 0);
  2108. if (!revtm)
  2109. return NULL;
  2110. i = revtm->length + 1;
  2111. if (reason)
  2112. i += strlen(reason) + 1;
  2113. if (other)
  2114. i += strlen(other) + 1;
  2115. str = app_malloc(i, "revocation reason");
  2116. OPENSSL_strlcpy(str, (char *)revtm->data, i);
  2117. if (reason) {
  2118. OPENSSL_strlcat(str, ",", i);
  2119. OPENSSL_strlcat(str, reason, i);
  2120. }
  2121. if (other) {
  2122. OPENSSL_strlcat(str, ",", i);
  2123. OPENSSL_strlcat(str, other, i);
  2124. }
  2125. ASN1_UTCTIME_free(revtm);
  2126. return str;
  2127. }
  2128. /*-
  2129. * Convert revocation field to X509_REVOKED entry
  2130. * return code:
  2131. * 0 error
  2132. * 1 OK
  2133. * 2 OK and some extensions added (i.e. V2 CRL)
  2134. */
  2135. static int make_revoked(X509_REVOKED *rev, const char *str)
  2136. {
  2137. char *tmp = NULL;
  2138. int reason_code = -1;
  2139. int i, ret = 0;
  2140. ASN1_OBJECT *hold = NULL;
  2141. ASN1_GENERALIZEDTIME *comp_time = NULL;
  2142. ASN1_ENUMERATED *rtmp = NULL;
  2143. ASN1_TIME *revDate = NULL;
  2144. i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
  2145. if (i == 0)
  2146. goto end;
  2147. if (rev && !X509_REVOKED_set_revocationDate(rev, revDate))
  2148. goto end;
  2149. if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) {
  2150. rtmp = ASN1_ENUMERATED_new();
  2151. if (rtmp == NULL || !ASN1_ENUMERATED_set(rtmp, reason_code))
  2152. goto end;
  2153. if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
  2154. goto end;
  2155. }
  2156. if (rev && comp_time) {
  2157. if (!X509_REVOKED_add1_ext_i2d
  2158. (rev, NID_invalidity_date, comp_time, 0, 0))
  2159. goto end;
  2160. }
  2161. if (rev && hold) {
  2162. if (!X509_REVOKED_add1_ext_i2d
  2163. (rev, NID_hold_instruction_code, hold, 0, 0))
  2164. goto end;
  2165. }
  2166. if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
  2167. ret = 2;
  2168. else
  2169. ret = 1;
  2170. end:
  2171. OPENSSL_free(tmp);
  2172. ASN1_OBJECT_free(hold);
  2173. ASN1_GENERALIZEDTIME_free(comp_time);
  2174. ASN1_ENUMERATED_free(rtmp);
  2175. ASN1_TIME_free(revDate);
  2176. return ret;
  2177. }
  2178. static int old_entry_print(const ASN1_OBJECT *obj, const ASN1_STRING *str)
  2179. {
  2180. char buf[25], *pbuf;
  2181. const char *p;
  2182. int j;
  2183. j = i2a_ASN1_OBJECT(bio_err, obj);
  2184. pbuf = buf;
  2185. for (j = 22 - j; j > 0; j--)
  2186. *(pbuf++) = ' ';
  2187. *(pbuf++) = ':';
  2188. *(pbuf++) = '\0';
  2189. BIO_puts(bio_err, buf);
  2190. if (str->type == V_ASN1_PRINTABLESTRING)
  2191. BIO_printf(bio_err, "PRINTABLE:'");
  2192. else if (str->type == V_ASN1_T61STRING)
  2193. BIO_printf(bio_err, "T61STRING:'");
  2194. else if (str->type == V_ASN1_IA5STRING)
  2195. BIO_printf(bio_err, "IA5STRING:'");
  2196. else if (str->type == V_ASN1_UNIVERSALSTRING)
  2197. BIO_printf(bio_err, "UNIVERSALSTRING:'");
  2198. else
  2199. BIO_printf(bio_err, "ASN.1 %2d:'", str->type);
  2200. p = (const char *)str->data;
  2201. for (j = str->length; j > 0; j--) {
  2202. if ((*p >= ' ') && (*p <= '~'))
  2203. BIO_printf(bio_err, "%c", *p);
  2204. else if (*p & 0x80)
  2205. BIO_printf(bio_err, "\\0x%02X", *p);
  2206. else if ((unsigned char)*p == 0xf7)
  2207. BIO_printf(bio_err, "^?");
  2208. else
  2209. BIO_printf(bio_err, "^%c", *p + '@');
  2210. p++;
  2211. }
  2212. BIO_printf(bio_err, "'\n");
  2213. return 1;
  2214. }
  2215. int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold,
  2216. ASN1_GENERALIZEDTIME **pinvtm, const char *str)
  2217. {
  2218. char *tmp;
  2219. char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
  2220. int reason_code = -1;
  2221. int ret = 0;
  2222. unsigned int i;
  2223. ASN1_OBJECT *hold = NULL;
  2224. ASN1_GENERALIZEDTIME *comp_time = NULL;
  2225. tmp = OPENSSL_strdup(str);
  2226. if (!tmp) {
  2227. BIO_printf(bio_err, "memory allocation failure\n");
  2228. goto end;
  2229. }
  2230. p = strchr(tmp, ',');
  2231. rtime_str = tmp;
  2232. if (p) {
  2233. *p = '\0';
  2234. p++;
  2235. reason_str = p;
  2236. p = strchr(p, ',');
  2237. if (p) {
  2238. *p = '\0';
  2239. arg_str = p + 1;
  2240. }
  2241. }
  2242. if (prevtm) {
  2243. *prevtm = ASN1_UTCTIME_new();
  2244. if (*prevtm == NULL) {
  2245. BIO_printf(bio_err, "memory allocation failure\n");
  2246. goto end;
  2247. }
  2248. if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) {
  2249. BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
  2250. goto end;
  2251. }
  2252. }
  2253. if (reason_str) {
  2254. for (i = 0; i < NUM_REASONS; i++) {
  2255. if (strcasecmp(reason_str, crl_reasons[i]) == 0) {
  2256. reason_code = i;
  2257. break;
  2258. }
  2259. }
  2260. if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) {
  2261. BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
  2262. goto end;
  2263. }
  2264. if (reason_code == 7) {
  2265. reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
  2266. } else if (reason_code == 8) { /* Hold instruction */
  2267. if (!arg_str) {
  2268. BIO_printf(bio_err, "missing hold instruction\n");
  2269. goto end;
  2270. }
  2271. reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
  2272. hold = OBJ_txt2obj(arg_str, 0);
  2273. if (!hold) {
  2274. BIO_printf(bio_err, "invalid object identifier %s\n", arg_str);
  2275. goto end;
  2276. }
  2277. if (phold)
  2278. *phold = hold;
  2279. else
  2280. ASN1_OBJECT_free(hold);
  2281. } else if ((reason_code == 9) || (reason_code == 10)) {
  2282. if (!arg_str) {
  2283. BIO_printf(bio_err, "missing compromised time\n");
  2284. goto end;
  2285. }
  2286. comp_time = ASN1_GENERALIZEDTIME_new();
  2287. if (comp_time == NULL) {
  2288. BIO_printf(bio_err, "memory allocation failure\n");
  2289. goto end;
  2290. }
  2291. if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) {
  2292. BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
  2293. goto end;
  2294. }
  2295. if (reason_code == 9)
  2296. reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
  2297. else
  2298. reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
  2299. }
  2300. }
  2301. if (preason)
  2302. *preason = reason_code;
  2303. if (pinvtm) {
  2304. *pinvtm = comp_time;
  2305. comp_time = NULL;
  2306. }
  2307. ret = 1;
  2308. end:
  2309. OPENSSL_free(tmp);
  2310. ASN1_GENERALIZEDTIME_free(comp_time);
  2311. return ret;
  2312. }