tests: add test cases for blob parsing
[project/libubox.git] / tests / test-blob-parse.c
1 /*
2 * Based on certificate dump functionality from ucert.c:
3 *
4 * Copyright (C) 2018 Daniel Golle <daniel@makrotopia.org>
5 * SPDX-License-Identifier: GPL-3.0
6 *
7 */
8
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stddef.h>
12 #include <libgen.h>
13
14 #include "blob.h"
15 #include "list.h"
16 #include "blobmsg_json.h"
17
18 #define CERT_BUF_LEN 4096
19
20 /*
21 * ucert structure
22 * | BLOB |
23 * | SIGNATURE | PAYLOAD |
24 * | |[ BLOBMSG CONTAINER ]|
25 * | |[[T,i,v,e,f,pubkey ]]|
26 */
27 enum cert_attr {
28 CERT_ATTR_SIGNATURE,
29 CERT_ATTR_PAYLOAD,
30 CERT_ATTR_MAX
31 };
32
33 static const struct blob_attr_info cert_policy[CERT_ATTR_MAX] = {
34 [CERT_ATTR_SIGNATURE] = { .type = BLOB_ATTR_BINARY },
35 [CERT_ATTR_PAYLOAD] = { .type = BLOB_ATTR_NESTED },
36 };
37
38 enum cert_cont_attr {
39 CERT_CT_ATTR_PAYLOAD,
40 CERT_CT_ATTR_MAX
41 };
42
43 enum cert_payload_attr {
44 CERT_PL_ATTR_CERTTYPE,
45 CERT_PL_ATTR_CERTID,
46 CERT_PL_ATTR_VALIDFROMTIME,
47 CERT_PL_ATTR_EXPIRETIME,
48 CERT_PL_ATTR_PUBKEY,
49 CERT_PL_ATTR_KEY_FINGERPRINT,
50 CERT_PL_ATTR_MAX
51 };
52
53 enum certtype_id {
54 CERTTYPE_UNSPEC,
55 CERTTYPE_AUTH,
56 CERTTYPE_REVOKE
57 };
58
59 /* list to store certificate chain at runtime */
60 struct cert_object {
61 struct list_head list;
62 struct blob_attr *cert[CERT_ATTR_MAX];
63 };
64
65 static int cert_load(const char *certfile, struct list_head *chain)
66 {
67 FILE *f;
68 struct blob_attr *certtb[CERT_ATTR_MAX];
69 struct blob_attr *bufpt;
70 struct cert_object *cobj;
71 char filebuf[CERT_BUF_LEN];
72 int ret = 0, pret = 0;
73 size_t len, pos = 0;
74
75 f = fopen(certfile, "r");
76 if (!f)
77 return 1;
78
79 len = fread(&filebuf, 1, CERT_BUF_LEN - 1, f);
80 if (len < 64)
81 return 1;
82
83 ret = ferror(f) || !feof(f);
84 fclose(f);
85 if (ret)
86 return 1;
87
88 bufpt = (struct blob_attr *)filebuf;
89 do {
90 pret = blob_parse(bufpt, certtb, cert_policy, CERT_ATTR_MAX);
91 if (pret <= 0)
92 /* no attributes found */
93 break;
94
95 if (pos + blob_pad_len(bufpt) > len)
96 /* blob exceeds filebuffer */
97 break;
98 else
99 pos += blob_pad_len(bufpt);
100
101 if (!certtb[CERT_ATTR_SIGNATURE])
102 /* no signature -> drop */
103 break;
104
105 cobj = calloc(1, sizeof(*cobj));
106 cobj->cert[CERT_ATTR_SIGNATURE] = blob_memdup(certtb[CERT_ATTR_SIGNATURE]);
107 if (certtb[CERT_ATTR_PAYLOAD])
108 cobj->cert[CERT_ATTR_PAYLOAD] = blob_memdup(certtb[CERT_ATTR_PAYLOAD]);
109
110 list_add_tail(&cobj->list, chain);
111 ret += pret;
112 /* repeat parsing while there is still enough remaining data in buffer */
113 } while(len > pos + sizeof(struct blob_attr) && (bufpt = blob_next(bufpt)));
114
115 return (ret <= 0);
116 }
117
118 /* dump single chain element to console */
119 static void cert_dump_blob(struct blob_attr *cert[CERT_ATTR_MAX])
120 {
121 int i;
122 char *json = NULL;
123
124 for (i = 0; i < CERT_ATTR_MAX; i++) {
125 struct blob_attr *v = cert[i];
126
127 if (!v)
128 continue;
129
130 switch(cert_policy[i].type) {
131 case BLOB_ATTR_BINARY:
132 fprintf(stdout, "signature:\n---\n%s---\n", (char *) blob_data(v));
133 break;
134 case BLOB_ATTR_NESTED:
135 json = blobmsg_format_json_indent(blob_data(v), false, 0);
136 if (!json)
137 continue;
138
139 fprintf(stdout, "payload:\n---\n%s\n---\n", json);
140 free(json);
141 break;
142 }
143 }
144 }
145
146 static int cert_dump(const char *certfile)
147 {
148 struct cert_object *cobj;
149 static LIST_HEAD(certchain);
150 unsigned int count = 0;
151
152 if (cert_load(certfile, &certchain)) {
153 fprintf(stderr, "cannot parse cert %s\n", basename((char *) certfile));
154 return 1;
155 }
156
157 list_for_each_entry(cobj, &certchain, list) {
158 fprintf(stdout, "=== CHAIN ELEMENT %02u ===\n", ++count);
159 cert_dump_blob(cobj->cert);
160 }
161
162 return 0;
163 }
164
165 int main(int argc, char *argv[])
166 {
167 if (argc != 2) {
168 fprintf(stderr, "Usage: %s <cert.ucert>\n", argv[0]);
169 return 3;
170 }
171
172 cert_dump(argv[1]);
173
174 return 0;
175 }