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; |
| 17 | using namespace ndn::ptr_lib; |
| 18 | |
| 19 | namespace ndn { |
| 20 | |
| 21 | MemoryIdentityStorage::~MemoryIdentityStorage() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | bool |
| 26 | MemoryIdentityStorage::doesIdentityExist(const Name& identityName) |
| 27 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 28 | string identityUri = identityName.toUri(); |
| 29 | return find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void |
| 33 | MemoryIdentityStorage::addIdentity(const Name& identityName) |
| 34 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 35 | 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bool |
| 43 | MemoryIdentityStorage::revokeIdentity() |
| 44 | { |
| 45 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 46 | throw runtime_error("MemoryIdentityStorage::revokeIdentity not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 47 | #endif |
| 48 | } |
| 49 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 50 | bool |
| 51 | MemoryIdentityStorage::doesKeyExist(const Name& keyName) |
| 52 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 53 | return keyStore_.find(keyName.toUri()) != keyStore_.end(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 56 | void |
Jeff Thompson | bd04b07 | 2013-09-27 15:14:09 -0700 | [diff] [blame] | 57 | MemoryIdentityStorage::addKey(const Name& keyName, KeyType keyType, const Blob& publicKeyDer) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 58 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 59 | 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | Blob |
| 71 | MemoryIdentityStorage::getKey(const Name& keyName) |
| 72 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 73 | 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void |
| 82 | MemoryIdentityStorage::activateKey(const Name& keyName) |
| 83 | { |
| 84 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 85 | throw runtime_error("MemoryIdentityStorage::activateKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 86 | #endif |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | MemoryIdentityStorage::deactivateKey(const Name& keyName) |
| 91 | { |
| 92 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 93 | throw runtime_error("MemoryIdentityStorage::deactivateKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 94 | #endif |
| 95 | } |
| 96 | |
| 97 | bool |
| 98 | MemoryIdentityStorage::doesCertificateExist(const Name& certificateName) |
| 99 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 100 | return certificateStore_.find(certificateName.toUri()) != certificateStore_.end(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 104 | MemoryIdentityStorage::addCertificate(const IdentityCertificate& certificate) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 105 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 106 | 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jeff Thompson | 3bd90bc | 2013-10-19 16:40:14 -0700 | [diff] [blame] | 127 | ptr_lib::shared_ptr<Data> |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 128 | MemoryIdentityStorage::getCertificate(const Name& certificateName, bool allowAny) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 129 | { |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 130 | 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | Name |
| 141 | MemoryIdentityStorage::getDefaultIdentity() |
| 142 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 143 | return Name(defaultIdentity_); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | Name |
| 147 | MemoryIdentityStorage::getDefaultKeyNameForIdentity(const Name& identityName) |
| 148 | { |
| 149 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 150 | throw runtime_error("MemoryIdentityStorage::getDefaultKeyNameForIdentity not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 151 | #endif |
| 152 | } |
| 153 | |
| 154 | Name |
| 155 | MemoryIdentityStorage::getDefaultCertificateNameForKey(const Name& keyName) |
| 156 | { |
| 157 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 158 | throw runtime_error("MemoryIdentityStorage::getDefaultCertificateNameForKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 159 | #endif |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | MemoryIdentityStorage::setDefaultIdentity(const Name& identityName) |
| 164 | { |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 165 | 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 174 | MemoryIdentityStorage::setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 175 | { |
| 176 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 177 | throw runtime_error("MemoryIdentityStorage::setDefaultKeyNameForIdentity not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 178 | #endif |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | MemoryIdentityStorage::setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName) |
| 183 | { |
| 184 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 185 | throw runtime_error("MemoryIdentityStorage::setDefaultCertificateNameForKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 186 | #endif |
| 187 | } |
| 188 | |
| 189 | } |