Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 8 | #include "memory-identity-storage.hpp" |
| 9 | |
Jeff Thompson | 1130afc | 2013-10-01 14:45:50 -0700 | [diff] [blame] | 10 | #include <algorithm> |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 11 | #include <ndn-cpp/security/certificate/identity-certificate.hpp> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 12 | |
| 13 | using namespace std; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | MemoryIdentityStorage::~MemoryIdentityStorage() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | bool |
| 22 | MemoryIdentityStorage::doesIdentityExist(const Name& identityName) |
| 23 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 24 | string identityUri = identityName.toUri(); |
| 25 | return find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void |
| 29 | MemoryIdentityStorage::addIdentity(const Name& identityName) |
| 30 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 31 | string identityUri = identityName.toUri(); |
| 32 | if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end()) |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 33 | throw Error("Identity already exists: " + identityUri); |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 34 | |
| 35 | identityStore_.push_back(identityUri); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | bool |
| 39 | MemoryIdentityStorage::revokeIdentity() |
| 40 | { |
| 41 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 42 | throw runtime_error("MemoryIdentityStorage::revokeIdentity not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 43 | #endif |
| 44 | } |
| 45 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 46 | bool |
| 47 | MemoryIdentityStorage::doesKeyExist(const Name& keyName) |
| 48 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 49 | return keyStore_.find(keyName.toUri()) != keyStore_.end(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 52 | void |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 53 | MemoryIdentityStorage::addKey(const Name& keyName, KeyType keyType, const PublicKey& publicKey) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 54 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 55 | Name identityName = keyName.getSubName(0, keyName.size() - 1); |
| 56 | |
| 57 | if (!doesIdentityExist(identityName)) |
| 58 | addIdentity(identityName); |
| 59 | |
| 60 | if (doesKeyExist(keyName)) |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 61 | throw Error("a key with the same name already exists!"); |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 62 | |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 63 | keyStore_[keyName.toUri()] = ptr_lib::make_shared<KeyRecord>(keyType, publicKey); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 66 | ptr_lib::shared_ptr<PublicKey> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 67 | MemoryIdentityStorage::getKey(const Name& keyName) |
| 68 | { |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 69 | KeyStore::iterator record = keyStore_.find(keyName.toUri()); |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 70 | if (record == keyStore_.end()) |
| 71 | // Not found. Silently return null. |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 72 | return ptr_lib::shared_ptr<PublicKey>(); |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 73 | |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 74 | return ptr_lib::make_shared<PublicKey> (record->second->getKey()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
| 78 | MemoryIdentityStorage::activateKey(const Name& keyName) |
| 79 | { |
| 80 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 81 | throw runtime_error("MemoryIdentityStorage::activateKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 82 | #endif |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | MemoryIdentityStorage::deactivateKey(const Name& keyName) |
| 87 | { |
| 88 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 89 | throw runtime_error("MemoryIdentityStorage::deactivateKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 90 | #endif |
| 91 | } |
| 92 | |
| 93 | bool |
| 94 | MemoryIdentityStorage::doesCertificateExist(const Name& certificateName) |
| 95 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 96 | return certificateStore_.find(certificateName.toUri()) != certificateStore_.end(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 100 | MemoryIdentityStorage::addCertificate(const IdentityCertificate& certificate) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 101 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 102 | const Name& certificateName = certificate.getName(); |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 103 | const Name& keyName = certificate.getPublicKeyName(); |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 104 | |
| 105 | if (!doesKeyExist(keyName)) |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 106 | throw Error("No corresponding Key record for certificate! " + keyName.toUri() + " " + certificateName.toUri()); |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 107 | |
| 108 | // Check if certificate has already existed! |
| 109 | if (doesCertificateExist(certificateName)) |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 110 | throw Error("Certificate has already been installed!"); |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 111 | |
| 112 | // Check if the public key of certificate is the same as the key record. |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 113 | 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 Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 116 | |
| 117 | // Insert the certificate. |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 118 | certificateStore_[certificateName.toUri()] = ptr_lib::make_shared<IdentityCertificate> (certificate); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 121 | ptr_lib::shared_ptr<IdentityCertificate> |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 122 | MemoryIdentityStorage::getCertificate(const Name& certificateName, bool allowAny) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 123 | { |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 124 | CertificateStore::iterator record = certificateStore_.find(certificateName.toUri()); |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 125 | if (record == certificateStore_.end()) |
| 126 | // Not found. Silently return null. |
Alexander Afanasyev | b695b95 | 2013-12-27 13:19:56 -0800 | [diff] [blame^] | 127 | return ptr_lib::shared_ptr<IdentityCertificate>(); |
| 128 | |
| 129 | return record->second; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | Name |
| 133 | MemoryIdentityStorage::getDefaultIdentity() |
| 134 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 135 | return Name(defaultIdentity_); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | Name |
| 139 | MemoryIdentityStorage::getDefaultKeyNameForIdentity(const Name& identityName) |
| 140 | { |
| 141 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 142 | throw runtime_error("MemoryIdentityStorage::getDefaultKeyNameForIdentity not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 143 | #endif |
| 144 | } |
| 145 | |
| 146 | Name |
| 147 | MemoryIdentityStorage::getDefaultCertificateNameForKey(const Name& keyName) |
| 148 | { |
| 149 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 150 | throw runtime_error("MemoryIdentityStorage::getDefaultCertificateNameForKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 151 | #endif |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | MemoryIdentityStorage::setDefaultIdentity(const Name& identityName) |
| 156 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 157 | string identityUri = identityName.toUri(); |
| 158 | if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end()) |
| 159 | defaultIdentity_ = identityUri; |
| 160 | else |
| 161 | // The identity doesn't exist, so clear the default. |
| 162 | defaultIdentity_.clear(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 166 | MemoryIdentityStorage::setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 167 | { |
| 168 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 169 | throw runtime_error("MemoryIdentityStorage::setDefaultKeyNameForIdentity not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 170 | #endif |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | MemoryIdentityStorage::setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName) |
| 175 | { |
| 176 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 177 | throw runtime_error("MemoryIdentityStorage::setDefaultCertificateNameForKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 178 | #endif |
| 179 | } |
| 180 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 181 | |
| 182 | std::vector<Name> |
| 183 | MemoryIdentityStorage::getAllIdentities(bool isDefault) |
| 184 | { |
| 185 | throw runtime_error("MemoryIdentityStorage::getAllIdentities not implemented"); |
| 186 | } |
| 187 | |
| 188 | std::vector<Name> |
| 189 | MemoryIdentityStorage::getAllKeyNames(bool isDefault) |
| 190 | { |
| 191 | throw runtime_error("MemoryIdentityStorage::getAllKeyNames not implemented"); |
| 192 | } |
| 193 | |
| 194 | std::vector<Name> |
| 195 | MemoryIdentityStorage::getAllKeyNamesOfIdentity(const Name& identity, bool isDefault) |
| 196 | { |
| 197 | throw runtime_error("MemoryIdentityStorage::getAllKeyNamesOfIdentity not implemented"); |
| 198 | } |
| 199 | |
| 200 | std::vector<Name> |
| 201 | MemoryIdentityStorage::getAllCertificateNames(bool isDefault) |
| 202 | { |
| 203 | throw runtime_error("MemoryIdentityStorage::getAllCertificateNames not implemented"); |
| 204 | } |
| 205 | |
| 206 | std::vector<Name> |
| 207 | MemoryIdentityStorage::getAllCertificateNamesOfKey(const Name& keyName, bool isDefault) |
| 208 | { |
| 209 | throw runtime_error("MemoryIdentityStorage::getAllCertificateNamesOfKey not implemented"); |
| 210 | } |
| 211 | |
| 212 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 213 | } |