2
0

ca.c 87 KB

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