blob: fae1f14eeb00e2722adc6c789ff7361f7efbdc6f [file] [log] [blame]
Jeff Thompson6c314bc2013-09-23 18:09:38 -07001/* -*- 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 Afanasyevb695b952013-12-27 13:19:56 -08008#include "memory-identity-storage.hpp"
9
Jeff Thompson1130afc2013-10-01 14:45:50 -070010#include <algorithm>
Jeff Thompson61805e92013-10-23 15:19:39 -070011#include <ndn-cpp/security/certificate/identity-certificate.hpp>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070012
13using namespace std;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070014
15namespace ndn {
16
17MemoryIdentityStorage::~MemoryIdentityStorage()
18{
19}
20
21bool
22MemoryIdentityStorage::doesIdentityExist(const Name& identityName)
23{
Jeff Thompson81842272013-09-25 16:12:33 -070024 string identityUri = identityName.toUri();
25 return find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070026}
27
28void
29MemoryIdentityStorage::addIdentity(const Name& identityName)
30{
Jeff Thompson81842272013-09-25 16:12:33 -070031 string identityUri = identityName.toUri();
32 if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end())
Alexander Afanasyevb695b952013-12-27 13:19:56 -080033 throw Error("Identity already exists: " + identityUri);
Jeff Thompson81842272013-09-25 16:12:33 -070034
35 identityStore_.push_back(identityUri);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070036}
37
38bool
39MemoryIdentityStorage::revokeIdentity()
40{
41#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070042 throw runtime_error("MemoryIdentityStorage::revokeIdentity not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070043#endif
44}
45
Jeff Thompson6c314bc2013-09-23 18:09:38 -070046bool
47MemoryIdentityStorage::doesKeyExist(const Name& keyName)
48{
Jeff Thompson61805e92013-10-23 15:19:39 -070049 return keyStore_.find(keyName.toUri()) != keyStore_.end();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070050}
51
Jeff Thompson6c314bc2013-09-23 18:09:38 -070052void
Alexander Afanasyevb695b952013-12-27 13:19:56 -080053MemoryIdentityStorage::addKey(const Name& keyName, KeyType keyType, const PublicKey& publicKey)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070054{
Jeff Thompson61805e92013-10-23 15:19:39 -070055 Name identityName = keyName.getSubName(0, keyName.size() - 1);
56
57 if (!doesIdentityExist(identityName))
58 addIdentity(identityName);
59
60 if (doesKeyExist(keyName))
Alexander Afanasyevb695b952013-12-27 13:19:56 -080061 throw Error("a key with the same name already exists!");
Jeff Thompson61805e92013-10-23 15:19:39 -070062
Alexander Afanasyevb695b952013-12-27 13:19:56 -080063 keyStore_[keyName.toUri()] = ptr_lib::make_shared<KeyRecord>(keyType, publicKey);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070064}
65
Alexander Afanasyevb695b952013-12-27 13:19:56 -080066ptr_lib::shared_ptr<PublicKey>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070067MemoryIdentityStorage::getKey(const Name& keyName)
68{
Alexander Afanasyevb695b952013-12-27 13:19:56 -080069 KeyStore::iterator record = keyStore_.find(keyName.toUri());
Jeff Thompson61805e92013-10-23 15:19:39 -070070 if (record == keyStore_.end())
71 // Not found. Silently return null.
Alexander Afanasyevb695b952013-12-27 13:19:56 -080072 return ptr_lib::shared_ptr<PublicKey>();
Jeff Thompson61805e92013-10-23 15:19:39 -070073
Alexander Afanasyevb695b952013-12-27 13:19:56 -080074 return ptr_lib::make_shared<PublicKey> (record->second->getKey());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070075}
76
77void
78MemoryIdentityStorage::activateKey(const Name& keyName)
79{
80#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070081 throw runtime_error("MemoryIdentityStorage::activateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070082#endif
83}
84
85void
86MemoryIdentityStorage::deactivateKey(const Name& keyName)
87{
88#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070089 throw runtime_error("MemoryIdentityStorage::deactivateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070090#endif
91}
92
93bool
94MemoryIdentityStorage::doesCertificateExist(const Name& certificateName)
95{
Jeff Thompson61805e92013-10-23 15:19:39 -070096 return certificateStore_.find(certificateName.toUri()) != certificateStore_.end();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070097}
98
99void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700100MemoryIdentityStorage::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700101{
Jeff Thompson61805e92013-10-23 15:19:39 -0700102 const Name& certificateName = certificate.getName();
Alexander Afanasyevb695b952013-12-27 13:19:56 -0800103 const Name& keyName = certificate.getPublicKeyName();
Jeff Thompson61805e92013-10-23 15:19:39 -0700104
105 if (!doesKeyExist(keyName))
Alexander Afanasyevb695b952013-12-27 13:19:56 -0800106 throw Error("No corresponding Key record for certificate! " + keyName.toUri() + " " + certificateName.toUri());
Jeff Thompson61805e92013-10-23 15:19:39 -0700107
108 // Check if certificate has already existed!
109 if (doesCertificateExist(certificateName))
Alexander Afanasyevb695b952013-12-27 13:19:56 -0800110 throw Error("Certificate has already been installed!");
Jeff Thompson61805e92013-10-23 15:19:39 -0700111
112 // Check if the public key of certificate is the same as the key record.
Alexander Afanasyevb695b952013-12-27 13:19:56 -0800113 ptr_lib::shared_ptr<PublicKey> pubKey = getKey(keyName);
114 if (!pubKey || (*pubKey) != certificate.getPublicKeyInfo())
115 throw Error("Certificate does not match the public key!");
Jeff Thompson61805e92013-10-23 15:19:39 -0700116
117 // Insert the certificate.
Alexander Afanasyevb695b952013-12-27 13:19:56 -0800118 certificateStore_[certificateName.toUri()] = ptr_lib::make_shared<IdentityCertificate> (certificate);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700119}
120
Alexander Afanasyevb695b952013-12-27 13:19:56 -0800121ptr_lib::shared_ptr<IdentityCertificate>
Jeff Thompson61805e92013-10-23 15:19:39 -0700122MemoryIdentityStorage::getCertificate(const Name& certificateName, bool allowAny)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700123{
Alexander Afanasyevb695b952013-12-27 13:19:56 -0800124 CertificateStore::iterator record = certificateStore_.find(certificateName.toUri());
Jeff Thompson61805e92013-10-23 15:19:39 -0700125 if (record == certificateStore_.end())
126 // Not found. Silently return null.
Alexander Afanasyevb695b952013-12-27 13:19:56 -0800127 return ptr_lib::shared_ptr<IdentityCertificate>();
128
129 return record->second;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700130}
131
132Name
133MemoryIdentityStorage::getDefaultIdentity()
134{
Jeff Thompson81842272013-09-25 16:12:33 -0700135 return Name(defaultIdentity_);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700136}
137
138Name
139MemoryIdentityStorage::getDefaultKeyNameForIdentity(const Name& identityName)
140{
141#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700142 throw runtime_error("MemoryIdentityStorage::getDefaultKeyNameForIdentity not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700143#endif
144}
145
146Name
147MemoryIdentityStorage::getDefaultCertificateNameForKey(const Name& keyName)
148{
149#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700150 throw runtime_error("MemoryIdentityStorage::getDefaultCertificateNameForKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700151#endif
152}
153
154void
155MemoryIdentityStorage::setDefaultIdentity(const Name& identityName)
156{
Jeff Thompson81842272013-09-25 16:12:33 -0700157 string identityUri = identityName.toUri();
158 if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end())
159 defaultIdentity_ = identityUri;
160 else
161 // The identity doesn't exist, so clear the default.
162 defaultIdentity_.clear();
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700163}
164
165void
Jeff Thompsonabcea7d2013-10-02 15:03:21 -0700166MemoryIdentityStorage::setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700167{
168#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700169 throw runtime_error("MemoryIdentityStorage::setDefaultKeyNameForIdentity not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700170#endif
171}
172
173void
174MemoryIdentityStorage::setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName)
175{
176#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700177 throw runtime_error("MemoryIdentityStorage::setDefaultCertificateNameForKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700178#endif
179}
180
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800181
182std::vector<Name>
183MemoryIdentityStorage::getAllIdentities(bool isDefault)
184{
185 throw runtime_error("MemoryIdentityStorage::getAllIdentities not implemented");
186}
187
188std::vector<Name>
189MemoryIdentityStorage::getAllKeyNames(bool isDefault)
190{
191 throw runtime_error("MemoryIdentityStorage::getAllKeyNames not implemented");
192}
193
194std::vector<Name>
195MemoryIdentityStorage::getAllKeyNamesOfIdentity(const Name& identity, bool isDefault)
196{
197 throw runtime_error("MemoryIdentityStorage::getAllKeyNamesOfIdentity not implemented");
198}
199
200std::vector<Name>
201MemoryIdentityStorage::getAllCertificateNames(bool isDefault)
202{
203 throw runtime_error("MemoryIdentityStorage::getAllCertificateNames not implemented");
204}
205
206std::vector<Name>
207MemoryIdentityStorage::getAllCertificateNamesOfKey(const Name& keyName, bool isDefault)
208{
209 throw runtime_error("MemoryIdentityStorage::getAllCertificateNamesOfKey not implemented");
210}
211
212
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700213}