Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 22 | */ |
| 23 | |
| 24 | #include "key-chain.hpp" |
| 25 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 26 | #include "../util/random.hpp" |
| 27 | #include "../util/config-file.hpp" |
| 28 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 29 | #include "sec-public-info-sqlite3.hpp" |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 30 | |
| 31 | #ifdef NDN_CXX_HAVE_OSX_SECURITY |
| 32 | #include "sec-tpm-osx.hpp" |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 33 | #endif // NDN_CXX_HAVE_OSX_SECURITY |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 35 | #include "sec-tpm-file.hpp" |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 38 | namespace security { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 39 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 40 | // Use a GUID as a magic number of KeyChain::DEFAULT_PREFIX identifier |
| 41 | const Name KeyChain::DEFAULT_PREFIX("/723821fd-f534-44b3-80d9-44bf5f58bbbb"); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 42 | const Name KeyChain::DIGEST_SHA256_IDENTITY("/localhost/identity/digest-sha256"); |
| 43 | |
| 44 | // Note: cannot use default constructor, as it depends on static variables which may or may not be |
| 45 | // initialized at this point |
| 46 | const SigningInfo KeyChain::DEFAULT_SIGNING_INFO(SigningInfo::SIGNER_TYPE_NULL, Name(), SignatureInfo()); |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 47 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 48 | const RsaKeyParams KeyChain::DEFAULT_KEY_PARAMS; |
| 49 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 50 | const std::string DEFAULT_PIB_SCHEME = "pib-sqlite3"; |
| 51 | |
| 52 | #if defined(NDN_CXX_HAVE_OSX_SECURITY) and defined(NDN_CXX_WITH_OSX_KEYCHAIN) |
| 53 | const std::string DEFAULT_TPM_SCHEME = "tpm-osxkeychain"; |
| 54 | #else |
| 55 | const std::string DEFAULT_TPM_SCHEME = "tpm-file"; |
| 56 | #endif // defined(NDN_CXX_HAVE_OSX_SECURITY) and defined(NDN_CXX_WITH_OSX_KEYCHAIN) |
| 57 | |
| 58 | // When static library is used, not everything is compiled into the resulting binary. |
| 59 | // Therefore, the following standard PIB and TPMs need to be registered here. |
| 60 | // http://stackoverflow.com/q/9459980/2150331 |
| 61 | // |
| 62 | // Also, cannot use Type::SCHEME, as its value may be uninitialized |
| 63 | NDN_CXX_KEYCHAIN_REGISTER_PIB(SecPublicInfoSqlite3, "pib-sqlite3", "sqlite3"); |
| 64 | |
| 65 | #ifdef NDN_CXX_HAVE_OSX_SECURITY |
| 66 | NDN_CXX_KEYCHAIN_REGISTER_TPM(SecTpmOsx, "tpm-osxkeychain", "osx-keychain"); |
| 67 | #endif // NDN_CXX_HAVE_OSX_SECURITY |
| 68 | |
| 69 | NDN_CXX_KEYCHAIN_REGISTER_TPM(SecTpmFile, "tpm-file", "file"); |
| 70 | |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 71 | template<class T> |
| 72 | struct Factory |
| 73 | { |
| 74 | Factory(const std::string& canonicalName, const T& create) |
| 75 | : canonicalName(canonicalName) |
| 76 | , create(create) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | std::string canonicalName; |
| 81 | T create; |
| 82 | }; |
| 83 | typedef Factory<KeyChain::PibCreateFunc> PibFactory; |
| 84 | typedef Factory<KeyChain::TpmCreateFunc> TpmFactory; |
| 85 | |
| 86 | static std::map<std::string, PibFactory>& |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 87 | getPibFactories() |
| 88 | { |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 89 | static std::map<std::string, PibFactory> pibFactories; |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 90 | return pibFactories; |
| 91 | } |
| 92 | |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 93 | static std::map<std::string, TpmFactory>& |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 94 | getTpmFactories() |
| 95 | { |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 96 | static std::map<std::string, TpmFactory> tpmFactories; |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 97 | return tpmFactories; |
| 98 | } |
| 99 | |
| 100 | void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 101 | KeyChain::registerPibImpl(const std::string& canonicalName, |
| 102 | std::initializer_list<std::string> aliases, |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 103 | KeyChain::PibCreateFunc createFunc) |
| 104 | { |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 105 | for (const std::string& alias : aliases) { |
| 106 | getPibFactories().insert(make_pair(alias, PibFactory(canonicalName, createFunc))); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 111 | KeyChain::registerTpmImpl(const std::string& canonicalName, |
| 112 | std::initializer_list<std::string> aliases, |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 113 | KeyChain::TpmCreateFunc createFunc) |
| 114 | { |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 115 | for (const std::string& alias : aliases) { |
| 116 | getTpmFactories().insert(make_pair(alias, TpmFactory(canonicalName, createFunc))); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 120 | KeyChain::KeyChain() |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 121 | : m_pib(nullptr) |
| 122 | , m_tpm(nullptr) |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 123 | , m_lastTimestamp(time::toUnixTimestamp(time::system_clock::now())) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 124 | { |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 125 | ConfigFile config; |
| 126 | const ConfigFile::Parsed& parsed = config.getParsedConfiguration(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 127 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 128 | std::string pibLocator = parsed.get<std::string>("pib", ""); |
| 129 | std::string tpmLocator = parsed.get<std::string>("tpm", ""); |
| 130 | |
| 131 | initialize(pibLocator, tpmLocator, false); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | KeyChain::KeyChain(const std::string& pibName, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 135 | const std::string& tpmName, |
| 136 | bool allowReset) |
| 137 | : m_pib(nullptr) |
| 138 | , m_tpm(nullptr) |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 139 | , m_lastTimestamp(time::toUnixTimestamp(time::system_clock::now())) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 140 | { |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 141 | initialize(pibName, tpmName, allowReset); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 144 | KeyChain::~KeyChain() |
| 145 | { |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 146 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 147 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 148 | static inline std::tuple<std::string/*type*/, std::string/*location*/> |
| 149 | parseUri(const std::string& uri) |
| 150 | { |
| 151 | size_t pos = uri.find(':'); |
| 152 | if (pos != std::string::npos) { |
| 153 | return std::make_tuple(uri.substr(0, pos), |
| 154 | uri.substr(pos + 1)); |
| 155 | } |
| 156 | else { |
| 157 | return std::make_tuple(uri, ""); |
| 158 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 161 | std::string |
| 162 | KeyChain::getDefaultPibLocator() |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 163 | { |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 164 | std::string defaultPibLocator = DEFAULT_PIB_SCHEME + ":"; |
| 165 | return defaultPibLocator; |
| 166 | } |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 167 | |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 168 | static inline std::tuple<std::string/*type*/, std::string/*location*/> |
| 169 | getCanonicalPibLocator(const std::string& pibLocator) |
| 170 | { |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 171 | std::string pibScheme, pibLocation; |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 172 | std::tie(pibScheme, pibLocation) = parseUri(pibLocator); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 173 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 174 | if (pibScheme.empty()) { |
| 175 | pibScheme = DEFAULT_PIB_SCHEME; |
| 176 | } |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 178 | auto pibFactory = getPibFactories().find(pibScheme); |
| 179 | if (pibFactory == getPibFactories().end()) { |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 180 | throw KeyChain::Error("PIB scheme '" + pibScheme + "' is not supported"); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 181 | } |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 182 | pibScheme = pibFactory->second.canonicalName; |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 183 | |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 184 | return std::make_tuple(pibScheme, pibLocation); |
| 185 | } |
| 186 | |
| 187 | unique_ptr<SecPublicInfo> |
| 188 | KeyChain::createPib(const std::string& pibLocator) |
| 189 | { |
| 190 | BOOST_ASSERT(!getPibFactories().empty()); |
| 191 | |
| 192 | std::string pibScheme, pibLocation; |
| 193 | std::tie(pibScheme, pibLocation) = getCanonicalPibLocator(pibLocator); |
| 194 | auto pibFactory = getPibFactories().find(pibScheme); |
| 195 | BOOST_ASSERT(pibFactory != getPibFactories().end()); |
| 196 | return pibFactory->second.create(pibLocation); |
| 197 | } |
| 198 | |
| 199 | std::string |
| 200 | KeyChain::getDefaultTpmLocator() |
| 201 | { |
| 202 | std::string defaultTpmLocator = DEFAULT_TPM_SCHEME + ":"; |
| 203 | return defaultTpmLocator; |
| 204 | } |
| 205 | |
| 206 | static inline std::tuple<std::string/*type*/, std::string/*location*/> |
| 207 | getCanonicalTpmLocator(const std::string& tpmLocator) |
| 208 | { |
| 209 | std::string tpmScheme, tpmLocation; |
| 210 | std::tie(tpmScheme, tpmLocation) = parseUri(tpmLocator); |
| 211 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 212 | if (tpmScheme.empty()) { |
| 213 | tpmScheme = DEFAULT_TPM_SCHEME; |
| 214 | } |
| 215 | auto tpmFactory = getTpmFactories().find(tpmScheme); |
| 216 | if (tpmFactory == getTpmFactories().end()) { |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 217 | throw KeyChain::Error("TPM scheme '" + tpmScheme + "' is not supported"); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 218 | } |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 219 | tpmScheme = tpmFactory->second.canonicalName; |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 220 | |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 221 | return std::make_tuple(tpmScheme, tpmLocation); |
| 222 | } |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 223 | |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 224 | unique_ptr<SecTpm> |
| 225 | KeyChain::createTpm(const std::string& tpmLocator) |
| 226 | { |
| 227 | BOOST_ASSERT(!getTpmFactories().empty()); |
| 228 | |
| 229 | std::string tpmScheme, tpmLocation; |
| 230 | std::tie(tpmScheme, tpmLocation) = getCanonicalTpmLocator(tpmLocator); |
| 231 | auto tpmFactory = getTpmFactories().find(tpmScheme); |
| 232 | BOOST_ASSERT(tpmFactory != getTpmFactories().end()); |
| 233 | return tpmFactory->second.create(tpmLocation); |
| 234 | } |
| 235 | |
| 236 | void |
| 237 | KeyChain::initialize(const std::string& pibLocator, |
| 238 | const std::string& tpmLocator, |
| 239 | bool allowReset) |
| 240 | { |
| 241 | // PIB Locator |
| 242 | std::string pibScheme, pibLocation; |
| 243 | std::tie(pibScheme, pibLocation) = getCanonicalPibLocator(pibLocator); |
| 244 | std::string canonicalPibLocator = pibScheme + ":" + pibLocation; |
| 245 | |
| 246 | // Create PIB |
| 247 | m_pib = createPib(canonicalPibLocator); |
| 248 | |
| 249 | // TPM Locator |
| 250 | std::string tpmScheme, tpmLocation; |
| 251 | std::tie(tpmScheme, tpmLocation) = getCanonicalTpmLocator(tpmLocator); |
| 252 | std::string canonicalTpmLocator = tpmScheme + ":" + tpmLocation; |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 253 | |
| 254 | // Create TPM, checking that it matches to the previously associated one |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 255 | try { |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 256 | if (!allowReset && |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 257 | !m_pib->getTpmLocator().empty() && m_pib->getTpmLocator() != canonicalTpmLocator) |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 258 | // Tpm mismatch, but we do not want to reset PIB |
| 259 | throw MismatchError("TPM locator supplied does not match TPM locator in PIB: " + |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 260 | m_pib->getTpmLocator() + " != " + canonicalTpmLocator); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 261 | } |
| 262 | catch (SecPublicInfo::Error&) { |
| 263 | // TPM locator is not set in PIB yet. |
| 264 | } |
| 265 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 266 | // note that key mismatch may still happen if the TPM locator is initially set to a |
| 267 | // wrong one or if the PIB was shared by more than one TPMs before. This is due to the |
| 268 | // old PIB does not have TPM info, new pib should not have this problem. |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 269 | m_tpm = createTpm(canonicalTpmLocator); |
| 270 | m_pib->setTpmLocator(canonicalTpmLocator); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 273 | Name |
| 274 | KeyChain::createIdentity(const Name& identityName, const KeyParams& params) |
| 275 | { |
| 276 | m_pib->addIdentity(identityName); |
| 277 | |
| 278 | Name keyName; |
| 279 | try |
| 280 | { |
| 281 | keyName = m_pib->getDefaultKeyNameForIdentity(identityName); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 282 | |
| 283 | shared_ptr<PublicKey> key = m_pib->getPublicKey(keyName); |
| 284 | |
| 285 | if (key->getKeyType() != params.getKeyType()) |
| 286 | { |
| 287 | keyName = generateKeyPair(identityName, true, params); |
| 288 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 289 | } |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 290 | } |
| 291 | catch (SecPublicInfo::Error& e) |
| 292 | { |
| 293 | keyName = generateKeyPair(identityName, true, params); |
| 294 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 295 | } |
| 296 | |
| 297 | Name certName; |
| 298 | try |
| 299 | { |
| 300 | certName = m_pib->getDefaultCertificateNameForKey(keyName); |
| 301 | } |
| 302 | catch (SecPublicInfo::Error& e) |
| 303 | { |
| 304 | shared_ptr<IdentityCertificate> selfCert = selfSign(keyName); |
| 305 | m_pib->addCertificateAsIdentityDefault(*selfCert); |
| 306 | certName = selfCert->getName(); |
| 307 | } |
| 308 | |
| 309 | return certName; |
| 310 | } |
| 311 | |
| 312 | Name |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 313 | KeyChain::generateRsaKeyPair(const Name& identityName, bool isKsk, uint32_t keySize) |
| 314 | { |
| 315 | RsaKeyParams params(keySize); |
| 316 | return generateKeyPair(identityName, isKsk, params); |
| 317 | } |
| 318 | |
| 319 | Name |
| 320 | KeyChain::generateEcdsaKeyPair(const Name& identityName, bool isKsk, uint32_t keySize) |
| 321 | { |
| 322 | EcdsaKeyParams params(keySize); |
| 323 | return generateKeyPair(identityName, isKsk, params); |
| 324 | } |
| 325 | |
| 326 | Name |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 327 | KeyChain::generateRsaKeyPairAsDefault(const Name& identityName, bool isKsk, uint32_t keySize) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 328 | { |
| 329 | RsaKeyParams params(keySize); |
| 330 | |
| 331 | Name keyName = generateKeyPair(identityName, isKsk, params); |
| 332 | |
| 333 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 334 | |
| 335 | return keyName; |
| 336 | } |
| 337 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 338 | Name |
| 339 | KeyChain::generateEcdsaKeyPairAsDefault(const Name& identityName, bool isKsk, uint32_t keySize) |
| 340 | { |
| 341 | EcdsaKeyParams params(keySize); |
| 342 | |
| 343 | Name keyName = generateKeyPair(identityName, isKsk, params); |
| 344 | |
| 345 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 346 | |
| 347 | return keyName; |
| 348 | } |
| 349 | |
| 350 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 351 | shared_ptr<IdentityCertificate> |
| 352 | KeyChain::prepareUnsignedIdentityCertificate(const Name& keyName, |
| 353 | const Name& signingIdentity, |
| 354 | const time::system_clock::TimePoint& notBefore, |
| 355 | const time::system_clock::TimePoint& notAfter, |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 356 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 357 | const Name& certPrefix) |
| 358 | { |
| 359 | shared_ptr<PublicKey> publicKey; |
| 360 | try |
| 361 | { |
| 362 | publicKey = m_pib->getPublicKey(keyName); |
| 363 | } |
| 364 | catch (SecPublicInfo::Error& e) |
| 365 | { |
| 366 | return shared_ptr<IdentityCertificate>(); |
| 367 | } |
| 368 | |
| 369 | return prepareUnsignedIdentityCertificate(keyName, *publicKey, signingIdentity, |
| 370 | notBefore, notAfter, |
| 371 | subjectDescription, certPrefix); |
| 372 | } |
| 373 | |
| 374 | shared_ptr<IdentityCertificate> |
| 375 | KeyChain::prepareUnsignedIdentityCertificate(const Name& keyName, |
| 376 | const PublicKey& publicKey, |
| 377 | const Name& signingIdentity, |
| 378 | const time::system_clock::TimePoint& notBefore, |
| 379 | const time::system_clock::TimePoint& notAfter, |
| 380 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 381 | const Name& certPrefix) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 382 | { |
| 383 | if (keyName.size() < 1) |
| 384 | return shared_ptr<IdentityCertificate>(); |
| 385 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 386 | std::string keyIdPrefix = keyName.get(-1).toUri().substr(0, 4); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 387 | if (keyIdPrefix != "ksk-" && keyIdPrefix != "dsk-") |
| 388 | return shared_ptr<IdentityCertificate>(); |
| 389 | |
| 390 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
| 391 | Name certName; |
| 392 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 393 | if (certPrefix == KeyChain::DEFAULT_PREFIX) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 394 | { |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 395 | // No certificate prefix hint, infer the prefix |
| 396 | if (signingIdentity.isPrefixOf(keyName)) |
| 397 | certName.append(signingIdentity) |
| 398 | .append("KEY") |
| 399 | .append(keyName.getSubName(signingIdentity.size())) |
| 400 | .append("ID-CERT") |
| 401 | .appendVersion(); |
| 402 | else |
| 403 | certName.append(keyName.getPrefix(-1)) |
| 404 | .append("KEY") |
| 405 | .append(keyName.get(-1)) |
| 406 | .append("ID-CERT") |
| 407 | .appendVersion(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 408 | } |
| 409 | else |
| 410 | { |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 411 | // cert prefix hint is supplied, determine the cert name. |
| 412 | if (certPrefix.isPrefixOf(keyName) && certPrefix != keyName) |
| 413 | certName.append(certPrefix) |
| 414 | .append("KEY") |
| 415 | .append(keyName.getSubName(certPrefix.size())) |
| 416 | .append("ID-CERT") |
| 417 | .appendVersion(); |
| 418 | else |
| 419 | return shared_ptr<IdentityCertificate>(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 422 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 423 | certificate->setName(certName); |
| 424 | certificate->setNotBefore(notBefore); |
| 425 | certificate->setNotAfter(notAfter); |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 426 | certificate->setPublicKeyInfo(publicKey); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 427 | |
| 428 | if (subjectDescription.empty()) |
| 429 | { |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 430 | CertificateSubjectDescription subjectName(oid::ATTRIBUTE_NAME, keyName.getPrefix(-1).toUri()); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 431 | certificate->addSubjectDescription(subjectName); |
| 432 | } |
| 433 | else |
| 434 | { |
| 435 | std::vector<CertificateSubjectDescription>::const_iterator sdIt = |
| 436 | subjectDescription.begin(); |
| 437 | std::vector<CertificateSubjectDescription>::const_iterator sdEnd = |
| 438 | subjectDescription.end(); |
| 439 | for(; sdIt != sdEnd; sdIt++) |
| 440 | certificate->addSubjectDescription(*sdIt); |
| 441 | } |
| 442 | |
| 443 | certificate->encode(); |
| 444 | |
| 445 | return certificate; |
| 446 | } |
| 447 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 448 | std::tuple<Name, SignatureInfo> |
| 449 | KeyChain::prepareSignatureInfo(const SigningInfo& params) |
| 450 | { |
| 451 | SignatureInfo sigInfo = params.getSignatureInfo(); |
| 452 | |
| 453 | shared_ptr<IdentityCertificate> signingCert; |
| 454 | |
| 455 | switch (params.getSignerType()) { |
| 456 | case SigningInfo::SIGNER_TYPE_NULL: |
| 457 | { |
| 458 | if (m_pib->getDefaultCertificate() == nullptr) |
| 459 | setDefaultCertificateInternal(); |
| 460 | |
| 461 | signingCert = m_pib->getDefaultCertificate(); |
| 462 | break; |
| 463 | } |
| 464 | case SigningInfo::SIGNER_TYPE_ID: |
| 465 | { |
| 466 | Name signingCertName; |
| 467 | try { |
| 468 | signingCertName = m_pib->getDefaultCertificateNameForIdentity(params.getSignerName()); |
| 469 | } |
| 470 | catch (SecPublicInfo::Error&) { |
| 471 | signingCertName = createIdentity(params.getSignerName()); |
| 472 | } |
| 473 | |
| 474 | signingCert = m_pib->getCertificate(signingCertName); |
| 475 | |
| 476 | break; |
| 477 | } |
| 478 | case SigningInfo::SIGNER_TYPE_KEY: |
| 479 | { |
| 480 | Name signingCertName; |
| 481 | try { |
| 482 | signingCertName = m_pib->getDefaultCertificateNameForKey(params.getSignerName()); |
| 483 | } |
| 484 | catch (SecPublicInfo::Error&) { |
| 485 | throw Error("signing certificate does not exist"); |
| 486 | } |
| 487 | |
| 488 | signingCert = m_pib->getCertificate(signingCertName); |
| 489 | |
| 490 | break; |
| 491 | } |
| 492 | case SigningInfo::SIGNER_TYPE_CERT: |
| 493 | { |
| 494 | signingCert = m_pib->getCertificate(params.getSignerName()); |
| 495 | if (signingCert == nullptr) |
| 496 | throw Error("signing certificate does not exist"); |
| 497 | |
| 498 | break; |
| 499 | } |
| 500 | case SigningInfo::SIGNER_TYPE_SHA256: |
| 501 | { |
| 502 | sigInfo.setSignatureType(tlv::DigestSha256); |
| 503 | return std::make_tuple(DIGEST_SHA256_IDENTITY, sigInfo); |
| 504 | } |
| 505 | default: |
| 506 | throw Error("Unrecognized signer type"); |
| 507 | } |
| 508 | |
| 509 | sigInfo.setSignatureType(getSignatureType(signingCert->getPublicKeyInfo().getKeyType(), |
| 510 | params.getDigestAlgorithm())); |
| 511 | sigInfo.setKeyLocator(KeyLocator(signingCert->getName().getPrefix(-1))); |
| 512 | |
| 513 | return std::make_tuple(signingCert->getPublicKeyName(), sigInfo); |
| 514 | } |
| 515 | |
| 516 | void |
| 517 | KeyChain::sign(Data& data, const SigningInfo& params) |
| 518 | { |
| 519 | signImpl(data, params); |
| 520 | } |
| 521 | |
| 522 | void |
| 523 | KeyChain::sign(Interest& interest, const SigningInfo& params) |
| 524 | { |
| 525 | signImpl(interest, params); |
| 526 | } |
| 527 | |
| 528 | Block |
| 529 | KeyChain::sign(const uint8_t* buffer, size_t bufferLength, const SigningInfo& params) |
| 530 | { |
| 531 | Name keyName; |
| 532 | SignatureInfo sigInfo; |
| 533 | std::tie(keyName, sigInfo) = prepareSignatureInfo(params); |
| 534 | return pureSign(buffer, bufferLength, keyName, DIGEST_ALGORITHM_SHA256); |
| 535 | } |
| 536 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 537 | Signature |
| 538 | KeyChain::sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName) |
| 539 | { |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 540 | shared_ptr<IdentityCertificate> certificate = m_pib->getCertificate(certificateName); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 541 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 542 | if (certificate == nullptr) |
| 543 | throw SecPublicInfo::Error("certificate does not exist"); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 544 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 545 | Signature sig; |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 546 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 547 | // For temporary usage, we support SHA256 only, but will support more. |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 548 | sig.setValue(m_tpm->signInTpm(buffer, bufferLength, |
| 549 | certificate->getPublicKeyName(), |
| 550 | DIGEST_ALGORITHM_SHA256)); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 551 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 552 | return sig; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | shared_ptr<IdentityCertificate> |
| 556 | KeyChain::selfSign(const Name& keyName) |
| 557 | { |
| 558 | shared_ptr<PublicKey> pubKey; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 559 | try { |
| 560 | pubKey = m_pib->getPublicKey(keyName); // may throw an exception. |
| 561 | } |
| 562 | catch (SecPublicInfo::Error&) { |
| 563 | return shared_ptr<IdentityCertificate>(); |
| 564 | } |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 565 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 566 | auto certificate = make_shared<IdentityCertificate>(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 567 | |
| 568 | Name certificateName = keyName.getPrefix(-1); |
| 569 | certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion(); |
| 570 | |
| 571 | certificate->setName(certificateName); |
| 572 | certificate->setNotBefore(time::system_clock::now()); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 573 | certificate->setNotAfter(time::system_clock::now() + time::days(7300)); // ~20 years |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 574 | certificate->setPublicKeyInfo(*pubKey); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 575 | certificate->addSubjectDescription(CertificateSubjectDescription(oid::ATTRIBUTE_NAME, |
| 576 | keyName.toUri())); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 577 | certificate->encode(); |
| 578 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 579 | certificate->setSignature(Signature(SignatureInfo())); |
| 580 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 581 | selfSign(*certificate); |
| 582 | return certificate; |
| 583 | } |
| 584 | |
| 585 | void |
| 586 | KeyChain::selfSign(IdentityCertificate& cert) |
| 587 | { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 588 | Name keyName = cert.getPublicKeyName(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 589 | if (!m_tpm->doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 590 | throw SecTpm::Error("Private key does not exist"); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 591 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 592 | SignatureInfo sigInfo(cert.getSignature().getInfo()); |
| 593 | sigInfo.setKeyLocator(KeyLocator(cert.getName().getPrefix(-1))); |
| 594 | sigInfo.setSignatureType(getSignatureType(cert.getPublicKeyInfo().getKeyType(), |
| 595 | DIGEST_ALGORITHM_SHA256)); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 596 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 597 | signPacketWrapper(cert, Signature(sigInfo), keyName, DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | shared_ptr<SecuredBag> |
| 601 | KeyChain::exportIdentity(const Name& identity, const std::string& passwordStr) |
| 602 | { |
| 603 | if (!m_pib->doesIdentityExist(identity)) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 604 | throw SecPublicInfo::Error("Identity does not exist"); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 605 | |
| 606 | Name keyName = m_pib->getDefaultKeyNameForIdentity(identity); |
| 607 | |
| 608 | ConstBufferPtr pkcs5; |
| 609 | try |
| 610 | { |
| 611 | pkcs5 = m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, passwordStr); |
| 612 | } |
| 613 | catch (SecTpm::Error& e) |
| 614 | { |
| 615 | throw SecPublicInfo::Error("Fail to export PKCS5 of private key"); |
| 616 | } |
| 617 | |
| 618 | shared_ptr<IdentityCertificate> cert; |
| 619 | try |
| 620 | { |
| 621 | cert = m_pib->getCertificate(m_pib->getDefaultCertificateNameForKey(keyName)); |
| 622 | } |
| 623 | catch (SecPublicInfo::Error& e) |
| 624 | { |
| 625 | cert = selfSign(keyName); |
| 626 | m_pib->addCertificateAsIdentityDefault(*cert); |
| 627 | } |
| 628 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 629 | // make_shared on OSX 10.9 has some strange problem here |
| 630 | shared_ptr<SecuredBag> secureBag(new SecuredBag(*cert, pkcs5)); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 631 | |
| 632 | return secureBag; |
| 633 | } |
| 634 | |
| 635 | void |
| 636 | KeyChain::importIdentity(const SecuredBag& securedBag, const std::string& passwordStr) |
| 637 | { |
| 638 | Name certificateName = securedBag.getCertificate().getName(); |
| 639 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
| 640 | Name identity = keyName.getPrefix(-1); |
| 641 | |
| 642 | // Add identity |
| 643 | m_pib->addIdentity(identity); |
| 644 | |
| 645 | // Add key |
| 646 | m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, |
| 647 | securedBag.getKey()->buf(), |
| 648 | securedBag.getKey()->size(), |
| 649 | passwordStr); |
| 650 | |
| 651 | shared_ptr<PublicKey> pubKey = m_tpm->getPublicKeyFromTpm(keyName.toUri()); |
| 652 | // HACK! We should set key type according to the pkcs8 info. |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 653 | m_pib->addKey(keyName, *pubKey); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 654 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 655 | |
| 656 | // Add cert |
| 657 | m_pib->addCertificateAsIdentityDefault(securedBag.getCertificate()); |
| 658 | } |
| 659 | |
| 660 | void |
| 661 | KeyChain::setDefaultCertificateInternal() |
| 662 | { |
| 663 | m_pib->refreshDefaultCertificate(); |
| 664 | |
Alexander Afanasyev | aab7966 | 2014-07-07 17:35:34 -0700 | [diff] [blame] | 665 | if (!static_cast<bool>(m_pib->getDefaultCertificate())) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 666 | { |
| 667 | Name defaultIdentity; |
| 668 | try |
| 669 | { |
| 670 | defaultIdentity = m_pib->getDefaultIdentity(); |
| 671 | } |
| 672 | catch (SecPublicInfo::Error& e) |
| 673 | { |
| 674 | uint32_t random = random::generateWord32(); |
| 675 | defaultIdentity.append("tmp-identity") |
| 676 | .append(reinterpret_cast<uint8_t*>(&random), 4); |
| 677 | } |
| 678 | createIdentity(defaultIdentity); |
| 679 | m_pib->setDefaultIdentity(defaultIdentity); |
| 680 | m_pib->refreshDefaultCertificate(); |
| 681 | } |
| 682 | } |
| 683 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 684 | Name |
| 685 | KeyChain::generateKeyPair(const Name& identityName, bool isKsk, const KeyParams& params) |
| 686 | { |
| 687 | Name keyName = m_pib->getNewKeyName(identityName, isKsk); |
| 688 | |
| 689 | m_tpm->generateKeyPairInTpm(keyName.toUri(), params); |
| 690 | |
| 691 | shared_ptr<PublicKey> pubKey = m_tpm->getPublicKeyFromTpm(keyName.toUri()); |
| 692 | m_pib->addKey(keyName, *pubKey); |
| 693 | |
| 694 | return keyName; |
| 695 | } |
| 696 | |
| 697 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 698 | KeyChain::signPacketWrapper(Data& data, const Signature& signature, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 699 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 700 | { |
| 701 | data.setSignature(signature); |
| 702 | |
| 703 | EncodingBuffer encoder; |
| 704 | data.wireEncode(encoder, true); |
| 705 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 706 | Block sigValue = pureSign(encoder.buf(), encoder.size(), keyName, digestAlgorithm); |
| 707 | |
| 708 | data.wireEncode(encoder, sigValue); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 712 | KeyChain::signPacketWrapper(Interest& interest, const Signature& signature, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 713 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 714 | { |
| 715 | time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now()); |
| 716 | if (timestamp <= m_lastTimestamp) |
| 717 | { |
| 718 | timestamp = m_lastTimestamp + time::milliseconds(1); |
| 719 | } |
| 720 | |
| 721 | Name signedName = interest.getName(); |
| 722 | signedName |
| 723 | .append(name::Component::fromNumber(timestamp.count())) // timestamp |
| 724 | .append(name::Component::fromNumber(random::generateWord64())) // nonce |
| 725 | .append(signature.getInfo()); // signatureInfo |
| 726 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 727 | Block sigValue = pureSign(signedName.wireEncode().value(), |
| 728 | signedName.wireEncode().value_size(), |
| 729 | keyName, |
| 730 | digestAlgorithm); |
| 731 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 732 | sigValue.encode(); |
| 733 | signedName.append(sigValue); // signatureValue |
| 734 | interest.setName(signedName); |
| 735 | } |
| 736 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 737 | Block |
| 738 | KeyChain::pureSign(const uint8_t* buf, size_t size, |
| 739 | const Name& keyName, DigestAlgorithm digestAlgorithm) const |
| 740 | { |
| 741 | if (keyName == DIGEST_SHA256_IDENTITY) |
| 742 | return Block(tlv::SignatureValue, crypto::sha256(buf, size)); |
| 743 | |
| 744 | return m_tpm->signInTpm(buf, size, keyName, digestAlgorithm); |
| 745 | } |
| 746 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 747 | Signature |
| 748 | KeyChain::signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName) |
| 749 | { |
| 750 | Name signingCertificateName; |
| 751 | try |
| 752 | { |
| 753 | signingCertificateName = m_pib->getDefaultCertificateNameForIdentity(identityName); |
| 754 | } |
| 755 | catch (SecPublicInfo::Error& e) |
| 756 | { |
| 757 | signingCertificateName = createIdentity(identityName); |
| 758 | // Ideally, no exception will be thrown out, unless something goes wrong in the TPM, which |
| 759 | // is a fatal error. |
| 760 | } |
| 761 | |
| 762 | // We either get or create the signing certificate, sign data! (no exception unless fatal error |
| 763 | // in TPM) |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 764 | Signature sig; |
| 765 | |
| 766 | // For temporary usage, we support SHA256 only, but will support more. |
| 767 | sig.setValue(sign(buffer, bufferLength, SigningInfo(SigningInfo::SIGNER_TYPE_CERT, |
| 768 | signingCertificateName))); |
| 769 | |
| 770 | return sig; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | void |
| 774 | KeyChain::signWithSha256(Data& data) |
| 775 | { |
| 776 | DigestSha256 sig; |
| 777 | data.setSignature(sig); |
| 778 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 779 | Block sigValue(tlv::SignatureValue, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 780 | crypto::sha256(data.wireEncode().value(), |
| 781 | data.wireEncode().value_size() - |
| 782 | data.getSignature().getValue().size())); |
| 783 | data.setSignatureValue(sigValue); |
| 784 | } |
| 785 | |
| 786 | void |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 787 | KeyChain::signWithSha256(Interest& interest) |
| 788 | { |
| 789 | DigestSha256 sig; |
| 790 | |
| 791 | time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now()); |
| 792 | if (timestamp <= m_lastTimestamp) |
| 793 | timestamp = m_lastTimestamp + time::milliseconds(1); |
| 794 | |
| 795 | Name signedName = interest.getName(); |
| 796 | signedName |
| 797 | .append(name::Component::fromNumber(timestamp.count())) // timestamp |
| 798 | .append(name::Component::fromNumber(random::generateWord64())) // nonce |
| 799 | .append(sig.getInfo()); // signatureInfo |
| 800 | |
| 801 | Block sigValue(tlv::SignatureValue, |
| 802 | crypto::sha256(signedName.wireEncode().value(), |
| 803 | signedName.wireEncode().value_size())); |
| 804 | |
| 805 | sigValue.encode(); |
| 806 | signedName.append(sigValue); // signatureValue |
| 807 | interest.setName(signedName); |
| 808 | } |
| 809 | |
| 810 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 811 | KeyChain::deleteCertificate(const Name& certificateName) |
| 812 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 813 | m_pib->deleteCertificateInfo(certificateName); |
| 814 | } |
| 815 | |
| 816 | void |
| 817 | KeyChain::deleteKey(const Name& keyName) |
| 818 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 819 | m_pib->deletePublicKeyInfo(keyName); |
| 820 | m_tpm->deleteKeyPairInTpm(keyName); |
| 821 | } |
| 822 | |
| 823 | void |
| 824 | KeyChain::deleteIdentity(const Name& identity) |
| 825 | { |
Yingdi Yu | 6147ef4 | 2014-12-08 17:48:32 -0800 | [diff] [blame] | 826 | std::vector<Name> keyNames; |
| 827 | m_pib->getAllKeyNamesOfIdentity(identity, keyNames, true); |
| 828 | m_pib->getAllKeyNamesOfIdentity(identity, keyNames, false); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 829 | |
| 830 | m_pib->deleteIdentityInfo(identity); |
| 831 | |
Yingdi Yu | 6147ef4 | 2014-12-08 17:48:32 -0800 | [diff] [blame] | 832 | for (const auto& keyName : keyNames) |
| 833 | m_tpm->deleteKeyPairInTpm(keyName); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 836 | tlv::SignatureTypeValue |
| 837 | KeyChain::getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm) |
| 838 | { |
| 839 | switch (keyType) { |
| 840 | case KEY_TYPE_RSA: |
| 841 | return tlv::SignatureSha256WithRsa; |
| 842 | case KEY_TYPE_ECDSA: |
| 843 | return tlv::SignatureSha256WithEcdsa; |
| 844 | default: |
| 845 | throw Error("Unsupported key types"); |
| 846 | } |
| 847 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 848 | } |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 849 | |
| 850 | } // namespace security |
| 851 | } // namespace ndn |