]> git.cameronkatri.com Git - trustcache.git/blob - info.c
8de924c475d45b9125ce2849f7dad928e47449d1
[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 <stdbool.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include "trustcache.h"
35
36 int
37 tcinfo(int argc, char **argv)
38 {
39 struct trust_cache cache;
40 bool headeronly = false, onlyhash = false;
41 uint32_t entrynum = -1;
42 const char *errstr = NULL;
43
44 int ch;
45 while ((ch = getopt(argc, argv, "che:")) != -1) {
46 switch (ch) {
47 case 'h':
48 headeronly = true;
49 break;
50 case 'e':
51 entrynum = strtonum(optarg, 1, UINT32_MAX, &errstr);
52 if (errstr != NULL) {
53 fprintf(stderr, "entry number is %s: %s\n", errstr, optarg);
54 exit(1);
55 }
56 break;
57 case 'c':
58 onlyhash = true;
59 break;
60 }
61 }
62
63 argc -= optind;
64 argv += optind;
65
66 if (argc == 0)
67 return -1;
68
69 cache = opentrustcache(argv[0]);
70
71 if (entrynum == -1 && !onlyhash)
72 print_header(cache);
73 if (!headeronly) {
74 if (onlyhash) {
75 for (uint32_t i = 0; i < cache.num_entries; i++) {
76 if (cache.version == 0)
77 print_hash(cache.hashes[i], true);
78 else
79 print_hash(cache.entries[i].cdhash, true);
80 }
81 goto done;
82 }
83 if (entrynum != -1) {
84 if (entrynum > cache.num_entries) {
85 fprintf(stderr, "no entry %i\n", entrynum);
86 exit(1);
87 }
88 if (cache.version == 1) {
89 print_entry(cache.entries[entrynum - 1]);
90 } else if (cache.version == 0) {
91 print_hash(cache.hashes[entrynum - 1], true);
92 }
93 } else {
94 print_entries(cache);
95 }
96 }
97
98 done:
99 free(cache.entries);
100
101 return 0;
102 }
103
104 void
105 print_header(struct trust_cache cache)
106 {
107 printf("version = %i\n", cache.version);
108 char out[37];
109 uuid_unparse(cache.uuid, out);
110 printf("uuid = %s\n", out);
111 printf("entry count = %i\n", cache.num_entries);
112 }
113
114 void
115 print_entries(struct trust_cache cache)
116 {
117 for (uint32_t i = 0; i < cache.num_entries; i++) {
118 if (cache.version == 0)
119 print_hash(cache.hashes[i], true);
120 else if (cache.version == 1)
121 print_entry(cache.entries[i]);
122 }
123 }
124
125 void
126 print_entry(struct trust_cache_entry1 entry)
127 {
128 print_hash(entry.cdhash, false);
129
130 switch (entry.flags) {
131 case CS_TRUST_CACHE_AMFID:
132 printf(" CS_TRUST_CACHE_AMFID ");
133 break;
134 case CS_TRUST_CACHE_ANE:
135 printf(" CS_TRUST_CACHE_ANE ");
136 break;
137 case CS_TRUST_CACHE_AMFID|CS_TRUST_CACHE_ANE:
138 printf(" CS_TRUST_CACHE_AMFID|CS_TRUST_CACHE_ANE ");
139 break;
140 case 0:
141 printf(" [none] ");
142 break;
143 default:
144 printf(" [%i] ", entry.flags);
145 break;
146 }
147
148 printf("[%i]\n", entry.hash_type);
149 }
150
151 void
152 print_hash(uint8_t cdhash[CS_CDHASH_LEN], bool newline)
153 {
154 for (size_t j = 0; j < CS_CDHASH_LEN; j++) {
155 printf("%02x", cdhash[j]);
156 }
157 if (newline)
158 printf("\n");
159 }