blob: 1072372170e76c1759371bc9354b635871cbcfff [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
Yingdi Yub61f5402014-02-26 17:46:11 -080013int
14ndnsec_sign_req(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080015{
16 using namespace ndn;
17 namespace po = boost::program_options;
18
19 std::string name;
20 bool isKeyName = false;
21
Yingdi Yub61f5402014-02-26 17:46:11 -080022 po::options_description description("General Usage\n ndnsec sign-req [-h] [-k] name\nGeneral options");
23 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024 ("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 Yub61f5402014-02-26 17:46:11 -080033 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080034 {
Yingdi Yub61f5402014-02-26 17:46:11 -080035 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 Yu8d7468f2014-02-21 14:49:45 -080049 return 0;
50 }
51
Yingdi Yub61f5402014-02-26 17:46:11 -080052 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080053 {
Yingdi Yub61f5402014-02-26 17:46:11 -080054 std::cerr << "ERROR: name must be specified" << std::endl;
55 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056 return 1;
57 }
Yingdi Yub61f5402014-02-26 17:46:11 -080058
59 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080060 isKeyName = true;
61
62 shared_ptr<IdentityCertificate> selfSignCert;
63
Yingdi Yub61f5402014-02-26 17:46:11 -080064 KeyChain keyChain;
65
66 if (isKeyName)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067 {
Yingdi Yub61f5402014-02-26 17:46:11 -080068 selfSignCert = keyChain.selfSign(name);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069 }
Yingdi Yub61f5402014-02-26 17:46:11 -080070 else
Yingdi Yu8d7468f2014-02-21 14:49:45 -080071 {
Yingdi Yub61f5402014-02-26 17:46:11 -080072 Name keyName = keyChain.getDefaultKeyNameForIdentity(name);
73 selfSignCert = keyChain.selfSign(keyName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080074 }
75
Yingdi Yub61f5402014-02-26 17:46:11 -080076 io::save(*selfSignCert, std::cout);
77
Yingdi Yu8d7468f2014-02-21 14:49:45 -080078 return 0;
79}
80
81#endif //NDNSEC_SIGN_REQ_HPP