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 | { |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 141 | return defaultKeyName_; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | Name |
| 145 | MemoryIdentityStorage::getDefaultCertificateNameForKey(const Name& keyName) |
| 146 | { |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 147 | return defaultCert_; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void |
| 151 | MemoryIdentityStorage::setDefaultIdentity(const Name& identityName) |
| 152 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 153 | string identityUri = identityName.toUri(); |
| 154 | if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end()) |
| 155 | defaultIdentity_ = identityUri; |
| 156 | else |
| 157 | // The identity doesn't exist, so clear the default. |
| 158 | defaultIdentity_.clear(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 162 | MemoryIdentityStorage::setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 163 | { |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 164 | defaultKeyName_ = identityNameCheck; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void |
| 168 | MemoryIdentityStorage::setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName) |
| 169 | { |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 170 | defaultCert_ = certificateName; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 173 | |
| 174 | std::vector<Name> |
| 175 | MemoryIdentityStorage::getAllIdentities(bool isDefault) |
| 176 | { |
| 177 | throw runtime_error("MemoryIdentityStorage::getAllIdentities not implemented"); |
| 178 | } |
| 179 | |
| 180 | std::vector<Name> |
| 181 | MemoryIdentityStorage::getAllKeyNames(bool isDefault) |
| 182 | { |
| 183 | throw runtime_error("MemoryIdentityStorage::getAllKeyNames not implemented"); |
| 184 | } |
| 185 | |
| 186 | std::vector<Name> |
| 187 | MemoryIdentityStorage::getAllKeyNamesOfIdentity(const Name& identity, bool isDefault) |
| 188 | { |
| 189 | throw runtime_error("MemoryIdentityStorage::getAllKeyNamesOfIdentity not implemented"); |
| 190 | } |
| 191 | |
| 192 | std::vector<Name> |
| 193 | MemoryIdentityStorage::getAllCertificateNames(bool isDefault) |
| 194 | { |
| 195 | throw runtime_error("MemoryIdentityStorage::getAllCertificateNames not implemented"); |
| 196 | } |
| 197 | |
| 198 | std::vector<Name> |
| 199 | MemoryIdentityStorage::getAllCertificateNamesOfKey(const Name& keyName, bool isDefault) |
| 200 | { |
| 201 | throw runtime_error("MemoryIdentityStorage::getAllCertificateNamesOfKey not implemented"); |
| 202 | } |
| 203 | |
| 204 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 205 | } |