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 | |
Jeff Thompson | 1130afc | 2013-10-01 14:45:50 -0700 | [diff] [blame] | 8 | #if 1 |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 9 | #include <stdexcept> |
Jeff Thompson | 1130afc | 2013-10-01 14:45:50 -0700 | [diff] [blame] | 10 | #endif |
| 11 | #include <algorithm> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 12 | #include <ndn-cpp/security/security-exception.hpp> |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 13 | #include <ndn-cpp/security/certificate/identity-certificate.hpp> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 14 | #include <ndn-cpp/security/identity/memory-identity-storage.hpp> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 15 | |
| 16 | using namespace std; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 17 | |
| 18 | namespace ndn { |
| 19 | |
| 20 | MemoryIdentityStorage::~MemoryIdentityStorage() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | bool |
| 25 | MemoryIdentityStorage::doesIdentityExist(const Name& identityName) |
| 26 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 27 | string identityUri = identityName.toUri(); |
| 28 | return find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void |
| 32 | MemoryIdentityStorage::addIdentity(const Name& identityName) |
| 33 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 34 | 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | bool |
| 42 | MemoryIdentityStorage::revokeIdentity() |
| 43 | { |
| 44 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 45 | throw runtime_error("MemoryIdentityStorage::revokeIdentity not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 46 | #endif |
| 47 | } |
| 48 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 49 | bool |
| 50 | MemoryIdentityStorage::doesKeyExist(const Name& keyName) |
| 51 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 52 | return keyStore_.find(keyName.toUri()) != keyStore_.end(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 55 | void |
Jeff Thompson | bd04b07 | 2013-09-27 15:14:09 -0700 | [diff] [blame] | 56 | MemoryIdentityStorage::addKey(const Name& keyName, KeyType keyType, const Blob& publicKeyDer) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 57 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 58 | 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 Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 66 | keyStore_[keyName.toUri()] = ptr_lib::make_shared<KeyRecord>(keyType, publicKeyDer); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | Blob |
| 70 | MemoryIdentityStorage::getKey(const Name& keyName) |
| 71 | { |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 72 | map<string, ptr_lib::shared_ptr<KeyRecord> >::iterator record = keyStore_.find(keyName.toUri()); |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 73 | if (record == keyStore_.end()) |
| 74 | // Not found. Silently return null. |
| 75 | return Blob(); |
| 76 | |
| 77 | return record->second->getKeyDer(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
| 81 | MemoryIdentityStorage::activateKey(const Name& keyName) |
| 82 | { |
| 83 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 84 | throw runtime_error("MemoryIdentityStorage::activateKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 85 | #endif |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | MemoryIdentityStorage::deactivateKey(const Name& keyName) |
| 90 | { |
| 91 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 92 | throw runtime_error("MemoryIdentityStorage::deactivateKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 93 | #endif |
| 94 | } |
| 95 | |
| 96 | bool |
| 97 | MemoryIdentityStorage::doesCertificateExist(const Name& certificateName) |
| 98 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 99 | return certificateStore_.find(certificateName.toUri()) != certificateStore_.end(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 103 | MemoryIdentityStorage::addCertificate(const IdentityCertificate& certificate) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 104 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 105 | 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Jeff Thompson | 3bd90bc | 2013-10-19 16:40:14 -0700 | [diff] [blame] | 126 | ptr_lib::shared_ptr<Data> |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 127 | MemoryIdentityStorage::getCertificate(const Name& certificateName, bool allowAny) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 128 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 129 | map<string, Blob>::iterator record = certificateStore_.find(certificateName.toUri()); |
| 130 | if (record == certificateStore_.end()) |
| 131 | // Not found. Silently return null. |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 132 | return ptr_lib::shared_ptr<Data>(); |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 133 | |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 134 | ptr_lib::shared_ptr<Data> data(new Data()); |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 135 | data->wireDecode(*record->second); |
| 136 | return data; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | Name |
| 140 | MemoryIdentityStorage::getDefaultIdentity() |
| 141 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 142 | return Name(defaultIdentity_); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | Name |
| 146 | MemoryIdentityStorage::getDefaultKeyNameForIdentity(const Name& identityName) |
| 147 | { |
| 148 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 149 | throw runtime_error("MemoryIdentityStorage::getDefaultKeyNameForIdentity not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 150 | #endif |
| 151 | } |
| 152 | |
| 153 | Name |
| 154 | MemoryIdentityStorage::getDefaultCertificateNameForKey(const Name& keyName) |
| 155 | { |
| 156 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 157 | throw runtime_error("MemoryIdentityStorage::getDefaultCertificateNameForKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 158 | #endif |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | MemoryIdentityStorage::setDefaultIdentity(const Name& identityName) |
| 163 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 164 | 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 173 | MemoryIdentityStorage::setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 174 | { |
| 175 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 176 | throw runtime_error("MemoryIdentityStorage::setDefaultKeyNameForIdentity not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 177 | #endif |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | MemoryIdentityStorage::setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName) |
| 182 | { |
| 183 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 184 | throw runtime_error("MemoryIdentityStorage::setDefaultCertificateNameForKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 185 | #endif |
| 186 | } |
| 187 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 188 | |
| 189 | std::vector<Name> |
| 190 | MemoryIdentityStorage::getAllIdentities(bool isDefault) |
| 191 | { |
| 192 | throw runtime_error("MemoryIdentityStorage::getAllIdentities not implemented"); |
| 193 | } |
| 194 | |
| 195 | std::vector<Name> |
| 196 | MemoryIdentityStorage::getAllKeyNames(bool isDefault) |
| 197 | { |
| 198 | throw runtime_error("MemoryIdentityStorage::getAllKeyNames not implemented"); |
| 199 | } |
| 200 | |
| 201 | std::vector<Name> |
| 202 | MemoryIdentityStorage::getAllKeyNamesOfIdentity(const Name& identity, bool isDefault) |
| 203 | { |
| 204 | throw runtime_error("MemoryIdentityStorage::getAllKeyNamesOfIdentity not implemented"); |
| 205 | } |
| 206 | |
| 207 | std::vector<Name> |
| 208 | MemoryIdentityStorage::getAllCertificateNames(bool isDefault) |
| 209 | { |
| 210 | throw runtime_error("MemoryIdentityStorage::getAllCertificateNames not implemented"); |
| 211 | } |
| 212 | |
| 213 | std::vector<Name> |
| 214 | MemoryIdentityStorage::getAllCertificateNamesOfKey(const Name& keyName, bool isDefault) |
| 215 | { |
| 216 | throw runtime_error("MemoryIdentityStorage::getAllCertificateNamesOfKey not implemented"); |
| 217 | } |
| 218 | |
| 219 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 220 | } |