blob: 3b72c2167b89c4f45efaca4d9a80a259e4f1cd3c [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu87581582014-01-14 14:28:39 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Yingdi Yu87581582014-01-14 14:28:39 -080022 */
23
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080024#include "common.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080025
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080026#include "sec-public-info-memory.hpp"
27#include "identity-certificate.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080028
29using namespace std;
30
31namespace ndn {
32
33SecPublicInfoMemory::~SecPublicInfoMemory()
34{
35}
36
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070037bool
Yingdi Yu87581582014-01-14 14:28:39 -080038SecPublicInfoMemory::doesIdentityExist(const Name& identityName)
39{
40 string identityUri = identityName.toUri();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070041 return find(m_identityStore.begin(), m_identityStore.end(), identityUri) != m_identityStore.end();
Yingdi Yu87581582014-01-14 14:28:39 -080042}
43
44void
45SecPublicInfoMemory::addIdentity(const Name& identityName)
46{
47 string identityUri = identityName.toUri();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070048 if (find(m_identityStore.begin(), m_identityStore.end(), identityUri) != m_identityStore.end())
Yingdi Yu2e57a582014-02-20 23:34:43 -080049 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070050
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070051 m_identityStore.push_back(identityUri);
Yingdi Yu87581582014-01-14 14:28:39 -080052}
53
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054bool
Yingdi Yu87581582014-01-14 14:28:39 -080055SecPublicInfoMemory::revokeIdentity()
56{
Yingdi Yu2e57a582014-02-20 23:34:43 -080057 throw Error("SecPublicInfoMemory::revokeIdentity not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -080058}
59
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060bool
Yingdi Yu87581582014-01-14 14:28:39 -080061SecPublicInfoMemory::doesPublicKeyExist(const Name& keyName)
62{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070063 return m_keyStore.find(keyName.toUri()) != m_keyStore.end();
Yingdi Yu87581582014-01-14 14:28:39 -080064}
65
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070066void
Yingdi Yu87581582014-01-14 14:28:39 -080067SecPublicInfoMemory::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKey)
68{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070069 Name identityName = keyName.getPrefix(-1);
Yingdi Yu87581582014-01-14 14:28:39 -080070
Yingdi Yu2e57a582014-02-20 23:34:43 -080071 addIdentity(identityName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070072
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070073 m_keyStore[keyName.toUri()] = make_shared<KeyRecord>(keyType, publicKey);
Yingdi Yu87581582014-01-14 14:28:39 -080074}
75
Yingdi Yu2e57a582014-02-20 23:34:43 -080076shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080077SecPublicInfoMemory::getPublicKey(const Name& keyName)
78{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070079 KeyStore::iterator record = m_keyStore.find(keyName.toUri());
80 if (record == m_keyStore.end())
Yingdi Yu2e57a582014-02-20 23:34:43 -080081 throw Error("SecPublicInfoMemory::getPublicKey " + keyName.toUri());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070082
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070083 return make_shared<PublicKey>(record->second->getKey());
Yingdi Yu87581582014-01-14 14:28:39 -080084}
85
86bool
87SecPublicInfoMemory::doesCertificateExist(const Name& certificateName)
88{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070089 return m_certificateStore.find(certificateName.toUri()) != m_certificateStore.end();
Yingdi Yu87581582014-01-14 14:28:39 -080090}
91
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070092void
Yingdi Yu87581582014-01-14 14:28:39 -080093SecPublicInfoMemory::addCertificate(const IdentityCertificate& certificate)
94{
95 const Name& certificateName = certificate.getName();
96 const Name& keyName = certificate.getPublicKeyName();
Yingdi Yu2e57a582014-02-20 23:34:43 -080097 const Name& identity = keyName.getPrefix(-1);
Yingdi Yu87581582014-01-14 14:28:39 -080098
Yingdi Yu2e57a582014-02-20 23:34:43 -080099 addIdentity(identity);
100 addPublicKey(keyName, KEY_TYPE_RSA, certificate.getPublicKeyInfo());
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700101 m_certificateStore[certificateName.toUri()] = make_shared<IdentityCertificate>(certificate);
Yingdi Yu87581582014-01-14 14:28:39 -0800102}
103
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -0800105SecPublicInfoMemory::getCertificate(const Name& certificateName)
Yingdi Yu87581582014-01-14 14:28:39 -0800106{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700107 CertificateStore::iterator record = m_certificateStore.find(certificateName.toUri());
108 if (record == m_certificateStore.end())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800109 throw Error("SecPublicInfoMemory::getCertificate " + certificateName.toUri());
Yingdi Yu87581582014-01-14 14:28:39 -0800110
111 return record->second;
112}
113
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700114Name
Yingdi Yu87581582014-01-14 14:28:39 -0800115SecPublicInfoMemory::getDefaultIdentity()
116{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700117 return Name(m_defaultIdentity);
Yingdi Yu87581582014-01-14 14:28:39 -0800118}
119
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700120void
Yingdi Yu87581582014-01-14 14:28:39 -0800121SecPublicInfoMemory::setDefaultIdentityInternal(const Name& identityName)
122{
123 string identityUri = identityName.toUri();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700124 if (find(m_identityStore.begin(), m_identityStore.end(), identityUri) != m_identityStore.end())
125 m_defaultIdentity = identityUri;
Yingdi Yu87581582014-01-14 14:28:39 -0800126 else
127 // The identity doesn't exist, so clear the default.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700128 m_defaultIdentity.clear();
Yingdi Yu87581582014-01-14 14:28:39 -0800129}
130
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700131Name
Yingdi Yu87581582014-01-14 14:28:39 -0800132SecPublicInfoMemory::getDefaultKeyNameForIdentity(const Name& identityName)
133{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700134 return m_defaultKeyName;
Yingdi Yu87581582014-01-14 14:28:39 -0800135}
136
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700137void
Yingdi Yu87581582014-01-14 14:28:39 -0800138SecPublicInfoMemory::setDefaultKeyNameForIdentityInternal(const Name& keyName)
139{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700140 m_defaultKeyName = keyName;
Yingdi Yu87581582014-01-14 14:28:39 -0800141}
142
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700143Name
Yingdi Yu87581582014-01-14 14:28:39 -0800144SecPublicInfoMemory::getDefaultCertificateNameForKey(const Name& keyName)
145{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700146 return m_defaultCert;
Yingdi Yu87581582014-01-14 14:28:39 -0800147}
148
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700149void
150SecPublicInfoMemory::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
Yingdi Yu87581582014-01-14 14:28:39 -0800151{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700152 m_defaultCert = certificateName;
Yingdi Yu87581582014-01-14 14:28:39 -0800153}
154
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800155void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700156SecPublicInfoMemory::getAllIdentities(std::vector<Name>& nameList, bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800157{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800158 throw Error("SecPublicInfoMemory::getAllIdentities not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -0800159}
160
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800161void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700162SecPublicInfoMemory::getAllKeyNames(std::vector<Name>& nameList, bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800163{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800164 throw Error("SecPublicInfoMemory::getAllKeyNames not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -0800165}
166
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800167void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700168SecPublicInfoMemory::getAllKeyNamesOfIdentity(const Name& identity,
169 std::vector<Name>& nameList,
170 bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800171{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800172 throw Error("SecPublicInfoMemory::getAllKeyNamesOfIdentity not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -0800173}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700174
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800175void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700176SecPublicInfoMemory::getAllCertificateNames(std::vector<Name>& nameList, bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800177{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800178 throw Error("SecPublicInfoMemory::getAllCertificateNames not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -0800179}
180
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800181void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700182SecPublicInfoMemory::getAllCertificateNamesOfKey(const Name& keyName,
183 std::vector<Name>& nameList,
184 bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800185{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800186 throw Error("SecPublicInfoMemory::getAllCertificateNamesOfKey not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -0800187}
188
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800189void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700190SecPublicInfoMemory::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800191{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800192 throw Error("SecPublicInfoMemory::deleteCertificateInfo not implemented");
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800193}
194
195void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700196SecPublicInfoMemory::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800197{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800198 throw Error("SecPublicInfoMemory::deletePublicKeyInfo not implemented");
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800199}
200
201void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700202SecPublicInfoMemory::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800203{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800204 throw Error("SecPublicInfoMemory::deleteIdentityInfo not implemented");
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800205}
206
Yingdi Yufc40d872014-02-18 12:56:04 -0800207} // namespace ndn