blob: ee986f051a56af6636a57f780793186eaef34498 [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
Jeff Thompson1130afc2013-10-01 14:45:50 -07008#if 1
Jeff Thompson6c314bc2013-09-23 18:09:38 -07009#include <stdexcept>
Jeff Thompson1130afc2013-10-01 14:45:50 -070010#endif
11#include <algorithm>
Jeff Thompson25b4e612013-10-10 16:03:24 -070012#include <ndn-cpp/security/security-exception.hpp>
Jeff Thompson61805e92013-10-23 15:19:39 -070013#include <ndn-cpp/security/certificate/identity-certificate.hpp>
Jeff Thompson25b4e612013-10-10 16:03:24 -070014#include <ndn-cpp/security/identity/memory-identity-storage.hpp>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070015
16using namespace std;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070017
18namespace ndn {
19
20MemoryIdentityStorage::~MemoryIdentityStorage()
21{
22}
23
24bool
25MemoryIdentityStorage::doesIdentityExist(const Name& identityName)
26{
Jeff Thompson81842272013-09-25 16:12:33 -070027 string identityUri = identityName.toUri();
28 return find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070029}
30
31void
32MemoryIdentityStorage::addIdentity(const Name& identityName)
33{
Jeff Thompson81842272013-09-25 16:12:33 -070034 string identityUri = identityName.toUri();
35 if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end())
36 throw SecurityException("Identity already exists: " + identityUri);
37
38 identityStore_.push_back(identityUri);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070039}
40
41bool
42MemoryIdentityStorage::revokeIdentity()
43{
44#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070045 throw runtime_error("MemoryIdentityStorage::revokeIdentity not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070046#endif
47}
48
Jeff Thompson6c314bc2013-09-23 18:09:38 -070049bool
50MemoryIdentityStorage::doesKeyExist(const Name& keyName)
51{
Jeff Thompson61805e92013-10-23 15:19:39 -070052 return keyStore_.find(keyName.toUri()) != keyStore_.end();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070053}
54
Jeff Thompson6c314bc2013-09-23 18:09:38 -070055void
Jeff Thompsonbd04b072013-09-27 15:14:09 -070056MemoryIdentityStorage::addKey(const Name& keyName, KeyType keyType, const Blob& publicKeyDer)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070057{
Jeff Thompson61805e92013-10-23 15:19:39 -070058 Name identityName = keyName.getSubName(0, keyName.size() - 1);
59
60 if (!doesIdentityExist(identityName))
61 addIdentity(identityName);
62
63 if (doesKeyExist(keyName))
64 throw SecurityException("a key with the same name already exists!");
65
Jeff Thompsonce115762013-12-18 14:59:56 -080066 keyStore_[keyName.toUri()] = ptr_lib::make_shared<KeyRecord>(keyType, publicKeyDer);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070067}
68
69Blob
70MemoryIdentityStorage::getKey(const Name& keyName)
71{
Jeff Thompsonce115762013-12-18 14:59:56 -080072 map<string, ptr_lib::shared_ptr<KeyRecord> >::iterator record = keyStore_.find(keyName.toUri());
Jeff Thompson61805e92013-10-23 15:19:39 -070073 if (record == keyStore_.end())
74 // Not found. Silently return null.
75 return Blob();
76
77 return record->second->getKeyDer();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070078}
79
80void
81MemoryIdentityStorage::activateKey(const Name& keyName)
82{
83#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070084 throw runtime_error("MemoryIdentityStorage::activateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070085#endif
86}
87
88void
89MemoryIdentityStorage::deactivateKey(const Name& keyName)
90{
91#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070092 throw runtime_error("MemoryIdentityStorage::deactivateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070093#endif
94}
95
96bool
97MemoryIdentityStorage::doesCertificateExist(const Name& certificateName)
98{
Jeff Thompson61805e92013-10-23 15:19:39 -070099 return certificateStore_.find(certificateName.toUri()) != certificateStore_.end();
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700100}
101
102void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700103MemoryIdentityStorage::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700104{
Jeff Thompson61805e92013-10-23 15:19:39 -0700105 const Name& certificateName = certificate.getName();
106 Name keyName = certificate.getPublicKeyName();
107
108 if (!doesKeyExist(keyName))
109 throw SecurityException("No corresponding Key record for certificate! " + keyName.toUri() + " " + certificateName.toUri());
110
111 // Check if certificate has already existed!
112 if (doesCertificateExist(certificateName))
113 throw SecurityException("Certificate has already been installed!");
114
115 // Check if the public key of certificate is the same as the key record.
116 Blob keyBlob = getKey(keyName);
117 if (!keyBlob || (*keyBlob) != *(certificate.getPublicKeyInfo().getKeyDer()))
118 throw SecurityException("Certificate does not match the public key!");
119
120 // Insert the certificate.
121 if (!certificate.getDefaultWireEncoding())
122 certificate.wireEncode();
123 certificateStore_[certificateName.toUri()] = certificate.getDefaultWireEncoding();
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700124}
125
Jeff Thompson3bd90bc2013-10-19 16:40:14 -0700126ptr_lib::shared_ptr<Data>
Jeff Thompson61805e92013-10-23 15:19:39 -0700127MemoryIdentityStorage::getCertificate(const Name& certificateName, bool allowAny)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700128{
Jeff Thompson61805e92013-10-23 15:19:39 -0700129 map<string, Blob>::iterator record = certificateStore_.find(certificateName.toUri());
130 if (record == certificateStore_.end())
131 // Not found. Silently return null.
Jeff Thompsonce115762013-12-18 14:59:56 -0800132 return ptr_lib::shared_ptr<Data>();
Jeff Thompson61805e92013-10-23 15:19:39 -0700133
Jeff Thompsonce115762013-12-18 14:59:56 -0800134 ptr_lib::shared_ptr<Data> data(new Data());
Jeff Thompson61805e92013-10-23 15:19:39 -0700135 data->wireDecode(*record->second);
136 return data;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700137}
138
139Name
140MemoryIdentityStorage::getDefaultIdentity()
141{
Jeff Thompson81842272013-09-25 16:12:33 -0700142 return Name(defaultIdentity_);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700143}
144
145Name
146MemoryIdentityStorage::getDefaultKeyNameForIdentity(const Name& identityName)
147{
148#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700149 throw runtime_error("MemoryIdentityStorage::getDefaultKeyNameForIdentity not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700150#endif
151}
152
153Name
154MemoryIdentityStorage::getDefaultCertificateNameForKey(const Name& keyName)
155{
156#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700157 throw runtime_error("MemoryIdentityStorage::getDefaultCertificateNameForKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700158#endif
159}
160
161void
162MemoryIdentityStorage::setDefaultIdentity(const Name& identityName)
163{
Jeff Thompson81842272013-09-25 16:12:33 -0700164 string identityUri = identityName.toUri();
165 if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end())
166 defaultIdentity_ = identityUri;
167 else
168 // The identity doesn't exist, so clear the default.
169 defaultIdentity_.clear();
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700170}
171
172void
Jeff Thompsonabcea7d2013-10-02 15:03:21 -0700173MemoryIdentityStorage::setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700174{
175#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700176 throw runtime_error("MemoryIdentityStorage::setDefaultKeyNameForIdentity not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700177#endif
178}
179
180void
181MemoryIdentityStorage::setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName)
182{
183#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700184 throw runtime_error("MemoryIdentityStorage::setDefaultCertificateNameForKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700185#endif
186}
187
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800188
189std::vector<Name>
190MemoryIdentityStorage::getAllIdentities(bool isDefault)
191{
192 throw runtime_error("MemoryIdentityStorage::getAllIdentities not implemented");
193}
194
195std::vector<Name>
196MemoryIdentityStorage::getAllKeyNames(bool isDefault)
197{
198 throw runtime_error("MemoryIdentityStorage::getAllKeyNames not implemented");
199}
200
201std::vector<Name>
202MemoryIdentityStorage::getAllKeyNamesOfIdentity(const Name& identity, bool isDefault)
203{
204 throw runtime_error("MemoryIdentityStorage::getAllKeyNamesOfIdentity not implemented");
205}
206
207std::vector<Name>
208MemoryIdentityStorage::getAllCertificateNames(bool isDefault)
209{
210 throw runtime_error("MemoryIdentityStorage::getAllCertificateNames not implemented");
211}
212
213std::vector<Name>
214MemoryIdentityStorage::getAllCertificateNamesOfKey(const Name& keyName, bool isDefault)
215{
216 throw runtime_error("MemoryIdentityStorage::getAllCertificateNamesOfKey not implemented");
217}
218
219
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700220}