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