Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 15 | #ifndef NDN_SECURITY_KEY_CHAIN_HPP |
| 16 | #define NDN_SECURITY_KEY_CHAIN_HPP |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 17 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 18 | #include "identity-certificate.hpp" |
| 19 | #include "public-key.hpp" |
| 20 | #include "signature-sha256-with-rsa.hpp" |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 21 | #include "signature-sha256.hpp" |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 22 | #include "secured-bag.hpp" |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 23 | #include "../interest.hpp" |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 24 | #include "../util/random.hpp" |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 25 | #include "../util/crypto.hpp" |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 26 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 27 | //PublicInfo |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 28 | #include "sec-public-info-sqlite3.hpp" |
| 29 | #include "sec-public-info-memory.hpp" |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 30 | //TPM |
| 31 | #include "sec-tpm-file.hpp" |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 32 | #include "sec-tpm-memory.hpp" |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 34 | #ifdef NDN_CXX_HAVE_OSX_SECURITY |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 35 | #include "sec-tpm-osx.hpp" |
| 36 | #endif |
| 37 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 38 | |
| 39 | namespace ndn { |
| 40 | |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 41 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 42 | * @brief KeyChain is one of the main classes of the security library. |
Jeff Thompson | ffa36f9 | 2013-09-20 08:42:41 -0700 | [diff] [blame] | 43 | * |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 44 | * The KeyChain class provides a set of interfaces of identity management and private key related |
| 45 | * operations. |
Jeff Thompson | ffa36f9 | 2013-09-20 08:42:41 -0700 | [diff] [blame] | 46 | */ |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 47 | template<class Info, class Tpm> |
| 48 | class KeyChainImpl : public Info, public Tpm |
| 49 | { |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 50 | typedef SecPublicInfo::Error InfoError; |
| 51 | typedef SecTpm::Error TpmError; |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 52 | public: |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 53 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 54 | * @brief Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a |
| 55 | * self-signed certificate of the KSK. |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 56 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 57 | * @param identityName The name of the identity. |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 58 | * @return The name of the default certificate of the identity. |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 59 | */ |
| 60 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 61 | createIdentity(const Name& identityName) |
| 62 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 63 | Info::addIdentity(identityName); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 64 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 65 | Name keyName; |
| 66 | try |
| 67 | { |
| 68 | keyName = Info::getDefaultKeyNameForIdentity(identityName); |
| 69 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 70 | catch (InfoError& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 71 | { |
| 72 | keyName = generateRSAKeyPairAsDefault(identityName, true); |
| 73 | } |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 74 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 75 | Name certName; |
| 76 | try |
| 77 | { |
| 78 | certName = Info::getDefaultCertificateNameForKey(keyName); |
| 79 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 80 | catch (InfoError& e) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 81 | { |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 82 | shared_ptr<IdentityCertificate> selfCert = selfSign(keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 83 | Info::addCertificateAsIdentityDefault(*selfCert); |
| 84 | certName = selfCert->getName(); |
| 85 | } |
| 86 | |
| 87 | return certName; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 88 | } |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 89 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 90 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 91 | * @brief Generate a pair of RSA keys for the specified identity. |
| 92 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 93 | * @param identityName The name of the identity. |
| 94 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 95 | * @param keySize The size of the key. |
| 96 | * @return The generated key name. |
| 97 | */ |
| 98 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 99 | generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048) |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 100 | { |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 101 | return generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 102 | } |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 103 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 104 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 105 | * @brief Generate a pair of RSA keys for the specified identity and set it as default key for |
| 106 | * the identity. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 107 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 108 | * @param identityName The name of the identity. |
| 109 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 110 | * @param keySize The size of the key. |
| 111 | * @return The generated key name. |
| 112 | */ |
| 113 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 114 | generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048) |
| 115 | { |
| 116 | Name keyName = generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
| 117 | |
| 118 | Info::setDefaultKeyNameForIdentity(keyName); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 119 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 120 | return keyName; |
| 121 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 122 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 123 | /** |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 124 | * @brief prepare an unsigned identity certificate |
| 125 | * |
| 126 | * @param keyName Key name, e.g., /<identity_name>/ksk-123456. |
| 127 | * @param signingIdentity The signing identity. |
| 128 | * @param notBefore Refer to IdentityCertificate. |
| 129 | * @param notAfter Refer to IdentityCertificate. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 130 | * @param subjectDescription Refer to IdentityCertificate. |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 131 | * @return IdentityCertificate. |
| 132 | */ |
| 133 | shared_ptr<IdentityCertificate> |
| 134 | prepareUnsignedIdentityCertificate(const Name& keyName, |
| 135 | const Name& signingIdentity, |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 136 | const time::system_clock::TimePoint& notBefore, |
| 137 | const time::system_clock::TimePoint& notAfter, |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 138 | const std::vector<CertificateSubjectDescription>& subjectDescription) |
| 139 | |
| 140 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 141 | if (keyName.size() < 1) |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 142 | return shared_ptr<IdentityCertificate>(); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 143 | |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 144 | std::string keyIdPrefix = keyName.get(-1).toEscapedString().substr(0, 4); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 145 | if (keyIdPrefix != "ksk-" && keyIdPrefix != "dsk-") |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 146 | return shared_ptr<IdentityCertificate>(); |
| 147 | |
| 148 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
| 149 | Name certName; |
| 150 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 151 | if (signingIdentity.isPrefixOf(keyName)) |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 152 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 153 | certName.append(signingIdentity) |
| 154 | .append("KEY") |
| 155 | .append(keyName.getSubName(signingIdentity.size())) |
| 156 | .append("ID-CERT") |
| 157 | .appendVersion(); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 158 | } |
| 159 | else |
| 160 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 161 | certName.append(keyName.getPrefix(-1)) |
| 162 | .append("KEY") |
| 163 | .append(keyName.get(-1)) |
| 164 | .append("ID-CERT") |
| 165 | .appendVersion(); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | certificate->setName(certName); |
| 169 | certificate->setNotBefore(notBefore); |
| 170 | certificate->setNotAfter(notAfter); |
| 171 | |
| 172 | shared_ptr<PublicKey> publicKey; |
| 173 | try |
| 174 | { |
| 175 | publicKey = Info::getPublicKey(keyName); |
| 176 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 177 | catch (InfoError& e) |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 178 | { |
| 179 | return shared_ptr<IdentityCertificate>(); |
| 180 | } |
| 181 | certificate->setPublicKeyInfo(*publicKey); |
| 182 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 183 | if (subjectDescription.empty()) |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 184 | { |
| 185 | CertificateSubjectDescription subDescryptName("2.5.4.41", keyName.getPrefix(-1).toUri()); |
| 186 | certificate->addSubjectDescription(subDescryptName); |
| 187 | } |
| 188 | else |
| 189 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 190 | std::vector<CertificateSubjectDescription>::const_iterator sdIt = |
| 191 | subjectDescription.begin(); |
| 192 | std::vector<CertificateSubjectDescription>::const_iterator sdEnd = |
| 193 | subjectDescription.end(); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 194 | for(; sdIt != sdEnd; sdIt++) |
| 195 | certificate->addSubjectDescription(*sdIt); |
| 196 | } |
| 197 | |
| 198 | certificate->encode(); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 199 | |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 200 | return certificate; |
| 201 | } |
| 202 | |
| 203 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 204 | * @brief Sign packet with default identity |
| 205 | * |
Yingdi Yu | 60bd708 | 2014-03-25 18:18:54 -0700 | [diff] [blame] | 206 | * On return, signatureInfo and signatureValue in the packet are set. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 207 | * If default identity does not exist, |
Yingdi Yu | 60bd708 | 2014-03-25 18:18:54 -0700 | [diff] [blame] | 208 | * a temporary identity will be created and set as default. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 209 | * |
| 210 | * @param packet The packet to be signed |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 211 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 212 | template<typename T> |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 213 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 214 | sign(T& packet) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 215 | { |
Yingdi Yu | 60bd708 | 2014-03-25 18:18:54 -0700 | [diff] [blame] | 216 | if (!static_cast<bool>(Info::defaultCertificate())) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 217 | { |
| 218 | Info::refreshDefaultCertificate(); |
| 219 | |
Yingdi Yu | 60bd708 | 2014-03-25 18:18:54 -0700 | [diff] [blame] | 220 | if (!static_cast<bool>(Info::defaultCertificate())) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 221 | { |
| 222 | Name defaultIdentity; |
| 223 | try |
| 224 | { |
| 225 | defaultIdentity = Info::getDefaultIdentity(); |
| 226 | } |
Yingdi Yu | 60bd708 | 2014-03-25 18:18:54 -0700 | [diff] [blame] | 227 | catch (InfoError& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 228 | { |
| 229 | uint32_t random = random::generateWord32(); |
Yingdi Yu | 60bd708 | 2014-03-25 18:18:54 -0700 | [diff] [blame] | 230 | defaultIdentity.append("tmp-identity") |
| 231 | .append(reinterpret_cast<uint8_t*>(&random), 4); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 232 | } |
| 233 | createIdentity(defaultIdentity); |
Yingdi Yu | 60bd708 | 2014-03-25 18:18:54 -0700 | [diff] [blame] | 234 | Info::setDefaultIdentity(defaultIdentity); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 235 | Info::refreshDefaultCertificate(); |
| 236 | } |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 239 | sign(packet, *Info::defaultCertificate()); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 240 | } |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 241 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 242 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 243 | * @brief Sign packet with a particular certificate. |
| 244 | * |
| 245 | * @param packet The packet to be signed. |
| 246 | * @param certificateName The certificate name of the key to use for signing. |
| 247 | * @throws SecPublicInfo::Error if certificate does not exist. |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 248 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 249 | template<typename T> |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 250 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 251 | sign(T& packet, const Name& certificateName) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 252 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 253 | if (!Info::doesCertificateExist(certificateName)) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 254 | throw InfoError("Requested certificate [" + certificateName.toUri() + "] doesn't exist"); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 255 | |
| 256 | SignatureSha256WithRsa signature; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 257 | // implicit conversion should take care |
| 258 | signature.setKeyLocator(certificateName.getPrefix(-1)); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 259 | |
| 260 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 261 | signPacketWrapper(packet, signature, |
| 262 | IdentityCertificate::certificateNameToPublicKeyName(certificateName), |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 263 | DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 264 | } |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 265 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 266 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 267 | * @brief Sign the byte array using a particular certificate. |
| 268 | * |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 269 | * @param buffer The byte array to be signed. |
| 270 | * @param bufferLength the length of buffer. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 271 | * @param certificateName The certificate name of the signing key. |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 272 | * @return The Signature. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 273 | * @throws SecPublicInfo::Error if certificate does not exist. |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 274 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 275 | Signature |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 276 | sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName) |
| 277 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 278 | if (!Info::doesCertificateExist(certificateName)) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 279 | throw InfoError("Requested certificate [" + certificateName.toUri() + "] doesn't exist"); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 280 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 281 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
| 282 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 283 | SignatureSha256WithRsa signature; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 284 | // implicit conversion should take care |
| 285 | signature.setKeyLocator(certificateName.getPrefix(-1)); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 286 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 287 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 288 | signature.setValue(Tpm::signInTpm(buffer, bufferLength, keyName, DIGEST_ALGORITHM_SHA256)); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 289 | return signature; |
| 290 | } |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 291 | |
| 292 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 293 | * @brief Sign packet using the default certificate of a particular identity. |
| 294 | * |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 295 | * If there is no default certificate of that identity, this method will create a self-signed |
| 296 | * certificate. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 297 | * |
| 298 | * @param packet The packet to be signed. |
| 299 | * @param identityName The signing identity name. |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 300 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 301 | template<typename T> |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 302 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 303 | signByIdentity(T& packet, const Name& identityName) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 304 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 305 | Name signingCertificateName; |
| 306 | try |
| 307 | { |
| 308 | signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName); |
| 309 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 310 | catch (InfoError& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 311 | { |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 312 | signingCertificateName = createIdentity(identityName); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 313 | // Ideally, no exception will be thrown out, unless something goes wrong in the TPM, which |
| 314 | // is a fatal error. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 315 | } |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 316 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 317 | // We either get or create the signing certificate, sign packet! (no exception unless fatal |
| 318 | // error in TPM) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 319 | sign(packet, signingCertificateName); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 320 | } |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 321 | |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 322 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 323 | * @brief Sign the byte array using the default certificate of a particular identity. |
| 324 | * |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 325 | * @param buffer The byte array to be signed. |
| 326 | * @param bufferLength the length of buffer. |
| 327 | * @param identityName The identity name. |
| 328 | * @return The Signature. |
| 329 | */ |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 330 | Signature |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 331 | signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 332 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 333 | Name signingCertificateName; |
| 334 | try |
| 335 | { |
| 336 | signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName); |
| 337 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 338 | catch (InfoError& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 339 | { |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 340 | signingCertificateName = createIdentity(identityName); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 341 | // Ideally, no exception will be thrown out, unless something goes wrong in the TPM, which |
| 342 | // is a fatal error. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 343 | } |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 344 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 345 | // We either get or create the signing certificate, sign data! (no exception unless fatal error |
| 346 | // in TPM) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 347 | return sign(buffer, bufferLength, signingCertificateName); |
| 348 | } |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 349 | |
| 350 | /** |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 351 | * @brief Set Sha256 weak signature. |
| 352 | * |
| 353 | * @param data. |
| 354 | */ |
| 355 | void |
| 356 | signWithSha256(Data& data) |
| 357 | { |
| 358 | SignatureSha256 sig; |
| 359 | data.setSignature(sig); |
| 360 | |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 361 | Block sigValue(Tlv::SignatureValue, |
| 362 | crypto::sha256(data.wireEncode().value(), |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 363 | data.wireEncode().value_size() - |
| 364 | data.getSignature().getValue().size())); |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 365 | data.setSignatureValue(sigValue); |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 369 | * @brief Generate a self-signed certificate for a public key. |
| 370 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 371 | * @param keyName The name of the public key. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 372 | * @return The generated certificate, NULL if selfSign fails. |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 373 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 374 | shared_ptr<IdentityCertificate> |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 375 | selfSign(const Name& keyName) |
| 376 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 377 | shared_ptr<PublicKey> pubKey; |
| 378 | try |
| 379 | { |
| 380 | pubKey = Info::getPublicKey(keyName); // may throw an exception. |
| 381 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 382 | catch (InfoError& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 383 | { |
| 384 | return shared_ptr<IdentityCertificate>(); |
| 385 | } |
Yingdi Yu | 7ea6950 | 2014-01-15 17:21:29 -0800 | [diff] [blame] | 386 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 387 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 388 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 389 | Name certificateName = keyName.getPrefix(-1); |
| 390 | certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion(); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 391 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 392 | certificate->setName(certificateName); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 393 | certificate->setNotBefore(time::system_clock::now()); |
| 394 | certificate->setNotAfter(time::system_clock::now() + time::days(7300)/* ~20 years*/); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 395 | certificate->setPublicKeyInfo(*pubKey); |
| 396 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
| 397 | certificate->encode(); |
| 398 | |
| 399 | selfSign(*certificate); |
| 400 | return certificate; |
| 401 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 402 | |
| 403 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 404 | * @brief Self-sign the supplied identity certificate. |
| 405 | * |
| 406 | * @param cert The supplied cert. |
| 407 | * @throws SecTpm::Error if the private key does not exist. |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 408 | */ |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 409 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 410 | selfSign (IdentityCertificate& cert) |
| 411 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 412 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(cert.getName()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 413 | if (!Tpm::doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 414 | throw TpmError("private key does not exist!"); |
| 415 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 416 | SignatureSha256WithRsa signature; |
| 417 | signature.setKeyLocator(cert.getName().getPrefix(-1)); // implicit conversion should take care |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 418 | |
| 419 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 420 | signPacketWrapper(cert, signature, keyName, DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 423 | /** |
| 424 | * @brief delete a certificate. |
| 425 | * |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 426 | * If the certificate to be deleted is current default system default, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 427 | * the method will not delete the certificate and return immediately. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 428 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 429 | * @param certificateName The certificate to be deleted. |
| 430 | */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 431 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 432 | deleteCertificate (const Name& certificateName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 433 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 434 | try |
| 435 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 436 | if (Info::getDefaultCertificateName() == certificateName) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 437 | return; |
| 438 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 439 | catch (InfoError& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 440 | { |
| 441 | // Not a real error, just try to delete the certificate |
| 442 | } |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 443 | |
| 444 | Info::deleteCertificateInfo(certificateName); |
| 445 | } |
| 446 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 447 | /** |
| 448 | * @brief delete a key. |
| 449 | * |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 450 | * If the key to be deleted is current default system default, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 451 | * the method will not delete the key and return immediately. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 452 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 453 | * @param keyName The key to be deleted. |
| 454 | */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 455 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 456 | deleteKey (const Name& keyName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 457 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 458 | try |
| 459 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 460 | if (Info::getDefaultKeyNameForIdentity(Info::getDefaultIdentity()) == keyName) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 461 | return; |
| 462 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 463 | catch (InfoError& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 464 | { |
| 465 | // Not a real error, just try to delete the key |
| 466 | } |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 467 | |
| 468 | Info::deletePublicKeyInfo(keyName); |
| 469 | Tpm::deleteKeyPairInTpm(keyName); |
| 470 | } |
| 471 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 472 | /** |
| 473 | * @brief delete an identity. |
| 474 | * |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 475 | * If the identity to be deleted is current default system default, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 476 | * the method will not delete the identity and return immediately. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 477 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 478 | * @param identity The identity to be deleted. |
| 479 | */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 480 | void |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 481 | deleteIdentity (const Name& identity) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 482 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 483 | try |
| 484 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 485 | if (Info::getDefaultIdentity() == identity) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 486 | return; |
| 487 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 488 | catch (InfoError& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 489 | { |
| 490 | // Not a real error, just try to delete the identity |
| 491 | } |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 492 | |
| 493 | std::vector<Name> nameList; |
| 494 | Info::getAllKeyNamesOfIdentity(identity, nameList, true); |
| 495 | Info::getAllKeyNamesOfIdentity(identity, nameList, false); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 496 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 497 | Info::deleteIdentityInfo(identity); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 498 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 499 | std::vector<Name>::const_iterator it = nameList.begin(); |
| 500 | for(; it != nameList.end(); it++) |
| 501 | Tpm::deleteKeyPairInTpm(*it); |
| 502 | } |
| 503 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 504 | /** |
| 505 | * @brief export an identity. |
| 506 | * |
| 507 | * @param identity The identity to export. |
| 508 | * @param passwordStr The password to secure the private key. |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 509 | * @return The encoded export data. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 510 | * @throws InfoError if anything goes wrong in exporting. |
| 511 | */ |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 512 | shared_ptr<SecuredBag> |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 513 | exportIdentity(const Name& identity, const std::string& passwordStr) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 514 | { |
| 515 | if (!Info::doesIdentityExist(identity)) |
| 516 | throw InfoError("Identity does not exist!"); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 517 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 518 | Name keyName = Info::getDefaultKeyNameForIdentity(identity); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 519 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 520 | ConstBufferPtr pkcs5; |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 521 | try |
| 522 | { |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 523 | pkcs5 = Tpm::exportPrivateKeyPkcs5FromTpm(keyName, passwordStr); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 524 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 525 | catch (TpmError& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 526 | { |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 527 | throw InfoError("Fail to export PKCS5 of private key"); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 528 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 529 | |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 530 | shared_ptr<IdentityCertificate> cert; |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 531 | try |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 532 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 533 | cert = Info::getCertificate(Info::getDefaultCertificateNameForKey(keyName)); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 534 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 535 | catch (InfoError& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 536 | { |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 537 | cert = selfSign(keyName); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 538 | Info::addCertificateAsIdentityDefault(*cert); |
| 539 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 540 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 541 | shared_ptr<SecuredBag> secureBag = make_shared<SecuredBag>(boost::cref(*cert), |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 542 | boost::cref(pkcs5)); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 543 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 544 | return secureBag; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 545 | } |
| 546 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 547 | /** |
| 548 | * @brief import an identity. |
| 549 | * |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 550 | * @param securedBag The encoded import data. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 551 | * @param passwordStr The password to secure the private key. |
| 552 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 553 | void |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 554 | importIdentity(const SecuredBag& securedBag, const std::string& passwordStr) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 555 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 556 | Name certificateName = securedBag.getCertificate().getName(); |
| 557 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 558 | Name identity = keyName.getPrefix(-1); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 559 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 560 | // Add identity |
| 561 | Info::addIdentity(identity); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 562 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 563 | // Add key |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 564 | Tpm::importPrivateKeyPkcs5IntoTpm(keyName, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 565 | securedBag.getKey()->buf(), |
| 566 | securedBag.getKey()->size(), |
| 567 | passwordStr); |
| 568 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 569 | shared_ptr<PublicKey> pubKey = Tpm::getPublicKeyFromTpm(keyName.toUri()); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 570 | // HACK! We should set key type according to the pkcs8 info. |
| 571 | Info::addPublicKey(keyName, KEY_TYPE_RSA, *pubKey); |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 572 | Info::setDefaultKeyNameForIdentity(keyName); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 573 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 574 | // Add cert |
| 575 | Info::addCertificateAsIdentityDefault(securedBag.getCertificate()); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 578 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 579 | private: |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 580 | /** |
| 581 | * @brief sign a packet using a pariticular certificate. |
| 582 | * |
| 583 | * @param packet The packet to be signed. |
| 584 | * @param certificate The signing certificate. |
| 585 | */ |
| 586 | template<typename T> |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 587 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 588 | sign(T& packet, const IdentityCertificate& certificate) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 589 | { |
| 590 | SignatureSha256WithRsa signature; |
| 591 | signature.setKeyLocator(certificate.getName().getPrefix(-1)); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 592 | |
| 593 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 594 | signPacketWrapper(packet, signature, certificate.getPublicKeyName(), DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 597 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 598 | * @brief Generate a key pair for the specified identity. |
| 599 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 600 | * @param identityName The name of the specified identity. |
| 601 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 602 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 603 | * @param keySize The size of the key pair. |
| 604 | * @return The name of the generated key. |
| 605 | */ |
| 606 | Name |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 607 | generateKeyPair(const Name& identityName, bool isKsk = false, |
| 608 | KeyType keyType = KEY_TYPE_RSA, int keySize = 2048) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 609 | { |
| 610 | Name keyName = Info::getNewKeyName(identityName, isKsk); |
| 611 | |
| 612 | Tpm::generateKeyPairInTpm(keyName.toUri(), keyType, keySize); |
| 613 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 614 | shared_ptr<PublicKey> pubKey = Tpm::getPublicKeyFromTpm(keyName.toUri()); |
Yingdi Yu | ef26ee3 | 2014-01-15 16:41:14 -0800 | [diff] [blame] | 615 | Info::addPublicKey(keyName, keyType, *pubKey); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 616 | |
| 617 | return keyName; |
| 618 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 619 | |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 620 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 621 | * @brief Sign the data using a particular key. |
| 622 | * |
| 623 | * @param data Reference to the data packet. |
| 624 | * @param signature Signature to be added. |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 625 | * @param keyName The name of the signing key. |
| 626 | * @param digestAlgorithm the digest algorithm. |
| 627 | * @throws Tpm::Error |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 628 | */ |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 629 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 630 | signPacketWrapper(Data& data, const SignatureSha256WithRsa& signature, |
| 631 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 632 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 633 | data.setSignature(signature); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 634 | data.setSignatureValue(Tpm::signInTpm(data.wireEncode().value(), |
| 635 | data.wireEncode().value_size() - |
| 636 | data.getSignature().getValue().size(), |
| 637 | keyName, digestAlgorithm)); |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 640 | /** |
| 641 | * @brief Sign the interest using a particular key. |
| 642 | * |
| 643 | * @param interest Reference to the interest packet. |
| 644 | * @param signature Signature to be added. |
| 645 | * @param keyName The name of the signing key. |
| 646 | * @param digestAlgorithm the digest algorithm. |
| 647 | * @throws Tpm::Error |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 648 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 649 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 650 | signPacketWrapper(Interest& interest, const SignatureSha256WithRsa& signature, |
| 651 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 652 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 653 | Name signedName = interest.getName(); |
| 654 | signedName.append(signature.getInfo()); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 655 | |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 656 | Block sigValue = Tpm::signInTpm(signedName.wireEncode().value(), |
| 657 | signedName.wireEncode().value_size(), |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 658 | keyName, |
| 659 | DIGEST_ALGORITHM_SHA256); |
| 660 | sigValue.encode(); |
| 661 | signedName.append(sigValue); |
| 662 | interest.setName(signedName); |
| 663 | } |
| 664 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 665 | }; |
| 666 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 667 | } // namespace ndn |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 668 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 669 | |
| 670 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 671 | #if defined(NDN_CXX_HAVE_OSX_SECURITY) and defined(NDN_CXX_WITH_OSX_KEYCHAIN) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 672 | |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 673 | namespace ndn { |
| 674 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 675 | typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmOsx> KeyChain; |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 676 | |
| 677 | } // namespace ndn |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 678 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 679 | #else |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 680 | |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 681 | namespace ndn { |
| 682 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 683 | typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> KeyChain; |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 684 | |
| 685 | } // namespace ndn |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 686 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 687 | #endif // NDN_CXX_HAVE_OSX_SECURITY |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 688 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 689 | #endif // NDN_SECURITY_KEY_CHAIN_HPP |