blob: 81881a8d95e9714560f9f729ad81777205a14105 [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 Afanasyev6be1a6a2014-01-06 00:08:14 -08008#include <ndn-cpp/security/identity/memory-identity-storage.hpp>
Alexander Afanasyevb695b952013-12-27 13:19:56 -08009
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{
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800141 return defaultKeyName_;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700142}
143
144Name
145MemoryIdentityStorage::getDefaultCertificateNameForKey(const Name& keyName)
146{
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800147 return defaultCert_;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700148}
149
150void
151MemoryIdentityStorage::setDefaultIdentity(const Name& identityName)
152{
Jeff Thompson81842272013-09-25 16:12:33 -0700153 string identityUri = identityName.toUri();
154 if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end())
155 defaultIdentity_ = identityUri;
156 else
157 // The identity doesn't exist, so clear the default.
158 defaultIdentity_.clear();
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700159}
160
161void
Jeff Thompsonabcea7d2013-10-02 15:03:21 -0700162MemoryIdentityStorage::setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700163{
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800164 defaultKeyName_ = identityNameCheck;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700165}
166
167void
168MemoryIdentityStorage::setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName)
169{
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800170 defaultCert_ = certificateName;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700171}
172
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800173
174std::vector<Name>
175MemoryIdentityStorage::getAllIdentities(bool isDefault)
176{
177 throw runtime_error("MemoryIdentityStorage::getAllIdentities not implemented");
178}
179
180std::vector<Name>
181MemoryIdentityStorage::getAllKeyNames(bool isDefault)
182{
183 throw runtime_error("MemoryIdentityStorage::getAllKeyNames not implemented");
184}
185
186std::vector<Name>
187MemoryIdentityStorage::getAllKeyNamesOfIdentity(const Name& identity, bool isDefault)
188{
189 throw runtime_error("MemoryIdentityStorage::getAllKeyNamesOfIdentity not implemented");
190}
191
192std::vector<Name>
193MemoryIdentityStorage::getAllCertificateNames(bool isDefault)
194{
195 throw runtime_error("MemoryIdentityStorage::getAllCertificateNames not implemented");
196}
197
198std::vector<Name>
199MemoryIdentityStorage::getAllCertificateNamesOfKey(const Name& keyName, bool isDefault)
200{
201 throw runtime_error("MemoryIdentityStorage::getAllCertificateNamesOfKey not implemented");
202}
203
204
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700205}