ca.c 73 KB

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