2
0

ca.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595
  1. /*
  2. * Copyright 1995-2021 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, def_nid, def_ret;
  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 Unix, 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 && outfile != NULL) {
  668. output_der = 1;
  669. batch = 1;
  670. }
  671. }
  672. def_ret = EVP_PKEY_get_default_digest_nid(pkey, &def_nid);
  673. /*
  674. * EVP_PKEY_get_default_digest_nid() returns 2 if the digest is
  675. * mandatory for this algorithm.
  676. */
  677. if (def_ret == 2 && def_nid == NID_undef) {
  678. /* The signing algorithm requires there to be no digest */
  679. dgst = EVP_md_null();
  680. } else if (md == NULL
  681. && (md = lookup_conf(conf, section, ENV_DEFAULT_MD)) == NULL) {
  682. goto end;
  683. } else {
  684. if (strcmp(md, "default") == 0) {
  685. if (def_ret <= 0) {
  686. BIO_puts(bio_err, "no default digest\n");
  687. goto end;
  688. }
  689. md = (char *)OBJ_nid2sn(def_nid);
  690. }
  691. if (!opt_md(md, &dgst))
  692. goto end;
  693. }
  694. if (req) {
  695. if (email_dn == 1) {
  696. char *tmp_email_dn = NULL;
  697. tmp_email_dn = NCONF_get_string(conf, section, ENV_DEFAULT_EMAIL_DN);
  698. if (tmp_email_dn != NULL && strcmp(tmp_email_dn, "no") == 0)
  699. email_dn = 0;
  700. }
  701. if (verbose)
  702. BIO_printf(bio_err, "message digest is %s\n",
  703. OBJ_nid2ln(EVP_MD_type(dgst)));
  704. if (policy == NULL
  705. && (policy = lookup_conf(conf, section, ENV_POLICY)) == NULL)
  706. goto end;
  707. if (verbose)
  708. BIO_printf(bio_err, "policy is %s\n", policy);
  709. if (NCONF_get_string(conf, section, ENV_RAND_SERIAL) != NULL) {
  710. rand_ser = 1;
  711. } else {
  712. serialfile = lookup_conf(conf, section, ENV_SERIAL);
  713. if (serialfile == NULL)
  714. goto end;
  715. }
  716. if (extconf == NULL) {
  717. /*
  718. * no '-extfile' option, so we look for extensions in the main
  719. * configuration file
  720. */
  721. if (extensions == NULL) {
  722. extensions = NCONF_get_string(conf, section, ENV_EXTENSIONS);
  723. if (extensions == NULL)
  724. ERR_clear_error();
  725. }
  726. if (extensions != NULL) {
  727. /* Check syntax of file */
  728. X509V3_CTX ctx;
  729. X509V3_set_ctx_test(&ctx);
  730. X509V3_set_nconf(&ctx, conf);
  731. if (!X509V3_EXT_add_nconf(conf, &ctx, extensions, NULL)) {
  732. BIO_printf(bio_err,
  733. "Error Loading extension section %s\n",
  734. extensions);
  735. ret = 1;
  736. goto end;
  737. }
  738. }
  739. }
  740. if (startdate == NULL) {
  741. startdate = NCONF_get_string(conf, section, ENV_DEFAULT_STARTDATE);
  742. if (startdate == NULL)
  743. ERR_clear_error();
  744. }
  745. if (startdate != NULL && !ASN1_TIME_set_string_X509(NULL, startdate)) {
  746. BIO_printf(bio_err,
  747. "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
  748. goto end;
  749. }
  750. if (startdate == NULL)
  751. startdate = "today";
  752. if (enddate == NULL) {
  753. enddate = NCONF_get_string(conf, section, ENV_DEFAULT_ENDDATE);
  754. if (enddate == NULL)
  755. ERR_clear_error();
  756. }
  757. if (enddate != NULL && !ASN1_TIME_set_string_X509(NULL, enddate)) {
  758. BIO_printf(bio_err,
  759. "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
  760. goto end;
  761. }
  762. if (days == 0) {
  763. if (!NCONF_get_number(conf, section, ENV_DEFAULT_DAYS, &days))
  764. days = 0;
  765. }
  766. if (enddate == NULL && days == 0) {
  767. BIO_printf(bio_err, "cannot lookup how many days to certify for\n");
  768. goto end;
  769. }
  770. if (rand_ser) {
  771. if ((serial = BN_new()) == NULL || !rand_serial(serial, NULL)) {
  772. BIO_printf(bio_err, "error generating serial number\n");
  773. goto end;
  774. }
  775. } else {
  776. if ((serial = load_serial(serialfile, create_ser, NULL)) == NULL) {
  777. BIO_printf(bio_err, "error while loading serial number\n");
  778. goto end;
  779. }
  780. if (verbose) {
  781. if (BN_is_zero(serial)) {
  782. BIO_printf(bio_err, "next serial number is 00\n");
  783. } else {
  784. if ((f = BN_bn2hex(serial)) == NULL)
  785. goto end;
  786. BIO_printf(bio_err, "next serial number is %s\n", f);
  787. OPENSSL_free(f);
  788. }
  789. }
  790. }
  791. if ((attribs = NCONF_get_section(conf, policy)) == NULL) {
  792. BIO_printf(bio_err, "unable to find 'section' for %s\n", policy);
  793. goto end;
  794. }
  795. if ((cert_sk = sk_X509_new_null()) == NULL) {
  796. BIO_printf(bio_err, "Memory allocation failure\n");
  797. goto end;
  798. }
  799. if (spkac_file != NULL) {
  800. total++;
  801. j = certify_spkac(&x, spkac_file, pkey, x509, dgst, sigopts,
  802. attribs, db, serial, subj, chtype, multirdn,
  803. email_dn, startdate, enddate, days, extensions,
  804. conf, verbose, certopt, get_nameopt(), default_op,
  805. ext_copy);
  806. if (j < 0)
  807. goto end;
  808. if (j > 0) {
  809. total_done++;
  810. BIO_printf(bio_err, "\n");
  811. if (!BN_add_word(serial, 1))
  812. goto end;
  813. if (!sk_X509_push(cert_sk, x)) {
  814. BIO_printf(bio_err, "Memory allocation failure\n");
  815. goto end;
  816. }
  817. }
  818. }
  819. if (ss_cert_file != NULL) {
  820. total++;
  821. j = certify_cert(&x, ss_cert_file, pkey, x509, dgst, sigopts,
  822. attribs,
  823. db, serial, subj, chtype, multirdn, email_dn,
  824. startdate, enddate, days, batch, extensions,
  825. conf, verbose, certopt, get_nameopt(), default_op,
  826. ext_copy);
  827. if (j < 0)
  828. goto end;
  829. if (j > 0) {
  830. total_done++;
  831. BIO_printf(bio_err, "\n");
  832. if (!BN_add_word(serial, 1))
  833. goto end;
  834. if (!sk_X509_push(cert_sk, x)) {
  835. BIO_printf(bio_err, "Memory allocation failure\n");
  836. goto end;
  837. }
  838. }
  839. }
  840. if (infile != NULL) {
  841. total++;
  842. j = certify(&x, infile, pkey, x509p, dgst, sigopts, attribs, db,
  843. serial, subj, chtype, multirdn, email_dn, startdate,
  844. enddate, days, batch, extensions, conf, verbose,
  845. certopt, get_nameopt(), default_op, ext_copy, selfsign);
  846. if (j < 0)
  847. goto end;
  848. if (j > 0) {
  849. total_done++;
  850. BIO_printf(bio_err, "\n");
  851. if (!BN_add_word(serial, 1))
  852. goto end;
  853. if (!sk_X509_push(cert_sk, x)) {
  854. BIO_printf(bio_err, "Memory allocation failure\n");
  855. goto end;
  856. }
  857. }
  858. }
  859. for (i = 0; i < argc; i++) {
  860. total++;
  861. j = certify(&x, argv[i], pkey, x509p, dgst, sigopts, attribs, db,
  862. serial, subj, chtype, multirdn, email_dn, startdate,
  863. enddate, days, batch, extensions, conf, verbose,
  864. certopt, get_nameopt(), default_op, ext_copy, selfsign);
  865. if (j < 0)
  866. goto end;
  867. if (j > 0) {
  868. total_done++;
  869. BIO_printf(bio_err, "\n");
  870. if (!BN_add_word(serial, 1)) {
  871. X509_free(x);
  872. goto end;
  873. }
  874. if (!sk_X509_push(cert_sk, x)) {
  875. BIO_printf(bio_err, "Memory allocation failure\n");
  876. X509_free(x);
  877. goto end;
  878. }
  879. }
  880. }
  881. /*
  882. * we have a stack of newly certified certificates and a data base
  883. * and serial number that need updating
  884. */
  885. if (sk_X509_num(cert_sk) > 0) {
  886. if (!batch) {
  887. BIO_printf(bio_err,
  888. "\n%d out of %d certificate requests certified, commit? [y/n]",
  889. total_done, total);
  890. (void)BIO_flush(bio_err);
  891. tmp[0] = '\0';
  892. if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
  893. BIO_printf(bio_err, "CERTIFICATION CANCELED: I/O error\n");
  894. ret = 0;
  895. goto end;
  896. }
  897. if (tmp[0] != 'y' && tmp[0] != 'Y') {
  898. BIO_printf(bio_err, "CERTIFICATION CANCELED\n");
  899. ret = 0;
  900. goto end;
  901. }
  902. }
  903. BIO_printf(bio_err, "Write out database with %d new entries\n",
  904. sk_X509_num(cert_sk));
  905. if (serialfile != NULL
  906. && !save_serial(serialfile, "new", serial, NULL))
  907. goto end;
  908. if (!save_index(dbfile, "new", db))
  909. goto end;
  910. }
  911. outdirlen = OPENSSL_strlcpy(new_cert, outdir, sizeof(new_cert));
  912. #ifndef OPENSSL_SYS_VMS
  913. outdirlen = OPENSSL_strlcat(new_cert, "/", sizeof(new_cert));
  914. #endif
  915. if (verbose)
  916. BIO_printf(bio_err, "writing new certificates\n");
  917. for (i = 0; i < sk_X509_num(cert_sk); i++) {
  918. BIO *Cout = NULL;
  919. X509 *xi = sk_X509_value(cert_sk, i);
  920. ASN1_INTEGER *serialNumber = X509_get_serialNumber(xi);
  921. const unsigned char *psn = ASN1_STRING_get0_data(serialNumber);
  922. const int snl = ASN1_STRING_length(serialNumber);
  923. const int filen_len = 2 * (snl > 0 ? snl : 1) + sizeof(".pem");
  924. char *n = new_cert + outdirlen;
  925. if (outdirlen + filen_len > PATH_MAX) {
  926. BIO_printf(bio_err, "certificate file name too long\n");
  927. goto end;
  928. }
  929. if (snl > 0) {
  930. static const char HEX_DIGITS[] = "0123456789ABCDEF";
  931. for (j = 0; j < snl; j++, psn++) {
  932. *n++ = HEX_DIGITS[*psn >> 4];
  933. *n++ = HEX_DIGITS[*psn & 0x0F];
  934. }
  935. } else {
  936. *(n++) = '0';
  937. *(n++) = '0';
  938. }
  939. *(n++) = '.';
  940. *(n++) = 'p';
  941. *(n++) = 'e';
  942. *(n++) = 'm';
  943. *n = '\0'; /* closing new_cert */
  944. if (verbose)
  945. BIO_printf(bio_err, "writing %s\n", new_cert);
  946. Sout = bio_open_default(outfile, 'w',
  947. output_der ? FORMAT_ASN1 : FORMAT_TEXT);
  948. if (Sout == NULL)
  949. goto end;
  950. Cout = BIO_new_file(new_cert, "w");
  951. if (Cout == NULL) {
  952. perror(new_cert);
  953. goto end;
  954. }
  955. write_new_certificate(Cout, xi, 0, notext);
  956. write_new_certificate(Sout, xi, output_der, notext);
  957. BIO_free_all(Cout);
  958. BIO_free_all(Sout);
  959. Sout = NULL;
  960. }
  961. if (sk_X509_num(cert_sk)) {
  962. /* Rename the database and the serial file */
  963. if (serialfile != NULL
  964. && !rotate_serial(serialfile, "new", "old"))
  965. goto end;
  966. if (!rotate_index(dbfile, "new", "old"))
  967. goto end;
  968. BIO_printf(bio_err, "Data Base Updated\n");
  969. }
  970. }
  971. /*****************************************************************/
  972. if (gencrl) {
  973. int crl_v2 = 0;
  974. if (crl_ext == NULL) {
  975. crl_ext = NCONF_get_string(conf, section, ENV_CRLEXT);
  976. if (crl_ext == NULL)
  977. ERR_clear_error();
  978. }
  979. if (crl_ext != NULL) {
  980. /* Check syntax of file */
  981. X509V3_CTX ctx;
  982. X509V3_set_ctx_test(&ctx);
  983. X509V3_set_nconf(&ctx, conf);
  984. if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) {
  985. BIO_printf(bio_err,
  986. "Error Loading CRL extension section %s\n", crl_ext);
  987. ret = 1;
  988. goto end;
  989. }
  990. }
  991. if ((crlnumberfile = NCONF_get_string(conf, section, ENV_CRLNUMBER))
  992. != NULL)
  993. if ((crlnumber = load_serial(crlnumberfile, 0, NULL)) == NULL) {
  994. BIO_printf(bio_err, "error while loading CRL number\n");
  995. goto end;
  996. }
  997. if (!crldays && !crlhours && !crlsec) {
  998. if (!NCONF_get_number(conf, section,
  999. ENV_DEFAULT_CRL_DAYS, &crldays))
  1000. crldays = 0;
  1001. if (!NCONF_get_number(conf, section,
  1002. ENV_DEFAULT_CRL_HOURS, &crlhours))
  1003. crlhours = 0;
  1004. ERR_clear_error();
  1005. }
  1006. if ((crldays == 0) && (crlhours == 0) && (crlsec == 0)) {
  1007. BIO_printf(bio_err,
  1008. "cannot lookup how long until the next CRL is issued\n");
  1009. goto end;
  1010. }
  1011. if (verbose)
  1012. BIO_printf(bio_err, "making CRL\n");
  1013. if ((crl = X509_CRL_new()) == NULL)
  1014. goto end;
  1015. if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509)))
  1016. goto end;
  1017. tmptm = ASN1_TIME_new();
  1018. if (tmptm == NULL
  1019. || X509_gmtime_adj(tmptm, 0) == NULL
  1020. || !X509_CRL_set1_lastUpdate(crl, tmptm)
  1021. || X509_time_adj_ex(tmptm, crldays, crlhours * 60 * 60 + crlsec,
  1022. NULL) == NULL) {
  1023. BIO_puts(bio_err, "error setting CRL nextUpdate\n");
  1024. ASN1_TIME_free(tmptm);
  1025. goto end;
  1026. }
  1027. X509_CRL_set1_nextUpdate(crl, tmptm);
  1028. ASN1_TIME_free(tmptm);
  1029. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  1030. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  1031. if (pp[DB_type][0] == DB_TYPE_REV) {
  1032. if ((r = X509_REVOKED_new()) == NULL)
  1033. goto end;
  1034. j = make_revoked(r, pp[DB_rev_date]);
  1035. if (!j)
  1036. goto end;
  1037. if (j == 2)
  1038. crl_v2 = 1;
  1039. if (!BN_hex2bn(&serial, pp[DB_serial]))
  1040. goto end;
  1041. tmpser = BN_to_ASN1_INTEGER(serial, NULL);
  1042. BN_free(serial);
  1043. serial = NULL;
  1044. if (!tmpser)
  1045. goto end;
  1046. X509_REVOKED_set_serialNumber(r, tmpser);
  1047. ASN1_INTEGER_free(tmpser);
  1048. X509_CRL_add0_revoked(crl, r);
  1049. }
  1050. }
  1051. /*
  1052. * sort the data so it will be written in serial number order
  1053. */
  1054. X509_CRL_sort(crl);
  1055. /* we now have a CRL */
  1056. if (verbose)
  1057. BIO_printf(bio_err, "signing CRL\n");
  1058. /* Add any extensions asked for */
  1059. if (crl_ext != NULL || crlnumberfile != NULL) {
  1060. X509V3_CTX crlctx;
  1061. X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
  1062. X509V3_set_nconf(&crlctx, conf);
  1063. if (crl_ext != NULL)
  1064. if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx, crl_ext, crl))
  1065. goto end;
  1066. if (crlnumberfile != NULL) {
  1067. tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
  1068. if (!tmpser)
  1069. goto end;
  1070. X509_CRL_add1_ext_i2d(crl, NID_crl_number, tmpser, 0, 0);
  1071. ASN1_INTEGER_free(tmpser);
  1072. crl_v2 = 1;
  1073. if (!BN_add_word(crlnumber, 1))
  1074. goto end;
  1075. }
  1076. }
  1077. if (crl_ext != NULL || crl_v2) {
  1078. if (!X509_CRL_set_version(crl, 1))
  1079. goto end; /* version 2 CRL */
  1080. }
  1081. /* we have a CRL number that need updating */
  1082. if (crlnumberfile != NULL
  1083. && !save_serial(crlnumberfile, "new", crlnumber, NULL))
  1084. goto end;
  1085. BN_free(crlnumber);
  1086. crlnumber = NULL;
  1087. if (!do_X509_CRL_sign(crl, pkey, dgst, sigopts))
  1088. goto end;
  1089. Sout = bio_open_default(outfile, 'w',
  1090. output_der ? FORMAT_ASN1 : FORMAT_TEXT);
  1091. if (Sout == NULL)
  1092. goto end;
  1093. PEM_write_bio_X509_CRL(Sout, crl);
  1094. /* Rename the crlnumber file */
  1095. if (crlnumberfile != NULL
  1096. && !rotate_serial(crlnumberfile, "new", "old"))
  1097. goto end;
  1098. }
  1099. /*****************************************************************/
  1100. if (dorevoke) {
  1101. if (infile == NULL) {
  1102. BIO_printf(bio_err, "no input files\n");
  1103. goto end;
  1104. } else {
  1105. X509 *revcert;
  1106. revcert = load_cert(infile, FORMAT_PEM, infile);
  1107. if (revcert == NULL)
  1108. goto end;
  1109. if (dorevoke == 2)
  1110. rev_type = REV_VALID;
  1111. j = do_revoke(revcert, db, rev_type, rev_arg);
  1112. if (j <= 0)
  1113. goto end;
  1114. X509_free(revcert);
  1115. if (!save_index(dbfile, "new", db))
  1116. goto end;
  1117. if (!rotate_index(dbfile, "new", "old"))
  1118. goto end;
  1119. BIO_printf(bio_err, "Data Base Updated\n");
  1120. }
  1121. }
  1122. ret = 0;
  1123. end:
  1124. if (ret)
  1125. ERR_print_errors(bio_err);
  1126. BIO_free_all(Sout);
  1127. BIO_free_all(out);
  1128. BIO_free_all(in);
  1129. sk_X509_pop_free(cert_sk, X509_free);
  1130. if (free_key)
  1131. OPENSSL_free(key);
  1132. BN_free(serial);
  1133. BN_free(crlnumber);
  1134. free_index(db);
  1135. sk_OPENSSL_STRING_free(sigopts);
  1136. EVP_PKEY_free(pkey);
  1137. X509_free(x509);
  1138. X509_CRL_free(crl);
  1139. NCONF_free(conf);
  1140. NCONF_free(extconf);
  1141. release_engine(e);
  1142. return ret;
  1143. }
  1144. static char *lookup_conf(const CONF *conf, const char *section, const char *tag)
  1145. {
  1146. char *entry = NCONF_get_string(conf, section, tag);
  1147. if (entry == NULL)
  1148. BIO_printf(bio_err, "variable lookup failed for %s::%s\n", section, tag);
  1149. return entry;
  1150. }
  1151. static int certify(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509,
  1152. const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
  1153. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1154. BIGNUM *serial, const char *subj, unsigned long chtype,
  1155. int multirdn, int email_dn, const char *startdate,
  1156. const char *enddate,
  1157. long days, int batch, const char *ext_sect, CONF *lconf,
  1158. int verbose, unsigned long certopt, unsigned long nameopt,
  1159. int default_op, int ext_copy, int selfsign)
  1160. {
  1161. X509_REQ *req = NULL;
  1162. BIO *in = NULL;
  1163. EVP_PKEY *pktmp = NULL;
  1164. int ok = -1, i;
  1165. in = BIO_new_file(infile, "r");
  1166. if (in == NULL) {
  1167. ERR_print_errors(bio_err);
  1168. goto end;
  1169. }
  1170. if ((req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL)) == NULL) {
  1171. BIO_printf(bio_err, "Error reading certificate request in %s\n",
  1172. infile);
  1173. goto end;
  1174. }
  1175. if (verbose)
  1176. X509_REQ_print_ex(bio_err, req, nameopt, X509_FLAG_COMPAT);
  1177. BIO_printf(bio_err, "Check that the request matches the signature\n");
  1178. if (selfsign && !X509_REQ_check_private_key(req, pkey)) {
  1179. BIO_printf(bio_err,
  1180. "Certificate request and CA private key do not match\n");
  1181. ok = 0;
  1182. goto end;
  1183. }
  1184. if ((pktmp = X509_REQ_get0_pubkey(req)) == NULL) {
  1185. BIO_printf(bio_err, "error unpacking public key\n");
  1186. goto end;
  1187. }
  1188. i = X509_REQ_verify(req, pktmp);
  1189. pktmp = NULL;
  1190. if (i < 0) {
  1191. ok = 0;
  1192. BIO_printf(bio_err, "Signature verification problems....\n");
  1193. ERR_print_errors(bio_err);
  1194. goto end;
  1195. }
  1196. if (i == 0) {
  1197. ok = 0;
  1198. BIO_printf(bio_err,
  1199. "Signature did not match the certificate request\n");
  1200. ERR_print_errors(bio_err);
  1201. goto end;
  1202. } else {
  1203. BIO_printf(bio_err, "Signature ok\n");
  1204. }
  1205. ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
  1206. chtype, multirdn, email_dn, startdate, enddate, days, batch,
  1207. verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
  1208. ext_copy, selfsign);
  1209. end:
  1210. X509_REQ_free(req);
  1211. BIO_free(in);
  1212. return ok;
  1213. }
  1214. static int certify_cert(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509,
  1215. const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
  1216. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1217. BIGNUM *serial, const char *subj, unsigned long chtype,
  1218. int multirdn, int email_dn, const char *startdate,
  1219. const char *enddate, long days, int batch, const char *ext_sect,
  1220. CONF *lconf, int verbose, unsigned long certopt,
  1221. unsigned long nameopt, int default_op, int ext_copy)
  1222. {
  1223. X509 *req = NULL;
  1224. X509_REQ *rreq = NULL;
  1225. EVP_PKEY *pktmp = NULL;
  1226. int ok = -1, i;
  1227. if ((req = load_cert(infile, FORMAT_PEM, infile)) == NULL)
  1228. goto end;
  1229. if (verbose)
  1230. X509_print(bio_err, req);
  1231. BIO_printf(bio_err, "Check that the request matches the signature\n");
  1232. if ((pktmp = X509_get0_pubkey(req)) == NULL) {
  1233. BIO_printf(bio_err, "error unpacking public key\n");
  1234. goto end;
  1235. }
  1236. i = X509_verify(req, pktmp);
  1237. if (i < 0) {
  1238. ok = 0;
  1239. BIO_printf(bio_err, "Signature verification problems....\n");
  1240. goto end;
  1241. }
  1242. if (i == 0) {
  1243. ok = 0;
  1244. BIO_printf(bio_err, "Signature did not match the certificate\n");
  1245. goto end;
  1246. } else {
  1247. BIO_printf(bio_err, "Signature ok\n");
  1248. }
  1249. if ((rreq = X509_to_X509_REQ(req, NULL, NULL)) == NULL)
  1250. goto end;
  1251. ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
  1252. chtype, multirdn, email_dn, startdate, enddate, days, batch,
  1253. verbose, rreq, ext_sect, lconf, certopt, nameopt, default_op,
  1254. ext_copy, 0);
  1255. end:
  1256. X509_REQ_free(rreq);
  1257. X509_free(req);
  1258. return ok;
  1259. }
  1260. static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
  1261. const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
  1262. STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,
  1263. const char *subj, unsigned long chtype, int multirdn,
  1264. int email_dn, const char *startdate, const char *enddate, long days,
  1265. int batch, int verbose, X509_REQ *req, const char *ext_sect,
  1266. CONF *lconf, unsigned long certopt, unsigned long nameopt,
  1267. int default_op, int ext_copy, int selfsign)
  1268. {
  1269. X509_NAME *name = NULL, *CAname = NULL, *subject = NULL;
  1270. const ASN1_TIME *tm;
  1271. ASN1_STRING *str, *str2;
  1272. ASN1_OBJECT *obj;
  1273. X509 *ret = NULL;
  1274. X509_NAME_ENTRY *ne, *tne;
  1275. EVP_PKEY *pktmp;
  1276. int ok = -1, i, j, last, nid;
  1277. const char *p;
  1278. CONF_VALUE *cv;
  1279. OPENSSL_STRING row[DB_NUMBER];
  1280. OPENSSL_STRING *irow = NULL;
  1281. OPENSSL_STRING *rrow = NULL;
  1282. char buf[25];
  1283. for (i = 0; i < DB_NUMBER; i++)
  1284. row[i] = NULL;
  1285. if (subj) {
  1286. X509_NAME *n = parse_name(subj, chtype, multirdn);
  1287. if (!n) {
  1288. ERR_print_errors(bio_err);
  1289. goto end;
  1290. }
  1291. X509_REQ_set_subject_name(req, n);
  1292. X509_NAME_free(n);
  1293. }
  1294. if (default_op)
  1295. BIO_printf(bio_err, "The Subject's Distinguished Name is as follows\n");
  1296. name = X509_REQ_get_subject_name(req);
  1297. for (i = 0; i < X509_NAME_entry_count(name); i++) {
  1298. ne = X509_NAME_get_entry(name, i);
  1299. str = X509_NAME_ENTRY_get_data(ne);
  1300. obj = X509_NAME_ENTRY_get_object(ne);
  1301. nid = OBJ_obj2nid(obj);
  1302. if (msie_hack) {
  1303. /* assume all type should be strings */
  1304. if (str->type == V_ASN1_UNIVERSALSTRING)
  1305. ASN1_UNIVERSALSTRING_to_string(str);
  1306. if (str->type == V_ASN1_IA5STRING && nid != NID_pkcs9_emailAddress)
  1307. str->type = V_ASN1_T61STRING;
  1308. if (nid == NID_pkcs9_emailAddress
  1309. && str->type == V_ASN1_PRINTABLESTRING)
  1310. str->type = V_ASN1_IA5STRING;
  1311. }
  1312. /* If no EMAIL is wanted in the subject */
  1313. if (nid == NID_pkcs9_emailAddress && !email_dn)
  1314. continue;
  1315. /* check some things */
  1316. if (nid == NID_pkcs9_emailAddress && str->type != V_ASN1_IA5STRING) {
  1317. BIO_printf(bio_err,
  1318. "\nemailAddress type needs to be of type IA5STRING\n");
  1319. goto end;
  1320. }
  1321. if (str->type != V_ASN1_BMPSTRING && str->type != V_ASN1_UTF8STRING) {
  1322. j = ASN1_PRINTABLE_type(str->data, str->length);
  1323. if ((j == V_ASN1_T61STRING && str->type != V_ASN1_T61STRING) ||
  1324. (j == V_ASN1_IA5STRING && str->type == V_ASN1_PRINTABLESTRING))
  1325. {
  1326. BIO_printf(bio_err,
  1327. "\nThe string contains characters that are illegal for the ASN.1 type\n");
  1328. goto end;
  1329. }
  1330. }
  1331. if (default_op)
  1332. old_entry_print(obj, str);
  1333. }
  1334. /* Ok, now we check the 'policy' stuff. */
  1335. if ((subject = X509_NAME_new()) == NULL) {
  1336. BIO_printf(bio_err, "Memory allocation failure\n");
  1337. goto end;
  1338. }
  1339. /* take a copy of the issuer name before we mess with it. */
  1340. if (selfsign)
  1341. CAname = X509_NAME_dup(name);
  1342. else
  1343. CAname = X509_NAME_dup(X509_get_subject_name(x509));
  1344. if (CAname == NULL)
  1345. goto end;
  1346. str = str2 = NULL;
  1347. for (i = 0; i < sk_CONF_VALUE_num(policy); i++) {
  1348. cv = sk_CONF_VALUE_value(policy, i); /* get the object id */
  1349. if ((j = OBJ_txt2nid(cv->name)) == NID_undef) {
  1350. BIO_printf(bio_err,
  1351. "%s:unknown object type in 'policy' configuration\n",
  1352. cv->name);
  1353. goto end;
  1354. }
  1355. obj = OBJ_nid2obj(j);
  1356. last = -1;
  1357. for (;;) {
  1358. X509_NAME_ENTRY *push = NULL;
  1359. /* lookup the object in the supplied name list */
  1360. j = X509_NAME_get_index_by_OBJ(name, obj, last);
  1361. if (j < 0) {
  1362. if (last != -1)
  1363. break;
  1364. tne = NULL;
  1365. } else {
  1366. tne = X509_NAME_get_entry(name, j);
  1367. }
  1368. last = j;
  1369. /* depending on the 'policy', decide what to do. */
  1370. if (strcmp(cv->value, "optional") == 0) {
  1371. if (tne != NULL)
  1372. push = tne;
  1373. } else if (strcmp(cv->value, "supplied") == 0) {
  1374. if (tne == NULL) {
  1375. BIO_printf(bio_err,
  1376. "The %s field needed to be supplied and was missing\n",
  1377. cv->name);
  1378. goto end;
  1379. } else {
  1380. push = tne;
  1381. }
  1382. } else if (strcmp(cv->value, "match") == 0) {
  1383. int last2;
  1384. if (tne == NULL) {
  1385. BIO_printf(bio_err,
  1386. "The mandatory %s field was missing\n",
  1387. cv->name);
  1388. goto end;
  1389. }
  1390. last2 = -1;
  1391. again2:
  1392. j = X509_NAME_get_index_by_OBJ(CAname, obj, last2);
  1393. if ((j < 0) && (last2 == -1)) {
  1394. BIO_printf(bio_err,
  1395. "The %s field does not exist in the CA certificate,\n"
  1396. "the 'policy' is misconfigured\n", cv->name);
  1397. goto end;
  1398. }
  1399. if (j >= 0) {
  1400. push = X509_NAME_get_entry(CAname, j);
  1401. str = X509_NAME_ENTRY_get_data(tne);
  1402. str2 = X509_NAME_ENTRY_get_data(push);
  1403. last2 = j;
  1404. if (ASN1_STRING_cmp(str, str2) != 0)
  1405. goto again2;
  1406. }
  1407. if (j < 0) {
  1408. BIO_printf(bio_err,
  1409. "The %s field is different between\n"
  1410. "CA certificate (%s) and the request (%s)\n",
  1411. cv->name,
  1412. ((str2 == NULL) ? "NULL" : (char *)str2->data),
  1413. ((str == NULL) ? "NULL" : (char *)str->data));
  1414. goto end;
  1415. }
  1416. } else {
  1417. BIO_printf(bio_err,
  1418. "%s:invalid type in 'policy' configuration\n",
  1419. cv->value);
  1420. goto end;
  1421. }
  1422. if (push != NULL) {
  1423. if (!X509_NAME_add_entry(subject, push, -1, 0)) {
  1424. BIO_printf(bio_err, "Memory allocation failure\n");
  1425. goto end;
  1426. }
  1427. }
  1428. if (j < 0)
  1429. break;
  1430. }
  1431. }
  1432. if (preserve) {
  1433. X509_NAME_free(subject);
  1434. /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
  1435. subject = X509_NAME_dup(name);
  1436. if (subject == NULL)
  1437. goto end;
  1438. }
  1439. /* We are now totally happy, lets make and sign the certificate */
  1440. if (verbose)
  1441. BIO_printf(bio_err,
  1442. "Everything appears to be ok, creating and signing the certificate\n");
  1443. if ((ret = X509_new()) == NULL)
  1444. goto end;
  1445. #ifdef X509_V3
  1446. /* Make it an X509 v3 certificate. */
  1447. if (!X509_set_version(ret, 2))
  1448. goto end;
  1449. #endif
  1450. if (BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(ret)) == NULL)
  1451. goto end;
  1452. if (selfsign) {
  1453. if (!X509_set_issuer_name(ret, subject))
  1454. goto end;
  1455. } else {
  1456. if (!X509_set_issuer_name(ret, X509_get_subject_name(x509)))
  1457. goto end;
  1458. }
  1459. if (!set_cert_times(ret, startdate, enddate, days))
  1460. goto end;
  1461. if (enddate != NULL) {
  1462. int tdays;
  1463. if (!ASN1_TIME_diff(&tdays, NULL, NULL, X509_get0_notAfter(ret)))
  1464. goto end;
  1465. days = tdays;
  1466. }
  1467. if (!X509_set_subject_name(ret, subject))
  1468. goto end;
  1469. pktmp = X509_REQ_get0_pubkey(req);
  1470. i = X509_set_pubkey(ret, pktmp);
  1471. if (!i)
  1472. goto end;
  1473. /* Lets add the extensions, if there are any */
  1474. if (ext_sect) {
  1475. X509V3_CTX ctx;
  1476. /* Initialize the context structure */
  1477. if (selfsign)
  1478. X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);
  1479. else
  1480. X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
  1481. if (extconf != NULL) {
  1482. if (verbose)
  1483. BIO_printf(bio_err, "Extra configuration file found\n");
  1484. /* Use the extconf configuration db LHASH */
  1485. X509V3_set_nconf(&ctx, extconf);
  1486. /* Test the structure (needed?) */
  1487. /* X509V3_set_ctx_test(&ctx); */
  1488. /* Adds exts contained in the configuration file */
  1489. if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect, ret)) {
  1490. BIO_printf(bio_err,
  1491. "ERROR: adding extensions in section %s\n",
  1492. ext_sect);
  1493. ERR_print_errors(bio_err);
  1494. goto end;
  1495. }
  1496. if (verbose)
  1497. BIO_printf(bio_err,
  1498. "Successfully added extensions from file.\n");
  1499. } else if (ext_sect) {
  1500. /* We found extensions to be set from config file */
  1501. X509V3_set_nconf(&ctx, lconf);
  1502. if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) {
  1503. BIO_printf(bio_err,
  1504. "ERROR: adding extensions in section %s\n",
  1505. ext_sect);
  1506. ERR_print_errors(bio_err);
  1507. goto end;
  1508. }
  1509. if (verbose)
  1510. BIO_printf(bio_err,
  1511. "Successfully added extensions from config\n");
  1512. }
  1513. }
  1514. /* Copy extensions from request (if any) */
  1515. if (!copy_extensions(ret, req, ext_copy)) {
  1516. BIO_printf(bio_err, "ERROR: adding extensions from request\n");
  1517. ERR_print_errors(bio_err);
  1518. goto end;
  1519. }
  1520. {
  1521. const STACK_OF(X509_EXTENSION) *exts = X509_get0_extensions(ret);
  1522. if (exts != NULL && sk_X509_EXTENSION_num(exts) > 0)
  1523. /* Make it an X509 v3 certificate. */
  1524. if (!X509_set_version(ret, 2))
  1525. goto end;
  1526. }
  1527. if (verbose)
  1528. BIO_printf(bio_err,
  1529. "The subject name appears to be ok, checking data base for clashes\n");
  1530. /* Build the correct Subject if no e-mail is wanted in the subject. */
  1531. if (!email_dn) {
  1532. X509_NAME_ENTRY *tmpne;
  1533. X509_NAME *dn_subject;
  1534. /*
  1535. * Its best to dup the subject DN and then delete any email addresses
  1536. * because this retains its structure.
  1537. */
  1538. if ((dn_subject = X509_NAME_dup(subject)) == NULL) {
  1539. BIO_printf(bio_err, "Memory allocation failure\n");
  1540. goto end;
  1541. }
  1542. i = -1;
  1543. while ((i = X509_NAME_get_index_by_NID(dn_subject,
  1544. NID_pkcs9_emailAddress,
  1545. i)) >= 0) {
  1546. tmpne = X509_NAME_delete_entry(dn_subject, i--);
  1547. X509_NAME_ENTRY_free(tmpne);
  1548. }
  1549. if (!X509_set_subject_name(ret, dn_subject)) {
  1550. X509_NAME_free(dn_subject);
  1551. goto end;
  1552. }
  1553. X509_NAME_free(dn_subject);
  1554. }
  1555. row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0);
  1556. if (row[DB_name] == NULL) {
  1557. BIO_printf(bio_err, "Memory allocation failure\n");
  1558. goto end;
  1559. }
  1560. if (BN_is_zero(serial))
  1561. row[DB_serial] = OPENSSL_strdup("00");
  1562. else
  1563. row[DB_serial] = BN_bn2hex(serial);
  1564. if (row[DB_serial] == NULL) {
  1565. BIO_printf(bio_err, "Memory allocation failure\n");
  1566. goto end;
  1567. }
  1568. if (row[DB_name][0] == '\0') {
  1569. /*
  1570. * An empty subject! We'll use the serial number instead. If
  1571. * unique_subject is in use then we don't want different entries with
  1572. * empty subjects matching each other.
  1573. */
  1574. OPENSSL_free(row[DB_name]);
  1575. row[DB_name] = OPENSSL_strdup(row[DB_serial]);
  1576. if (row[DB_name] == NULL) {
  1577. BIO_printf(bio_err, "Memory allocation failure\n");
  1578. goto end;
  1579. }
  1580. }
  1581. if (db->attributes.unique_subject) {
  1582. OPENSSL_STRING *crow = row;
  1583. rrow = TXT_DB_get_by_index(db->db, DB_name, crow);
  1584. if (rrow != NULL) {
  1585. BIO_printf(bio_err,
  1586. "ERROR:There is already a certificate for %s\n",
  1587. row[DB_name]);
  1588. }
  1589. }
  1590. if (rrow == NULL) {
  1591. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1592. if (rrow != NULL) {
  1593. BIO_printf(bio_err,
  1594. "ERROR:Serial number %s has already been issued,\n",
  1595. row[DB_serial]);
  1596. BIO_printf(bio_err,
  1597. " check the database/serial_file for corruption\n");
  1598. }
  1599. }
  1600. if (rrow != NULL) {
  1601. BIO_printf(bio_err, "The matching entry has the following details\n");
  1602. if (rrow[DB_type][0] == DB_TYPE_EXP)
  1603. p = "Expired";
  1604. else if (rrow[DB_type][0] == DB_TYPE_REV)
  1605. p = "Revoked";
  1606. else if (rrow[DB_type][0] == DB_TYPE_VAL)
  1607. p = "Valid";
  1608. else
  1609. p = "\ninvalid type, Data base error\n";
  1610. BIO_printf(bio_err, "Type :%s\n", p);;
  1611. if (rrow[DB_type][0] == DB_TYPE_REV) {
  1612. p = rrow[DB_exp_date];
  1613. if (p == NULL)
  1614. p = "undef";
  1615. BIO_printf(bio_err, "Was revoked on:%s\n", p);
  1616. }
  1617. p = rrow[DB_exp_date];
  1618. if (p == NULL)
  1619. p = "undef";
  1620. BIO_printf(bio_err, "Expires on :%s\n", p);
  1621. p = rrow[DB_serial];
  1622. if (p == NULL)
  1623. p = "undef";
  1624. BIO_printf(bio_err, "Serial Number :%s\n", p);
  1625. p = rrow[DB_file];
  1626. if (p == NULL)
  1627. p = "undef";
  1628. BIO_printf(bio_err, "File name :%s\n", p);
  1629. p = rrow[DB_name];
  1630. if (p == NULL)
  1631. p = "undef";
  1632. BIO_printf(bio_err, "Subject Name :%s\n", p);
  1633. ok = -1; /* This is now a 'bad' error. */
  1634. goto end;
  1635. }
  1636. if (!default_op) {
  1637. BIO_printf(bio_err, "Certificate Details:\n");
  1638. /*
  1639. * Never print signature details because signature not present
  1640. */
  1641. certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
  1642. X509_print_ex(bio_err, ret, nameopt, certopt);
  1643. }
  1644. BIO_printf(bio_err, "Certificate is to be certified until ");
  1645. ASN1_TIME_print(bio_err, X509_get0_notAfter(ret));
  1646. if (days)
  1647. BIO_printf(bio_err, " (%ld days)", days);
  1648. BIO_printf(bio_err, "\n");
  1649. if (!batch) {
  1650. BIO_printf(bio_err, "Sign the certificate? [y/n]:");
  1651. (void)BIO_flush(bio_err);
  1652. buf[0] = '\0';
  1653. if (fgets(buf, sizeof(buf), stdin) == NULL) {
  1654. BIO_printf(bio_err,
  1655. "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
  1656. ok = 0;
  1657. goto end;
  1658. }
  1659. if (!(buf[0] == 'y' || buf[0] == 'Y')) {
  1660. BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\n");
  1661. ok = 0;
  1662. goto end;
  1663. }
  1664. }
  1665. pktmp = X509_get0_pubkey(ret);
  1666. if (EVP_PKEY_missing_parameters(pktmp) &&
  1667. !EVP_PKEY_missing_parameters(pkey))
  1668. EVP_PKEY_copy_parameters(pktmp, pkey);
  1669. if (!do_X509_sign(ret, pkey, dgst, sigopts))
  1670. goto end;
  1671. /* We now just add it to the database as DB_TYPE_VAL('V') */
  1672. row[DB_type] = OPENSSL_strdup("V");
  1673. tm = X509_get0_notAfter(ret);
  1674. row[DB_exp_date] = app_malloc(tm->length + 1, "row expdate");
  1675. memcpy(row[DB_exp_date], tm->data, tm->length);
  1676. row[DB_exp_date][tm->length] = '\0';
  1677. row[DB_rev_date] = NULL;
  1678. row[DB_file] = OPENSSL_strdup("unknown");
  1679. if ((row[DB_type] == NULL) || (row[DB_file] == NULL)
  1680. || (row[DB_name] == NULL)) {
  1681. BIO_printf(bio_err, "Memory allocation failure\n");
  1682. goto end;
  1683. }
  1684. irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row space");
  1685. for (i = 0; i < DB_NUMBER; i++)
  1686. irow[i] = row[i];
  1687. irow[DB_NUMBER] = NULL;
  1688. if (!TXT_DB_insert(db->db, irow)) {
  1689. BIO_printf(bio_err, "failed to update database\n");
  1690. BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
  1691. goto end;
  1692. }
  1693. irow = NULL;
  1694. ok = 1;
  1695. end:
  1696. if (ok != 1) {
  1697. for (i = 0; i < DB_NUMBER; i++)
  1698. OPENSSL_free(row[i]);
  1699. }
  1700. OPENSSL_free(irow);
  1701. X509_NAME_free(CAname);
  1702. X509_NAME_free(subject);
  1703. if (ok <= 0)
  1704. X509_free(ret);
  1705. else
  1706. *xret = ret;
  1707. return ok;
  1708. }
  1709. static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext)
  1710. {
  1711. if (output_der) {
  1712. (void)i2d_X509_bio(bp, x);
  1713. return;
  1714. }
  1715. if (!notext)
  1716. X509_print(bp, x);
  1717. PEM_write_bio_X509(bp, x);
  1718. }
  1719. static int certify_spkac(X509 **xret, const char *infile, EVP_PKEY *pkey,
  1720. X509 *x509, const EVP_MD *dgst,
  1721. STACK_OF(OPENSSL_STRING) *sigopts,
  1722. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1723. BIGNUM *serial, const char *subj, unsigned long chtype,
  1724. int multirdn, int email_dn, const char *startdate,
  1725. const char *enddate, long days, const char *ext_sect,
  1726. CONF *lconf, int verbose, unsigned long certopt,
  1727. unsigned long nameopt, int default_op, int ext_copy)
  1728. {
  1729. STACK_OF(CONF_VALUE) *sk = NULL;
  1730. LHASH_OF(CONF_VALUE) *parms = NULL;
  1731. X509_REQ *req = NULL;
  1732. CONF_VALUE *cv = NULL;
  1733. NETSCAPE_SPKI *spki = NULL;
  1734. char *type, *buf;
  1735. EVP_PKEY *pktmp = NULL;
  1736. X509_NAME *n = NULL;
  1737. X509_NAME_ENTRY *ne = NULL;
  1738. int ok = -1, i, j;
  1739. long errline;
  1740. int nid;
  1741. /*
  1742. * Load input file into a hash table. (This is just an easy
  1743. * way to read and parse the file, then put it into a convenient
  1744. * STACK format).
  1745. */
  1746. parms = CONF_load(NULL, infile, &errline);
  1747. if (parms == NULL) {
  1748. BIO_printf(bio_err, "error on line %ld of %s\n", errline, infile);
  1749. ERR_print_errors(bio_err);
  1750. goto end;
  1751. }
  1752. sk = CONF_get_section(parms, "default");
  1753. if (sk_CONF_VALUE_num(sk) == 0) {
  1754. BIO_printf(bio_err, "no name/value pairs found in %s\n", infile);
  1755. goto end;
  1756. }
  1757. /*
  1758. * Now create a dummy X509 request structure. We don't actually
  1759. * have an X509 request, but we have many of the components
  1760. * (a public key, various DN components). The idea is that we
  1761. * put these components into the right X509 request structure
  1762. * and we can use the same code as if you had a real X509 request.
  1763. */
  1764. req = X509_REQ_new();
  1765. if (req == NULL) {
  1766. ERR_print_errors(bio_err);
  1767. goto end;
  1768. }
  1769. /*
  1770. * Build up the subject name set.
  1771. */
  1772. n = X509_REQ_get_subject_name(req);
  1773. for (i = 0;; i++) {
  1774. if (sk_CONF_VALUE_num(sk) <= i)
  1775. break;
  1776. cv = sk_CONF_VALUE_value(sk, i);
  1777. type = cv->name;
  1778. /*
  1779. * Skip past any leading X. X: X, etc to allow for multiple instances
  1780. */
  1781. for (buf = cv->name; *buf; buf++)
  1782. if ((*buf == ':') || (*buf == ',') || (*buf == '.')) {
  1783. buf++;
  1784. if (*buf)
  1785. type = buf;
  1786. break;
  1787. }
  1788. buf = cv->value;
  1789. if ((nid = OBJ_txt2nid(type)) == NID_undef) {
  1790. if (strcmp(type, "SPKAC") == 0) {
  1791. spki = NETSCAPE_SPKI_b64_decode(cv->value, -1);
  1792. if (spki == NULL) {
  1793. BIO_printf(bio_err,
  1794. "unable to load Netscape SPKAC structure\n");
  1795. ERR_print_errors(bio_err);
  1796. goto end;
  1797. }
  1798. }
  1799. continue;
  1800. }
  1801. if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
  1802. (unsigned char *)buf, -1, -1, 0))
  1803. goto end;
  1804. }
  1805. if (spki == NULL) {
  1806. BIO_printf(bio_err, "Netscape SPKAC structure not found in %s\n",
  1807. infile);
  1808. goto end;
  1809. }
  1810. /*
  1811. * Now extract the key from the SPKI structure.
  1812. */
  1813. BIO_printf(bio_err, "Check that the SPKAC request matches the signature\n");
  1814. if ((pktmp = NETSCAPE_SPKI_get_pubkey(spki)) == NULL) {
  1815. BIO_printf(bio_err, "error unpacking SPKAC public key\n");
  1816. goto end;
  1817. }
  1818. j = NETSCAPE_SPKI_verify(spki, pktmp);
  1819. if (j <= 0) {
  1820. EVP_PKEY_free(pktmp);
  1821. BIO_printf(bio_err,
  1822. "signature verification failed on SPKAC public key\n");
  1823. goto end;
  1824. }
  1825. BIO_printf(bio_err, "Signature ok\n");
  1826. X509_REQ_set_pubkey(req, pktmp);
  1827. EVP_PKEY_free(pktmp);
  1828. ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
  1829. chtype, multirdn, email_dn, startdate, enddate, days, 1,
  1830. verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
  1831. ext_copy, 0);
  1832. end:
  1833. X509_REQ_free(req);
  1834. CONF_free(parms);
  1835. NETSCAPE_SPKI_free(spki);
  1836. X509_NAME_ENTRY_free(ne);
  1837. return ok;
  1838. }
  1839. static int check_time_format(const char *str)
  1840. {
  1841. return ASN1_TIME_set_string(NULL, str);
  1842. }
  1843. static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type,
  1844. const char *value)
  1845. {
  1846. const ASN1_TIME *tm = NULL;
  1847. char *row[DB_NUMBER], **rrow, **irow;
  1848. char *rev_str = NULL;
  1849. BIGNUM *bn = NULL;
  1850. int ok = -1, i;
  1851. for (i = 0; i < DB_NUMBER; i++)
  1852. row[i] = NULL;
  1853. row[DB_name] = X509_NAME_oneline(X509_get_subject_name(x509), NULL, 0);
  1854. bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509), NULL);
  1855. if (!bn)
  1856. goto end;
  1857. if (BN_is_zero(bn))
  1858. row[DB_serial] = OPENSSL_strdup("00");
  1859. else
  1860. row[DB_serial] = BN_bn2hex(bn);
  1861. BN_free(bn);
  1862. if (row[DB_name] != NULL && row[DB_name][0] == '\0') {
  1863. /* Entries with empty Subjects actually use the serial number instead */
  1864. OPENSSL_free(row[DB_name]);
  1865. row[DB_name] = OPENSSL_strdup(row[DB_serial]);
  1866. }
  1867. if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) {
  1868. BIO_printf(bio_err, "Memory allocation failure\n");
  1869. goto end;
  1870. }
  1871. /*
  1872. * We have to lookup by serial number because name lookup skips revoked
  1873. * certs
  1874. */
  1875. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1876. if (rrow == NULL) {
  1877. BIO_printf(bio_err,
  1878. "Adding Entry with serial number %s to DB for %s\n",
  1879. row[DB_serial], row[DB_name]);
  1880. /* We now just add it to the database as DB_TYPE_REV('V') */
  1881. row[DB_type] = OPENSSL_strdup("V");
  1882. tm = X509_get0_notAfter(x509);
  1883. row[DB_exp_date] = app_malloc(tm->length + 1, "row exp_data");
  1884. memcpy(row[DB_exp_date], tm->data, tm->length);
  1885. row[DB_exp_date][tm->length] = '\0';
  1886. row[DB_rev_date] = NULL;
  1887. row[DB_file] = OPENSSL_strdup("unknown");
  1888. if (row[DB_type] == NULL || row[DB_file] == NULL) {
  1889. BIO_printf(bio_err, "Memory allocation failure\n");
  1890. goto end;
  1891. }
  1892. irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row ptr");
  1893. for (i = 0; i < DB_NUMBER; i++)
  1894. irow[i] = row[i];
  1895. irow[DB_NUMBER] = NULL;
  1896. if (!TXT_DB_insert(db->db, irow)) {
  1897. BIO_printf(bio_err, "failed to update database\n");
  1898. BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
  1899. OPENSSL_free(irow);
  1900. goto end;
  1901. }
  1902. for (i = 0; i < DB_NUMBER; i++)
  1903. row[i] = NULL;
  1904. /* Revoke Certificate */
  1905. if (rev_type == REV_VALID)
  1906. ok = 1;
  1907. else
  1908. /* Retry revocation after DB insertion */
  1909. ok = do_revoke(x509, db, rev_type, value);
  1910. goto end;
  1911. } else if (index_name_cmp_noconst(row, rrow)) {
  1912. BIO_printf(bio_err, "ERROR:name does not match %s\n", row[DB_name]);
  1913. goto end;
  1914. } else if (rev_type == REV_VALID) {
  1915. BIO_printf(bio_err, "ERROR:Already present, serial number %s\n",
  1916. row[DB_serial]);
  1917. goto end;
  1918. } else if (rrow[DB_type][0] == DB_TYPE_REV) {
  1919. BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n",
  1920. row[DB_serial]);
  1921. goto end;
  1922. } else {
  1923. BIO_printf(bio_err, "Revoking Certificate %s.\n", rrow[DB_serial]);
  1924. rev_str = make_revocation_str(rev_type, value);
  1925. if (!rev_str) {
  1926. BIO_printf(bio_err, "Error in revocation arguments\n");
  1927. goto end;
  1928. }
  1929. rrow[DB_type][0] = DB_TYPE_REV;
  1930. rrow[DB_type][1] = '\0';
  1931. rrow[DB_rev_date] = rev_str;
  1932. }
  1933. ok = 1;
  1934. end:
  1935. for (i = 0; i < DB_NUMBER; i++)
  1936. OPENSSL_free(row[i]);
  1937. return ok;
  1938. }
  1939. static int get_certificate_status(const char *serial, CA_DB *db)
  1940. {
  1941. char *row[DB_NUMBER], **rrow;
  1942. int ok = -1, i;
  1943. size_t serial_len = strlen(serial);
  1944. /* Free Resources */
  1945. for (i = 0; i < DB_NUMBER; i++)
  1946. row[i] = NULL;
  1947. /* Malloc needed char spaces */
  1948. row[DB_serial] = app_malloc(serial_len + 2, "row serial#");
  1949. if (serial_len % 2) {
  1950. /*
  1951. * Set the first char to 0
  1952. */
  1953. row[DB_serial][0] = '0';
  1954. /* Copy String from serial to row[DB_serial] */
  1955. memcpy(row[DB_serial] + 1, serial, serial_len);
  1956. row[DB_serial][serial_len + 1] = '\0';
  1957. } else {
  1958. /* Copy String from serial to row[DB_serial] */
  1959. memcpy(row[DB_serial], serial, serial_len);
  1960. row[DB_serial][serial_len] = '\0';
  1961. }
  1962. /* Make it Upper Case */
  1963. make_uppercase(row[DB_serial]);
  1964. ok = 1;
  1965. /* Search for the certificate */
  1966. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1967. if (rrow == NULL) {
  1968. BIO_printf(bio_err, "Serial %s not present in db.\n", row[DB_serial]);
  1969. ok = -1;
  1970. goto end;
  1971. } else if (rrow[DB_type][0] == DB_TYPE_VAL) {
  1972. BIO_printf(bio_err, "%s=Valid (%c)\n",
  1973. row[DB_serial], rrow[DB_type][0]);
  1974. goto end;
  1975. } else if (rrow[DB_type][0] == DB_TYPE_REV) {
  1976. BIO_printf(bio_err, "%s=Revoked (%c)\n",
  1977. row[DB_serial], rrow[DB_type][0]);
  1978. goto end;
  1979. } else if (rrow[DB_type][0] == DB_TYPE_EXP) {
  1980. BIO_printf(bio_err, "%s=Expired (%c)\n",
  1981. row[DB_serial], rrow[DB_type][0]);
  1982. goto end;
  1983. } else if (rrow[DB_type][0] == DB_TYPE_SUSP) {
  1984. BIO_printf(bio_err, "%s=Suspended (%c)\n",
  1985. row[DB_serial], rrow[DB_type][0]);
  1986. goto end;
  1987. } else {
  1988. BIO_printf(bio_err, "%s=Unknown (%c).\n",
  1989. row[DB_serial], rrow[DB_type][0]);
  1990. ok = -1;
  1991. }
  1992. end:
  1993. for (i = 0; i < DB_NUMBER; i++) {
  1994. OPENSSL_free(row[i]);
  1995. }
  1996. return ok;
  1997. }
  1998. static int do_updatedb(CA_DB *db)
  1999. {
  2000. ASN1_TIME *a_tm = NULL;
  2001. int i, cnt = 0;
  2002. char **rrow;
  2003. a_tm = ASN1_TIME_new();
  2004. if (a_tm == NULL)
  2005. return -1;
  2006. /* get actual time */
  2007. if (X509_gmtime_adj(a_tm, 0) == NULL) {
  2008. ASN1_TIME_free(a_tm);
  2009. return -1;
  2010. }
  2011. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  2012. rrow = sk_OPENSSL_PSTRING_value(db->db->data, i);
  2013. if (rrow[DB_type][0] == DB_TYPE_VAL) {
  2014. /* ignore entries that are not valid */
  2015. ASN1_TIME *exp_date = NULL;
  2016. exp_date = ASN1_TIME_new();
  2017. if (exp_date == NULL) {
  2018. ASN1_TIME_free(a_tm);
  2019. return -1;
  2020. }
  2021. if (!ASN1_TIME_set_string(exp_date, rrow[DB_exp_date])) {
  2022. ASN1_TIME_free(a_tm);
  2023. ASN1_TIME_free(exp_date);
  2024. return -1;
  2025. }
  2026. if (ASN1_TIME_compare(exp_date, a_tm) <= 0) {
  2027. rrow[DB_type][0] = DB_TYPE_EXP;
  2028. rrow[DB_type][1] = '\0';
  2029. cnt++;
  2030. BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
  2031. }
  2032. ASN1_TIME_free(exp_date);
  2033. }
  2034. }
  2035. ASN1_TIME_free(a_tm);
  2036. return cnt;
  2037. }
  2038. static const char *crl_reasons[] = {
  2039. /* CRL reason strings */
  2040. "unspecified",
  2041. "keyCompromise",
  2042. "CACompromise",
  2043. "affiliationChanged",
  2044. "superseded",
  2045. "cessationOfOperation",
  2046. "certificateHold",
  2047. "removeFromCRL",
  2048. /* Additional pseudo reasons */
  2049. "holdInstruction",
  2050. "keyTime",
  2051. "CAkeyTime"
  2052. };
  2053. #define NUM_REASONS OSSL_NELEM(crl_reasons)
  2054. /*
  2055. * Given revocation information convert to a DB string. The format of the
  2056. * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time
  2057. * (the current time). 'reason' is the optional CRL reason and 'extra' is any
  2058. * additional argument
  2059. */
  2060. static char *make_revocation_str(REVINFO_TYPE rev_type, const char *rev_arg)
  2061. {
  2062. char *str;
  2063. const char *reason = NULL, *other = NULL;
  2064. ASN1_OBJECT *otmp;
  2065. ASN1_UTCTIME *revtm = NULL;
  2066. int i;
  2067. switch (rev_type) {
  2068. case REV_NONE:
  2069. case REV_VALID:
  2070. break;
  2071. case REV_CRL_REASON:
  2072. for (i = 0; i < 8; i++) {
  2073. if (strcasecmp(rev_arg, crl_reasons[i]) == 0) {
  2074. reason = crl_reasons[i];
  2075. break;
  2076. }
  2077. }
  2078. if (reason == NULL) {
  2079. BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
  2080. return NULL;
  2081. }
  2082. break;
  2083. case REV_HOLD:
  2084. /* Argument is an OID */
  2085. otmp = OBJ_txt2obj(rev_arg, 0);
  2086. ASN1_OBJECT_free(otmp);
  2087. if (otmp == NULL) {
  2088. BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg);
  2089. return NULL;
  2090. }
  2091. reason = "holdInstruction";
  2092. other = rev_arg;
  2093. break;
  2094. case REV_KEY_COMPROMISE:
  2095. case REV_CA_COMPROMISE:
  2096. /* Argument is the key compromise time */
  2097. if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) {
  2098. BIO_printf(bio_err,
  2099. "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
  2100. rev_arg);
  2101. return NULL;
  2102. }
  2103. other = rev_arg;
  2104. if (rev_type == REV_KEY_COMPROMISE)
  2105. reason = "keyTime";
  2106. else
  2107. reason = "CAkeyTime";
  2108. break;
  2109. }
  2110. revtm = X509_gmtime_adj(NULL, 0);
  2111. if (!revtm)
  2112. return NULL;
  2113. i = revtm->length + 1;
  2114. if (reason)
  2115. i += strlen(reason) + 1;
  2116. if (other)
  2117. i += strlen(other) + 1;
  2118. str = app_malloc(i, "revocation reason");
  2119. OPENSSL_strlcpy(str, (char *)revtm->data, i);
  2120. if (reason) {
  2121. OPENSSL_strlcat(str, ",", i);
  2122. OPENSSL_strlcat(str, reason, i);
  2123. }
  2124. if (other) {
  2125. OPENSSL_strlcat(str, ",", i);
  2126. OPENSSL_strlcat(str, other, i);
  2127. }
  2128. ASN1_UTCTIME_free(revtm);
  2129. return str;
  2130. }
  2131. /*-
  2132. * Convert revocation field to X509_REVOKED entry
  2133. * return code:
  2134. * 0 error
  2135. * 1 OK
  2136. * 2 OK and some extensions added (i.e. V2 CRL)
  2137. */
  2138. static int make_revoked(X509_REVOKED *rev, const char *str)
  2139. {
  2140. char *tmp = NULL;
  2141. int reason_code = -1;
  2142. int i, ret = 0;
  2143. ASN1_OBJECT *hold = NULL;
  2144. ASN1_GENERALIZEDTIME *comp_time = NULL;
  2145. ASN1_ENUMERATED *rtmp = NULL;
  2146. ASN1_TIME *revDate = NULL;
  2147. i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
  2148. if (i == 0)
  2149. goto end;
  2150. if (rev && !X509_REVOKED_set_revocationDate(rev, revDate))
  2151. goto end;
  2152. if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) {
  2153. rtmp = ASN1_ENUMERATED_new();
  2154. if (rtmp == NULL || !ASN1_ENUMERATED_set(rtmp, reason_code))
  2155. goto end;
  2156. if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
  2157. goto end;
  2158. }
  2159. if (rev && comp_time) {
  2160. if (!X509_REVOKED_add1_ext_i2d
  2161. (rev, NID_invalidity_date, comp_time, 0, 0))
  2162. goto end;
  2163. }
  2164. if (rev && hold) {
  2165. if (!X509_REVOKED_add1_ext_i2d
  2166. (rev, NID_hold_instruction_code, hold, 0, 0))
  2167. goto end;
  2168. }
  2169. if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
  2170. ret = 2;
  2171. else
  2172. ret = 1;
  2173. end:
  2174. OPENSSL_free(tmp);
  2175. ASN1_OBJECT_free(hold);
  2176. ASN1_GENERALIZEDTIME_free(comp_time);
  2177. ASN1_ENUMERATED_free(rtmp);
  2178. ASN1_TIME_free(revDate);
  2179. return ret;
  2180. }
  2181. static int old_entry_print(const ASN1_OBJECT *obj, const ASN1_STRING *str)
  2182. {
  2183. char buf[25], *pbuf;
  2184. const char *p;
  2185. int j;
  2186. j = i2a_ASN1_OBJECT(bio_err, obj);
  2187. pbuf = buf;
  2188. for (j = 22 - j; j > 0; j--)
  2189. *(pbuf++) = ' ';
  2190. *(pbuf++) = ':';
  2191. *(pbuf++) = '\0';
  2192. BIO_puts(bio_err, buf);
  2193. if (str->type == V_ASN1_PRINTABLESTRING)
  2194. BIO_printf(bio_err, "PRINTABLE:'");
  2195. else if (str->type == V_ASN1_T61STRING)
  2196. BIO_printf(bio_err, "T61STRING:'");
  2197. else if (str->type == V_ASN1_IA5STRING)
  2198. BIO_printf(bio_err, "IA5STRING:'");
  2199. else if (str->type == V_ASN1_UNIVERSALSTRING)
  2200. BIO_printf(bio_err, "UNIVERSALSTRING:'");
  2201. else
  2202. BIO_printf(bio_err, "ASN.1 %2d:'", str->type);
  2203. p = (const char *)str->data;
  2204. for (j = str->length; j > 0; j--) {
  2205. if ((*p >= ' ') && (*p <= '~'))
  2206. BIO_printf(bio_err, "%c", *p);
  2207. else if (*p & 0x80)
  2208. BIO_printf(bio_err, "\\0x%02X", *p);
  2209. else if ((unsigned char)*p == 0xf7)
  2210. BIO_printf(bio_err, "^?");
  2211. else
  2212. BIO_printf(bio_err, "^%c", *p + '@');
  2213. p++;
  2214. }
  2215. BIO_printf(bio_err, "'\n");
  2216. return 1;
  2217. }
  2218. int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold,
  2219. ASN1_GENERALIZEDTIME **pinvtm, const char *str)
  2220. {
  2221. char *tmp;
  2222. char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
  2223. int reason_code = -1;
  2224. int ret = 0;
  2225. unsigned int i;
  2226. ASN1_OBJECT *hold = NULL;
  2227. ASN1_GENERALIZEDTIME *comp_time = NULL;
  2228. tmp = OPENSSL_strdup(str);
  2229. if (!tmp) {
  2230. BIO_printf(bio_err, "memory allocation failure\n");
  2231. goto end;
  2232. }
  2233. p = strchr(tmp, ',');
  2234. rtime_str = tmp;
  2235. if (p) {
  2236. *p = '\0';
  2237. p++;
  2238. reason_str = p;
  2239. p = strchr(p, ',');
  2240. if (p) {
  2241. *p = '\0';
  2242. arg_str = p + 1;
  2243. }
  2244. }
  2245. if (prevtm) {
  2246. *prevtm = ASN1_UTCTIME_new();
  2247. if (*prevtm == NULL) {
  2248. BIO_printf(bio_err, "memory allocation failure\n");
  2249. goto end;
  2250. }
  2251. if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) {
  2252. BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
  2253. goto end;
  2254. }
  2255. }
  2256. if (reason_str) {
  2257. for (i = 0; i < NUM_REASONS; i++) {
  2258. if (strcasecmp(reason_str, crl_reasons[i]) == 0) {
  2259. reason_code = i;
  2260. break;
  2261. }
  2262. }
  2263. if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) {
  2264. BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
  2265. goto end;
  2266. }
  2267. if (reason_code == 7) {
  2268. reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
  2269. } else if (reason_code == 8) { /* Hold instruction */
  2270. if (!arg_str) {
  2271. BIO_printf(bio_err, "missing hold instruction\n");
  2272. goto end;
  2273. }
  2274. reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
  2275. hold = OBJ_txt2obj(arg_str, 0);
  2276. if (!hold) {
  2277. BIO_printf(bio_err, "invalid object identifier %s\n", arg_str);
  2278. goto end;
  2279. }
  2280. if (phold)
  2281. *phold = hold;
  2282. else
  2283. ASN1_OBJECT_free(hold);
  2284. } else if ((reason_code == 9) || (reason_code == 10)) {
  2285. if (!arg_str) {
  2286. BIO_printf(bio_err, "missing compromised time\n");
  2287. goto end;
  2288. }
  2289. comp_time = ASN1_GENERALIZEDTIME_new();
  2290. if (comp_time == NULL) {
  2291. BIO_printf(bio_err, "memory allocation failure\n");
  2292. goto end;
  2293. }
  2294. if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) {
  2295. BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
  2296. goto end;
  2297. }
  2298. if (reason_code == 9)
  2299. reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
  2300. else
  2301. reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
  2302. }
  2303. }
  2304. if (preason)
  2305. *preason = reason_code;
  2306. if (pinvtm) {
  2307. *pinvtm = comp_time;
  2308. comp_time = NULL;
  2309. }
  2310. ret = 1;
  2311. end:
  2312. OPENSSL_free(tmp);
  2313. ASN1_GENERALIZEDTIME_free(comp_time);
  2314. return ret;
  2315. }