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