]> git.cameronkatri.com Git - trustcache.git/blob - info.c
Initial import
[trustcache.git] / info.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022 Cameron Katri. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY CAMERON KATRI AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL CAMERON KATRI OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <getopt.h>
29 #include <limits.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include "trustcache.h"
35
36 void print_header(struct trust_cache cache);
37 void print_hash(uint8_t cdhash[CS_CDHASH_LEN], bool newline);
38 void print_entry(struct trust_cache_entry1 entry);
39 void print_entries(struct trust_cache cache);
40
41 int
42 tcinfo(int argc, char **argv)
43 {
44 struct trust_cache cache;
45 bool headeronly = false, onlyhash = false;
46 int entrynum = -1;
47 const char *errstr = NULL;
48
49 int ch;
50 while ((ch = getopt(argc, argv, "che:")) != -1) {
51 switch (ch) {
52 case 'h':
53 headeronly = true;
54 break;
55 case 'e':
56 entrynum = strtonum(optarg, 1, INT_MAX, &errstr);
57 if (errstr != NULL) {
58 fprintf(stderr, "entry number is %s: %s\n", errstr, optarg);
59 exit(1);
60 }
61 break;
62 case 'c':
63 onlyhash = true;
64 break;
65 }
66 }
67
68 argc -= optind;
69 argv += optind;
70
71 if (argc == 0) {
72 return -1;
73 }
74
75 cache = opentrustcache(argv[0]);
76
77 if (entrynum == -1 && !onlyhash)
78 print_header(cache);
79 if (!headeronly) {
80 if (onlyhash) {
81 for (int i = 0; i < cache.num_entries; i++) {
82 if (cache.version == 0)
83 print_hash(cache.hashes[i], true);
84 else
85 print_hash(cache.entries[i].cdhash, true);
86 }
87 goto done;
88 }
89 if (entrynum != -1) {
90 if (entrynum > cache.num_entries) {
91 fprintf(stderr, "no entry %i\n", entrynum);
92 exit(1);
93 }
94 if (cache.version == 1) {
95 print_entry(cache.entries[entrynum - 1]);
96 } else if (cache.version == 0) {
97 print_hash(cache.hashes[entrynum - 1], true);
98 }
99 } else {
100 print_entries(cache);
101 }
102 }
103
104 done:
105 free(cache.entries);
106
107 return 0;
108 }
109
110 void
111 print_header(struct trust_cache cache)
112 {
113 printf("version = %i\n", cache.version);
114 char out[37];
115 uuid_unparse(cache.uuid, out);
116 printf("uuid = %s\n", out);
117 printf("entry count = %i\n", cache.num_entries);
118 }
119
120 void
121 print_entries(struct trust_cache cache)
122 {
123 for (int i = 0; i < cache.num_entries; i++) {
124 if (cache.version == 0)
125 print_hash(cache.hashes[i], true);
126 else if (cache.version == 1)
127 print_entry(cache.entries[i]);
128 }
129 }
130
131 void
132 print_entry(struct trust_cache_entry1 entry)
133 {
134 print_hash(entry.cdhash, false);
135
136 switch (entry.flags) {
137 case CS_TRUST_CACHE_AMFID:
138 printf(" CS_TRUST_CACHE_AMFID ");
139 break;
140 case CS_TRUST_CACHE_ANE:
141 printf(" CS_TRUST_CACHE_ANE ");
142 break;
143 case CS_TRUST_CACHE_AMFID|CS_TRUST_CACHE_ANE:
144 printf(" CS_TRUST_CACHE_AMFID|CS_TRUST_CACHE_ANE ");
145 break;
146 default:
147 printf(" [none] ");
148 break;
149 }
150
151 printf("[%i]\n", entry.hash_type);
152 }
153
154 void
155 print_hash(uint8_t cdhash[CS_CDHASH_LEN], bool newline)
156 {
157 for (int j = 0; j < CS_CDHASH_LEN; j++) {
158 printf("%02x", cdhash[j]);
159 }
160 if (newline)
161 printf("\n");
162 }