ccn_verifysig.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <fcntl.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <sys/types.h>
00025 #include <unistd.h>
00026
00027 #include <ccn/ccn.h>
00028 #include <ccn/keystore.h>
00029 #include <ccn/signing.h>
00030
00031 static unsigned char rawbuf[8801];
00032
00033 #define MOAN(args) do { fprintf args; Moan(__LINE__); status = 1; } while(0)
00034
00035 void
00036 Moan(int line) {
00037 fprintf(stderr, " at ccn_verifysig.c:%d\n", line);
00038 }
00039
00040 int
00041 main(int argc, char **argv)
00042 {
00043 int opt;
00044 int res;
00045 int argi;
00046 int fd;
00047 ssize_t size;
00048 const char *filename;
00049 struct ccn_parsed_ContentObject obj = {0};
00050 struct ccn_parsed_ContentObject *co = &obj;
00051 struct ccn_indexbuf *comps = ccn_indexbuf_create();
00052 struct ccn_keystore *keystore;
00053 char *home = getenv("HOME");
00054 char *keystore_suffix = "/.ccnx/.ccnx_keystore";
00055 char *keystore_name = NULL;
00056
00057 int status = 0;
00058
00059 int good = 0;
00060 int bad = 0;
00061
00062
00063
00064
00065
00066
00067 const void *verification_pubkey = NULL;
00068
00069 if (home == NULL) {
00070 printf("Unable to determine home directory for keystore\n");
00071 exit(1);
00072 }
00073 keystore_name = calloc(1, strlen(home) + strlen(keystore_suffix) + 1);
00074
00075 strcat(keystore_name, home);
00076 strcat(keystore_name, keystore_suffix);
00077
00078 keystore = ccn_keystore_create();
00079 if (0 != ccn_keystore_init(keystore, keystore_name, "Th1s1sn0t8g00dp8ssw0rd.")) {
00080 printf("Failed to initialize keystore\n");
00081 exit(1);
00082 }
00083 verification_pubkey = ccn_keystore_public_key(keystore);
00084
00085 while ((opt = getopt(argc, argv, "h")) != -1) {
00086 switch (opt) {
00087 default:
00088 case 'h':
00089 fprintf(stderr, "provide names of files containing ccnb format content\n");
00090 exit(1);
00091 }
00092 }
00093 argc -= optind;
00094 argv += optind;
00095 for (argi = 0; argv[argi] != NULL; argi++) {
00096 filename = argv[argi];
00097 fd = open(filename, O_RDONLY);
00098 if (fd == -1) {
00099 perror(filename);
00100 status = 1;
00101 continue;
00102 }
00103 fprintf(stderr, "Reading %s ... ", filename);
00104 size = read(fd, rawbuf, sizeof(rawbuf));
00105 if (size < 0) {
00106 perror("skipping");
00107 close(fd);
00108 status = 1;
00109 continue;
00110 }
00111 close(fd);
00112 if (size == sizeof(rawbuf)) {
00113 fprintf(stderr, "skipping: too big\n");
00114 status = 1;
00115 continue;
00116 }
00117 res = ccn_parse_ContentObject(rawbuf, size, co, comps);
00118 if (res < 0) {
00119 fprintf(stderr, "skipping: not a ContentObject\n");
00120 status = 1;
00121 continue;
00122 }
00123 if (co->offset[CCN_PCO_B_KeyLocator] != co->offset[CCN_PCO_E_KeyLocator]) {
00124 struct ccn_buf_decoder decoder;
00125 struct ccn_buf_decoder *d =
00126 ccn_buf_decoder_start(&decoder,
00127 rawbuf + co->offset[CCN_PCO_B_Key_Certificate_KeyName],
00128 co->offset[CCN_PCO_E_Key_Certificate_KeyName] - co->offset[CCN_PCO_B_Key_Certificate_KeyName]);
00129
00130 fprintf(stderr, "[has KeyLocator: ");
00131 if (ccn_buf_match_dtag(d, CCN_DTAG_KeyName)) fprintf(stderr, "KeyName] ");
00132 if (ccn_buf_match_dtag(d, CCN_DTAG_Certificate)) fprintf(stderr, "Certificate] ");
00133 if (ccn_buf_match_dtag(d, CCN_DTAG_Key)) fprintf(stderr, "Key] ");
00134 }
00135
00136 res = ccn_verify_signature(rawbuf, size, co, verification_pubkey);
00137
00138 if (res != 1) {
00139 fprintf(stderr, "Signature failed to verify\n");
00140 bad++;
00141 } else {
00142 fprintf(stderr, "Verified\n");
00143 good++;
00144 }
00145 }
00146 printf("\n%d files, %d skipped, %d good, %d bad.\n", argi, argi - good - bad, good, bad);
00147 exit(status);
00148 }