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