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 | |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 301 | shared_ptr<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) { |
| 318 | shared_ptr<IdentityCertificate> selfCert = selfSign(keyName); |
| 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 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 365 | shared_ptr<IdentityCertificate> |
| 366 | KeyChain::prepareUnsignedIdentityCertificate(const Name& keyName, |
| 367 | const Name& signingIdentity, |
| 368 | const time::system_clock::TimePoint& notBefore, |
| 369 | const time::system_clock::TimePoint& notAfter, |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 370 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 371 | const Name& certPrefix) |
| 372 | { |
| 373 | shared_ptr<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 | |
| 386 | shared_ptr<IdentityCertificate> |
| 387 | KeyChain::prepareUnsignedIdentityCertificate(const Name& keyName, |
| 388 | const PublicKey& publicKey, |
| 389 | const Name& signingIdentity, |
| 390 | const time::system_clock::TimePoint& notBefore, |
| 391 | const time::system_clock::TimePoint& notAfter, |
| 392 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 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 | |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 431 | auto certificate = make_shared<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()) { |
| 438 | CertificateSubjectDescription subjectName(oid::ATTRIBUTE_NAME, keyName.getPrefix(-1).toUri()); |
| 439 | certificate->addSubjectDescription(subjectName); |
| 440 | } |
| 441 | else { |
| 442 | std::vector<CertificateSubjectDescription>::const_iterator sdIt = |
| 443 | subjectDescription.begin(); |
| 444 | std::vector<CertificateSubjectDescription>::const_iterator sdEnd = |
| 445 | subjectDescription.end(); |
| 446 | for(; sdIt != sdEnd; sdIt++) |
| 447 | certificate->addSubjectDescription(*sdIt); |
| 448 | } |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 449 | |
| 450 | certificate->encode(); |
| 451 | |
| 452 | return certificate; |
| 453 | } |
| 454 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 455 | std::tuple<Name, SignatureInfo> |
| 456 | KeyChain::prepareSignatureInfo(const SigningInfo& params) |
| 457 | { |
| 458 | SignatureInfo sigInfo = params.getSignatureInfo(); |
| 459 | |
| 460 | shared_ptr<IdentityCertificate> signingCert; |
| 461 | |
| 462 | switch (params.getSignerType()) { |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 463 | case SigningInfo::SIGNER_TYPE_NULL: { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 464 | if (m_pib->getDefaultCertificate() == nullptr) |
| 465 | setDefaultCertificateInternal(); |
| 466 | |
| 467 | signingCert = m_pib->getDefaultCertificate(); |
| 468 | break; |
| 469 | } |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 470 | case SigningInfo::SIGNER_TYPE_ID: { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 471 | Name signingCertName; |
| 472 | try { |
| 473 | signingCertName = m_pib->getDefaultCertificateNameForIdentity(params.getSignerName()); |
| 474 | } |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 475 | catch (const SecPublicInfo::Error&) { |
| 476 | signingCertName = createIdentity(params.getSignerName(), getDefaultKeyParamsForIdentity(params.getSignerName())); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | signingCert = m_pib->getCertificate(signingCertName); |
| 480 | |
| 481 | break; |
| 482 | } |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 483 | case SigningInfo::SIGNER_TYPE_KEY: { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 484 | Name signingCertName; |
| 485 | try { |
| 486 | signingCertName = m_pib->getDefaultCertificateNameForKey(params.getSignerName()); |
| 487 | } |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 488 | catch (const SecPublicInfo::Error&) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 489 | BOOST_THROW_EXCEPTION(Error("signing certificate does not exist")); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | signingCert = m_pib->getCertificate(signingCertName); |
| 493 | |
| 494 | break; |
| 495 | } |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 496 | case SigningInfo::SIGNER_TYPE_CERT: { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 497 | signingCert = m_pib->getCertificate(params.getSignerName()); |
| 498 | if (signingCert == nullptr) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 499 | BOOST_THROW_EXCEPTION(Error("signing certificate does not exist")); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 500 | |
| 501 | break; |
| 502 | } |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 503 | case SigningInfo::SIGNER_TYPE_SHA256: { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 504 | sigInfo.setSignatureType(tlv::DigestSha256); |
| 505 | return std::make_tuple(DIGEST_SHA256_IDENTITY, sigInfo); |
| 506 | } |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 507 | default: |
| 508 | BOOST_THROW_EXCEPTION(Error("Unrecognized signer type")); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | sigInfo.setSignatureType(getSignatureType(signingCert->getPublicKeyInfo().getKeyType(), |
| 512 | params.getDigestAlgorithm())); |
| 513 | sigInfo.setKeyLocator(KeyLocator(signingCert->getName().getPrefix(-1))); |
| 514 | |
| 515 | return std::make_tuple(signingCert->getPublicKeyName(), sigInfo); |
| 516 | } |
| 517 | |
| 518 | void |
| 519 | KeyChain::sign(Data& data, const SigningInfo& params) |
| 520 | { |
| 521 | signImpl(data, params); |
| 522 | } |
| 523 | |
| 524 | void |
| 525 | KeyChain::sign(Interest& interest, const SigningInfo& params) |
| 526 | { |
| 527 | signImpl(interest, params); |
| 528 | } |
| 529 | |
| 530 | Block |
| 531 | KeyChain::sign(const uint8_t* buffer, size_t bufferLength, const SigningInfo& params) |
| 532 | { |
| 533 | Name keyName; |
| 534 | SignatureInfo sigInfo; |
| 535 | std::tie(keyName, sigInfo) = prepareSignatureInfo(params); |
| 536 | return pureSign(buffer, bufferLength, keyName, DIGEST_ALGORITHM_SHA256); |
| 537 | } |
| 538 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 539 | Signature |
| 540 | KeyChain::sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName) |
| 541 | { |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 542 | shared_ptr<IdentityCertificate> certificate = m_pib->getCertificate(certificateName); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 543 | |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 544 | if (certificate == nullptr) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 545 | BOOST_THROW_EXCEPTION(SecPublicInfo::Error("certificate does not exist")); |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 546 | } |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 547 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 548 | Signature sig; |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 549 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 550 | // For temporary usage, we support SHA256 only, but will support more. |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 551 | sig.setValue(m_tpm->signInTpm(buffer, bufferLength, |
| 552 | certificate->getPublicKeyName(), |
| 553 | DIGEST_ALGORITHM_SHA256)); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 554 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 555 | return sig; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | shared_ptr<IdentityCertificate> |
| 559 | KeyChain::selfSign(const Name& keyName) |
| 560 | { |
| 561 | shared_ptr<PublicKey> pubKey; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 562 | try { |
| 563 | pubKey = m_pib->getPublicKey(keyName); // may throw an exception. |
| 564 | } |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 565 | catch (const SecPublicInfo::Error&) { |
| 566 | return nullptr; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 567 | } |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 568 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 569 | auto certificate = make_shared<IdentityCertificate>(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 570 | |
| 571 | Name certificateName = keyName.getPrefix(-1); |
| 572 | certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion(); |
| 573 | |
| 574 | certificate->setName(certificateName); |
| 575 | certificate->setNotBefore(time::system_clock::now()); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 576 | certificate->setNotAfter(time::system_clock::now() + time::days(7300)); // ~20 years |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 577 | certificate->setPublicKeyInfo(*pubKey); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 578 | certificate->addSubjectDescription(CertificateSubjectDescription(oid::ATTRIBUTE_NAME, |
| 579 | keyName.toUri())); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 580 | certificate->encode(); |
| 581 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 582 | certificate->setSignature(Signature(SignatureInfo())); |
| 583 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 584 | selfSign(*certificate); |
| 585 | return certificate; |
| 586 | } |
| 587 | |
| 588 | void |
| 589 | KeyChain::selfSign(IdentityCertificate& cert) |
| 590 | { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 591 | Name keyName = cert.getPublicKeyName(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 592 | if (!m_tpm->doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 593 | BOOST_THROW_EXCEPTION(SecTpm::Error("Private key does not exist")); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 594 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 595 | SignatureInfo sigInfo(cert.getSignature().getInfo()); |
| 596 | sigInfo.setKeyLocator(KeyLocator(cert.getName().getPrefix(-1))); |
| 597 | sigInfo.setSignatureType(getSignatureType(cert.getPublicKeyInfo().getKeyType(), |
| 598 | DIGEST_ALGORITHM_SHA256)); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 599 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 600 | signPacketWrapper(cert, Signature(sigInfo), keyName, DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | shared_ptr<SecuredBag> |
| 604 | KeyChain::exportIdentity(const Name& identity, const std::string& passwordStr) |
| 605 | { |
| 606 | if (!m_pib->doesIdentityExist(identity)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 607 | BOOST_THROW_EXCEPTION(SecPublicInfo::Error("Identity does not exist")); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 608 | |
| 609 | Name keyName = m_pib->getDefaultKeyNameForIdentity(identity); |
| 610 | |
| 611 | ConstBufferPtr pkcs5; |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 612 | try { |
| 613 | pkcs5 = m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, passwordStr); |
| 614 | } |
| 615 | catch (const SecTpm::Error& e) { |
| 616 | BOOST_THROW_EXCEPTION(SecPublicInfo::Error("Fail to export PKCS5 of private key")); |
| 617 | } |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 618 | |
| 619 | shared_ptr<IdentityCertificate> cert; |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 620 | try { |
| 621 | cert = m_pib->getCertificate(m_pib->getDefaultCertificateNameForKey(keyName)); |
| 622 | } |
| 623 | catch (const SecPublicInfo::Error& e) { |
| 624 | cert = selfSign(keyName); |
| 625 | m_pib->addCertificateAsIdentityDefault(*cert); |
| 626 | } |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 627 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 628 | // make_shared on OSX 10.9 has some strange problem here |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 629 | return shared_ptr<SecuredBag>(new SecuredBag(*cert, pkcs5)); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | void |
| 633 | KeyChain::importIdentity(const SecuredBag& securedBag, const std::string& passwordStr) |
| 634 | { |
| 635 | Name certificateName = securedBag.getCertificate().getName(); |
| 636 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
| 637 | Name identity = keyName.getPrefix(-1); |
| 638 | |
| 639 | // Add identity |
| 640 | m_pib->addIdentity(identity); |
| 641 | |
| 642 | // Add key |
| 643 | m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, |
| 644 | securedBag.getKey()->buf(), |
| 645 | securedBag.getKey()->size(), |
| 646 | passwordStr); |
| 647 | |
| 648 | shared_ptr<PublicKey> pubKey = m_tpm->getPublicKeyFromTpm(keyName.toUri()); |
| 649 | // HACK! We should set key type according to the pkcs8 info. |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 650 | m_pib->addKey(keyName, *pubKey); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 651 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 652 | |
| 653 | // Add cert |
| 654 | m_pib->addCertificateAsIdentityDefault(securedBag.getCertificate()); |
| 655 | } |
| 656 | |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 657 | const KeyParams& |
| 658 | KeyChain::getDefaultKeyParamsForIdentity(const Name &identityName) const |
| 659 | { |
| 660 | KeyType keyType = KEY_TYPE_NULL; |
| 661 | try { |
| 662 | keyType = m_pib->getPublicKeyType(m_pib->getDefaultKeyNameForIdentity(identityName)); |
| 663 | } |
| 664 | catch (const SecPublicInfo::Error& e) { // @TODO Switch to Pib::Error |
| 665 | return DEFAULT_KEY_PARAMS; |
| 666 | } |
| 667 | |
| 668 | switch (keyType) { |
| 669 | case KEY_TYPE_RSA: { |
| 670 | static RsaKeyParams defaultRsaParams; |
| 671 | return defaultRsaParams; |
| 672 | } |
| 673 | case KEY_TYPE_ECDSA: { |
| 674 | static EcdsaKeyParams defaultEcdsaParams; |
| 675 | return defaultEcdsaParams; |
| 676 | } |
| 677 | case KEY_TYPE_NULL: { |
| 678 | return DEFAULT_KEY_PARAMS; |
| 679 | } |
| 680 | default: |
| 681 | BOOST_THROW_EXCEPTION(Error("Unsupported key type")); |
| 682 | } |
| 683 | } |
| 684 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 685 | void |
| 686 | KeyChain::setDefaultCertificateInternal() |
| 687 | { |
| 688 | m_pib->refreshDefaultCertificate(); |
| 689 | |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 690 | if (m_pib->getDefaultCertificate() == nullptr) { |
| 691 | Name defaultIdentity; |
| 692 | try { |
| 693 | defaultIdentity = m_pib->getDefaultIdentity(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 694 | } |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 695 | catch (const SecPublicInfo::Error& e) { |
| 696 | uint32_t random = random::generateWord32(); |
| 697 | defaultIdentity.append("tmp-identity") |
| 698 | .append(reinterpret_cast<uint8_t*>(&random), 4); |
| 699 | } |
| 700 | createIdentity(defaultIdentity); |
| 701 | m_pib->setDefaultIdentity(defaultIdentity); |
| 702 | m_pib->refreshDefaultCertificate(); |
| 703 | } |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 706 | Name |
| 707 | KeyChain::generateKeyPair(const Name& identityName, bool isKsk, const KeyParams& params) |
| 708 | { |
| 709 | Name keyName = m_pib->getNewKeyName(identityName, isKsk); |
| 710 | |
| 711 | m_tpm->generateKeyPairInTpm(keyName.toUri(), params); |
| 712 | |
| 713 | shared_ptr<PublicKey> pubKey = m_tpm->getPublicKeyFromTpm(keyName.toUri()); |
| 714 | m_pib->addKey(keyName, *pubKey); |
| 715 | |
| 716 | return keyName; |
| 717 | } |
| 718 | |
| 719 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 720 | KeyChain::signPacketWrapper(Data& data, const Signature& signature, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 721 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 722 | { |
| 723 | data.setSignature(signature); |
| 724 | |
| 725 | EncodingBuffer encoder; |
| 726 | data.wireEncode(encoder, true); |
| 727 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 728 | Block sigValue = pureSign(encoder.buf(), encoder.size(), keyName, digestAlgorithm); |
| 729 | |
| 730 | data.wireEncode(encoder, sigValue); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 734 | KeyChain::signPacketWrapper(Interest& interest, const Signature& signature, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 735 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 736 | { |
| 737 | time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now()); |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 738 | if (timestamp <= m_lastTimestamp) { |
| 739 | timestamp = m_lastTimestamp + time::milliseconds(1); |
| 740 | } |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 741 | |
| 742 | Name signedName = interest.getName(); |
| 743 | signedName |
| 744 | .append(name::Component::fromNumber(timestamp.count())) // timestamp |
| 745 | .append(name::Component::fromNumber(random::generateWord64())) // nonce |
| 746 | .append(signature.getInfo()); // signatureInfo |
| 747 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 748 | Block sigValue = pureSign(signedName.wireEncode().value(), |
| 749 | signedName.wireEncode().value_size(), |
| 750 | keyName, |
| 751 | digestAlgorithm); |
| 752 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 753 | sigValue.encode(); |
| 754 | signedName.append(sigValue); // signatureValue |
| 755 | interest.setName(signedName); |
| 756 | } |
| 757 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 758 | Block |
| 759 | KeyChain::pureSign(const uint8_t* buf, size_t size, |
| 760 | const Name& keyName, DigestAlgorithm digestAlgorithm) const |
| 761 | { |
| 762 | if (keyName == DIGEST_SHA256_IDENTITY) |
| 763 | return Block(tlv::SignatureValue, crypto::sha256(buf, size)); |
| 764 | |
| 765 | return m_tpm->signInTpm(buf, size, keyName, digestAlgorithm); |
| 766 | } |
| 767 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 768 | Signature |
| 769 | KeyChain::signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName) |
| 770 | { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 771 | Signature sig; |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 772 | sig.setValue(sign(buffer, bufferLength, signingByIdentity(identityName))); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 773 | return sig; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | void |
| 777 | KeyChain::signWithSha256(Data& data) |
| 778 | { |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 779 | return sign(data, signingWithSha256()); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | void |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 783 | KeyChain::signWithSha256(Interest& interest) |
| 784 | { |
| 785 | DigestSha256 sig; |
| 786 | |
| 787 | time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now()); |
| 788 | if (timestamp <= m_lastTimestamp) |
| 789 | timestamp = m_lastTimestamp + time::milliseconds(1); |
| 790 | |
| 791 | Name signedName = interest.getName(); |
| 792 | signedName |
| 793 | .append(name::Component::fromNumber(timestamp.count())) // timestamp |
| 794 | .append(name::Component::fromNumber(random::generateWord64())) // nonce |
| 795 | .append(sig.getInfo()); // signatureInfo |
| 796 | |
| 797 | Block sigValue(tlv::SignatureValue, |
| 798 | crypto::sha256(signedName.wireEncode().value(), |
| 799 | signedName.wireEncode().value_size())); |
| 800 | |
| 801 | sigValue.encode(); |
| 802 | signedName.append(sigValue); // signatureValue |
| 803 | interest.setName(signedName); |
| 804 | } |
| 805 | |
| 806 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 807 | KeyChain::deleteCertificate(const Name& certificateName) |
| 808 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 809 | m_pib->deleteCertificateInfo(certificateName); |
| 810 | } |
| 811 | |
| 812 | void |
| 813 | KeyChain::deleteKey(const Name& keyName) |
| 814 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 815 | m_pib->deletePublicKeyInfo(keyName); |
| 816 | m_tpm->deleteKeyPairInTpm(keyName); |
| 817 | } |
| 818 | |
| 819 | void |
| 820 | KeyChain::deleteIdentity(const Name& identity) |
| 821 | { |
Yingdi Yu | 6147ef4 | 2014-12-08 17:48:32 -0800 | [diff] [blame] | 822 | std::vector<Name> keyNames; |
| 823 | m_pib->getAllKeyNamesOfIdentity(identity, keyNames, true); |
| 824 | m_pib->getAllKeyNamesOfIdentity(identity, keyNames, false); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 825 | |
| 826 | m_pib->deleteIdentityInfo(identity); |
| 827 | |
Yingdi Yu | 6147ef4 | 2014-12-08 17:48:32 -0800 | [diff] [blame] | 828 | for (const auto& keyName : keyNames) |
| 829 | m_tpm->deleteKeyPairInTpm(keyName); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 832 | tlv::SignatureTypeValue |
| 833 | KeyChain::getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm) |
| 834 | { |
| 835 | switch (keyType) { |
| 836 | case KEY_TYPE_RSA: |
| 837 | return tlv::SignatureSha256WithRsa; |
| 838 | case KEY_TYPE_ECDSA: |
| 839 | return tlv::SignatureSha256WithEcdsa; |
| 840 | default: |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 841 | BOOST_THROW_EXCEPTION(Error("Unsupported key types")); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 844 | } |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 845 | |
| 846 | } // namespace security |
| 847 | } // namespace ndn |