pytest/dns_aging: use correct variable names
[Samba.git] / lib / mscat / dumpmscat.c
blob5364610d48313067c757f49df83e37f68ed6f4ce
1 /*
2 * Copyright (c) 2016 Andreas Schneider <asn@samba.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <errno.h>
19 #include <stdbool.h>
20 #include <stdarg.h>
21 #include <stdint.h>
22 #include <stdio.h>
24 #include <talloc.h>
26 #include <libtasn1.h>
27 #include <gnutls/pkcs7.h>
29 #include "mscat.h"
31 static const char *mac_to_string(enum mscat_mac_algorithm algo) {
32 switch(algo) {
33 case MSCAT_MAC_NULL:
34 return "NULL";
35 case MSCAT_MAC_MD5:
36 return "MD5";
37 case MSCAT_MAC_SHA1:
38 return "SHA1";
39 case MSCAT_MAC_SHA256:
40 return "SHA256";
41 case MSCAT_MAC_SHA512:
42 return "SHA512";
43 case MSCAT_MAC_UNKNOWN:
44 return "UNKNOWN";
47 return "UNKNOWN";
50 int main(int argc, char *argv[]) {
51 TALLOC_CTX *mem_ctx;
52 const char *filename = NULL;
53 const char *ca_file = NULL;
54 struct mscat_pkcs7 *cat_pkcs7;
55 struct mscat_ctl *msctl;
56 unsigned int member_count = 0;
57 unsigned int attribute_count = 0;
58 unsigned int i;
59 int rc;
61 if (argc < 1) {
62 return -1;
64 filename = argv[1];
66 if (filename == NULL || filename[0] == '\0') {
67 return -1;
70 mem_ctx = talloc_init("dumpmscat");
71 if (mem_ctx == NULL) {
72 fprintf(stderr, "Failed to initialize talloc\n");
73 exit(1);
76 /* READ MS ROOT CERTIFICATE */
78 cat_pkcs7 = mscat_pkcs7_init(mem_ctx);
79 if (cat_pkcs7 == NULL) {
80 exit(1);
83 rc = mscat_pkcs7_import_catfile(cat_pkcs7,
84 filename);
85 if (rc != 0) {
86 exit(1);
89 if (argc >= 2) {
90 ca_file = argv[2];
93 rc = mscat_pkcs7_verify(cat_pkcs7, ca_file);
94 if (rc != 0) {
95 printf("FAILED TO VERIFY CATALOG FILE!\n");
96 exit(1);
98 printf("CATALOG FILE VERIFIED!\n\n");
100 msctl = mscat_ctl_init(mem_ctx);
101 if (msctl == NULL) {
102 exit(1);
105 rc = mscat_ctl_import(msctl, cat_pkcs7);
106 if (rc < 0) {
107 exit(1);
110 rc = mscat_ctl_get_member_count(msctl);
111 if (rc < 0) {
112 exit(1);
115 member_count = rc;
116 printf("CATALOG MEMBER COUNT=%d\n", member_count);
118 for (i = 0; i < member_count; i++) {
119 struct mscat_ctl_member *m;
120 size_t j;
122 rc = mscat_ctl_get_member(msctl,
123 mem_ctx,
124 i + 1,
125 &m);
126 if (rc != 0) {
127 exit(1);
130 printf("CATALOG MEMBER\n");
131 if (m->checksum.type == MSCAT_CHECKSUM_STRING) {
132 printf(" CHECKSUM: %s\n", m->checksum.string);
133 } else if (m->checksum.type == MSCAT_CHECKSUM_BLOB) {
134 printf(" CHECKSUM: ");
135 for (j = 0; j < m->checksum.size; j++) {
136 printf("%X", m->checksum.blob[j]);
138 printf("\n");
140 printf("\n");
142 if (m->file.name != NULL) {
143 printf(" FILE: %s, FLAGS=0x%08x\n",
144 m->file.name,
145 m->file.flags);
148 if (m->info.guid != NULL) {
149 printf(" GUID: %s, ID=0x%08x\n",
150 m->info.guid,
151 m->info.id);
154 if (m->osattr.value != NULL) {
155 printf(" OSATTR: %s, FLAGS=0x%08x\n",
156 m->osattr.value,
157 m->osattr.flags);
160 if (m->mac.type != MSCAT_MAC_UNKNOWN) {
161 printf(" MAC: %s, DIGEST: ",
162 mac_to_string(m->mac.type));
163 for (j = 0; j < m->mac.digest_size; j++) {
164 printf("%X", m->mac.digest[j]);
166 printf("\n");
168 printf("\n");
170 printf("\n");
172 rc = mscat_ctl_get_attribute_count(msctl);
173 if (rc < 0) {
174 exit(1);
176 attribute_count = rc;
177 printf("CATALOG ATTRIBUTE COUNT=%d\n", attribute_count);
179 for (i = 0; i < attribute_count; i++) {
180 struct mscat_ctl_attribute *a;
182 rc = mscat_ctl_get_attribute(msctl,
183 mem_ctx,
184 i + 1,
185 &a);
186 if (rc != 0) {
187 exit(1);
190 printf(" NAME=%s, FLAGS=0x%08x, VALUE=%s\n",
191 a->name,
192 a->flags,
193 a->value);
195 talloc_free(mem_ctx);
196 return 0;