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.
30 #include <json-c/json.h>
31 #include <libubox/blob.h>
32 #include <libubox/utils.h>
33 #include <libubox/list.h>
34 #include <libubox/vlist.h>
35 #include <libubox/blobmsg_json.h>
39 #define CERT_BUF_LEN 4096
51 #ifndef UCERT_STRIP_MESSAGES
52 #define DPRINTF(format, ...) \
55 fprintf(stderr, "%s: " format, __func__, ## __VA_ARGS__); \
58 #define DPRINTF(format, ...) do { } while (0)
64 * | SIGNATURE | PAYLOAD |
65 * | |[ BLOBMSG CONTAINER ]|
66 * | |[[T,i,v,e,f,pubkey ]]|
74 static const struct blob_attr_info cert_policy
[CERT_ATTR_MAX
] = {
75 [CERT_ATTR_SIGNATURE
] = { .type
= BLOB_ATTR_BINARY
},
76 [CERT_ATTR_PAYLOAD
] = { .type
= BLOB_ATTR_NESTED
},
84 static const struct blobmsg_policy cert_cont_policy
[CERT_CT_ATTR_MAX
] = {
85 [CERT_CT_ATTR_PAYLOAD
] = { .name
= "ucert", .type
= BLOBMSG_TYPE_TABLE
},
88 enum cert_payload_attr
{
89 CERT_PL_ATTR_CERTTYPE
,
91 CERT_PL_ATTR_VALIDFROMTIME
,
92 CERT_PL_ATTR_EXPIRETIME
,
94 CERT_PL_ATTR_KEY_FINGERPRINT
,
104 static const struct blobmsg_policy cert_payload_policy
[CERT_PL_ATTR_MAX
] = {
105 [CERT_PL_ATTR_CERTTYPE
] = { .name
= "certtype", .type
= BLOBMSG_TYPE_INT32
},
106 [CERT_PL_ATTR_CERTID
] = { .name
= "certid", .type
= BLOBMSG_TYPE_INT32
},
107 [CERT_PL_ATTR_VALIDFROMTIME
] = { .name
= "validfrom", .type
= BLOBMSG_TYPE_INT64
},
108 [CERT_PL_ATTR_EXPIRETIME
] = { .name
= "expiresat", .type
= BLOBMSG_TYPE_INT64
},
109 [CERT_PL_ATTR_PUBKEY
] = { .name
= "pubkey", .type
= BLOBMSG_TYPE_STRING
},
110 [CERT_PL_ATTR_KEY_FINGERPRINT
] = { .name
= "fingerprint", .type
= BLOBMSG_TYPE_STRING
},
113 /* list to store certificate chain at runtime */
115 struct list_head list
;
116 struct blob_attr
*cert
[CERT_ATTR_MAX
];
119 /* write buffer to file */
120 static bool write_file(const char *filename
, void *buf
, size_t len
, bool append
) {
124 f
= fopen(filename
, append
?"a":"w");
128 outlen
= fwrite(buf
, 1, len
, f
);
130 return (outlen
== len
);
133 /* reads a whole file to a buffer - returns -1 on errors and sets errno */
134 static ssize_t
read_file(const char *filename
, void *buf
, size_t len
, size_t minlen
) {
138 f
= fopen(filename
, "r");
142 ret
= fread(buf
, 1, len
, f
);
144 /* Ensure that feof() yields the correct result when the file is exactly
150 } else if (!feof(f
)) {
153 } else if ((size_t)ret
< minlen
) {
162 /* load certfile into list */
163 static int cert_load(const char *certfile
, struct list_head
*chain
) {
164 struct blob_attr
*certtb
[CERT_ATTR_MAX
];
165 struct blob_attr
*bufpt
;
166 struct cert_object
*cobj
;
167 char filebuf
[CERT_BUF_LEN
], *end
;
171 len
= read_file(certfile
, filebuf
, sizeof(filebuf
) - 1, 0);
174 perror("Unable to load certificate file");
178 bufpt
= (struct blob_attr
*)filebuf
;
182 len
= end
- (char *)bufpt
;
186 if (blob_parse_untrusted(bufpt
, len
, certtb
, cert_policy
, CERT_ATTR_MAX
) <= 0)
187 /* no attributes found */
190 if (!certtb
[CERT_ATTR_SIGNATURE
])
191 /* no signature -> drop */
194 cobj
= calloc(1, sizeof(*cobj
));
195 cobj
->cert
[CERT_ATTR_SIGNATURE
] = blob_memdup(certtb
[CERT_ATTR_SIGNATURE
]);
196 if (certtb
[CERT_ATTR_PAYLOAD
])
197 cobj
->cert
[CERT_ATTR_PAYLOAD
] = blob_memdup(certtb
[CERT_ATTR_PAYLOAD
]);
199 list_add_tail(&cobj
->list
, chain
);
202 /* Repeat parsing while there is still enough remaining data in buffer
204 * Note that blob_next() is only valid for untrusted data because blob_parse_untrusted()
205 * verified that the buffer contains at least one blob, and that it is completely contained
207 bufpt
= blob_next(bufpt
);
214 /* append signature to certfile */
215 static int cert_append(const char *certfile
, const char *sigfile
) {
216 char filebuf
[CERT_BUF_LEN
];
217 struct blob_buf sigbuf
= {0};
221 len
= read_file(sigfile
, filebuf
, sizeof(filebuf
) - 1, 64);
224 perror("Unable to load signature file");
229 blob_buf_init(&sigbuf
, 0);
230 blob_put(&sigbuf
, CERT_ATTR_SIGNATURE
, filebuf
, len
);
231 ret
= write_file(certfile
, sigbuf
.head
, blob_raw_len(sigbuf
.head
), true);
232 blob_buf_free(&sigbuf
);
237 /* verify the signature of a single chain element */
238 static int cert_verify_blob(struct blob_attr
*cert
[CERT_ATTR_MAX
],
239 const char *pubkeyfile
, const char *pubkeydir
) {
241 char msgfname
[256], sigfname
[256];
243 char tmpdir
[] = "/tmp/ucert-XXXXXX";
245 if (mkdtemp(tmpdir
) == NULL
)
248 snprintf(msgfname
, sizeof(msgfname
) - 1, "%s/%s", tmpdir
, "payload");
249 snprintf(sigfname
, sizeof(sigfname
) - 1, "%s/%s", tmpdir
, "payload.sig");
251 for (i
= 0; i
< CERT_ATTR_MAX
; i
++) {
252 struct blob_attr
*v
= cert
[i
];
257 switch(cert_policy
[i
].type
) {
258 case BLOB_ATTR_BINARY
:
259 write_file(sigfname
, blob_data(v
), blob_len(v
), false);
261 case BLOB_ATTR_NESTED
:
262 write_file(msgfname
, blob_data(v
), blob_len(v
), false);
267 ret
= usign_v(msgfname
, pubkeyfile
, pubkeydir
, sigfname
, quiet
);
276 /* verify cert chain (and message) */
277 static int chain_verify(const char *msgfile
, const char *pubkeyfile
,
278 const char *pubkeydir
, struct list_head
*chain
) {
279 struct cert_object
*cobj
;
280 struct blob_attr
*containertb
[CERT_CT_ATTR_MAX
];
281 struct blob_attr
*payloadtb
[CERT_PL_ATTR_MAX
];
282 char tmpdir
[] = "/tmp/ucert-XXXXXX";
283 char chainedpubkey
[256] = {0};
284 char chainedfp
[17] = {0};
285 char extsigfile
[256] = {0};
290 if (mkdtemp(tmpdir
) == NULL
)
296 gettimeofday(&tv
, NULL
);
298 list_for_each_entry(cobj
, chain
, list
) {
299 /* blob has payload, verify that using signature */
300 if (cobj
->cert
[CERT_ATTR_PAYLOAD
]) {
305 ret
= cert_verify_blob(cobj
->cert
, chainedpubkey
[0]?chainedpubkey
:pubkeyfile
, pubkeydir
);
307 goto clean_and_return
;
309 blobmsg_parse(cert_cont_policy
,
310 ARRAY_SIZE(cert_cont_policy
),
312 blob_data(cobj
->cert
[CERT_ATTR_PAYLOAD
]),
313 blob_len(cobj
->cert
[CERT_ATTR_PAYLOAD
]));
314 if (!containertb
[CERT_CT_ATTR_PAYLOAD
]) {
316 DPRINTF("no ucert in signed payload\n");
317 goto clean_and_return
;
319 blobmsg_parse(cert_payload_policy
,
320 ARRAY_SIZE(cert_payload_policy
),
322 blobmsg_data(containertb
[CERT_CT_ATTR_PAYLOAD
]),
323 blobmsg_data_len(containertb
[CERT_CT_ATTR_PAYLOAD
]));
325 if (!payloadtb
[CERT_PL_ATTR_CERTTYPE
] ||
326 !payloadtb
[CERT_PL_ATTR_VALIDFROMTIME
] ||
327 !payloadtb
[CERT_PL_ATTR_EXPIRETIME
] ||
328 !payloadtb
[CERT_PL_ATTR_PUBKEY
]) {
330 DPRINTF("missing mandatory ucert attributes\n");
331 goto clean_and_return
;
333 certtype
= blobmsg_get_u32(payloadtb
[CERT_PL_ATTR_CERTTYPE
]);
334 validfrom
= blobmsg_get_u64(payloadtb
[CERT_PL_ATTR_VALIDFROMTIME
]);
335 expiresat
= blobmsg_get_u64(payloadtb
[CERT_PL_ATTR_EXPIRETIME
]);
337 if (certtype
!= CERTTYPE_AUTH
) {
339 DPRINTF("wrong certificate type\n");
340 goto clean_and_return
;
343 if (tv
.tv_sec
< validfrom
||
344 tv
.tv_sec
>= expiresat
) {
346 DPRINTF("certificate expired\n");
347 goto clean_and_return
;
350 snprintf(chainedpubkey
, sizeof(chainedpubkey
) - 1, "%s/%s", tmpdir
, "chained-pubkey");
351 write_file(chainedpubkey
,
352 blobmsg_data(payloadtb
[CERT_PL_ATTR_PUBKEY
]),
353 blobmsg_data_len(payloadtb
[CERT_PL_ATTR_PUBKEY
]),
356 if (usign_f_pubkey(chainedfp
, chainedpubkey
, quiet
)) {
357 DPRINTF("cannot get fingerprint for chained key\n");
359 goto clean_and_return
;
361 if (pubkeydir
&& _usign_key_is_revoked(chainedfp
, pubkeydir
)) {
362 DPRINTF("key %s has been revoked!\n", chainedfp
);
364 goto clean_and_return
;
367 /* blob doesn't have payload, verify message using signature */
369 snprintf(extsigfile
, sizeof(extsigfile
) - 1, "%s/%s", tmpdir
, "ext-sig");
370 write_file(extsigfile
,
371 blob_data(cobj
->cert
[CERT_ATTR_SIGNATURE
]),
372 blob_len(cobj
->cert
[CERT_ATTR_SIGNATURE
]),
374 checkmsg
= ret
= usign_v(msgfile
,
375 chainedpubkey
[0]?chainedpubkey
:pubkeyfile
,
376 pubkeydir
, extsigfile
, quiet
);
379 DPRINTF("stray trailing signature without anything to verify!\n");
386 DPRINTF("missing signature to verify message!\n");
389 if (chainedpubkey
[0])
390 unlink(chainedpubkey
);
392 return ret
| checkmsg
;
396 /* dump single chain element to console */
397 static void cert_dump_blob(struct blob_attr
*cert
[CERT_ATTR_MAX
]) {
401 for (i
= 0; i
< CERT_ATTR_MAX
; i
++) {
402 struct blob_attr
*v
= cert
[i
];
407 switch(cert_policy
[i
].type
) {
408 case BLOB_ATTR_BINARY
:
409 printf("signature:\n---\n%s---\n", (char *) blob_data(v
));
411 case BLOB_ATTR_NESTED
:
412 json
= blobmsg_format_json_indent(blob_data(v
), false, 0);
414 DPRINTF("cannot parse payload\n");
417 printf("payload:\n---\n%s\n---\n", json
);
424 /* dump certfile to console */
425 static int cert_dump(const char *certfile
) {
426 struct cert_object
*cobj
;
427 static LIST_HEAD(certchain
);
428 unsigned int count
= 0;
430 if (cert_load(certfile
, &certchain
)) {
431 DPRINTF("cannot parse cert\n");
435 list_for_each_entry(cobj
, &certchain
, list
) {
436 printf("=== CHAIN ELEMENT %02u ===\n", ++count
);
437 cert_dump_blob(cobj
->cert
);
443 /* issue an auth certificate for pubkey */
444 static int cert_issue(const char *certfile
, const char *pubkeyfile
, const char *seckeyfile
) {
445 struct blob_buf payloadbuf
= {0};
446 struct blob_buf certbuf
= {0};
448 ssize_t pklen
, siglen
;
453 char fname
[256], sfname
[256];
455 char tmpdir
[] = "/tmp/ucert-XXXXXX";
457 pklen
= read_file(pubkeyfile
, pkb
, sizeof(pkb
) - 1, 32);
460 perror("Unable to load public key file");
467 if (usign_f_pubkey(pkfp
, pubkeyfile
, quiet
))
470 gettimeofday(&tv
, NULL
);
472 if (mkdtemp(tmpdir
) == NULL
)
475 while (revoker
>= 0) {
476 blob_buf_init(&payloadbuf
, 0);
477 c
= blobmsg_open_table(&payloadbuf
, "ucert");
478 blobmsg_add_u32(&payloadbuf
, "certtype", revoker
?CERTTYPE_REVOKE
:CERTTYPE_AUTH
);
479 blobmsg_add_u64(&payloadbuf
, "validfrom", tv
.tv_sec
);
481 blobmsg_add_u64(&payloadbuf
, "expiresat", tv
.tv_sec
+ 60 * 60 * 24 * 365);
482 blobmsg_add_string(&payloadbuf
, "pubkey", pkb
);
484 blobmsg_add_string(&payloadbuf
, "fingerprint", pkfp
);
487 blobmsg_close_table(&payloadbuf
, c
);
489 snprintf(fname
, sizeof(fname
) - 1, "%s/%s", tmpdir
, revoker
?"revoker":"payload");
490 write_file(fname
, blob_data(payloadbuf
.head
), blob_len(payloadbuf
.head
), false);
492 snprintf(sfname
, sizeof(sfname
) - 1, "%s/%s", tmpdir
, revoker
?"revoker.sig":"payload.sig");
493 if (usign_s(fname
, seckeyfile
, sfname
, quiet
))
496 siglen
= read_file(sfname
, sigb
, sizeof(sigb
) - 1, 1);
499 perror("Unable to load signature file");
509 blob_buf_init(&certbuf
, 0);
510 blob_put(&certbuf
, CERT_ATTR_SIGNATURE
, sigb
, siglen
);
511 blob_put(&certbuf
, CERT_ATTR_PAYLOAD
, blob_data(payloadbuf
.head
), blob_len(payloadbuf
.head
));
512 snprintf(fname
, sizeof(fname
) - 1, "%s%s", certfile
, revoker
?".revoke":"");
513 write_file(fname
, certbuf
.head
, blob_raw_len(certbuf
.head
), true);
514 blob_buf_free(&certbuf
);
515 blob_buf_free(&payloadbuf
);
526 /* process revoker certificate */
527 static int cert_process_revoker(const char *certfile
, const char *pubkeydir
) {
528 static LIST_HEAD(certchain
);
529 struct cert_object
*cobj
;
530 struct blob_attr
*containertb
[CERT_CT_ATTR_MAX
];
531 struct blob_attr
*payloadtb
[CERT_PL_ATTR_MAX
];
535 enum certtype_id certtype
;
541 if (cert_load(certfile
, &certchain
)) {
542 DPRINTF("cannot parse cert\n");
546 gettimeofday(&tv
, NULL
);
548 list_for_each_entry(cobj
, &certchain
, list
) {
549 if (!cobj
->cert
[CERT_ATTR_PAYLOAD
])
552 /* blob has payload, verify that using signature */
553 ret
= cert_verify_blob(cobj
->cert
, NULL
, pubkeydir
);
557 blobmsg_parse(cert_cont_policy
,
558 ARRAY_SIZE(cert_cont_policy
),
560 blob_data(cobj
->cert
[CERT_ATTR_PAYLOAD
]),
561 blob_len(cobj
->cert
[CERT_ATTR_PAYLOAD
]));
562 if (!containertb
[CERT_CT_ATTR_PAYLOAD
]) {
563 DPRINTF("no ucert in signed payload\n");
567 blobmsg_parse(cert_payload_policy
,
568 ARRAY_SIZE(cert_payload_policy
),
570 blobmsg_data(containertb
[CERT_CT_ATTR_PAYLOAD
]),
571 blobmsg_data_len(containertb
[CERT_CT_ATTR_PAYLOAD
]));
573 if (!payloadtb
[CERT_PL_ATTR_CERTTYPE
] ||
574 !payloadtb
[CERT_PL_ATTR_VALIDFROMTIME
] ||
575 !payloadtb
[CERT_PL_ATTR_KEY_FINGERPRINT
]) {
576 DPRINTF("missing mandatory ucert attributes\n");
580 certtype
= blobmsg_get_u32(payloadtb
[CERT_PL_ATTR_CERTTYPE
]);
581 validfrom
= blobmsg_get_u64(payloadtb
[CERT_PL_ATTR_VALIDFROMTIME
]);
582 fingerprint
= blobmsg_get_string(payloadtb
[CERT_PL_ATTR_KEY_FINGERPRINT
]);
584 if (certtype
!= CERTTYPE_REVOKE
) {
585 DPRINTF("wrong certificate type\n");
589 if (tv
.tv_sec
< validfrom
) {
593 snprintf(rfname
, sizeof(rfname
)-1, "%s/%s", pubkeydir
, fingerprint
);
594 /* check if entry in pubkeydir exists */
595 if (stat(rfname
, &st
) == 0) {
596 if (_usign_key_is_revoked(fingerprint
, pubkeydir
)) {
597 DPRINTF("existing revoker deadlink for key %s\n", fingerprint
);
601 /* remove any other entry */
606 ret
= symlink(".revoked.", rfname
);
610 DPRINTF("created revoker deadlink for key %s\n", fingerprint
);
616 /* load and verify certfile (and message) */
617 static int cert_verify(const char *certfile
, const char *pubkeyfile
, const char *pubkeydir
, const char *msgfile
) {
618 static LIST_HEAD(certchain
);
620 if (cert_load(certfile
, &certchain
)) {
621 DPRINTF("cannot parse cert\n");
625 return chain_verify(msgfile
, pubkeyfile
, pubkeydir
, &certchain
);
629 static int usage(const char *cmd
)
631 #ifndef UCERT_STRIP_MESSAGES
633 "Usage: %s <command> <options>\n"
636 " -A: append signature (needs -c and -x)\n"
637 " -D: dump (needs -c)\n"
638 " -I: issue cert and revoker (needs -c and -p and -s)\n"
639 #endif /* UCERT_FULL */
640 " -R: process revoker certificate (needs -c and -P)\n"
641 " -V: verify (needs -c and -p|-P, may have -m)\n"
643 " -c <file>: certificate file\n"
644 " -m <file>: message file (verify only)\n"
645 " -p <file>: public key file\n"
646 " -P <path>: public key directory (verify only)\n"
647 " -q: quiet (do not print verification result, use return code only)\n"
649 " -s <file>: secret key file (issue only)\n"
650 " -x <file>: signature file (append only)\n"
651 #endif /* UCERT_FULL */
654 #endif /* UCERT_STRIP_MESSAGES */
658 /* parse command line options and call functions */
659 int main(int argc
, char *argv
[]) {
661 const char *msgfile
= NULL
;
662 const char *pubkeyfile
= NULL
;
663 const char *pubkeydir
= NULL
;
664 const char *certfile
= NULL
;
666 const char *sigfile
= NULL
;
667 const char *seckeyfile
= NULL
;
671 while ((ch
= getopt(argc
, argv
,
681 return usage(argv
[0]);
686 return usage(argv
[0]);
691 return usage(argv
[0]);
697 return usage(argv
[0]);
702 return usage(argv
[0]);
706 if (certfile
|| cmd
== CMD_NONE
)
707 return usage(argv
[0]);
711 if (msgfile
|| cmd
!= CMD_VERIFY
)
712 return usage(argv
[0]);
716 if (pubkeyfile
|| (cmd
!= CMD_VERIFY
&& cmd
!= CMD_ISSUE
) || cmd
== CMD_NONE
)
717 return usage(argv
[0]);
721 if (pubkeydir
|| (cmd
!= CMD_VERIFY
&& cmd
!= CMD_REVOKE
) || cmd
== CMD_NONE
)
722 return usage(argv
[0]);
727 return usage(argv
[0]);
732 if (seckeyfile
|| cmd
!= CMD_ISSUE
|| cmd
== CMD_NONE
)
733 return usage(argv
[0]);
737 if (sigfile
|| cmd
!= CMD_APPEND
|| cmd
== CMD_NONE
)
738 return usage(argv
[0]);
743 return usage(argv
[0]);
750 if (certfile
&& sigfile
)
751 return cert_append(certfile
, sigfile
);
753 return usage(argv
[0]);
756 return cert_dump(certfile
);
758 return usage(argv
[0]);
760 if (certfile
&& pubkeyfile
&& seckeyfile
)
761 return cert_issue(certfile
, pubkeyfile
, seckeyfile
);
763 return usage(argv
[0]);
766 if (certfile
&& pubkeydir
)
767 return cert_process_revoker(certfile
, pubkeydir
);
769 return usage(argv
[0]);
771 if (certfile
&& (pubkeyfile
|| pubkeydir
))
772 return cert_verify(certfile
, pubkeyfile
, pubkeydir
, msgfile
);
774 return usage(argv
[0]);
776 return usage(argv
[0]);
780 return usage(argv
[0]);