ca.c 84 KB

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