ca.c 88 KB

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