PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/libkeynote/keynote-verify.c

https://bitbucket.org/kmv/aeriebsd-src
C | 434 lines | 335 code | 69 blank | 30 comment | 66 complexity | 93510eb7141faa1f46757d1222a24192 MD5 | raw file
  1. /*
  2. * The author of this code is Angelos D. Keromytis (angelos@dsl.cis.upenn.edu)
  3. *
  4. * This code was written by Angelos D. Keromytis in Philadelphia, PA, USA,
  5. * in April-May 1998
  6. *
  7. * Copyright (C) 1998, 1999 by Angelos D. Keromytis.
  8. *
  9. * Permission to use, copy, and modify this software with or without fee
  10. * is hereby granted, provided that this entire notice is included in
  11. * all copies of any software which is or includes a copy or
  12. * modification of this software.
  13. *
  14. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
  15. * IMPLIED WARRANTY. IN PARTICULAR, THE AUTHORS MAKES NO
  16. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
  17. * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
  18. * PURPOSE.
  19. */
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <ctype.h>
  23. #include <fcntl.h>
  24. #include <getopt.h>
  25. #include <memory.h>
  26. #include <regex.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include "header.h"
  32. #include "keynote.h"
  33. void verifyusage(void);
  34. void
  35. verifyusage(void)
  36. {
  37. fprintf(stderr, "Arguments:\n");
  38. fprintf(stderr, "\t-h: This message\n");
  39. fprintf(stderr,
  40. "\t-r <valuelist>: Comma separated, ordered return-value list\n");
  41. fprintf(stderr, "\t-e <filename>: Environment settings\n");
  42. fprintf(stderr, "\t-l <filename>: Trusted (local) assertion\n");
  43. fprintf(stderr, "\t-k <filename>: File containing key\n");
  44. fprintf(stderr, "Followed by a list of:\n");
  45. fprintf(stderr, "\t<filename>: Non-local assertion\n");
  46. }
  47. void
  48. keynote_verify(int argc, char *argv[])
  49. {
  50. #ifdef LoopTesting
  51. int loopvar = 1000;
  52. #endif /* LoopTesting */
  53. int fd, i, ch, se = 0, cl = 8192, sk = 0, sl = 0, p, ac = argc;
  54. char *buf, **av = argv, **retv, **foov, *ptr;
  55. int numretv = 16, numret = 0, sn;
  56. struct stat sb;
  57. if (argc == 1)
  58. {
  59. verifyusage();
  60. exit(1);
  61. }
  62. if ((buf = (char *) calloc(cl, sizeof(char))) == (char *) NULL)
  63. {
  64. perror("calloc()");
  65. exit(1);
  66. }
  67. #ifdef LoopTesting
  68. while(loopvar--) {
  69. #endif /* LoopTesting */
  70. if ((retv = (char **) calloc(numretv, sizeof(char *))) == (char **) NULL)
  71. {
  72. perror("calloc()");
  73. exit(1);
  74. }
  75. /* "ac" and "av" are used for stress-testing, ignore otherwise */
  76. argv = av;
  77. argc = ac;
  78. sn = 0;
  79. opterr = 0;
  80. sessid = kn_init();
  81. if (sessid == -1)
  82. {
  83. fprintf(stderr, "kn_init() failed (errno %d).\n", keynote_errno);
  84. exit(keynote_errno);
  85. }
  86. while ((ch = getopt(argc, argv, "hqistl:e:k:r:")) != -1)
  87. {
  88. switch (ch)
  89. {
  90. case 'e':
  91. if (read_environment(optarg) == -1)
  92. exit(1);
  93. se = 1;
  94. break;
  95. case 'k':
  96. sk = 1;
  97. if ((fd = open(optarg, O_RDONLY, 0)) < 0)
  98. {
  99. perror(optarg);
  100. exit(1);
  101. }
  102. if (fstat(fd, &sb) < 0)
  103. {
  104. perror("fstat()");
  105. exit(1);
  106. }
  107. if (sb.st_size > cl - 1)
  108. {
  109. free(buf);
  110. cl = sb.st_size + 1;
  111. buf = (char *) calloc(cl, sizeof(char));
  112. if (buf == (char *) NULL)
  113. {
  114. perror("calloc()");
  115. exit(1);
  116. }
  117. }
  118. i = read(fd, buf, sb.st_size);
  119. if (i < 0)
  120. {
  121. perror("read()");
  122. exit(1);
  123. }
  124. close(fd);
  125. parse_key(buf);
  126. switch (keynote_errno)
  127. {
  128. case 0: /* No errors */
  129. break;
  130. case ERROR_SYNTAX:
  131. fprintf(stderr, "Syntax error adding authorizer "
  132. "%s\n", optarg);
  133. exit(1);
  134. case ERROR_MEMORY:
  135. perror("Out of memory.\n");
  136. exit(1);
  137. default:
  138. fprintf(stderr, "Unknown error (%d).\n",
  139. keynote_errno);
  140. }
  141. break;
  142. case 'h':
  143. verifyusage();
  144. exit(0);
  145. case 'r':
  146. if (sn != 0)
  147. {
  148. fprintf(stderr,
  149. "Do not define two sets of return values.\n");
  150. exit(1);
  151. }
  152. sn = 1;
  153. for (numret = 0;
  154. (ptr = strchr(optarg, ',')) != (char *) NULL;
  155. numret++)
  156. {
  157. /* Running out of memory */
  158. if (numret > numretv - 3)
  159. {
  160. numretv *= 2;
  161. foov = (char **) calloc(numretv, sizeof(char **));
  162. if (foov == (char **) NULL)
  163. {
  164. /*
  165. * If this were a real program, we 'd be freeing
  166. * retv here. Since we're exiting, we can be a
  167. * little sloppy.
  168. */
  169. perror("calloc()");
  170. exit(1);
  171. }
  172. memcpy(foov, retv, numretv * sizeof(char **));
  173. free(retv);
  174. retv = foov;
  175. }
  176. retv[numret] = (char *) calloc((ptr - optarg) + 1,
  177. sizeof(char));
  178. if (retv[numret] == (char *) NULL)
  179. {
  180. /* Comment from above applies here as well */
  181. perror("calloc()");
  182. exit(1);
  183. }
  184. /* Copy */
  185. memcpy(retv[numret], optarg, ptr - optarg);
  186. optarg = ptr + 1;
  187. }
  188. /* Last component */
  189. retv[numret] = (char *) strdup(optarg);
  190. if (retv[numret] == (char *) NULL)
  191. {
  192. perror("calloc()");
  193. exit(1);
  194. }
  195. numret++;
  196. break;
  197. case 'l':
  198. if ((fd = open(optarg, O_RDONLY, 0)) < 0)
  199. {
  200. perror(optarg);
  201. exit(1);
  202. }
  203. if (fstat(fd, &sb) < 0)
  204. {
  205. perror("fstat()");
  206. exit(1);
  207. }
  208. if (sb.st_size > cl - 1)
  209. {
  210. free(buf);
  211. cl = sb.st_size + 1;
  212. buf = (char *) calloc(cl, sizeof(char));
  213. if (buf == (char *) NULL)
  214. {
  215. perror("calloc()");
  216. exit(1);
  217. }
  218. }
  219. i = read(fd, buf, sb.st_size);
  220. if (i < 0)
  221. {
  222. perror("read()");
  223. exit(1);
  224. }
  225. close(fd);
  226. p = kn_add_assertion(sessid, buf, i, ASSERT_FLAG_LOCAL);
  227. if (p == -1)
  228. {
  229. fprintf(stderr,
  230. "Error for assertion in file <%s>, errno %d.\n",
  231. optarg, keynote_errno);
  232. keynote_errno = 0;
  233. }
  234. memset(buf, 0, sb.st_size);
  235. sl = 1;
  236. break;
  237. case '?':
  238. default:
  239. verifyusage();
  240. exit(1);
  241. }
  242. }
  243. argc -= optind;
  244. argv += optind;
  245. optind = 1;
  246. #ifdef LoopTesting
  247. optreset = 1;
  248. #endif /* LoopTesting */
  249. if (sn == 0)
  250. {
  251. fprintf(stderr,
  252. "Should set return values before evaluations begin.\n");
  253. exit(1);
  254. }
  255. if (se == 0)
  256. {
  257. fprintf(stderr, "Should set environment before evaluations begin.\n");
  258. exit(1);
  259. }
  260. if (sk == 0)
  261. {
  262. fprintf(stderr, "Should specify at least one action authorizer.\n");
  263. exit(1);
  264. }
  265. if (sl == 0)
  266. {
  267. fprintf(stderr,
  268. "Should specify at least one trusted assertion (POLICY).\n");
  269. exit(1);
  270. }
  271. while (argc--)
  272. {
  273. if ((fd = open(argv[argc], O_RDONLY, 0)) < 0)
  274. {
  275. perror(argv[argc]);
  276. exit(1);
  277. }
  278. if (fstat(fd, &sb) < 0)
  279. {
  280. perror("fstat()");
  281. exit(1);
  282. }
  283. if (sb.st_size > cl - 1)
  284. {
  285. free(buf);
  286. cl = sb.st_size + 1;
  287. buf = (char *) calloc(cl, sizeof(char));
  288. if (buf == (char *) NULL)
  289. {
  290. perror("calloc()");
  291. exit(1);
  292. }
  293. }
  294. i = read(fd, buf, sb.st_size);
  295. if (i < 0)
  296. {
  297. perror("read()");
  298. exit(1);
  299. }
  300. close(fd);
  301. p = kn_add_assertion(sessid, buf, i, 0);
  302. if (p == -1)
  303. {
  304. fprintf(stderr, "Error for assertion in file <%s>, errno %d.\n",
  305. argv[argc], keynote_errno);
  306. keynote_errno = 0;
  307. }
  308. memset(buf, 0, sb.st_size);
  309. }
  310. p = kn_do_query(sessid, retv, numret); /* Evaluation time */
  311. #ifndef LoopTesting
  312. printf("Query result = ");
  313. switch (keynote_errno)
  314. {
  315. case ERROR_MEMORY:
  316. printf("<out of memory>\n");
  317. exit(1);
  318. case ERROR_SYNTAX:
  319. printf("<uninitialized authorizers or all POLICY "
  320. "assertions are malformed!>\n");
  321. exit(1);
  322. case ERROR_NOTFOUND:
  323. printf("<session or other information not found!>\n");
  324. exit(1);
  325. case 0: /* No errors */
  326. break;
  327. default:
  328. printf("<should never happen (%d)!>\n", keynote_errno);
  329. exit(1);
  330. }
  331. printf("%s\n", retv[p]);
  332. #endif /* LoopTesting */
  333. keynote_errno = 0;
  334. while ((i = kn_get_failed(sessid, KEYNOTE_ERROR_MEMORY, 0)) != -1)
  335. {
  336. printf("Failed assertion %d due to memory error.\n", i);
  337. kn_remove_assertion(sessid, i);
  338. }
  339. while ((i = kn_get_failed(sessid, KEYNOTE_ERROR_SYNTAX, 0)) != -1)
  340. {
  341. printf("Failed assertion %d due to syntax or semantic error.\n", i);
  342. kn_remove_assertion(sessid, i);
  343. }
  344. while ((i = kn_get_failed(sessid, KEYNOTE_ERROR_SIGNATURE, 0)) != -1)
  345. {
  346. printf("Failed assertion %d due to signature verification failure.\n",
  347. i);
  348. kn_remove_assertion(sessid, i);
  349. }
  350. while ((i = kn_get_failed(sessid, KEYNOTE_ERROR_ANY, 0)) != -1)
  351. {
  352. printf("Failed assertion %d due to unspecified error.\n", i);
  353. kn_remove_assertion(sessid, i);
  354. }
  355. kn_close(sessid);
  356. #ifdef LoopTesting
  357. }
  358. #endif /* LoopTesting */
  359. /* This is a reminder that return values are not free'ed by KeyNote */
  360. for (sn = 0; sn < numret; sn++)
  361. free(retv[sn]);
  362. free(retv);
  363. retv = (char **) NULL;
  364. exit(0);
  365. }