blob: a36a85bb8295fb714558c1985a12a06cdafaedce [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
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 Yu8d7468f2014-02-21 14:49:45 -080013 */
14
15#ifndef NDNSEC_DSK_GEN_HPP
16#define NDNSEC_DSK_GEN_HPP
17
18#include "ndnsec-util.hpp"
19
Yingdi Yub61f5402014-02-26 17:46:11 -080020int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080021ndnsec_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 Yub61f5402014-02-26 17:46:11 -080030 po::options_description description("General Usage\n ndnsec dsk-gen [-h] identity\nGeneral options");
31 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032 ("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 Yub61f5402014-02-26 17:46:11 -080042 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080043 {
Yingdi Yub61f5402014-02-26 17:46:11 -080044 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 Yu8d7468f2014-02-21 14:49:45 -080058 return 0;
59 }
60
Yingdi Yub61f5402014-02-26 17:46:11 -080061 if (vm.count("identity") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080062 {
63 std::cerr << "identity must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080064 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065 return 1;
66 }
67
68 shared_ptr<IdentityCertificate> kskCert;
69 Name signingCertName;
Yingdi Yub61f5402014-02-26 17:46:11 -080070
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 Yu8d7468f2014-02-21 14:49:45 -080079 {
Yingdi Yub61f5402014-02-26 17:46:11 -080080 shared_ptr<IdentityCertificate> dskCert = keyChain.getCertificate(defaultCertName);
81 SignatureSha256WithRsa sha256sig(dskCert->getSignature());
Yingdi Yu8d7468f2014-02-21 14:49:45 -080082
Yingdi Yub61f5402014-02-26 17:46:11 -080083 Name keyLocatorName = sha256sig.getKeyLocator().getName(); // will throw exception if keylocator is absent or it is not a name
Yingdi Yu8d7468f2014-02-21 14:49:45 -080084
Yingdi Yub61f5402014-02-26 17:46:11 -080085 Name kskName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName);
86 Name kskCertName = keyChain.getDefaultCertificateNameForKey(kskName);
87 signingCertName = kskCertName;
88 kskCert = keyChain.getCertificate(kskCertName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080089 }
Yingdi Yub61f5402014-02-26 17:46:11 -080090 else
Yingdi Yu8d7468f2014-02-21 14:49:45 -080091 {
Yingdi Yub61f5402014-02-26 17:46:11 -080092 signingCertName = defaultCertName;
93 kskCert = keyChain.getCertificate(defaultCertName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080094 }
95
Yingdi Yub61f5402014-02-26 17:46:11 -080096 if (!static_cast<bool>(kskCert))
Yingdi Yu8d7468f2014-02-21 14:49:45 -080097 {
98 std::cerr << "ERROR: no KSK certificate." << std::endl;
99 return 1;
100 }
101
Yingdi Yub61f5402014-02-26 17:46:11 -0800102 Name newKeyName;
103 switch (keyType)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800104 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800105 case 'r':
106 {
107 newKeyName = keyChain.generateRSAKeyPair(Name(identityName), false, keySize);
108 if (0 == newKeyName.size())
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800109 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800110 std::cerr << "fail to generate key!" << std::endl;
111 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800112 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800113 break;
114 }
115 default:
116 std::cerr << "Unrecongized key type" << "\n";
117 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800118 return 1;
119 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800120
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 Yu8d7468f2014-02-21 14:49:45 -0800148}
149
150#endif //NDNSEC_DSK_GEN_HPP