2
0

apps.c 68 KB

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