blob: db381b70bf355521d45e702f830499b16de94a7a [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080013 */
14
15#ifndef NDNSEC_SIGN_REQ_HPP
16#define NDNSEC_SIGN_REQ_HPP
17
18#include "ndnsec-util.hpp"
19
Yingdi Yub61f5402014-02-26 17:46:11 -080020int
21ndnsec_sign_req(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022{
23 using namespace ndn;
24 namespace po = boost::program_options;
25
26 std::string name;
27 bool isKeyName = false;
28
Yingdi Yub61f5402014-02-26 17:46:11 -080029 po::options_description description("General Usage\n ndnsec sign-req [-h] [-k] name\nGeneral options");
30 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031 ("help,h", "produce help message")
32 ("key,k", "optional, if specified, name is keyName (e.g. /ndn/edu/ucla/alice/ksk-123456789), otherwise identity name")
33 ("name,n", po::value<std::string>(&name), "name, for example, /ndn/edu/ucla/alice")
34 ;
35
36 po::positional_options_description p;
37 p.add("name", 1);
38
39 po::variables_map vm;
Yingdi Yub61f5402014-02-26 17:46:11 -080040 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 {
Yingdi Yub61f5402014-02-26 17:46:11 -080042 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
43 vm);
44 po::notify(vm);
45 }
46 catch (const std::exception& e)
47 {
48 std::cerr << "ERROR: " << e.what() << std::endl;
49 std::cerr << description << std::endl;
50 return 1;
51 }
52
53 if (vm.count("help") != 0)
54 {
55 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056 return 0;
57 }
58
Yingdi Yub61f5402014-02-26 17:46:11 -080059 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080060 {
Yingdi Yub61f5402014-02-26 17:46:11 -080061 std::cerr << "ERROR: name must be specified" << std::endl;
62 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063 return 1;
64 }
Yingdi Yub61f5402014-02-26 17:46:11 -080065
66 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067 isKeyName = true;
68
69 shared_ptr<IdentityCertificate> selfSignCert;
70
Yingdi Yub61f5402014-02-26 17:46:11 -080071 KeyChain keyChain;
72
73 if (isKeyName)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080074 {
Yingdi Yub61f5402014-02-26 17:46:11 -080075 selfSignCert = keyChain.selfSign(name);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076 }
Yingdi Yub61f5402014-02-26 17:46:11 -080077 else
Yingdi Yu8d7468f2014-02-21 14:49:45 -080078 {
Yingdi Yub61f5402014-02-26 17:46:11 -080079 Name keyName = keyChain.getDefaultKeyNameForIdentity(name);
80 selfSignCert = keyChain.selfSign(keyName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080081 }
82
Yingdi Yub61f5402014-02-26 17:46:11 -080083 io::save(*selfSignCert, std::cout);
84
Yingdi Yu8d7468f2014-02-21 14:49:45 -080085 return 0;
86}
87
88#endif //NDNSEC_SIGN_REQ_HPP