Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 22 | */ |
| 23 | |
| 24 | #include "key-chain.hpp" |
| 25 | |
| 26 | #include "sec-public-info-sqlite3.hpp" |
| 27 | #include "sec-tpm-file.hpp" |
| 28 | |
| 29 | #ifdef NDN_CXX_HAVE_OSX_SECURITY |
| 30 | #include "sec-tpm-osx.hpp" |
| 31 | #endif |
| 32 | |
| 33 | #include "../util/random.hpp" |
| 34 | #include "../util/config-file.hpp" |
| 35 | |
| 36 | namespace ndn { |
| 37 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 38 | // Use a GUID as a magic number of KeyChain::DEFAULT_PREFIX identifier |
| 39 | const Name KeyChain::DEFAULT_PREFIX("/723821fd-f534-44b3-80d9-44bf5f58bbbb"); |
| 40 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 41 | const RsaKeyParams KeyChain::DEFAULT_KEY_PARAMS; |
| 42 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 43 | KeyChain::KeyChain() |
| 44 | : m_pib(0) |
| 45 | , m_tpm(0) |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 46 | , m_lastTimestamp(time::toUnixTimestamp(time::system_clock::now())) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 47 | { |
| 48 | |
| 49 | ConfigFile config; |
| 50 | const ConfigFile::Parsed& parsed = config.getParsedConfiguration(); |
| 51 | |
| 52 | std::string pibName; |
| 53 | try |
| 54 | { |
| 55 | pibName = parsed.get<std::string>("pib"); |
| 56 | } |
| 57 | catch (boost::property_tree::ptree_bad_path& error) |
| 58 | { |
| 59 | // pib is not specified, take the default |
| 60 | } |
| 61 | catch (boost::property_tree::ptree_bad_data& error) |
| 62 | { |
| 63 | throw ConfigFile::Error(error.what()); |
| 64 | } |
| 65 | |
| 66 | std::string tpmName; |
| 67 | try |
| 68 | { |
| 69 | tpmName = parsed.get<std::string>("tpm"); |
| 70 | } |
| 71 | catch (boost::property_tree::ptree_bad_path& error) |
| 72 | { |
| 73 | // tpm is not specified, take the default |
| 74 | } |
| 75 | catch (boost::property_tree::ptree_bad_data& error) |
| 76 | { |
| 77 | throw ConfigFile::Error(error.what()); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | if (pibName.empty() || pibName == "sqlite3") |
| 82 | m_pib = new SecPublicInfoSqlite3; |
| 83 | else |
| 84 | throw Error("PIB type '" + pibName + "' is not supported"); |
| 85 | |
| 86 | if (tpmName.empty()) |
| 87 | #if defined(NDN_CXX_HAVE_OSX_SECURITY) and defined(NDN_CXX_WITH_OSX_KEYCHAIN) |
| 88 | m_tpm = new SecTpmOsx(); |
| 89 | #else |
| 90 | m_tpm = new SecTpmFile(); |
| 91 | #endif // defined(NDN_CXX_HAVE_OSX_SECURITY) and defined(NDN_CXX_WITH_OSX_KEYCHAIN) |
| 92 | else if (tpmName == "osx-keychain") |
| 93 | #if defined(NDN_CXX_HAVE_OSX_SECURITY) |
| 94 | m_tpm = new SecTpmOsx(); |
| 95 | #else |
| 96 | throw Error("TPM type '" + tpmName + "' is not supported on this platform"); |
| 97 | #endif // NDN_CXX_HAVE_OSX_SECURITY |
| 98 | else if (tpmName == "file") |
| 99 | m_tpm = new SecTpmFile(); |
| 100 | else |
| 101 | throw Error("TPM type '" + tpmName + "' is not supported"); |
| 102 | } |
| 103 | |
| 104 | KeyChain::KeyChain(const std::string& pibName, |
| 105 | const std::string& tpmName) |
| 106 | : m_pib(0) |
| 107 | , m_tpm(0) |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 108 | , m_lastTimestamp(time::toUnixTimestamp(time::system_clock::now())) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 109 | { |
| 110 | if (pibName == "sqlite3") |
| 111 | m_pib = new SecPublicInfoSqlite3; |
| 112 | else |
| 113 | throw Error("PIB type '" + pibName + "' is not supported"); |
| 114 | |
| 115 | if (tpmName == "file") |
| 116 | m_tpm = new SecTpmFile; |
| 117 | #if defined(NDN_CXX_HAVE_OSX_SECURITY) |
| 118 | else if (tpmName == "osx-keychain") |
| 119 | m_tpm = new SecTpmOsx(); |
| 120 | #endif //NDN_CXX_HAVE_OSX_SECURITY |
| 121 | else |
| 122 | throw Error("TPM type '" + tpmName + "' is not supported"); |
| 123 | } |
| 124 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 125 | KeyChain::~KeyChain() |
| 126 | { |
| 127 | if (m_pib != 0) |
| 128 | delete m_pib; |
| 129 | |
| 130 | if (m_tpm != 0) |
| 131 | delete m_tpm; |
| 132 | } |
| 133 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 134 | Name |
| 135 | KeyChain::createIdentity(const Name& identityName, const KeyParams& params) |
| 136 | { |
| 137 | m_pib->addIdentity(identityName); |
| 138 | |
| 139 | Name keyName; |
| 140 | try |
| 141 | { |
| 142 | keyName = m_pib->getDefaultKeyNameForIdentity(identityName); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 143 | |
| 144 | shared_ptr<PublicKey> key = m_pib->getPublicKey(keyName); |
| 145 | |
| 146 | if (key->getKeyType() != params.getKeyType()) |
| 147 | { |
| 148 | keyName = generateKeyPair(identityName, true, params); |
| 149 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 150 | } |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 151 | } |
| 152 | catch (SecPublicInfo::Error& e) |
| 153 | { |
| 154 | keyName = generateKeyPair(identityName, true, params); |
| 155 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 156 | } |
| 157 | |
| 158 | Name certName; |
| 159 | try |
| 160 | { |
| 161 | certName = m_pib->getDefaultCertificateNameForKey(keyName); |
| 162 | } |
| 163 | catch (SecPublicInfo::Error& e) |
| 164 | { |
| 165 | shared_ptr<IdentityCertificate> selfCert = selfSign(keyName); |
| 166 | m_pib->addCertificateAsIdentityDefault(*selfCert); |
| 167 | certName = selfCert->getName(); |
| 168 | } |
| 169 | |
| 170 | return certName; |
| 171 | } |
| 172 | |
| 173 | Name |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 174 | KeyChain::generateRsaKeyPairAsDefault(const Name& identityName, bool isKsk, uint32_t keySize) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 175 | { |
| 176 | RsaKeyParams params(keySize); |
| 177 | |
| 178 | Name keyName = generateKeyPair(identityName, isKsk, params); |
| 179 | |
| 180 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 181 | |
| 182 | return keyName; |
| 183 | } |
| 184 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 185 | Name |
| 186 | KeyChain::generateEcdsaKeyPairAsDefault(const Name& identityName, bool isKsk, uint32_t keySize) |
| 187 | { |
| 188 | EcdsaKeyParams params(keySize); |
| 189 | |
| 190 | Name keyName = generateKeyPair(identityName, isKsk, params); |
| 191 | |
| 192 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 193 | |
| 194 | return keyName; |
| 195 | } |
| 196 | |
| 197 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 198 | shared_ptr<IdentityCertificate> |
| 199 | KeyChain::prepareUnsignedIdentityCertificate(const Name& keyName, |
| 200 | const Name& signingIdentity, |
| 201 | const time::system_clock::TimePoint& notBefore, |
| 202 | const time::system_clock::TimePoint& notAfter, |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 203 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 204 | const Name& certPrefix) |
| 205 | { |
| 206 | shared_ptr<PublicKey> publicKey; |
| 207 | try |
| 208 | { |
| 209 | publicKey = m_pib->getPublicKey(keyName); |
| 210 | } |
| 211 | catch (SecPublicInfo::Error& e) |
| 212 | { |
| 213 | return shared_ptr<IdentityCertificate>(); |
| 214 | } |
| 215 | |
| 216 | return prepareUnsignedIdentityCertificate(keyName, *publicKey, signingIdentity, |
| 217 | notBefore, notAfter, |
| 218 | subjectDescription, certPrefix); |
| 219 | } |
| 220 | |
| 221 | shared_ptr<IdentityCertificate> |
| 222 | KeyChain::prepareUnsignedIdentityCertificate(const Name& keyName, |
| 223 | const PublicKey& publicKey, |
| 224 | const Name& signingIdentity, |
| 225 | const time::system_clock::TimePoint& notBefore, |
| 226 | const time::system_clock::TimePoint& notAfter, |
| 227 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 228 | const Name& certPrefix) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 229 | { |
| 230 | if (keyName.size() < 1) |
| 231 | return shared_ptr<IdentityCertificate>(); |
| 232 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 233 | std::string keyIdPrefix = keyName.get(-1).toUri().substr(0, 4); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 234 | if (keyIdPrefix != "ksk-" && keyIdPrefix != "dsk-") |
| 235 | return shared_ptr<IdentityCertificate>(); |
| 236 | |
| 237 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
| 238 | Name certName; |
| 239 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 240 | if (certPrefix == KeyChain::DEFAULT_PREFIX) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 241 | { |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 242 | // No certificate prefix hint, infer the prefix |
| 243 | if (signingIdentity.isPrefixOf(keyName)) |
| 244 | certName.append(signingIdentity) |
| 245 | .append("KEY") |
| 246 | .append(keyName.getSubName(signingIdentity.size())) |
| 247 | .append("ID-CERT") |
| 248 | .appendVersion(); |
| 249 | else |
| 250 | certName.append(keyName.getPrefix(-1)) |
| 251 | .append("KEY") |
| 252 | .append(keyName.get(-1)) |
| 253 | .append("ID-CERT") |
| 254 | .appendVersion(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 255 | } |
| 256 | else |
| 257 | { |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 258 | // cert prefix hint is supplied, determine the cert name. |
| 259 | if (certPrefix.isPrefixOf(keyName) && certPrefix != keyName) |
| 260 | certName.append(certPrefix) |
| 261 | .append("KEY") |
| 262 | .append(keyName.getSubName(certPrefix.size())) |
| 263 | .append("ID-CERT") |
| 264 | .appendVersion(); |
| 265 | else |
| 266 | return shared_ptr<IdentityCertificate>(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 269 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 270 | certificate->setName(certName); |
| 271 | certificate->setNotBefore(notBefore); |
| 272 | certificate->setNotAfter(notAfter); |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 273 | certificate->setPublicKeyInfo(publicKey); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 274 | |
| 275 | if (subjectDescription.empty()) |
| 276 | { |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 277 | CertificateSubjectDescription subjectName(oid::ATTRIBUTE_NAME, keyName.getPrefix(-1).toUri()); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 278 | certificate->addSubjectDescription(subjectName); |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | std::vector<CertificateSubjectDescription>::const_iterator sdIt = |
| 283 | subjectDescription.begin(); |
| 284 | std::vector<CertificateSubjectDescription>::const_iterator sdEnd = |
| 285 | subjectDescription.end(); |
| 286 | for(; sdIt != sdEnd; sdIt++) |
| 287 | certificate->addSubjectDescription(*sdIt); |
| 288 | } |
| 289 | |
| 290 | certificate->encode(); |
| 291 | |
| 292 | return certificate; |
| 293 | } |
| 294 | |
| 295 | Signature |
| 296 | KeyChain::sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName) |
| 297 | { |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 298 | shared_ptr<IdentityCertificate> certificate = m_pib->getCertificate(certificateName); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 299 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 300 | shared_ptr<SignatureWithPublicKey> sig = |
| 301 | determineSignatureWithPublicKey(certificate->getPublicKeyInfo().getKeyType()); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 302 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 303 | if (!static_cast<bool>(sig)) |
| 304 | throw SecTpm::Error("unknown key type"); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 305 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 306 | sig->setKeyLocator(certificate->getName().getPrefix(-1)); |
| 307 | // For temporary usage, we support SHA256 only, but will support more. |
| 308 | sig->setValue(m_tpm->signInTpm(buffer, bufferLength, |
| 309 | certificate->getPublicKeyName(), |
| 310 | DIGEST_ALGORITHM_SHA256)); |
| 311 | |
| 312 | return *sig; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | shared_ptr<IdentityCertificate> |
| 316 | KeyChain::selfSign(const Name& keyName) |
| 317 | { |
| 318 | shared_ptr<PublicKey> pubKey; |
| 319 | try |
| 320 | { |
| 321 | pubKey = m_pib->getPublicKey(keyName); // may throw an exception. |
| 322 | } |
| 323 | catch (SecPublicInfo::Error& e) |
| 324 | { |
| 325 | return shared_ptr<IdentityCertificate>(); |
| 326 | } |
| 327 | |
| 328 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
| 329 | |
| 330 | Name certificateName = keyName.getPrefix(-1); |
| 331 | certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion(); |
| 332 | |
| 333 | certificate->setName(certificateName); |
| 334 | certificate->setNotBefore(time::system_clock::now()); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 335 | certificate->setNotAfter(time::system_clock::now() + time::days(7300)); // ~20 years |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 336 | certificate->setPublicKeyInfo(*pubKey); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 337 | certificate->addSubjectDescription(CertificateSubjectDescription(oid::ATTRIBUTE_NAME, |
| 338 | keyName.toUri())); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 339 | certificate->encode(); |
| 340 | |
| 341 | selfSign(*certificate); |
| 342 | return certificate; |
| 343 | } |
| 344 | |
| 345 | void |
| 346 | KeyChain::selfSign(IdentityCertificate& cert) |
| 347 | { |
| 348 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(cert.getName()); |
| 349 | if (!m_tpm->doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 350 | throw SecTpm::Error("Private key does not exist"); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 351 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 352 | shared_ptr<SignatureWithPublicKey> sig = |
| 353 | determineSignatureWithPublicKey(cert.getPublicKeyInfo().getKeyType()); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 354 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 355 | if (!static_cast<bool>(sig)) |
| 356 | throw SecTpm::Error("unknown key type"); |
| 357 | |
| 358 | sig->setKeyLocator(cert.getName().getPrefix(-1)); // implicit conversion should take care |
| 359 | signPacketWrapper(cert, *sig, keyName, DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | shared_ptr<SecuredBag> |
| 363 | KeyChain::exportIdentity(const Name& identity, const std::string& passwordStr) |
| 364 | { |
| 365 | if (!m_pib->doesIdentityExist(identity)) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 366 | throw SecPublicInfo::Error("Identity does not exist"); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 367 | |
| 368 | Name keyName = m_pib->getDefaultKeyNameForIdentity(identity); |
| 369 | |
| 370 | ConstBufferPtr pkcs5; |
| 371 | try |
| 372 | { |
| 373 | pkcs5 = m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, passwordStr); |
| 374 | } |
| 375 | catch (SecTpm::Error& e) |
| 376 | { |
| 377 | throw SecPublicInfo::Error("Fail to export PKCS5 of private key"); |
| 378 | } |
| 379 | |
| 380 | shared_ptr<IdentityCertificate> cert; |
| 381 | try |
| 382 | { |
| 383 | cert = m_pib->getCertificate(m_pib->getDefaultCertificateNameForKey(keyName)); |
| 384 | } |
| 385 | catch (SecPublicInfo::Error& e) |
| 386 | { |
| 387 | cert = selfSign(keyName); |
| 388 | m_pib->addCertificateAsIdentityDefault(*cert); |
| 389 | } |
| 390 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 391 | // make_shared on OSX 10.9 has some strange problem here |
| 392 | shared_ptr<SecuredBag> secureBag(new SecuredBag(*cert, pkcs5)); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 393 | |
| 394 | return secureBag; |
| 395 | } |
| 396 | |
| 397 | void |
| 398 | KeyChain::importIdentity(const SecuredBag& securedBag, const std::string& passwordStr) |
| 399 | { |
| 400 | Name certificateName = securedBag.getCertificate().getName(); |
| 401 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
| 402 | Name identity = keyName.getPrefix(-1); |
| 403 | |
| 404 | // Add identity |
| 405 | m_pib->addIdentity(identity); |
| 406 | |
| 407 | // Add key |
| 408 | m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, |
| 409 | securedBag.getKey()->buf(), |
| 410 | securedBag.getKey()->size(), |
| 411 | passwordStr); |
| 412 | |
| 413 | shared_ptr<PublicKey> pubKey = m_tpm->getPublicKeyFromTpm(keyName.toUri()); |
| 414 | // HACK! We should set key type according to the pkcs8 info. |
| 415 | m_pib->addPublicKey(keyName, KEY_TYPE_RSA, *pubKey); |
| 416 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 417 | |
| 418 | // Add cert |
| 419 | m_pib->addCertificateAsIdentityDefault(securedBag.getCertificate()); |
| 420 | } |
| 421 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 422 | shared_ptr<SignatureWithPublicKey> |
| 423 | KeyChain::determineSignatureWithPublicKey(KeyType keyType, DigestAlgorithm digestAlgorithm) |
| 424 | { |
| 425 | switch (keyType) |
| 426 | { |
| 427 | case KEY_TYPE_RSA: |
| 428 | { |
| 429 | // For temporary usage, we support SHA256 only, but will support more. |
| 430 | if (digestAlgorithm != DIGEST_ALGORITHM_SHA256) |
| 431 | return shared_ptr<SignatureWithPublicKey>(); |
| 432 | |
| 433 | return make_shared<SignatureSha256WithRsa>(); |
| 434 | } |
| 435 | case KEY_TYPE_ECDSA: |
| 436 | { |
| 437 | // For temporary usage, we support SHA256 only, but will support more. |
| 438 | if (digestAlgorithm != DIGEST_ALGORITHM_SHA256) |
| 439 | return shared_ptr<SignatureWithPublicKey>(); |
| 440 | |
| 441 | return make_shared<SignatureSha256WithEcdsa>(); |
| 442 | } |
| 443 | default: |
| 444 | return shared_ptr<SignatureWithPublicKey>(); |
| 445 | } |
| 446 | } |
| 447 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 448 | void |
| 449 | KeyChain::setDefaultCertificateInternal() |
| 450 | { |
| 451 | m_pib->refreshDefaultCertificate(); |
| 452 | |
Alexander Afanasyev | aab7966 | 2014-07-07 17:35:34 -0700 | [diff] [blame] | 453 | if (!static_cast<bool>(m_pib->getDefaultCertificate())) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 454 | { |
| 455 | Name defaultIdentity; |
| 456 | try |
| 457 | { |
| 458 | defaultIdentity = m_pib->getDefaultIdentity(); |
| 459 | } |
| 460 | catch (SecPublicInfo::Error& e) |
| 461 | { |
| 462 | uint32_t random = random::generateWord32(); |
| 463 | defaultIdentity.append("tmp-identity") |
| 464 | .append(reinterpret_cast<uint8_t*>(&random), 4); |
| 465 | } |
| 466 | createIdentity(defaultIdentity); |
| 467 | m_pib->setDefaultIdentity(defaultIdentity); |
| 468 | m_pib->refreshDefaultCertificate(); |
| 469 | } |
| 470 | } |
| 471 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 472 | Name |
| 473 | KeyChain::generateKeyPair(const Name& identityName, bool isKsk, const KeyParams& params) |
| 474 | { |
| 475 | Name keyName = m_pib->getNewKeyName(identityName, isKsk); |
| 476 | |
| 477 | m_tpm->generateKeyPairInTpm(keyName.toUri(), params); |
| 478 | |
| 479 | shared_ptr<PublicKey> pubKey = m_tpm->getPublicKeyFromTpm(keyName.toUri()); |
| 480 | m_pib->addKey(keyName, *pubKey); |
| 481 | |
| 482 | return keyName; |
| 483 | } |
| 484 | |
| 485 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 486 | KeyChain::signPacketWrapper(Data& data, const Signature& signature, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 487 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 488 | { |
| 489 | data.setSignature(signature); |
| 490 | |
| 491 | EncodingBuffer encoder; |
| 492 | data.wireEncode(encoder, true); |
| 493 | |
| 494 | Block signatureValue = m_tpm->signInTpm(encoder.buf(), encoder.size(), |
| 495 | keyName, digestAlgorithm); |
| 496 | data.wireEncode(encoder, signatureValue); |
| 497 | } |
| 498 | |
| 499 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 500 | KeyChain::signPacketWrapper(Interest& interest, const Signature& signature, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 501 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 502 | { |
| 503 | time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now()); |
| 504 | if (timestamp <= m_lastTimestamp) |
| 505 | { |
| 506 | timestamp = m_lastTimestamp + time::milliseconds(1); |
| 507 | } |
| 508 | |
| 509 | Name signedName = interest.getName(); |
| 510 | signedName |
| 511 | .append(name::Component::fromNumber(timestamp.count())) // timestamp |
| 512 | .append(name::Component::fromNumber(random::generateWord64())) // nonce |
| 513 | .append(signature.getInfo()); // signatureInfo |
| 514 | |
| 515 | Block sigValue = m_tpm->signInTpm(signedName.wireEncode().value(), |
| 516 | signedName.wireEncode().value_size(), |
| 517 | keyName, |
| 518 | digestAlgorithm); |
| 519 | sigValue.encode(); |
| 520 | signedName.append(sigValue); // signatureValue |
| 521 | interest.setName(signedName); |
| 522 | } |
| 523 | |
| 524 | Signature |
| 525 | KeyChain::signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName) |
| 526 | { |
| 527 | Name signingCertificateName; |
| 528 | try |
| 529 | { |
| 530 | signingCertificateName = m_pib->getDefaultCertificateNameForIdentity(identityName); |
| 531 | } |
| 532 | catch (SecPublicInfo::Error& e) |
| 533 | { |
| 534 | signingCertificateName = createIdentity(identityName); |
| 535 | // Ideally, no exception will be thrown out, unless something goes wrong in the TPM, which |
| 536 | // is a fatal error. |
| 537 | } |
| 538 | |
| 539 | // We either get or create the signing certificate, sign data! (no exception unless fatal error |
| 540 | // in TPM) |
| 541 | return sign(buffer, bufferLength, signingCertificateName); |
| 542 | } |
| 543 | |
| 544 | void |
| 545 | KeyChain::signWithSha256(Data& data) |
| 546 | { |
| 547 | DigestSha256 sig; |
| 548 | data.setSignature(sig); |
| 549 | |
| 550 | Block sigValue(Tlv::SignatureValue, |
| 551 | crypto::sha256(data.wireEncode().value(), |
| 552 | data.wireEncode().value_size() - |
| 553 | data.getSignature().getValue().size())); |
| 554 | data.setSignatureValue(sigValue); |
| 555 | } |
| 556 | |
| 557 | void |
| 558 | KeyChain::deleteCertificate(const Name& certificateName) |
| 559 | { |
| 560 | try |
| 561 | { |
| 562 | if (m_pib->getDefaultCertificateName() == certificateName) |
| 563 | return; |
| 564 | } |
| 565 | catch (SecPublicInfo::Error& e) |
| 566 | { |
| 567 | // Not a real error, just try to delete the certificate |
| 568 | } |
| 569 | |
| 570 | m_pib->deleteCertificateInfo(certificateName); |
| 571 | } |
| 572 | |
| 573 | void |
| 574 | KeyChain::deleteKey(const Name& keyName) |
| 575 | { |
| 576 | try |
| 577 | { |
| 578 | if (m_pib->getDefaultKeyNameForIdentity(m_pib->getDefaultIdentity()) == keyName) |
| 579 | return; |
| 580 | } |
| 581 | catch (SecPublicInfo::Error& e) |
| 582 | { |
| 583 | // Not a real error, just try to delete the key |
| 584 | } |
| 585 | |
| 586 | m_pib->deletePublicKeyInfo(keyName); |
| 587 | m_tpm->deleteKeyPairInTpm(keyName); |
| 588 | } |
| 589 | |
| 590 | void |
| 591 | KeyChain::deleteIdentity(const Name& identity) |
| 592 | { |
| 593 | try |
| 594 | { |
| 595 | if (m_pib->getDefaultIdentity() == identity) |
| 596 | return; |
| 597 | } |
| 598 | catch (SecPublicInfo::Error& e) |
| 599 | { |
| 600 | // Not a real error, just try to delete the identity |
| 601 | } |
| 602 | |
| 603 | std::vector<Name> nameList; |
| 604 | m_pib->getAllKeyNamesOfIdentity(identity, nameList, true); |
| 605 | m_pib->getAllKeyNamesOfIdentity(identity, nameList, false); |
| 606 | |
| 607 | m_pib->deleteIdentityInfo(identity); |
| 608 | |
| 609 | std::vector<Name>::const_iterator it = nameList.begin(); |
| 610 | for(; it != nameList.end(); it++) |
| 611 | m_tpm->deleteKeyPairInTpm(*it); |
| 612 | } |
| 613 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 614 | } |