blob: e9f93f240afaa8f299915dfb122b8a76b7dc3801 [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
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08008#include "common.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -08009
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080010#include "sec-public-info-memory.hpp"
11#include "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
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800174void
175SecPublicInfoMemory::getAllIdentities(std::vector<Name> &nameList, bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800176{
177 throw runtime_error("SecPublicInfoMemory::getAllIdentities not implemented");
178}
179
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800180void
181SecPublicInfoMemory::getAllKeyNames(std::vector<Name> &nameList, bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800182{
183 throw runtime_error("SecPublicInfoMemory::getAllKeyNames not implemented");
184}
185
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800186void
187SecPublicInfoMemory::getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name> &nameList, bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800188{
189 throw runtime_error("SecPublicInfoMemory::getAllKeyNamesOfIdentity not implemented");
190}
191
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800192void
193SecPublicInfoMemory::getAllCertificateNames(std::vector<Name> &nameList, bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800194{
195 throw runtime_error("SecPublicInfoMemory::getAllCertificateNames not implemented");
196}
197
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800198void
199SecPublicInfoMemory::getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name> &nameList, bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800200{
201 throw runtime_error("SecPublicInfoMemory::getAllCertificateNamesOfKey not implemented");
202}
203
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800204void
205SecPublicInfoMemory::deleteCertificateInfo(const Name &certName)
206{
207 throw runtime_error("SecPublicInfoMemory::deleteCertificateInfo not implemented");
208}
209
210void
211SecPublicInfoMemory::deletePublicKeyInfo(const Name &keyName)
212{
213 throw runtime_error("SecPublicInfoMemory::deletePublicKeyInfo not implemented");
214}
215
216void
217SecPublicInfoMemory::deleteIdentityInfo(const Name &identityName)
218{
219 throw runtime_error("SecPublicInfoMemory::deleteIdentityInfo not implemented");
220}
221
222
Yingdi Yu87581582014-01-14 14:28:39 -0800223
224}