Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 22 | #include "ndnsec.hpp" |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 23 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 25 | namespace ndn { |
| 26 | namespace ndnsec { |
| 27 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 28 | int |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 29 | ndnsec_dsk_gen(int argc, char** argv) |
| 30 | { |
| 31 | using namespace ndn; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 32 | using namespace ndn::security; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 33 | namespace po = boost::program_options; |
| 34 | |
| 35 | std::string identityName; |
| 36 | char keyType = 'r'; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 38 | po::options_description description("General Usage\n" |
| 39 | " ndnsec dsk-gen [-h] [-t keyType] identity\n" |
| 40 | "General options"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 41 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 42 | ("help,h", "produce help message") |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 43 | ("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 Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 46 | "optional, key type, r for RSA key (default), e for EC key.") |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 47 | // ("size,s", po::value<int>(&keySize)->default_value(2048), |
| 48 | // "optional, key size, 2048 (default)") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 49 | ; |
| 50 | |
| 51 | po::positional_options_description p; |
| 52 | p.add("identity", 1); |
| 53 | |
| 54 | po::variables_map vm; |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 55 | 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 Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 65 | if (vm.count("help") != 0) { |
| 66 | std::cerr << description << std::endl; |
| 67 | return 0; |
| 68 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 69 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 70 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 75 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 76 | shared_ptr<security::v1::IdentityCertificate> kskCert; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 77 | Name signingCertName; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 78 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 79 | security::v1::KeyChain keyChain; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 80 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 81 | try { |
| 82 | Name defaultCertName = keyChain.getDefaultCertificateNameForIdentity(identityName); |
| 83 | bool isDefaultDsk = false; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 84 | std::string keyUsageTag = defaultCertName.get(-3).toUri().substr(0, 4); |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 85 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 92 | } |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 93 | |
| 94 | if (isDefaultDsk) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 95 | shared_ptr<security::v1::IdentityCertificate> dskCert = keyChain.getCertificate(defaultCertName); |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 97 | if (dskCert != nullptr) { |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 98 | SignatureSha256WithRsa sha256sig(dskCert->getSignature()); |
| 99 | |
| 100 | Name keyLocatorName = sha256sig.getKeyLocator().getName(); |
| 101 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 102 | Name kskName = security::v1::IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName); |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 103 | 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 Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 113 | signingCertName = defaultCertName; |
| 114 | kskCert = keyChain.getCertificate(defaultCertName); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 117 | if (kskCert == nullptr) { |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 118 | std::cerr << "ERROR: KSK certificate is missing." << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 119 | return 1; |
| 120 | } |
| 121 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 122 | Name newKeyName; |
| 123 | switch (keyType) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 124 | case 'r': { |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 125 | 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 Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 133 | case 'e': { |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 134 | EcKeyParams params; |
| 135 | newKeyName = keyChain.generateEcKeyPair(Name(identityName), false, params.getKeySize()); |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 136 | if (0 == newKeyName.size()) { |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 137 | std::cerr << "ERROR: Fail to generate EC key!" << std::endl; |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 138 | return 1; |
| 139 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 140 | break; |
| 141 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 142 | default: |
| 143 | std::cerr << "ERROR: Unrecongized key type" |
| 144 | << "\n"; |
| 145 | std::cerr << description << std::endl; |
| 146 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 147 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 149 | Name certName = newKeyName.getPrefix(-1); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 150 | certName.append("KEY").append(newKeyName.get(-1)).append("ID-CERT").appendVersion(); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 152 | shared_ptr<security::v1::IdentityCertificate> certificate = |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 153 | keyChain.prepareUnsignedIdentityCertificate(newKeyName, |
| 154 | Name(identityName), |
| 155 | kskCert->getNotBefore(), |
| 156 | kskCert->getNotAfter(), |
| 157 | kskCert->getSubjectDescriptionList()); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 158 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 159 | if (certificate != nullptr) |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 160 | certificate->encode(); |
| 161 | else { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 162 | std::cerr << "ERROR: Cannot format the certificate of the requested dsk." |
| 163 | << "\n"; |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 164 | return 1; |
| 165 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 166 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 167 | keyChain.sign(*certificate, |
| 168 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_CERT, signingCertName)); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 169 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 170 | keyChain.addCertificateAsIdentityDefault(*certificate); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 172 | std::cerr << "OK: dsk certificate with name [" << certificate->getName() |
| 173 | << "] has been successfully installed\n"; |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 174 | return 0; |
| 175 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 176 | catch (const std::runtime_error& e) { |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 177 | std::cerr << "ERROR: other runtime errors: " << e.what() << "\n"; |
| 178 | return 1; |
| 179 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 182 | } // namespace ndnsec |
| 183 | } // namespace ndn |