blob: eced361695076b710f01d0e7b50b3862154a99b0 [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 Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 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.
Yingdi Yu8d7468f2014-02-21 14:49:45 -080020 */
21
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080022#include "ndnsec.hpp"
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080023#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080025namespace ndn {
26namespace ndnsec {
27
Yingdi Yub61f5402014-02-26 17:46:11 -080028int
29ndnsec_sign_req(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030{
31 using namespace ndn;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070032 using namespace ndn::security;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080033 namespace po = boost::program_options;
34
35 std::string name;
36 bool isKeyName = false;
37
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080038 po::options_description description(
39 "General Usage\n ndnsec sign-req [-h] [-k] name\nGeneral options");
Yingdi Yub61f5402014-02-26 17:46:11 -080040 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 ("help,h", "produce help message")
42 ("key,k", "optional, if specified, name is keyName (e.g. /ndn/edu/ucla/alice/ksk-123456789), otherwise identity name")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080043 ("name,n", po::value<std::string>(&name), "name, for example, /ndn/edu/ucla/alice");
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044
45 po::positional_options_description p;
46 p.add("name", 1);
47
48 po::variables_map vm;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080049 try {
50 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
51 po::notify(vm);
52 }
53 catch (const std::exception& e) {
54 std::cerr << "ERROR: " << e.what() << std::endl;
55 std::cerr << description << std::endl;
56 return 1;
57 }
Yingdi Yub61f5402014-02-26 17:46:11 -080058
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080059 if (vm.count("help") != 0) {
60 std::cerr << description << std::endl;
61 return 0;
62 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080064 if (vm.count("name") == 0) {
65 std::cerr << "ERROR: name must be specified" << std::endl;
66 std::cerr << description << std::endl;
67 return 1;
68 }
Yingdi Yub61f5402014-02-26 17:46:11 -080069
70 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080071 isKeyName = true;
72
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080073 shared_ptr<security::v1::IdentityCertificate> selfSignCert;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080074
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080075 security::v1::KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -080076
77 if (isKeyName)
Yingdi Yu37339fd2014-11-26 22:26:43 -080078 selfSignCert = keyChain.selfSign(name);
79 else {
80 Name keyName = keyChain.getDefaultKeyNameForIdentity(name);
81 selfSignCert = keyChain.selfSign(keyName);
82 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080083
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080084 if (selfSignCert != nullptr) {
Yingdi Yu37339fd2014-11-26 22:26:43 -080085 io::save(*selfSignCert, std::cout);
86 return 0;
87 }
88 else {
89 std::cerr << "ERROR: Public key does not exist" << std::endl;
90 return 1;
91 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080092}
93
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080094} // namespace ndnsec
95} // namespace ndn