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