apps.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024
  1. /* apps/apps.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. /* ====================================================================
  59. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #ifndef _POSIX_C_SOURCE
  112. #define _POSIX_C_SOURCE 2 /* On VMS, you need to define this to get
  113. the declaration of fileno(). The value
  114. 2 is to make sure no function defined
  115. in POSIX-2 is left undefined. */
  116. #endif
  117. #include <stdio.h>
  118. #include <stdlib.h>
  119. #include <string.h>
  120. #if !defined(OPENSSL_SYSNAME_WIN32) && !defined(NETWARE_CLIB)
  121. #include <strings.h>
  122. #endif
  123. #include <sys/types.h>
  124. #include <ctype.h>
  125. #include <errno.h>
  126. #include <assert.h>
  127. #include <openssl/err.h>
  128. #include <openssl/x509.h>
  129. #include <openssl/x509v3.h>
  130. #include <openssl/pem.h>
  131. #include <openssl/pkcs12.h>
  132. #include <openssl/ui.h>
  133. #include <openssl/safestack.h>
  134. #ifndef OPENSSL_NO_ENGINE
  135. #include <openssl/engine.h>
  136. #endif
  137. #ifndef OPENSSL_NO_RSA
  138. #include <openssl/rsa.h>
  139. #endif
  140. #include <openssl/bn.h>
  141. #ifndef OPENSSL_NO_JPAKE
  142. #include <openssl/jpake.h>
  143. #endif
  144. #define NON_MAIN
  145. #include "apps.h"
  146. #undef NON_MAIN
  147. #ifdef _WIN32
  148. static int WIN32_rename(const char *from, const char *to);
  149. #define rename(from,to) WIN32_rename((from),(to))
  150. #endif
  151. typedef struct {
  152. const char *name;
  153. unsigned long flag;
  154. unsigned long mask;
  155. } NAME_EX_TBL;
  156. static UI_METHOD *ui_method = NULL;
  157. static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
  158. static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
  159. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  160. /* Looks like this stuff is worth moving into separate function */
  161. static EVP_PKEY *
  162. load_netscape_key(BIO *err, BIO *key, const char *file,
  163. const char *key_descrip, int format);
  164. #endif
  165. int app_init(long mesgwin);
  166. #ifdef undef /* never finished - probably never will be :-) */
  167. int args_from_file(char *file, int *argc, char **argv[])
  168. {
  169. FILE *fp;
  170. int num,i;
  171. unsigned int len;
  172. static char *buf=NULL;
  173. static char **arg=NULL;
  174. char *p;
  175. fp=fopen(file,"r");
  176. if (fp == NULL)
  177. return(0);
  178. if (fseek(fp,0,SEEK_END)==0)
  179. len=ftell(fp), rewind(fp);
  180. else len=-1;
  181. if (len<=0)
  182. {
  183. fclose(fp);
  184. return(0);
  185. }
  186. *argc=0;
  187. *argv=NULL;
  188. if (buf != NULL) OPENSSL_free(buf);
  189. buf=(char *)OPENSSL_malloc(len+1);
  190. if (buf == NULL) return(0);
  191. len=fread(buf,1,len,fp);
  192. if (len <= 1) return(0);
  193. buf[len]='\0';
  194. i=0;
  195. for (p=buf; *p; p++)
  196. if (*p == '\n') i++;
  197. if (arg != NULL) OPENSSL_free(arg);
  198. arg=(char **)OPENSSL_malloc(sizeof(char *)*(i*2));
  199. *argv=arg;
  200. num=0;
  201. p=buf;
  202. for (;;)
  203. {
  204. if (!*p) break;
  205. if (*p == '#') /* comment line */
  206. {
  207. while (*p && (*p != '\n')) p++;
  208. continue;
  209. }
  210. /* else we have a line */
  211. *(arg++)=p;
  212. num++;
  213. while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
  214. p++;
  215. if (!*p) break;
  216. if (*p == '\n')
  217. {
  218. *(p++)='\0';
  219. continue;
  220. }
  221. /* else it is a tab or space */
  222. p++;
  223. while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
  224. p++;
  225. if (!*p) break;
  226. if (*p == '\n')
  227. {
  228. p++;
  229. continue;
  230. }
  231. *(arg++)=p++;
  232. num++;
  233. while (*p && (*p != '\n')) p++;
  234. if (!*p) break;
  235. /* else *p == '\n' */
  236. *(p++)='\0';
  237. }
  238. *argc=num;
  239. return(1);
  240. }
  241. #endif
  242. int str2fmt(char *s)
  243. {
  244. if ((*s == 'D') || (*s == 'd'))
  245. return(FORMAT_ASN1);
  246. else if ((*s == 'T') || (*s == 't'))
  247. return(FORMAT_TEXT);
  248. else if ((*s == 'N') || (*s == 'n'))
  249. return(FORMAT_NETSCAPE);
  250. else if ((*s == 'S') || (*s == 's'))
  251. return(FORMAT_SMIME);
  252. else if ((*s == 'M') || (*s == 'm'))
  253. return(FORMAT_MSBLOB);
  254. else if ((*s == '1')
  255. || (strcmp(s,"PKCS12") == 0) || (strcmp(s,"pkcs12") == 0)
  256. || (strcmp(s,"P12") == 0) || (strcmp(s,"p12") == 0))
  257. return(FORMAT_PKCS12);
  258. else if ((*s == 'E') || (*s == 'e'))
  259. return(FORMAT_ENGINE);
  260. else if ((*s == 'P') || (*s == 'p'))
  261. {
  262. if (s[1] == 'V' || s[1] == 'v')
  263. return FORMAT_PVK;
  264. else
  265. return(FORMAT_PEM);
  266. }
  267. else
  268. return(FORMAT_UNDEF);
  269. }
  270. #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_SYS_NETWARE)
  271. void program_name(char *in, char *out, int size)
  272. {
  273. int i,n;
  274. char *p=NULL;
  275. n=strlen(in);
  276. /* find the last '/', '\' or ':' */
  277. for (i=n-1; i>0; i--)
  278. {
  279. if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':'))
  280. {
  281. p= &(in[i+1]);
  282. break;
  283. }
  284. }
  285. if (p == NULL)
  286. p=in;
  287. n=strlen(p);
  288. #if defined(OPENSSL_SYS_NETWARE)
  289. /* strip off trailing .nlm if present. */
  290. if ((n > 4) && (p[n-4] == '.') &&
  291. ((p[n-3] == 'n') || (p[n-3] == 'N')) &&
  292. ((p[n-2] == 'l') || (p[n-2] == 'L')) &&
  293. ((p[n-1] == 'm') || (p[n-1] == 'M')))
  294. n-=4;
  295. #else
  296. /* strip off trailing .exe if present. */
  297. if ((n > 4) && (p[n-4] == '.') &&
  298. ((p[n-3] == 'e') || (p[n-3] == 'E')) &&
  299. ((p[n-2] == 'x') || (p[n-2] == 'X')) &&
  300. ((p[n-1] == 'e') || (p[n-1] == 'E')))
  301. n-=4;
  302. #endif
  303. if (n > size-1)
  304. n=size-1;
  305. for (i=0; i<n; i++)
  306. {
  307. if ((p[i] >= 'A') && (p[i] <= 'Z'))
  308. out[i]=p[i]-'A'+'a';
  309. else
  310. out[i]=p[i];
  311. }
  312. out[n]='\0';
  313. }
  314. #else
  315. #ifdef OPENSSL_SYS_VMS
  316. void program_name(char *in, char *out, int size)
  317. {
  318. char *p=in, *q;
  319. char *chars=":]>";
  320. while(*chars != '\0')
  321. {
  322. q=strrchr(p,*chars);
  323. if (q > p)
  324. p = q + 1;
  325. chars++;
  326. }
  327. q=strrchr(p,'.');
  328. if (q == NULL)
  329. q = p + strlen(p);
  330. strncpy(out,p,size-1);
  331. if (q-p >= size)
  332. {
  333. out[size-1]='\0';
  334. }
  335. else
  336. {
  337. out[q-p]='\0';
  338. }
  339. }
  340. #else
  341. void program_name(char *in, char *out, int size)
  342. {
  343. char *p;
  344. p=strrchr(in,'/');
  345. if (p != NULL)
  346. p++;
  347. else
  348. p=in;
  349. BUF_strlcpy(out,p,size);
  350. }
  351. #endif
  352. #endif
  353. int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
  354. {
  355. int num,len,i;
  356. char *p;
  357. *argc=0;
  358. *argv=NULL;
  359. len=strlen(buf);
  360. i=0;
  361. if (arg->count == 0)
  362. {
  363. arg->count=20;
  364. arg->data=(char **)OPENSSL_malloc(sizeof(char *)*arg->count);
  365. }
  366. for (i=0; i<arg->count; i++)
  367. arg->data[i]=NULL;
  368. num=0;
  369. p=buf;
  370. for (;;)
  371. {
  372. /* first scan over white space */
  373. if (!*p) break;
  374. while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
  375. p++;
  376. if (!*p) break;
  377. /* The start of something good :-) */
  378. if (num >= arg->count)
  379. {
  380. char **tmp_p;
  381. int tlen = arg->count + 20;
  382. tmp_p = (char **)OPENSSL_realloc(arg->data,
  383. sizeof(char *)*tlen);
  384. if (tmp_p == NULL)
  385. return 0;
  386. arg->data = tmp_p;
  387. arg->count = tlen;
  388. /* initialize newly allocated data */
  389. for (i = num; i < arg->count; i++)
  390. arg->data[i] = NULL;
  391. }
  392. arg->data[num++]=p;
  393. /* now look for the end of this */
  394. if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */
  395. {
  396. i= *(p++);
  397. arg->data[num-1]++; /* jump over quote */
  398. while (*p && (*p != i))
  399. p++;
  400. *p='\0';
  401. }
  402. else
  403. {
  404. while (*p && ((*p != ' ') &&
  405. (*p != '\t') && (*p != '\n')))
  406. p++;
  407. if (*p == '\0')
  408. p--;
  409. else
  410. *p='\0';
  411. }
  412. p++;
  413. }
  414. *argc=num;
  415. *argv=arg->data;
  416. return(1);
  417. }
  418. #ifndef APP_INIT
  419. int app_init(long mesgwin)
  420. {
  421. return(1);
  422. }
  423. #endif
  424. int dump_cert_text (BIO *out, X509 *x)
  425. {
  426. char *p;
  427. p=X509_NAME_oneline(X509_get_subject_name(x),NULL,0);
  428. BIO_puts(out,"subject=");
  429. BIO_puts(out,p);
  430. OPENSSL_free(p);
  431. p=X509_NAME_oneline(X509_get_issuer_name(x),NULL,0);
  432. BIO_puts(out,"\nissuer=");
  433. BIO_puts(out,p);
  434. BIO_puts(out,"\n");
  435. OPENSSL_free(p);
  436. return 0;
  437. }
  438. static int ui_open(UI *ui)
  439. {
  440. return UI_method_get_opener(UI_OpenSSL())(ui);
  441. }
  442. static int ui_read(UI *ui, UI_STRING *uis)
  443. {
  444. if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
  445. && UI_get0_user_data(ui))
  446. {
  447. switch(UI_get_string_type(uis))
  448. {
  449. case UIT_PROMPT:
  450. case UIT_VERIFY:
  451. {
  452. const char *password =
  453. ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
  454. if (password && password[0] != '\0')
  455. {
  456. UI_set_result(ui, uis, password);
  457. return 1;
  458. }
  459. }
  460. default:
  461. break;
  462. }
  463. }
  464. return UI_method_get_reader(UI_OpenSSL())(ui, uis);
  465. }
  466. static int ui_write(UI *ui, UI_STRING *uis)
  467. {
  468. if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
  469. && UI_get0_user_data(ui))
  470. {
  471. switch(UI_get_string_type(uis))
  472. {
  473. case UIT_PROMPT:
  474. case UIT_VERIFY:
  475. {
  476. const char *password =
  477. ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
  478. if (password && password[0] != '\0')
  479. return 1;
  480. }
  481. default:
  482. break;
  483. }
  484. }
  485. return UI_method_get_writer(UI_OpenSSL())(ui, uis);
  486. }
  487. static int ui_close(UI *ui)
  488. {
  489. return UI_method_get_closer(UI_OpenSSL())(ui);
  490. }
  491. int setup_ui_method(void)
  492. {
  493. ui_method = UI_create_method("OpenSSL application user interface");
  494. UI_method_set_opener(ui_method, ui_open);
  495. UI_method_set_reader(ui_method, ui_read);
  496. UI_method_set_writer(ui_method, ui_write);
  497. UI_method_set_closer(ui_method, ui_close);
  498. return 0;
  499. }
  500. void destroy_ui_method(void)
  501. {
  502. if(ui_method)
  503. {
  504. UI_destroy_method(ui_method);
  505. ui_method = NULL;
  506. }
  507. }
  508. int password_callback(char *buf, int bufsiz, int verify,
  509. PW_CB_DATA *cb_tmp)
  510. {
  511. UI *ui = NULL;
  512. int res = 0;
  513. const char *prompt_info = NULL;
  514. const char *password = NULL;
  515. PW_CB_DATA *cb_data = (PW_CB_DATA *)cb_tmp;
  516. if (cb_data)
  517. {
  518. if (cb_data->password)
  519. password = cb_data->password;
  520. if (cb_data->prompt_info)
  521. prompt_info = cb_data->prompt_info;
  522. }
  523. if (password)
  524. {
  525. res = strlen(password);
  526. if (res > bufsiz)
  527. res = bufsiz;
  528. memcpy(buf, password, res);
  529. return res;
  530. }
  531. ui = UI_new_method(ui_method);
  532. if (ui)
  533. {
  534. int ok = 0;
  535. char *buff = NULL;
  536. int ui_flags = 0;
  537. char *prompt = NULL;
  538. prompt = UI_construct_prompt(ui, "pass phrase",
  539. prompt_info);
  540. ui_flags |= UI_INPUT_FLAG_DEFAULT_PWD;
  541. UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0);
  542. if (ok >= 0)
  543. ok = UI_add_input_string(ui,prompt,ui_flags,buf,
  544. PW_MIN_LENGTH,BUFSIZ-1);
  545. if (ok >= 0 && verify)
  546. {
  547. buff = (char *)OPENSSL_malloc(bufsiz);
  548. ok = UI_add_verify_string(ui,prompt,ui_flags,buff,
  549. PW_MIN_LENGTH,BUFSIZ-1, buf);
  550. }
  551. if (ok >= 0)
  552. do
  553. {
  554. ok = UI_process(ui);
  555. }
  556. while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0));
  557. if (buff)
  558. {
  559. OPENSSL_cleanse(buff,(unsigned int)bufsiz);
  560. OPENSSL_free(buff);
  561. }
  562. if (ok >= 0)
  563. res = strlen(buf);
  564. if (ok == -1)
  565. {
  566. BIO_printf(bio_err, "User interface error\n");
  567. ERR_print_errors(bio_err);
  568. OPENSSL_cleanse(buf,(unsigned int)bufsiz);
  569. res = 0;
  570. }
  571. if (ok == -2)
  572. {
  573. BIO_printf(bio_err,"aborted!\n");
  574. OPENSSL_cleanse(buf,(unsigned int)bufsiz);
  575. res = 0;
  576. }
  577. UI_free(ui);
  578. OPENSSL_free(prompt);
  579. }
  580. return res;
  581. }
  582. static char *app_get_pass(BIO *err, char *arg, int keepbio);
  583. int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2)
  584. {
  585. int same;
  586. if(!arg2 || !arg1 || strcmp(arg1, arg2)) same = 0;
  587. else same = 1;
  588. if(arg1) {
  589. *pass1 = app_get_pass(err, arg1, same);
  590. if(!*pass1) return 0;
  591. } else if(pass1) *pass1 = NULL;
  592. if(arg2) {
  593. *pass2 = app_get_pass(err, arg2, same ? 2 : 0);
  594. if(!*pass2) return 0;
  595. } else if(pass2) *pass2 = NULL;
  596. return 1;
  597. }
  598. static char *app_get_pass(BIO *err, char *arg, int keepbio)
  599. {
  600. char *tmp, tpass[APP_PASS_LEN];
  601. static BIO *pwdbio = NULL;
  602. int i;
  603. if(!strncmp(arg, "pass:", 5)) return BUF_strdup(arg + 5);
  604. if(!strncmp(arg, "env:", 4)) {
  605. tmp = getenv(arg + 4);
  606. if(!tmp) {
  607. BIO_printf(err, "Can't read environment variable %s\n", arg + 4);
  608. return NULL;
  609. }
  610. return BUF_strdup(tmp);
  611. }
  612. if(!keepbio || !pwdbio) {
  613. if(!strncmp(arg, "file:", 5)) {
  614. pwdbio = BIO_new_file(arg + 5, "r");
  615. if(!pwdbio) {
  616. BIO_printf(err, "Can't open file %s\n", arg + 5);
  617. return NULL;
  618. }
  619. #if !defined(_WIN32)
  620. /*
  621. * Under _WIN32, which covers even Win64 and CE, file
  622. * descriptors referenced by BIO_s_fd are not inherited
  623. * by child process and therefore below is not an option.
  624. * It could have been an option if bss_fd.c was operating
  625. * on real Windows descriptors, such as those obtained
  626. * with CreateFile.
  627. */
  628. } else if(!strncmp(arg, "fd:", 3)) {
  629. BIO *btmp;
  630. i = atoi(arg + 3);
  631. if(i >= 0) pwdbio = BIO_new_fd(i, BIO_NOCLOSE);
  632. if((i < 0) || !pwdbio) {
  633. BIO_printf(err, "Can't access file descriptor %s\n", arg + 3);
  634. return NULL;
  635. }
  636. /* Can't do BIO_gets on an fd BIO so add a buffering BIO */
  637. btmp = BIO_new(BIO_f_buffer());
  638. pwdbio = BIO_push(btmp, pwdbio);
  639. #endif
  640. } else if(!strcmp(arg, "stdin")) {
  641. pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE);
  642. if(!pwdbio) {
  643. BIO_printf(err, "Can't open BIO for stdin\n");
  644. return NULL;
  645. }
  646. } else {
  647. BIO_printf(err, "Invalid password argument \"%s\"\n", arg);
  648. return NULL;
  649. }
  650. }
  651. i = BIO_gets(pwdbio, tpass, APP_PASS_LEN);
  652. if(keepbio != 1) {
  653. BIO_free_all(pwdbio);
  654. pwdbio = NULL;
  655. }
  656. if(i <= 0) {
  657. BIO_printf(err, "Error reading password from BIO\n");
  658. return NULL;
  659. }
  660. tmp = strchr(tpass, '\n');
  661. if(tmp) *tmp = 0;
  662. return BUF_strdup(tpass);
  663. }
  664. int add_oid_section(BIO *err, CONF *conf)
  665. {
  666. char *p;
  667. STACK_OF(CONF_VALUE) *sktmp;
  668. CONF_VALUE *cnf;
  669. int i;
  670. if(!(p=NCONF_get_string(conf,NULL,"oid_section")))
  671. {
  672. ERR_clear_error();
  673. return 1;
  674. }
  675. if(!(sktmp = NCONF_get_section(conf, p))) {
  676. BIO_printf(err, "problem loading oid section %s\n", p);
  677. return 0;
  678. }
  679. for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
  680. cnf = sk_CONF_VALUE_value(sktmp, i);
  681. if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
  682. BIO_printf(err, "problem creating object %s=%s\n",
  683. cnf->name, cnf->value);
  684. return 0;
  685. }
  686. }
  687. return 1;
  688. }
  689. static int load_pkcs12(BIO *err, BIO *in, const char *desc,
  690. pem_password_cb *pem_cb, void *cb_data,
  691. EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca)
  692. {
  693. const char *pass;
  694. char tpass[PEM_BUFSIZE];
  695. int len, ret = 0;
  696. PKCS12 *p12;
  697. p12 = d2i_PKCS12_bio(in, NULL);
  698. if (p12 == NULL)
  699. {
  700. BIO_printf(err, "Error loading PKCS12 file for %s\n", desc);
  701. goto die;
  702. }
  703. /* See if an empty password will do */
  704. if (PKCS12_verify_mac(p12, "", 0) || PKCS12_verify_mac(p12, NULL, 0))
  705. pass = "";
  706. else
  707. {
  708. if (!pem_cb)
  709. pem_cb = (pem_password_cb *)password_callback;
  710. len = pem_cb(tpass, PEM_BUFSIZE, 0, cb_data);
  711. if (len < 0)
  712. {
  713. BIO_printf(err, "Passpharse callback error for %s\n",
  714. desc);
  715. goto die;
  716. }
  717. if (len < PEM_BUFSIZE)
  718. tpass[len] = 0;
  719. if (!PKCS12_verify_mac(p12, tpass, len))
  720. {
  721. BIO_printf(err,
  722. "Mac verify error (wrong password?) in PKCS12 file for %s\n", desc);
  723. goto die;
  724. }
  725. pass = tpass;
  726. }
  727. ret = PKCS12_parse(p12, pass, pkey, cert, ca);
  728. die:
  729. if (p12)
  730. PKCS12_free(p12);
  731. return ret;
  732. }
  733. X509 *load_cert(BIO *err, const char *file, int format,
  734. const char *pass, ENGINE *e, const char *cert_descrip)
  735. {
  736. X509 *x=NULL;
  737. BIO *cert;
  738. if ((cert=BIO_new(BIO_s_file())) == NULL)
  739. {
  740. ERR_print_errors(err);
  741. goto end;
  742. }
  743. if (file == NULL)
  744. {
  745. #ifdef _IONBF
  746. setvbuf(stdin, NULL, _IONBF, 0);
  747. #endif
  748. BIO_set_fp(cert,stdin,BIO_NOCLOSE);
  749. }
  750. else
  751. {
  752. if (BIO_read_filename(cert,file) <= 0)
  753. {
  754. BIO_printf(err, "Error opening %s %s\n",
  755. cert_descrip, file);
  756. ERR_print_errors(err);
  757. goto end;
  758. }
  759. }
  760. if (format == FORMAT_ASN1)
  761. x=d2i_X509_bio(cert,NULL);
  762. else if (format == FORMAT_NETSCAPE)
  763. {
  764. NETSCAPE_X509 *nx;
  765. nx=ASN1_item_d2i_bio(ASN1_ITEM_rptr(NETSCAPE_X509),cert,NULL);
  766. if (nx == NULL)
  767. goto end;
  768. if ((strncmp(NETSCAPE_CERT_HDR,(char *)nx->header->data,
  769. nx->header->length) != 0))
  770. {
  771. NETSCAPE_X509_free(nx);
  772. BIO_printf(err,"Error reading header on certificate\n");
  773. goto end;
  774. }
  775. x=nx->cert;
  776. nx->cert = NULL;
  777. NETSCAPE_X509_free(nx);
  778. }
  779. else if (format == FORMAT_PEM)
  780. x=PEM_read_bio_X509_AUX(cert,NULL,
  781. (pem_password_cb *)password_callback, NULL);
  782. else if (format == FORMAT_PKCS12)
  783. {
  784. if (!load_pkcs12(err, cert,cert_descrip, NULL, NULL,
  785. NULL, &x, NULL))
  786. goto end;
  787. }
  788. else {
  789. BIO_printf(err,"bad input format specified for %s\n",
  790. cert_descrip);
  791. goto end;
  792. }
  793. end:
  794. if (x == NULL)
  795. {
  796. BIO_printf(err,"unable to load certificate\n");
  797. ERR_print_errors(err);
  798. }
  799. if (cert != NULL) BIO_free(cert);
  800. return(x);
  801. }
  802. EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin,
  803. const char *pass, ENGINE *e, const char *key_descrip)
  804. {
  805. BIO *key=NULL;
  806. EVP_PKEY *pkey=NULL;
  807. PW_CB_DATA cb_data;
  808. cb_data.password = pass;
  809. cb_data.prompt_info = file;
  810. if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
  811. {
  812. BIO_printf(err,"no keyfile specified\n");
  813. goto end;
  814. }
  815. #ifndef OPENSSL_NO_ENGINE
  816. if (format == FORMAT_ENGINE)
  817. {
  818. if (!e)
  819. BIO_printf(bio_err,"no engine specified\n");
  820. else
  821. pkey = ENGINE_load_private_key(e, file,
  822. ui_method, &cb_data);
  823. goto end;
  824. }
  825. #endif
  826. key=BIO_new(BIO_s_file());
  827. if (key == NULL)
  828. {
  829. ERR_print_errors(err);
  830. goto end;
  831. }
  832. if (file == NULL && maybe_stdin)
  833. {
  834. #ifdef _IONBF
  835. setvbuf(stdin, NULL, _IONBF, 0);
  836. #endif
  837. BIO_set_fp(key,stdin,BIO_NOCLOSE);
  838. }
  839. else
  840. if (BIO_read_filename(key,file) <= 0)
  841. {
  842. BIO_printf(err, "Error opening %s %s\n",
  843. key_descrip, file);
  844. ERR_print_errors(err);
  845. goto end;
  846. }
  847. if (format == FORMAT_ASN1)
  848. {
  849. pkey=d2i_PrivateKey_bio(key, NULL);
  850. }
  851. else if (format == FORMAT_PEM)
  852. {
  853. pkey=PEM_read_bio_PrivateKey(key,NULL,
  854. (pem_password_cb *)password_callback, &cb_data);
  855. }
  856. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  857. else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
  858. pkey = load_netscape_key(err, key, file, key_descrip, format);
  859. #endif
  860. else if (format == FORMAT_PKCS12)
  861. {
  862. if (!load_pkcs12(err, key, key_descrip,
  863. (pem_password_cb *)password_callback, &cb_data,
  864. &pkey, NULL, NULL))
  865. goto end;
  866. }
  867. #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) && !defined (OPENSSL_NO_RC4)
  868. else if (format == FORMAT_MSBLOB)
  869. pkey = b2i_PrivateKey_bio(key);
  870. else if (format == FORMAT_PVK)
  871. pkey = b2i_PVK_bio(key, (pem_password_cb *)password_callback,
  872. &cb_data);
  873. #endif
  874. else
  875. {
  876. BIO_printf(err,"bad input format specified for key file\n");
  877. goto end;
  878. }
  879. end:
  880. if (key != NULL) BIO_free(key);
  881. if (pkey == NULL)
  882. BIO_printf(err,"unable to load %s\n", key_descrip);
  883. return(pkey);
  884. }
  885. EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin,
  886. const char *pass, ENGINE *e, const char *key_descrip)
  887. {
  888. BIO *key=NULL;
  889. EVP_PKEY *pkey=NULL;
  890. PW_CB_DATA cb_data;
  891. cb_data.password = pass;
  892. cb_data.prompt_info = file;
  893. if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
  894. {
  895. BIO_printf(err,"no keyfile specified\n");
  896. goto end;
  897. }
  898. #ifndef OPENSSL_NO_ENGINE
  899. if (format == FORMAT_ENGINE)
  900. {
  901. if (!e)
  902. BIO_printf(bio_err,"no engine specified\n");
  903. else
  904. pkey = ENGINE_load_public_key(e, file,
  905. ui_method, &cb_data);
  906. goto end;
  907. }
  908. #endif
  909. key=BIO_new(BIO_s_file());
  910. if (key == NULL)
  911. {
  912. ERR_print_errors(err);
  913. goto end;
  914. }
  915. if (file == NULL && maybe_stdin)
  916. {
  917. #ifdef _IONBF
  918. setvbuf(stdin, NULL, _IONBF, 0);
  919. #endif
  920. BIO_set_fp(key,stdin,BIO_NOCLOSE);
  921. }
  922. else
  923. if (BIO_read_filename(key,file) <= 0)
  924. {
  925. BIO_printf(err, "Error opening %s %s\n",
  926. key_descrip, file);
  927. ERR_print_errors(err);
  928. goto end;
  929. }
  930. if (format == FORMAT_ASN1)
  931. {
  932. pkey=d2i_PUBKEY_bio(key, NULL);
  933. }
  934. #ifndef OPENSSL_NO_RSA
  935. else if (format == FORMAT_ASN1RSA)
  936. {
  937. RSA *rsa;
  938. rsa = d2i_RSAPublicKey_bio(key, NULL);
  939. if (rsa)
  940. {
  941. pkey = EVP_PKEY_new();
  942. if (pkey)
  943. EVP_PKEY_set1_RSA(pkey, rsa);
  944. RSA_free(rsa);
  945. }
  946. else
  947. pkey = NULL;
  948. }
  949. else if (format == FORMAT_PEMRSA)
  950. {
  951. RSA *rsa;
  952. rsa = PEM_read_bio_RSAPublicKey(key, NULL,
  953. (pem_password_cb *)password_callback, &cb_data);
  954. if (rsa)
  955. {
  956. pkey = EVP_PKEY_new();
  957. if (pkey)
  958. EVP_PKEY_set1_RSA(pkey, rsa);
  959. RSA_free(rsa);
  960. }
  961. else
  962. pkey = NULL;
  963. }
  964. #endif
  965. else if (format == FORMAT_PEM)
  966. {
  967. pkey=PEM_read_bio_PUBKEY(key,NULL,
  968. (pem_password_cb *)password_callback, &cb_data);
  969. }
  970. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  971. else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
  972. pkey = load_netscape_key(err, key, file, key_descrip, format);
  973. #endif
  974. #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
  975. else if (format == FORMAT_MSBLOB)
  976. pkey = b2i_PublicKey_bio(key);
  977. #endif
  978. else
  979. {
  980. BIO_printf(err,"bad input format specified for key file\n");
  981. goto end;
  982. }
  983. end:
  984. if (key != NULL) BIO_free(key);
  985. if (pkey == NULL)
  986. BIO_printf(err,"unable to load %s\n", key_descrip);
  987. return(pkey);
  988. }
  989. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  990. static EVP_PKEY *
  991. load_netscape_key(BIO *err, BIO *key, const char *file,
  992. const char *key_descrip, int format)
  993. {
  994. EVP_PKEY *pkey;
  995. BUF_MEM *buf;
  996. RSA *rsa;
  997. const unsigned char *p;
  998. int size, i;
  999. buf=BUF_MEM_new();
  1000. pkey = EVP_PKEY_new();
  1001. size = 0;
  1002. if (buf == NULL || pkey == NULL)
  1003. goto error;
  1004. for (;;)
  1005. {
  1006. if (!BUF_MEM_grow_clean(buf,size+1024*10))
  1007. goto error;
  1008. i = BIO_read(key, &(buf->data[size]), 1024*10);
  1009. size += i;
  1010. if (i == 0)
  1011. break;
  1012. if (i < 0)
  1013. {
  1014. BIO_printf(err, "Error reading %s %s",
  1015. key_descrip, file);
  1016. goto error;
  1017. }
  1018. }
  1019. p=(unsigned char *)buf->data;
  1020. rsa = d2i_RSA_NET(NULL,&p,(long)size,NULL,
  1021. (format == FORMAT_IISSGC ? 1 : 0));
  1022. if (rsa == NULL)
  1023. goto error;
  1024. BUF_MEM_free(buf);
  1025. EVP_PKEY_set1_RSA(pkey, rsa);
  1026. return pkey;
  1027. error:
  1028. BUF_MEM_free(buf);
  1029. EVP_PKEY_free(pkey);
  1030. return NULL;
  1031. }
  1032. #endif /* ndef OPENSSL_NO_RC4 */
  1033. static int load_certs_crls(BIO *err, const char *file, int format,
  1034. const char *pass, ENGINE *e, const char *desc,
  1035. STACK_OF(X509) **pcerts, STACK_OF(X509_CRL) **pcrls)
  1036. {
  1037. int i;
  1038. BIO *bio;
  1039. STACK_OF(X509_INFO) *xis = NULL;
  1040. X509_INFO *xi;
  1041. PW_CB_DATA cb_data;
  1042. int rv = 0;
  1043. cb_data.password = pass;
  1044. cb_data.prompt_info = file;
  1045. if (format != FORMAT_PEM)
  1046. {
  1047. BIO_printf(err,"bad input format specified for %s\n", desc);
  1048. return 0;
  1049. }
  1050. if (file == NULL)
  1051. bio = BIO_new_fp(stdin,BIO_NOCLOSE);
  1052. else
  1053. bio = BIO_new_file(file, "r");
  1054. if (bio == NULL)
  1055. {
  1056. BIO_printf(err, "Error opening %s %s\n",
  1057. desc, file ? file : "stdin");
  1058. ERR_print_errors(err);
  1059. return 0;
  1060. }
  1061. xis = PEM_X509_INFO_read_bio(bio, NULL,
  1062. (pem_password_cb *)password_callback, &cb_data);
  1063. BIO_free(bio);
  1064. if (pcerts)
  1065. {
  1066. *pcerts = sk_X509_new_null();
  1067. if (!*pcerts)
  1068. goto end;
  1069. }
  1070. if (pcrls)
  1071. {
  1072. *pcrls = sk_X509_CRL_new_null();
  1073. if (!*pcrls)
  1074. goto end;
  1075. }
  1076. for(i = 0; i < sk_X509_INFO_num(xis); i++)
  1077. {
  1078. xi = sk_X509_INFO_value (xis, i);
  1079. if (xi->x509 && pcerts)
  1080. {
  1081. if (!sk_X509_push(*pcerts, xi->x509))
  1082. goto end;
  1083. xi->x509 = NULL;
  1084. }
  1085. if (xi->crl && pcrls)
  1086. {
  1087. if (!sk_X509_CRL_push(*pcrls, xi->crl))
  1088. goto end;
  1089. xi->crl = NULL;
  1090. }
  1091. }
  1092. if (pcerts && sk_X509_num(*pcerts) > 0)
  1093. rv = 1;
  1094. if (pcrls && sk_X509_CRL_num(*pcrls) > 0)
  1095. rv = 1;
  1096. end:
  1097. if (xis)
  1098. sk_X509_INFO_pop_free(xis, X509_INFO_free);
  1099. if (rv == 0)
  1100. {
  1101. if (pcerts)
  1102. {
  1103. sk_X509_pop_free(*pcerts, X509_free);
  1104. *pcerts = NULL;
  1105. }
  1106. if (pcrls)
  1107. {
  1108. sk_X509_CRL_pop_free(*pcrls, X509_CRL_free);
  1109. *pcrls = NULL;
  1110. }
  1111. BIO_printf(err,"unable to load %s\n",
  1112. pcerts ? "certificates" : "CRLs");
  1113. ERR_print_errors(err);
  1114. }
  1115. return rv;
  1116. }
  1117. STACK_OF(X509) *load_certs(BIO *err, const char *file, int format,
  1118. const char *pass, ENGINE *e, const char *desc)
  1119. {
  1120. STACK_OF(X509) *certs;
  1121. load_certs_crls(err, file, format, pass, e, desc, &certs, NULL);
  1122. return certs;
  1123. }
  1124. STACK_OF(X509_CRL) *load_crls(BIO *err, const char *file, int format,
  1125. const char *pass, ENGINE *e, const char *desc)
  1126. {
  1127. STACK_OF(X509_CRL) *crls;
  1128. load_certs_crls(err, file, format, pass, e, desc, NULL, &crls);
  1129. return crls;
  1130. }
  1131. #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
  1132. /* Return error for unknown extensions */
  1133. #define X509V3_EXT_DEFAULT 0
  1134. /* Print error for unknown extensions */
  1135. #define X509V3_EXT_ERROR_UNKNOWN (1L << 16)
  1136. /* ASN1 parse unknown extensions */
  1137. #define X509V3_EXT_PARSE_UNKNOWN (2L << 16)
  1138. /* BIO_dump unknown extensions */
  1139. #define X509V3_EXT_DUMP_UNKNOWN (3L << 16)
  1140. #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \
  1141. X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION)
  1142. int set_cert_ex(unsigned long *flags, const char *arg)
  1143. {
  1144. static const NAME_EX_TBL cert_tbl[] = {
  1145. { "compatible", X509_FLAG_COMPAT, 0xffffffffl},
  1146. { "ca_default", X509_FLAG_CA, 0xffffffffl},
  1147. { "no_header", X509_FLAG_NO_HEADER, 0},
  1148. { "no_version", X509_FLAG_NO_VERSION, 0},
  1149. { "no_serial", X509_FLAG_NO_SERIAL, 0},
  1150. { "no_signame", X509_FLAG_NO_SIGNAME, 0},
  1151. { "no_validity", X509_FLAG_NO_VALIDITY, 0},
  1152. { "no_subject", X509_FLAG_NO_SUBJECT, 0},
  1153. { "no_issuer", X509_FLAG_NO_ISSUER, 0},
  1154. { "no_pubkey", X509_FLAG_NO_PUBKEY, 0},
  1155. { "no_extensions", X509_FLAG_NO_EXTENSIONS, 0},
  1156. { "no_sigdump", X509_FLAG_NO_SIGDUMP, 0},
  1157. { "no_aux", X509_FLAG_NO_AUX, 0},
  1158. { "no_attributes", X509_FLAG_NO_ATTRIBUTES, 0},
  1159. { "ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK},
  1160. { "ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1161. { "ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1162. { "ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1163. { NULL, 0, 0}
  1164. };
  1165. return set_multi_opts(flags, arg, cert_tbl);
  1166. }
  1167. int set_name_ex(unsigned long *flags, const char *arg)
  1168. {
  1169. static const NAME_EX_TBL ex_tbl[] = {
  1170. { "esc_2253", ASN1_STRFLGS_ESC_2253, 0},
  1171. { "esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0},
  1172. { "esc_msb", ASN1_STRFLGS_ESC_MSB, 0},
  1173. { "use_quote", ASN1_STRFLGS_ESC_QUOTE, 0},
  1174. { "utf8", ASN1_STRFLGS_UTF8_CONVERT, 0},
  1175. { "ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0},
  1176. { "show_type", ASN1_STRFLGS_SHOW_TYPE, 0},
  1177. { "dump_all", ASN1_STRFLGS_DUMP_ALL, 0},
  1178. { "dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0},
  1179. { "dump_der", ASN1_STRFLGS_DUMP_DER, 0},
  1180. { "compat", XN_FLAG_COMPAT, 0xffffffffL},
  1181. { "sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK},
  1182. { "sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK},
  1183. { "sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK},
  1184. { "sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK},
  1185. { "dn_rev", XN_FLAG_DN_REV, 0},
  1186. { "nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK},
  1187. { "sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK},
  1188. { "lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK},
  1189. { "align", XN_FLAG_FN_ALIGN, 0},
  1190. { "oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK},
  1191. { "space_eq", XN_FLAG_SPC_EQ, 0},
  1192. { "dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0},
  1193. { "RFC2253", XN_FLAG_RFC2253, 0xffffffffL},
  1194. { "oneline", XN_FLAG_ONELINE, 0xffffffffL},
  1195. { "multiline", XN_FLAG_MULTILINE, 0xffffffffL},
  1196. { "ca_default", XN_FLAG_MULTILINE, 0xffffffffL},
  1197. { NULL, 0, 0}
  1198. };
  1199. return set_multi_opts(flags, arg, ex_tbl);
  1200. }
  1201. int set_ext_copy(int *copy_type, const char *arg)
  1202. {
  1203. if (!strcasecmp(arg, "none"))
  1204. *copy_type = EXT_COPY_NONE;
  1205. else if (!strcasecmp(arg, "copy"))
  1206. *copy_type = EXT_COPY_ADD;
  1207. else if (!strcasecmp(arg, "copyall"))
  1208. *copy_type = EXT_COPY_ALL;
  1209. else
  1210. return 0;
  1211. return 1;
  1212. }
  1213. int copy_extensions(X509 *x, X509_REQ *req, int copy_type)
  1214. {
  1215. STACK_OF(X509_EXTENSION) *exts = NULL;
  1216. X509_EXTENSION *ext, *tmpext;
  1217. ASN1_OBJECT *obj;
  1218. int i, idx, ret = 0;
  1219. if (!x || !req || (copy_type == EXT_COPY_NONE))
  1220. return 1;
  1221. exts = X509_REQ_get_extensions(req);
  1222. for(i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  1223. ext = sk_X509_EXTENSION_value(exts, i);
  1224. obj = X509_EXTENSION_get_object(ext);
  1225. idx = X509_get_ext_by_OBJ(x, obj, -1);
  1226. /* Does extension exist? */
  1227. if (idx != -1) {
  1228. /* If normal copy don't override existing extension */
  1229. if (copy_type == EXT_COPY_ADD)
  1230. continue;
  1231. /* Delete all extensions of same type */
  1232. do {
  1233. tmpext = X509_get_ext(x, idx);
  1234. X509_delete_ext(x, idx);
  1235. X509_EXTENSION_free(tmpext);
  1236. idx = X509_get_ext_by_OBJ(x, obj, -1);
  1237. } while (idx != -1);
  1238. }
  1239. if (!X509_add_ext(x, ext, -1))
  1240. goto end;
  1241. }
  1242. ret = 1;
  1243. end:
  1244. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  1245. return ret;
  1246. }
  1247. static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
  1248. {
  1249. STACK_OF(CONF_VALUE) *vals;
  1250. CONF_VALUE *val;
  1251. int i, ret = 1;
  1252. if(!arg) return 0;
  1253. vals = X509V3_parse_list(arg);
  1254. for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
  1255. val = sk_CONF_VALUE_value(vals, i);
  1256. if (!set_table_opts(flags, val->name, in_tbl))
  1257. ret = 0;
  1258. }
  1259. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  1260. return ret;
  1261. }
  1262. static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
  1263. {
  1264. char c;
  1265. const NAME_EX_TBL *ptbl;
  1266. c = arg[0];
  1267. if(c == '-') {
  1268. c = 0;
  1269. arg++;
  1270. } else if (c == '+') {
  1271. c = 1;
  1272. arg++;
  1273. } else c = 1;
  1274. for(ptbl = in_tbl; ptbl->name; ptbl++) {
  1275. if(!strcasecmp(arg, ptbl->name)) {
  1276. *flags &= ~ptbl->mask;
  1277. if(c) *flags |= ptbl->flag;
  1278. else *flags &= ~ptbl->flag;
  1279. return 1;
  1280. }
  1281. }
  1282. return 0;
  1283. }
  1284. void print_name(BIO *out, const char *title, X509_NAME *nm, unsigned long lflags)
  1285. {
  1286. char *buf;
  1287. char mline = 0;
  1288. int indent = 0;
  1289. if(title) BIO_puts(out, title);
  1290. if((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
  1291. mline = 1;
  1292. indent = 4;
  1293. }
  1294. if(lflags == XN_FLAG_COMPAT) {
  1295. buf = X509_NAME_oneline(nm, 0, 0);
  1296. BIO_puts(out, buf);
  1297. BIO_puts(out, "\n");
  1298. OPENSSL_free(buf);
  1299. } else {
  1300. if(mline) BIO_puts(out, "\n");
  1301. X509_NAME_print_ex(out, nm, indent, lflags);
  1302. BIO_puts(out, "\n");
  1303. }
  1304. }
  1305. X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath)
  1306. {
  1307. X509_STORE *store;
  1308. X509_LOOKUP *lookup;
  1309. if(!(store = X509_STORE_new())) goto end;
  1310. lookup=X509_STORE_add_lookup(store,X509_LOOKUP_file());
  1311. if (lookup == NULL) goto end;
  1312. if (CAfile) {
  1313. if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
  1314. BIO_printf(bp, "Error loading file %s\n", CAfile);
  1315. goto end;
  1316. }
  1317. } else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
  1318. lookup=X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
  1319. if (lookup == NULL) goto end;
  1320. if (CApath) {
  1321. if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
  1322. BIO_printf(bp, "Error loading directory %s\n", CApath);
  1323. goto end;
  1324. }
  1325. } else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
  1326. ERR_clear_error();
  1327. return store;
  1328. end:
  1329. X509_STORE_free(store);
  1330. return NULL;
  1331. }
  1332. #ifndef OPENSSL_NO_ENGINE
  1333. /* Try to load an engine in a shareable library */
  1334. static ENGINE *try_load_engine(BIO *err, const char *engine, int debug)
  1335. {
  1336. ENGINE *e = ENGINE_by_id("dynamic");
  1337. if (e)
  1338. {
  1339. if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0)
  1340. || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
  1341. {
  1342. ENGINE_free(e);
  1343. e = NULL;
  1344. }
  1345. }
  1346. return e;
  1347. }
  1348. ENGINE *setup_engine(BIO *err, const char *engine, int debug)
  1349. {
  1350. ENGINE *e = NULL;
  1351. if (engine)
  1352. {
  1353. if(strcmp(engine, "auto") == 0)
  1354. {
  1355. BIO_printf(err,"enabling auto ENGINE support\n");
  1356. ENGINE_register_all_complete();
  1357. return NULL;
  1358. }
  1359. if((e = ENGINE_by_id(engine)) == NULL
  1360. && (e = try_load_engine(err, engine, debug)) == NULL)
  1361. {
  1362. BIO_printf(err,"invalid engine \"%s\"\n", engine);
  1363. ERR_print_errors(err);
  1364. return NULL;
  1365. }
  1366. if (debug)
  1367. {
  1368. ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM,
  1369. 0, err, 0);
  1370. }
  1371. ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, ui_method, 0, 1);
  1372. if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
  1373. {
  1374. BIO_printf(err,"can't use that engine\n");
  1375. ERR_print_errors(err);
  1376. ENGINE_free(e);
  1377. return NULL;
  1378. }
  1379. BIO_printf(err,"engine \"%s\" set.\n", ENGINE_get_id(e));
  1380. /* Free our "structural" reference. */
  1381. ENGINE_free(e);
  1382. }
  1383. return e;
  1384. }
  1385. #endif
  1386. int load_config(BIO *err, CONF *cnf)
  1387. {
  1388. static int load_config_called = 0;
  1389. if (load_config_called)
  1390. return 1;
  1391. load_config_called = 1;
  1392. if (!cnf)
  1393. cnf = config;
  1394. if (!cnf)
  1395. return 1;
  1396. OPENSSL_load_builtin_modules();
  1397. if (CONF_modules_load(cnf, NULL, 0) <= 0)
  1398. {
  1399. BIO_printf(err, "Error configuring OpenSSL\n");
  1400. ERR_print_errors(err);
  1401. return 0;
  1402. }
  1403. return 1;
  1404. }
  1405. char *make_config_name()
  1406. {
  1407. const char *t=X509_get_default_cert_area();
  1408. size_t len;
  1409. char *p;
  1410. len=strlen(t)+strlen(OPENSSL_CONF)+2;
  1411. p=OPENSSL_malloc(len);
  1412. BUF_strlcpy(p,t,len);
  1413. #ifndef OPENSSL_SYS_VMS
  1414. BUF_strlcat(p,"/",len);
  1415. #endif
  1416. BUF_strlcat(p,OPENSSL_CONF,len);
  1417. return p;
  1418. }
  1419. static unsigned long index_serial_hash(const OPENSSL_CSTRING *a)
  1420. {
  1421. const char *n;
  1422. n=a[DB_serial];
  1423. while (*n == '0') n++;
  1424. return(lh_strhash(n));
  1425. }
  1426. static int index_serial_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b)
  1427. {
  1428. const char *aa,*bb;
  1429. for (aa=a[DB_serial]; *aa == '0'; aa++);
  1430. for (bb=b[DB_serial]; *bb == '0'; bb++);
  1431. return(strcmp(aa,bb));
  1432. }
  1433. static int index_name_qual(char **a)
  1434. { return(a[0][0] == 'V'); }
  1435. static unsigned long index_name_hash(const OPENSSL_CSTRING *a)
  1436. { return(lh_strhash(a[DB_name])); }
  1437. int index_name_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b)
  1438. { return(strcmp(a[DB_name], b[DB_name])); }
  1439. static IMPLEMENT_LHASH_HASH_FN(index_serial, OPENSSL_CSTRING)
  1440. static IMPLEMENT_LHASH_COMP_FN(index_serial, OPENSSL_CSTRING)
  1441. static IMPLEMENT_LHASH_HASH_FN(index_name, OPENSSL_CSTRING)
  1442. static IMPLEMENT_LHASH_COMP_FN(index_name, OPENSSL_CSTRING)
  1443. #undef BSIZE
  1444. #define BSIZE 256
  1445. BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai)
  1446. {
  1447. BIO *in=NULL;
  1448. BIGNUM *ret=NULL;
  1449. MS_STATIC char buf[1024];
  1450. ASN1_INTEGER *ai=NULL;
  1451. ai=ASN1_INTEGER_new();
  1452. if (ai == NULL) goto err;
  1453. if ((in=BIO_new(BIO_s_file())) == NULL)
  1454. {
  1455. ERR_print_errors(bio_err);
  1456. goto err;
  1457. }
  1458. if (BIO_read_filename(in,serialfile) <= 0)
  1459. {
  1460. if (!create)
  1461. {
  1462. perror(serialfile);
  1463. goto err;
  1464. }
  1465. else
  1466. {
  1467. ret=BN_new();
  1468. if (ret == NULL || !rand_serial(ret, ai))
  1469. BIO_printf(bio_err, "Out of memory\n");
  1470. }
  1471. }
  1472. else
  1473. {
  1474. if (!a2i_ASN1_INTEGER(in,ai,buf,1024))
  1475. {
  1476. BIO_printf(bio_err,"unable to load number from %s\n",
  1477. serialfile);
  1478. goto err;
  1479. }
  1480. ret=ASN1_INTEGER_to_BN(ai,NULL);
  1481. if (ret == NULL)
  1482. {
  1483. BIO_printf(bio_err,"error converting number from bin to BIGNUM\n");
  1484. goto err;
  1485. }
  1486. }
  1487. if (ret && retai)
  1488. {
  1489. *retai = ai;
  1490. ai = NULL;
  1491. }
  1492. err:
  1493. if (in != NULL) BIO_free(in);
  1494. if (ai != NULL) ASN1_INTEGER_free(ai);
  1495. return(ret);
  1496. }
  1497. int save_serial(char *serialfile, char *suffix, BIGNUM *serial, ASN1_INTEGER **retai)
  1498. {
  1499. char buf[1][BSIZE];
  1500. BIO *out = NULL;
  1501. int ret=0;
  1502. ASN1_INTEGER *ai=NULL;
  1503. int j;
  1504. if (suffix == NULL)
  1505. j = strlen(serialfile);
  1506. else
  1507. j = strlen(serialfile) + strlen(suffix) + 1;
  1508. if (j >= BSIZE)
  1509. {
  1510. BIO_printf(bio_err,"file name too long\n");
  1511. goto err;
  1512. }
  1513. if (suffix == NULL)
  1514. BUF_strlcpy(buf[0], serialfile, BSIZE);
  1515. else
  1516. {
  1517. #ifndef OPENSSL_SYS_VMS
  1518. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", serialfile, suffix);
  1519. #else
  1520. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", serialfile, suffix);
  1521. #endif
  1522. }
  1523. #ifdef RL_DEBUG
  1524. BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
  1525. #endif
  1526. out=BIO_new(BIO_s_file());
  1527. if (out == NULL)
  1528. {
  1529. ERR_print_errors(bio_err);
  1530. goto err;
  1531. }
  1532. if (BIO_write_filename(out,buf[0]) <= 0)
  1533. {
  1534. perror(serialfile);
  1535. goto err;
  1536. }
  1537. if ((ai=BN_to_ASN1_INTEGER(serial,NULL)) == NULL)
  1538. {
  1539. BIO_printf(bio_err,"error converting serial to ASN.1 format\n");
  1540. goto err;
  1541. }
  1542. i2a_ASN1_INTEGER(out,ai);
  1543. BIO_puts(out,"\n");
  1544. ret=1;
  1545. if (retai)
  1546. {
  1547. *retai = ai;
  1548. ai = NULL;
  1549. }
  1550. err:
  1551. if (out != NULL) BIO_free_all(out);
  1552. if (ai != NULL) ASN1_INTEGER_free(ai);
  1553. return(ret);
  1554. }
  1555. int rotate_serial(char *serialfile, char *new_suffix, char *old_suffix)
  1556. {
  1557. char buf[5][BSIZE];
  1558. int i,j;
  1559. i = strlen(serialfile) + strlen(old_suffix);
  1560. j = strlen(serialfile) + strlen(new_suffix);
  1561. if (i > j) j = i;
  1562. if (j + 1 >= BSIZE)
  1563. {
  1564. BIO_printf(bio_err,"file name too long\n");
  1565. goto err;
  1566. }
  1567. #ifndef OPENSSL_SYS_VMS
  1568. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s",
  1569. serialfile, new_suffix);
  1570. #else
  1571. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s",
  1572. serialfile, new_suffix);
  1573. #endif
  1574. #ifndef OPENSSL_SYS_VMS
  1575. j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s",
  1576. serialfile, old_suffix);
  1577. #else
  1578. j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s",
  1579. serialfile, old_suffix);
  1580. #endif
  1581. #ifdef RL_DEBUG
  1582. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1583. serialfile, buf[1]);
  1584. #endif
  1585. if (rename(serialfile,buf[1]) < 0 && errno != ENOENT
  1586. #ifdef ENOTDIR
  1587. && errno != ENOTDIR
  1588. #endif
  1589. ) {
  1590. BIO_printf(bio_err,
  1591. "unable to rename %s to %s\n",
  1592. serialfile, buf[1]);
  1593. perror("reason");
  1594. goto err;
  1595. }
  1596. #ifdef RL_DEBUG
  1597. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1598. buf[0],serialfile);
  1599. #endif
  1600. if (rename(buf[0],serialfile) < 0)
  1601. {
  1602. BIO_printf(bio_err,
  1603. "unable to rename %s to %s\n",
  1604. buf[0],serialfile);
  1605. perror("reason");
  1606. rename(buf[1],serialfile);
  1607. goto err;
  1608. }
  1609. return 1;
  1610. err:
  1611. return 0;
  1612. }
  1613. int rand_serial(BIGNUM *b, ASN1_INTEGER *ai)
  1614. {
  1615. BIGNUM *btmp;
  1616. int ret = 0;
  1617. if (b)
  1618. btmp = b;
  1619. else
  1620. btmp = BN_new();
  1621. if (!btmp)
  1622. return 0;
  1623. if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0))
  1624. goto error;
  1625. if (ai && !BN_to_ASN1_INTEGER(btmp, ai))
  1626. goto error;
  1627. ret = 1;
  1628. error:
  1629. if (!b)
  1630. BN_free(btmp);
  1631. return ret;
  1632. }
  1633. CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
  1634. {
  1635. CA_DB *retdb = NULL;
  1636. TXT_DB *tmpdb = NULL;
  1637. BIO *in = BIO_new(BIO_s_file());
  1638. CONF *dbattr_conf = NULL;
  1639. char buf[1][BSIZE];
  1640. long errorline= -1;
  1641. if (in == NULL)
  1642. {
  1643. ERR_print_errors(bio_err);
  1644. goto err;
  1645. }
  1646. if (BIO_read_filename(in,dbfile) <= 0)
  1647. {
  1648. perror(dbfile);
  1649. BIO_printf(bio_err,"unable to open '%s'\n",dbfile);
  1650. goto err;
  1651. }
  1652. if ((tmpdb = TXT_DB_read(in,DB_NUMBER)) == NULL)
  1653. goto err;
  1654. #ifndef OPENSSL_SYS_VMS
  1655. BIO_snprintf(buf[0], sizeof buf[0], "%s.attr", dbfile);
  1656. #else
  1657. BIO_snprintf(buf[0], sizeof buf[0], "%s-attr", dbfile);
  1658. #endif
  1659. dbattr_conf = NCONF_new(NULL);
  1660. if (NCONF_load(dbattr_conf,buf[0],&errorline) <= 0)
  1661. {
  1662. if (errorline > 0)
  1663. {
  1664. BIO_printf(bio_err,
  1665. "error on line %ld of db attribute file '%s'\n"
  1666. ,errorline,buf[0]);
  1667. goto err;
  1668. }
  1669. else
  1670. {
  1671. NCONF_free(dbattr_conf);
  1672. dbattr_conf = NULL;
  1673. }
  1674. }
  1675. if ((retdb = OPENSSL_malloc(sizeof(CA_DB))) == NULL)
  1676. {
  1677. fprintf(stderr, "Out of memory\n");
  1678. goto err;
  1679. }
  1680. retdb->db = tmpdb;
  1681. tmpdb = NULL;
  1682. if (db_attr)
  1683. retdb->attributes = *db_attr;
  1684. else
  1685. {
  1686. retdb->attributes.unique_subject = 1;
  1687. }
  1688. if (dbattr_conf)
  1689. {
  1690. char *p = NCONF_get_string(dbattr_conf,NULL,"unique_subject");
  1691. if (p)
  1692. {
  1693. #ifdef RL_DEBUG
  1694. BIO_printf(bio_err, "DEBUG[load_index]: unique_subject = \"%s\"\n", p);
  1695. #endif
  1696. retdb->attributes.unique_subject = parse_yesno(p,1);
  1697. }
  1698. }
  1699. err:
  1700. if (dbattr_conf) NCONF_free(dbattr_conf);
  1701. if (tmpdb) TXT_DB_free(tmpdb);
  1702. if (in) BIO_free_all(in);
  1703. return retdb;
  1704. }
  1705. int index_index(CA_DB *db)
  1706. {
  1707. if (!TXT_DB_create_index(db->db, DB_serial, NULL,
  1708. LHASH_HASH_FN(index_serial),
  1709. LHASH_COMP_FN(index_serial)))
  1710. {
  1711. BIO_printf(bio_err,
  1712. "error creating serial number index:(%ld,%ld,%ld)\n",
  1713. db->db->error,db->db->arg1,db->db->arg2);
  1714. return 0;
  1715. }
  1716. if (db->attributes.unique_subject
  1717. && !TXT_DB_create_index(db->db, DB_name, index_name_qual,
  1718. LHASH_HASH_FN(index_name),
  1719. LHASH_COMP_FN(index_name)))
  1720. {
  1721. BIO_printf(bio_err,"error creating name index:(%ld,%ld,%ld)\n",
  1722. db->db->error,db->db->arg1,db->db->arg2);
  1723. return 0;
  1724. }
  1725. return 1;
  1726. }
  1727. int save_index(const char *dbfile, const char *suffix, CA_DB *db)
  1728. {
  1729. char buf[3][BSIZE];
  1730. BIO *out = BIO_new(BIO_s_file());
  1731. int j;
  1732. if (out == NULL)
  1733. {
  1734. ERR_print_errors(bio_err);
  1735. goto err;
  1736. }
  1737. j = strlen(dbfile) + strlen(suffix);
  1738. if (j + 6 >= BSIZE)
  1739. {
  1740. BIO_printf(bio_err,"file name too long\n");
  1741. goto err;
  1742. }
  1743. #ifndef OPENSSL_SYS_VMS
  1744. j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr", dbfile);
  1745. #else
  1746. j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr", dbfile);
  1747. #endif
  1748. #ifndef OPENSSL_SYS_VMS
  1749. j = BIO_snprintf(buf[1], sizeof buf[1], "%s.attr.%s", dbfile, suffix);
  1750. #else
  1751. j = BIO_snprintf(buf[1], sizeof buf[1], "%s-attr-%s", dbfile, suffix);
  1752. #endif
  1753. #ifndef OPENSSL_SYS_VMS
  1754. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", dbfile, suffix);
  1755. #else
  1756. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", dbfile, suffix);
  1757. #endif
  1758. #ifdef RL_DEBUG
  1759. BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
  1760. #endif
  1761. if (BIO_write_filename(out,buf[0]) <= 0)
  1762. {
  1763. perror(dbfile);
  1764. BIO_printf(bio_err,"unable to open '%s'\n", dbfile);
  1765. goto err;
  1766. }
  1767. j=TXT_DB_write(out,db->db);
  1768. if (j <= 0) goto err;
  1769. BIO_free(out);
  1770. out = BIO_new(BIO_s_file());
  1771. #ifdef RL_DEBUG
  1772. BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[1]);
  1773. #endif
  1774. if (BIO_write_filename(out,buf[1]) <= 0)
  1775. {
  1776. perror(buf[2]);
  1777. BIO_printf(bio_err,"unable to open '%s'\n", buf[2]);
  1778. goto err;
  1779. }
  1780. BIO_printf(out,"unique_subject = %s\n",
  1781. db->attributes.unique_subject ? "yes" : "no");
  1782. BIO_free(out);
  1783. return 1;
  1784. err:
  1785. return 0;
  1786. }
  1787. int rotate_index(const char *dbfile, const char *new_suffix, const char *old_suffix)
  1788. {
  1789. char buf[5][BSIZE];
  1790. int i,j;
  1791. i = strlen(dbfile) + strlen(old_suffix);
  1792. j = strlen(dbfile) + strlen(new_suffix);
  1793. if (i > j) j = i;
  1794. if (j + 6 >= BSIZE)
  1795. {
  1796. BIO_printf(bio_err,"file name too long\n");
  1797. goto err;
  1798. }
  1799. #ifndef OPENSSL_SYS_VMS
  1800. j = BIO_snprintf(buf[4], sizeof buf[4], "%s.attr", dbfile);
  1801. #else
  1802. j = BIO_snprintf(buf[4], sizeof buf[4], "%s-attr", dbfile);
  1803. #endif
  1804. #ifndef OPENSSL_SYS_VMS
  1805. j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr.%s",
  1806. dbfile, new_suffix);
  1807. #else
  1808. j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr-%s",
  1809. dbfile, new_suffix);
  1810. #endif
  1811. #ifndef OPENSSL_SYS_VMS
  1812. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s",
  1813. dbfile, new_suffix);
  1814. #else
  1815. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s",
  1816. dbfile, new_suffix);
  1817. #endif
  1818. #ifndef OPENSSL_SYS_VMS
  1819. j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s",
  1820. dbfile, old_suffix);
  1821. #else
  1822. j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s",
  1823. dbfile, old_suffix);
  1824. #endif
  1825. #ifndef OPENSSL_SYS_VMS
  1826. j = BIO_snprintf(buf[3], sizeof buf[3], "%s.attr.%s",
  1827. dbfile, old_suffix);
  1828. #else
  1829. j = BIO_snprintf(buf[3], sizeof buf[3], "%s-attr-%s",
  1830. dbfile, old_suffix);
  1831. #endif
  1832. #ifdef RL_DEBUG
  1833. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1834. dbfile, buf[1]);
  1835. #endif
  1836. if (rename(dbfile,buf[1]) < 0 && errno != ENOENT
  1837. #ifdef ENOTDIR
  1838. && errno != ENOTDIR
  1839. #endif
  1840. ) {
  1841. BIO_printf(bio_err,
  1842. "unable to rename %s to %s\n",
  1843. dbfile, buf[1]);
  1844. perror("reason");
  1845. goto err;
  1846. }
  1847. #ifdef RL_DEBUG
  1848. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1849. buf[0],dbfile);
  1850. #endif
  1851. if (rename(buf[0],dbfile) < 0)
  1852. {
  1853. BIO_printf(bio_err,
  1854. "unable to rename %s to %s\n",
  1855. buf[0],dbfile);
  1856. perror("reason");
  1857. rename(buf[1],dbfile);
  1858. goto err;
  1859. }
  1860. #ifdef RL_DEBUG
  1861. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1862. buf[4],buf[3]);
  1863. #endif
  1864. if (rename(buf[4],buf[3]) < 0 && errno != ENOENT
  1865. #ifdef ENOTDIR
  1866. && errno != ENOTDIR
  1867. #endif
  1868. ) {
  1869. BIO_printf(bio_err,
  1870. "unable to rename %s to %s\n",
  1871. buf[4], buf[3]);
  1872. perror("reason");
  1873. rename(dbfile,buf[0]);
  1874. rename(buf[1],dbfile);
  1875. goto err;
  1876. }
  1877. #ifdef RL_DEBUG
  1878. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1879. buf[2],buf[4]);
  1880. #endif
  1881. if (rename(buf[2],buf[4]) < 0)
  1882. {
  1883. BIO_printf(bio_err,
  1884. "unable to rename %s to %s\n",
  1885. buf[2],buf[4]);
  1886. perror("reason");
  1887. rename(buf[3],buf[4]);
  1888. rename(dbfile,buf[0]);
  1889. rename(buf[1],dbfile);
  1890. goto err;
  1891. }
  1892. return 1;
  1893. err:
  1894. return 0;
  1895. }
  1896. void free_index(CA_DB *db)
  1897. {
  1898. if (db)
  1899. {
  1900. if (db->db) TXT_DB_free(db->db);
  1901. OPENSSL_free(db);
  1902. }
  1903. }
  1904. int parse_yesno(const char *str, int def)
  1905. {
  1906. int ret = def;
  1907. if (str)
  1908. {
  1909. switch (*str)
  1910. {
  1911. case 'f': /* false */
  1912. case 'F': /* FALSE */
  1913. case 'n': /* no */
  1914. case 'N': /* NO */
  1915. case '0': /* 0 */
  1916. ret = 0;
  1917. break;
  1918. case 't': /* true */
  1919. case 'T': /* TRUE */
  1920. case 'y': /* yes */
  1921. case 'Y': /* YES */
  1922. case '1': /* 1 */
  1923. ret = 1;
  1924. break;
  1925. default:
  1926. ret = def;
  1927. break;
  1928. }
  1929. }
  1930. return ret;
  1931. }
  1932. /*
  1933. * subject is expected to be in the format /type0=value0/type1=value1/type2=...
  1934. * where characters may be escaped by \
  1935. */
  1936. X509_NAME *parse_name(char *subject, long chtype, int multirdn)
  1937. {
  1938. size_t buflen = strlen(subject)+1; /* to copy the types and values into. due to escaping, the copy can only become shorter */
  1939. char *buf = OPENSSL_malloc(buflen);
  1940. size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */
  1941. char **ne_types = OPENSSL_malloc(max_ne * sizeof (char *));
  1942. char **ne_values = OPENSSL_malloc(max_ne * sizeof (char *));
  1943. int *mval = OPENSSL_malloc (max_ne * sizeof (int));
  1944. char *sp = subject, *bp = buf;
  1945. int i, ne_num = 0;
  1946. X509_NAME *n = NULL;
  1947. int nid;
  1948. if (!buf || !ne_types || !ne_values)
  1949. {
  1950. BIO_printf(bio_err, "malloc error\n");
  1951. goto error;
  1952. }
  1953. if (*subject != '/')
  1954. {
  1955. BIO_printf(bio_err, "Subject does not start with '/'.\n");
  1956. goto error;
  1957. }
  1958. sp++; /* skip leading / */
  1959. /* no multivalued RDN by default */
  1960. mval[ne_num] = 0;
  1961. while (*sp)
  1962. {
  1963. /* collect type */
  1964. ne_types[ne_num] = bp;
  1965. while (*sp)
  1966. {
  1967. if (*sp == '\\') /* is there anything to escape in the type...? */
  1968. {
  1969. if (*++sp)
  1970. *bp++ = *sp++;
  1971. else
  1972. {
  1973. BIO_printf(bio_err, "escape character at end of string\n");
  1974. goto error;
  1975. }
  1976. }
  1977. else if (*sp == '=')
  1978. {
  1979. sp++;
  1980. *bp++ = '\0';
  1981. break;
  1982. }
  1983. else
  1984. *bp++ = *sp++;
  1985. }
  1986. if (!*sp)
  1987. {
  1988. BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num);
  1989. goto error;
  1990. }
  1991. ne_values[ne_num] = bp;
  1992. while (*sp)
  1993. {
  1994. if (*sp == '\\')
  1995. {
  1996. if (*++sp)
  1997. *bp++ = *sp++;
  1998. else
  1999. {
  2000. BIO_printf(bio_err, "escape character at end of string\n");
  2001. goto error;
  2002. }
  2003. }
  2004. else if (*sp == '/')
  2005. {
  2006. sp++;
  2007. /* no multivalued RDN by default */
  2008. mval[ne_num+1] = 0;
  2009. break;
  2010. }
  2011. else if (*sp == '+' && multirdn)
  2012. {
  2013. /* a not escaped + signals a mutlivalued RDN */
  2014. sp++;
  2015. mval[ne_num+1] = -1;
  2016. break;
  2017. }
  2018. else
  2019. *bp++ = *sp++;
  2020. }
  2021. *bp++ = '\0';
  2022. ne_num++;
  2023. }
  2024. if (!(n = X509_NAME_new()))
  2025. goto error;
  2026. for (i = 0; i < ne_num; i++)
  2027. {
  2028. if ((nid=OBJ_txt2nid(ne_types[i])) == NID_undef)
  2029. {
  2030. BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]);
  2031. continue;
  2032. }
  2033. if (!*ne_values[i])
  2034. {
  2035. BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]);
  2036. continue;
  2037. }
  2038. if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char*)ne_values[i], -1,-1,mval[i]))
  2039. goto error;
  2040. }
  2041. OPENSSL_free(ne_values);
  2042. OPENSSL_free(ne_types);
  2043. OPENSSL_free(buf);
  2044. return n;
  2045. error:
  2046. X509_NAME_free(n);
  2047. if (ne_values)
  2048. OPENSSL_free(ne_values);
  2049. if (ne_types)
  2050. OPENSSL_free(ne_types);
  2051. if (buf)
  2052. OPENSSL_free(buf);
  2053. return NULL;
  2054. }
  2055. int args_verify(char ***pargs, int *pargc,
  2056. int *badarg, BIO *err, X509_VERIFY_PARAM **pm)
  2057. {
  2058. ASN1_OBJECT *otmp = NULL;
  2059. unsigned long flags = 0;
  2060. int i;
  2061. int purpose = 0, depth = -1;
  2062. char **oldargs = *pargs;
  2063. char *arg = **pargs, *argn = (*pargs)[1];
  2064. const X509_VERIFY_PARAM *vpm = NULL;
  2065. if (!strcmp(arg, "-policy"))
  2066. {
  2067. if (!argn)
  2068. *badarg = 1;
  2069. else
  2070. {
  2071. otmp = OBJ_txt2obj(argn, 0);
  2072. if (!otmp)
  2073. {
  2074. BIO_printf(err, "Invalid Policy \"%s\"\n",
  2075. argn);
  2076. *badarg = 1;
  2077. }
  2078. }
  2079. (*pargs)++;
  2080. }
  2081. else if (strcmp(arg,"-purpose") == 0)
  2082. {
  2083. X509_PURPOSE *xptmp;
  2084. if (!argn)
  2085. *badarg = 1;
  2086. else
  2087. {
  2088. i = X509_PURPOSE_get_by_sname(argn);
  2089. if(i < 0)
  2090. {
  2091. BIO_printf(err, "unrecognized purpose\n");
  2092. *badarg = 1;
  2093. }
  2094. else
  2095. {
  2096. xptmp = X509_PURPOSE_get0(i);
  2097. purpose = X509_PURPOSE_get_id(xptmp);
  2098. }
  2099. }
  2100. (*pargs)++;
  2101. }
  2102. else if (strcmp(arg,"-verify_name") == 0)
  2103. {
  2104. if (!argn)
  2105. *badarg = 1;
  2106. else
  2107. {
  2108. vpm = X509_VERIFY_PARAM_lookup(argn);
  2109. if(!vpm)
  2110. {
  2111. BIO_printf(err, "unrecognized verify name\n");
  2112. *badarg = 1;
  2113. }
  2114. }
  2115. (*pargs)++;
  2116. }
  2117. else if (strcmp(arg,"-verify_depth") == 0)
  2118. {
  2119. if (!argn)
  2120. *badarg = 1;
  2121. else
  2122. {
  2123. depth = atoi(argn);
  2124. if(depth < 0)
  2125. {
  2126. BIO_printf(err, "invalid depth\n");
  2127. *badarg = 1;
  2128. }
  2129. }
  2130. (*pargs)++;
  2131. }
  2132. else if (!strcmp(arg, "-ignore_critical"))
  2133. flags |= X509_V_FLAG_IGNORE_CRITICAL;
  2134. else if (!strcmp(arg, "-issuer_checks"))
  2135. flags |= X509_V_FLAG_CB_ISSUER_CHECK;
  2136. else if (!strcmp(arg, "-crl_check"))
  2137. flags |= X509_V_FLAG_CRL_CHECK;
  2138. else if (!strcmp(arg, "-crl_check_all"))
  2139. flags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
  2140. else if (!strcmp(arg, "-policy_check"))
  2141. flags |= X509_V_FLAG_POLICY_CHECK;
  2142. else if (!strcmp(arg, "-explicit_policy"))
  2143. flags |= X509_V_FLAG_EXPLICIT_POLICY;
  2144. else if (!strcmp(arg, "-inhibit_any"))
  2145. flags |= X509_V_FLAG_INHIBIT_ANY;
  2146. else if (!strcmp(arg, "-inhibit_map"))
  2147. flags |= X509_V_FLAG_INHIBIT_MAP;
  2148. else if (!strcmp(arg, "-x509_strict"))
  2149. flags |= X509_V_FLAG_X509_STRICT;
  2150. else if (!strcmp(arg, "-extended_crl"))
  2151. flags |= X509_V_FLAG_EXTENDED_CRL_SUPPORT;
  2152. else if (!strcmp(arg, "-use_deltas"))
  2153. flags |= X509_V_FLAG_USE_DELTAS;
  2154. else if (!strcmp(arg, "-policy_print"))
  2155. flags |= X509_V_FLAG_NOTIFY_POLICY;
  2156. else if (!strcmp(arg, "-check_ss_sig"))
  2157. flags |= X509_V_FLAG_CHECK_SS_SIGNATURE;
  2158. else if (!strcmp(arg, "-trusted_first"))
  2159. flags |= X509_V_FLAG_TRUSTED_FIRST;
  2160. else
  2161. return 0;
  2162. if (*badarg)
  2163. {
  2164. if (*pm)
  2165. X509_VERIFY_PARAM_free(*pm);
  2166. *pm = NULL;
  2167. goto end;
  2168. }
  2169. if (!*pm && !(*pm = X509_VERIFY_PARAM_new()))
  2170. {
  2171. *badarg = 1;
  2172. goto end;
  2173. }
  2174. if (vpm)
  2175. X509_VERIFY_PARAM_set1(*pm, vpm);
  2176. if (otmp)
  2177. X509_VERIFY_PARAM_add0_policy(*pm, otmp);
  2178. if (flags)
  2179. X509_VERIFY_PARAM_set_flags(*pm, flags);
  2180. if (purpose)
  2181. X509_VERIFY_PARAM_set_purpose(*pm, purpose);
  2182. if (depth >= 0)
  2183. X509_VERIFY_PARAM_set_depth(*pm, depth);
  2184. end:
  2185. (*pargs)++;
  2186. if (pargc)
  2187. *pargc -= *pargs - oldargs;
  2188. return 1;
  2189. }
  2190. /* Read whole contents of a BIO into an allocated memory buffer and
  2191. * return it.
  2192. */
  2193. int bio_to_mem(unsigned char **out, int maxlen, BIO *in)
  2194. {
  2195. BIO *mem;
  2196. int len, ret;
  2197. unsigned char tbuf[1024];
  2198. mem = BIO_new(BIO_s_mem());
  2199. if (!mem)
  2200. return -1;
  2201. for(;;)
  2202. {
  2203. if ((maxlen != -1) && maxlen < 1024)
  2204. len = maxlen;
  2205. else
  2206. len = 1024;
  2207. len = BIO_read(in, tbuf, len);
  2208. if (len <= 0)
  2209. break;
  2210. if (BIO_write(mem, tbuf, len) != len)
  2211. {
  2212. BIO_free(mem);
  2213. return -1;
  2214. }
  2215. maxlen -= len;
  2216. if (maxlen == 0)
  2217. break;
  2218. }
  2219. ret = BIO_get_mem_data(mem, (char **)out);
  2220. BIO_set_flags(mem, BIO_FLAGS_MEM_RDONLY);
  2221. BIO_free(mem);
  2222. return ret;
  2223. }
  2224. int pkey_ctrl_string(EVP_PKEY_CTX *ctx, char *value)
  2225. {
  2226. int rv;
  2227. char *stmp, *vtmp = NULL;
  2228. stmp = BUF_strdup(value);
  2229. if (!stmp)
  2230. return -1;
  2231. vtmp = strchr(stmp, ':');
  2232. if (vtmp)
  2233. {
  2234. *vtmp = 0;
  2235. vtmp++;
  2236. }
  2237. rv = EVP_PKEY_CTX_ctrl_str(ctx, stmp, vtmp);
  2238. OPENSSL_free(stmp);
  2239. return rv;
  2240. }
  2241. static void nodes_print(BIO *out, const char *name,
  2242. STACK_OF(X509_POLICY_NODE) *nodes)
  2243. {
  2244. X509_POLICY_NODE *node;
  2245. int i;
  2246. BIO_printf(out, "%s Policies:", name);
  2247. if (nodes)
  2248. {
  2249. BIO_puts(out, "\n");
  2250. for (i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++)
  2251. {
  2252. node = sk_X509_POLICY_NODE_value(nodes, i);
  2253. X509_POLICY_NODE_print(out, node, 2);
  2254. }
  2255. }
  2256. else
  2257. BIO_puts(out, " <empty>\n");
  2258. }
  2259. void policies_print(BIO *out, X509_STORE_CTX *ctx)
  2260. {
  2261. X509_POLICY_TREE *tree;
  2262. int explicit_policy;
  2263. int free_out = 0;
  2264. if (out == NULL)
  2265. {
  2266. out = BIO_new_fp(stderr, BIO_NOCLOSE);
  2267. free_out = 1;
  2268. }
  2269. tree = X509_STORE_CTX_get0_policy_tree(ctx);
  2270. explicit_policy = X509_STORE_CTX_get_explicit_policy(ctx);
  2271. BIO_printf(out, "Require explicit Policy: %s\n",
  2272. explicit_policy ? "True" : "False");
  2273. nodes_print(out, "Authority", X509_policy_tree_get0_policies(tree));
  2274. nodes_print(out, "User", X509_policy_tree_get0_user_policies(tree));
  2275. if (free_out)
  2276. BIO_free(out);
  2277. }
  2278. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  2279. static JPAKE_CTX *jpake_init(const char *us, const char *them,
  2280. const char *secret)
  2281. {
  2282. BIGNUM *p = NULL;
  2283. BIGNUM *g = NULL;
  2284. BIGNUM *q = NULL;
  2285. BIGNUM *bnsecret = BN_new();
  2286. JPAKE_CTX *ctx;
  2287. /* Use a safe prime for p (that we found earlier) */
  2288. BN_hex2bn(&p, "F9E5B365665EA7A05A9C534502780FEE6F1AB5BD4F49947FD036DBD7E905269AF46EF28B0FC07487EE4F5D20FB3C0AF8E700F3A2FA3414970CBED44FEDFF80CE78D800F184BB82435D137AADA2C6C16523247930A63B85661D1FC817A51ACD96168E95898A1F83A79FFB529368AA7833ABD1B0C3AEDDB14D2E1A2F71D99F763F");
  2289. g = BN_new();
  2290. BN_set_word(g, 2);
  2291. q = BN_new();
  2292. BN_rshift1(q, p);
  2293. BN_bin2bn((const unsigned char *)secret, strlen(secret), bnsecret);
  2294. ctx = JPAKE_CTX_new(us, them, p, g, q, bnsecret);
  2295. BN_free(bnsecret);
  2296. BN_free(q);
  2297. BN_free(g);
  2298. BN_free(p);
  2299. return ctx;
  2300. }
  2301. static void jpake_send_part(BIO *conn, const JPAKE_STEP_PART *p)
  2302. {
  2303. BN_print(conn, p->gx);
  2304. BIO_puts(conn, "\n");
  2305. BN_print(conn, p->zkpx.gr);
  2306. BIO_puts(conn, "\n");
  2307. BN_print(conn, p->zkpx.b);
  2308. BIO_puts(conn, "\n");
  2309. }
  2310. static void jpake_send_step1(BIO *bconn, JPAKE_CTX *ctx)
  2311. {
  2312. JPAKE_STEP1 s1;
  2313. JPAKE_STEP1_init(&s1);
  2314. JPAKE_STEP1_generate(&s1, ctx);
  2315. jpake_send_part(bconn, &s1.p1);
  2316. jpake_send_part(bconn, &s1.p2);
  2317. (void)BIO_flush(bconn);
  2318. JPAKE_STEP1_release(&s1);
  2319. }
  2320. static void jpake_send_step2(BIO *bconn, JPAKE_CTX *ctx)
  2321. {
  2322. JPAKE_STEP2 s2;
  2323. JPAKE_STEP2_init(&s2);
  2324. JPAKE_STEP2_generate(&s2, ctx);
  2325. jpake_send_part(bconn, &s2);
  2326. (void)BIO_flush(bconn);
  2327. JPAKE_STEP2_release(&s2);
  2328. }
  2329. static void jpake_send_step3a(BIO *bconn, JPAKE_CTX *ctx)
  2330. {
  2331. JPAKE_STEP3A s3a;
  2332. JPAKE_STEP3A_init(&s3a);
  2333. JPAKE_STEP3A_generate(&s3a, ctx);
  2334. BIO_write(bconn, s3a.hhk, sizeof s3a.hhk);
  2335. (void)BIO_flush(bconn);
  2336. JPAKE_STEP3A_release(&s3a);
  2337. }
  2338. static void jpake_send_step3b(BIO *bconn, JPAKE_CTX *ctx)
  2339. {
  2340. JPAKE_STEP3B s3b;
  2341. JPAKE_STEP3B_init(&s3b);
  2342. JPAKE_STEP3B_generate(&s3b, ctx);
  2343. BIO_write(bconn, s3b.hk, sizeof s3b.hk);
  2344. (void)BIO_flush(bconn);
  2345. JPAKE_STEP3B_release(&s3b);
  2346. }
  2347. static void readbn(BIGNUM **bn, BIO *bconn)
  2348. {
  2349. char buf[10240];
  2350. int l;
  2351. l = BIO_gets(bconn, buf, sizeof buf);
  2352. assert(l > 0);
  2353. assert(buf[l-1] == '\n');
  2354. buf[l-1] = '\0';
  2355. BN_hex2bn(bn, buf);
  2356. }
  2357. static void jpake_receive_part(JPAKE_STEP_PART *p, BIO *bconn)
  2358. {
  2359. readbn(&p->gx, bconn);
  2360. readbn(&p->zkpx.gr, bconn);
  2361. readbn(&p->zkpx.b, bconn);
  2362. }
  2363. static void jpake_receive_step1(JPAKE_CTX *ctx, BIO *bconn)
  2364. {
  2365. JPAKE_STEP1 s1;
  2366. JPAKE_STEP1_init(&s1);
  2367. jpake_receive_part(&s1.p1, bconn);
  2368. jpake_receive_part(&s1.p2, bconn);
  2369. if(!JPAKE_STEP1_process(ctx, &s1))
  2370. {
  2371. ERR_print_errors(bio_err);
  2372. exit(1);
  2373. }
  2374. JPAKE_STEP1_release(&s1);
  2375. }
  2376. static void jpake_receive_step2(JPAKE_CTX *ctx, BIO *bconn)
  2377. {
  2378. JPAKE_STEP2 s2;
  2379. JPAKE_STEP2_init(&s2);
  2380. jpake_receive_part(&s2, bconn);
  2381. if(!JPAKE_STEP2_process(ctx, &s2))
  2382. {
  2383. ERR_print_errors(bio_err);
  2384. exit(1);
  2385. }
  2386. JPAKE_STEP2_release(&s2);
  2387. }
  2388. static void jpake_receive_step3a(JPAKE_CTX *ctx, BIO *bconn)
  2389. {
  2390. JPAKE_STEP3A s3a;
  2391. int l;
  2392. JPAKE_STEP3A_init(&s3a);
  2393. l = BIO_read(bconn, s3a.hhk, sizeof s3a.hhk);
  2394. assert(l == sizeof s3a.hhk);
  2395. if(!JPAKE_STEP3A_process(ctx, &s3a))
  2396. {
  2397. ERR_print_errors(bio_err);
  2398. exit(1);
  2399. }
  2400. JPAKE_STEP3A_release(&s3a);
  2401. }
  2402. static void jpake_receive_step3b(JPAKE_CTX *ctx, BIO *bconn)
  2403. {
  2404. JPAKE_STEP3B s3b;
  2405. int l;
  2406. JPAKE_STEP3B_init(&s3b);
  2407. l = BIO_read(bconn, s3b.hk, sizeof s3b.hk);
  2408. assert(l == sizeof s3b.hk);
  2409. if(!JPAKE_STEP3B_process(ctx, &s3b))
  2410. {
  2411. ERR_print_errors(bio_err);
  2412. exit(1);
  2413. }
  2414. JPAKE_STEP3B_release(&s3b);
  2415. }
  2416. void jpake_client_auth(BIO *out, BIO *conn, const char *secret)
  2417. {
  2418. JPAKE_CTX *ctx;
  2419. BIO *bconn;
  2420. BIO_puts(out, "Authenticating with JPAKE\n");
  2421. ctx = jpake_init("client", "server", secret);
  2422. bconn = BIO_new(BIO_f_buffer());
  2423. BIO_push(bconn, conn);
  2424. jpake_send_step1(bconn, ctx);
  2425. jpake_receive_step1(ctx, bconn);
  2426. jpake_send_step2(bconn, ctx);
  2427. jpake_receive_step2(ctx, bconn);
  2428. jpake_send_step3a(bconn, ctx);
  2429. jpake_receive_step3b(ctx, bconn);
  2430. BIO_puts(out, "JPAKE authentication succeeded, setting PSK\n");
  2431. psk_key = BN_bn2hex(JPAKE_get_shared_key(ctx));
  2432. BIO_pop(bconn);
  2433. BIO_free(bconn);
  2434. JPAKE_CTX_free(ctx);
  2435. }
  2436. void jpake_server_auth(BIO *out, BIO *conn, const char *secret)
  2437. {
  2438. JPAKE_CTX *ctx;
  2439. BIO *bconn;
  2440. BIO_puts(out, "Authenticating with JPAKE\n");
  2441. ctx = jpake_init("server", "client", secret);
  2442. bconn = BIO_new(BIO_f_buffer());
  2443. BIO_push(bconn, conn);
  2444. jpake_receive_step1(ctx, bconn);
  2445. jpake_send_step1(bconn, ctx);
  2446. jpake_receive_step2(ctx, bconn);
  2447. jpake_send_step2(bconn, ctx);
  2448. jpake_receive_step3a(ctx, bconn);
  2449. jpake_send_step3b(bconn, ctx);
  2450. BIO_puts(out, "JPAKE authentication succeeded, setting PSK\n");
  2451. psk_key = BN_bn2hex(JPAKE_get_shared_key(ctx));
  2452. BIO_pop(bconn);
  2453. BIO_free(bconn);
  2454. JPAKE_CTX_free(ctx);
  2455. }
  2456. #endif
  2457. /*
  2458. * Platform-specific sections
  2459. */
  2460. #if defined(_WIN32)
  2461. # ifdef fileno
  2462. # undef fileno
  2463. # define fileno(a) (int)_fileno(a)
  2464. # endif
  2465. # include <windows.h>
  2466. # include <tchar.h>
  2467. static int WIN32_rename(const char *from, const char *to)
  2468. {
  2469. TCHAR *tfrom=NULL,*tto;
  2470. DWORD err;
  2471. int ret=0;
  2472. if (sizeof(TCHAR) == 1)
  2473. {
  2474. tfrom = (TCHAR *)from;
  2475. tto = (TCHAR *)to;
  2476. }
  2477. else /* UNICODE path */
  2478. {
  2479. size_t i,flen=strlen(from)+1,tlen=strlen(to)+1;
  2480. tfrom = (TCHAR *)malloc(sizeof(TCHAR)*(flen+tlen));
  2481. if (tfrom==NULL) goto err;
  2482. tto=tfrom+flen;
  2483. #if !defined(_WIN32_WCE) || _WIN32_WCE>=101
  2484. if (!MultiByteToWideChar(CP_ACP,0,from,flen,(WCHAR *)tfrom,flen))
  2485. #endif
  2486. for (i=0;i<flen;i++) tfrom[i]=(TCHAR)from[i];
  2487. #if !defined(_WIN32_WCE) || _WIN32_WCE>=101
  2488. if (!MultiByteToWideChar(CP_ACP,0,to, tlen,(WCHAR *)tto, tlen))
  2489. #endif
  2490. for (i=0;i<tlen;i++) tto[i] =(TCHAR)to[i];
  2491. }
  2492. if (MoveFile(tfrom,tto)) goto ok;
  2493. err=GetLastError();
  2494. if (err==ERROR_ALREADY_EXISTS || err==ERROR_FILE_EXISTS)
  2495. {
  2496. if (DeleteFile(tto) && MoveFile(tfrom,tto))
  2497. goto ok;
  2498. err=GetLastError();
  2499. }
  2500. if (err==ERROR_FILE_NOT_FOUND || err==ERROR_PATH_NOT_FOUND)
  2501. errno = ENOENT;
  2502. else if (err==ERROR_ACCESS_DENIED)
  2503. errno = EACCES;
  2504. else
  2505. errno = EINVAL; /* we could map more codes... */
  2506. err:
  2507. ret=-1;
  2508. ok:
  2509. if (tfrom!=NULL && tfrom!=(TCHAR *)from) free(tfrom);
  2510. return ret;
  2511. }
  2512. #endif
  2513. /* app_tminterval section */
  2514. #if defined(_WIN32)
  2515. double app_tminterval(int stop,int usertime)
  2516. {
  2517. FILETIME now;
  2518. double ret=0;
  2519. static ULARGE_INTEGER tmstart;
  2520. static int warning=1;
  2521. #ifdef _WIN32_WINNT
  2522. static HANDLE proc=NULL;
  2523. if (proc==NULL)
  2524. {
  2525. if (GetVersion() < 0x80000000)
  2526. proc = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,
  2527. GetCurrentProcessId());
  2528. if (proc==NULL) proc = (HANDLE)-1;
  2529. }
  2530. if (usertime && proc!=(HANDLE)-1)
  2531. {
  2532. FILETIME junk;
  2533. GetProcessTimes(proc,&junk,&junk,&junk,&now);
  2534. }
  2535. else
  2536. #endif
  2537. {
  2538. SYSTEMTIME systime;
  2539. if (usertime && warning)
  2540. {
  2541. BIO_printf(bio_err,"To get meaningful results, run "
  2542. "this program on idle system.\n");
  2543. warning=0;
  2544. }
  2545. GetSystemTime(&systime);
  2546. SystemTimeToFileTime(&systime,&now);
  2547. }
  2548. if (stop==TM_START)
  2549. {
  2550. tmstart.u.LowPart = now.dwLowDateTime;
  2551. tmstart.u.HighPart = now.dwHighDateTime;
  2552. }
  2553. else {
  2554. ULARGE_INTEGER tmstop;
  2555. tmstop.u.LowPart = now.dwLowDateTime;
  2556. tmstop.u.HighPart = now.dwHighDateTime;
  2557. ret = (__int64)(tmstop.QuadPart - tmstart.QuadPart)*1e-7;
  2558. }
  2559. return (ret);
  2560. }
  2561. #elif defined(OPENSSL_SYS_NETWARE)
  2562. #include <time.h>
  2563. double app_tminterval(int stop,int usertime)
  2564. {
  2565. double ret=0;
  2566. static clock_t tmstart;
  2567. static int warning=1;
  2568. if (usertime && warning)
  2569. {
  2570. BIO_printf(bio_err,"To get meaningful results, run "
  2571. "this program on idle system.\n");
  2572. warning=0;
  2573. }
  2574. if (stop==TM_START) tmstart = clock();
  2575. else ret = (clock()-tmstart)/(double)CLOCKS_PER_SEC;
  2576. return (ret);
  2577. }
  2578. #elif defined(OPENSSL_SYSTEM_VXWORKS)
  2579. #include <time.h>
  2580. double app_tminterval(int stop,int usertime)
  2581. {
  2582. double ret=0;
  2583. #ifdef CLOCK_REALTIME
  2584. static struct timespec tmstart;
  2585. struct timespec now;
  2586. #else
  2587. static unsigned long tmstart;
  2588. unsigned long now;
  2589. #endif
  2590. static int warning=1;
  2591. if (usertime && warning)
  2592. {
  2593. BIO_printf(bio_err,"To get meaningful results, run "
  2594. "this program on idle system.\n");
  2595. warning=0;
  2596. }
  2597. #ifdef CLOCK_REALTIME
  2598. clock_gettime(CLOCK_REALTIME,&now);
  2599. if (stop==TM_START) tmstart = now;
  2600. else ret = ( (now.tv_sec+now.tv_nsec*1e-9)
  2601. - (tmstart.tv_sec+tmstart.tv_nsec*1e-9) );
  2602. #else
  2603. now = tickGet();
  2604. if (stop==TM_START) tmstart = now;
  2605. else ret = (now - tmstart)/(double)sysClkRateGet();
  2606. #endif
  2607. return (ret);
  2608. }
  2609. #elif defined(OPENSSL_SYSTEM_VMS)
  2610. #include <time.h>
  2611. #include <times.h>
  2612. double app_tminterval(int stop,int usertime)
  2613. {
  2614. static clock_t tmstart;
  2615. double ret = 0;
  2616. clock_t now;
  2617. #ifdef __TMS
  2618. struct tms rus;
  2619. now = times(&rus);
  2620. if (usertime) now = rus.tms_utime;
  2621. #else
  2622. if (usertime)
  2623. now = clock(); /* sum of user and kernel times */
  2624. else {
  2625. struct timeval tv;
  2626. gettimeofday(&tv,NULL);
  2627. now = (clock_t)(
  2628. (unsigned long long)tv.tv_sec*CLK_TCK +
  2629. (unsigned long long)tv.tv_usec*(1000000/CLK_TCK)
  2630. );
  2631. }
  2632. #endif
  2633. if (stop==TM_START) tmstart = now;
  2634. else ret = (now - tmstart)/(double)(CLK_TCK);
  2635. return (ret);
  2636. }
  2637. #elif defined(_SC_CLK_TCK) /* by means of unistd.h */
  2638. #include <sys/times.h>
  2639. double app_tminterval(int stop,int usertime)
  2640. {
  2641. double ret = 0;
  2642. struct tms rus;
  2643. clock_t now = times(&rus);
  2644. static clock_t tmstart;
  2645. if (usertime) now = rus.tms_utime;
  2646. if (stop==TM_START) tmstart = now;
  2647. else
  2648. {
  2649. long int tck = sysconf(_SC_CLK_TCK);
  2650. ret = (now - tmstart)/(double)tck;
  2651. }
  2652. return (ret);
  2653. }
  2654. #else
  2655. #include <sys/time.h>
  2656. #include <sys/resource.h>
  2657. double app_tminterval(int stop,int usertime)
  2658. {
  2659. double ret = 0;
  2660. struct rusage rus;
  2661. struct timeval now;
  2662. static struct timeval tmstart;
  2663. if (usertime) getrusage(RUSAGE_SELF,&rus), now = rus.ru_utime;
  2664. else gettimeofday(&now,NULL);
  2665. if (stop==TM_START) tmstart = now;
  2666. else ret = ( (now.tv_sec+now.tv_usec*1e-6)
  2667. - (tmstart.tv_sec+tmstart.tv_usec*1e-6) );
  2668. return ret;
  2669. }
  2670. #endif
  2671. /* app_isdir section */
  2672. #ifdef _WIN32
  2673. int app_isdir(const char *name)
  2674. {
  2675. HANDLE hList;
  2676. WIN32_FIND_DATA FileData;
  2677. #if defined(UNICODE) || defined(_UNICODE)
  2678. size_t i, len_0 = strlen(name)+1;
  2679. if (len_0 > sizeof(FileData.cFileName)/sizeof(FileData.cFileName[0]))
  2680. return -1;
  2681. #if !defined(_WIN32_WCE) || _WIN32_WCE>=101
  2682. if (!MultiByteToWideChar(CP_ACP,0,name,len_0,FileData.cFileName,len_0))
  2683. #endif
  2684. for (i=0;i<len_0;i++)
  2685. FileData.cFileName[i] = (WCHAR)name[i];
  2686. hList = FindFirstFile(FileData.cFileName,&FileData);
  2687. #else
  2688. hList = FindFirstFile(name,&FileData);
  2689. #endif
  2690. if (hList == INVALID_HANDLE_VALUE) return -1;
  2691. FindClose(hList);
  2692. return ((FileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=0);
  2693. }
  2694. #else
  2695. #include <sys/stat.h>
  2696. #ifndef S_ISDIR
  2697. # if defined(_S_IFMT) && defined(_S_IFDIR)
  2698. # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
  2699. # else
  2700. # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
  2701. # endif
  2702. #endif
  2703. int app_isdir(const char *name)
  2704. {
  2705. #if defined(S_ISDIR)
  2706. struct stat st;
  2707. if (stat(name,&st)==0) return S_ISDIR(st.st_mode);
  2708. else return -1;
  2709. #else
  2710. return -1;
  2711. #endif
  2712. }
  2713. #endif
  2714. /* raw_read|write section */
  2715. #if defined(_WIN32) && defined(STD_INPUT_HANDLE)
  2716. int raw_read_stdin(void *buf,int siz)
  2717. {
  2718. DWORD n;
  2719. if (ReadFile(GetStdHandle(STD_INPUT_HANDLE),buf,siz,&n,NULL))
  2720. return (n);
  2721. else return (-1);
  2722. }
  2723. #else
  2724. int raw_read_stdin(void *buf,int siz)
  2725. { return read(fileno(stdin),buf,siz); }
  2726. #endif
  2727. #if defined(_WIN32) && defined(STD_OUTPUT_HANDLE)
  2728. int raw_write_stdout(const void *buf,int siz)
  2729. {
  2730. DWORD n;
  2731. if (WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),buf,siz,&n,NULL))
  2732. return (n);
  2733. else return (-1);
  2734. }
  2735. #else
  2736. int raw_write_stdout(const void *buf,int siz)
  2737. { return write(fileno(stdout),buf,siz); }
  2738. #endif