apps.c 65 KB

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