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 | af99f46 | 2015-01-19 21:43:09 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 24 | #ifndef NDN_TOOLS_NDNSEC_DSK_GEN_HPP |
| 25 | #define NDN_TOOLS_NDNSEC_DSK_GEN_HPP |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 27 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 28 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 29 | int |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 30 | ndnsec_dsk_gen(int argc, char** argv) |
| 31 | { |
| 32 | using namespace ndn; |
| 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'), |
| 46 | "optional, key type, r for RSA key (default), e for ECDSA key.") |
| 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 | |
| 76 | shared_ptr<IdentityCertificate> kskCert; |
| 77 | Name signingCertName; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 78 | |
| 79 | KeyChain keyChain; |
| 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; |
| 84 | std::string keyUsageTag = defaultCertName.get(-3).toUri().substr(0,4); |
| 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) { |
| 95 | shared_ptr<IdentityCertificate> dskCert = keyChain.getCertificate(defaultCertName); |
| 96 | |
| 97 | if (static_cast<bool>(dskCert)) { |
| 98 | SignatureSha256WithRsa sha256sig(dskCert->getSignature()); |
| 99 | |
| 100 | Name keyLocatorName = sha256sig.getKeyLocator().getName(); |
| 101 | |
| 102 | Name kskName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName); |
| 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 | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 117 | if (!static_cast<bool>(kskCert)) { |
| 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) { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 124 | case 'r': |
| 125 | { |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 126 | RsaKeyParams params; |
| 127 | newKeyName = keyChain.generateRsaKeyPair(Name(identityName), false, params.getKeySize()); |
| 128 | if (0 == newKeyName.size()) { |
| 129 | std::cerr << "ERROR: Fail to generate RSA key!" << std::endl; |
| 130 | return 1; |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | case 'e': |
| 135 | { |
| 136 | EcdsaKeyParams params; |
| 137 | newKeyName = keyChain.generateEcdsaKeyPair(Name(identityName), false, params.getKeySize()); |
| 138 | if (0 == newKeyName.size()) { |
| 139 | std::cerr << "ERROR: Fail to generate ECDSA key!" << std::endl; |
| 140 | return 1; |
| 141 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 142 | break; |
| 143 | } |
| 144 | default: |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 145 | std::cerr << "ERROR: Unrecongized key type" << "\n"; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 146 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 147 | return 1; |
| 148 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 150 | Name certName = newKeyName.getPrefix(-1); |
| 151 | certName.append("KEY") |
| 152 | .append(newKeyName.get(-1)) |
| 153 | .append("ID-CERT") |
| 154 | .appendVersion(); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 155 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 156 | shared_ptr<IdentityCertificate> certificate = |
| 157 | keyChain.prepareUnsignedIdentityCertificate(newKeyName, |
| 158 | Name(identityName), |
| 159 | kskCert->getNotBefore(), |
| 160 | kskCert->getNotAfter(), |
| 161 | kskCert->getSubjectDescriptionList()); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 162 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 163 | if (static_cast<bool>(certificate)) |
| 164 | certificate->encode(); |
| 165 | else { |
| 166 | std::cerr << "ERROR: Cannot format the certificate of the requested dsk." << "\n"; |
| 167 | return 1; |
| 168 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 169 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 170 | keyChain.sign(*certificate, |
| 171 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_CERT, signingCertName)); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 172 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 173 | keyChain.addCertificateAsIdentityDefault(*certificate); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 174 | |
Alexander Afanasyev | ace7445 | 2014-11-30 22:28:24 -0800 | [diff] [blame] | 175 | std::cerr << "OK: dsk certificate with name [" << certificate->getName() << |
| 176 | "] has been successfully installed\n"; |
| 177 | return 0; |
| 178 | } |
| 179 | catch (std::runtime_error& e) { |
| 180 | std::cerr << "ERROR: other runtime errors: " << e.what() << "\n"; |
| 181 | return 1; |
| 182 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 185 | #endif // NDN_TOOLS_NDNSEC_DSK_GEN_HPP |