blob: e0db0f38300ba8e4f17bc1a8ababb1797491d0aa [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022 */
23
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080024#ifndef NDN_TOOLS_NDNSEC_SIGN_REQ_HPP
25#define NDN_TOOLS_NDNSEC_SIGN_REQ_HPP
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028
Yingdi Yub61f5402014-02-26 17:46:11 -080029int
30ndnsec_sign_req(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031{
32 using namespace ndn;
33 namespace po = boost::program_options;
34
35 std::string name;
36 bool isKeyName = false;
37
Yingdi Yub61f5402014-02-26 17:46:11 -080038 po::options_description description("General Usage\n ndnsec sign-req [-h] [-k] name\nGeneral options");
39 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080040 ("help,h", "produce help message")
41 ("key,k", "optional, if specified, name is keyName (e.g. /ndn/edu/ucla/alice/ksk-123456789), otherwise identity name")
42 ("name,n", po::value<std::string>(&name), "name, for example, /ndn/edu/ucla/alice")
43 ;
44
45 po::positional_options_description p;
46 p.add("name", 1);
47
48 po::variables_map vm;
Yingdi Yub61f5402014-02-26 17:46:11 -080049 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080050 {
Yingdi Yub61f5402014-02-26 17:46:11 -080051 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
52 vm);
53 po::notify(vm);
54 }
55 catch (const std::exception& e)
56 {
57 std::cerr << "ERROR: " << e.what() << std::endl;
58 std::cerr << description << std::endl;
59 return 1;
60 }
61
62 if (vm.count("help") != 0)
63 {
64 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065 return 0;
66 }
67
Yingdi Yub61f5402014-02-26 17:46:11 -080068 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069 {
Yingdi Yub61f5402014-02-26 17:46:11 -080070 std::cerr << "ERROR: name must be specified" << std::endl;
71 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072 return 1;
73 }
Yingdi Yub61f5402014-02-26 17:46:11 -080074
75 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076 isKeyName = true;
77
78 shared_ptr<IdentityCertificate> selfSignCert;
79
Yingdi Yub61f5402014-02-26 17:46:11 -080080 KeyChain keyChain;
81
82 if (isKeyName)
Yingdi Yu37339fd2014-11-26 22:26:43 -080083 selfSignCert = keyChain.selfSign(name);
84 else {
85 Name keyName = keyChain.getDefaultKeyNameForIdentity(name);
86 selfSignCert = keyChain.selfSign(keyName);
87 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080088
Yingdi Yu37339fd2014-11-26 22:26:43 -080089 if (static_cast<bool>(selfSignCert)) {
90 io::save(*selfSignCert, std::cout);
91 return 0;
92 }
93 else {
94 std::cerr << "ERROR: Public key does not exist" << std::endl;
95 return 1;
96 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080097}
98
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080099#endif // NDN_TOOLS_NDNSEC_SIGN_REQ_HPP