ccn_verifysig.c

Go to the documentation of this file.
00001 /**
00002  * @file ccn_verifysig.c
00003  * Utility to check the signature on ccnb-formatted ContentObjects.
00004  * 
00005  * A CCNx program.
00006  *
00007  * Copyright (C) 2009, 2010 Palo Alto Research Center, Inc.
00008  *
00009  * This work is free software; you can redistribute it and/or modify it under
00010  * the terms of the GNU General Public License version 2 as published by the
00011  * Free Software Foundation.
00012  * This work is distributed in the hope that it will be useful, but WITHOUT ANY
00013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00015  * for more details. You should have received a copy of the GNU General Public
00016  * License along with this program; if not, write to the
00017  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
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     /*    OpenSSL_add_all_digests(); */
00063     
00064     /* verify against the user's own public key until we have the infrastructure
00065      * to locate keys
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 }
Generated on Fri May 13 16:27:03 2011 for Content-Centric Networking in C by  doxygen 1.6.3