aboutsummaryrefslogtreecommitdiffstats
path: root/info.c
diff options
context:
space:
mode:
authorCameron Katri <me@cameronkatri.com>2022-05-20 10:42:38 -0400
committerCameron Katri <me@cameronkatri.com>2022-05-20 10:42:38 -0400
commitaa035f73ce081b3f07247bd15860d72355a096b2 (patch)
tree170a83f5aa816f936df5ae906f20da3fb6b9404e /info.c
downloadtrustcache-aa035f73ce081b3f07247bd15860d72355a096b2.tar.gz
trustcache-aa035f73ce081b3f07247bd15860d72355a096b2.tar.zst
trustcache-aa035f73ce081b3f07247bd15860d72355a096b2.zip
Initial import
Diffstat (limited to 'info.c')
-rw-r--r--info.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/info.c b/info.c
new file mode 100644
index 0000000..c676586
--- /dev/null
+++ b/info.c
@@ -0,0 +1,162 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2022 Cameron Katri. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY CAMERON KATRI AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL CAMERON KATRI OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <getopt.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "trustcache.h"
+
+void print_header(struct trust_cache cache);
+void print_hash(uint8_t cdhash[CS_CDHASH_LEN], bool newline);
+void print_entry(struct trust_cache_entry1 entry);
+void print_entries(struct trust_cache cache);
+
+int
+tcinfo(int argc, char **argv)
+{
+ struct trust_cache cache;
+ bool headeronly = false, onlyhash = false;
+ int entrynum = -1;
+ const char *errstr = NULL;
+
+ int ch;
+ while ((ch = getopt(argc, argv, "che:")) != -1) {
+ switch (ch) {
+ case 'h':
+ headeronly = true;
+ break;
+ case 'e':
+ entrynum = strtonum(optarg, 1, INT_MAX, &errstr);
+ if (errstr != NULL) {
+ fprintf(stderr, "entry number is %s: %s\n", errstr, optarg);
+ exit(1);
+ }
+ break;
+ case 'c':
+ onlyhash = true;
+ break;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc == 0) {
+ return -1;
+ }
+
+ cache = opentrustcache(argv[0]);
+
+ if (entrynum == -1 && !onlyhash)
+ print_header(cache);
+ if (!headeronly) {
+ if (onlyhash) {
+ for (int i = 0; i < cache.num_entries; i++) {
+ if (cache.version == 0)
+ print_hash(cache.hashes[i], true);
+ else
+ print_hash(cache.entries[i].cdhash, true);
+ }
+ goto done;
+ }
+ if (entrynum != -1) {
+ if (entrynum > cache.num_entries) {
+ fprintf(stderr, "no entry %i\n", entrynum);
+ exit(1);
+ }
+ if (cache.version == 1) {
+ print_entry(cache.entries[entrynum - 1]);
+ } else if (cache.version == 0) {
+ print_hash(cache.hashes[entrynum - 1], true);
+ }
+ } else {
+ print_entries(cache);
+ }
+ }
+
+done:
+ free(cache.entries);
+
+ return 0;
+}
+
+void
+print_header(struct trust_cache cache)
+{
+ printf("version = %i\n", cache.version);
+ char out[37];
+ uuid_unparse(cache.uuid, out);
+ printf("uuid = %s\n", out);
+ printf("entry count = %i\n", cache.num_entries);
+}
+
+void
+print_entries(struct trust_cache cache)
+{
+ for (int i = 0; i < cache.num_entries; i++) {
+ if (cache.version == 0)
+ print_hash(cache.hashes[i], true);
+ else if (cache.version == 1)
+ print_entry(cache.entries[i]);
+ }
+}
+
+void
+print_entry(struct trust_cache_entry1 entry)
+{
+ print_hash(entry.cdhash, false);
+
+ switch (entry.flags) {
+ case CS_TRUST_CACHE_AMFID:
+ printf(" CS_TRUST_CACHE_AMFID ");
+ break;
+ case CS_TRUST_CACHE_ANE:
+ printf(" CS_TRUST_CACHE_ANE ");
+ break;
+ case CS_TRUST_CACHE_AMFID|CS_TRUST_CACHE_ANE:
+ printf(" CS_TRUST_CACHE_AMFID|CS_TRUST_CACHE_ANE ");
+ break;
+ default:
+ printf(" [none] ");
+ break;
+ }
+
+ printf("[%i]\n", entry.hash_type);
+}
+
+void
+print_hash(uint8_t cdhash[CS_CDHASH_LEN], bool newline)
+{
+ for (int j = 0; j < CS_CDHASH_LEN; j++) {
+ printf("%02x", cdhash[j]);
+ }
+ if (newline)
+ printf("\n");
+}