ca.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003
  1. /* apps/ca.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* The PPKI stuff has been donated by Jeff Barber <jeffb@issl.atl.hp.com> */
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <ctype.h>
  63. #include <sys/types.h>
  64. #include <openssl/conf.h>
  65. #include <openssl/bio.h>
  66. #include <openssl/err.h>
  67. #include <openssl/bn.h>
  68. #include <openssl/txt_db.h>
  69. #include <openssl/evp.h>
  70. #include <openssl/x509.h>
  71. #include <openssl/x509v3.h>
  72. #include <openssl/objects.h>
  73. #include <openssl/ocsp.h>
  74. #include <openssl/pem.h>
  75. #ifndef W_OK
  76. # ifdef OPENSSL_SYS_VMS
  77. # if defined(__DECC)
  78. # include <unistd.h>
  79. # else
  80. # include <unixlib.h>
  81. # endif
  82. # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_NETWARE)
  83. # include <sys/file.h>
  84. # endif
  85. #endif
  86. #include "apps.h"
  87. #ifndef W_OK
  88. # define F_OK 0
  89. # define X_OK 1
  90. # define W_OK 2
  91. # define R_OK 4
  92. #endif
  93. #undef PROG
  94. #define PROG ca_main
  95. #define BASE_SECTION "ca"
  96. #define CONFIG_FILE "openssl.cnf"
  97. #define ENV_DEFAULT_CA "default_ca"
  98. #define STRING_MASK "string_mask"
  99. #define UTF8_IN "utf8"
  100. #define ENV_DIR "dir"
  101. #define ENV_CERTS "certs"
  102. #define ENV_CRL_DIR "crl_dir"
  103. #define ENV_CA_DB "CA_DB"
  104. #define ENV_NEW_CERTS_DIR "new_certs_dir"
  105. #define ENV_CERTIFICATE "certificate"
  106. #define ENV_SERIAL "serial"
  107. #define ENV_CRLNUMBER "crlnumber"
  108. #define ENV_CRL "crl"
  109. #define ENV_PRIVATE_KEY "private_key"
  110. #define ENV_RANDFILE "RANDFILE"
  111. #define ENV_DEFAULT_DAYS "default_days"
  112. #define ENV_DEFAULT_STARTDATE "default_startdate"
  113. #define ENV_DEFAULT_ENDDATE "default_enddate"
  114. #define ENV_DEFAULT_CRL_DAYS "default_crl_days"
  115. #define ENV_DEFAULT_CRL_HOURS "default_crl_hours"
  116. #define ENV_DEFAULT_MD "default_md"
  117. #define ENV_DEFAULT_EMAIL_DN "email_in_dn"
  118. #define ENV_PRESERVE "preserve"
  119. #define ENV_POLICY "policy"
  120. #define ENV_EXTENSIONS "x509_extensions"
  121. #define ENV_CRLEXT "crl_extensions"
  122. #define ENV_MSIE_HACK "msie_hack"
  123. #define ENV_NAMEOPT "name_opt"
  124. #define ENV_CERTOPT "cert_opt"
  125. #define ENV_EXTCOPY "copy_extensions"
  126. #define ENV_UNIQUE_SUBJECT "unique_subject"
  127. #define ENV_DATABASE "database"
  128. /* Additional revocation information types */
  129. #define REV_NONE 0 /* No addditional information */
  130. #define REV_CRL_REASON 1 /* Value is CRL reason code */
  131. #define REV_HOLD 2 /* Value is hold instruction */
  132. #define REV_KEY_COMPROMISE 3 /* Value is cert key compromise time */
  133. #define REV_CA_COMPROMISE 4 /* Value is CA key compromise time */
  134. static const char *ca_usage[]={
  135. "usage: ca args\n",
  136. "\n",
  137. " -verbose - Talk alot while doing things\n",
  138. " -config file - A config file\n",
  139. " -name arg - The particular CA definition to use\n",
  140. " -gencrl - Generate a new CRL\n",
  141. " -crldays days - Days is when the next CRL is due\n",
  142. " -crlhours hours - Hours is when the next CRL is due\n",
  143. " -startdate YYMMDDHHMMSSZ - certificate validity notBefore\n",
  144. " -enddate YYMMDDHHMMSSZ - certificate validity notAfter (overrides -days)\n",
  145. " -days arg - number of days to certify the certificate for\n",
  146. " -md arg - md to use, one of md2, md5, sha or sha1\n",
  147. " -policy arg - The CA 'policy' to support\n",
  148. " -keyfile arg - private key file\n",
  149. " -keyform arg - private key file format (PEM or ENGINE)\n",
  150. " -key arg - key to decode the private key if it is encrypted\n",
  151. " -cert file - The CA certificate\n",
  152. " -selfsign - sign a certificate with the key associated with it\n",
  153. " -in file - The input PEM encoded certificate request(s)\n",
  154. " -out file - Where to put the output file(s)\n",
  155. " -outdir dir - Where to put output certificates\n",
  156. " -infiles .... - The last argument, requests to process\n",
  157. " -spkac file - File contains DN and signed public key and challenge\n",
  158. " -ss_cert file - File contains a self signed cert to sign\n",
  159. " -preserveDN - Don't re-order the DN\n",
  160. " -noemailDN - Don't add the EMAIL field into certificate' subject\n",
  161. " -batch - Don't ask questions\n",
  162. " -msie_hack - msie modifications to handle all those universal strings\n",
  163. " -revoke file - Revoke a certificate (given in file)\n",
  164. " -subj arg - Use arg instead of request's subject\n",
  165. " -utf8 - input characters are UTF8 (default ASCII)\n",
  166. " -multivalue-rdn - enable support for multivalued RDNs\n",
  167. " -extensions .. - Extension section (override value in config file)\n",
  168. " -extfile file - Configuration file with X509v3 extentions to add\n",
  169. " -crlexts .. - CRL extension section (override value in config file)\n",
  170. #ifndef OPENSSL_NO_ENGINE
  171. " -engine e - use engine e, possibly a hardware device.\n",
  172. #endif
  173. " -status serial - Shows certificate status given the serial number\n",
  174. " -updatedb - Updates db for expired certificates\n",
  175. NULL
  176. };
  177. #ifdef EFENCE
  178. extern int EF_PROTECT_FREE;
  179. extern int EF_PROTECT_BELOW;
  180. extern int EF_ALIGNMENT;
  181. #endif
  182. static void lookup_fail(const char *name, const char *tag);
  183. static int certify(X509 **xret, char *infile,EVP_PKEY *pkey,X509 *x509,
  184. const EVP_MD *dgst,STACK_OF(CONF_VALUE) *policy,CA_DB *db,
  185. BIGNUM *serial, char *subj,unsigned long chtype, int multirdn, int email_dn, char *startdate,
  186. char *enddate, long days, int batch, char *ext_sect, CONF *conf,
  187. int verbose, unsigned long certopt, unsigned long nameopt,
  188. int default_op, int ext_copy, int selfsign);
  189. static int certify_cert(X509 **xret, char *infile,EVP_PKEY *pkey,X509 *x509,
  190. const EVP_MD *dgst,STACK_OF(CONF_VALUE) *policy,
  191. CA_DB *db, BIGNUM *serial, char *subj,unsigned long chtype, int multirdn, int email_dn,
  192. char *startdate, char *enddate, long days, int batch,
  193. char *ext_sect, CONF *conf,int verbose, unsigned long certopt,
  194. unsigned long nameopt, int default_op, int ext_copy,
  195. ENGINE *e);
  196. static int certify_spkac(X509 **xret, char *infile,EVP_PKEY *pkey,X509 *x509,
  197. const EVP_MD *dgst,STACK_OF(CONF_VALUE) *policy,
  198. CA_DB *db, BIGNUM *serial,char *subj,unsigned long chtype, int multirdn, int email_dn,
  199. char *startdate, char *enddate, long days, char *ext_sect,
  200. CONF *conf, int verbose, unsigned long certopt,
  201. unsigned long nameopt, int default_op, int ext_copy);
  202. static int fix_data(int nid, int *type);
  203. static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext);
  204. static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,
  205. STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,char *subj,unsigned long chtype, int multirdn,
  206. int email_dn, char *startdate, char *enddate, long days, int batch,
  207. int verbose, X509_REQ *req, char *ext_sect, CONF *conf,
  208. unsigned long certopt, unsigned long nameopt, int default_op,
  209. int ext_copy, int selfsign);
  210. static int do_revoke(X509 *x509, CA_DB *db, int ext, char *extval);
  211. static int get_certificate_status(const char *ser_status, CA_DB *db);
  212. static int do_updatedb(CA_DB *db);
  213. static int check_time_format(const char *str);
  214. char *make_revocation_str(int rev_type, char *rev_arg);
  215. int make_revoked(X509_REVOKED *rev, const char *str);
  216. int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str);
  217. static CONF *conf=NULL;
  218. static CONF *extconf=NULL;
  219. static char *section=NULL;
  220. static int preserve=0;
  221. static int msie_hack=0;
  222. int MAIN(int, char **);
  223. int MAIN(int argc, char **argv)
  224. {
  225. ENGINE *e = NULL;
  226. char *key=NULL,*passargin=NULL;
  227. int create_ser = 0;
  228. int free_key = 0;
  229. int total=0;
  230. int total_done=0;
  231. int badops=0;
  232. int ret=1;
  233. int email_dn=1;
  234. int req=0;
  235. int verbose=0;
  236. int gencrl=0;
  237. int dorevoke=0;
  238. int doupdatedb=0;
  239. long crldays=0;
  240. long crlhours=0;
  241. long crlsec=0;
  242. long errorline= -1;
  243. char *configfile=NULL;
  244. char *md=NULL;
  245. char *policy=NULL;
  246. char *keyfile=NULL;
  247. char *certfile=NULL;
  248. int keyform=FORMAT_PEM;
  249. char *infile=NULL;
  250. char *spkac_file=NULL;
  251. char *ss_cert_file=NULL;
  252. char *ser_status=NULL;
  253. EVP_PKEY *pkey=NULL;
  254. int output_der = 0;
  255. char *outfile=NULL;
  256. char *outdir=NULL;
  257. char *serialfile=NULL;
  258. char *crlnumberfile=NULL;
  259. char *extensions=NULL;
  260. char *extfile=NULL;
  261. char *subj=NULL;
  262. unsigned long chtype = MBSTRING_ASC;
  263. int multirdn = 0;
  264. char *tmp_email_dn=NULL;
  265. char *crl_ext=NULL;
  266. int rev_type = REV_NONE;
  267. char *rev_arg = NULL;
  268. BIGNUM *serial=NULL;
  269. BIGNUM *crlnumber=NULL;
  270. char *startdate=NULL;
  271. char *enddate=NULL;
  272. long days=0;
  273. int batch=0;
  274. int notext=0;
  275. unsigned long nameopt = 0, certopt = 0;
  276. int default_op = 1;
  277. int ext_copy = EXT_COPY_NONE;
  278. int selfsign = 0;
  279. X509 *x509=NULL, *x509p = NULL;
  280. X509 *x=NULL;
  281. BIO *in=NULL,*out=NULL,*Sout=NULL,*Cout=NULL;
  282. char *dbfile=NULL;
  283. CA_DB *db=NULL;
  284. X509_CRL *crl=NULL;
  285. X509_REVOKED *r=NULL;
  286. ASN1_TIME *tmptm;
  287. ASN1_INTEGER *tmpser;
  288. char *f;
  289. const char *p;
  290. char * const *pp;
  291. int i,j;
  292. const EVP_MD *dgst=NULL;
  293. STACK_OF(CONF_VALUE) *attribs=NULL;
  294. STACK_OF(X509) *cert_sk=NULL;
  295. #undef BSIZE
  296. #define BSIZE 256
  297. MS_STATIC char buf[3][BSIZE];
  298. char *randfile=NULL;
  299. #ifndef OPENSSL_NO_ENGINE
  300. char *engine = NULL;
  301. #endif
  302. char *tofree=NULL;
  303. DB_ATTR db_attr;
  304. #ifdef EFENCE
  305. EF_PROTECT_FREE=1;
  306. EF_PROTECT_BELOW=1;
  307. EF_ALIGNMENT=0;
  308. #endif
  309. apps_startup();
  310. conf = NULL;
  311. key = NULL;
  312. section = NULL;
  313. preserve=0;
  314. msie_hack=0;
  315. if (bio_err == NULL)
  316. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  317. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  318. argc--;
  319. argv++;
  320. while (argc >= 1)
  321. {
  322. if (strcmp(*argv,"-verbose") == 0)
  323. verbose=1;
  324. else if (strcmp(*argv,"-config") == 0)
  325. {
  326. if (--argc < 1) goto bad;
  327. configfile= *(++argv);
  328. }
  329. else if (strcmp(*argv,"-name") == 0)
  330. {
  331. if (--argc < 1) goto bad;
  332. section= *(++argv);
  333. }
  334. else if (strcmp(*argv,"-subj") == 0)
  335. {
  336. if (--argc < 1) goto bad;
  337. subj= *(++argv);
  338. /* preserve=1; */
  339. }
  340. else if (strcmp(*argv,"-utf8") == 0)
  341. chtype = MBSTRING_UTF8;
  342. else if (strcmp(*argv,"-create_serial") == 0)
  343. create_ser = 1;
  344. else if (strcmp(*argv,"-multivalue-rdn") == 0)
  345. multirdn=1;
  346. else if (strcmp(*argv,"-startdate") == 0)
  347. {
  348. if (--argc < 1) goto bad;
  349. startdate= *(++argv);
  350. }
  351. else if (strcmp(*argv,"-enddate") == 0)
  352. {
  353. if (--argc < 1) goto bad;
  354. enddate= *(++argv);
  355. }
  356. else if (strcmp(*argv,"-days") == 0)
  357. {
  358. if (--argc < 1) goto bad;
  359. days=atoi(*(++argv));
  360. }
  361. else if (strcmp(*argv,"-md") == 0)
  362. {
  363. if (--argc < 1) goto bad;
  364. md= *(++argv);
  365. }
  366. else if (strcmp(*argv,"-policy") == 0)
  367. {
  368. if (--argc < 1) goto bad;
  369. policy= *(++argv);
  370. }
  371. else if (strcmp(*argv,"-keyfile") == 0)
  372. {
  373. if (--argc < 1) goto bad;
  374. keyfile= *(++argv);
  375. }
  376. else if (strcmp(*argv,"-keyform") == 0)
  377. {
  378. if (--argc < 1) goto bad;
  379. keyform=str2fmt(*(++argv));
  380. }
  381. else if (strcmp(*argv,"-passin") == 0)
  382. {
  383. if (--argc < 1) goto bad;
  384. passargin= *(++argv);
  385. }
  386. else if (strcmp(*argv,"-key") == 0)
  387. {
  388. if (--argc < 1) goto bad;
  389. key= *(++argv);
  390. }
  391. else if (strcmp(*argv,"-cert") == 0)
  392. {
  393. if (--argc < 1) goto bad;
  394. certfile= *(++argv);
  395. }
  396. else if (strcmp(*argv,"-selfsign") == 0)
  397. selfsign=1;
  398. else if (strcmp(*argv,"-in") == 0)
  399. {
  400. if (--argc < 1) goto bad;
  401. infile= *(++argv);
  402. req=1;
  403. }
  404. else if (strcmp(*argv,"-out") == 0)
  405. {
  406. if (--argc < 1) goto bad;
  407. outfile= *(++argv);
  408. }
  409. else if (strcmp(*argv,"-outdir") == 0)
  410. {
  411. if (--argc < 1) goto bad;
  412. outdir= *(++argv);
  413. }
  414. else if (strcmp(*argv,"-notext") == 0)
  415. notext=1;
  416. else if (strcmp(*argv,"-batch") == 0)
  417. batch=1;
  418. else if (strcmp(*argv,"-preserveDN") == 0)
  419. preserve=1;
  420. else if (strcmp(*argv,"-noemailDN") == 0)
  421. email_dn=0;
  422. else if (strcmp(*argv,"-gencrl") == 0)
  423. gencrl=1;
  424. else if (strcmp(*argv,"-msie_hack") == 0)
  425. msie_hack=1;
  426. else if (strcmp(*argv,"-crldays") == 0)
  427. {
  428. if (--argc < 1) goto bad;
  429. crldays= atol(*(++argv));
  430. }
  431. else if (strcmp(*argv,"-crlhours") == 0)
  432. {
  433. if (--argc < 1) goto bad;
  434. crlhours= atol(*(++argv));
  435. }
  436. else if (strcmp(*argv,"-crlsec") == 0)
  437. {
  438. if (--argc < 1) goto bad;
  439. crlsec = atol(*(++argv));
  440. }
  441. else if (strcmp(*argv,"-infiles") == 0)
  442. {
  443. argc--;
  444. argv++;
  445. req=1;
  446. break;
  447. }
  448. else if (strcmp(*argv, "-ss_cert") == 0)
  449. {
  450. if (--argc < 1) goto bad;
  451. ss_cert_file = *(++argv);
  452. req=1;
  453. }
  454. else if (strcmp(*argv, "-spkac") == 0)
  455. {
  456. if (--argc < 1) goto bad;
  457. spkac_file = *(++argv);
  458. req=1;
  459. }
  460. else if (strcmp(*argv,"-revoke") == 0)
  461. {
  462. if (--argc < 1) goto bad;
  463. infile= *(++argv);
  464. dorevoke=1;
  465. }
  466. else if (strcmp(*argv,"-extensions") == 0)
  467. {
  468. if (--argc < 1) goto bad;
  469. extensions= *(++argv);
  470. }
  471. else if (strcmp(*argv,"-extfile") == 0)
  472. {
  473. if (--argc < 1) goto bad;
  474. extfile= *(++argv);
  475. }
  476. else if (strcmp(*argv,"-status") == 0)
  477. {
  478. if (--argc < 1) goto bad;
  479. ser_status= *(++argv);
  480. }
  481. else if (strcmp(*argv,"-updatedb") == 0)
  482. {
  483. doupdatedb=1;
  484. }
  485. else if (strcmp(*argv,"-crlexts") == 0)
  486. {
  487. if (--argc < 1) goto bad;
  488. crl_ext= *(++argv);
  489. }
  490. else if (strcmp(*argv,"-crl_reason") == 0)
  491. {
  492. if (--argc < 1) goto bad;
  493. rev_arg = *(++argv);
  494. rev_type = REV_CRL_REASON;
  495. }
  496. else if (strcmp(*argv,"-crl_hold") == 0)
  497. {
  498. if (--argc < 1) goto bad;
  499. rev_arg = *(++argv);
  500. rev_type = REV_HOLD;
  501. }
  502. else if (strcmp(*argv,"-crl_compromise") == 0)
  503. {
  504. if (--argc < 1) goto bad;
  505. rev_arg = *(++argv);
  506. rev_type = REV_KEY_COMPROMISE;
  507. }
  508. else if (strcmp(*argv,"-crl_CA_compromise") == 0)
  509. {
  510. if (--argc < 1) goto bad;
  511. rev_arg = *(++argv);
  512. rev_type = REV_CA_COMPROMISE;
  513. }
  514. #ifndef OPENSSL_NO_ENGINE
  515. else if (strcmp(*argv,"-engine") == 0)
  516. {
  517. if (--argc < 1) goto bad;
  518. engine= *(++argv);
  519. }
  520. #endif
  521. else
  522. {
  523. bad:
  524. BIO_printf(bio_err,"unknown option %s\n",*argv);
  525. badops=1;
  526. break;
  527. }
  528. argc--;
  529. argv++;
  530. }
  531. if (badops)
  532. {
  533. const char **pp2;
  534. for (pp2=ca_usage; (*pp2 != NULL); pp2++)
  535. BIO_printf(bio_err,"%s",*pp2);
  536. goto err;
  537. }
  538. ERR_load_crypto_strings();
  539. /*****************************************************************/
  540. tofree=NULL;
  541. if (configfile == NULL) configfile = getenv("OPENSSL_CONF");
  542. if (configfile == NULL) configfile = getenv("SSLEAY_CONF");
  543. if (configfile == NULL)
  544. {
  545. const char *s=X509_get_default_cert_area();
  546. size_t len;
  547. #ifdef OPENSSL_SYS_VMS
  548. len = strlen(s)+sizeof(CONFIG_FILE);
  549. tofree=OPENSSL_malloc(len);
  550. strcpy(tofree,s);
  551. #else
  552. len = strlen(s)+sizeof(CONFIG_FILE)+1;
  553. tofree=OPENSSL_malloc(len);
  554. BUF_strlcpy(tofree,s,len);
  555. BUF_strlcat(tofree,"/",len);
  556. #endif
  557. BUF_strlcat(tofree,CONFIG_FILE,len);
  558. configfile=tofree;
  559. }
  560. BIO_printf(bio_err,"Using configuration from %s\n",configfile);
  561. conf = NCONF_new(NULL);
  562. if (NCONF_load(conf,configfile,&errorline) <= 0)
  563. {
  564. if (errorline <= 0)
  565. BIO_printf(bio_err,"error loading the config file '%s'\n",
  566. configfile);
  567. else
  568. BIO_printf(bio_err,"error on line %ld of config file '%s'\n"
  569. ,errorline,configfile);
  570. goto err;
  571. }
  572. if(tofree)
  573. {
  574. OPENSSL_free(tofree);
  575. tofree = NULL;
  576. }
  577. if (!load_config(bio_err, conf))
  578. goto err;
  579. #ifndef OPENSSL_NO_ENGINE
  580. e = setup_engine(bio_err, engine, 0);
  581. #endif
  582. /* Lets get the config section we are using */
  583. if (section == NULL)
  584. {
  585. section=NCONF_get_string(conf,BASE_SECTION,ENV_DEFAULT_CA);
  586. if (section == NULL)
  587. {
  588. lookup_fail(BASE_SECTION,ENV_DEFAULT_CA);
  589. goto err;
  590. }
  591. }
  592. if (conf != NULL)
  593. {
  594. p=NCONF_get_string(conf,NULL,"oid_file");
  595. if (p == NULL)
  596. ERR_clear_error();
  597. if (p != NULL)
  598. {
  599. BIO *oid_bio;
  600. oid_bio=BIO_new_file(p,"r");
  601. if (oid_bio == NULL)
  602. {
  603. /*
  604. BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
  605. ERR_print_errors(bio_err);
  606. */
  607. ERR_clear_error();
  608. }
  609. else
  610. {
  611. OBJ_create_objects(oid_bio);
  612. BIO_free(oid_bio);
  613. }
  614. }
  615. if (!add_oid_section(bio_err,conf))
  616. {
  617. ERR_print_errors(bio_err);
  618. goto err;
  619. }
  620. }
  621. randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE");
  622. if (randfile == NULL)
  623. ERR_clear_error();
  624. app_RAND_load_file(randfile, bio_err, 0);
  625. f = NCONF_get_string(conf, section, STRING_MASK);
  626. if (!f)
  627. ERR_clear_error();
  628. if(f && !ASN1_STRING_set_default_mask_asc(f)) {
  629. BIO_printf(bio_err, "Invalid global string mask setting %s\n", f);
  630. goto err;
  631. }
  632. if (chtype != MBSTRING_UTF8){
  633. f = NCONF_get_string(conf, section, UTF8_IN);
  634. if (!f)
  635. ERR_clear_error();
  636. else if (!strcmp(f, "yes"))
  637. chtype = MBSTRING_UTF8;
  638. }
  639. db_attr.unique_subject = 1;
  640. p = NCONF_get_string(conf, section, ENV_UNIQUE_SUBJECT);
  641. if (p)
  642. {
  643. #ifdef RL_DEBUG
  644. BIO_printf(bio_err, "DEBUG: unique_subject = \"%s\"\n", p);
  645. #endif
  646. db_attr.unique_subject = parse_yesno(p,1);
  647. }
  648. else
  649. ERR_clear_error();
  650. #ifdef RL_DEBUG
  651. if (!p)
  652. BIO_printf(bio_err, "DEBUG: unique_subject undefined\n", p);
  653. #endif
  654. #ifdef RL_DEBUG
  655. BIO_printf(bio_err, "DEBUG: configured unique_subject is %d\n",
  656. db_attr.unique_subject);
  657. #endif
  658. in=BIO_new(BIO_s_file());
  659. out=BIO_new(BIO_s_file());
  660. Sout=BIO_new(BIO_s_file());
  661. Cout=BIO_new(BIO_s_file());
  662. if ((in == NULL) || (out == NULL) || (Sout == NULL) || (Cout == NULL))
  663. {
  664. ERR_print_errors(bio_err);
  665. goto err;
  666. }
  667. /*****************************************************************/
  668. /* report status of cert with serial number given on command line */
  669. if (ser_status)
  670. {
  671. if ((dbfile=NCONF_get_string(conf,section,ENV_DATABASE)) == NULL)
  672. {
  673. lookup_fail(section,ENV_DATABASE);
  674. goto err;
  675. }
  676. db = load_index(dbfile,&db_attr);
  677. if (db == NULL) goto err;
  678. if (!index_index(db)) goto err;
  679. if (get_certificate_status(ser_status,db) != 1)
  680. BIO_printf(bio_err,"Error verifying serial %s!\n",
  681. ser_status);
  682. goto err;
  683. }
  684. /*****************************************************************/
  685. /* we definitely need a private key, so let's get it */
  686. if ((keyfile == NULL) && ((keyfile=NCONF_get_string(conf,
  687. section,ENV_PRIVATE_KEY)) == NULL))
  688. {
  689. lookup_fail(section,ENV_PRIVATE_KEY);
  690. goto err;
  691. }
  692. if (!key)
  693. {
  694. free_key = 1;
  695. if (!app_passwd(bio_err, passargin, NULL, &key, NULL))
  696. {
  697. BIO_printf(bio_err,"Error getting password\n");
  698. goto err;
  699. }
  700. }
  701. pkey = load_key(bio_err, keyfile, keyform, 0, key, e,
  702. "CA private key");
  703. if (key) OPENSSL_cleanse(key,strlen(key));
  704. if (pkey == NULL)
  705. {
  706. /* load_key() has already printed an appropriate message */
  707. goto err;
  708. }
  709. /*****************************************************************/
  710. /* we need a certificate */
  711. if (!selfsign || spkac_file || ss_cert_file || gencrl)
  712. {
  713. if ((certfile == NULL)
  714. && ((certfile=NCONF_get_string(conf,
  715. section,ENV_CERTIFICATE)) == NULL))
  716. {
  717. lookup_fail(section,ENV_CERTIFICATE);
  718. goto err;
  719. }
  720. x509=load_cert(bio_err, certfile, FORMAT_PEM, NULL, e,
  721. "CA certificate");
  722. if (x509 == NULL)
  723. goto err;
  724. if (!X509_check_private_key(x509,pkey))
  725. {
  726. BIO_printf(bio_err,"CA certificate and CA private key do not match\n");
  727. goto err;
  728. }
  729. }
  730. if (!selfsign) x509p = x509;
  731. f=NCONF_get_string(conf,BASE_SECTION,ENV_PRESERVE);
  732. if (f == NULL)
  733. ERR_clear_error();
  734. if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
  735. preserve=1;
  736. f=NCONF_get_string(conf,BASE_SECTION,ENV_MSIE_HACK);
  737. if (f == NULL)
  738. ERR_clear_error();
  739. if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
  740. msie_hack=1;
  741. f=NCONF_get_string(conf,section,ENV_NAMEOPT);
  742. if (f)
  743. {
  744. if (!set_name_ex(&nameopt, f))
  745. {
  746. BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f);
  747. goto err;
  748. }
  749. default_op = 0;
  750. }
  751. else
  752. ERR_clear_error();
  753. f=NCONF_get_string(conf,section,ENV_CERTOPT);
  754. if (f)
  755. {
  756. if (!set_cert_ex(&certopt, f))
  757. {
  758. BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f);
  759. goto err;
  760. }
  761. default_op = 0;
  762. }
  763. else
  764. ERR_clear_error();
  765. f=NCONF_get_string(conf,section,ENV_EXTCOPY);
  766. if (f)
  767. {
  768. if (!set_ext_copy(&ext_copy, f))
  769. {
  770. BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f);
  771. goto err;
  772. }
  773. }
  774. else
  775. ERR_clear_error();
  776. /*****************************************************************/
  777. /* lookup where to write new certificates */
  778. if ((outdir == NULL) && (req))
  779. {
  780. if ((outdir=NCONF_get_string(conf,section,ENV_NEW_CERTS_DIR))
  781. == NULL)
  782. {
  783. BIO_printf(bio_err,"there needs to be defined a directory for new certificate to be placed in\n");
  784. goto err;
  785. }
  786. #ifndef OPENSSL_SYS_VMS
  787. /* outdir is a directory spec, but access() for VMS demands a
  788. filename. In any case, stat(), below, will catch the problem
  789. if outdir is not a directory spec, and the fopen() or open()
  790. will catch an error if there is no write access.
  791. Presumably, this problem could also be solved by using the DEC
  792. C routines to convert the directory syntax to Unixly, and give
  793. that to access(). However, time's too short to do that just
  794. now.
  795. */
  796. if (access(outdir,R_OK|W_OK|X_OK) != 0)
  797. {
  798. BIO_printf(bio_err,"I am unable to access the %s directory\n",outdir);
  799. perror(outdir);
  800. goto err;
  801. }
  802. if (app_isdir(outdir)<=0)
  803. {
  804. BIO_printf(bio_err,"%s need to be a directory\n",outdir);
  805. perror(outdir);
  806. goto err;
  807. }
  808. #endif
  809. }
  810. /*****************************************************************/
  811. /* we need to load the database file */
  812. if ((dbfile=NCONF_get_string(conf,section,ENV_DATABASE)) == NULL)
  813. {
  814. lookup_fail(section,ENV_DATABASE);
  815. goto err;
  816. }
  817. db = load_index(dbfile, &db_attr);
  818. if (db == NULL) goto err;
  819. /* Lets check some fields */
  820. for (i=0; i<sk_PSTRING_num(db->db->data); i++)
  821. {
  822. pp=sk_PSTRING_value(db->db->data,i);
  823. if ((pp[DB_type][0] != DB_TYPE_REV) &&
  824. (pp[DB_rev_date][0] != '\0'))
  825. {
  826. BIO_printf(bio_err,"entry %d: not revoked yet, but has a revocation date\n",i+1);
  827. goto err;
  828. }
  829. if ((pp[DB_type][0] == DB_TYPE_REV) &&
  830. !make_revoked(NULL, pp[DB_rev_date]))
  831. {
  832. BIO_printf(bio_err," in entry %d\n", i+1);
  833. goto err;
  834. }
  835. if (!check_time_format((char *)pp[DB_exp_date]))
  836. {
  837. BIO_printf(bio_err,"entry %d: invalid expiry date\n",i+1);
  838. goto err;
  839. }
  840. p=pp[DB_serial];
  841. j=strlen(p);
  842. if (*p == '-')
  843. {
  844. p++;
  845. j--;
  846. }
  847. if ((j&1) || (j < 2))
  848. {
  849. BIO_printf(bio_err,"entry %d: bad serial number length (%d)\n",i+1,j);
  850. goto err;
  851. }
  852. while (*p)
  853. {
  854. if (!( ((*p >= '0') && (*p <= '9')) ||
  855. ((*p >= 'A') && (*p <= 'F')) ||
  856. ((*p >= 'a') && (*p <= 'f'))) )
  857. {
  858. BIO_printf(bio_err,"entry %d: bad serial number characters, char pos %ld, char is '%c'\n",i+1,(long)(p-pp[DB_serial]),*p);
  859. goto err;
  860. }
  861. p++;
  862. }
  863. }
  864. if (verbose)
  865. {
  866. BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT); /* cannot fail */
  867. #ifdef OPENSSL_SYS_VMS
  868. {
  869. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  870. out = BIO_push(tmpbio, out);
  871. }
  872. #endif
  873. TXT_DB_write(out,db->db);
  874. BIO_printf(bio_err,"%d entries loaded from the database\n",
  875. sk_PSTRING_num(db->db->data));
  876. BIO_printf(bio_err,"generating index\n");
  877. }
  878. if (!index_index(db)) goto err;
  879. /*****************************************************************/
  880. /* Update the db file for expired certificates */
  881. if (doupdatedb)
  882. {
  883. if (verbose)
  884. BIO_printf(bio_err, "Updating %s ...\n",
  885. dbfile);
  886. i = do_updatedb(db);
  887. if (i == -1)
  888. {
  889. BIO_printf(bio_err,"Malloc failure\n");
  890. goto err;
  891. }
  892. else if (i == 0)
  893. {
  894. if (verbose) BIO_printf(bio_err,
  895. "No entries found to mark expired\n");
  896. }
  897. else
  898. {
  899. if (!save_index(dbfile,"new",db)) goto err;
  900. if (!rotate_index(dbfile,"new","old")) goto err;
  901. if (verbose) BIO_printf(bio_err,
  902. "Done. %d entries marked as expired\n",i);
  903. }
  904. }
  905. /*****************************************************************/
  906. /* Read extentions config file */
  907. if (extfile)
  908. {
  909. extconf = NCONF_new(NULL);
  910. if (NCONF_load(extconf,extfile,&errorline) <= 0)
  911. {
  912. if (errorline <= 0)
  913. BIO_printf(bio_err, "ERROR: loading the config file '%s'\n",
  914. extfile);
  915. else
  916. BIO_printf(bio_err, "ERROR: on line %ld of config file '%s'\n",
  917. errorline,extfile);
  918. ret = 1;
  919. goto err;
  920. }
  921. if (verbose)
  922. BIO_printf(bio_err, "Successfully loaded extensions file %s\n", extfile);
  923. /* We can have sections in the ext file */
  924. if (!extensions && !(extensions = NCONF_get_string(extconf, "default", "extensions")))
  925. extensions = "default";
  926. }
  927. /*****************************************************************/
  928. if (req || gencrl)
  929. {
  930. if (outfile != NULL)
  931. {
  932. if (BIO_write_filename(Sout,outfile) <= 0)
  933. {
  934. perror(outfile);
  935. goto err;
  936. }
  937. }
  938. else
  939. {
  940. BIO_set_fp(Sout,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
  941. #ifdef OPENSSL_SYS_VMS
  942. {
  943. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  944. Sout = BIO_push(tmpbio, Sout);
  945. }
  946. #endif
  947. }
  948. }
  949. if ((md == NULL) && ((md=NCONF_get_string(conf,
  950. section,ENV_DEFAULT_MD)) == NULL))
  951. {
  952. lookup_fail(section,ENV_DEFAULT_MD);
  953. goto err;
  954. }
  955. if (!strcmp(md, "default"))
  956. {
  957. int def_nid;
  958. if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
  959. {
  960. BIO_puts(bio_err,"no default digest\n");
  961. goto err;
  962. }
  963. md = (char *)OBJ_nid2sn(def_nid);
  964. }
  965. if ((dgst=EVP_get_digestbyname(md)) == NULL)
  966. {
  967. BIO_printf(bio_err,"%s is an unsupported message digest type\n",md);
  968. goto err;
  969. }
  970. if (req)
  971. {
  972. if ((email_dn == 1) && ((tmp_email_dn=NCONF_get_string(conf,
  973. section,ENV_DEFAULT_EMAIL_DN)) != NULL ))
  974. {
  975. if(strcmp(tmp_email_dn,"no") == 0)
  976. email_dn=0;
  977. }
  978. if (verbose)
  979. BIO_printf(bio_err,"message digest is %s\n",
  980. OBJ_nid2ln(dgst->type));
  981. if ((policy == NULL) && ((policy=NCONF_get_string(conf,
  982. section,ENV_POLICY)) == NULL))
  983. {
  984. lookup_fail(section,ENV_POLICY);
  985. goto err;
  986. }
  987. if (verbose)
  988. BIO_printf(bio_err,"policy is %s\n",policy);
  989. if ((serialfile=NCONF_get_string(conf,section,ENV_SERIAL))
  990. == NULL)
  991. {
  992. lookup_fail(section,ENV_SERIAL);
  993. goto err;
  994. }
  995. if (!extconf)
  996. {
  997. /* no '-extfile' option, so we look for extensions
  998. * in the main configuration file */
  999. if (!extensions)
  1000. {
  1001. extensions=NCONF_get_string(conf,section,
  1002. ENV_EXTENSIONS);
  1003. if (!extensions)
  1004. ERR_clear_error();
  1005. }
  1006. if (extensions)
  1007. {
  1008. /* Check syntax of file */
  1009. X509V3_CTX ctx;
  1010. X509V3_set_ctx_test(&ctx);
  1011. X509V3_set_nconf(&ctx, conf);
  1012. if (!X509V3_EXT_add_nconf(conf, &ctx, extensions,
  1013. NULL))
  1014. {
  1015. BIO_printf(bio_err,
  1016. "Error Loading extension section %s\n",
  1017. extensions);
  1018. ret = 1;
  1019. goto err;
  1020. }
  1021. }
  1022. }
  1023. if (startdate == NULL)
  1024. {
  1025. startdate=NCONF_get_string(conf,section,
  1026. ENV_DEFAULT_STARTDATE);
  1027. if (startdate == NULL)
  1028. ERR_clear_error();
  1029. }
  1030. if (startdate && !ASN1_UTCTIME_set_string(NULL,startdate))
  1031. {
  1032. BIO_printf(bio_err,"start date is invalid, it should be YYMMDDHHMMSSZ\n");
  1033. goto err;
  1034. }
  1035. if (startdate == NULL) startdate="today";
  1036. if (enddate == NULL)
  1037. {
  1038. enddate=NCONF_get_string(conf,section,
  1039. ENV_DEFAULT_ENDDATE);
  1040. if (enddate == NULL)
  1041. ERR_clear_error();
  1042. }
  1043. if (enddate && !ASN1_UTCTIME_set_string(NULL,enddate))
  1044. {
  1045. BIO_printf(bio_err,"end date is invalid, it should be YYMMDDHHMMSSZ\n");
  1046. goto err;
  1047. }
  1048. if (days == 0)
  1049. {
  1050. if(!NCONF_get_number(conf,section, ENV_DEFAULT_DAYS, &days))
  1051. days = 0;
  1052. }
  1053. if (!enddate && (days == 0))
  1054. {
  1055. BIO_printf(bio_err,"cannot lookup how many days to certify for\n");
  1056. goto err;
  1057. }
  1058. if ((serial=load_serial(serialfile, create_ser, NULL)) == NULL)
  1059. {
  1060. BIO_printf(bio_err,"error while loading serial number\n");
  1061. goto err;
  1062. }
  1063. if (verbose)
  1064. {
  1065. if (BN_is_zero(serial))
  1066. BIO_printf(bio_err,"next serial number is 00\n");
  1067. else
  1068. {
  1069. if ((f=BN_bn2hex(serial)) == NULL) goto err;
  1070. BIO_printf(bio_err,"next serial number is %s\n",f);
  1071. OPENSSL_free(f);
  1072. }
  1073. }
  1074. if ((attribs=NCONF_get_section(conf,policy)) == NULL)
  1075. {
  1076. BIO_printf(bio_err,"unable to find 'section' for %s\n",policy);
  1077. goto err;
  1078. }
  1079. if ((cert_sk=sk_X509_new_null()) == NULL)
  1080. {
  1081. BIO_printf(bio_err,"Memory allocation failure\n");
  1082. goto err;
  1083. }
  1084. if (spkac_file != NULL)
  1085. {
  1086. total++;
  1087. j=certify_spkac(&x,spkac_file,pkey,x509,dgst,attribs,db,
  1088. serial,subj,chtype,multirdn,email_dn,startdate,enddate,days,extensions,
  1089. conf,verbose,certopt,nameopt,default_op,ext_copy);
  1090. if (j < 0) goto err;
  1091. if (j > 0)
  1092. {
  1093. total_done++;
  1094. BIO_printf(bio_err,"\n");
  1095. if (!BN_add_word(serial,1)) goto err;
  1096. if (!sk_X509_push(cert_sk,x))
  1097. {
  1098. BIO_printf(bio_err,"Memory allocation failure\n");
  1099. goto err;
  1100. }
  1101. if (outfile)
  1102. {
  1103. output_der = 1;
  1104. batch = 1;
  1105. }
  1106. }
  1107. }
  1108. if (ss_cert_file != NULL)
  1109. {
  1110. total++;
  1111. j=certify_cert(&x,ss_cert_file,pkey,x509,dgst,attribs,
  1112. db,serial,subj,chtype,multirdn,email_dn,startdate,enddate,days,batch,
  1113. extensions,conf,verbose, certopt, nameopt,
  1114. default_op, ext_copy, e);
  1115. if (j < 0) goto err;
  1116. if (j > 0)
  1117. {
  1118. total_done++;
  1119. BIO_printf(bio_err,"\n");
  1120. if (!BN_add_word(serial,1)) goto err;
  1121. if (!sk_X509_push(cert_sk,x))
  1122. {
  1123. BIO_printf(bio_err,"Memory allocation failure\n");
  1124. goto err;
  1125. }
  1126. }
  1127. }
  1128. if (infile != NULL)
  1129. {
  1130. total++;
  1131. j=certify(&x,infile,pkey,x509p,dgst,attribs,db,
  1132. serial,subj,chtype,multirdn,email_dn,startdate,enddate,days,batch,
  1133. extensions,conf,verbose, certopt, nameopt,
  1134. default_op, ext_copy, selfsign);
  1135. if (j < 0) goto err;
  1136. if (j > 0)
  1137. {
  1138. total_done++;
  1139. BIO_printf(bio_err,"\n");
  1140. if (!BN_add_word(serial,1)) goto err;
  1141. if (!sk_X509_push(cert_sk,x))
  1142. {
  1143. BIO_printf(bio_err,"Memory allocation failure\n");
  1144. goto err;
  1145. }
  1146. }
  1147. }
  1148. for (i=0; i<argc; i++)
  1149. {
  1150. total++;
  1151. j=certify(&x,argv[i],pkey,x509p,dgst,attribs,db,
  1152. serial,subj,chtype,multirdn,email_dn,startdate,enddate,days,batch,
  1153. extensions,conf,verbose, certopt, nameopt,
  1154. default_op, ext_copy, selfsign);
  1155. if (j < 0) goto err;
  1156. if (j > 0)
  1157. {
  1158. total_done++;
  1159. BIO_printf(bio_err,"\n");
  1160. if (!BN_add_word(serial,1)) goto err;
  1161. if (!sk_X509_push(cert_sk,x))
  1162. {
  1163. BIO_printf(bio_err,"Memory allocation failure\n");
  1164. goto err;
  1165. }
  1166. }
  1167. }
  1168. /* we have a stack of newly certified certificates
  1169. * and a data base and serial number that need
  1170. * updating */
  1171. if (sk_X509_num(cert_sk) > 0)
  1172. {
  1173. if (!batch)
  1174. {
  1175. BIO_printf(bio_err,"\n%d out of %d certificate requests certified, commit? [y/n]",total_done,total);
  1176. (void)BIO_flush(bio_err);
  1177. buf[0][0]='\0';
  1178. fgets(buf[0],10,stdin);
  1179. if ((buf[0][0] != 'y') && (buf[0][0] != 'Y'))
  1180. {
  1181. BIO_printf(bio_err,"CERTIFICATION CANCELED\n");
  1182. ret=0;
  1183. goto err;
  1184. }
  1185. }
  1186. BIO_printf(bio_err,"Write out database with %d new entries\n",sk_X509_num(cert_sk));
  1187. if (!save_serial(serialfile,"new",serial,NULL)) goto err;
  1188. if (!save_index(dbfile, "new", db)) goto err;
  1189. }
  1190. if (verbose)
  1191. BIO_printf(bio_err,"writing new certificates\n");
  1192. for (i=0; i<sk_X509_num(cert_sk); i++)
  1193. {
  1194. int k;
  1195. char *n;
  1196. x=sk_X509_value(cert_sk,i);
  1197. j=x->cert_info->serialNumber->length;
  1198. p=(const char *)x->cert_info->serialNumber->data;
  1199. if(strlen(outdir) >= (size_t)(j ? BSIZE-j*2-6 : BSIZE-8))
  1200. {
  1201. BIO_printf(bio_err,"certificate file name too long\n");
  1202. goto err;
  1203. }
  1204. strcpy(buf[2],outdir);
  1205. #ifndef OPENSSL_SYS_VMS
  1206. BUF_strlcat(buf[2],"/",sizeof(buf[2]));
  1207. #endif
  1208. n=(char *)&(buf[2][strlen(buf[2])]);
  1209. if (j > 0)
  1210. {
  1211. for (k=0; k<j; k++)
  1212. {
  1213. if (n >= &(buf[2][sizeof(buf[2])]))
  1214. break;
  1215. BIO_snprintf(n,
  1216. &buf[2][0] + sizeof(buf[2]) - n,
  1217. "%02X",(unsigned char)*(p++));
  1218. n+=2;
  1219. }
  1220. }
  1221. else
  1222. {
  1223. *(n++)='0';
  1224. *(n++)='0';
  1225. }
  1226. *(n++)='.'; *(n++)='p'; *(n++)='e'; *(n++)='m';
  1227. *n='\0';
  1228. if (verbose)
  1229. BIO_printf(bio_err,"writing %s\n",buf[2]);
  1230. if (BIO_write_filename(Cout,buf[2]) <= 0)
  1231. {
  1232. perror(buf[2]);
  1233. goto err;
  1234. }
  1235. write_new_certificate(Cout,x, 0, notext);
  1236. write_new_certificate(Sout,x, output_der, notext);
  1237. }
  1238. if (sk_X509_num(cert_sk))
  1239. {
  1240. /* Rename the database and the serial file */
  1241. if (!rotate_serial(serialfile,"new","old")) goto err;
  1242. if (!rotate_index(dbfile,"new","old")) goto err;
  1243. BIO_printf(bio_err,"Data Base Updated\n");
  1244. }
  1245. }
  1246. /*****************************************************************/
  1247. if (gencrl)
  1248. {
  1249. int crl_v2 = 0;
  1250. if (!crl_ext)
  1251. {
  1252. crl_ext=NCONF_get_string(conf,section,ENV_CRLEXT);
  1253. if (!crl_ext)
  1254. ERR_clear_error();
  1255. }
  1256. if (crl_ext)
  1257. {
  1258. /* Check syntax of file */
  1259. X509V3_CTX ctx;
  1260. X509V3_set_ctx_test(&ctx);
  1261. X509V3_set_nconf(&ctx, conf);
  1262. if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL))
  1263. {
  1264. BIO_printf(bio_err,
  1265. "Error Loading CRL extension section %s\n",
  1266. crl_ext);
  1267. ret = 1;
  1268. goto err;
  1269. }
  1270. }
  1271. if ((crlnumberfile=NCONF_get_string(conf,section,ENV_CRLNUMBER))
  1272. != NULL)
  1273. if ((crlnumber=load_serial(crlnumberfile,0,NULL)) == NULL)
  1274. {
  1275. BIO_printf(bio_err,"error while loading CRL number\n");
  1276. goto err;
  1277. }
  1278. if (!crldays && !crlhours && !crlsec)
  1279. {
  1280. if (!NCONF_get_number(conf,section,
  1281. ENV_DEFAULT_CRL_DAYS, &crldays))
  1282. crldays = 0;
  1283. if (!NCONF_get_number(conf,section,
  1284. ENV_DEFAULT_CRL_HOURS, &crlhours))
  1285. crlhours = 0;
  1286. }
  1287. if ((crldays == 0) && (crlhours == 0) && (crlsec == 0))
  1288. {
  1289. BIO_printf(bio_err,"cannot lookup how long until the next CRL is issued\n");
  1290. goto err;
  1291. }
  1292. if (verbose) BIO_printf(bio_err,"making CRL\n");
  1293. if ((crl=X509_CRL_new()) == NULL) goto err;
  1294. if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509))) goto err;
  1295. tmptm = ASN1_TIME_new();
  1296. if (!tmptm) goto err;
  1297. X509_gmtime_adj(tmptm,0);
  1298. X509_CRL_set_lastUpdate(crl, tmptm);
  1299. X509_time_adj_ex(tmptm, crldays, crlhours*60*60 + crlsec, NULL);
  1300. X509_CRL_set_nextUpdate(crl, tmptm);
  1301. ASN1_TIME_free(tmptm);
  1302. for (i=0; i<sk_PSTRING_num(db->db->data); i++)
  1303. {
  1304. pp=sk_PSTRING_value(db->db->data,i);
  1305. if (pp[DB_type][0] == DB_TYPE_REV)
  1306. {
  1307. if ((r=X509_REVOKED_new()) == NULL) goto err;
  1308. j = make_revoked(r, pp[DB_rev_date]);
  1309. if (!j) goto err;
  1310. if (j == 2) crl_v2 = 1;
  1311. if (!BN_hex2bn(&serial, pp[DB_serial]))
  1312. goto err;
  1313. tmpser = BN_to_ASN1_INTEGER(serial, NULL);
  1314. BN_free(serial);
  1315. serial = NULL;
  1316. if (!tmpser)
  1317. goto err;
  1318. X509_REVOKED_set_serialNumber(r, tmpser);
  1319. ASN1_INTEGER_free(tmpser);
  1320. X509_CRL_add0_revoked(crl,r);
  1321. }
  1322. }
  1323. /* sort the data so it will be written in serial
  1324. * number order */
  1325. X509_CRL_sort(crl);
  1326. /* we now have a CRL */
  1327. if (verbose) BIO_printf(bio_err,"signing CRL\n");
  1328. /* Add any extensions asked for */
  1329. if (crl_ext || crlnumberfile != NULL)
  1330. {
  1331. X509V3_CTX crlctx;
  1332. X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
  1333. X509V3_set_nconf(&crlctx, conf);
  1334. if (crl_ext)
  1335. if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx,
  1336. crl_ext, crl)) goto err;
  1337. if (crlnumberfile != NULL)
  1338. {
  1339. tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
  1340. if (!tmpser) goto err;
  1341. X509_CRL_add1_ext_i2d(crl,NID_crl_number,tmpser,0,0);
  1342. ASN1_INTEGER_free(tmpser);
  1343. crl_v2 = 1;
  1344. if (!BN_add_word(crlnumber,1)) goto err;
  1345. }
  1346. }
  1347. if (crl_ext || crl_v2)
  1348. {
  1349. if (!X509_CRL_set_version(crl, 1))
  1350. goto err; /* version 2 CRL */
  1351. }
  1352. if (crlnumberfile != NULL) /* we have a CRL number that need updating */
  1353. if (!save_serial(crlnumberfile,"new",crlnumber,NULL)) goto err;
  1354. if (crlnumber)
  1355. {
  1356. BN_free(crlnumber);
  1357. crlnumber = NULL;
  1358. }
  1359. if (!X509_CRL_sign(crl,pkey,dgst)) goto err;
  1360. PEM_write_bio_X509_CRL(Sout,crl);
  1361. if (crlnumberfile != NULL) /* Rename the crlnumber file */
  1362. if (!rotate_serial(crlnumberfile,"new","old")) goto err;
  1363. }
  1364. /*****************************************************************/
  1365. if (dorevoke)
  1366. {
  1367. if (infile == NULL)
  1368. {
  1369. BIO_printf(bio_err,"no input files\n");
  1370. goto err;
  1371. }
  1372. else
  1373. {
  1374. X509 *revcert;
  1375. revcert=load_cert(bio_err, infile, FORMAT_PEM,
  1376. NULL, e, infile);
  1377. if (revcert == NULL)
  1378. goto err;
  1379. j=do_revoke(revcert,db, rev_type, rev_arg);
  1380. if (j <= 0) goto err;
  1381. X509_free(revcert);
  1382. if (!save_index(dbfile, "new", db)) goto err;
  1383. if (!rotate_index(dbfile, "new", "old")) goto err;
  1384. BIO_printf(bio_err,"Data Base Updated\n");
  1385. }
  1386. }
  1387. /*****************************************************************/
  1388. ret=0;
  1389. err:
  1390. if(tofree)
  1391. OPENSSL_free(tofree);
  1392. BIO_free_all(Cout);
  1393. BIO_free_all(Sout);
  1394. BIO_free_all(out);
  1395. BIO_free_all(in);
  1396. if (cert_sk)
  1397. sk_X509_pop_free(cert_sk,X509_free);
  1398. if (ret) ERR_print_errors(bio_err);
  1399. app_RAND_write_file(randfile, bio_err);
  1400. if (free_key && key)
  1401. OPENSSL_free(key);
  1402. BN_free(serial);
  1403. BN_free(crlnumber);
  1404. free_index(db);
  1405. EVP_PKEY_free(pkey);
  1406. if (x509) X509_free(x509);
  1407. X509_CRL_free(crl);
  1408. NCONF_free(conf);
  1409. NCONF_free(extconf);
  1410. OBJ_cleanup();
  1411. apps_shutdown();
  1412. OPENSSL_EXIT(ret);
  1413. }
  1414. static void lookup_fail(const char *name, const char *tag)
  1415. {
  1416. BIO_printf(bio_err,"variable lookup failed for %s::%s\n",name,tag);
  1417. }
  1418. static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
  1419. const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1420. BIGNUM *serial, char *subj,unsigned long chtype, int multirdn, int email_dn, char *startdate, char *enddate,
  1421. long days, int batch, char *ext_sect, CONF *lconf, int verbose,
  1422. unsigned long certopt, unsigned long nameopt, int default_op,
  1423. int ext_copy, int selfsign)
  1424. {
  1425. X509_REQ *req=NULL;
  1426. BIO *in=NULL;
  1427. EVP_PKEY *pktmp=NULL;
  1428. int ok= -1,i;
  1429. in=BIO_new(BIO_s_file());
  1430. if (BIO_read_filename(in,infile) <= 0)
  1431. {
  1432. perror(infile);
  1433. goto err;
  1434. }
  1435. if ((req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL)) == NULL)
  1436. {
  1437. BIO_printf(bio_err,"Error reading certificate request in %s\n",
  1438. infile);
  1439. goto err;
  1440. }
  1441. if (verbose)
  1442. X509_REQ_print(bio_err,req);
  1443. BIO_printf(bio_err,"Check that the request matches the signature\n");
  1444. if (selfsign && !X509_REQ_check_private_key(req,pkey))
  1445. {
  1446. BIO_printf(bio_err,"Certificate request and CA private key do not match\n");
  1447. ok=0;
  1448. goto err;
  1449. }
  1450. if ((pktmp=X509_REQ_get_pubkey(req)) == NULL)
  1451. {
  1452. BIO_printf(bio_err,"error unpacking public key\n");
  1453. goto err;
  1454. }
  1455. i=X509_REQ_verify(req,pktmp);
  1456. EVP_PKEY_free(pktmp);
  1457. if (i < 0)
  1458. {
  1459. ok=0;
  1460. BIO_printf(bio_err,"Signature verification problems....\n");
  1461. goto err;
  1462. }
  1463. if (i == 0)
  1464. {
  1465. ok=0;
  1466. BIO_printf(bio_err,"Signature did not match the certificate request\n");
  1467. goto err;
  1468. }
  1469. else
  1470. BIO_printf(bio_err,"Signature ok\n");
  1471. ok=do_body(xret,pkey,x509,dgst,policy,db,serial,subj,chtype,multirdn, email_dn,
  1472. startdate,enddate,days,batch,verbose,req,ext_sect,lconf,
  1473. certopt, nameopt, default_op, ext_copy, selfsign);
  1474. err:
  1475. if (req != NULL) X509_REQ_free(req);
  1476. if (in != NULL) BIO_free(in);
  1477. return(ok);
  1478. }
  1479. static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
  1480. const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1481. BIGNUM *serial, char *subj, unsigned long chtype, int multirdn, int email_dn, char *startdate, char *enddate,
  1482. long days, int batch, char *ext_sect, CONF *lconf, int verbose,
  1483. unsigned long certopt, unsigned long nameopt, int default_op,
  1484. int ext_copy, ENGINE *e)
  1485. {
  1486. X509 *req=NULL;
  1487. X509_REQ *rreq=NULL;
  1488. EVP_PKEY *pktmp=NULL;
  1489. int ok= -1,i;
  1490. if ((req=load_cert(bio_err, infile, FORMAT_PEM, NULL, e, infile)) == NULL)
  1491. goto err;
  1492. if (verbose)
  1493. X509_print(bio_err,req);
  1494. BIO_printf(bio_err,"Check that the request matches the signature\n");
  1495. if ((pktmp=X509_get_pubkey(req)) == NULL)
  1496. {
  1497. BIO_printf(bio_err,"error unpacking public key\n");
  1498. goto err;
  1499. }
  1500. i=X509_verify(req,pktmp);
  1501. EVP_PKEY_free(pktmp);
  1502. if (i < 0)
  1503. {
  1504. ok=0;
  1505. BIO_printf(bio_err,"Signature verification problems....\n");
  1506. goto err;
  1507. }
  1508. if (i == 0)
  1509. {
  1510. ok=0;
  1511. BIO_printf(bio_err,"Signature did not match the certificate\n");
  1512. goto err;
  1513. }
  1514. else
  1515. BIO_printf(bio_err,"Signature ok\n");
  1516. if ((rreq=X509_to_X509_REQ(req,NULL,EVP_md5())) == NULL)
  1517. goto err;
  1518. ok=do_body(xret,pkey,x509,dgst,policy,db,serial,subj,chtype,multirdn,email_dn,startdate,enddate,
  1519. days,batch,verbose,rreq,ext_sect,lconf, certopt, nameopt, default_op,
  1520. ext_copy, 0);
  1521. err:
  1522. if (rreq != NULL) X509_REQ_free(rreq);
  1523. if (req != NULL) X509_free(req);
  1524. return(ok);
  1525. }
  1526. static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,
  1527. STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, char *subj,
  1528. unsigned long chtype, int multirdn,
  1529. int email_dn, char *startdate, char *enddate, long days, int batch,
  1530. int verbose, X509_REQ *req, char *ext_sect, CONF *lconf,
  1531. unsigned long certopt, unsigned long nameopt, int default_op,
  1532. int ext_copy, int selfsign)
  1533. {
  1534. X509_NAME *name=NULL,*CAname=NULL,*subject=NULL, *dn_subject=NULL;
  1535. ASN1_UTCTIME *tm,*tmptm;
  1536. ASN1_STRING *str,*str2;
  1537. ASN1_OBJECT *obj;
  1538. X509 *ret=NULL;
  1539. X509_CINF *ci;
  1540. X509_NAME_ENTRY *ne;
  1541. X509_NAME_ENTRY *tne,*push;
  1542. EVP_PKEY *pktmp;
  1543. int ok= -1,i,j,last,nid;
  1544. const char *p;
  1545. CONF_VALUE *cv;
  1546. STRING row[DB_NUMBER];
  1547. STRING *irow=NULL;
  1548. STRING *rrow=NULL;
  1549. char buf[25];
  1550. tmptm=ASN1_UTCTIME_new();
  1551. if (tmptm == NULL)
  1552. {
  1553. BIO_printf(bio_err,"malloc error\n");
  1554. return(0);
  1555. }
  1556. for (i=0; i<DB_NUMBER; i++)
  1557. row[i]=NULL;
  1558. if (subj)
  1559. {
  1560. X509_NAME *n = parse_name(subj, chtype, multirdn);
  1561. if (!n)
  1562. {
  1563. ERR_print_errors(bio_err);
  1564. goto err;
  1565. }
  1566. X509_REQ_set_subject_name(req,n);
  1567. req->req_info->enc.modified = 1;
  1568. X509_NAME_free(n);
  1569. }
  1570. if (default_op)
  1571. BIO_printf(bio_err,"The Subject's Distinguished Name is as follows\n");
  1572. name=X509_REQ_get_subject_name(req);
  1573. for (i=0; i<X509_NAME_entry_count(name); i++)
  1574. {
  1575. ne= X509_NAME_get_entry(name,i);
  1576. str=X509_NAME_ENTRY_get_data(ne);
  1577. obj=X509_NAME_ENTRY_get_object(ne);
  1578. if (msie_hack)
  1579. {
  1580. /* assume all type should be strings */
  1581. nid=OBJ_obj2nid(ne->object);
  1582. if (str->type == V_ASN1_UNIVERSALSTRING)
  1583. ASN1_UNIVERSALSTRING_to_string(str);
  1584. if ((str->type == V_ASN1_IA5STRING) &&
  1585. (nid != NID_pkcs9_emailAddress))
  1586. str->type=V_ASN1_T61STRING;
  1587. if ((nid == NID_pkcs9_emailAddress) &&
  1588. (str->type == V_ASN1_PRINTABLESTRING))
  1589. str->type=V_ASN1_IA5STRING;
  1590. }
  1591. /* If no EMAIL is wanted in the subject */
  1592. if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (!email_dn))
  1593. continue;
  1594. /* check some things */
  1595. if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) &&
  1596. (str->type != V_ASN1_IA5STRING))
  1597. {
  1598. BIO_printf(bio_err,"\nemailAddress type needs to be of type IA5STRING\n");
  1599. goto err;
  1600. }
  1601. if ((str->type != V_ASN1_BMPSTRING) && (str->type != V_ASN1_UTF8STRING))
  1602. {
  1603. j=ASN1_PRINTABLE_type(str->data,str->length);
  1604. if ( ((j == V_ASN1_T61STRING) &&
  1605. (str->type != V_ASN1_T61STRING)) ||
  1606. ((j == V_ASN1_IA5STRING) &&
  1607. (str->type == V_ASN1_PRINTABLESTRING)))
  1608. {
  1609. BIO_printf(bio_err,"\nThe string contains characters that are illegal for the ASN.1 type\n");
  1610. goto err;
  1611. }
  1612. }
  1613. if (default_op)
  1614. old_entry_print(bio_err, obj, str);
  1615. }
  1616. /* Ok, now we check the 'policy' stuff. */
  1617. if ((subject=X509_NAME_new()) == NULL)
  1618. {
  1619. BIO_printf(bio_err,"Memory allocation failure\n");
  1620. goto err;
  1621. }
  1622. /* take a copy of the issuer name before we mess with it. */
  1623. if (selfsign)
  1624. CAname=X509_NAME_dup(name);
  1625. else
  1626. CAname=X509_NAME_dup(x509->cert_info->subject);
  1627. if (CAname == NULL) goto err;
  1628. str=str2=NULL;
  1629. for (i=0; i<sk_CONF_VALUE_num(policy); i++)
  1630. {
  1631. cv=sk_CONF_VALUE_value(policy,i); /* get the object id */
  1632. if ((j=OBJ_txt2nid(cv->name)) == NID_undef)
  1633. {
  1634. BIO_printf(bio_err,"%s:unknown object type in 'policy' configuration\n",cv->name);
  1635. goto err;
  1636. }
  1637. obj=OBJ_nid2obj(j);
  1638. last= -1;
  1639. for (;;)
  1640. {
  1641. /* lookup the object in the supplied name list */
  1642. j=X509_NAME_get_index_by_OBJ(name,obj,last);
  1643. if (j < 0)
  1644. {
  1645. if (last != -1) break;
  1646. tne=NULL;
  1647. }
  1648. else
  1649. {
  1650. tne=X509_NAME_get_entry(name,j);
  1651. }
  1652. last=j;
  1653. /* depending on the 'policy', decide what to do. */
  1654. push=NULL;
  1655. if (strcmp(cv->value,"optional") == 0)
  1656. {
  1657. if (tne != NULL)
  1658. push=tne;
  1659. }
  1660. else if (strcmp(cv->value,"supplied") == 0)
  1661. {
  1662. if (tne == NULL)
  1663. {
  1664. BIO_printf(bio_err,"The %s field needed to be supplied and was missing\n",cv->name);
  1665. goto err;
  1666. }
  1667. else
  1668. push=tne;
  1669. }
  1670. else if (strcmp(cv->value,"match") == 0)
  1671. {
  1672. int last2;
  1673. if (tne == NULL)
  1674. {
  1675. BIO_printf(bio_err,"The mandatory %s field was missing\n",cv->name);
  1676. goto err;
  1677. }
  1678. last2= -1;
  1679. again2:
  1680. j=X509_NAME_get_index_by_OBJ(CAname,obj,last2);
  1681. if ((j < 0) && (last2 == -1))
  1682. {
  1683. BIO_printf(bio_err,"The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n",cv->name);
  1684. goto err;
  1685. }
  1686. if (j >= 0)
  1687. {
  1688. push=X509_NAME_get_entry(CAname,j);
  1689. str=X509_NAME_ENTRY_get_data(tne);
  1690. str2=X509_NAME_ENTRY_get_data(push);
  1691. last2=j;
  1692. if (ASN1_STRING_cmp(str,str2) != 0)
  1693. goto again2;
  1694. }
  1695. if (j < 0)
  1696. {
  1697. BIO_printf(bio_err,"The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n",cv->name,((str2 == NULL)?"NULL":(char *)str2->data),((str == NULL)?"NULL":(char *)str->data));
  1698. goto err;
  1699. }
  1700. }
  1701. else
  1702. {
  1703. BIO_printf(bio_err,"%s:invalid type in 'policy' configuration\n",cv->value);
  1704. goto err;
  1705. }
  1706. if (push != NULL)
  1707. {
  1708. if (!X509_NAME_add_entry(subject,push, -1, 0))
  1709. {
  1710. if (push != NULL)
  1711. X509_NAME_ENTRY_free(push);
  1712. BIO_printf(bio_err,"Memory allocation failure\n");
  1713. goto err;
  1714. }
  1715. }
  1716. if (j < 0) break;
  1717. }
  1718. }
  1719. if (preserve)
  1720. {
  1721. X509_NAME_free(subject);
  1722. /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
  1723. subject=X509_NAME_dup(name);
  1724. if (subject == NULL) goto err;
  1725. }
  1726. if (verbose)
  1727. BIO_printf(bio_err,"The subject name appears to be ok, checking data base for clashes\n");
  1728. /* Build the correct Subject if no e-mail is wanted in the subject */
  1729. /* and add it later on because of the method extensions are added (altName) */
  1730. if (email_dn)
  1731. dn_subject = subject;
  1732. else
  1733. {
  1734. X509_NAME_ENTRY *tmpne;
  1735. /* Its best to dup the subject DN and then delete any email
  1736. * addresses because this retains its structure.
  1737. */
  1738. if (!(dn_subject = X509_NAME_dup(subject)))
  1739. {
  1740. BIO_printf(bio_err,"Memory allocation failure\n");
  1741. goto err;
  1742. }
  1743. while((i = X509_NAME_get_index_by_NID(dn_subject,
  1744. NID_pkcs9_emailAddress, -1)) >= 0)
  1745. {
  1746. tmpne = X509_NAME_get_entry(dn_subject, i);
  1747. X509_NAME_delete_entry(dn_subject, i);
  1748. X509_NAME_ENTRY_free(tmpne);
  1749. }
  1750. }
  1751. if (BN_is_zero(serial))
  1752. row[DB_serial]=BUF_strdup("00");
  1753. else
  1754. row[DB_serial]=BN_bn2hex(serial);
  1755. if (row[DB_serial] == NULL)
  1756. {
  1757. BIO_printf(bio_err,"Memory allocation failure\n");
  1758. goto err;
  1759. }
  1760. if (db->attributes.unique_subject)
  1761. {
  1762. STRING *crow=row;
  1763. rrow=TXT_DB_get_by_index(db->db,DB_name,crow);
  1764. if (rrow != NULL)
  1765. {
  1766. BIO_printf(bio_err,
  1767. "ERROR:There is already a certificate for %s\n",
  1768. row[DB_name]);
  1769. }
  1770. }
  1771. if (rrow == NULL)
  1772. {
  1773. rrow=TXT_DB_get_by_index(db->db,DB_serial,row);
  1774. if (rrow != NULL)
  1775. {
  1776. BIO_printf(bio_err,"ERROR:Serial number %s has already been issued,\n",
  1777. row[DB_serial]);
  1778. BIO_printf(bio_err," check the database/serial_file for corruption\n");
  1779. }
  1780. }
  1781. if (rrow != NULL)
  1782. {
  1783. BIO_printf(bio_err,
  1784. "The matching entry has the following details\n");
  1785. if (rrow[DB_type][0] == 'E')
  1786. p="Expired";
  1787. else if (rrow[DB_type][0] == 'R')
  1788. p="Revoked";
  1789. else if (rrow[DB_type][0] == 'V')
  1790. p="Valid";
  1791. else
  1792. p="\ninvalid type, Data base error\n";
  1793. BIO_printf(bio_err,"Type :%s\n",p);;
  1794. if (rrow[DB_type][0] == 'R')
  1795. {
  1796. p=rrow[DB_exp_date]; if (p == NULL) p="undef";
  1797. BIO_printf(bio_err,"Was revoked on:%s\n",p);
  1798. }
  1799. p=rrow[DB_exp_date]; if (p == NULL) p="undef";
  1800. BIO_printf(bio_err,"Expires on :%s\n",p);
  1801. p=rrow[DB_serial]; if (p == NULL) p="undef";
  1802. BIO_printf(bio_err,"Serial Number :%s\n",p);
  1803. p=rrow[DB_file]; if (p == NULL) p="undef";
  1804. BIO_printf(bio_err,"File name :%s\n",p);
  1805. p=rrow[DB_name]; if (p == NULL) p="undef";
  1806. BIO_printf(bio_err,"Subject Name :%s\n",p);
  1807. ok= -1; /* This is now a 'bad' error. */
  1808. goto err;
  1809. }
  1810. /* We are now totally happy, lets make and sign the certificate */
  1811. if (verbose)
  1812. BIO_printf(bio_err,"Everything appears to be ok, creating and signing the certificate\n");
  1813. if ((ret=X509_new()) == NULL) goto err;
  1814. ci=ret->cert_info;
  1815. #ifdef X509_V3
  1816. /* Make it an X509 v3 certificate. */
  1817. if (!X509_set_version(ret,2)) goto err;
  1818. #endif
  1819. if (BN_to_ASN1_INTEGER(serial,ci->serialNumber) == NULL)
  1820. goto err;
  1821. if (selfsign)
  1822. {
  1823. if (!X509_set_issuer_name(ret,subject))
  1824. goto err;
  1825. }
  1826. else
  1827. {
  1828. if (!X509_set_issuer_name(ret,X509_get_subject_name(x509)))
  1829. goto err;
  1830. }
  1831. if (strcmp(startdate,"today") == 0)
  1832. X509_gmtime_adj(X509_get_notBefore(ret),0);
  1833. else ASN1_UTCTIME_set_string(X509_get_notBefore(ret),startdate);
  1834. if (enddate == NULL)
  1835. X509_time_adj_ex(X509_get_notAfter(ret),days, 0, NULL);
  1836. else ASN1_UTCTIME_set_string(X509_get_notAfter(ret),enddate);
  1837. if (!X509_set_subject_name(ret,subject)) goto err;
  1838. pktmp=X509_REQ_get_pubkey(req);
  1839. i = X509_set_pubkey(ret,pktmp);
  1840. EVP_PKEY_free(pktmp);
  1841. if (!i) goto err;
  1842. /* Lets add the extensions, if there are any */
  1843. if (ext_sect)
  1844. {
  1845. X509V3_CTX ctx;
  1846. if (ci->version == NULL)
  1847. if ((ci->version=ASN1_INTEGER_new()) == NULL)
  1848. goto err;
  1849. ASN1_INTEGER_set(ci->version,2); /* version 3 certificate */
  1850. /* Free the current entries if any, there should not
  1851. * be any I believe */
  1852. if (ci->extensions != NULL)
  1853. sk_X509_EXTENSION_pop_free(ci->extensions,
  1854. X509_EXTENSION_free);
  1855. ci->extensions = NULL;
  1856. /* Initialize the context structure */
  1857. if (selfsign)
  1858. X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);
  1859. else
  1860. X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
  1861. if (extconf)
  1862. {
  1863. if (verbose)
  1864. BIO_printf(bio_err, "Extra configuration file found\n");
  1865. /* Use the extconf configuration db LHASH */
  1866. X509V3_set_nconf(&ctx, extconf);
  1867. /* Test the structure (needed?) */
  1868. /* X509V3_set_ctx_test(&ctx); */
  1869. /* Adds exts contained in the configuration file */
  1870. if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect,ret))
  1871. {
  1872. BIO_printf(bio_err,
  1873. "ERROR: adding extensions in section %s\n",
  1874. ext_sect);
  1875. ERR_print_errors(bio_err);
  1876. goto err;
  1877. }
  1878. if (verbose)
  1879. BIO_printf(bio_err, "Successfully added extensions from file.\n");
  1880. }
  1881. else if (ext_sect)
  1882. {
  1883. /* We found extensions to be set from config file */
  1884. X509V3_set_nconf(&ctx, lconf);
  1885. if(!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret))
  1886. {
  1887. BIO_printf(bio_err, "ERROR: adding extensions in section %s\n", ext_sect);
  1888. ERR_print_errors(bio_err);
  1889. goto err;
  1890. }
  1891. if (verbose)
  1892. BIO_printf(bio_err, "Successfully added extensions from config\n");
  1893. }
  1894. }
  1895. /* Copy extensions from request (if any) */
  1896. if (!copy_extensions(ret, req, ext_copy))
  1897. {
  1898. BIO_printf(bio_err, "ERROR: adding extensions from request\n");
  1899. ERR_print_errors(bio_err);
  1900. goto err;
  1901. }
  1902. /* Set the right value for the noemailDN option */
  1903. if( email_dn == 0 )
  1904. {
  1905. if (!X509_set_subject_name(ret,dn_subject)) goto err;
  1906. }
  1907. if (!default_op)
  1908. {
  1909. BIO_printf(bio_err, "Certificate Details:\n");
  1910. /* Never print signature details because signature not present */
  1911. certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
  1912. X509_print_ex(bio_err, ret, nameopt, certopt);
  1913. }
  1914. BIO_printf(bio_err,"Certificate is to be certified until ");
  1915. ASN1_UTCTIME_print(bio_err,X509_get_notAfter(ret));
  1916. if (days) BIO_printf(bio_err," (%ld days)",days);
  1917. BIO_printf(bio_err, "\n");
  1918. if (!batch)
  1919. {
  1920. BIO_printf(bio_err,"Sign the certificate? [y/n]:");
  1921. (void)BIO_flush(bio_err);
  1922. buf[0]='\0';
  1923. fgets(buf,sizeof(buf)-1,stdin);
  1924. if (!((buf[0] == 'y') || (buf[0] == 'Y')))
  1925. {
  1926. BIO_printf(bio_err,"CERTIFICATE WILL NOT BE CERTIFIED\n");
  1927. ok=0;
  1928. goto err;
  1929. }
  1930. }
  1931. pktmp=X509_get_pubkey(ret);
  1932. if (EVP_PKEY_missing_parameters(pktmp) &&
  1933. !EVP_PKEY_missing_parameters(pkey))
  1934. EVP_PKEY_copy_parameters(pktmp,pkey);
  1935. EVP_PKEY_free(pktmp);
  1936. if (!X509_sign(ret,pkey,dgst))
  1937. goto err;
  1938. /* We now just add it to the database */
  1939. row[DB_type]=(char *)OPENSSL_malloc(2);
  1940. tm=X509_get_notAfter(ret);
  1941. row[DB_exp_date]=(char *)OPENSSL_malloc(tm->length+1);
  1942. memcpy(row[DB_exp_date],tm->data,tm->length);
  1943. row[DB_exp_date][tm->length]='\0';
  1944. row[DB_rev_date]=NULL;
  1945. /* row[DB_serial] done already */
  1946. row[DB_file]=(char *)OPENSSL_malloc(8);
  1947. row[DB_name]=X509_NAME_oneline(X509_get_subject_name(ret),NULL,0);
  1948. if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
  1949. (row[DB_file] == NULL) || (row[DB_name] == NULL))
  1950. {
  1951. BIO_printf(bio_err,"Memory allocation failure\n");
  1952. goto err;
  1953. }
  1954. BUF_strlcpy(row[DB_file],"unknown",8);
  1955. row[DB_type][0]='V';
  1956. row[DB_type][1]='\0';
  1957. if ((irow=(char **)OPENSSL_malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL)
  1958. {
  1959. BIO_printf(bio_err,"Memory allocation failure\n");
  1960. goto err;
  1961. }
  1962. for (i=0; i<DB_NUMBER; i++)
  1963. {
  1964. irow[i]=row[i];
  1965. row[i]=NULL;
  1966. }
  1967. irow[DB_NUMBER]=NULL;
  1968. if (!TXT_DB_insert(db->db,irow))
  1969. {
  1970. BIO_printf(bio_err,"failed to update database\n");
  1971. BIO_printf(bio_err,"TXT_DB error number %ld\n",db->db->error);
  1972. goto err;
  1973. }
  1974. ok=1;
  1975. err:
  1976. for (i=0; i<DB_NUMBER; i++)
  1977. if (row[i] != NULL) OPENSSL_free(row[i]);
  1978. if (CAname != NULL)
  1979. X509_NAME_free(CAname);
  1980. if (subject != NULL)
  1981. X509_NAME_free(subject);
  1982. if ((dn_subject != NULL) && !email_dn)
  1983. X509_NAME_free(dn_subject);
  1984. if (tmptm != NULL)
  1985. ASN1_UTCTIME_free(tmptm);
  1986. if (ok <= 0)
  1987. {
  1988. if (ret != NULL) X509_free(ret);
  1989. ret=NULL;
  1990. }
  1991. else
  1992. *xret=ret;
  1993. return(ok);
  1994. }
  1995. static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext)
  1996. {
  1997. if (output_der)
  1998. {
  1999. (void)i2d_X509_bio(bp,x);
  2000. return;
  2001. }
  2002. #if 0
  2003. /* ??? Not needed since X509_print prints all this stuff anyway */
  2004. f=X509_NAME_oneline(X509_get_issuer_name(x),buf,256);
  2005. BIO_printf(bp,"issuer :%s\n",f);
  2006. f=X509_NAME_oneline(X509_get_subject_name(x),buf,256);
  2007. BIO_printf(bp,"subject:%s\n",f);
  2008. BIO_puts(bp,"serial :");
  2009. i2a_ASN1_INTEGER(bp,x->cert_info->serialNumber);
  2010. BIO_puts(bp,"\n\n");
  2011. #endif
  2012. if (!notext)X509_print(bp,x);
  2013. PEM_write_bio_X509(bp,x);
  2014. }
  2015. static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
  2016. const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  2017. BIGNUM *serial, char *subj,unsigned long chtype, int multirdn, int email_dn, char *startdate, char *enddate,
  2018. long days, char *ext_sect, CONF *lconf, int verbose, unsigned long certopt,
  2019. unsigned long nameopt, int default_op, int ext_copy)
  2020. {
  2021. STACK_OF(CONF_VALUE) *sk=NULL;
  2022. LHASH_OF(CONF_VALUE) *parms=NULL;
  2023. X509_REQ *req=NULL;
  2024. CONF_VALUE *cv=NULL;
  2025. NETSCAPE_SPKI *spki = NULL;
  2026. X509_REQ_INFO *ri;
  2027. char *type,*buf;
  2028. EVP_PKEY *pktmp=NULL;
  2029. X509_NAME *n=NULL;
  2030. X509_NAME_ENTRY *ne=NULL;
  2031. int ok= -1,i,j;
  2032. long errline;
  2033. int nid;
  2034. /*
  2035. * Load input file into a hash table. (This is just an easy
  2036. * way to read and parse the file, then put it into a convenient
  2037. * STACK format).
  2038. */
  2039. parms=CONF_load(NULL,infile,&errline);
  2040. if (parms == NULL)
  2041. {
  2042. BIO_printf(bio_err,"error on line %ld of %s\n",errline,infile);
  2043. ERR_print_errors(bio_err);
  2044. goto err;
  2045. }
  2046. sk=CONF_get_section(parms, "default");
  2047. if (sk_CONF_VALUE_num(sk) == 0)
  2048. {
  2049. BIO_printf(bio_err, "no name/value pairs found in %s\n", infile);
  2050. CONF_free(parms);
  2051. goto err;
  2052. }
  2053. /*
  2054. * Now create a dummy X509 request structure. We don't actually
  2055. * have an X509 request, but we have many of the components
  2056. * (a public key, various DN components). The idea is that we
  2057. * put these components into the right X509 request structure
  2058. * and we can use the same code as if you had a real X509 request.
  2059. */
  2060. req=X509_REQ_new();
  2061. if (req == NULL)
  2062. {
  2063. ERR_print_errors(bio_err);
  2064. goto err;
  2065. }
  2066. /*
  2067. * Build up the subject name set.
  2068. */
  2069. ri=req->req_info;
  2070. n = ri->subject;
  2071. for (i = 0; ; i++)
  2072. {
  2073. if (sk_CONF_VALUE_num(sk) <= i) break;
  2074. cv=sk_CONF_VALUE_value(sk,i);
  2075. type=cv->name;
  2076. /* Skip past any leading X. X: X, etc to allow for
  2077. * multiple instances
  2078. */
  2079. for (buf = cv->name; *buf ; buf++)
  2080. if ((*buf == ':') || (*buf == ',') || (*buf == '.'))
  2081. {
  2082. buf++;
  2083. if (*buf) type = buf;
  2084. break;
  2085. }
  2086. buf=cv->value;
  2087. if ((nid=OBJ_txt2nid(type)) == NID_undef)
  2088. {
  2089. if (strcmp(type, "SPKAC") == 0)
  2090. {
  2091. spki = NETSCAPE_SPKI_b64_decode(cv->value, -1);
  2092. if (spki == NULL)
  2093. {
  2094. BIO_printf(bio_err,"unable to load Netscape SPKAC structure\n");
  2095. ERR_print_errors(bio_err);
  2096. goto err;
  2097. }
  2098. }
  2099. continue;
  2100. }
  2101. /*
  2102. if ((nid == NID_pkcs9_emailAddress) && (email_dn == 0))
  2103. continue;
  2104. */
  2105. j=ASN1_PRINTABLE_type((unsigned char *)buf,-1);
  2106. if (fix_data(nid, &j) == 0)
  2107. {
  2108. BIO_printf(bio_err,
  2109. "invalid characters in string %s\n",buf);
  2110. goto err;
  2111. }
  2112. if ((ne=X509_NAME_ENTRY_create_by_NID(&ne,nid,j,
  2113. (unsigned char *)buf,
  2114. strlen(buf))) == NULL)
  2115. goto err;
  2116. if (!X509_NAME_add_entry(n,ne,-1, 0)) goto err;
  2117. }
  2118. if (spki == NULL)
  2119. {
  2120. BIO_printf(bio_err,"Netscape SPKAC structure not found in %s\n",
  2121. infile);
  2122. goto err;
  2123. }
  2124. /*
  2125. * Now extract the key from the SPKI structure.
  2126. */
  2127. BIO_printf(bio_err,"Check that the SPKAC request matches the signature\n");
  2128. if ((pktmp=NETSCAPE_SPKI_get_pubkey(spki)) == NULL)
  2129. {
  2130. BIO_printf(bio_err,"error unpacking SPKAC public key\n");
  2131. goto err;
  2132. }
  2133. j = NETSCAPE_SPKI_verify(spki, pktmp);
  2134. if (j <= 0)
  2135. {
  2136. BIO_printf(bio_err,"signature verification failed on SPKAC public key\n");
  2137. goto err;
  2138. }
  2139. BIO_printf(bio_err,"Signature ok\n");
  2140. X509_REQ_set_pubkey(req,pktmp);
  2141. EVP_PKEY_free(pktmp);
  2142. ok=do_body(xret,pkey,x509,dgst,policy,db,serial,subj,chtype,multirdn,email_dn,startdate,enddate,
  2143. days,1,verbose,req,ext_sect,lconf, certopt, nameopt, default_op,
  2144. ext_copy, 0);
  2145. err:
  2146. if (req != NULL) X509_REQ_free(req);
  2147. if (parms != NULL) CONF_free(parms);
  2148. if (spki != NULL) NETSCAPE_SPKI_free(spki);
  2149. if (ne != NULL) X509_NAME_ENTRY_free(ne);
  2150. return(ok);
  2151. }
  2152. static int fix_data(int nid, int *type)
  2153. {
  2154. if (nid == NID_pkcs9_emailAddress)
  2155. *type=V_ASN1_IA5STRING;
  2156. if ((nid == NID_commonName) && (*type == V_ASN1_IA5STRING))
  2157. *type=V_ASN1_T61STRING;
  2158. if ((nid == NID_pkcs9_challengePassword) && (*type == V_ASN1_IA5STRING))
  2159. *type=V_ASN1_T61STRING;
  2160. if ((nid == NID_pkcs9_unstructuredName) && (*type == V_ASN1_T61STRING))
  2161. return(0);
  2162. if (nid == NID_pkcs9_unstructuredName)
  2163. *type=V_ASN1_IA5STRING;
  2164. return(1);
  2165. }
  2166. static int check_time_format(const char *str)
  2167. {
  2168. ASN1_UTCTIME tm;
  2169. tm.data=(unsigned char *)str;
  2170. tm.length=strlen(str);
  2171. tm.type=V_ASN1_UTCTIME;
  2172. return(ASN1_UTCTIME_check(&tm));
  2173. }
  2174. static int do_revoke(X509 *x509, CA_DB *db, int type, char *value)
  2175. {
  2176. ASN1_UTCTIME *tm=NULL;
  2177. char *row[DB_NUMBER],**rrow,**irow;
  2178. char *rev_str = NULL;
  2179. BIGNUM *bn = NULL;
  2180. int ok=-1,i;
  2181. for (i=0; i<DB_NUMBER; i++)
  2182. row[i]=NULL;
  2183. row[DB_name]=X509_NAME_oneline(X509_get_subject_name(x509),NULL,0);
  2184. bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509),NULL);
  2185. if (!bn)
  2186. goto err;
  2187. if (BN_is_zero(bn))
  2188. row[DB_serial]=BUF_strdup("00");
  2189. else
  2190. row[DB_serial]=BN_bn2hex(bn);
  2191. BN_free(bn);
  2192. if ((row[DB_name] == NULL) || (row[DB_serial] == NULL))
  2193. {
  2194. BIO_printf(bio_err,"Memory allocation failure\n");
  2195. goto err;
  2196. }
  2197. /* We have to lookup by serial number because name lookup
  2198. * skips revoked certs
  2199. */
  2200. rrow=TXT_DB_get_by_index(db->db,DB_serial,row);
  2201. if (rrow == NULL)
  2202. {
  2203. BIO_printf(bio_err,"Adding Entry with serial number %s to DB for %s\n", row[DB_serial], row[DB_name]);
  2204. /* We now just add it to the database */
  2205. row[DB_type]=(char *)OPENSSL_malloc(2);
  2206. tm=X509_get_notAfter(x509);
  2207. row[DB_exp_date]=(char *)OPENSSL_malloc(tm->length+1);
  2208. memcpy(row[DB_exp_date],tm->data,tm->length);
  2209. row[DB_exp_date][tm->length]='\0';
  2210. row[DB_rev_date]=NULL;
  2211. /* row[DB_serial] done already */
  2212. row[DB_file]=(char *)OPENSSL_malloc(8);
  2213. /* row[DB_name] done already */
  2214. if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
  2215. (row[DB_file] == NULL))
  2216. {
  2217. BIO_printf(bio_err,"Memory allocation failure\n");
  2218. goto err;
  2219. }
  2220. BUF_strlcpy(row[DB_file],"unknown",8);
  2221. row[DB_type][0]='V';
  2222. row[DB_type][1]='\0';
  2223. if ((irow=(char **)OPENSSL_malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL)
  2224. {
  2225. BIO_printf(bio_err,"Memory allocation failure\n");
  2226. goto err;
  2227. }
  2228. for (i=0; i<DB_NUMBER; i++)
  2229. {
  2230. irow[i]=row[i];
  2231. row[i]=NULL;
  2232. }
  2233. irow[DB_NUMBER]=NULL;
  2234. if (!TXT_DB_insert(db->db,irow))
  2235. {
  2236. BIO_printf(bio_err,"failed to update database\n");
  2237. BIO_printf(bio_err,"TXT_DB error number %ld\n",db->db->error);
  2238. goto err;
  2239. }
  2240. /* Revoke Certificate */
  2241. ok = do_revoke(x509,db, type, value);
  2242. goto err;
  2243. }
  2244. else if (index_name_cmp_noconst(row, rrow))
  2245. {
  2246. BIO_printf(bio_err,"ERROR:name does not match %s\n",
  2247. row[DB_name]);
  2248. goto err;
  2249. }
  2250. else if (rrow[DB_type][0]=='R')
  2251. {
  2252. BIO_printf(bio_err,"ERROR:Already revoked, serial number %s\n",
  2253. row[DB_serial]);
  2254. goto err;
  2255. }
  2256. else
  2257. {
  2258. BIO_printf(bio_err,"Revoking Certificate %s.\n", rrow[DB_serial]);
  2259. rev_str = make_revocation_str(type, value);
  2260. if (!rev_str)
  2261. {
  2262. BIO_printf(bio_err, "Error in revocation arguments\n");
  2263. goto err;
  2264. }
  2265. rrow[DB_type][0]='R';
  2266. rrow[DB_type][1]='\0';
  2267. rrow[DB_rev_date] = rev_str;
  2268. }
  2269. ok=1;
  2270. err:
  2271. for (i=0; i<DB_NUMBER; i++)
  2272. {
  2273. if (row[i] != NULL)
  2274. OPENSSL_free(row[i]);
  2275. }
  2276. return(ok);
  2277. }
  2278. static int get_certificate_status(const char *serial, CA_DB *db)
  2279. {
  2280. char *row[DB_NUMBER],**rrow;
  2281. int ok=-1,i;
  2282. /* Free Resources */
  2283. for (i=0; i<DB_NUMBER; i++)
  2284. row[i]=NULL;
  2285. /* Malloc needed char spaces */
  2286. row[DB_serial] = OPENSSL_malloc(strlen(serial) + 2);
  2287. if (row[DB_serial] == NULL)
  2288. {
  2289. BIO_printf(bio_err,"Malloc failure\n");
  2290. goto err;
  2291. }
  2292. if (strlen(serial) % 2)
  2293. {
  2294. /* Set the first char to 0 */;
  2295. row[DB_serial][0]='0';
  2296. /* Copy String from serial to row[DB_serial] */
  2297. memcpy(row[DB_serial]+1, serial, strlen(serial));
  2298. row[DB_serial][strlen(serial)+1]='\0';
  2299. }
  2300. else
  2301. {
  2302. /* Copy String from serial to row[DB_serial] */
  2303. memcpy(row[DB_serial], serial, strlen(serial));
  2304. row[DB_serial][strlen(serial)]='\0';
  2305. }
  2306. /* Make it Upper Case */
  2307. for (i=0; row[DB_serial][i] != '\0'; i++)
  2308. row[DB_serial][i] = toupper(row[DB_serial][i]);
  2309. ok=1;
  2310. /* Search for the certificate */
  2311. rrow=TXT_DB_get_by_index(db->db,DB_serial,row);
  2312. if (rrow == NULL)
  2313. {
  2314. BIO_printf(bio_err,"Serial %s not present in db.\n",
  2315. row[DB_serial]);
  2316. ok=-1;
  2317. goto err;
  2318. }
  2319. else if (rrow[DB_type][0]=='V')
  2320. {
  2321. BIO_printf(bio_err,"%s=Valid (%c)\n",
  2322. row[DB_serial], rrow[DB_type][0]);
  2323. goto err;
  2324. }
  2325. else if (rrow[DB_type][0]=='R')
  2326. {
  2327. BIO_printf(bio_err,"%s=Revoked (%c)\n",
  2328. row[DB_serial], rrow[DB_type][0]);
  2329. goto err;
  2330. }
  2331. else if (rrow[DB_type][0]=='E')
  2332. {
  2333. BIO_printf(bio_err,"%s=Expired (%c)\n",
  2334. row[DB_serial], rrow[DB_type][0]);
  2335. goto err;
  2336. }
  2337. else if (rrow[DB_type][0]=='S')
  2338. {
  2339. BIO_printf(bio_err,"%s=Suspended (%c)\n",
  2340. row[DB_serial], rrow[DB_type][0]);
  2341. goto err;
  2342. }
  2343. else
  2344. {
  2345. BIO_printf(bio_err,"%s=Unknown (%c).\n",
  2346. row[DB_serial], rrow[DB_type][0]);
  2347. ok=-1;
  2348. }
  2349. err:
  2350. for (i=0; i<DB_NUMBER; i++)
  2351. {
  2352. if (row[i] != NULL)
  2353. OPENSSL_free(row[i]);
  2354. }
  2355. return(ok);
  2356. }
  2357. static int do_updatedb (CA_DB *db)
  2358. {
  2359. ASN1_UTCTIME *a_tm = NULL;
  2360. int i, cnt = 0;
  2361. int db_y2k, a_y2k; /* flags = 1 if y >= 2000 */
  2362. char **rrow, *a_tm_s;
  2363. a_tm = ASN1_UTCTIME_new();
  2364. /* get actual time and make a string */
  2365. a_tm = X509_gmtime_adj(a_tm, 0);
  2366. a_tm_s = (char *) OPENSSL_malloc(a_tm->length+1);
  2367. if (a_tm_s == NULL)
  2368. {
  2369. cnt = -1;
  2370. goto err;
  2371. }
  2372. memcpy(a_tm_s, a_tm->data, a_tm->length);
  2373. a_tm_s[a_tm->length] = '\0';
  2374. if (strncmp(a_tm_s, "49", 2) <= 0)
  2375. a_y2k = 1;
  2376. else
  2377. a_y2k = 0;
  2378. for (i = 0; i < sk_PSTRING_num(db->db->data); i++)
  2379. {
  2380. rrow = sk_PSTRING_value(db->db->data, i);
  2381. if (rrow[DB_type][0] == 'V')
  2382. {
  2383. /* ignore entries that are not valid */
  2384. if (strncmp(rrow[DB_exp_date], "49", 2) <= 0)
  2385. db_y2k = 1;
  2386. else
  2387. db_y2k = 0;
  2388. if (db_y2k == a_y2k)
  2389. {
  2390. /* all on the same y2k side */
  2391. if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0)
  2392. {
  2393. rrow[DB_type][0] = 'E';
  2394. rrow[DB_type][1] = '\0';
  2395. cnt++;
  2396. BIO_printf(bio_err, "%s=Expired\n",
  2397. rrow[DB_serial]);
  2398. }
  2399. }
  2400. else if (db_y2k < a_y2k)
  2401. {
  2402. rrow[DB_type][0] = 'E';
  2403. rrow[DB_type][1] = '\0';
  2404. cnt++;
  2405. BIO_printf(bio_err, "%s=Expired\n",
  2406. rrow[DB_serial]);
  2407. }
  2408. }
  2409. }
  2410. err:
  2411. ASN1_UTCTIME_free(a_tm);
  2412. OPENSSL_free(a_tm_s);
  2413. return (cnt);
  2414. }
  2415. static const char *crl_reasons[] = {
  2416. /* CRL reason strings */
  2417. "unspecified",
  2418. "keyCompromise",
  2419. "CACompromise",
  2420. "affiliationChanged",
  2421. "superseded",
  2422. "cessationOfOperation",
  2423. "certificateHold",
  2424. "removeFromCRL",
  2425. /* Additional pseudo reasons */
  2426. "holdInstruction",
  2427. "keyTime",
  2428. "CAkeyTime"
  2429. };
  2430. #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
  2431. /* Given revocation information convert to a DB string.
  2432. * The format of the string is:
  2433. * revtime[,reason,extra]. Where 'revtime' is the
  2434. * revocation time (the current time). 'reason' is the
  2435. * optional CRL reason and 'extra' is any additional
  2436. * argument
  2437. */
  2438. char *make_revocation_str(int rev_type, char *rev_arg)
  2439. {
  2440. char *other = NULL, *str;
  2441. const char *reason = NULL;
  2442. ASN1_OBJECT *otmp;
  2443. ASN1_UTCTIME *revtm = NULL;
  2444. int i;
  2445. switch (rev_type)
  2446. {
  2447. case REV_NONE:
  2448. break;
  2449. case REV_CRL_REASON:
  2450. for (i = 0; i < 8; i++)
  2451. {
  2452. if (!strcasecmp(rev_arg, crl_reasons[i]))
  2453. {
  2454. reason = crl_reasons[i];
  2455. break;
  2456. }
  2457. }
  2458. if (reason == NULL)
  2459. {
  2460. BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
  2461. return NULL;
  2462. }
  2463. break;
  2464. case REV_HOLD:
  2465. /* Argument is an OID */
  2466. otmp = OBJ_txt2obj(rev_arg, 0);
  2467. ASN1_OBJECT_free(otmp);
  2468. if (otmp == NULL)
  2469. {
  2470. BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg);
  2471. return NULL;
  2472. }
  2473. reason = "holdInstruction";
  2474. other = rev_arg;
  2475. break;
  2476. case REV_KEY_COMPROMISE:
  2477. case REV_CA_COMPROMISE:
  2478. /* Argument is the key compromise time */
  2479. if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg))
  2480. {
  2481. BIO_printf(bio_err, "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n", rev_arg);
  2482. return NULL;
  2483. }
  2484. other = rev_arg;
  2485. if (rev_type == REV_KEY_COMPROMISE)
  2486. reason = "keyTime";
  2487. else
  2488. reason = "CAkeyTime";
  2489. break;
  2490. }
  2491. revtm = X509_gmtime_adj(NULL, 0);
  2492. i = revtm->length + 1;
  2493. if (reason) i += strlen(reason) + 1;
  2494. if (other) i += strlen(other) + 1;
  2495. str = OPENSSL_malloc(i);
  2496. if (!str) return NULL;
  2497. BUF_strlcpy(str, (char *)revtm->data, i);
  2498. if (reason)
  2499. {
  2500. BUF_strlcat(str, ",", i);
  2501. BUF_strlcat(str, reason, i);
  2502. }
  2503. if (other)
  2504. {
  2505. BUF_strlcat(str, ",", i);
  2506. BUF_strlcat(str, other, i);
  2507. }
  2508. ASN1_UTCTIME_free(revtm);
  2509. return str;
  2510. }
  2511. /* Convert revocation field to X509_REVOKED entry
  2512. * return code:
  2513. * 0 error
  2514. * 1 OK
  2515. * 2 OK and some extensions added (i.e. V2 CRL)
  2516. */
  2517. int make_revoked(X509_REVOKED *rev, const char *str)
  2518. {
  2519. char *tmp = NULL;
  2520. int reason_code = -1;
  2521. int i, ret = 0;
  2522. ASN1_OBJECT *hold = NULL;
  2523. ASN1_GENERALIZEDTIME *comp_time = NULL;
  2524. ASN1_ENUMERATED *rtmp = NULL;
  2525. ASN1_TIME *revDate = NULL;
  2526. i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
  2527. if (i == 0)
  2528. goto err;
  2529. if (rev && !X509_REVOKED_set_revocationDate(rev, revDate))
  2530. goto err;
  2531. if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS))
  2532. {
  2533. rtmp = ASN1_ENUMERATED_new();
  2534. if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code))
  2535. goto err;
  2536. if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
  2537. goto err;
  2538. }
  2539. if (rev && comp_time)
  2540. {
  2541. if (!X509_REVOKED_add1_ext_i2d(rev, NID_invalidity_date, comp_time, 0, 0))
  2542. goto err;
  2543. }
  2544. if (rev && hold)
  2545. {
  2546. if (!X509_REVOKED_add1_ext_i2d(rev, NID_hold_instruction_code, hold, 0, 0))
  2547. goto err;
  2548. }
  2549. if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
  2550. ret = 2;
  2551. else ret = 1;
  2552. err:
  2553. if (tmp) OPENSSL_free(tmp);
  2554. ASN1_OBJECT_free(hold);
  2555. ASN1_GENERALIZEDTIME_free(comp_time);
  2556. ASN1_ENUMERATED_free(rtmp);
  2557. ASN1_TIME_free(revDate);
  2558. return ret;
  2559. }
  2560. int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str)
  2561. {
  2562. char buf[25],*pbuf, *p;
  2563. int j;
  2564. j=i2a_ASN1_OBJECT(bp,obj);
  2565. pbuf=buf;
  2566. for (j=22-j; j>0; j--)
  2567. *(pbuf++)=' ';
  2568. *(pbuf++)=':';
  2569. *(pbuf++)='\0';
  2570. BIO_puts(bp,buf);
  2571. if (str->type == V_ASN1_PRINTABLESTRING)
  2572. BIO_printf(bp,"PRINTABLE:'");
  2573. else if (str->type == V_ASN1_T61STRING)
  2574. BIO_printf(bp,"T61STRING:'");
  2575. else if (str->type == V_ASN1_IA5STRING)
  2576. BIO_printf(bp,"IA5STRING:'");
  2577. else if (str->type == V_ASN1_UNIVERSALSTRING)
  2578. BIO_printf(bp,"UNIVERSALSTRING:'");
  2579. else
  2580. BIO_printf(bp,"ASN.1 %2d:'",str->type);
  2581. p=(char *)str->data;
  2582. for (j=str->length; j>0; j--)
  2583. {
  2584. if ((*p >= ' ') && (*p <= '~'))
  2585. BIO_printf(bp,"%c",*p);
  2586. else if (*p & 0x80)
  2587. BIO_printf(bp,"\\0x%02X",*p);
  2588. else if ((unsigned char)*p == 0xf7)
  2589. BIO_printf(bp,"^?");
  2590. else BIO_printf(bp,"^%c",*p+'@');
  2591. p++;
  2592. }
  2593. BIO_printf(bp,"'\n");
  2594. return 1;
  2595. }
  2596. int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, const char *str)
  2597. {
  2598. char *tmp = NULL;
  2599. char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
  2600. int reason_code = -1;
  2601. int ret = 0;
  2602. unsigned int i;
  2603. ASN1_OBJECT *hold = NULL;
  2604. ASN1_GENERALIZEDTIME *comp_time = NULL;
  2605. tmp = BUF_strdup(str);
  2606. p = strchr(tmp, ',');
  2607. rtime_str = tmp;
  2608. if (p)
  2609. {
  2610. *p = '\0';
  2611. p++;
  2612. reason_str = p;
  2613. p = strchr(p, ',');
  2614. if (p)
  2615. {
  2616. *p = '\0';
  2617. arg_str = p + 1;
  2618. }
  2619. }
  2620. if (prevtm)
  2621. {
  2622. *prevtm = ASN1_UTCTIME_new();
  2623. if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str))
  2624. {
  2625. BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
  2626. goto err;
  2627. }
  2628. }
  2629. if (reason_str)
  2630. {
  2631. for (i = 0; i < NUM_REASONS; i++)
  2632. {
  2633. if(!strcasecmp(reason_str, crl_reasons[i]))
  2634. {
  2635. reason_code = i;
  2636. break;
  2637. }
  2638. }
  2639. if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)
  2640. {
  2641. BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
  2642. goto err;
  2643. }
  2644. if (reason_code == 7)
  2645. reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
  2646. else if (reason_code == 8) /* Hold instruction */
  2647. {
  2648. if (!arg_str)
  2649. {
  2650. BIO_printf(bio_err, "missing hold instruction\n");
  2651. goto err;
  2652. }
  2653. reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
  2654. hold = OBJ_txt2obj(arg_str, 0);
  2655. if (!hold)
  2656. {
  2657. BIO_printf(bio_err, "invalid object identifier %s\n", arg_str);
  2658. goto err;
  2659. }
  2660. if (phold) *phold = hold;
  2661. }
  2662. else if ((reason_code == 9) || (reason_code == 10))
  2663. {
  2664. if (!arg_str)
  2665. {
  2666. BIO_printf(bio_err, "missing compromised time\n");
  2667. goto err;
  2668. }
  2669. comp_time = ASN1_GENERALIZEDTIME_new();
  2670. if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))
  2671. {
  2672. BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
  2673. goto err;
  2674. }
  2675. if (reason_code == 9)
  2676. reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
  2677. else
  2678. reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
  2679. }
  2680. }
  2681. if (preason) *preason = reason_code;
  2682. if (pinvtm) *pinvtm = comp_time;
  2683. else ASN1_GENERALIZEDTIME_free(comp_time);
  2684. ret = 1;
  2685. err:
  2686. if (tmp) OPENSSL_free(tmp);
  2687. if (!phold) ASN1_OBJECT_free(hold);
  2688. if (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time);
  2689. return ret;
  2690. }