blob: c5fb40998e154620adb1a63beac64a9150cd6edf [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;
17using namespace ndn::ptr_lib;
18
19namespace ndn {
20
21MemoryIdentityStorage::~MemoryIdentityStorage()
22{
23}
24
25bool
26MemoryIdentityStorage::doesIdentityExist(const Name& identityName)
27{
Jeff Thompson81842272013-09-25 16:12:33 -070028 string identityUri = identityName.toUri();
29 return find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070030}
31
32void
33MemoryIdentityStorage::addIdentity(const Name& identityName)
34{
Jeff Thompson81842272013-09-25 16:12:33 -070035 string identityUri = identityName.toUri();
36 if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end())
37 throw SecurityException("Identity already exists: " + identityUri);
38
39 identityStore_.push_back(identityUri);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070040}
41
42bool
43MemoryIdentityStorage::revokeIdentity()
44{
45#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070046 throw runtime_error("MemoryIdentityStorage::revokeIdentity not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070047#endif
48}
49
Jeff Thompson6c314bc2013-09-23 18:09:38 -070050bool
51MemoryIdentityStorage::doesKeyExist(const Name& keyName)
52{
Jeff Thompson61805e92013-10-23 15:19:39 -070053 return keyStore_.find(keyName.toUri()) != keyStore_.end();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070054}
55
Jeff Thompson6c314bc2013-09-23 18:09:38 -070056void
Jeff Thompsonbd04b072013-09-27 15:14:09 -070057MemoryIdentityStorage::addKey(const Name& keyName, KeyType keyType, const Blob& publicKeyDer)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070058{
Jeff Thompson61805e92013-10-23 15:19:39 -070059 Name identityName = keyName.getSubName(0, keyName.size() - 1);
60
61 if (!doesIdentityExist(identityName))
62 addIdentity(identityName);
63
64 if (doesKeyExist(keyName))
65 throw SecurityException("a key with the same name already exists!");
66
67 keyStore_[keyName.toUri()] = make_shared<KeyRecord>(keyType, publicKeyDer);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070068}
69
70Blob
71MemoryIdentityStorage::getKey(const Name& keyName)
72{
Jeff Thompson61805e92013-10-23 15:19:39 -070073 map<string, shared_ptr<KeyRecord> >::iterator record = keyStore_.find(keyName.toUri());
74 if (record == keyStore_.end())
75 // Not found. Silently return null.
76 return Blob();
77
78 return record->second->getKeyDer();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070079}
80
81void
82MemoryIdentityStorage::activateKey(const Name& keyName)
83{
84#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070085 throw runtime_error("MemoryIdentityStorage::activateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070086#endif
87}
88
89void
90MemoryIdentityStorage::deactivateKey(const Name& keyName)
91{
92#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070093 throw runtime_error("MemoryIdentityStorage::deactivateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070094#endif
95}
96
97bool
98MemoryIdentityStorage::doesCertificateExist(const Name& certificateName)
99{
Jeff Thompson61805e92013-10-23 15:19:39 -0700100 return certificateStore_.find(certificateName.toUri()) != certificateStore_.end();
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700101}
102
103void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700104MemoryIdentityStorage::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700105{
Jeff Thompson61805e92013-10-23 15:19:39 -0700106 const Name& certificateName = certificate.getName();
107 Name keyName = certificate.getPublicKeyName();
108
109 if (!doesKeyExist(keyName))
110 throw SecurityException("No corresponding Key record for certificate! " + keyName.toUri() + " " + certificateName.toUri());
111
112 // Check if certificate has already existed!
113 if (doesCertificateExist(certificateName))
114 throw SecurityException("Certificate has already been installed!");
115
116 // Check if the public key of certificate is the same as the key record.
117 Blob keyBlob = getKey(keyName);
118 if (!keyBlob || (*keyBlob) != *(certificate.getPublicKeyInfo().getKeyDer()))
119 throw SecurityException("Certificate does not match the public key!");
120
121 // Insert the certificate.
122 if (!certificate.getDefaultWireEncoding())
123 certificate.wireEncode();
124 certificateStore_[certificateName.toUri()] = certificate.getDefaultWireEncoding();
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700125}
126
Jeff Thompson3bd90bc2013-10-19 16:40:14 -0700127ptr_lib::shared_ptr<Data>
Jeff Thompson61805e92013-10-23 15:19:39 -0700128MemoryIdentityStorage::getCertificate(const Name& certificateName, bool allowAny)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700129{
Jeff Thompson61805e92013-10-23 15:19:39 -0700130 map<string, Blob>::iterator record = certificateStore_.find(certificateName.toUri());
131 if (record == certificateStore_.end())
132 // Not found. Silently return null.
133 return shared_ptr<Data>();
134
135 shared_ptr<Data> data(new Data());
136 data->wireDecode(*record->second);
137 return data;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700138}
139
140Name
141MemoryIdentityStorage::getDefaultIdentity()
142{
Jeff Thompson81842272013-09-25 16:12:33 -0700143 return Name(defaultIdentity_);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700144}
145
146Name
147MemoryIdentityStorage::getDefaultKeyNameForIdentity(const Name& identityName)
148{
149#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700150 throw runtime_error("MemoryIdentityStorage::getDefaultKeyNameForIdentity not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700151#endif
152}
153
154Name
155MemoryIdentityStorage::getDefaultCertificateNameForKey(const Name& keyName)
156{
157#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700158 throw runtime_error("MemoryIdentityStorage::getDefaultCertificateNameForKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700159#endif
160}
161
162void
163MemoryIdentityStorage::setDefaultIdentity(const Name& identityName)
164{
Jeff Thompson81842272013-09-25 16:12:33 -0700165 string identityUri = identityName.toUri();
166 if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end())
167 defaultIdentity_ = identityUri;
168 else
169 // The identity doesn't exist, so clear the default.
170 defaultIdentity_.clear();
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700171}
172
173void
Jeff Thompsonabcea7d2013-10-02 15:03:21 -0700174MemoryIdentityStorage::setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700175{
176#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700177 throw runtime_error("MemoryIdentityStorage::setDefaultKeyNameForIdentity not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700178#endif
179}
180
181void
182MemoryIdentityStorage::setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName)
183{
184#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -0700185 throw runtime_error("MemoryIdentityStorage::setDefaultCertificateNameForKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700186#endif
187}
188
189}