apps.c 81 KB

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