ca.c 73 KB

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