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 | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 125 | Name |
| 126 | KeyChain::createIdentity(const Name& identityName, const KeyParams& params) |
| 127 | { |
| 128 | m_pib->addIdentity(identityName); |
| 129 | |
| 130 | Name keyName; |
| 131 | try |
| 132 | { |
| 133 | keyName = m_pib->getDefaultKeyNameForIdentity(identityName); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 134 | |
| 135 | shared_ptr<PublicKey> key = m_pib->getPublicKey(keyName); |
| 136 | |
| 137 | if (key->getKeyType() != params.getKeyType()) |
| 138 | { |
| 139 | keyName = generateKeyPair(identityName, true, params); |
| 140 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 141 | } |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 142 | } |
| 143 | catch (SecPublicInfo::Error& e) |
| 144 | { |
| 145 | keyName = generateKeyPair(identityName, true, params); |
| 146 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 147 | } |
| 148 | |
| 149 | Name certName; |
| 150 | try |
| 151 | { |
| 152 | certName = m_pib->getDefaultCertificateNameForKey(keyName); |
| 153 | } |
| 154 | catch (SecPublicInfo::Error& e) |
| 155 | { |
| 156 | shared_ptr<IdentityCertificate> selfCert = selfSign(keyName); |
| 157 | m_pib->addCertificateAsIdentityDefault(*selfCert); |
| 158 | certName = selfCert->getName(); |
| 159 | } |
| 160 | |
| 161 | return certName; |
| 162 | } |
| 163 | |
| 164 | Name |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 165 | KeyChain::generateRsaKeyPairAsDefault(const Name& identityName, bool isKsk, uint32_t keySize) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 166 | { |
| 167 | RsaKeyParams params(keySize); |
| 168 | |
| 169 | Name keyName = generateKeyPair(identityName, isKsk, params); |
| 170 | |
| 171 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 172 | |
| 173 | return keyName; |
| 174 | } |
| 175 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 176 | Name |
| 177 | KeyChain::generateEcdsaKeyPairAsDefault(const Name& identityName, bool isKsk, uint32_t keySize) |
| 178 | { |
| 179 | EcdsaKeyParams params(keySize); |
| 180 | |
| 181 | Name keyName = generateKeyPair(identityName, isKsk, params); |
| 182 | |
| 183 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 184 | |
| 185 | return keyName; |
| 186 | } |
| 187 | |
| 188 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 189 | shared_ptr<IdentityCertificate> |
| 190 | KeyChain::prepareUnsignedIdentityCertificate(const Name& keyName, |
| 191 | const Name& signingIdentity, |
| 192 | const time::system_clock::TimePoint& notBefore, |
| 193 | const time::system_clock::TimePoint& notAfter, |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 194 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 195 | const Name& certPrefix) |
| 196 | { |
| 197 | shared_ptr<PublicKey> publicKey; |
| 198 | try |
| 199 | { |
| 200 | publicKey = m_pib->getPublicKey(keyName); |
| 201 | } |
| 202 | catch (SecPublicInfo::Error& e) |
| 203 | { |
| 204 | return shared_ptr<IdentityCertificate>(); |
| 205 | } |
| 206 | |
| 207 | return prepareUnsignedIdentityCertificate(keyName, *publicKey, signingIdentity, |
| 208 | notBefore, notAfter, |
| 209 | subjectDescription, certPrefix); |
| 210 | } |
| 211 | |
| 212 | shared_ptr<IdentityCertificate> |
| 213 | KeyChain::prepareUnsignedIdentityCertificate(const Name& keyName, |
| 214 | const PublicKey& publicKey, |
| 215 | const Name& signingIdentity, |
| 216 | const time::system_clock::TimePoint& notBefore, |
| 217 | const time::system_clock::TimePoint& notAfter, |
| 218 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 219 | const Name& certPrefix) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 220 | { |
| 221 | if (keyName.size() < 1) |
| 222 | return shared_ptr<IdentityCertificate>(); |
| 223 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 224 | std::string keyIdPrefix = keyName.get(-1).toUri().substr(0, 4); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 225 | if (keyIdPrefix != "ksk-" && keyIdPrefix != "dsk-") |
| 226 | return shared_ptr<IdentityCertificate>(); |
| 227 | |
| 228 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
| 229 | Name certName; |
| 230 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 231 | if (certPrefix == KeyChain::DEFAULT_PREFIX) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 232 | { |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 233 | // No certificate prefix hint, infer the prefix |
| 234 | if (signingIdentity.isPrefixOf(keyName)) |
| 235 | certName.append(signingIdentity) |
| 236 | .append("KEY") |
| 237 | .append(keyName.getSubName(signingIdentity.size())) |
| 238 | .append("ID-CERT") |
| 239 | .appendVersion(); |
| 240 | else |
| 241 | certName.append(keyName.getPrefix(-1)) |
| 242 | .append("KEY") |
| 243 | .append(keyName.get(-1)) |
| 244 | .append("ID-CERT") |
| 245 | .appendVersion(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 246 | } |
| 247 | else |
| 248 | { |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 249 | // cert prefix hint is supplied, determine the cert name. |
| 250 | if (certPrefix.isPrefixOf(keyName) && certPrefix != keyName) |
| 251 | certName.append(certPrefix) |
| 252 | .append("KEY") |
| 253 | .append(keyName.getSubName(certPrefix.size())) |
| 254 | .append("ID-CERT") |
| 255 | .appendVersion(); |
| 256 | else |
| 257 | return shared_ptr<IdentityCertificate>(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 260 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 261 | certificate->setName(certName); |
| 262 | certificate->setNotBefore(notBefore); |
| 263 | certificate->setNotAfter(notAfter); |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 264 | certificate->setPublicKeyInfo(publicKey); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 265 | |
| 266 | if (subjectDescription.empty()) |
| 267 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 268 | // OID 2.5.4.41 is the oid of subject name. |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 269 | CertificateSubjectDescription subjectName("2.5.4.41", keyName.getPrefix(-1).toUri()); |
| 270 | certificate->addSubjectDescription(subjectName); |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | std::vector<CertificateSubjectDescription>::const_iterator sdIt = |
| 275 | subjectDescription.begin(); |
| 276 | std::vector<CertificateSubjectDescription>::const_iterator sdEnd = |
| 277 | subjectDescription.end(); |
| 278 | for(; sdIt != sdEnd; sdIt++) |
| 279 | certificate->addSubjectDescription(*sdIt); |
| 280 | } |
| 281 | |
| 282 | certificate->encode(); |
| 283 | |
| 284 | return certificate; |
| 285 | } |
| 286 | |
| 287 | Signature |
| 288 | KeyChain::sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName) |
| 289 | { |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 290 | shared_ptr<IdentityCertificate> certificate = m_pib->getCertificate(certificateName); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 291 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 292 | shared_ptr<SignatureWithPublicKey> sig = |
| 293 | determineSignatureWithPublicKey(certificate->getPublicKeyInfo().getKeyType()); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 294 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 295 | if (!static_cast<bool>(sig)) |
| 296 | throw SecTpm::Error("unknown key type"); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 297 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 298 | sig->setKeyLocator(certificate->getName().getPrefix(-1)); |
| 299 | // For temporary usage, we support SHA256 only, but will support more. |
| 300 | sig->setValue(m_tpm->signInTpm(buffer, bufferLength, |
| 301 | certificate->getPublicKeyName(), |
| 302 | DIGEST_ALGORITHM_SHA256)); |
| 303 | |
| 304 | return *sig; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | shared_ptr<IdentityCertificate> |
| 308 | KeyChain::selfSign(const Name& keyName) |
| 309 | { |
| 310 | shared_ptr<PublicKey> pubKey; |
| 311 | try |
| 312 | { |
| 313 | pubKey = m_pib->getPublicKey(keyName); // may throw an exception. |
| 314 | } |
| 315 | catch (SecPublicInfo::Error& e) |
| 316 | { |
| 317 | return shared_ptr<IdentityCertificate>(); |
| 318 | } |
| 319 | |
| 320 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
| 321 | |
| 322 | Name certificateName = keyName.getPrefix(-1); |
| 323 | certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion(); |
| 324 | |
| 325 | certificate->setName(certificateName); |
| 326 | certificate->setNotBefore(time::system_clock::now()); |
| 327 | certificate->setNotAfter(time::system_clock::now() + time::days(7300)/* ~20 years*/); |
| 328 | certificate->setPublicKeyInfo(*pubKey); |
| 329 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
| 330 | certificate->encode(); |
| 331 | |
| 332 | selfSign(*certificate); |
| 333 | return certificate; |
| 334 | } |
| 335 | |
| 336 | void |
| 337 | KeyChain::selfSign(IdentityCertificate& cert) |
| 338 | { |
| 339 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(cert.getName()); |
| 340 | if (!m_tpm->doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 341 | throw SecTpm::Error("Private key does not exist"); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 342 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 343 | shared_ptr<SignatureWithPublicKey> sig = |
| 344 | determineSignatureWithPublicKey(cert.getPublicKeyInfo().getKeyType()); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 345 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 346 | if (!static_cast<bool>(sig)) |
| 347 | throw SecTpm::Error("unknown key type"); |
| 348 | |
| 349 | sig->setKeyLocator(cert.getName().getPrefix(-1)); // implicit conversion should take care |
| 350 | signPacketWrapper(cert, *sig, keyName, DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | shared_ptr<SecuredBag> |
| 354 | KeyChain::exportIdentity(const Name& identity, const std::string& passwordStr) |
| 355 | { |
| 356 | if (!m_pib->doesIdentityExist(identity)) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 357 | throw SecPublicInfo::Error("Identity does not exist"); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 358 | |
| 359 | Name keyName = m_pib->getDefaultKeyNameForIdentity(identity); |
| 360 | |
| 361 | ConstBufferPtr pkcs5; |
| 362 | try |
| 363 | { |
| 364 | pkcs5 = m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, passwordStr); |
| 365 | } |
| 366 | catch (SecTpm::Error& e) |
| 367 | { |
| 368 | throw SecPublicInfo::Error("Fail to export PKCS5 of private key"); |
| 369 | } |
| 370 | |
| 371 | shared_ptr<IdentityCertificate> cert; |
| 372 | try |
| 373 | { |
| 374 | cert = m_pib->getCertificate(m_pib->getDefaultCertificateNameForKey(keyName)); |
| 375 | } |
| 376 | catch (SecPublicInfo::Error& e) |
| 377 | { |
| 378 | cert = selfSign(keyName); |
| 379 | m_pib->addCertificateAsIdentityDefault(*cert); |
| 380 | } |
| 381 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 382 | // make_shared on OSX 10.9 has some strange problem here |
| 383 | shared_ptr<SecuredBag> secureBag(new SecuredBag(*cert, pkcs5)); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 384 | |
| 385 | return secureBag; |
| 386 | } |
| 387 | |
| 388 | void |
| 389 | KeyChain::importIdentity(const SecuredBag& securedBag, const std::string& passwordStr) |
| 390 | { |
| 391 | Name certificateName = securedBag.getCertificate().getName(); |
| 392 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
| 393 | Name identity = keyName.getPrefix(-1); |
| 394 | |
| 395 | // Add identity |
| 396 | m_pib->addIdentity(identity); |
| 397 | |
| 398 | // Add key |
| 399 | m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, |
| 400 | securedBag.getKey()->buf(), |
| 401 | securedBag.getKey()->size(), |
| 402 | passwordStr); |
| 403 | |
| 404 | shared_ptr<PublicKey> pubKey = m_tpm->getPublicKeyFromTpm(keyName.toUri()); |
| 405 | // HACK! We should set key type according to the pkcs8 info. |
| 406 | m_pib->addPublicKey(keyName, KEY_TYPE_RSA, *pubKey); |
| 407 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 408 | |
| 409 | // Add cert |
| 410 | m_pib->addCertificateAsIdentityDefault(securedBag.getCertificate()); |
| 411 | } |
| 412 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 413 | shared_ptr<SignatureWithPublicKey> |
| 414 | KeyChain::determineSignatureWithPublicKey(KeyType keyType, DigestAlgorithm digestAlgorithm) |
| 415 | { |
| 416 | switch (keyType) |
| 417 | { |
| 418 | case KEY_TYPE_RSA: |
| 419 | { |
| 420 | // For temporary usage, we support SHA256 only, but will support more. |
| 421 | if (digestAlgorithm != DIGEST_ALGORITHM_SHA256) |
| 422 | return shared_ptr<SignatureWithPublicKey>(); |
| 423 | |
| 424 | return make_shared<SignatureSha256WithRsa>(); |
| 425 | } |
| 426 | case KEY_TYPE_ECDSA: |
| 427 | { |
| 428 | // For temporary usage, we support SHA256 only, but will support more. |
| 429 | if (digestAlgorithm != DIGEST_ALGORITHM_SHA256) |
| 430 | return shared_ptr<SignatureWithPublicKey>(); |
| 431 | |
| 432 | return make_shared<SignatureSha256WithEcdsa>(); |
| 433 | } |
| 434 | default: |
| 435 | return shared_ptr<SignatureWithPublicKey>(); |
| 436 | } |
| 437 | } |
| 438 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 439 | void |
| 440 | KeyChain::setDefaultCertificateInternal() |
| 441 | { |
| 442 | m_pib->refreshDefaultCertificate(); |
| 443 | |
| 444 | if (!static_cast<bool>(m_pib->defaultCertificate())) |
| 445 | { |
| 446 | Name defaultIdentity; |
| 447 | try |
| 448 | { |
| 449 | defaultIdentity = m_pib->getDefaultIdentity(); |
| 450 | } |
| 451 | catch (SecPublicInfo::Error& e) |
| 452 | { |
| 453 | uint32_t random = random::generateWord32(); |
| 454 | defaultIdentity.append("tmp-identity") |
| 455 | .append(reinterpret_cast<uint8_t*>(&random), 4); |
| 456 | } |
| 457 | createIdentity(defaultIdentity); |
| 458 | m_pib->setDefaultIdentity(defaultIdentity); |
| 459 | m_pib->refreshDefaultCertificate(); |
| 460 | } |
| 461 | } |
| 462 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 463 | Name |
| 464 | KeyChain::generateKeyPair(const Name& identityName, bool isKsk, const KeyParams& params) |
| 465 | { |
| 466 | Name keyName = m_pib->getNewKeyName(identityName, isKsk); |
| 467 | |
| 468 | m_tpm->generateKeyPairInTpm(keyName.toUri(), params); |
| 469 | |
| 470 | shared_ptr<PublicKey> pubKey = m_tpm->getPublicKeyFromTpm(keyName.toUri()); |
| 471 | m_pib->addKey(keyName, *pubKey); |
| 472 | |
| 473 | return keyName; |
| 474 | } |
| 475 | |
| 476 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 477 | KeyChain::signPacketWrapper(Data& data, const Signature& signature, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 478 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 479 | { |
| 480 | data.setSignature(signature); |
| 481 | |
| 482 | EncodingBuffer encoder; |
| 483 | data.wireEncode(encoder, true); |
| 484 | |
| 485 | Block signatureValue = m_tpm->signInTpm(encoder.buf(), encoder.size(), |
| 486 | keyName, digestAlgorithm); |
| 487 | data.wireEncode(encoder, signatureValue); |
| 488 | } |
| 489 | |
| 490 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 491 | KeyChain::signPacketWrapper(Interest& interest, const Signature& signature, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 492 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 493 | { |
| 494 | time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now()); |
| 495 | if (timestamp <= m_lastTimestamp) |
| 496 | { |
| 497 | timestamp = m_lastTimestamp + time::milliseconds(1); |
| 498 | } |
| 499 | |
| 500 | Name signedName = interest.getName(); |
| 501 | signedName |
| 502 | .append(name::Component::fromNumber(timestamp.count())) // timestamp |
| 503 | .append(name::Component::fromNumber(random::generateWord64())) // nonce |
| 504 | .append(signature.getInfo()); // signatureInfo |
| 505 | |
| 506 | Block sigValue = m_tpm->signInTpm(signedName.wireEncode().value(), |
| 507 | signedName.wireEncode().value_size(), |
| 508 | keyName, |
| 509 | digestAlgorithm); |
| 510 | sigValue.encode(); |
| 511 | signedName.append(sigValue); // signatureValue |
| 512 | interest.setName(signedName); |
| 513 | } |
| 514 | |
| 515 | Signature |
| 516 | KeyChain::signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName) |
| 517 | { |
| 518 | Name signingCertificateName; |
| 519 | try |
| 520 | { |
| 521 | signingCertificateName = m_pib->getDefaultCertificateNameForIdentity(identityName); |
| 522 | } |
| 523 | catch (SecPublicInfo::Error& e) |
| 524 | { |
| 525 | signingCertificateName = createIdentity(identityName); |
| 526 | // Ideally, no exception will be thrown out, unless something goes wrong in the TPM, which |
| 527 | // is a fatal error. |
| 528 | } |
| 529 | |
| 530 | // We either get or create the signing certificate, sign data! (no exception unless fatal error |
| 531 | // in TPM) |
| 532 | return sign(buffer, bufferLength, signingCertificateName); |
| 533 | } |
| 534 | |
| 535 | void |
| 536 | KeyChain::signWithSha256(Data& data) |
| 537 | { |
| 538 | DigestSha256 sig; |
| 539 | data.setSignature(sig); |
| 540 | |
| 541 | Block sigValue(Tlv::SignatureValue, |
| 542 | crypto::sha256(data.wireEncode().value(), |
| 543 | data.wireEncode().value_size() - |
| 544 | data.getSignature().getValue().size())); |
| 545 | data.setSignatureValue(sigValue); |
| 546 | } |
| 547 | |
| 548 | void |
| 549 | KeyChain::deleteCertificate(const Name& certificateName) |
| 550 | { |
| 551 | try |
| 552 | { |
| 553 | if (m_pib->getDefaultCertificateName() == certificateName) |
| 554 | return; |
| 555 | } |
| 556 | catch (SecPublicInfo::Error& e) |
| 557 | { |
| 558 | // Not a real error, just try to delete the certificate |
| 559 | } |
| 560 | |
| 561 | m_pib->deleteCertificateInfo(certificateName); |
| 562 | } |
| 563 | |
| 564 | void |
| 565 | KeyChain::deleteKey(const Name& keyName) |
| 566 | { |
| 567 | try |
| 568 | { |
| 569 | if (m_pib->getDefaultKeyNameForIdentity(m_pib->getDefaultIdentity()) == keyName) |
| 570 | return; |
| 571 | } |
| 572 | catch (SecPublicInfo::Error& e) |
| 573 | { |
| 574 | // Not a real error, just try to delete the key |
| 575 | } |
| 576 | |
| 577 | m_pib->deletePublicKeyInfo(keyName); |
| 578 | m_tpm->deleteKeyPairInTpm(keyName); |
| 579 | } |
| 580 | |
| 581 | void |
| 582 | KeyChain::deleteIdentity(const Name& identity) |
| 583 | { |
| 584 | try |
| 585 | { |
| 586 | if (m_pib->getDefaultIdentity() == identity) |
| 587 | return; |
| 588 | } |
| 589 | catch (SecPublicInfo::Error& e) |
| 590 | { |
| 591 | // Not a real error, just try to delete the identity |
| 592 | } |
| 593 | |
| 594 | std::vector<Name> nameList; |
| 595 | m_pib->getAllKeyNamesOfIdentity(identity, nameList, true); |
| 596 | m_pib->getAllKeyNamesOfIdentity(identity, nameList, false); |
| 597 | |
| 598 | m_pib->deleteIdentityInfo(identity); |
| 599 | |
| 600 | std::vector<Name>::const_iterator it = nameList.begin(); |
| 601 | for(; it != nameList.end(); it++) |
| 602 | m_tpm->deleteKeyPairInTpm(*it); |
| 603 | } |
| 604 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 605 | } |