apps.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335
  1. /* apps/apps.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #include <stdio.h>
  112. #include <stdlib.h>
  113. #include <string.h>
  114. #include <sys/types.h>
  115. #include <sys/stat.h>
  116. #include <ctype.h>
  117. #include <openssl/err.h>
  118. #include <openssl/x509.h>
  119. #include <openssl/x509v3.h>
  120. #include <openssl/pem.h>
  121. #include <openssl/pkcs12.h>
  122. #include <openssl/ui.h>
  123. #include <openssl/safestack.h>
  124. #ifndef OPENSSL_NO_ENGINE
  125. #include <openssl/engine.h>
  126. #endif
  127. #ifndef OPENSSL_NO_RSA
  128. #include <openssl/rsa.h>
  129. #endif
  130. #include <openssl/bn.h>
  131. #define NON_MAIN
  132. #include "apps.h"
  133. #undef NON_MAIN
  134. typedef struct {
  135. const char *name;
  136. unsigned long flag;
  137. unsigned long mask;
  138. } NAME_EX_TBL;
  139. static UI_METHOD *ui_method = NULL;
  140. static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
  141. static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
  142. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  143. /* Looks like this stuff is worth moving into separate function */
  144. static EVP_PKEY *
  145. load_netscape_key(BIO *err, BIO *key, const char *file,
  146. const char *key_descrip, int format);
  147. #endif
  148. int app_init(long mesgwin);
  149. #ifdef undef /* never finished - probably never will be :-) */
  150. int args_from_file(char *file, int *argc, char **argv[])
  151. {
  152. FILE *fp;
  153. int num,i;
  154. unsigned int len;
  155. static char *buf=NULL;
  156. static char **arg=NULL;
  157. char *p;
  158. struct stat stbuf;
  159. if (stat(file,&stbuf) < 0) return(0);
  160. fp=fopen(file,"r");
  161. if (fp == NULL)
  162. return(0);
  163. *argc=0;
  164. *argv=NULL;
  165. len=(unsigned int)stbuf.st_size;
  166. if (buf != NULL) OPENSSL_free(buf);
  167. buf=(char *)OPENSSL_malloc(len+1);
  168. if (buf == NULL) return(0);
  169. len=fread(buf,1,len,fp);
  170. if (len <= 1) return(0);
  171. buf[len]='\0';
  172. i=0;
  173. for (p=buf; *p; p++)
  174. if (*p == '\n') i++;
  175. if (arg != NULL) OPENSSL_free(arg);
  176. arg=(char **)OPENSSL_malloc(sizeof(char *)*(i*2));
  177. *argv=arg;
  178. num=0;
  179. p=buf;
  180. for (;;)
  181. {
  182. if (!*p) break;
  183. if (*p == '#') /* comment line */
  184. {
  185. while (*p && (*p != '\n')) p++;
  186. continue;
  187. }
  188. /* else we have a line */
  189. *(arg++)=p;
  190. num++;
  191. while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
  192. p++;
  193. if (!*p) break;
  194. if (*p == '\n')
  195. {
  196. *(p++)='\0';
  197. continue;
  198. }
  199. /* else it is a tab or space */
  200. p++;
  201. while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
  202. p++;
  203. if (!*p) break;
  204. if (*p == '\n')
  205. {
  206. p++;
  207. continue;
  208. }
  209. *(arg++)=p++;
  210. num++;
  211. while (*p && (*p != '\n')) p++;
  212. if (!*p) break;
  213. /* else *p == '\n' */
  214. *(p++)='\0';
  215. }
  216. *argc=num;
  217. return(1);
  218. }
  219. #endif
  220. int str2fmt(char *s)
  221. {
  222. if ((*s == 'D') || (*s == 'd'))
  223. return(FORMAT_ASN1);
  224. else if ((*s == 'T') || (*s == 't'))
  225. return(FORMAT_TEXT);
  226. else if ((*s == 'P') || (*s == 'p'))
  227. return(FORMAT_PEM);
  228. else if ((*s == 'N') || (*s == 'n'))
  229. return(FORMAT_NETSCAPE);
  230. else if ((*s == 'S') || (*s == 's'))
  231. return(FORMAT_SMIME);
  232. else if ((*s == '1')
  233. || (strcmp(s,"PKCS12") == 0) || (strcmp(s,"pkcs12") == 0)
  234. || (strcmp(s,"P12") == 0) || (strcmp(s,"p12") == 0))
  235. return(FORMAT_PKCS12);
  236. else if ((*s == 'E') || (*s == 'e'))
  237. return(FORMAT_ENGINE);
  238. else
  239. return(FORMAT_UNDEF);
  240. }
  241. #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_SYS_NETWARE)
  242. void program_name(char *in, char *out, int size)
  243. {
  244. int i,n;
  245. char *p=NULL;
  246. n=strlen(in);
  247. /* find the last '/', '\' or ':' */
  248. for (i=n-1; i>0; i--)
  249. {
  250. if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':'))
  251. {
  252. p= &(in[i+1]);
  253. break;
  254. }
  255. }
  256. if (p == NULL)
  257. p=in;
  258. n=strlen(p);
  259. #if defined(OPENSSL_SYS_NETWARE)
  260. /* strip off trailing .nlm if present. */
  261. if ((n > 4) && (p[n-4] == '.') &&
  262. ((p[n-3] == 'n') || (p[n-3] == 'N')) &&
  263. ((p[n-2] == 'l') || (p[n-2] == 'L')) &&
  264. ((p[n-1] == 'm') || (p[n-1] == 'M')))
  265. n-=4;
  266. #else
  267. /* strip off trailing .exe if present. */
  268. if ((n > 4) && (p[n-4] == '.') &&
  269. ((p[n-3] == 'e') || (p[n-3] == 'E')) &&
  270. ((p[n-2] == 'x') || (p[n-2] == 'X')) &&
  271. ((p[n-1] == 'e') || (p[n-1] == 'E')))
  272. n-=4;
  273. #endif
  274. if (n > size-1)
  275. n=size-1;
  276. for (i=0; i<n; i++)
  277. {
  278. if ((p[i] >= 'A') && (p[i] <= 'Z'))
  279. out[i]=p[i]-'A'+'a';
  280. else
  281. out[i]=p[i];
  282. }
  283. out[n]='\0';
  284. }
  285. #else
  286. #ifdef OPENSSL_SYS_VMS
  287. void program_name(char *in, char *out, int size)
  288. {
  289. char *p=in, *q;
  290. char *chars=":]>";
  291. while(*chars != '\0')
  292. {
  293. q=strrchr(p,*chars);
  294. if (q > p)
  295. p = q + 1;
  296. chars++;
  297. }
  298. q=strrchr(p,'.');
  299. if (q == NULL)
  300. q = p + strlen(p);
  301. strncpy(out,p,size-1);
  302. if (q-p >= size)
  303. {
  304. out[size-1]='\0';
  305. }
  306. else
  307. {
  308. out[q-p]='\0';
  309. }
  310. }
  311. #else
  312. void program_name(char *in, char *out, int size)
  313. {
  314. char *p;
  315. p=strrchr(in,'/');
  316. if (p != NULL)
  317. p++;
  318. else
  319. p=in;
  320. BUF_strlcpy(out,p,size);
  321. }
  322. #endif
  323. #endif
  324. int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
  325. {
  326. int num,len,i;
  327. char *p;
  328. *argc=0;
  329. *argv=NULL;
  330. len=strlen(buf);
  331. i=0;
  332. if (arg->count == 0)
  333. {
  334. arg->count=20;
  335. arg->data=(char **)OPENSSL_malloc(sizeof(char *)*arg->count);
  336. }
  337. for (i=0; i<arg->count; i++)
  338. arg->data[i]=NULL;
  339. num=0;
  340. p=buf;
  341. for (;;)
  342. {
  343. /* first scan over white space */
  344. if (!*p) break;
  345. while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
  346. p++;
  347. if (!*p) break;
  348. /* The start of something good :-) */
  349. if (num >= arg->count)
  350. {
  351. char **tmp_p;
  352. int tlen = arg->count + 20;
  353. tmp_p = (char **)OPENSSL_realloc(arg->data,
  354. sizeof(char *)*tlen);
  355. if (tmp_p == NULL)
  356. return 0;
  357. arg->data = tmp_p;
  358. arg->count = tlen;
  359. /* initialize newly allocated data */
  360. for (i = num; i < arg->count; i++)
  361. arg->data[i] = NULL;
  362. }
  363. arg->data[num++]=p;
  364. /* now look for the end of this */
  365. if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */
  366. {
  367. i= *(p++);
  368. arg->data[num-1]++; /* jump over quote */
  369. while (*p && (*p != i))
  370. p++;
  371. *p='\0';
  372. }
  373. else
  374. {
  375. while (*p && ((*p != ' ') &&
  376. (*p != '\t') && (*p != '\n')))
  377. p++;
  378. if (*p == '\0')
  379. p--;
  380. else
  381. *p='\0';
  382. }
  383. p++;
  384. }
  385. *argc=num;
  386. *argv=arg->data;
  387. return(1);
  388. }
  389. #ifndef APP_INIT
  390. int app_init(long mesgwin)
  391. {
  392. return(1);
  393. }
  394. #endif
  395. int dump_cert_text (BIO *out, X509 *x)
  396. {
  397. char *p;
  398. p=X509_NAME_oneline(X509_get_subject_name(x),NULL,0);
  399. BIO_puts(out,"subject=");
  400. BIO_puts(out,p);
  401. OPENSSL_free(p);
  402. p=X509_NAME_oneline(X509_get_issuer_name(x),NULL,0);
  403. BIO_puts(out,"\nissuer=");
  404. BIO_puts(out,p);
  405. BIO_puts(out,"\n");
  406. OPENSSL_free(p);
  407. return 0;
  408. }
  409. static int ui_open(UI *ui)
  410. {
  411. return UI_method_get_opener(UI_OpenSSL())(ui);
  412. }
  413. static int ui_read(UI *ui, UI_STRING *uis)
  414. {
  415. if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
  416. && UI_get0_user_data(ui))
  417. {
  418. switch(UI_get_string_type(uis))
  419. {
  420. case UIT_PROMPT:
  421. case UIT_VERIFY:
  422. {
  423. const char *password =
  424. ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
  425. if (password && password[0] != '\0')
  426. {
  427. UI_set_result(ui, uis, password);
  428. return 1;
  429. }
  430. }
  431. default:
  432. break;
  433. }
  434. }
  435. return UI_method_get_reader(UI_OpenSSL())(ui, uis);
  436. }
  437. static int ui_write(UI *ui, UI_STRING *uis)
  438. {
  439. if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
  440. && UI_get0_user_data(ui))
  441. {
  442. switch(UI_get_string_type(uis))
  443. {
  444. case UIT_PROMPT:
  445. case UIT_VERIFY:
  446. {
  447. const char *password =
  448. ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
  449. if (password && password[0] != '\0')
  450. return 1;
  451. }
  452. default:
  453. break;
  454. }
  455. }
  456. return UI_method_get_writer(UI_OpenSSL())(ui, uis);
  457. }
  458. static int ui_close(UI *ui)
  459. {
  460. return UI_method_get_closer(UI_OpenSSL())(ui);
  461. }
  462. int setup_ui_method(void)
  463. {
  464. ui_method = UI_create_method("OpenSSL application user interface");
  465. UI_method_set_opener(ui_method, ui_open);
  466. UI_method_set_reader(ui_method, ui_read);
  467. UI_method_set_writer(ui_method, ui_write);
  468. UI_method_set_closer(ui_method, ui_close);
  469. return 0;
  470. }
  471. void destroy_ui_method(void)
  472. {
  473. if(ui_method)
  474. {
  475. UI_destroy_method(ui_method);
  476. ui_method = NULL;
  477. }
  478. }
  479. int password_callback(char *buf, int bufsiz, int verify,
  480. PW_CB_DATA *cb_tmp)
  481. {
  482. UI *ui = NULL;
  483. int res = 0;
  484. const char *prompt_info = NULL;
  485. const char *password = NULL;
  486. PW_CB_DATA *cb_data = (PW_CB_DATA *)cb_tmp;
  487. if (cb_data)
  488. {
  489. if (cb_data->password)
  490. password = cb_data->password;
  491. if (cb_data->prompt_info)
  492. prompt_info = cb_data->prompt_info;
  493. }
  494. if (password)
  495. {
  496. res = strlen(password);
  497. if (res > bufsiz)
  498. res = bufsiz;
  499. memcpy(buf, password, res);
  500. return res;
  501. }
  502. ui = UI_new_method(ui_method);
  503. if (ui)
  504. {
  505. int ok = 0;
  506. char *buff = NULL;
  507. int ui_flags = 0;
  508. char *prompt = NULL;
  509. prompt = UI_construct_prompt(ui, "pass phrase",
  510. prompt_info);
  511. ui_flags |= UI_INPUT_FLAG_DEFAULT_PWD;
  512. UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0);
  513. if (ok >= 0)
  514. ok = UI_add_input_string(ui,prompt,ui_flags,buf,
  515. PW_MIN_LENGTH,BUFSIZ-1);
  516. if (ok >= 0 && verify)
  517. {
  518. buff = (char *)OPENSSL_malloc(bufsiz);
  519. ok = UI_add_verify_string(ui,prompt,ui_flags,buff,
  520. PW_MIN_LENGTH,BUFSIZ-1, buf);
  521. }
  522. if (ok >= 0)
  523. do
  524. {
  525. ok = UI_process(ui);
  526. }
  527. while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0));
  528. if (buff)
  529. {
  530. OPENSSL_cleanse(buff,(unsigned int)bufsiz);
  531. OPENSSL_free(buff);
  532. }
  533. if (ok >= 0)
  534. res = strlen(buf);
  535. if (ok == -1)
  536. {
  537. BIO_printf(bio_err, "User interface error\n");
  538. ERR_print_errors(bio_err);
  539. OPENSSL_cleanse(buf,(unsigned int)bufsiz);
  540. res = 0;
  541. }
  542. if (ok == -2)
  543. {
  544. BIO_printf(bio_err,"aborted!\n");
  545. OPENSSL_cleanse(buf,(unsigned int)bufsiz);
  546. res = 0;
  547. }
  548. UI_free(ui);
  549. OPENSSL_free(prompt);
  550. }
  551. return res;
  552. }
  553. static char *app_get_pass(BIO *err, char *arg, int keepbio);
  554. int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2)
  555. {
  556. int same;
  557. if(!arg2 || !arg1 || strcmp(arg1, arg2)) same = 0;
  558. else same = 1;
  559. if(arg1) {
  560. *pass1 = app_get_pass(err, arg1, same);
  561. if(!*pass1) return 0;
  562. } else if(pass1) *pass1 = NULL;
  563. if(arg2) {
  564. *pass2 = app_get_pass(err, arg2, same ? 2 : 0);
  565. if(!*pass2) return 0;
  566. } else if(pass2) *pass2 = NULL;
  567. return 1;
  568. }
  569. static char *app_get_pass(BIO *err, char *arg, int keepbio)
  570. {
  571. char *tmp, tpass[APP_PASS_LEN];
  572. static BIO *pwdbio = NULL;
  573. int i;
  574. if(!strncmp(arg, "pass:", 5)) return BUF_strdup(arg + 5);
  575. if(!strncmp(arg, "env:", 4)) {
  576. tmp = getenv(arg + 4);
  577. if(!tmp) {
  578. BIO_printf(err, "Can't read environment variable %s\n", arg + 4);
  579. return NULL;
  580. }
  581. return BUF_strdup(tmp);
  582. }
  583. if(!keepbio || !pwdbio) {
  584. if(!strncmp(arg, "file:", 5)) {
  585. pwdbio = BIO_new_file(arg + 5, "r");
  586. if(!pwdbio) {
  587. BIO_printf(err, "Can't open file %s\n", arg + 5);
  588. return NULL;
  589. }
  590. } else if(!strncmp(arg, "fd:", 3)) {
  591. BIO *btmp;
  592. i = atoi(arg + 3);
  593. if(i >= 0) pwdbio = BIO_new_fd(i, BIO_NOCLOSE);
  594. if((i < 0) || !pwdbio) {
  595. BIO_printf(err, "Can't access file descriptor %s\n", arg + 3);
  596. return NULL;
  597. }
  598. /* Can't do BIO_gets on an fd BIO so add a buffering BIO */
  599. btmp = BIO_new(BIO_f_buffer());
  600. pwdbio = BIO_push(btmp, pwdbio);
  601. } else if(!strcmp(arg, "stdin")) {
  602. pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE);
  603. if(!pwdbio) {
  604. BIO_printf(err, "Can't open BIO for stdin\n");
  605. return NULL;
  606. }
  607. } else {
  608. BIO_printf(err, "Invalid password argument \"%s\"\n", arg);
  609. return NULL;
  610. }
  611. }
  612. i = BIO_gets(pwdbio, tpass, APP_PASS_LEN);
  613. if(keepbio != 1) {
  614. BIO_free_all(pwdbio);
  615. pwdbio = NULL;
  616. }
  617. if(i <= 0) {
  618. BIO_printf(err, "Error reading password from BIO\n");
  619. return NULL;
  620. }
  621. tmp = strchr(tpass, '\n');
  622. if(tmp) *tmp = 0;
  623. return BUF_strdup(tpass);
  624. }
  625. int add_oid_section(BIO *err, CONF *conf)
  626. {
  627. char *p;
  628. STACK_OF(CONF_VALUE) *sktmp;
  629. CONF_VALUE *cnf;
  630. int i;
  631. if(!(p=NCONF_get_string(conf,NULL,"oid_section")))
  632. {
  633. ERR_clear_error();
  634. return 1;
  635. }
  636. if(!(sktmp = NCONF_get_section(conf, p))) {
  637. BIO_printf(err, "problem loading oid section %s\n", p);
  638. return 0;
  639. }
  640. for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
  641. cnf = sk_CONF_VALUE_value(sktmp, i);
  642. if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
  643. BIO_printf(err, "problem creating object %s=%s\n",
  644. cnf->name, cnf->value);
  645. return 0;
  646. }
  647. }
  648. return 1;
  649. }
  650. static int load_pkcs12(BIO *err, BIO *in, const char *desc,
  651. pem_password_cb *pem_cb, void *cb_data,
  652. EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca)
  653. {
  654. const char *pass;
  655. char tpass[PEM_BUFSIZE];
  656. int len, ret = 0;
  657. PKCS12 *p12;
  658. p12 = d2i_PKCS12_bio(in, NULL);
  659. if (p12 == NULL)
  660. {
  661. BIO_printf(err, "Error loading PKCS12 file for %s\n", desc);
  662. goto die;
  663. }
  664. /* See if an empty password will do */
  665. if (PKCS12_verify_mac(p12, "", 0) || PKCS12_verify_mac(p12, NULL, 0))
  666. pass = "";
  667. else
  668. {
  669. if (!pem_cb)
  670. pem_cb = (pem_password_cb *)password_callback;
  671. len = pem_cb(tpass, PEM_BUFSIZE, 0, cb_data);
  672. if (len < 0)
  673. {
  674. BIO_printf(err, "Passpharse callback error for %s\n",
  675. desc);
  676. goto die;
  677. }
  678. if (len < PEM_BUFSIZE)
  679. tpass[len] = 0;
  680. if (!PKCS12_verify_mac(p12, tpass, len))
  681. {
  682. BIO_printf(err,
  683. "Mac verify error (wrong password?) in PKCS12 file for %s\n", desc);
  684. goto die;
  685. }
  686. pass = tpass;
  687. }
  688. ret = PKCS12_parse(p12, pass, pkey, cert, ca);
  689. die:
  690. if (p12)
  691. PKCS12_free(p12);
  692. return ret;
  693. }
  694. X509 *load_cert(BIO *err, const char *file, int format,
  695. const char *pass, ENGINE *e, const char *cert_descrip)
  696. {
  697. ASN1_HEADER *ah=NULL;
  698. BUF_MEM *buf=NULL;
  699. X509 *x=NULL;
  700. BIO *cert;
  701. if ((cert=BIO_new(BIO_s_file())) == NULL)
  702. {
  703. ERR_print_errors(err);
  704. goto end;
  705. }
  706. if (file == NULL)
  707. {
  708. setvbuf(stdin, NULL, _IONBF, 0);
  709. BIO_set_fp(cert,stdin,BIO_NOCLOSE);
  710. }
  711. else
  712. {
  713. if (BIO_read_filename(cert,file) <= 0)
  714. {
  715. BIO_printf(err, "Error opening %s %s\n",
  716. cert_descrip, file);
  717. ERR_print_errors(err);
  718. goto end;
  719. }
  720. }
  721. if (format == FORMAT_ASN1)
  722. x=d2i_X509_bio(cert,NULL);
  723. else if (format == FORMAT_NETSCAPE)
  724. {
  725. const unsigned char *p,*op;
  726. int size=0,i;
  727. /* We sort of have to do it this way because it is sort of nice
  728. * to read the header first and check it, then
  729. * try to read the certificate */
  730. buf=BUF_MEM_new();
  731. for (;;)
  732. {
  733. if ((buf == NULL) || (!BUF_MEM_grow(buf,size+1024*10)))
  734. goto end;
  735. i=BIO_read(cert,&(buf->data[size]),1024*10);
  736. size+=i;
  737. if (i == 0) break;
  738. if (i < 0)
  739. {
  740. perror("reading certificate");
  741. goto end;
  742. }
  743. }
  744. p=(unsigned char *)buf->data;
  745. op=p;
  746. /* First load the header */
  747. if ((ah=d2i_ASN1_HEADER(NULL,&p,(long)size)) == NULL)
  748. goto end;
  749. if ((ah->header == NULL) || (ah->header->data == NULL) ||
  750. (strncmp(NETSCAPE_CERT_HDR,(char *)ah->header->data,
  751. ah->header->length) != 0))
  752. {
  753. BIO_printf(err,"Error reading header on certificate\n");
  754. goto end;
  755. }
  756. /* header is ok, so now read the object */
  757. p=op;
  758. ah->meth=X509_asn1_meth();
  759. if ((ah=d2i_ASN1_HEADER(&ah,&p,(long)size)) == NULL)
  760. goto end;
  761. x=(X509 *)ah->data;
  762. ah->data=NULL;
  763. }
  764. else if (format == FORMAT_PEM)
  765. x=PEM_read_bio_X509_AUX(cert,NULL,
  766. (pem_password_cb *)password_callback, NULL);
  767. else if (format == FORMAT_PKCS12)
  768. {
  769. if (!load_pkcs12(err, cert,cert_descrip, NULL, NULL,
  770. NULL, &x, NULL))
  771. goto end;
  772. }
  773. else {
  774. BIO_printf(err,"bad input format specified for %s\n",
  775. cert_descrip);
  776. goto end;
  777. }
  778. end:
  779. if (x == NULL)
  780. {
  781. BIO_printf(err,"unable to load certificate\n");
  782. ERR_print_errors(err);
  783. }
  784. if (ah != NULL) ASN1_HEADER_free(ah);
  785. if (cert != NULL) BIO_free(cert);
  786. if (buf != NULL) BUF_MEM_free(buf);
  787. return(x);
  788. }
  789. EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin,
  790. const char *pass, ENGINE *e, const char *key_descrip)
  791. {
  792. BIO *key=NULL;
  793. EVP_PKEY *pkey=NULL;
  794. PW_CB_DATA cb_data;
  795. cb_data.password = pass;
  796. cb_data.prompt_info = file;
  797. if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
  798. {
  799. BIO_printf(err,"no keyfile specified\n");
  800. goto end;
  801. }
  802. #ifndef OPENSSL_NO_ENGINE
  803. if (format == FORMAT_ENGINE)
  804. {
  805. if (!e)
  806. BIO_printf(bio_err,"no engine specified\n");
  807. else
  808. pkey = ENGINE_load_private_key(e, file,
  809. ui_method, &cb_data);
  810. goto end;
  811. }
  812. #endif
  813. key=BIO_new(BIO_s_file());
  814. if (key == NULL)
  815. {
  816. ERR_print_errors(err);
  817. goto end;
  818. }
  819. if (file == NULL && maybe_stdin)
  820. {
  821. setvbuf(stdin, NULL, _IONBF, 0);
  822. BIO_set_fp(key,stdin,BIO_NOCLOSE);
  823. }
  824. else
  825. if (BIO_read_filename(key,file) <= 0)
  826. {
  827. BIO_printf(err, "Error opening %s %s\n",
  828. key_descrip, file);
  829. ERR_print_errors(err);
  830. goto end;
  831. }
  832. if (format == FORMAT_ASN1)
  833. {
  834. pkey=d2i_PrivateKey_bio(key, NULL);
  835. }
  836. else if (format == FORMAT_PEM)
  837. {
  838. pkey=PEM_read_bio_PrivateKey(key,NULL,
  839. (pem_password_cb *)password_callback, &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. {
  847. if (!load_pkcs12(err, key, key_descrip,
  848. (pem_password_cb *)password_callback, &cb_data,
  849. &pkey, NULL, NULL))
  850. goto end;
  851. }
  852. else
  853. {
  854. BIO_printf(err,"bad input format specified for key file\n");
  855. goto end;
  856. }
  857. end:
  858. if (key != NULL) BIO_free(key);
  859. if (pkey == NULL)
  860. BIO_printf(err,"unable to load %s\n", key_descrip);
  861. return(pkey);
  862. }
  863. EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin,
  864. const char *pass, ENGINE *e, const char *key_descrip)
  865. {
  866. BIO *key=NULL;
  867. EVP_PKEY *pkey=NULL;
  868. PW_CB_DATA cb_data;
  869. cb_data.password = pass;
  870. cb_data.prompt_info = file;
  871. if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
  872. {
  873. BIO_printf(err,"no keyfile specified\n");
  874. goto end;
  875. }
  876. #ifndef OPENSSL_NO_ENGINE
  877. if (format == FORMAT_ENGINE)
  878. {
  879. if (!e)
  880. BIO_printf(bio_err,"no engine specified\n");
  881. else
  882. pkey = ENGINE_load_public_key(e, file,
  883. ui_method, &cb_data);
  884. goto end;
  885. }
  886. #endif
  887. key=BIO_new(BIO_s_file());
  888. if (key == NULL)
  889. {
  890. ERR_print_errors(err);
  891. goto end;
  892. }
  893. if (file == NULL && maybe_stdin)
  894. {
  895. setvbuf(stdin, NULL, _IONBF, 0);
  896. BIO_set_fp(key,stdin,BIO_NOCLOSE);
  897. }
  898. else
  899. if (BIO_read_filename(key,file) <= 0)
  900. {
  901. BIO_printf(err, "Error opening %s %s\n",
  902. key_descrip, file);
  903. ERR_print_errors(err);
  904. goto end;
  905. }
  906. if (format == FORMAT_ASN1)
  907. {
  908. pkey=d2i_PUBKEY_bio(key, NULL);
  909. }
  910. else if (format == FORMAT_PEM)
  911. {
  912. pkey=PEM_read_bio_PUBKEY(key,NULL,
  913. (pem_password_cb *)password_callback, &cb_data);
  914. }
  915. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  916. else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
  917. pkey = load_netscape_key(err, key, file, key_descrip, format);
  918. #endif
  919. else
  920. {
  921. BIO_printf(err,"bad input format specified for key file\n");
  922. goto end;
  923. }
  924. end:
  925. if (key != NULL) BIO_free(key);
  926. if (pkey == NULL)
  927. BIO_printf(err,"unable to load %s\n", key_descrip);
  928. return(pkey);
  929. }
  930. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  931. static EVP_PKEY *
  932. load_netscape_key(BIO *err, BIO *key, const char *file,
  933. const char *key_descrip, int format)
  934. {
  935. EVP_PKEY *pkey;
  936. BUF_MEM *buf;
  937. RSA *rsa;
  938. const unsigned char *p;
  939. int size, i;
  940. buf=BUF_MEM_new();
  941. pkey = EVP_PKEY_new();
  942. size = 0;
  943. if (buf == NULL || pkey == NULL)
  944. goto error;
  945. for (;;)
  946. {
  947. if (!BUF_MEM_grow_clean(buf,size+1024*10))
  948. goto error;
  949. i = BIO_read(key, &(buf->data[size]), 1024*10);
  950. size += i;
  951. if (i == 0)
  952. break;
  953. if (i < 0)
  954. {
  955. BIO_printf(err, "Error reading %s %s",
  956. key_descrip, file);
  957. goto error;
  958. }
  959. }
  960. p=(unsigned char *)buf->data;
  961. rsa = d2i_RSA_NET(NULL,&p,(long)size,NULL,
  962. (format == FORMAT_IISSGC ? 1 : 0));
  963. if (rsa == NULL)
  964. goto error;
  965. BUF_MEM_free(buf);
  966. EVP_PKEY_set1_RSA(pkey, rsa);
  967. return pkey;
  968. error:
  969. BUF_MEM_free(buf);
  970. EVP_PKEY_free(pkey);
  971. return NULL;
  972. }
  973. #endif /* ndef OPENSSL_NO_RC4 */
  974. STACK_OF(X509) *load_certs(BIO *err, const char *file, int format,
  975. const char *pass, ENGINE *e, const char *cert_descrip)
  976. {
  977. BIO *certs;
  978. int i;
  979. STACK_OF(X509) *othercerts = NULL;
  980. STACK_OF(X509_INFO) *allcerts = NULL;
  981. X509_INFO *xi;
  982. PW_CB_DATA cb_data;
  983. cb_data.password = pass;
  984. cb_data.prompt_info = file;
  985. if((certs = BIO_new(BIO_s_file())) == NULL)
  986. {
  987. ERR_print_errors(err);
  988. goto end;
  989. }
  990. if (file == NULL)
  991. BIO_set_fp(certs,stdin,BIO_NOCLOSE);
  992. else
  993. {
  994. if (BIO_read_filename(certs,file) <= 0)
  995. {
  996. BIO_printf(err, "Error opening %s %s\n",
  997. cert_descrip, file);
  998. ERR_print_errors(err);
  999. goto end;
  1000. }
  1001. }
  1002. if (format == FORMAT_PEM)
  1003. {
  1004. othercerts = sk_X509_new_null();
  1005. if(!othercerts)
  1006. {
  1007. sk_X509_free(othercerts);
  1008. othercerts = NULL;
  1009. goto end;
  1010. }
  1011. allcerts = PEM_X509_INFO_read_bio(certs, NULL,
  1012. (pem_password_cb *)password_callback, &cb_data);
  1013. for(i = 0; i < sk_X509_INFO_num(allcerts); i++)
  1014. {
  1015. xi = sk_X509_INFO_value (allcerts, i);
  1016. if (xi->x509)
  1017. {
  1018. sk_X509_push(othercerts, xi->x509);
  1019. xi->x509 = NULL;
  1020. }
  1021. }
  1022. goto end;
  1023. }
  1024. else {
  1025. BIO_printf(err,"bad input format specified for %s\n",
  1026. cert_descrip);
  1027. goto end;
  1028. }
  1029. end:
  1030. if (othercerts == NULL)
  1031. {
  1032. BIO_printf(err,"unable to load certificates\n");
  1033. ERR_print_errors(err);
  1034. }
  1035. if (allcerts) sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
  1036. if (certs != NULL) BIO_free(certs);
  1037. return(othercerts);
  1038. }
  1039. #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
  1040. /* Return error for unknown extensions */
  1041. #define X509V3_EXT_DEFAULT 0
  1042. /* Print error for unknown extensions */
  1043. #define X509V3_EXT_ERROR_UNKNOWN (1L << 16)
  1044. /* ASN1 parse unknown extensions */
  1045. #define X509V3_EXT_PARSE_UNKNOWN (2L << 16)
  1046. /* BIO_dump unknown extensions */
  1047. #define X509V3_EXT_DUMP_UNKNOWN (3L << 16)
  1048. #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \
  1049. X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION)
  1050. int set_cert_ex(unsigned long *flags, const char *arg)
  1051. {
  1052. static const NAME_EX_TBL cert_tbl[] = {
  1053. { "compatible", X509_FLAG_COMPAT, 0xffffffffl},
  1054. { "ca_default", X509_FLAG_CA, 0xffffffffl},
  1055. { "no_header", X509_FLAG_NO_HEADER, 0},
  1056. { "no_version", X509_FLAG_NO_VERSION, 0},
  1057. { "no_serial", X509_FLAG_NO_SERIAL, 0},
  1058. { "no_signame", X509_FLAG_NO_SIGNAME, 0},
  1059. { "no_validity", X509_FLAG_NO_VALIDITY, 0},
  1060. { "no_subject", X509_FLAG_NO_SUBJECT, 0},
  1061. { "no_issuer", X509_FLAG_NO_ISSUER, 0},
  1062. { "no_pubkey", X509_FLAG_NO_PUBKEY, 0},
  1063. { "no_extensions", X509_FLAG_NO_EXTENSIONS, 0},
  1064. { "no_sigdump", X509_FLAG_NO_SIGDUMP, 0},
  1065. { "no_aux", X509_FLAG_NO_AUX, 0},
  1066. { "no_attributes", X509_FLAG_NO_ATTRIBUTES, 0},
  1067. { "ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK},
  1068. { "ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1069. { "ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1070. { "ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1071. { NULL, 0, 0}
  1072. };
  1073. return set_multi_opts(flags, arg, cert_tbl);
  1074. }
  1075. int set_name_ex(unsigned long *flags, const char *arg)
  1076. {
  1077. static const NAME_EX_TBL ex_tbl[] = {
  1078. { "esc_2253", ASN1_STRFLGS_ESC_2253, 0},
  1079. { "esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0},
  1080. { "esc_msb", ASN1_STRFLGS_ESC_MSB, 0},
  1081. { "use_quote", ASN1_STRFLGS_ESC_QUOTE, 0},
  1082. { "utf8", ASN1_STRFLGS_UTF8_CONVERT, 0},
  1083. { "ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0},
  1084. { "show_type", ASN1_STRFLGS_SHOW_TYPE, 0},
  1085. { "dump_all", ASN1_STRFLGS_DUMP_ALL, 0},
  1086. { "dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0},
  1087. { "dump_der", ASN1_STRFLGS_DUMP_DER, 0},
  1088. { "compat", XN_FLAG_COMPAT, 0xffffffffL},
  1089. { "sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK},
  1090. { "sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK},
  1091. { "sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK},
  1092. { "sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK},
  1093. { "dn_rev", XN_FLAG_DN_REV, 0},
  1094. { "nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK},
  1095. { "sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK},
  1096. { "lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK},
  1097. { "align", XN_FLAG_FN_ALIGN, 0},
  1098. { "oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK},
  1099. { "space_eq", XN_FLAG_SPC_EQ, 0},
  1100. { "dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0},
  1101. { "RFC2253", XN_FLAG_RFC2253, 0xffffffffL},
  1102. { "oneline", XN_FLAG_ONELINE, 0xffffffffL},
  1103. { "multiline", XN_FLAG_MULTILINE, 0xffffffffL},
  1104. { "ca_default", XN_FLAG_MULTILINE, 0xffffffffL},
  1105. { NULL, 0, 0}
  1106. };
  1107. return set_multi_opts(flags, arg, ex_tbl);
  1108. }
  1109. int set_ext_copy(int *copy_type, const char *arg)
  1110. {
  1111. if (!strcasecmp(arg, "none"))
  1112. *copy_type = EXT_COPY_NONE;
  1113. else if (!strcasecmp(arg, "copy"))
  1114. *copy_type = EXT_COPY_ADD;
  1115. else if (!strcasecmp(arg, "copyall"))
  1116. *copy_type = EXT_COPY_ALL;
  1117. else
  1118. return 0;
  1119. return 1;
  1120. }
  1121. int copy_extensions(X509 *x, X509_REQ *req, int copy_type)
  1122. {
  1123. STACK_OF(X509_EXTENSION) *exts = NULL;
  1124. X509_EXTENSION *ext, *tmpext;
  1125. ASN1_OBJECT *obj;
  1126. int i, idx, ret = 0;
  1127. if (!x || !req || (copy_type == EXT_COPY_NONE))
  1128. return 1;
  1129. exts = X509_REQ_get_extensions(req);
  1130. for(i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  1131. ext = sk_X509_EXTENSION_value(exts, i);
  1132. obj = X509_EXTENSION_get_object(ext);
  1133. idx = X509_get_ext_by_OBJ(x, obj, -1);
  1134. /* Does extension exist? */
  1135. if (idx != -1) {
  1136. /* If normal copy don't override existing extension */
  1137. if (copy_type == EXT_COPY_ADD)
  1138. continue;
  1139. /* Delete all extensions of same type */
  1140. do {
  1141. tmpext = X509_get_ext(x, idx);
  1142. X509_delete_ext(x, idx);
  1143. X509_EXTENSION_free(tmpext);
  1144. idx = X509_get_ext_by_OBJ(x, obj, -1);
  1145. } while (idx != -1);
  1146. }
  1147. if (!X509_add_ext(x, ext, -1))
  1148. goto end;
  1149. }
  1150. ret = 1;
  1151. end:
  1152. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  1153. return ret;
  1154. }
  1155. static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
  1156. {
  1157. STACK_OF(CONF_VALUE) *vals;
  1158. CONF_VALUE *val;
  1159. int i, ret = 1;
  1160. if(!arg) return 0;
  1161. vals = X509V3_parse_list(arg);
  1162. for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
  1163. val = sk_CONF_VALUE_value(vals, i);
  1164. if (!set_table_opts(flags, val->name, in_tbl))
  1165. ret = 0;
  1166. }
  1167. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  1168. return ret;
  1169. }
  1170. static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
  1171. {
  1172. char c;
  1173. const NAME_EX_TBL *ptbl;
  1174. c = arg[0];
  1175. if(c == '-') {
  1176. c = 0;
  1177. arg++;
  1178. } else if (c == '+') {
  1179. c = 1;
  1180. arg++;
  1181. } else c = 1;
  1182. for(ptbl = in_tbl; ptbl->name; ptbl++) {
  1183. if(!strcasecmp(arg, ptbl->name)) {
  1184. *flags &= ~ptbl->mask;
  1185. if(c) *flags |= ptbl->flag;
  1186. else *flags &= ~ptbl->flag;
  1187. return 1;
  1188. }
  1189. }
  1190. return 0;
  1191. }
  1192. void print_name(BIO *out, const char *title, X509_NAME *nm, unsigned long lflags)
  1193. {
  1194. char *buf;
  1195. char mline = 0;
  1196. int indent = 0;
  1197. if(title) BIO_puts(out, title);
  1198. if((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
  1199. mline = 1;
  1200. indent = 4;
  1201. }
  1202. if(lflags == XN_FLAG_COMPAT) {
  1203. buf = X509_NAME_oneline(nm, 0, 0);
  1204. BIO_puts(out, buf);
  1205. BIO_puts(out, "\n");
  1206. OPENSSL_free(buf);
  1207. } else {
  1208. if(mline) BIO_puts(out, "\n");
  1209. X509_NAME_print_ex(out, nm, indent, lflags);
  1210. BIO_puts(out, "\n");
  1211. }
  1212. }
  1213. X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath)
  1214. {
  1215. X509_STORE *store;
  1216. X509_LOOKUP *lookup;
  1217. if(!(store = X509_STORE_new())) goto end;
  1218. lookup=X509_STORE_add_lookup(store,X509_LOOKUP_file());
  1219. if (lookup == NULL) goto end;
  1220. if (CAfile) {
  1221. if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
  1222. BIO_printf(bp, "Error loading file %s\n", CAfile);
  1223. goto end;
  1224. }
  1225. } else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
  1226. lookup=X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
  1227. if (lookup == NULL) goto end;
  1228. if (CApath) {
  1229. if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
  1230. BIO_printf(bp, "Error loading directory %s\n", CApath);
  1231. goto end;
  1232. }
  1233. } else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
  1234. ERR_clear_error();
  1235. return store;
  1236. end:
  1237. X509_STORE_free(store);
  1238. return NULL;
  1239. }
  1240. #ifndef OPENSSL_NO_ENGINE
  1241. /* Try to load an engine in a shareable library */
  1242. static ENGINE *try_load_engine(BIO *err, const char *engine, int debug)
  1243. {
  1244. ENGINE *e = ENGINE_by_id("dynamic");
  1245. if (e)
  1246. {
  1247. if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0)
  1248. || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
  1249. {
  1250. ENGINE_free(e);
  1251. e = NULL;
  1252. }
  1253. }
  1254. return e;
  1255. }
  1256. ENGINE *setup_engine(BIO *err, const char *engine, int debug)
  1257. {
  1258. ENGINE *e = NULL;
  1259. if (engine)
  1260. {
  1261. if(strcmp(engine, "auto") == 0)
  1262. {
  1263. BIO_printf(err,"enabling auto ENGINE support\n");
  1264. ENGINE_register_all_complete();
  1265. return NULL;
  1266. }
  1267. if((e = ENGINE_by_id(engine)) == NULL
  1268. && (e = try_load_engine(err, engine, debug)) == NULL)
  1269. {
  1270. BIO_printf(err,"invalid engine \"%s\"\n", engine);
  1271. ERR_print_errors(err);
  1272. return NULL;
  1273. }
  1274. if (debug)
  1275. {
  1276. ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM,
  1277. 0, err, 0);
  1278. }
  1279. ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, ui_method, 0, 1);
  1280. if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
  1281. {
  1282. BIO_printf(err,"can't use that engine\n");
  1283. ERR_print_errors(err);
  1284. ENGINE_free(e);
  1285. return NULL;
  1286. }
  1287. BIO_printf(err,"engine \"%s\" set.\n", ENGINE_get_id(e));
  1288. /* Free our "structural" reference. */
  1289. ENGINE_free(e);
  1290. }
  1291. return e;
  1292. }
  1293. #endif
  1294. int load_config(BIO *err, CONF *cnf)
  1295. {
  1296. if (!cnf)
  1297. cnf = config;
  1298. if (!cnf)
  1299. return 1;
  1300. OPENSSL_load_builtin_modules();
  1301. if (CONF_modules_load(cnf, NULL, 0) <= 0)
  1302. {
  1303. BIO_printf(err, "Error configuring OpenSSL\n");
  1304. ERR_print_errors(err);
  1305. return 0;
  1306. }
  1307. return 1;
  1308. }
  1309. char *make_config_name()
  1310. {
  1311. const char *t=X509_get_default_cert_area();
  1312. size_t len;
  1313. char *p;
  1314. len=strlen(t)+strlen(OPENSSL_CONF)+2;
  1315. p=OPENSSL_malloc(len);
  1316. BUF_strlcpy(p,t,len);
  1317. #ifndef OPENSSL_SYS_VMS
  1318. BUF_strlcat(p,"/",len);
  1319. #endif
  1320. BUF_strlcat(p,OPENSSL_CONF,len);
  1321. return p;
  1322. }
  1323. static unsigned long index_serial_hash(const char **a)
  1324. {
  1325. const char *n;
  1326. n=a[DB_serial];
  1327. while (*n == '0') n++;
  1328. return(lh_strhash(n));
  1329. }
  1330. static int index_serial_cmp(const char **a, const char **b)
  1331. {
  1332. const char *aa,*bb;
  1333. for (aa=a[DB_serial]; *aa == '0'; aa++);
  1334. for (bb=b[DB_serial]; *bb == '0'; bb++);
  1335. return(strcmp(aa,bb));
  1336. }
  1337. static int index_name_qual(char **a)
  1338. { return(a[0][0] == 'V'); }
  1339. static unsigned long index_name_hash(const char **a)
  1340. { return(lh_strhash(a[DB_name])); }
  1341. int index_name_cmp(const char **a, const char **b)
  1342. { return(strcmp(a[DB_name],
  1343. b[DB_name])); }
  1344. static IMPLEMENT_LHASH_HASH_FN(index_serial_hash,const char **)
  1345. static IMPLEMENT_LHASH_COMP_FN(index_serial_cmp,const char **)
  1346. static IMPLEMENT_LHASH_HASH_FN(index_name_hash,const char **)
  1347. static IMPLEMENT_LHASH_COMP_FN(index_name_cmp,const char **)
  1348. #undef BSIZE
  1349. #define BSIZE 256
  1350. BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai)
  1351. {
  1352. BIO *in=NULL;
  1353. BIGNUM *ret=NULL;
  1354. MS_STATIC char buf[1024];
  1355. ASN1_INTEGER *ai=NULL;
  1356. ai=ASN1_INTEGER_new();
  1357. if (ai == NULL) goto err;
  1358. if ((in=BIO_new(BIO_s_file())) == NULL)
  1359. {
  1360. ERR_print_errors(bio_err);
  1361. goto err;
  1362. }
  1363. if (BIO_read_filename(in,serialfile) <= 0)
  1364. {
  1365. if (!create)
  1366. {
  1367. perror(serialfile);
  1368. goto err;
  1369. }
  1370. else
  1371. {
  1372. ret=BN_new();
  1373. if (ret == NULL || !rand_serial(ret, ai))
  1374. BIO_printf(bio_err, "Out of memory\n");
  1375. }
  1376. }
  1377. else
  1378. {
  1379. if (!a2i_ASN1_INTEGER(in,ai,buf,1024))
  1380. {
  1381. BIO_printf(bio_err,"unable to load number from %s\n",
  1382. serialfile);
  1383. goto err;
  1384. }
  1385. ret=ASN1_INTEGER_to_BN(ai,NULL);
  1386. if (ret == NULL)
  1387. {
  1388. BIO_printf(bio_err,"error converting number from bin to BIGNUM\n");
  1389. goto err;
  1390. }
  1391. }
  1392. if (ret && retai)
  1393. {
  1394. *retai = ai;
  1395. ai = NULL;
  1396. }
  1397. err:
  1398. if (in != NULL) BIO_free(in);
  1399. if (ai != NULL) ASN1_INTEGER_free(ai);
  1400. return(ret);
  1401. }
  1402. int save_serial(char *serialfile, char *suffix, BIGNUM *serial, ASN1_INTEGER **retai)
  1403. {
  1404. char buf[1][BSIZE];
  1405. BIO *out = NULL;
  1406. int ret=0;
  1407. ASN1_INTEGER *ai=NULL;
  1408. int j;
  1409. if (suffix == NULL)
  1410. j = strlen(serialfile);
  1411. else
  1412. j = strlen(serialfile) + strlen(suffix) + 1;
  1413. if (j >= BSIZE)
  1414. {
  1415. BIO_printf(bio_err,"file name too long\n");
  1416. goto err;
  1417. }
  1418. if (suffix == NULL)
  1419. BUF_strlcpy(buf[0], serialfile, BSIZE);
  1420. else
  1421. {
  1422. #ifndef OPENSSL_SYS_VMS
  1423. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", serialfile, suffix);
  1424. #else
  1425. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", serialfile, suffix);
  1426. #endif
  1427. }
  1428. #ifdef RL_DEBUG
  1429. BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
  1430. #endif
  1431. out=BIO_new(BIO_s_file());
  1432. if (out == NULL)
  1433. {
  1434. ERR_print_errors(bio_err);
  1435. goto err;
  1436. }
  1437. if (BIO_write_filename(out,buf[0]) <= 0)
  1438. {
  1439. perror(serialfile);
  1440. goto err;
  1441. }
  1442. if ((ai=BN_to_ASN1_INTEGER(serial,NULL)) == NULL)
  1443. {
  1444. BIO_printf(bio_err,"error converting serial to ASN.1 format\n");
  1445. goto err;
  1446. }
  1447. i2a_ASN1_INTEGER(out,ai);
  1448. BIO_puts(out,"\n");
  1449. ret=1;
  1450. if (retai)
  1451. {
  1452. *retai = ai;
  1453. ai = NULL;
  1454. }
  1455. err:
  1456. if (out != NULL) BIO_free_all(out);
  1457. if (ai != NULL) ASN1_INTEGER_free(ai);
  1458. return(ret);
  1459. }
  1460. int rotate_serial(char *serialfile, char *new_suffix, char *old_suffix)
  1461. {
  1462. char buf[5][BSIZE];
  1463. int i,j;
  1464. struct stat sb;
  1465. i = strlen(serialfile) + strlen(old_suffix);
  1466. j = strlen(serialfile) + strlen(new_suffix);
  1467. if (i > j) j = i;
  1468. if (j + 1 >= BSIZE)
  1469. {
  1470. BIO_printf(bio_err,"file name too long\n");
  1471. goto err;
  1472. }
  1473. #ifndef OPENSSL_SYS_VMS
  1474. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s",
  1475. serialfile, new_suffix);
  1476. #else
  1477. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s",
  1478. serialfile, new_suffix);
  1479. #endif
  1480. #ifndef OPENSSL_SYS_VMS
  1481. j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s",
  1482. serialfile, old_suffix);
  1483. #else
  1484. j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s",
  1485. serialfile, old_suffix);
  1486. #endif
  1487. if (stat(serialfile,&sb) < 0)
  1488. {
  1489. if (errno != ENOENT
  1490. #ifdef ENOTDIR
  1491. && errno != ENOTDIR
  1492. #endif
  1493. )
  1494. goto err;
  1495. }
  1496. else
  1497. {
  1498. #ifdef RL_DEBUG
  1499. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1500. serialfile, buf[1]);
  1501. #endif
  1502. if (rename(serialfile,buf[1]) < 0)
  1503. {
  1504. BIO_printf(bio_err,
  1505. "unable to rename %s to %s\n",
  1506. serialfile, buf[1]);
  1507. perror("reason");
  1508. goto err;
  1509. }
  1510. }
  1511. #ifdef RL_DEBUG
  1512. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1513. buf[0],serialfile);
  1514. #endif
  1515. if (rename(buf[0],serialfile) < 0)
  1516. {
  1517. BIO_printf(bio_err,
  1518. "unable to rename %s to %s\n",
  1519. buf[0],serialfile);
  1520. perror("reason");
  1521. rename(buf[1],serialfile);
  1522. goto err;
  1523. }
  1524. return 1;
  1525. err:
  1526. return 0;
  1527. }
  1528. int rand_serial(BIGNUM *b, ASN1_INTEGER *ai)
  1529. {
  1530. BIGNUM *btmp;
  1531. int ret = 0;
  1532. if (b)
  1533. btmp = b;
  1534. else
  1535. btmp = BN_new();
  1536. if (!btmp)
  1537. return 0;
  1538. if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0))
  1539. goto error;
  1540. if (ai && !BN_to_ASN1_INTEGER(btmp, ai))
  1541. goto error;
  1542. ret = 1;
  1543. error:
  1544. if (!b)
  1545. BN_free(btmp);
  1546. return ret;
  1547. }
  1548. CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
  1549. {
  1550. CA_DB *retdb = NULL;
  1551. TXT_DB *tmpdb = NULL;
  1552. BIO *in = BIO_new(BIO_s_file());
  1553. CONF *dbattr_conf = NULL;
  1554. char buf[1][BSIZE];
  1555. long errorline= -1;
  1556. if (in == NULL)
  1557. {
  1558. ERR_print_errors(bio_err);
  1559. goto err;
  1560. }
  1561. if (BIO_read_filename(in,dbfile) <= 0)
  1562. {
  1563. perror(dbfile);
  1564. BIO_printf(bio_err,"unable to open '%s'\n",dbfile);
  1565. goto err;
  1566. }
  1567. if ((tmpdb = TXT_DB_read(in,DB_NUMBER)) == NULL)
  1568. {
  1569. if (tmpdb != NULL) TXT_DB_free(tmpdb);
  1570. goto err;
  1571. }
  1572. #ifndef OPENSSL_SYS_VMS
  1573. BIO_snprintf(buf[0], sizeof buf[0], "%s.attr", dbfile);
  1574. #else
  1575. BIO_snprintf(buf[0], sizeof buf[0], "%s-attr", dbfile);
  1576. #endif
  1577. dbattr_conf = NCONF_new(NULL);
  1578. if (NCONF_load(dbattr_conf,buf[0],&errorline) <= 0)
  1579. {
  1580. if (errorline > 0)
  1581. {
  1582. BIO_printf(bio_err,
  1583. "error on line %ld of db attribute file '%s'\n"
  1584. ,errorline,buf[0]);
  1585. goto err;
  1586. }
  1587. else
  1588. {
  1589. NCONF_free(dbattr_conf);
  1590. dbattr_conf = NULL;
  1591. }
  1592. }
  1593. if ((retdb = OPENSSL_malloc(sizeof(CA_DB))) == NULL)
  1594. {
  1595. fprintf(stderr, "Out of memory\n");
  1596. goto err;
  1597. }
  1598. retdb->db = tmpdb;
  1599. tmpdb = NULL;
  1600. if (db_attr)
  1601. retdb->attributes = *db_attr;
  1602. else
  1603. {
  1604. retdb->attributes.unique_subject = 1;
  1605. }
  1606. if (dbattr_conf)
  1607. {
  1608. char *p = NCONF_get_string(dbattr_conf,NULL,"unique_subject");
  1609. if (p)
  1610. {
  1611. #ifdef RL_DEBUG
  1612. BIO_printf(bio_err, "DEBUG[load_index]: unique_subject = \"%s\"\n", p);
  1613. #endif
  1614. retdb->attributes.unique_subject = parse_yesno(p,1);
  1615. }
  1616. }
  1617. err:
  1618. if (dbattr_conf) NCONF_free(dbattr_conf);
  1619. if (tmpdb) TXT_DB_free(tmpdb);
  1620. if (in) BIO_free_all(in);
  1621. return retdb;
  1622. }
  1623. int index_index(CA_DB *db)
  1624. {
  1625. if (!TXT_DB_create_index(db->db, DB_serial, NULL,
  1626. LHASH_HASH_FN(index_serial_hash),
  1627. LHASH_COMP_FN(index_serial_cmp)))
  1628. {
  1629. BIO_printf(bio_err,
  1630. "error creating serial number index:(%ld,%ld,%ld)\n",
  1631. db->db->error,db->db->arg1,db->db->arg2);
  1632. return 0;
  1633. }
  1634. if (db->attributes.unique_subject
  1635. && !TXT_DB_create_index(db->db, DB_name, index_name_qual,
  1636. LHASH_HASH_FN(index_name_hash),
  1637. LHASH_COMP_FN(index_name_cmp)))
  1638. {
  1639. BIO_printf(bio_err,"error creating name index:(%ld,%ld,%ld)\n",
  1640. db->db->error,db->db->arg1,db->db->arg2);
  1641. return 0;
  1642. }
  1643. return 1;
  1644. }
  1645. int save_index(const char *dbfile, const char *suffix, CA_DB *db)
  1646. {
  1647. char buf[3][BSIZE];
  1648. BIO *out = BIO_new(BIO_s_file());
  1649. int j;
  1650. if (out == NULL)
  1651. {
  1652. ERR_print_errors(bio_err);
  1653. goto err;
  1654. }
  1655. j = strlen(dbfile) + strlen(suffix);
  1656. if (j + 6 >= BSIZE)
  1657. {
  1658. BIO_printf(bio_err,"file name too long\n");
  1659. goto err;
  1660. }
  1661. #ifndef OPENSSL_SYS_VMS
  1662. j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr", dbfile);
  1663. #else
  1664. j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr", dbfile);
  1665. #endif
  1666. #ifndef OPENSSL_SYS_VMS
  1667. j = BIO_snprintf(buf[1], sizeof buf[1], "%s.attr.%s", dbfile, suffix);
  1668. #else
  1669. j = BIO_snprintf(buf[1], sizeof buf[1], "%s-attr-%s", dbfile, suffix);
  1670. #endif
  1671. #ifndef OPENSSL_SYS_VMS
  1672. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", dbfile, suffix);
  1673. #else
  1674. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", dbfile, suffix);
  1675. #endif
  1676. #ifdef RL_DEBUG
  1677. BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
  1678. #endif
  1679. if (BIO_write_filename(out,buf[0]) <= 0)
  1680. {
  1681. perror(dbfile);
  1682. BIO_printf(bio_err,"unable to open '%s'\n", dbfile);
  1683. goto err;
  1684. }
  1685. j=TXT_DB_write(out,db->db);
  1686. if (j <= 0) goto err;
  1687. BIO_free(out);
  1688. out = BIO_new(BIO_s_file());
  1689. #ifdef RL_DEBUG
  1690. BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[1]);
  1691. #endif
  1692. if (BIO_write_filename(out,buf[1]) <= 0)
  1693. {
  1694. perror(buf[2]);
  1695. BIO_printf(bio_err,"unable to open '%s'\n", buf[2]);
  1696. goto err;
  1697. }
  1698. BIO_printf(out,"unique_subject = %s\n",
  1699. db->attributes.unique_subject ? "yes" : "no");
  1700. BIO_free(out);
  1701. return 1;
  1702. err:
  1703. return 0;
  1704. }
  1705. int rotate_index(const char *dbfile, const char *new_suffix, const char *old_suffix)
  1706. {
  1707. char buf[5][BSIZE];
  1708. int i,j;
  1709. struct stat sb;
  1710. i = strlen(dbfile) + strlen(old_suffix);
  1711. j = strlen(dbfile) + strlen(new_suffix);
  1712. if (i > j) j = i;
  1713. if (j + 6 >= BSIZE)
  1714. {
  1715. BIO_printf(bio_err,"file name too long\n");
  1716. goto err;
  1717. }
  1718. #ifndef OPENSSL_SYS_VMS
  1719. j = BIO_snprintf(buf[4], sizeof buf[4], "%s.attr", dbfile);
  1720. #else
  1721. j = BIO_snprintf(buf[4], sizeof buf[4], "%s-attr", dbfile);
  1722. #endif
  1723. #ifndef OPENSSL_SYS_VMS
  1724. j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr.%s",
  1725. dbfile, new_suffix);
  1726. #else
  1727. j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr-%s",
  1728. dbfile, new_suffix);
  1729. #endif
  1730. #ifndef OPENSSL_SYS_VMS
  1731. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s",
  1732. dbfile, new_suffix);
  1733. #else
  1734. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s",
  1735. dbfile, new_suffix);
  1736. #endif
  1737. #ifndef OPENSSL_SYS_VMS
  1738. j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s",
  1739. dbfile, old_suffix);
  1740. #else
  1741. j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s",
  1742. dbfile, old_suffix);
  1743. #endif
  1744. #ifndef OPENSSL_SYS_VMS
  1745. j = BIO_snprintf(buf[3], sizeof buf[3], "%s.attr.%s",
  1746. dbfile, old_suffix);
  1747. #else
  1748. j = BIO_snprintf(buf[3], sizeof buf[3], "%s-attr-%s",
  1749. dbfile, old_suffix);
  1750. #endif
  1751. if (stat(dbfile,&sb) < 0)
  1752. {
  1753. if (errno != ENOENT
  1754. #ifdef ENOTDIR
  1755. && errno != ENOTDIR
  1756. #endif
  1757. )
  1758. goto err;
  1759. }
  1760. else
  1761. {
  1762. #ifdef RL_DEBUG
  1763. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1764. dbfile, buf[1]);
  1765. #endif
  1766. if (rename(dbfile,buf[1]) < 0)
  1767. {
  1768. BIO_printf(bio_err,
  1769. "unable to rename %s to %s\n",
  1770. dbfile, buf[1]);
  1771. perror("reason");
  1772. goto err;
  1773. }
  1774. }
  1775. #ifdef RL_DEBUG
  1776. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1777. buf[0],dbfile);
  1778. #endif
  1779. if (rename(buf[0],dbfile) < 0)
  1780. {
  1781. BIO_printf(bio_err,
  1782. "unable to rename %s to %s\n",
  1783. buf[0],dbfile);
  1784. perror("reason");
  1785. rename(buf[1],dbfile);
  1786. goto err;
  1787. }
  1788. if (stat(buf[4],&sb) < 0)
  1789. {
  1790. if (errno != ENOENT
  1791. #ifdef ENOTDIR
  1792. && errno != ENOTDIR
  1793. #endif
  1794. )
  1795. goto err;
  1796. }
  1797. else
  1798. {
  1799. #ifdef RL_DEBUG
  1800. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1801. buf[4],buf[3]);
  1802. #endif
  1803. if (rename(buf[4],buf[3]) < 0)
  1804. {
  1805. BIO_printf(bio_err,
  1806. "unable to rename %s to %s\n",
  1807. buf[4], buf[3]);
  1808. perror("reason");
  1809. rename(dbfile,buf[0]);
  1810. rename(buf[1],dbfile);
  1811. goto err;
  1812. }
  1813. }
  1814. #ifdef RL_DEBUG
  1815. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1816. buf[2],buf[4]);
  1817. #endif
  1818. if (rename(buf[2],buf[4]) < 0)
  1819. {
  1820. BIO_printf(bio_err,
  1821. "unable to rename %s to %s\n",
  1822. buf[2],buf[4]);
  1823. perror("reason");
  1824. rename(buf[3],buf[4]);
  1825. rename(dbfile,buf[0]);
  1826. rename(buf[1],dbfile);
  1827. goto err;
  1828. }
  1829. return 1;
  1830. err:
  1831. return 0;
  1832. }
  1833. void free_index(CA_DB *db)
  1834. {
  1835. if (db)
  1836. {
  1837. if (db->db) TXT_DB_free(db->db);
  1838. OPENSSL_free(db);
  1839. }
  1840. }
  1841. int parse_yesno(const char *str, int def)
  1842. {
  1843. int ret = def;
  1844. if (str)
  1845. {
  1846. switch (*str)
  1847. {
  1848. case 'f': /* false */
  1849. case 'F': /* FALSE */
  1850. case 'n': /* no */
  1851. case 'N': /* NO */
  1852. case '0': /* 0 */
  1853. ret = 0;
  1854. break;
  1855. case 't': /* true */
  1856. case 'T': /* TRUE */
  1857. case 'y': /* yes */
  1858. case 'Y': /* YES */
  1859. case '1': /* 1 */
  1860. ret = 0;
  1861. break;
  1862. default:
  1863. ret = def;
  1864. break;
  1865. }
  1866. }
  1867. return ret;
  1868. }
  1869. /*
  1870. * subject is expected to be in the format /type0=value0/type1=value1/type2=...
  1871. * where characters may be escaped by \
  1872. */
  1873. X509_NAME *parse_name(char *subject, long chtype, int multirdn)
  1874. {
  1875. size_t buflen = strlen(subject)+1; /* to copy the types and values into. due to escaping, the copy can only become shorter */
  1876. char *buf = OPENSSL_malloc(buflen);
  1877. size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */
  1878. char **ne_types = OPENSSL_malloc(max_ne * sizeof (char *));
  1879. char **ne_values = OPENSSL_malloc(max_ne * sizeof (char *));
  1880. int *mval = OPENSSL_malloc (max_ne * sizeof (int));
  1881. char *sp = subject, *bp = buf;
  1882. int i, ne_num = 0;
  1883. X509_NAME *n = NULL;
  1884. int nid;
  1885. if (!buf || !ne_types || !ne_values)
  1886. {
  1887. BIO_printf(bio_err, "malloc error\n");
  1888. goto error;
  1889. }
  1890. if (*subject != '/')
  1891. {
  1892. BIO_printf(bio_err, "Subject does not start with '/'.\n");
  1893. goto error;
  1894. }
  1895. sp++; /* skip leading / */
  1896. /* no multivalued RDN by default */
  1897. mval[ne_num] = 0;
  1898. while (*sp)
  1899. {
  1900. /* collect type */
  1901. ne_types[ne_num] = bp;
  1902. while (*sp)
  1903. {
  1904. if (*sp == '\\') /* is there anything to escape in the type...? */
  1905. {
  1906. if (*++sp)
  1907. *bp++ = *sp++;
  1908. else
  1909. {
  1910. BIO_printf(bio_err, "escape character at end of string\n");
  1911. goto error;
  1912. }
  1913. }
  1914. else if (*sp == '=')
  1915. {
  1916. sp++;
  1917. *bp++ = '\0';
  1918. break;
  1919. }
  1920. else
  1921. *bp++ = *sp++;
  1922. }
  1923. if (!*sp)
  1924. {
  1925. BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num);
  1926. goto error;
  1927. }
  1928. ne_values[ne_num] = bp;
  1929. while (*sp)
  1930. {
  1931. if (*sp == '\\')
  1932. {
  1933. if (*++sp)
  1934. *bp++ = *sp++;
  1935. else
  1936. {
  1937. BIO_printf(bio_err, "escape character at end of string\n");
  1938. goto error;
  1939. }
  1940. }
  1941. else if (*sp == '/')
  1942. {
  1943. sp++;
  1944. /* no multivalued RDN by default */
  1945. mval[ne_num+1] = 0;
  1946. break;
  1947. }
  1948. else if (*sp == '+' && multirdn)
  1949. {
  1950. /* a not escaped + signals a mutlivalued RDN */
  1951. sp++;
  1952. mval[ne_num+1] = -1;
  1953. break;
  1954. }
  1955. else
  1956. *bp++ = *sp++;
  1957. }
  1958. *bp++ = '\0';
  1959. ne_num++;
  1960. }
  1961. if (!(n = X509_NAME_new()))
  1962. goto error;
  1963. for (i = 0; i < ne_num; i++)
  1964. {
  1965. if ((nid=OBJ_txt2nid(ne_types[i])) == NID_undef)
  1966. {
  1967. BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]);
  1968. continue;
  1969. }
  1970. if (!*ne_values[i])
  1971. {
  1972. BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]);
  1973. continue;
  1974. }
  1975. if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char*)ne_values[i], -1,-1,mval[i]))
  1976. goto error;
  1977. }
  1978. OPENSSL_free(ne_values);
  1979. OPENSSL_free(ne_types);
  1980. OPENSSL_free(buf);
  1981. return n;
  1982. error:
  1983. X509_NAME_free(n);
  1984. if (ne_values)
  1985. OPENSSL_free(ne_values);
  1986. if (ne_types)
  1987. OPENSSL_free(ne_types);
  1988. if (buf)
  1989. OPENSSL_free(buf);
  1990. return NULL;
  1991. }
  1992. /* This code MUST COME AFTER anything that uses rename() */
  1993. #ifdef OPENSSL_SYS_WIN32
  1994. int WIN32_rename(const char *from, const char *to)
  1995. {
  1996. #ifndef OPENSSL_SYS_WINCE
  1997. /* Windows rename gives an error if 'to' exists, so delete it
  1998. * first and ignore file not found errror
  1999. */
  2000. if((remove(to) != 0) && (errno != ENOENT))
  2001. return -1;
  2002. #undef rename
  2003. return rename(from, to);
  2004. #else
  2005. /* convert strings to UNICODE */
  2006. {
  2007. BOOL result = FALSE;
  2008. WCHAR* wfrom;
  2009. WCHAR* wto;
  2010. int i;
  2011. wfrom = malloc((strlen(from)+1)*2);
  2012. wto = malloc((strlen(to)+1)*2);
  2013. if (wfrom != NULL && wto != NULL)
  2014. {
  2015. for (i=0; i<(int)strlen(from)+1; i++)
  2016. wfrom[i] = (short)from[i];
  2017. for (i=0; i<(int)strlen(to)+1; i++)
  2018. wto[i] = (short)to[i];
  2019. result = MoveFile(wfrom, wto);
  2020. }
  2021. if (wfrom != NULL)
  2022. free(wfrom);
  2023. if (wto != NULL)
  2024. free(wto);
  2025. return result;
  2026. }
  2027. #endif
  2028. }
  2029. #endif
  2030. int args_verify(char ***pargs, int *pargc,
  2031. int *badarg, BIO *err, X509_VERIFY_PARAM **pm)
  2032. {
  2033. ASN1_OBJECT *otmp = NULL;
  2034. unsigned long flags = 0;
  2035. int i;
  2036. int purpose = 0;
  2037. char **oldargs = *pargs;
  2038. char *arg = **pargs, *argn = (*pargs)[1];
  2039. if (!strcmp(arg, "-policy"))
  2040. {
  2041. if (!argn)
  2042. *badarg = 1;
  2043. else
  2044. {
  2045. otmp = OBJ_txt2obj(argn, 0);
  2046. if (!otmp)
  2047. {
  2048. BIO_printf(err, "Invalid Policy \"%s\"\n",
  2049. argn);
  2050. *badarg = 1;
  2051. }
  2052. }
  2053. (*pargs)++;
  2054. }
  2055. else if (strcmp(arg,"-purpose") == 0)
  2056. {
  2057. X509_PURPOSE *xptmp;
  2058. if (!argn)
  2059. *badarg = 1;
  2060. else
  2061. {
  2062. i = X509_PURPOSE_get_by_sname(argn);
  2063. if(i < 0)
  2064. {
  2065. BIO_printf(err, "unrecognized purpose\n");
  2066. *badarg = 1;
  2067. }
  2068. else
  2069. {
  2070. xptmp = X509_PURPOSE_get0(i);
  2071. purpose = X509_PURPOSE_get_id(xptmp);
  2072. }
  2073. }
  2074. (*pargs)++;
  2075. }
  2076. else if (!strcmp(arg, "-ignore_critical"))
  2077. flags |= X509_V_FLAG_IGNORE_CRITICAL;
  2078. else if (!strcmp(arg, "-issuer_checks"))
  2079. flags |= X509_V_FLAG_CB_ISSUER_CHECK;
  2080. else if (!strcmp(arg, "-crl_check"))
  2081. flags |= X509_V_FLAG_CRL_CHECK;
  2082. else if (!strcmp(arg, "-crl_check_all"))
  2083. flags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
  2084. else if (!strcmp(arg, "-policy_check"))
  2085. flags |= X509_V_FLAG_POLICY_CHECK;
  2086. else if (!strcmp(arg, "-explicit_policy"))
  2087. flags |= X509_V_FLAG_EXPLICIT_POLICY;
  2088. else if (!strcmp(arg, "-x509_strict"))
  2089. flags |= X509_V_FLAG_X509_STRICT;
  2090. else if (!strcmp(arg, "-policy_print"))
  2091. flags |= X509_V_FLAG_NOTIFY_POLICY;
  2092. else
  2093. return 0;
  2094. if (*badarg)
  2095. {
  2096. if (*pm)
  2097. X509_VERIFY_PARAM_free(*pm);
  2098. *pm = NULL;
  2099. goto end;
  2100. }
  2101. if (!*pm && !(*pm = X509_VERIFY_PARAM_new()))
  2102. {
  2103. *badarg = 1;
  2104. goto end;
  2105. }
  2106. if (otmp)
  2107. X509_VERIFY_PARAM_add0_policy(*pm, otmp);
  2108. if (flags)
  2109. X509_VERIFY_PARAM_set_flags(*pm, flags);
  2110. if (purpose)
  2111. X509_VERIFY_PARAM_set_purpose(*pm, purpose);
  2112. end:
  2113. (*pargs)++;
  2114. if (pargc)
  2115. *pargc -= *pargs - oldargs;
  2116. return 1;
  2117. }
  2118. static void nodes_print(BIO *out, const char *name,
  2119. STACK_OF(X509_POLICY_NODE) *nodes)
  2120. {
  2121. X509_POLICY_NODE *node;
  2122. int i;
  2123. BIO_printf(out, "%s Policies:", name);
  2124. if (nodes)
  2125. {
  2126. BIO_puts(out, "\n");
  2127. for (i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++)
  2128. {
  2129. node = sk_X509_POLICY_NODE_value(nodes, i);
  2130. X509_POLICY_NODE_print(out, node, 2);
  2131. }
  2132. }
  2133. else
  2134. BIO_puts(out, " <empty>\n");
  2135. }
  2136. void policies_print(BIO *out, X509_STORE_CTX *ctx)
  2137. {
  2138. X509_POLICY_TREE *tree;
  2139. int explicit_policy;
  2140. int free_out = 0;
  2141. if (out == NULL)
  2142. {
  2143. out = BIO_new_fp(stderr, BIO_NOCLOSE);
  2144. free_out = 1;
  2145. }
  2146. tree = X509_STORE_CTX_get0_policy_tree(ctx);
  2147. explicit_policy = X509_STORE_CTX_get_explicit_policy(ctx);
  2148. BIO_printf(out, "Require explicit Policy: %s\n",
  2149. explicit_policy ? "True" : "False");
  2150. nodes_print(out, "Authority", X509_policy_tree_get0_policies(tree));
  2151. nodes_print(out, "User", X509_policy_tree_get0_user_policies(tree));
  2152. if (free_out)
  2153. BIO_free(out);
  2154. }