blob: 61c6109491a84396c22d0c5553f24278d3272d9f [file] [log] [blame]
Yingdi Yu87581582014-01-14 14:28:39 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
Yingdi Yu4f324632014-01-15 18:10:03 -08008#include <ndn-cpp/security/sec-public-info-memory.hpp>
Yingdi Yu87581582014-01-14 14:28:39 -08009
10#include <algorithm>
Yingdi Yu4f324632014-01-15 18:10:03 -080011#include <ndn-cpp/security/identity-certificate.hpp>
Yingdi Yu87581582014-01-14 14:28:39 -080012
13using namespace std;
14
15namespace ndn {
16
17SecPublicInfoMemory::~SecPublicInfoMemory()
18{
19}
20
21bool
22SecPublicInfoMemory::doesIdentityExist(const Name& identityName)
23{
24 string identityUri = identityName.toUri();
25 return find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end();
26}
27
28void
29SecPublicInfoMemory::addIdentity(const Name& identityName)
30{
31 string identityUri = identityName.toUri();
32 if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end())
33 throw Error("Identity already exists: " + identityUri);
34
35 identityStore_.push_back(identityUri);
36}
37
38bool
39SecPublicInfoMemory::revokeIdentity()
40{
41#if 1
42 throw runtime_error("SecPublicInfoMemory::revokeIdentity not implemented");
43#endif
44}
45
46bool
47SecPublicInfoMemory::doesPublicKeyExist(const Name& keyName)
48{
49 return keyStore_.find(keyName.toUri()) != keyStore_.end();
50}
51
52void
53SecPublicInfoMemory::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKey)
54{
55 Name identityName = keyName.getSubName(0, keyName.size() - 1);
56
57 if (!doesIdentityExist(identityName))
58 addIdentity(identityName);
59
60 if (doesPublicKeyExist(keyName))
61 throw Error("a key with the same name already exists!");
62
63 keyStore_[keyName.toUri()] = ptr_lib::make_shared<KeyRecord>(keyType, publicKey);
64}
65
66ptr_lib::shared_ptr<PublicKey>
67SecPublicInfoMemory::getPublicKey(const Name& keyName)
68{
69 KeyStore::iterator record = keyStore_.find(keyName.toUri());
70 if (record == keyStore_.end())
71 // Not found. Silently return null.
72 return ptr_lib::shared_ptr<PublicKey>();
73
74 return ptr_lib::make_shared<PublicKey> (record->second->getKey());
75}
76
77void
78SecPublicInfoMemory::activatePublicKey(const Name& keyName)
79{
80#if 1
81 throw runtime_error("SecPublicInfoMemory::activateKey not implemented");
82#endif
83}
84
85void
86SecPublicInfoMemory::deactivatePublicKey(const Name& keyName)
87{
88#if 1
89 throw runtime_error("SecPublicInfoMemory::deactivateKey not implemented");
90#endif
91}
92
93bool
94SecPublicInfoMemory::doesCertificateExist(const Name& certificateName)
95{
96 return certificateStore_.find(certificateName.toUri()) != certificateStore_.end();
97}
98
99void
100SecPublicInfoMemory::addCertificate(const IdentityCertificate& certificate)
101{
102 const Name& certificateName = certificate.getName();
103 const Name& keyName = certificate.getPublicKeyName();
104
105 if (!doesPublicKeyExist(keyName))
106 throw Error("No corresponding Key record for certificate! " + keyName.toUri() + " " + certificateName.toUri());
107
108 // Check if certificate has already existed!
109 if (doesCertificateExist(certificateName))
110 throw Error("Certificate has already been installed!");
111
112 // Check if the public key of certificate is the same as the key record.
113 ptr_lib::shared_ptr<PublicKey> pubKey = getPublicKey(keyName);
114 if (!pubKey || (*pubKey) != certificate.getPublicKeyInfo())
115 throw Error("Certificate does not match the public key!");
116
117 // Insert the certificate.
118 certificateStore_[certificateName.toUri()] = ptr_lib::make_shared<IdentityCertificate> (certificate);
119}
120
121ptr_lib::shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -0800122SecPublicInfoMemory::getCertificate(const Name& certificateName)
Yingdi Yu87581582014-01-14 14:28:39 -0800123{
124 CertificateStore::iterator record = certificateStore_.find(certificateName.toUri());
125 if (record == certificateStore_.end())
126 // Not found. Silently return null.
127 return ptr_lib::shared_ptr<IdentityCertificate>();
128
129 return record->second;
130}
131
132Name
133SecPublicInfoMemory::getDefaultIdentity()
134{
135 return Name(defaultIdentity_);
136}
137
138void
139SecPublicInfoMemory::setDefaultIdentityInternal(const Name& identityName)
140{
141 string identityUri = identityName.toUri();
142 if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end())
143 defaultIdentity_ = identityUri;
144 else
145 // The identity doesn't exist, so clear the default.
146 defaultIdentity_.clear();
147}
148
149Name
150SecPublicInfoMemory::getDefaultKeyNameForIdentity(const Name& identityName)
151{
152 return defaultKeyName_;
153}
154
155void
156SecPublicInfoMemory::setDefaultKeyNameForIdentityInternal(const Name& keyName)
157{
158 defaultKeyName_ = keyName;
159}
160
161Name
162SecPublicInfoMemory::getDefaultCertificateNameForKey(const Name& keyName)
163{
164 return defaultCert_;
165}
166
167void
168SecPublicInfoMemory::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
169{
170 defaultCert_ = certificateName;
171}
172
173
174std::vector<Name>
175SecPublicInfoMemory::getAllIdentities(bool isDefault)
176{
177 throw runtime_error("SecPublicInfoMemory::getAllIdentities not implemented");
178}
179
180std::vector<Name>
181SecPublicInfoMemory::getAllKeyNames(bool isDefault)
182{
183 throw runtime_error("SecPublicInfoMemory::getAllKeyNames not implemented");
184}
185
186std::vector<Name>
187SecPublicInfoMemory::getAllKeyNamesOfIdentity(const Name& identity, bool isDefault)
188{
189 throw runtime_error("SecPublicInfoMemory::getAllKeyNamesOfIdentity not implemented");
190}
191
192std::vector<Name>
193SecPublicInfoMemory::getAllCertificateNames(bool isDefault)
194{
195 throw runtime_error("SecPublicInfoMemory::getAllCertificateNames not implemented");
196}
197
198std::vector<Name>
199SecPublicInfoMemory::getAllCertificateNamesOfKey(const Name& keyName, bool isDefault)
200{
201 throw runtime_error("SecPublicInfoMemory::getAllCertificateNamesOfKey not implemented");
202}
203
204
205}