2 * Copyright (C) 2018 Daniel Golle <daniel@makrotopia.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 version 3
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
29 #include <json-c/json.h>
30 #include <libubox/blob.h>
31 #include <libubox/utils.h>
32 #include <libubox/list.h>
33 #include <libubox/vlist.h>
34 #include <libubox/blobmsg_json.h>
36 #define CERT_BUF_LEN 4096
55 static const struct blob_attr_info cert_policy
[CERT_ATTR_MAX
] = {
56 [CERT_ATTR_SIGNATURE
] = { .type
= BLOB_ATTR_BINARY
},
57 [CERT_ATTR_PAYLOAD
] = { .type
= BLOB_ATTR_NESTED
},
60 enum cert_payload_attr
{
61 CERT_PL_ATTR_CERTTYPE
,
63 CERT_PL_ATTR_VALIDFROMTIME
,
64 CERT_PL_ATTR_EXPIRETIME
,
66 CERT_PL_ATTR_KEY_FINGERPRINT
,
76 static const struct blobmsg_policy cert_payload_policy
[CERT_PL_ATTR_MAX
] = {
77 [CERT_PL_ATTR_CERTTYPE
] = { .type
= BLOBMSG_TYPE_INT8
},
78 [CERT_PL_ATTR_CERTID
] = { .type
= BLOBMSG_TYPE_INT64
},
79 [CERT_PL_ATTR_VALIDFROMTIME
] = { .type
= BLOBMSG_TYPE_INT64
},
80 [CERT_PL_ATTR_EXPIRETIME
] = { .type
= BLOBMSG_TYPE_INT64
},
81 [CERT_PL_ATTR_PUBKEY
] = { .type
= BLOBMSG_TYPE_STRING
},
82 [CERT_PL_ATTR_KEY_FINGERPRINT
] = { .type
= BLOBMSG_TYPE_STRING
},
86 static int cert_load(const char *certfile
, struct blob_attr
*certtb
[]) {
88 struct blob_buf certbuf
;
90 char filebuf
[CERT_BUF_LEN
];
93 blob_buf_init(&certbuf
, 0);
95 f
= fopen(certfile
, "r");
100 len
= fread(&filebuf
, 1, CERT_BUF_LEN
- 1, f
);
101 blob_put_raw(&certbuf
, filebuf
, len
);
102 } while(!feof(f
) && !ferror(f
));
110 blob_parse(certbuf
.head
, certtb
, cert_policy
, CERT_ATTR_MAX
);
115 static int cert_append(const char *certfile
, const char *pubkeyfile
, const char *sigfile
) {
116 fprintf(stderr
, "not implemented\n");
120 static int cert_dump(const char *certfile
) {
121 struct blob_attr
*certtb
[CERT_ATTR_MAX
];
123 if (cert_load(certfile
, certtb
)) {
124 fprintf(stderr
, "cannot parse cert\n");
131 static int cert_issue(const char *certfile
, const char *pubkeyfile
, const char *seckeyfile
) {
132 fprintf(stderr
, "not implemented\n");
136 static int cert_process_revoker(const char *certfile
) {
137 fprintf(stderr
, "not implemented\n");
141 static int cert_verify(const char *certfile
, const char *pubkeyfile
, const char *pubkeydir
, const char *msgfile
) {
142 fprintf(stderr
, "not implemented\n");
146 static int usage(const char *cmd
)
149 "Usage: %s <command> <options>\n"
151 " -A: append (needs -c and -p and/or -x)\n"
153 " -I: issue cert and revoker (needs -c and -p and -s)\n"
154 " -R: process revoker certificate (needs -c)\n"
155 " -V: verify (needs -c and -p|-P)\n"
157 " -c <file>: certificate file\n"
158 " -m <file>: message file (verify only)\n"
159 " -p <file>: public key file\n"
160 " -P <path>: public key directory (verify only)\n"
161 " -q: quiet (do not print verification result, use return code only)\n"
162 " -s <file>: secret key file (issue only)\n"
163 " -x <file>: signature file\n"
169 int main(int argc
, char *argv
[]) {
171 const char *msgfile
= NULL
;
172 const char *sigfile
= NULL
;
173 const char *pubkeyfile
= NULL
;
174 const char *pubkeydir
= NULL
;
175 const char *certfile
= NULL
;
176 const char *seckeyfile
= NULL
;
179 while ((ch
= getopt(argc
, argv
, "ADIRVc:m:p:P:qs:x:")) != -1) {
218 return usage(argv
[0]);
224 if (certfile
&& (pubkeyfile
|| sigfile
))
225 return cert_append(certfile
, pubkeyfile
, sigfile
);
227 return usage(argv
[0]);
230 return cert_dump(certfile
);
232 return usage(argv
[0]);
234 if (certfile
&& pubkeyfile
&& seckeyfile
)
235 return cert_issue(certfile
, pubkeyfile
, seckeyfile
);
237 return usage(argv
[0]);
240 return cert_process_revoker(certfile
);
242 return usage(argv
[0]);
244 if (certfile
&& (pubkeyfile
|| pubkeydir
))
245 return cert_verify(certfile
, pubkeyfile
, pubkeydir
, msgfile
);
247 return usage(argv
[0]);
249 return usage(argv
[0]);
253 return usage(argv
[0]);