blob: c9efc2fe1cef2509e0de34b0eea36d86a230259d [file] [log] [blame]
Yingdi Yu87581582014-01-14 14:28:39 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Yingdi Yu87581582014-01-14 14:28:39 -080013 */
14
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080015#include "common.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080016
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080017#include "sec-public-info-memory.hpp"
18#include "identity-certificate.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080019
20using namespace std;
21
22namespace ndn {
23
24SecPublicInfoMemory::~SecPublicInfoMemory()
25{
26}
27
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070028bool
Yingdi Yu87581582014-01-14 14:28:39 -080029SecPublicInfoMemory::doesIdentityExist(const Name& identityName)
30{
31 string identityUri = identityName.toUri();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070032 return find(m_identityStore.begin(), m_identityStore.end(), identityUri) != m_identityStore.end();
Yingdi Yu87581582014-01-14 14:28:39 -080033}
34
35void
36SecPublicInfoMemory::addIdentity(const Name& identityName)
37{
38 string identityUri = identityName.toUri();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070039 if (find(m_identityStore.begin(), m_identityStore.end(), identityUri) != m_identityStore.end())
Yingdi Yu2e57a582014-02-20 23:34:43 -080040 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070042 m_identityStore.push_back(identityUri);
Yingdi Yu87581582014-01-14 14:28:39 -080043}
44
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045bool
Yingdi Yu87581582014-01-14 14:28:39 -080046SecPublicInfoMemory::revokeIdentity()
47{
Yingdi Yu2e57a582014-02-20 23:34:43 -080048 throw Error("SecPublicInfoMemory::revokeIdentity not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -080049}
50
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070051bool
Yingdi Yu87581582014-01-14 14:28:39 -080052SecPublicInfoMemory::doesPublicKeyExist(const Name& keyName)
53{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070054 return m_keyStore.find(keyName.toUri()) != m_keyStore.end();
Yingdi Yu87581582014-01-14 14:28:39 -080055}
56
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070057void
Yingdi Yu87581582014-01-14 14:28:39 -080058SecPublicInfoMemory::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKey)
59{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070060 Name identityName = keyName.getPrefix(-1);
Yingdi Yu87581582014-01-14 14:28:39 -080061
Yingdi Yu2e57a582014-02-20 23:34:43 -080062 addIdentity(identityName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070063
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070064 m_keyStore[keyName.toUri()] = make_shared<KeyRecord>(keyType, cref(publicKey));
Yingdi Yu87581582014-01-14 14:28:39 -080065}
66
Yingdi Yu2e57a582014-02-20 23:34:43 -080067shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080068SecPublicInfoMemory::getPublicKey(const Name& keyName)
69{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070070 KeyStore::iterator record = m_keyStore.find(keyName.toUri());
71 if (record == m_keyStore.end())
Yingdi Yu2e57a582014-02-20 23:34:43 -080072 throw Error("SecPublicInfoMemory::getPublicKey " + keyName.toUri());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070073
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070074 return make_shared<PublicKey>(record->second->getKey());
Yingdi Yu87581582014-01-14 14:28:39 -080075}
76
77bool
78SecPublicInfoMemory::doesCertificateExist(const Name& certificateName)
79{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070080 return m_certificateStore.find(certificateName.toUri()) != m_certificateStore.end();
Yingdi Yu87581582014-01-14 14:28:39 -080081}
82
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083void
Yingdi Yu87581582014-01-14 14:28:39 -080084SecPublicInfoMemory::addCertificate(const IdentityCertificate& certificate)
85{
86 const Name& certificateName = certificate.getName();
87 const Name& keyName = certificate.getPublicKeyName();
Yingdi Yu2e57a582014-02-20 23:34:43 -080088 const Name& identity = keyName.getPrefix(-1);
Yingdi Yu87581582014-01-14 14:28:39 -080089
Yingdi Yu2e57a582014-02-20 23:34:43 -080090 addIdentity(identity);
91 addPublicKey(keyName, KEY_TYPE_RSA, certificate.getPublicKeyInfo());
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070092 m_certificateStore[certificateName.toUri()] = make_shared<IdentityCertificate>(certificate);
Yingdi Yu87581582014-01-14 14:28:39 -080093}
94
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -080096SecPublicInfoMemory::getCertificate(const Name& certificateName)
Yingdi Yu87581582014-01-14 14:28:39 -080097{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070098 CertificateStore::iterator record = m_certificateStore.find(certificateName.toUri());
99 if (record == m_certificateStore.end())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800100 throw Error("SecPublicInfoMemory::getCertificate " + certificateName.toUri());
Yingdi Yu87581582014-01-14 14:28:39 -0800101
102 return record->second;
103}
104
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700105Name
Yingdi Yu87581582014-01-14 14:28:39 -0800106SecPublicInfoMemory::getDefaultIdentity()
107{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700108 return Name(m_defaultIdentity);
Yingdi Yu87581582014-01-14 14:28:39 -0800109}
110
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700111void
Yingdi Yu87581582014-01-14 14:28:39 -0800112SecPublicInfoMemory::setDefaultIdentityInternal(const Name& identityName)
113{
114 string identityUri = identityName.toUri();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700115 if (find(m_identityStore.begin(), m_identityStore.end(), identityUri) != m_identityStore.end())
116 m_defaultIdentity = identityUri;
Yingdi Yu87581582014-01-14 14:28:39 -0800117 else
118 // The identity doesn't exist, so clear the default.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700119 m_defaultIdentity.clear();
Yingdi Yu87581582014-01-14 14:28:39 -0800120}
121
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122Name
Yingdi Yu87581582014-01-14 14:28:39 -0800123SecPublicInfoMemory::getDefaultKeyNameForIdentity(const Name& identityName)
124{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700125 return m_defaultKeyName;
Yingdi Yu87581582014-01-14 14:28:39 -0800126}
127
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700128void
Yingdi Yu87581582014-01-14 14:28:39 -0800129SecPublicInfoMemory::setDefaultKeyNameForIdentityInternal(const Name& keyName)
130{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700131 m_defaultKeyName = keyName;
Yingdi Yu87581582014-01-14 14:28:39 -0800132}
133
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134Name
Yingdi Yu87581582014-01-14 14:28:39 -0800135SecPublicInfoMemory::getDefaultCertificateNameForKey(const Name& keyName)
136{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700137 return m_defaultCert;
Yingdi Yu87581582014-01-14 14:28:39 -0800138}
139
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700140void
141SecPublicInfoMemory::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
Yingdi Yu87581582014-01-14 14:28:39 -0800142{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700143 m_defaultCert = certificateName;
Yingdi Yu87581582014-01-14 14:28:39 -0800144}
145
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800146void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700147SecPublicInfoMemory::getAllIdentities(std::vector<Name>& nameList, bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800148{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800149 throw Error("SecPublicInfoMemory::getAllIdentities not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -0800150}
151
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800152void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700153SecPublicInfoMemory::getAllKeyNames(std::vector<Name>& nameList, bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800154{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800155 throw Error("SecPublicInfoMemory::getAllKeyNames not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -0800156}
157
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800158void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700159SecPublicInfoMemory::getAllKeyNamesOfIdentity(const Name& identity,
160 std::vector<Name>& nameList,
161 bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800162{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800163 throw Error("SecPublicInfoMemory::getAllKeyNamesOfIdentity not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -0800164}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700165
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800166void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700167SecPublicInfoMemory::getAllCertificateNames(std::vector<Name>& nameList, bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800168{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800169 throw Error("SecPublicInfoMemory::getAllCertificateNames not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -0800170}
171
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800172void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700173SecPublicInfoMemory::getAllCertificateNamesOfKey(const Name& keyName,
174 std::vector<Name>& nameList,
175 bool isDefault)
Yingdi Yu87581582014-01-14 14:28:39 -0800176{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800177 throw Error("SecPublicInfoMemory::getAllCertificateNamesOfKey not implemented");
Yingdi Yu87581582014-01-14 14:28:39 -0800178}
179
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800180void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700181SecPublicInfoMemory::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800182{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800183 throw Error("SecPublicInfoMemory::deleteCertificateInfo not implemented");
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800184}
185
186void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700187SecPublicInfoMemory::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800188{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800189 throw Error("SecPublicInfoMemory::deletePublicKeyInfo not implemented");
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800190}
191
192void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700193SecPublicInfoMemory::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800194{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800195 throw Error("SecPublicInfoMemory::deleteIdentityInfo not implemented");
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800196}
197
Yingdi Yufc40d872014-02-18 12:56:04 -0800198} // namespace ndn