apps.c 89 KB

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