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