ca.c 86 KB

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