Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include <iostream> |
| 12 | #include <fstream> |
| 13 | |
| 14 | #include <boost/program_options/options_description.hpp> |
| 15 | #include <boost/program_options/variables_map.hpp> |
| 16 | #include <boost/program_options/parsers.hpp> |
| 17 | |
| 18 | #include <cryptopp/base64.h> |
| 19 | #include <cryptopp/files.h> |
| 20 | |
| 21 | #include "security/key-chain.hpp" |
| 22 | |
| 23 | using namespace ndn; |
| 24 | namespace po = boost::program_options; |
| 25 | |
| 26 | shared_ptr<IdentityCertificate> |
| 27 | getCertificate(const std::string& certString) |
| 28 | { |
| 29 | std::string decoded; |
| 30 | CryptoPP::StringSource ss2(reinterpret_cast<const unsigned char *>(certString.c_str()), certString.size(), true, |
| 31 | new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded))); |
| 32 | |
| 33 | Data data; |
| 34 | data.wireDecode(Block(make_shared<Buffer>(decoded.begin(), decoded.end()))); |
| 35 | |
| 36 | shared_ptr<IdentityCertificate> identityCertificate = make_shared<IdentityCertificate>(boost::cref(data)); |
| 37 | |
| 38 | return identityCertificate; |
| 39 | } |
| 40 | |
| 41 | bool |
| 42 | verifySignature(shared_ptr<IdentityCertificate> certificate, bool isDataPacket) |
| 43 | { |
| 44 | throw std::runtime_error("Not supported yet"); |
| 45 | // if(isDataPacket) |
| 46 | // { |
| 47 | // std::string decoded; |
| 48 | // CryptoPP::FileSource ss2(cin, true, |
| 49 | // new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded))); |
| 50 | |
| 51 | // Data data; |
| 52 | // data.wireDecode(ptr_lib::make_shared<Buffer>(decoded.c_str(), decoded.size())); |
| 53 | // return PolicyManager::verifySignature(data, certificate->getPublicKeyInfo()); |
| 54 | // } |
| 55 | // else |
| 56 | // { |
| 57 | // // The first two bytes indicates the boundary of the of the signed data and signature. |
| 58 | // // for example, if the size of the signed data is 300, then the boundary should be 300, so the first two bytes should be: 0x01 0x2C |
| 59 | // ptr_lib::shared_ptr<Blob> input = ptr_lib::shared_ptr<Blob>(new Blob ((istreambuf_iterator<char>(cin)), istreambuf_iterator<char>())); |
| 60 | // size_t size = input->at(0); |
| 61 | // size = ((size << 8) + input->at(1)); |
| 62 | |
| 63 | // Blob signedBlob(input->buf()+2, size); |
| 64 | // Blob signature(input->buf()+2+size, input->size()-2-size); |
| 65 | |
| 66 | // return PolicyManager::verifySignature(signedBlob, signature, certificate->getPublicKeyInfo()); |
| 67 | // } |
| 68 | } |
| 69 | |
| 70 | int main(int argc, char** argv) |
| 71 | { |
| 72 | bool isDataPacket = false; |
| 73 | std::string certString; |
| 74 | |
| 75 | po::options_description desc("General Usage\n ndn-sig-verify [-h] [-d] certificate\nGeneral options"); |
| 76 | desc.add_options() |
| 77 | ("help,h", "produce help message") |
| 78 | ("data,d", "if specified, input from stdin will be treated as a Data packet, otherwise binary data") |
| 79 | ("certificate,c", po::value<std::string>(&certString), "the certificate bits") |
| 80 | ; |
| 81 | |
| 82 | po::positional_options_description p; |
| 83 | p.add("certificate", 1); |
| 84 | |
| 85 | po::variables_map vm; |
| 86 | try |
| 87 | { |
| 88 | po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); |
| 89 | po::notify(vm); |
| 90 | } |
| 91 | catch( const std::exception& e) |
| 92 | { |
| 93 | std::cerr << e.what() << std::endl; |
| 94 | std::cerr << desc << std::endl; |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | if (vm.count("help") || vm.count("certificate")==0) |
| 99 | { |
| 100 | std::cerr << desc << std::endl; |
| 101 | return 1; |
| 102 | } |
| 103 | if (vm.count("data")) |
| 104 | isDataPacket = true; |
| 105 | |
| 106 | try |
| 107 | { |
| 108 | shared_ptr<IdentityCertificate> certificate = getCertificate(certString); |
| 109 | bool res = verifySignature(certificate, isDataPacket); |
| 110 | return (res ? 0 : 1); |
| 111 | } |
| 112 | catch(const std::exception &e) |
| 113 | { |
| 114 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 115 | return 1; |
| 116 | } |
| 117 | } |