blob: a9109ee3b62adddaac1e5db9b57449c7195d4c44 [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
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029ndnsec_dsk_gen(int argc, char** argv)
30{
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 identityName;
36 char keyType = 'r';
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037
Alexander Afanasyevace74452014-11-30 22:28:24 -080038 po::options_description description("General Usage\n"
39 " ndnsec dsk-gen [-h] [-t keyType] identity\n"
40 "General options");
Yingdi Yub61f5402014-02-26 17:46:11 -080041 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080042 ("help,h", "produce help message")
Alexander Afanasyevace74452014-11-30 22:28:24 -080043 ("identity,i", po::value<std::string>(&identityName),
44 "identity name, for example, /ndn/ucla.edu/alice")
45 ("type,t", po::value<char>(&keyType)->default_value('r'),
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070046 "optional, key type, r for RSA key (default), e for EC key.")
Alexander Afanasyevace74452014-11-30 22:28:24 -080047 // ("size,s", po::value<int>(&keySize)->default_value(2048),
48 // "optional, key size, 2048 (default)")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080049 ;
50
51 po::positional_options_description p;
52 p.add("identity", 1);
53
54 po::variables_map vm;
Alexander Afanasyevace74452014-11-30 22:28:24 -080055 try {
56 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
57 po::notify(vm);
58 }
59 catch (const std::exception& e) {
60 std::cerr << "ERROR: " << e.what() << std::endl;
61 std::cerr << description << std::endl;
62 return 1;
63 }
Yingdi Yub61f5402014-02-26 17:46:11 -080064
Alexander Afanasyevace74452014-11-30 22:28:24 -080065 if (vm.count("help") != 0) {
66 std::cerr << description << std::endl;
67 return 0;
68 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069
Alexander Afanasyevace74452014-11-30 22:28:24 -080070 if (vm.count("identity") == 0) {
71 std::cerr << "identity must be specified" << std::endl;
72 std::cerr << description << std::endl;
73 return 1;
74 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080075
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080076 shared_ptr<security::v1::IdentityCertificate> kskCert;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080077 Name signingCertName;
Yingdi Yub61f5402014-02-26 17:46:11 -080078
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080079 security::v1::KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -080080
Alexander Afanasyevace74452014-11-30 22:28:24 -080081 try {
82 Name defaultCertName = keyChain.getDefaultCertificateNameForIdentity(identityName);
83 bool isDefaultDsk = false;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080084 std::string keyUsageTag = defaultCertName.get(-3).toUri().substr(0, 4);
Alexander Afanasyevace74452014-11-30 22:28:24 -080085 if (keyUsageTag == "ksk-")
86 isDefaultDsk = false;
87 else if (keyUsageTag == "dsk-")
88 isDefaultDsk = true;
89 else {
90 std::cerr << "ERROR: Unknown key usage tag: " << keyUsageTag << std::endl;
91 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080092 }
Alexander Afanasyevace74452014-11-30 22:28:24 -080093
94 if (isDefaultDsk) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080095 shared_ptr<security::v1::IdentityCertificate> dskCert = keyChain.getCertificate(defaultCertName);
Alexander Afanasyevace74452014-11-30 22:28:24 -080096
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080097 if (dskCert != nullptr) {
Alexander Afanasyevace74452014-11-30 22:28:24 -080098 SignatureSha256WithRsa sha256sig(dskCert->getSignature());
99
100 Name keyLocatorName = sha256sig.getKeyLocator().getName();
101
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800102 Name kskName = security::v1::IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName);
Alexander Afanasyevace74452014-11-30 22:28:24 -0800103 Name kskCertName = keyChain.getDefaultCertificateNameForKey(kskName);
104 signingCertName = kskCertName;
105 kskCert = keyChain.getCertificate(kskCertName);
106 }
107 else {
108 std::cerr << "ERROR: The default certificate is missing." << std::endl;
109 return 1;
110 }
111 }
112 else {
Yingdi Yub61f5402014-02-26 17:46:11 -0800113 signingCertName = defaultCertName;
114 kskCert = keyChain.getCertificate(defaultCertName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800115 }
116
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800117 if (kskCert == nullptr) {
Alexander Afanasyevace74452014-11-30 22:28:24 -0800118 std::cerr << "ERROR: KSK certificate is missing." << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800119 return 1;
120 }
121
Alexander Afanasyevace74452014-11-30 22:28:24 -0800122 Name newKeyName;
123 switch (keyType) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800124 case 'r': {
Alexander Afanasyevace74452014-11-30 22:28:24 -0800125 RsaKeyParams params;
126 newKeyName = keyChain.generateRsaKeyPair(Name(identityName), false, params.getKeySize());
127 if (0 == newKeyName.size()) {
128 std::cerr << "ERROR: Fail to generate RSA key!" << std::endl;
129 return 1;
130 }
131 break;
132 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800133 case 'e': {
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700134 EcKeyParams params;
135 newKeyName = keyChain.generateEcKeyPair(Name(identityName), false, params.getKeySize());
Alexander Afanasyevace74452014-11-30 22:28:24 -0800136 if (0 == newKeyName.size()) {
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700137 std::cerr << "ERROR: Fail to generate EC key!" << std::endl;
Alexander Afanasyevace74452014-11-30 22:28:24 -0800138 return 1;
139 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800140 break;
141 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800142 default:
143 std::cerr << "ERROR: Unrecongized key type"
144 << "\n";
145 std::cerr << description << std::endl;
146 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800147 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800148
Alexander Afanasyevace74452014-11-30 22:28:24 -0800149 Name certName = newKeyName.getPrefix(-1);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800150 certName.append("KEY").append(newKeyName.get(-1)).append("ID-CERT").appendVersion();
Yingdi Yub61f5402014-02-26 17:46:11 -0800151
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800152 shared_ptr<security::v1::IdentityCertificate> certificate =
Alexander Afanasyevace74452014-11-30 22:28:24 -0800153 keyChain.prepareUnsignedIdentityCertificate(newKeyName,
154 Name(identityName),
155 kskCert->getNotBefore(),
156 kskCert->getNotAfter(),
157 kskCert->getSubjectDescriptionList());
Yingdi Yub61f5402014-02-26 17:46:11 -0800158
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800159 if (certificate != nullptr)
Alexander Afanasyevace74452014-11-30 22:28:24 -0800160 certificate->encode();
161 else {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800162 std::cerr << "ERROR: Cannot format the certificate of the requested dsk."
163 << "\n";
Alexander Afanasyevace74452014-11-30 22:28:24 -0800164 return 1;
165 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800166
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700167 keyChain.sign(*certificate,
168 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_CERT, signingCertName));
Yingdi Yub61f5402014-02-26 17:46:11 -0800169
Alexander Afanasyevace74452014-11-30 22:28:24 -0800170 keyChain.addCertificateAsIdentityDefault(*certificate);
Yingdi Yub61f5402014-02-26 17:46:11 -0800171
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800172 std::cerr << "OK: dsk certificate with name [" << certificate->getName()
173 << "] has been successfully installed\n";
Alexander Afanasyevace74452014-11-30 22:28:24 -0800174 return 0;
175 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800176 catch (const std::runtime_error& e) {
Alexander Afanasyevace74452014-11-30 22:28:24 -0800177 std::cerr << "ERROR: other runtime errors: " << e.what() << "\n";
178 return 1;
179 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800180}
181
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800182} // namespace ndnsec
183} // namespace ndn