Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -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 | * BSD license, See the LICENSE file for more information |
| 5 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDNSEC_SIGN_REQ_HPP |
| 9 | #define NDNSEC_SIGN_REQ_HPP |
| 10 | |
| 11 | #include "ndnsec-util.hpp" |
| 12 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 13 | int |
| 14 | ndnsec_sign_req(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 15 | { |
| 16 | using namespace ndn; |
| 17 | namespace po = boost::program_options; |
| 18 | |
| 19 | std::string name; |
| 20 | bool isKeyName = false; |
| 21 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 22 | po::options_description description("General Usage\n ndnsec sign-req [-h] [-k] name\nGeneral options"); |
| 23 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | ("help,h", "produce help message") |
| 25 | ("key,k", "optional, if specified, name is keyName (e.g. /ndn/edu/ucla/alice/ksk-123456789), otherwise identity name") |
| 26 | ("name,n", po::value<std::string>(&name), "name, for example, /ndn/edu/ucla/alice") |
| 27 | ; |
| 28 | |
| 29 | po::positional_options_description p; |
| 30 | p.add("name", 1); |
| 31 | |
| 32 | po::variables_map vm; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 33 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 34 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 35 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 36 | vm); |
| 37 | po::notify(vm); |
| 38 | } |
| 39 | catch (const std::exception& e) |
| 40 | { |
| 41 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 42 | std::cerr << description << std::endl; |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | if (vm.count("help") != 0) |
| 47 | { |
| 48 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 52 | if (vm.count("name") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 53 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 54 | std::cerr << "ERROR: name must be specified" << std::endl; |
| 55 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 56 | return 1; |
| 57 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 58 | |
| 59 | if (vm.count("key") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 60 | isKeyName = true; |
| 61 | |
| 62 | shared_ptr<IdentityCertificate> selfSignCert; |
| 63 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 64 | KeyChain keyChain; |
| 65 | |
| 66 | if (isKeyName) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 67 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 68 | selfSignCert = keyChain.selfSign(name); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 69 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 70 | else |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 71 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 72 | Name keyName = keyChain.getDefaultKeyNameForIdentity(name); |
| 73 | selfSignCert = keyChain.selfSign(keyName); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 76 | io::save(*selfSignCert, std::cout); |
| 77 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | #endif //NDNSEC_SIGN_REQ_HPP |