ca.c 71 KB

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