apps.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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. #include <openssl/engine.h>
  125. #ifdef OPENSSL_SYS_WINDOWS
  126. #define strcasecmp _stricmp
  127. #else
  128. # ifdef NO_STRINGS_H
  129. int strcasecmp();
  130. # else
  131. # include <strings.h>
  132. # endif /* NO_STRINGS_H */
  133. #endif
  134. #define NON_MAIN
  135. #include "apps.h"
  136. #undef NON_MAIN
  137. #ifdef OPENSSL_SYS_WINDOWS
  138. # include "bss_file.c"
  139. #endif
  140. typedef struct {
  141. char *name;
  142. unsigned long flag;
  143. unsigned long mask;
  144. } NAME_EX_TBL;
  145. static UI_METHOD *ui_method = NULL;
  146. static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
  147. static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
  148. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  149. /* Looks like this stuff is worth moving into separate function */
  150. static EVP_PKEY *
  151. load_netscape_key(BIO *err, BIO *key, const char *file,
  152. const char *key_descrip, int format);
  153. #endif
  154. int app_init(long mesgwin);
  155. #ifdef undef /* never finished - probably never will be :-) */
  156. int args_from_file(char *file, int *argc, char **argv[])
  157. {
  158. FILE *fp;
  159. int num,i;
  160. unsigned int len;
  161. static char *buf=NULL;
  162. static char **arg=NULL;
  163. char *p;
  164. struct stat stbuf;
  165. if (stat(file,&stbuf) < 0) return(0);
  166. fp=fopen(file,"r");
  167. if (fp == NULL)
  168. return(0);
  169. *argc=0;
  170. *argv=NULL;
  171. len=(unsigned int)stbuf.st_size;
  172. if (buf != NULL) OPENSSL_free(buf);
  173. buf=(char *)OPENSSL_malloc(len+1);
  174. if (buf == NULL) return(0);
  175. len=fread(buf,1,len,fp);
  176. if (len <= 1) return(0);
  177. buf[len]='\0';
  178. i=0;
  179. for (p=buf; *p; p++)
  180. if (*p == '\n') i++;
  181. if (arg != NULL) OPENSSL_free(arg);
  182. arg=(char **)OPENSSL_malloc(sizeof(char *)*(i*2));
  183. *argv=arg;
  184. num=0;
  185. p=buf;
  186. for (;;)
  187. {
  188. if (!*p) break;
  189. if (*p == '#') /* comment line */
  190. {
  191. while (*p && (*p != '\n')) p++;
  192. continue;
  193. }
  194. /* else we have a line */
  195. *(arg++)=p;
  196. num++;
  197. while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
  198. p++;
  199. if (!*p) break;
  200. if (*p == '\n')
  201. {
  202. *(p++)='\0';
  203. continue;
  204. }
  205. /* else it is a tab or space */
  206. p++;
  207. while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
  208. p++;
  209. if (!*p) break;
  210. if (*p == '\n')
  211. {
  212. p++;
  213. continue;
  214. }
  215. *(arg++)=p++;
  216. num++;
  217. while (*p && (*p != '\n')) p++;
  218. if (!*p) break;
  219. /* else *p == '\n' */
  220. *(p++)='\0';
  221. }
  222. *argc=num;
  223. return(1);
  224. }
  225. #endif
  226. int str2fmt(char *s)
  227. {
  228. if ((*s == 'D') || (*s == 'd'))
  229. return(FORMAT_ASN1);
  230. else if ((*s == 'T') || (*s == 't'))
  231. return(FORMAT_TEXT);
  232. else if ((*s == 'P') || (*s == 'p'))
  233. return(FORMAT_PEM);
  234. else if ((*s == 'N') || (*s == 'n'))
  235. return(FORMAT_NETSCAPE);
  236. else if ((*s == 'S') || (*s == 's'))
  237. return(FORMAT_SMIME);
  238. else if ((*s == '1')
  239. || (strcmp(s,"PKCS12") == 0) || (strcmp(s,"pkcs12") == 0)
  240. || (strcmp(s,"P12") == 0) || (strcmp(s,"p12") == 0))
  241. return(FORMAT_PKCS12);
  242. else if ((*s == 'E') || (*s == 'e'))
  243. return(FORMAT_ENGINE);
  244. else
  245. return(FORMAT_UNDEF);
  246. }
  247. #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
  248. void program_name(char *in, char *out, int size)
  249. {
  250. int i,n;
  251. char *p=NULL;
  252. n=strlen(in);
  253. /* find the last '/', '\' or ':' */
  254. for (i=n-1; i>0; i--)
  255. {
  256. if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':'))
  257. {
  258. p= &(in[i+1]);
  259. break;
  260. }
  261. }
  262. if (p == NULL)
  263. p=in;
  264. n=strlen(p);
  265. /* strip off trailing .exe if present. */
  266. if ((n > 4) && (p[n-4] == '.') &&
  267. ((p[n-3] == 'e') || (p[n-3] == 'E')) &&
  268. ((p[n-2] == 'x') || (p[n-2] == 'X')) &&
  269. ((p[n-1] == 'e') || (p[n-1] == 'E')))
  270. n-=4;
  271. if (n > size-1)
  272. n=size-1;
  273. for (i=0; i<n; i++)
  274. {
  275. if ((p[i] >= 'A') && (p[i] <= 'Z'))
  276. out[i]=p[i]-'A'+'a';
  277. else
  278. out[i]=p[i];
  279. }
  280. out[n]='\0';
  281. }
  282. #else
  283. #ifdef OPENSSL_SYS_VMS
  284. void program_name(char *in, char *out, int size)
  285. {
  286. char *p=in, *q;
  287. char *chars=":]>";
  288. while(*chars != '\0')
  289. {
  290. q=strrchr(p,*chars);
  291. if (q > p)
  292. p = q + 1;
  293. chars++;
  294. }
  295. q=strrchr(p,'.');
  296. if (q == NULL)
  297. q = p + strlen(p);
  298. strncpy(out,p,size-1);
  299. if (q-p >= size)
  300. {
  301. out[size-1]='\0';
  302. }
  303. else
  304. {
  305. out[q-p]='\0';
  306. }
  307. }
  308. #else
  309. void program_name(char *in, char *out, int size)
  310. {
  311. char *p;
  312. p=strrchr(in,'/');
  313. if (p != NULL)
  314. p++;
  315. else
  316. p=in;
  317. BUF_strlcpy(out,p,size);
  318. }
  319. #endif
  320. #endif
  321. #ifdef OPENSSL_SYS_WIN32
  322. int WIN32_rename(char *from, char *to)
  323. {
  324. #ifndef OPENSSL_SYS_WINCE
  325. /* Windows rename gives an error if 'to' exists, so delete it
  326. * first and ignore file not found errror
  327. */
  328. if((remove(to) != 0) && (errno != ENOENT))
  329. return -1;
  330. #undef rename
  331. return rename(from, to);
  332. #else
  333. /* convert strings to UNICODE */
  334. {
  335. BOOL result = FALSE;
  336. WCHAR* wfrom;
  337. WCHAR* wto;
  338. int i;
  339. wfrom = malloc((strlen(from)+1)*2);
  340. wto = malloc((strlen(to)+1)*2);
  341. if (wfrom != NULL && wto != NULL)
  342. {
  343. for (i=0; i<(int)strlen(from)+1; i++)
  344. wfrom[i] = (short)from[i];
  345. for (i=0; i<(int)strlen(to)+1; i++)
  346. wto[i] = (short)to[i];
  347. result = MoveFile(wfrom, wto);
  348. }
  349. if (wfrom != NULL)
  350. free(wfrom);
  351. if (wto != NULL)
  352. free(wto);
  353. return result;
  354. }
  355. #endif
  356. }
  357. #endif
  358. #ifdef OPENSSL_SYS_VMS
  359. int VMS_strcasecmp(const char *str1, const char *str2)
  360. {
  361. while (*str1 && *str2)
  362. {
  363. int res = toupper(*str1) - toupper(*str2);
  364. if (res) return res < 0 ? -1 : 1;
  365. }
  366. if (*str1)
  367. return 1;
  368. if (*str2)
  369. return -1;
  370. return 0;
  371. }
  372. #endif
  373. int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
  374. {
  375. int num,len,i;
  376. char *p;
  377. *argc=0;
  378. *argv=NULL;
  379. len=strlen(buf);
  380. i=0;
  381. if (arg->count == 0)
  382. {
  383. arg->count=20;
  384. arg->data=(char **)OPENSSL_malloc(sizeof(char *)*arg->count);
  385. }
  386. for (i=0; i<arg->count; i++)
  387. arg->data[i]=NULL;
  388. num=0;
  389. p=buf;
  390. for (;;)
  391. {
  392. /* first scan over white space */
  393. if (!*p) break;
  394. while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
  395. p++;
  396. if (!*p) break;
  397. /* The start of something good :-) */
  398. if (num >= arg->count)
  399. {
  400. arg->count+=20;
  401. arg->data=(char **)OPENSSL_realloc(arg->data,
  402. sizeof(char *)*arg->count);
  403. if (argc == 0) return(0);
  404. }
  405. arg->data[num++]=p;
  406. /* now look for the end of this */
  407. if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */
  408. {
  409. i= *(p++);
  410. arg->data[num-1]++; /* jump over quote */
  411. while (*p && (*p != i))
  412. p++;
  413. *p='\0';
  414. }
  415. else
  416. {
  417. while (*p && ((*p != ' ') &&
  418. (*p != '\t') && (*p != '\n')))
  419. p++;
  420. if (*p == '\0')
  421. p--;
  422. else
  423. *p='\0';
  424. }
  425. p++;
  426. }
  427. *argc=num;
  428. *argv=arg->data;
  429. return(1);
  430. }
  431. #ifndef APP_INIT
  432. int app_init(long mesgwin)
  433. {
  434. return(1);
  435. }
  436. #endif
  437. int dump_cert_text (BIO *out, X509 *x)
  438. {
  439. char *p;
  440. p=X509_NAME_oneline(X509_get_subject_name(x),NULL,0);
  441. BIO_puts(out,"subject=");
  442. BIO_puts(out,p);
  443. OPENSSL_free(p);
  444. p=X509_NAME_oneline(X509_get_issuer_name(x),NULL,0);
  445. BIO_puts(out,"\nissuer=");
  446. BIO_puts(out,p);
  447. BIO_puts(out,"\n");
  448. OPENSSL_free(p);
  449. return 0;
  450. }
  451. static int ui_open(UI *ui)
  452. {
  453. return UI_method_get_opener(UI_OpenSSL())(ui);
  454. }
  455. static int ui_read(UI *ui, UI_STRING *uis)
  456. {
  457. if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
  458. && UI_get0_user_data(ui))
  459. {
  460. switch(UI_get_string_type(uis))
  461. {
  462. case UIT_PROMPT:
  463. case UIT_VERIFY:
  464. {
  465. const char *password =
  466. ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
  467. if (password[0] != '\0')
  468. {
  469. UI_set_result(ui, uis, password);
  470. return 1;
  471. }
  472. }
  473. default:
  474. break;
  475. }
  476. }
  477. return UI_method_get_reader(UI_OpenSSL())(ui, uis);
  478. }
  479. static int ui_write(UI *ui, UI_STRING *uis)
  480. {
  481. if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
  482. && UI_get0_user_data(ui))
  483. {
  484. switch(UI_get_string_type(uis))
  485. {
  486. case UIT_PROMPT:
  487. case UIT_VERIFY:
  488. {
  489. const char *password =
  490. ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
  491. if (password[0] != '\0')
  492. return 1;
  493. }
  494. default:
  495. break;
  496. }
  497. }
  498. return UI_method_get_writer(UI_OpenSSL())(ui, uis);
  499. }
  500. static int ui_close(UI *ui)
  501. {
  502. return UI_method_get_closer(UI_OpenSSL())(ui);
  503. }
  504. int setup_ui_method(void)
  505. {
  506. ui_method = UI_create_method("OpenSSL application user interface");
  507. UI_method_set_opener(ui_method, ui_open);
  508. UI_method_set_reader(ui_method, ui_read);
  509. UI_method_set_writer(ui_method, ui_write);
  510. UI_method_set_closer(ui_method, ui_close);
  511. return 0;
  512. }
  513. void destroy_ui_method(void)
  514. {
  515. if(ui_method)
  516. {
  517. UI_destroy_method(ui_method);
  518. ui_method = NULL;
  519. }
  520. }
  521. int password_callback(char *buf, int bufsiz, int verify,
  522. PW_CB_DATA *cb_tmp)
  523. {
  524. UI *ui = NULL;
  525. int res = 0;
  526. const char *prompt_info = NULL;
  527. const char *password = NULL;
  528. PW_CB_DATA *cb_data = (PW_CB_DATA *)cb_tmp;
  529. if (cb_data)
  530. {
  531. if (cb_data->password)
  532. password = cb_data->password;
  533. if (cb_data->prompt_info)
  534. prompt_info = cb_data->prompt_info;
  535. }
  536. if (password)
  537. {
  538. res = strlen(password);
  539. if (res > bufsiz)
  540. res = bufsiz;
  541. memcpy(buf, password, res);
  542. return res;
  543. }
  544. ui = UI_new_method(ui_method);
  545. if (ui)
  546. {
  547. int ok = 0;
  548. char *buff = NULL;
  549. int ui_flags = 0;
  550. char *prompt = NULL;
  551. prompt = UI_construct_prompt(ui, "pass phrase",
  552. cb_data->prompt_info);
  553. ui_flags |= UI_INPUT_FLAG_DEFAULT_PWD;
  554. UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0);
  555. if (ok >= 0)
  556. ok = UI_add_input_string(ui,prompt,ui_flags,buf,
  557. PW_MIN_LENGTH,BUFSIZ-1);
  558. if (ok >= 0 && verify)
  559. {
  560. buff = (char *)OPENSSL_malloc(bufsiz);
  561. ok = UI_add_verify_string(ui,prompt,ui_flags,buff,
  562. PW_MIN_LENGTH,BUFSIZ-1, buf);
  563. }
  564. if (ok >= 0)
  565. do
  566. {
  567. ok = UI_process(ui);
  568. }
  569. while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0));
  570. if (buff)
  571. {
  572. OPENSSL_cleanse(buff,(unsigned int)bufsiz);
  573. OPENSSL_free(buff);
  574. }
  575. if (ok >= 0)
  576. res = strlen(buf);
  577. if (ok == -1)
  578. {
  579. BIO_printf(bio_err, "User interface error\n");
  580. ERR_print_errors(bio_err);
  581. OPENSSL_cleanse(buf,(unsigned int)bufsiz);
  582. res = 0;
  583. }
  584. if (ok == -2)
  585. {
  586. BIO_printf(bio_err,"aborted!\n");
  587. OPENSSL_cleanse(buf,(unsigned int)bufsiz);
  588. res = 0;
  589. }
  590. UI_free(ui);
  591. OPENSSL_free(prompt);
  592. }
  593. return res;
  594. }
  595. static char *app_get_pass(BIO *err, char *arg, int keepbio);
  596. int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2)
  597. {
  598. int same;
  599. if(!arg2 || !arg1 || strcmp(arg1, arg2)) same = 0;
  600. else same = 1;
  601. if(arg1) {
  602. *pass1 = app_get_pass(err, arg1, same);
  603. if(!*pass1) return 0;
  604. } else if(pass1) *pass1 = NULL;
  605. if(arg2) {
  606. *pass2 = app_get_pass(err, arg2, same ? 2 : 0);
  607. if(!*pass2) return 0;
  608. } else if(pass2) *pass2 = NULL;
  609. return 1;
  610. }
  611. static char *app_get_pass(BIO *err, char *arg, int keepbio)
  612. {
  613. char *tmp, tpass[APP_PASS_LEN];
  614. static BIO *pwdbio = NULL;
  615. int i;
  616. if(!strncmp(arg, "pass:", 5)) return BUF_strdup(arg + 5);
  617. if(!strncmp(arg, "env:", 4)) {
  618. tmp = getenv(arg + 4);
  619. if(!tmp) {
  620. BIO_printf(err, "Can't read environment variable %s\n", arg + 4);
  621. return NULL;
  622. }
  623. return BUF_strdup(tmp);
  624. }
  625. if(!keepbio || !pwdbio) {
  626. if(!strncmp(arg, "file:", 5)) {
  627. pwdbio = BIO_new_file(arg + 5, "r");
  628. if(!pwdbio) {
  629. BIO_printf(err, "Can't open file %s\n", arg + 5);
  630. return NULL;
  631. }
  632. } else if(!strncmp(arg, "fd:", 3)) {
  633. BIO *btmp;
  634. i = atoi(arg + 3);
  635. if(i >= 0) pwdbio = BIO_new_fd(i, BIO_NOCLOSE);
  636. if((i < 0) || !pwdbio) {
  637. BIO_printf(err, "Can't access file descriptor %s\n", arg + 3);
  638. return NULL;
  639. }
  640. /* Can't do BIO_gets on an fd BIO so add a buffering BIO */
  641. btmp = BIO_new(BIO_f_buffer());
  642. pwdbio = BIO_push(btmp, pwdbio);
  643. } else if(!strcmp(arg, "stdin")) {
  644. pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE);
  645. if(!pwdbio) {
  646. BIO_printf(err, "Can't open BIO for stdin\n");
  647. return NULL;
  648. }
  649. } else {
  650. BIO_printf(err, "Invalid password argument \"%s\"\n", arg);
  651. return NULL;
  652. }
  653. }
  654. i = BIO_gets(pwdbio, tpass, APP_PASS_LEN);
  655. if(keepbio != 1) {
  656. BIO_free_all(pwdbio);
  657. pwdbio = NULL;
  658. }
  659. if(i <= 0) {
  660. BIO_printf(err, "Error reading password from BIO\n");
  661. return NULL;
  662. }
  663. tmp = strchr(tpass, '\n');
  664. if(tmp) *tmp = 0;
  665. return BUF_strdup(tpass);
  666. }
  667. int add_oid_section(BIO *err, CONF *conf)
  668. {
  669. char *p;
  670. STACK_OF(CONF_VALUE) *sktmp;
  671. CONF_VALUE *cnf;
  672. int i;
  673. if(!(p=NCONF_get_string(conf,NULL,"oid_section")))
  674. {
  675. ERR_clear_error();
  676. return 1;
  677. }
  678. if(!(sktmp = NCONF_get_section(conf, p))) {
  679. BIO_printf(err, "problem loading oid section %s\n", p);
  680. return 0;
  681. }
  682. for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
  683. cnf = sk_CONF_VALUE_value(sktmp, i);
  684. if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
  685. BIO_printf(err, "problem creating object %s=%s\n",
  686. cnf->name, cnf->value);
  687. return 0;
  688. }
  689. }
  690. return 1;
  691. }
  692. X509 *load_cert(BIO *err, const char *file, int format,
  693. const char *pass, ENGINE *e, const char *cert_descrip)
  694. {
  695. ASN1_HEADER *ah=NULL;
  696. BUF_MEM *buf=NULL;
  697. X509 *x=NULL;
  698. BIO *cert;
  699. if ((cert=BIO_new(BIO_s_file())) == NULL)
  700. {
  701. ERR_print_errors(err);
  702. goto end;
  703. }
  704. if (file == NULL)
  705. {
  706. setvbuf(stdin, NULL, _IONBF, 0);
  707. BIO_set_fp(cert,stdin,BIO_NOCLOSE);
  708. }
  709. else
  710. {
  711. if (BIO_read_filename(cert,file) <= 0)
  712. {
  713. BIO_printf(err, "Error opening %s %s\n",
  714. cert_descrip, file);
  715. ERR_print_errors(err);
  716. goto end;
  717. }
  718. }
  719. if (format == FORMAT_ASN1)
  720. x=d2i_X509_bio(cert,NULL);
  721. else if (format == FORMAT_NETSCAPE)
  722. {
  723. unsigned char *p,*op;
  724. int size=0,i;
  725. /* We sort of have to do it this way because it is sort of nice
  726. * to read the header first and check it, then
  727. * try to read the certificate */
  728. buf=BUF_MEM_new();
  729. for (;;)
  730. {
  731. if ((buf == NULL) || (!BUF_MEM_grow(buf,size+1024*10)))
  732. goto end;
  733. i=BIO_read(cert,&(buf->data[size]),1024*10);
  734. size+=i;
  735. if (i == 0) break;
  736. if (i < 0)
  737. {
  738. perror("reading certificate");
  739. goto end;
  740. }
  741. }
  742. p=(unsigned char *)buf->data;
  743. op=p;
  744. /* First load the header */
  745. if ((ah=d2i_ASN1_HEADER(NULL,&p,(long)size)) == NULL)
  746. goto end;
  747. if ((ah->header == NULL) || (ah->header->data == NULL) ||
  748. (strncmp(NETSCAPE_CERT_HDR,(char *)ah->header->data,
  749. ah->header->length) != 0))
  750. {
  751. BIO_printf(err,"Error reading header on certificate\n");
  752. goto end;
  753. }
  754. /* header is ok, so now read the object */
  755. p=op;
  756. ah->meth=X509_asn1_meth();
  757. if ((ah=d2i_ASN1_HEADER(&ah,&p,(long)size)) == NULL)
  758. goto end;
  759. x=(X509 *)ah->data;
  760. ah->data=NULL;
  761. }
  762. else if (format == FORMAT_PEM)
  763. x=PEM_read_bio_X509_AUX(cert,NULL,
  764. (pem_password_cb *)password_callback, NULL);
  765. else if (format == FORMAT_PKCS12)
  766. {
  767. PKCS12 *p12 = d2i_PKCS12_bio(cert, NULL);
  768. PKCS12_parse(p12, NULL, NULL, &x, NULL);
  769. PKCS12_free(p12);
  770. p12 = NULL;
  771. }
  772. else {
  773. BIO_printf(err,"bad input format specified for %s\n",
  774. cert_descrip);
  775. goto end;
  776. }
  777. end:
  778. if (x == NULL)
  779. {
  780. BIO_printf(err,"unable to load certificate\n");
  781. ERR_print_errors(err);
  782. }
  783. if (ah != NULL) ASN1_HEADER_free(ah);
  784. if (cert != NULL) BIO_free(cert);
  785. if (buf != NULL) BUF_MEM_free(buf);
  786. return(x);
  787. }
  788. EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin,
  789. const char *pass, ENGINE *e, const char *key_descrip)
  790. {
  791. BIO *key=NULL;
  792. EVP_PKEY *pkey=NULL;
  793. PW_CB_DATA cb_data;
  794. cb_data.password = pass;
  795. cb_data.prompt_info = file;
  796. if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
  797. {
  798. BIO_printf(err,"no keyfile specified\n");
  799. goto end;
  800. }
  801. if (format == FORMAT_ENGINE)
  802. {
  803. if (!e)
  804. BIO_printf(bio_err,"no engine specified\n");
  805. else
  806. pkey = ENGINE_load_private_key(e, file,
  807. ui_method, &cb_data);
  808. goto end;
  809. }
  810. key=BIO_new(BIO_s_file());
  811. if (key == NULL)
  812. {
  813. ERR_print_errors(err);
  814. goto end;
  815. }
  816. if (file == NULL && maybe_stdin)
  817. {
  818. setvbuf(stdin, NULL, _IONBF, 0);
  819. BIO_set_fp(key,stdin,BIO_NOCLOSE);
  820. }
  821. else
  822. if (BIO_read_filename(key,file) <= 0)
  823. {
  824. BIO_printf(err, "Error opening %s %s\n",
  825. key_descrip, file);
  826. ERR_print_errors(err);
  827. goto end;
  828. }
  829. if (format == FORMAT_ASN1)
  830. {
  831. pkey=d2i_PrivateKey_bio(key, NULL);
  832. }
  833. else if (format == FORMAT_PEM)
  834. {
  835. pkey=PEM_read_bio_PrivateKey(key,NULL,
  836. (pem_password_cb *)password_callback, &cb_data);
  837. }
  838. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  839. else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
  840. pkey = load_netscape_key(err, key, file, key_descrip, format);
  841. #endif
  842. else if (format == FORMAT_PKCS12)
  843. {
  844. PKCS12 *p12 = d2i_PKCS12_bio(key, NULL);
  845. PKCS12_parse(p12, pass, &pkey, NULL, NULL);
  846. PKCS12_free(p12);
  847. p12 = NULL;
  848. }
  849. else
  850. {
  851. BIO_printf(err,"bad input format specified for key file\n");
  852. goto end;
  853. }
  854. end:
  855. if (key != NULL) BIO_free(key);
  856. if (pkey == NULL)
  857. BIO_printf(err,"unable to load %s\n", key_descrip);
  858. return(pkey);
  859. }
  860. EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin,
  861. const char *pass, ENGINE *e, const char *key_descrip)
  862. {
  863. BIO *key=NULL;
  864. EVP_PKEY *pkey=NULL;
  865. PW_CB_DATA cb_data;
  866. cb_data.password = pass;
  867. cb_data.prompt_info = file;
  868. if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
  869. {
  870. BIO_printf(err,"no keyfile specified\n");
  871. goto end;
  872. }
  873. if (format == FORMAT_ENGINE)
  874. {
  875. if (!e)
  876. BIO_printf(bio_err,"no engine specified\n");
  877. else
  878. pkey = ENGINE_load_public_key(e, file,
  879. ui_method, &cb_data);
  880. goto end;
  881. }
  882. key=BIO_new(BIO_s_file());
  883. if (key == NULL)
  884. {
  885. ERR_print_errors(err);
  886. goto end;
  887. }
  888. if (file == NULL && maybe_stdin)
  889. {
  890. setvbuf(stdin, NULL, _IONBF, 0);
  891. BIO_set_fp(key,stdin,BIO_NOCLOSE);
  892. }
  893. else
  894. if (BIO_read_filename(key,file) <= 0)
  895. {
  896. BIO_printf(err, "Error opening %s %s\n",
  897. key_descrip, file);
  898. ERR_print_errors(err);
  899. goto end;
  900. }
  901. if (format == FORMAT_ASN1)
  902. {
  903. pkey=d2i_PUBKEY_bio(key, NULL);
  904. }
  905. else if (format == FORMAT_PEM)
  906. {
  907. pkey=PEM_read_bio_PUBKEY(key,NULL,
  908. (pem_password_cb *)password_callback, &cb_data);
  909. }
  910. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  911. else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
  912. pkey = load_netscape_key(err, key, file, key_descrip, format);
  913. #endif
  914. else
  915. {
  916. BIO_printf(err,"bad input format specified for key file\n");
  917. goto end;
  918. }
  919. end:
  920. if (key != NULL) BIO_free(key);
  921. if (pkey == NULL)
  922. BIO_printf(err,"unable to load %s\n", key_descrip);
  923. return(pkey);
  924. }
  925. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  926. static EVP_PKEY *
  927. load_netscape_key(BIO *err, BIO *key, const char *file,
  928. const char *key_descrip, int format)
  929. {
  930. EVP_PKEY *pkey;
  931. BUF_MEM *buf;
  932. RSA *rsa;
  933. const unsigned char *p;
  934. int size, i;
  935. buf=BUF_MEM_new();
  936. pkey = EVP_PKEY_new();
  937. size = 0;
  938. if (buf == NULL || pkey == NULL)
  939. goto error;
  940. for (;;)
  941. {
  942. if (!BUF_MEM_grow_clean(buf,size+1024*10))
  943. goto error;
  944. i = BIO_read(key, &(buf->data[size]), 1024*10);
  945. size += i;
  946. if (i == 0)
  947. break;
  948. if (i < 0)
  949. {
  950. BIO_printf(err, "Error reading %s %s",
  951. key_descrip, file);
  952. goto error;
  953. }
  954. }
  955. p=(unsigned char *)buf->data;
  956. rsa = d2i_RSA_NET(NULL,&p,(long)size,NULL,
  957. (format == FORMAT_IISSGC ? 1 : 0));
  958. if (rsa == NULL)
  959. goto error;
  960. BUF_MEM_free(buf);
  961. EVP_PKEY_set1_RSA(pkey, rsa);
  962. return pkey;
  963. error:
  964. BUF_MEM_free(buf);
  965. EVP_PKEY_free(pkey);
  966. return NULL;
  967. }
  968. #endif /* ndef OPENSSL_NO_RC4 */
  969. STACK_OF(X509) *load_certs(BIO *err, const char *file, int format,
  970. const char *pass, ENGINE *e, const char *cert_descrip)
  971. {
  972. BIO *certs;
  973. int i;
  974. STACK_OF(X509) *othercerts = NULL;
  975. STACK_OF(X509_INFO) *allcerts = NULL;
  976. X509_INFO *xi;
  977. PW_CB_DATA cb_data;
  978. cb_data.password = pass;
  979. cb_data.prompt_info = file;
  980. if((certs = BIO_new(BIO_s_file())) == NULL)
  981. {
  982. ERR_print_errors(err);
  983. goto end;
  984. }
  985. if (file == NULL)
  986. BIO_set_fp(certs,stdin,BIO_NOCLOSE);
  987. else
  988. {
  989. if (BIO_read_filename(certs,file) <= 0)
  990. {
  991. BIO_printf(err, "Error opening %s %s\n",
  992. cert_descrip, file);
  993. ERR_print_errors(err);
  994. goto end;
  995. }
  996. }
  997. if (format == FORMAT_PEM)
  998. {
  999. othercerts = sk_X509_new_null();
  1000. if(!othercerts)
  1001. {
  1002. sk_X509_free(othercerts);
  1003. othercerts = NULL;
  1004. goto end;
  1005. }
  1006. allcerts = PEM_X509_INFO_read_bio(certs, NULL,
  1007. (pem_password_cb *)password_callback, &cb_data);
  1008. for(i = 0; i < sk_X509_INFO_num(allcerts); i++)
  1009. {
  1010. xi = sk_X509_INFO_value (allcerts, i);
  1011. if (xi->x509)
  1012. {
  1013. sk_X509_push(othercerts, xi->x509);
  1014. xi->x509 = NULL;
  1015. }
  1016. }
  1017. goto end;
  1018. }
  1019. else {
  1020. BIO_printf(err,"bad input format specified for %s\n",
  1021. cert_descrip);
  1022. goto end;
  1023. }
  1024. end:
  1025. if (othercerts == NULL)
  1026. {
  1027. BIO_printf(err,"unable to load certificates\n");
  1028. ERR_print_errors(err);
  1029. }
  1030. if (allcerts) sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
  1031. if (certs != NULL) BIO_free(certs);
  1032. return(othercerts);
  1033. }
  1034. #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
  1035. /* Return error for unknown extensions */
  1036. #define X509V3_EXT_DEFAULT 0
  1037. /* Print error for unknown extensions */
  1038. #define X509V3_EXT_ERROR_UNKNOWN (1L << 16)
  1039. /* ASN1 parse unknown extensions */
  1040. #define X509V3_EXT_PARSE_UNKNOWN (2L << 16)
  1041. /* BIO_dump unknown extensions */
  1042. #define X509V3_EXT_DUMP_UNKNOWN (3L << 16)
  1043. #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \
  1044. X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION)
  1045. int set_cert_ex(unsigned long *flags, const char *arg)
  1046. {
  1047. static const NAME_EX_TBL cert_tbl[] = {
  1048. { "compatible", X509_FLAG_COMPAT, 0xffffffffl},
  1049. { "ca_default", X509_FLAG_CA, 0xffffffffl},
  1050. { "no_header", X509_FLAG_NO_HEADER, 0},
  1051. { "no_version", X509_FLAG_NO_VERSION, 0},
  1052. { "no_serial", X509_FLAG_NO_SERIAL, 0},
  1053. { "no_signame", X509_FLAG_NO_SIGNAME, 0},
  1054. { "no_validity", X509_FLAG_NO_VALIDITY, 0},
  1055. { "no_subject", X509_FLAG_NO_SUBJECT, 0},
  1056. { "no_issuer", X509_FLAG_NO_ISSUER, 0},
  1057. { "no_pubkey", X509_FLAG_NO_PUBKEY, 0},
  1058. { "no_extensions", X509_FLAG_NO_EXTENSIONS, 0},
  1059. { "no_sigdump", X509_FLAG_NO_SIGDUMP, 0},
  1060. { "no_aux", X509_FLAG_NO_AUX, 0},
  1061. { "no_attributes", X509_FLAG_NO_ATTRIBUTES, 0},
  1062. { "ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK},
  1063. { "ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1064. { "ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1065. { "ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1066. { NULL, 0, 0}
  1067. };
  1068. return set_multi_opts(flags, arg, cert_tbl);
  1069. }
  1070. int set_name_ex(unsigned long *flags, const char *arg)
  1071. {
  1072. static const NAME_EX_TBL ex_tbl[] = {
  1073. { "esc_2253", ASN1_STRFLGS_ESC_2253, 0},
  1074. { "esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0},
  1075. { "esc_msb", ASN1_STRFLGS_ESC_MSB, 0},
  1076. { "use_quote", ASN1_STRFLGS_ESC_QUOTE, 0},
  1077. { "utf8", ASN1_STRFLGS_UTF8_CONVERT, 0},
  1078. { "ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0},
  1079. { "show_type", ASN1_STRFLGS_SHOW_TYPE, 0},
  1080. { "dump_all", ASN1_STRFLGS_DUMP_ALL, 0},
  1081. { "dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0},
  1082. { "dump_der", ASN1_STRFLGS_DUMP_DER, 0},
  1083. { "compat", XN_FLAG_COMPAT, 0xffffffffL},
  1084. { "sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK},
  1085. { "sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK},
  1086. { "sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK},
  1087. { "sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK},
  1088. { "dn_rev", XN_FLAG_DN_REV, 0},
  1089. { "nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK},
  1090. { "sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK},
  1091. { "lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK},
  1092. { "align", XN_FLAG_FN_ALIGN, 0},
  1093. { "oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK},
  1094. { "space_eq", XN_FLAG_SPC_EQ, 0},
  1095. { "dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0},
  1096. { "RFC2253", XN_FLAG_RFC2253, 0xffffffffL},
  1097. { "oneline", XN_FLAG_ONELINE, 0xffffffffL},
  1098. { "multiline", XN_FLAG_MULTILINE, 0xffffffffL},
  1099. { "ca_default", XN_FLAG_MULTILINE, 0xffffffffL},
  1100. { NULL, 0, 0}
  1101. };
  1102. return set_multi_opts(flags, arg, ex_tbl);
  1103. }
  1104. int set_ext_copy(int *copy_type, const char *arg)
  1105. {
  1106. if (!strcasecmp(arg, "none"))
  1107. *copy_type = EXT_COPY_NONE;
  1108. else if (!strcasecmp(arg, "copy"))
  1109. *copy_type = EXT_COPY_ADD;
  1110. else if (!strcasecmp(arg, "copyall"))
  1111. *copy_type = EXT_COPY_ALL;
  1112. else
  1113. return 0;
  1114. return 1;
  1115. }
  1116. int copy_extensions(X509 *x, X509_REQ *req, int copy_type)
  1117. {
  1118. STACK_OF(X509_EXTENSION) *exts = NULL;
  1119. X509_EXTENSION *ext, *tmpext;
  1120. ASN1_OBJECT *obj;
  1121. int i, idx, ret = 0;
  1122. if (!x || !req || (copy_type == EXT_COPY_NONE))
  1123. return 1;
  1124. exts = X509_REQ_get_extensions(req);
  1125. for(i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  1126. ext = sk_X509_EXTENSION_value(exts, i);
  1127. obj = X509_EXTENSION_get_object(ext);
  1128. idx = X509_get_ext_by_OBJ(x, obj, -1);
  1129. /* Does extension exist? */
  1130. if (idx != -1) {
  1131. /* If normal copy don't override existing extension */
  1132. if (copy_type == EXT_COPY_ADD)
  1133. continue;
  1134. /* Delete all extensions of same type */
  1135. do {
  1136. tmpext = X509_get_ext(x, idx);
  1137. X509_delete_ext(x, idx);
  1138. X509_EXTENSION_free(tmpext);
  1139. idx = X509_get_ext_by_OBJ(x, obj, -1);
  1140. } while (idx != -1);
  1141. }
  1142. if (!X509_add_ext(x, ext, -1))
  1143. goto end;
  1144. }
  1145. ret = 1;
  1146. end:
  1147. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  1148. return ret;
  1149. }
  1150. static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
  1151. {
  1152. STACK_OF(CONF_VALUE) *vals;
  1153. CONF_VALUE *val;
  1154. int i, ret = 1;
  1155. if(!arg) return 0;
  1156. vals = X509V3_parse_list(arg);
  1157. for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
  1158. val = sk_CONF_VALUE_value(vals, i);
  1159. if (!set_table_opts(flags, val->name, in_tbl))
  1160. ret = 0;
  1161. }
  1162. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  1163. return ret;
  1164. }
  1165. static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
  1166. {
  1167. char c;
  1168. const NAME_EX_TBL *ptbl;
  1169. c = arg[0];
  1170. if(c == '-') {
  1171. c = 0;
  1172. arg++;
  1173. } else if (c == '+') {
  1174. c = 1;
  1175. arg++;
  1176. } else c = 1;
  1177. for(ptbl = in_tbl; ptbl->name; ptbl++) {
  1178. if(!strcasecmp(arg, ptbl->name)) {
  1179. *flags &= ~ptbl->mask;
  1180. if(c) *flags |= ptbl->flag;
  1181. else *flags &= ~ptbl->flag;
  1182. return 1;
  1183. }
  1184. }
  1185. return 0;
  1186. }
  1187. void print_name(BIO *out, char *title, X509_NAME *nm, unsigned long lflags)
  1188. {
  1189. char *buf;
  1190. char mline = 0;
  1191. int indent = 0;
  1192. if(title) BIO_puts(out, title);
  1193. if((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
  1194. mline = 1;
  1195. indent = 4;
  1196. }
  1197. if(lflags == XN_FLAG_COMPAT) {
  1198. buf = X509_NAME_oneline(nm, 0, 0);
  1199. BIO_puts(out, buf);
  1200. BIO_puts(out, "\n");
  1201. OPENSSL_free(buf);
  1202. } else {
  1203. if(mline) BIO_puts(out, "\n");
  1204. X509_NAME_print_ex(out, nm, indent, lflags);
  1205. BIO_puts(out, "\n");
  1206. }
  1207. }
  1208. X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath)
  1209. {
  1210. X509_STORE *store;
  1211. X509_LOOKUP *lookup;
  1212. if(!(store = X509_STORE_new())) goto end;
  1213. lookup=X509_STORE_add_lookup(store,X509_LOOKUP_file());
  1214. if (lookup == NULL) goto end;
  1215. if (CAfile) {
  1216. if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
  1217. BIO_printf(bp, "Error loading file %s\n", CAfile);
  1218. goto end;
  1219. }
  1220. } else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
  1221. lookup=X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
  1222. if (lookup == NULL) goto end;
  1223. if (CApath) {
  1224. if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
  1225. BIO_printf(bp, "Error loading directory %s\n", CApath);
  1226. goto end;
  1227. }
  1228. } else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
  1229. ERR_clear_error();
  1230. return store;
  1231. end:
  1232. X509_STORE_free(store);
  1233. return NULL;
  1234. }
  1235. /* Try to load an engine in a shareable library */
  1236. static ENGINE *try_load_engine(BIO *err, const char *engine, int debug)
  1237. {
  1238. ENGINE *e = ENGINE_by_id("dynamic");
  1239. if (e)
  1240. {
  1241. if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0)
  1242. || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
  1243. {
  1244. ENGINE_free(e);
  1245. e = NULL;
  1246. }
  1247. }
  1248. return e;
  1249. }
  1250. ENGINE *setup_engine(BIO *err, const char *engine, int debug)
  1251. {
  1252. ENGINE *e = NULL;
  1253. if (engine)
  1254. {
  1255. if(strcmp(engine, "auto") == 0)
  1256. {
  1257. BIO_printf(err,"enabling auto ENGINE support\n");
  1258. ENGINE_register_all_complete();
  1259. return NULL;
  1260. }
  1261. if((e = ENGINE_by_id(engine)) == NULL
  1262. && (e = try_load_engine(err, engine, debug)) == NULL)
  1263. {
  1264. BIO_printf(err,"invalid engine \"%s\"\n", engine);
  1265. ERR_print_errors(err);
  1266. return NULL;
  1267. }
  1268. if (debug)
  1269. {
  1270. ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM,
  1271. 0, err, 0);
  1272. }
  1273. ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, ui_method, 0, 1);
  1274. if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
  1275. {
  1276. BIO_printf(err,"can't use that engine\n");
  1277. ERR_print_errors(err);
  1278. ENGINE_free(e);
  1279. return NULL;
  1280. }
  1281. BIO_printf(err,"engine \"%s\" set.\n", ENGINE_get_id(e));
  1282. /* Free our "structural" reference. */
  1283. ENGINE_free(e);
  1284. }
  1285. return e;
  1286. }
  1287. int load_config(BIO *err, CONF *cnf)
  1288. {
  1289. if (!cnf)
  1290. cnf = config;
  1291. if (!cnf)
  1292. return 1;
  1293. OPENSSL_load_builtin_modules();
  1294. if (CONF_modules_load(cnf, NULL, 0) <= 0)
  1295. {
  1296. BIO_printf(err, "Error configuring OpenSSL\n");
  1297. ERR_print_errors(err);
  1298. return 0;
  1299. }
  1300. return 1;
  1301. }
  1302. char *make_config_name()
  1303. {
  1304. const char *t=X509_get_default_cert_area();
  1305. char *p;
  1306. p=OPENSSL_malloc(strlen(t)+strlen(OPENSSL_CONF)+2);
  1307. strcpy(p,t);
  1308. #ifndef OPENSSL_SYS_VMS
  1309. strcat(p,"/");
  1310. #endif
  1311. strcat(p,OPENSSL_CONF);
  1312. return p;
  1313. }