ca.c 86 KB

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