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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 | |
| 24 | #ifndef NDNSEC_DSK_GEN_HPP |
| 25 | #define NDNSEC_DSK_GEN_HPP |
| 26 | |
| 27 | #include "ndnsec-util.hpp" |
| 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'; |
| 37 | int keySize = 2048; |
| 38 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 39 | po::options_description description("General Usage\n ndnsec dsk-gen [-h] identity\nGeneral options"); |
| 40 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 41 | ("help,h", "produce help message") |
| 42 | ("identity,i", po::value<std::string>(&identityName), "identity name, for example, /ndn/ucla.edu/alice") |
| 43 | // ("type,t", po::value<char>(&keyType)->default_value('r'), "optional, key type, r for RSA key (default)") |
| 44 | // ("size,s", po::value<int>(&keySize)->default_value(2048), "optional, key size, 2048 (default)") |
| 45 | ; |
| 46 | |
| 47 | po::positional_options_description p; |
| 48 | p.add("identity", 1); |
| 49 | |
| 50 | po::variables_map vm; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 51 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 52 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 53 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 54 | vm); |
| 55 | po::notify(vm); |
| 56 | } |
| 57 | catch (const std::exception& e) |
| 58 | { |
| 59 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 60 | std::cerr << description << std::endl; |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | if (vm.count("help") != 0) |
| 65 | { |
| 66 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 70 | if (vm.count("identity") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 71 | { |
| 72 | std::cerr << "identity must be specified" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 73 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | shared_ptr<IdentityCertificate> kskCert; |
| 78 | Name signingCertName; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 79 | |
| 80 | KeyChain keyChain; |
| 81 | |
| 82 | Name defaultCertName = keyChain.getDefaultCertificateNameForIdentity(identityName); |
| 83 | bool isDefaultDsk = false; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 84 | if (defaultCertName.get(-3).toUri().substr(0,4) == "dsk-") |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 85 | isDefaultDsk = true; |
| 86 | |
| 87 | if (isDefaultDsk) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 88 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 89 | shared_ptr<IdentityCertificate> dskCert = keyChain.getCertificate(defaultCertName); |
| 90 | SignatureSha256WithRsa sha256sig(dskCert->getSignature()); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 91 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 92 | Name keyLocatorName = sha256sig.getKeyLocator().getName(); // will throw exception if keylocator is absent or it is not a name |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 93 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 94 | Name kskName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName); |
| 95 | Name kskCertName = keyChain.getDefaultCertificateNameForKey(kskName); |
| 96 | signingCertName = kskCertName; |
| 97 | kskCert = keyChain.getCertificate(kskCertName); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 98 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 99 | else |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 100 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 101 | signingCertName = defaultCertName; |
| 102 | kskCert = keyChain.getCertificate(defaultCertName); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 105 | if (!static_cast<bool>(kskCert)) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 106 | { |
| 107 | std::cerr << "ERROR: no KSK certificate." << std::endl; |
| 108 | return 1; |
| 109 | } |
| 110 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 111 | Name newKeyName; |
| 112 | switch (keyType) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 113 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 114 | case 'r': |
| 115 | { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 116 | newKeyName = keyChain.generateRsaKeyPair(Name(identityName), false, keySize); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 117 | if (0 == newKeyName.size()) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 118 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 119 | std::cerr << "fail to generate key!" << std::endl; |
| 120 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 121 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | default: |
| 125 | std::cerr << "Unrecongized key type" << "\n"; |
| 126 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 127 | return 1; |
| 128 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 129 | |
| 130 | Name certName = newKeyName.getPrefix(-1); |
| 131 | certName.append("KEY") |
| 132 | .append(newKeyName.get(-1)) |
| 133 | .append("ID-CERT") |
| 134 | .appendVersion(); |
| 135 | |
| 136 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
| 137 | certificate->setName(certName); |
| 138 | certificate->setNotBefore(kskCert->getNotBefore()); |
| 139 | certificate->setNotAfter(kskCert->getNotAfter()); |
| 140 | |
| 141 | certificate->setPublicKeyInfo(*keyChain.getPublicKey(newKeyName)); |
| 142 | |
| 143 | const std::vector<CertificateSubjectDescription>& subList = |
| 144 | kskCert->getSubjectDescriptionList(); |
| 145 | |
| 146 | for (std::vector<CertificateSubjectDescription>::const_iterator it = subList.begin(); |
| 147 | it != subList.end(); it++) |
| 148 | certificate->addSubjectDescription(*it); |
| 149 | |
| 150 | certificate->encode(); |
| 151 | |
| 152 | keyChain.sign(*certificate, signingCertName); |
| 153 | |
| 154 | keyChain.addCertificateAsIdentityDefault(*certificate); |
| 155 | |
| 156 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | #endif //NDNSEC_DSK_GEN_HPP |