blob: 54a5c9a8392a2e83880a475daee16ecc81a1d10d [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- 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
13int
14ndnsec_sign_req(int argc, char** argv)
15{
16 using namespace ndn;
17 namespace po = boost::program_options;
18
19 std::string name;
20 bool isKeyName = false;
21
22 po::options_description desc("General Usage\n ndnsec sign-req [-h] [-k] name\nGeneral options");
23 desc.add_options()
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;
33 try {
34 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
35 po::notify(vm);
36 }
37 catch(const std::exception &e) {
38 std::cerr << "ERROR: " << e.what() << std::endl;
39 std::cerr << desc << std::endl;
40 return 1;
41 }
42
43 if (vm.count("help"))
44 {
45 std::cerr << desc << std::endl;
46 return 0;
47 }
48
49 if (0 == vm.count("name"))
50 {
51 std::cerr << "identity_name must be specified" << std::endl;
52 std::cerr << desc << std::endl;
53 return 1;
54 }
55
56 if (vm.count("key"))
57 isKeyName = true;
58
59 shared_ptr<IdentityCertificate> selfSignCert;
60
61 try
62 {
63 KeyChain keyChain;
64
65 if(isKeyName)
66 {
67 selfSignCert = keyChain.selfSign(name);
68 }
69 else
70 {
71 Name keyName = keyChain.getDefaultKeyNameForIdentity(name);
72 selfSignCert = keyChain.selfSign(keyName);
73 }
74 }
75 catch(SecPublicInfo::Error& e)
76 {
77 std::cerr << "ERROR: " << e.what() << std::endl;
78 return 1;
79 }
80 catch(SecTpm::Error& e)
81 {
82 std::cerr << "ERROR: " << e.what() << std::endl;
83 return 1;
84 }
85
86 try
87 {
88 using namespace CryptoPP;
89 StringSource ss(selfSignCert->wireEncode().wire(), selfSignCert->wireEncode().size(), true,
90 new Base64Encoder(new FileSink(std::cout), true, 64));
91 }
92 catch(CryptoPP::Exception& e)
93 {
94 std::cerr << "ERROR: " << e.what() << std::endl;
95 return 1;
96 }
97
98 return 0;
99}
100
101#endif //NDNSEC_SIGN_REQ_HPP