blob: 7b3b7ba20e84ff5123caf58030951c8d849ef81a [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#ifndef NLSR_KM_HPP
2#define NLSR_KM_HPP
3
4#include <ndn-cpp-dev/face.hpp>
5#include <ndn-cpp-dev/data.hpp>
6#include <ndn-cpp-dev/security/key-chain.hpp>
7#include <ndn-cpp-dev/security/validator.hpp>
8#include <ndn-cpp-dev/util/scheduler.hpp>
9#include <ndn-cpp-dev/util/random.hpp>
10#include <ndn-cpp-dev/security/identity-certificate.hpp>
11#include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp>
12
13#include <ndn-cpp-dev/security/sec-public-info-sqlite3.hpp>
14#include <ndn-cpp-dev/security/sec-public-info-memory.hpp>
15//TPM
16#include <ndn-cpp-dev/security/sec-tpm-file.hpp>
17#include <ndn-cpp-dev/security/sec-tpm-memory.hpp>
18
19#ifdef NDN_CPP_HAVE_OSX_SECURITY
20#include <ndn-cpp-dev/security/sec-tpm-osx.hpp>
21#endif
22
23#include <list>
24#include "nlsr_conf_param.hpp"
25
26namespace nlsr
27{
28 enum nlsrKeyType
29 {
30 KEY_TYPE_ROOT,
31 KEY_TYPE_SITE,
32 KEY_TYPE_OPERATOR,
33 KEY_TYPE_ROUTER,
34 KEY_TYPE_PROCESS
35 };
36
37 class KeyManager: public ndn::KeyChain, public ndn::Validator
38 {
39 typedef SecPublicInfo::Error InfoError;
40 typedef SecTpm::Error TpmError;
41 public:
42 KeyManager()
43 : certSeqNo(1)
44 {
45 }
46
47 void initKeyManager(ConfParameter &cp);
48
49 void
50 checkPolicy (const ndn::Data& data,
51 int stepCount,
52 const ndn::OnDataValidated &onValidated,
53 const ndn::OnDataValidationFailed &onValidationFailed,
54 std::vector<ndn::shared_ptr<ndn::ValidationRequest> > &nextSteps)
55 {}
56
57 void
58 checkPolicy (const ndn::Interest& interest,
59 int stepCount,
60 const ndn::OnInterestValidated &onValidated,
61 const ndn::OnInterestValidationFailed &onValidationFailed,
62 std::vector<ndn::shared_ptr<ndn::ValidationRequest> > &nextSteps)
63 {}
64
65 void signData(ndn::Data& data)
66 {
67 ndn::KeyChain::signByIdentity(data,routerIdentity);
68 //ndn::SignatureSha256WithRsa signature(data.getSignature());
69 //signature.setKeyLocator(routerCertName);
70 }
71
72 ndn::shared_ptr<ndn::IdentityCertificate>
73 getCertificate(ndn::Name certificateName)
74 {
75 return ndn::KeyChain::getCertificate(routerCertName);
76 }
77
78 ndn::Name
79 createIdentity(const ndn::Name identityName, const ndn::Name signee)
80 {
81 ndn::KeyChain::addIdentity(identityName);
82 ndn::Name keyName;
83 try
84 {
85 keyName = ndn::KeyChain::getDefaultKeyNameForIdentity(identityName);
86 }
87 catch(InfoError& e)
88 {
89 keyName = ndn::KeyChain::generateRSAKeyPairAsDefault(identityName, true);
90 }
91 ndn::shared_ptr<ndn::PublicKey> pubKey;
92 try
93 {
94 pubKey = ndn::KeyChain::getPublicKey(keyName);
95 }
96 catch(InfoError& e)
97 {
98 //return ndn::shared_ptr<ndn::IdentityCertificate>()->getName();
99 return identityName;
100 }
101 ndn::Name certName;
102 try
103 {
104 certName = ndn::KeyChain::getDefaultCertificateNameForKey(keyName);
105 }
106 catch(InfoError& e)
107 {
108 ndn::shared_ptr<ndn::IdentityCertificate> certificate =
109 ndn::make_shared<ndn::IdentityCertificate>();
110 ndn::Name certificateName = keyName.getPrefix(-1);
111 certificateName.append("KEY").append(
112 keyName.get(-1)).append("ID-CERT").appendVersion();
113 certificate->setName(certificateName);
114 certificate->setNotBefore(ndn::getNow());
115 certificate->setNotAfter(ndn::getNow() + 31536000 /* 1 year*/);
116 certificate->setPublicKeyInfo(*pubKey);
117 certificate->addSubjectDescription(
118 ndn::CertificateSubjectDescription("2.5.4.41",
119 keyName.toUri()));
120 certificate->encode();
121 try
122 {
123 ndn::KeyChain::signByIdentity(*certificate,signee);
124 }
125 catch(InfoError& e)
126 {
127 try
128 {
129 ndn::KeyChain::deleteIdentity(identityName);
130 }
131 catch(InfoError& e)
132 {
133 }
134 return identityName;
135 }
136 certName=certificate->getName();
137 }
138 return certName;
139 }
140
141 ndn::Name getRouterCertName();
142
143 uint32_t getCertSeqNo();
144 void setCerSeqNo(uint32_t csn);
145 void initCertSeqFromFile(string certSeqFileDir);
146 void writeCertSeqToFile();
147
148 private:
149 ndn::Name routerIdentity;
150 ndn::Name routerCertName;
151 ndn::Name routerKeyName;
152 uint32_t certSeqNo;
153 string certSeqFileNameWithPath;
154
155 };
156}
157
158#endif