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 | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 18 | #include "sec-public-info.hpp" |
| 19 | #include "sec-tpm.hpp" |
| 20 | #include "secured-bag.hpp" |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 21 | #include "signature-sha256-with-rsa.hpp" |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 22 | #include "signature-sha256.hpp" |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 23 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 24 | #include "../interest.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 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 30 | template<class TypePib, class TypeTpm> |
| 31 | class KeyChainTraits |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 32 | { |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 33 | public: |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 34 | typedef TypePib Pib; |
| 35 | typedef TypeTpm Tpm; |
| 36 | }; |
| 37 | |
Alexander Afanasyev | 45a3713 | 2014-04-28 16:54:57 -0700 | [diff] [blame] | 38 | class KeyChain : noncopyable |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 39 | { |
| 40 | public: |
| 41 | class Error : public std::runtime_error |
| 42 | { |
| 43 | public: |
| 44 | explicit |
| 45 | Error(const std::string& what) |
| 46 | : std::runtime_error(what) |
| 47 | { |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | KeyChain(); |
| 52 | |
| 53 | template<class KeyChainTraits> |
| 54 | KeyChain(KeyChainTraits traits); |
| 55 | |
| 56 | KeyChain(const std::string& pibName, |
| 57 | const std::string& tpmName); |
| 58 | |
| 59 | virtual |
| 60 | ~KeyChain() |
| 61 | { |
| 62 | if (m_pib != 0) |
| 63 | delete m_pib; |
| 64 | |
| 65 | if (m_tpm != 0) |
| 66 | delete m_tpm; |
| 67 | } |
| 68 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 69 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 70 | * @brief Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a |
| 71 | * self-signed certificate of the KSK. |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 72 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 73 | * @param identityName The name of the identity. |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 74 | * @return The name of the default certificate of the identity. |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 75 | */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 76 | inline Name |
| 77 | createIdentity(const Name& identityName); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 78 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 79 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 80 | * @brief Generate a pair of RSA keys for the specified identity. |
| 81 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 82 | * @param identityName The name of the identity. |
| 83 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 84 | * @param keySize The size of the key. |
| 85 | * @return The generated key name. |
| 86 | */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 87 | inline Name |
| 88 | generateRsaKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048); |
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 | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 91 | * @brief Generate a pair of RSA keys for the specified identity and set it as default key for |
| 92 | * the identity. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 93 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 94 | * @param identityName The name of the identity. |
| 95 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 96 | * @param keySize The size of the key. |
| 97 | * @return The generated key name. |
| 98 | */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 99 | inline Name |
| 100 | generateRsaKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048); |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 101 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 102 | /** |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 103 | * @brief prepare an unsigned identity certificate |
| 104 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 105 | * @param keyName Key name, e.g., `/<identity_name>/ksk-123456`. |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 106 | * @param signingIdentity The signing identity. |
| 107 | * @param notBefore Refer to IdentityCertificate. |
| 108 | * @param notAfter Refer to IdentityCertificate. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 109 | * @param subjectDescription Refer to IdentityCertificate. |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 110 | * @return IdentityCertificate. |
| 111 | */ |
| 112 | shared_ptr<IdentityCertificate> |
| 113 | prepareUnsignedIdentityCertificate(const Name& keyName, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 114 | const Name& signingIdentity, |
| 115 | const time::system_clock::TimePoint& notBefore, |
| 116 | const time::system_clock::TimePoint& notAfter, |
| 117 | const std::vector<CertificateSubjectDescription>& subjectDescription); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 118 | |
| 119 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 120 | * @brief Sign packet with default identity |
| 121 | * |
Yingdi Yu | 60bd708 | 2014-03-25 18:18:54 -0700 | [diff] [blame] | 122 | * On return, signatureInfo and signatureValue in the packet are set. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 123 | * If default identity does not exist, |
Yingdi Yu | 60bd708 | 2014-03-25 18:18:54 -0700 | [diff] [blame] | 124 | * a temporary identity will be created and set as default. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 125 | * |
| 126 | * @param packet The packet to be signed |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 127 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 128 | template<typename T> |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 129 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 130 | sign(T& packet); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 131 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 132 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 133 | * @brief Sign packet with a particular certificate. |
| 134 | * |
| 135 | * @param packet The packet to be signed. |
| 136 | * @param certificateName The certificate name of the key to use for signing. |
| 137 | * @throws SecPublicInfo::Error if certificate does not exist. |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 138 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 139 | template<typename T> |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 140 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 141 | sign(T& packet, const Name& certificateName); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 142 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 143 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 144 | * @brief Sign the byte array using a particular certificate. |
| 145 | * |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 146 | * @param buffer The byte array to be signed. |
| 147 | * @param bufferLength the length of buffer. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 148 | * @param certificateName The certificate name of the signing key. |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 149 | * @return The Signature. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 150 | * @throws SecPublicInfo::Error if certificate does not exist. |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 151 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 152 | Signature |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 153 | sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName); |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 154 | |
| 155 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 156 | * @brief Sign packet using the default certificate of a particular identity. |
| 157 | * |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 158 | * If there is no default certificate of that identity, this method will create a self-signed |
| 159 | * certificate. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 160 | * |
| 161 | * @param packet The packet to be signed. |
| 162 | * @param identityName The signing identity name. |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 163 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 164 | template<typename T> |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 165 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 166 | signByIdentity(T& packet, const Name& identityName); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 167 | |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 168 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 169 | * @brief Sign the byte array using the default certificate of a particular identity. |
| 170 | * |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 171 | * @param buffer The byte array to be signed. |
| 172 | * @param bufferLength the length of buffer. |
| 173 | * @param identityName The identity name. |
| 174 | * @return The Signature. |
| 175 | */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 176 | inline Signature |
| 177 | signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName); |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 180 | * @brief Set Sha256 weak signature for @param data |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 181 | */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 182 | inline void |
| 183 | signWithSha256(Data& data); |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 184 | |
| 185 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 186 | * @brief Generate a self-signed certificate for a public key. |
| 187 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 188 | * @param keyName The name of the public key |
| 189 | * @return The generated certificate, shared_ptr<IdentityCertificate>() if selfSign fails |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 190 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 191 | shared_ptr<IdentityCertificate> |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 192 | selfSign(const Name& keyName); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 193 | |
| 194 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 195 | * @brief Self-sign the supplied identity certificate. |
| 196 | * |
| 197 | * @param cert The supplied cert. |
| 198 | * @throws SecTpm::Error if the private key does not exist. |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 199 | */ |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 200 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 201 | selfSign(IdentityCertificate& cert); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 202 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 203 | /** |
| 204 | * @brief delete a certificate. |
| 205 | * |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 206 | * If the certificate to be deleted is current default system default, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 207 | * the method will not delete the certificate and return immediately. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 208 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 209 | * @param certificateName The certificate to be deleted. |
| 210 | */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 211 | inline void |
| 212 | deleteCertificate(const Name& certificateName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 213 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 214 | /** |
| 215 | * @brief delete a key. |
| 216 | * |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 217 | * If the key to be deleted is current default system default, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 218 | * the method will not delete the key and return immediately. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 219 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 220 | * @param keyName The key to be deleted. |
| 221 | */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 222 | inline void |
| 223 | deleteKey(const Name& keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 224 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 225 | /** |
| 226 | * @brief delete an identity. |
| 227 | * |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 228 | * If the identity to be deleted is current default system default, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 229 | * the method will not delete the identity and return immediately. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 230 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 231 | * @param identity The identity to be deleted. |
| 232 | */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 233 | inline void |
| 234 | deleteIdentity(const Name& identity); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 235 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 236 | /** |
| 237 | * @brief export an identity. |
| 238 | * |
| 239 | * @param identity The identity to export. |
| 240 | * @param passwordStr The password to secure the private key. |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 241 | * @return The encoded export data. |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 242 | * @throws SecPublicInfo::Error if anything goes wrong in exporting. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 243 | */ |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 244 | shared_ptr<SecuredBag> |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 245 | exportIdentity(const Name& identity, const std::string& passwordStr); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 246 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 247 | /** |
| 248 | * @brief import an identity. |
| 249 | * |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 250 | * @param securedBag The encoded import data. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 251 | * @param passwordStr The password to secure the private key. |
| 252 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 253 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 254 | importIdentity(const SecuredBag& securedBag, const std::string& passwordStr); |
| 255 | |
| 256 | SecPublicInfo& |
| 257 | getPib() |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 258 | { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 259 | return *m_pib; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 262 | const SecPublicInfo& |
| 263 | getPib() const |
| 264 | { |
| 265 | return *m_pib; |
| 266 | } |
| 267 | |
| 268 | SecTpm& |
| 269 | getTpm() |
| 270 | { |
| 271 | return *m_tpm; |
| 272 | } |
| 273 | |
| 274 | const SecTpm& |
| 275 | getTpm() const |
| 276 | { |
| 277 | return *m_tpm; |
| 278 | } |
| 279 | |
| 280 | /******************************* |
| 281 | * Wrapper of SecPublicInfo * |
| 282 | *******************************/ |
| 283 | bool |
| 284 | doesIdentityExist(const Name& identityName) const |
| 285 | { |
| 286 | return m_pib->doesIdentityExist(identityName); |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | addIdentity(const Name& identityName) |
| 291 | { |
| 292 | return m_pib->addIdentity(identityName); |
| 293 | } |
| 294 | |
| 295 | bool |
| 296 | doesPublicKeyExist(const Name& keyName) const |
| 297 | { |
| 298 | return m_pib->doesPublicKeyExist(keyName); |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) |
| 303 | { |
| 304 | return m_pib->addPublicKey(keyName, keyType, publicKeyDer); |
| 305 | } |
| 306 | |
| 307 | shared_ptr<PublicKey> |
| 308 | getPublicKey(const Name& keyName) const |
| 309 | { |
| 310 | return m_pib->getPublicKey(keyName); |
| 311 | } |
| 312 | |
| 313 | bool |
| 314 | doesCertificateExist(const Name& certificateName) const |
| 315 | { |
| 316 | return m_pib->doesCertificateExist(certificateName); |
| 317 | } |
| 318 | |
| 319 | void |
| 320 | addCertificate(const IdentityCertificate& certificate) |
| 321 | { |
| 322 | return m_pib->addCertificate(certificate); |
| 323 | } |
| 324 | |
| 325 | shared_ptr<IdentityCertificate> |
| 326 | getCertificate(const Name& certificateName) const |
| 327 | { |
| 328 | return m_pib->getCertificate(certificateName); |
| 329 | } |
| 330 | |
| 331 | Name |
| 332 | getDefaultIdentity() const |
| 333 | { |
| 334 | return m_pib->getDefaultIdentity(); |
| 335 | } |
| 336 | |
| 337 | Name |
| 338 | getDefaultKeyNameForIdentity(const Name& identityName) const |
| 339 | { |
| 340 | return m_pib->getDefaultKeyNameForIdentity(identityName); |
| 341 | } |
| 342 | |
| 343 | Name |
| 344 | getDefaultCertificateNameForKey(const Name& keyName) const |
| 345 | { |
| 346 | return m_pib->getDefaultCertificateNameForKey(keyName); |
| 347 | } |
| 348 | |
| 349 | void |
| 350 | getAllIdentities(std::vector<Name>& nameList, bool isDefault) const |
| 351 | { |
| 352 | return m_pib->getAllIdentities(nameList, isDefault); |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | getAllKeyNames(std::vector<Name>& nameList, bool isDefault) const |
| 357 | { |
| 358 | return m_pib->getAllKeyNames(nameList, isDefault); |
| 359 | } |
| 360 | |
| 361 | void |
| 362 | getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) const |
| 363 | { |
| 364 | return m_pib->getAllKeyNamesOfIdentity(identity, nameList, isDefault); |
| 365 | } |
| 366 | |
| 367 | void |
| 368 | getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) const |
| 369 | { |
| 370 | return m_pib->getAllCertificateNames(nameList, isDefault); |
| 371 | } |
| 372 | |
| 373 | void |
| 374 | getAllCertificateNamesOfKey(const Name& keyName, |
| 375 | std::vector<Name>& nameList, |
| 376 | bool isDefault) const |
| 377 | { |
| 378 | return m_pib->getAllCertificateNamesOfKey(keyName, nameList, isDefault); |
| 379 | } |
| 380 | |
| 381 | void |
| 382 | deleteCertificateInfo(const Name& certificateName) |
| 383 | { |
| 384 | return m_pib->deleteCertificateInfo(certificateName); |
| 385 | } |
| 386 | |
| 387 | void |
| 388 | deletePublicKeyInfo(const Name& keyName) |
| 389 | { |
| 390 | return m_pib->deletePublicKeyInfo(keyName); |
| 391 | } |
| 392 | |
| 393 | void |
| 394 | deleteIdentityInfo(const Name& identity) |
| 395 | { |
| 396 | return m_pib->deleteIdentityInfo(identity); |
| 397 | } |
| 398 | |
| 399 | void |
| 400 | setDefaultIdentity(const Name& identityName) |
| 401 | { |
| 402 | return m_pib->setDefaultIdentity(identityName); |
| 403 | } |
| 404 | |
| 405 | void |
| 406 | setDefaultKeyNameForIdentity(const Name& keyName) |
| 407 | { |
| 408 | return m_pib->setDefaultKeyNameForIdentity(keyName); |
| 409 | } |
| 410 | |
| 411 | void |
| 412 | setDefaultCertificateNameForKey(const Name& certificateName) |
| 413 | { |
| 414 | return m_pib->setDefaultCertificateNameForKey(certificateName); |
| 415 | } |
| 416 | |
| 417 | Name |
| 418 | getNewKeyName(const Name& identityName, bool useKsk) |
| 419 | { |
| 420 | return m_pib->getNewKeyName(identityName, useKsk); |
| 421 | } |
| 422 | |
| 423 | Name |
| 424 | getDefaultCertificateNameForIdentity(const Name& identityName) const |
| 425 | { |
| 426 | return m_pib->getDefaultCertificateNameForIdentity(identityName); |
| 427 | } |
| 428 | |
| 429 | Name |
| 430 | getDefaultCertificateName() const |
| 431 | { |
| 432 | return m_pib->getDefaultCertificateName(); |
| 433 | } |
| 434 | |
| 435 | void |
| 436 | addCertificateAsKeyDefault(const IdentityCertificate& certificate) |
| 437 | { |
| 438 | return m_pib->addCertificateAsKeyDefault(certificate); |
| 439 | } |
| 440 | |
| 441 | void |
| 442 | addCertificateAsIdentityDefault(const IdentityCertificate& certificate) |
| 443 | { |
| 444 | return m_pib->addCertificateAsIdentityDefault(certificate); |
| 445 | } |
| 446 | |
| 447 | void |
| 448 | addCertificateAsSystemDefault(const IdentityCertificate& certificate) |
| 449 | { |
| 450 | return m_pib->addCertificateAsSystemDefault(certificate); |
| 451 | } |
| 452 | |
| 453 | shared_ptr<IdentityCertificate> |
| 454 | getDefaultCertificate() const |
| 455 | { |
| 456 | return m_pib->defaultCertificate(); |
| 457 | } |
| 458 | |
| 459 | void |
| 460 | refreshDefaultCertificate() |
| 461 | { |
| 462 | return m_pib->refreshDefaultCertificate(); |
| 463 | } |
| 464 | |
| 465 | /******************************* |
| 466 | * Wrapper of SecTpm * |
| 467 | *******************************/ |
| 468 | |
| 469 | void |
| 470 | setTpmPassword(const uint8_t* password, size_t passwordLength) |
| 471 | { |
| 472 | return m_tpm->setTpmPassword(password, passwordLength); |
| 473 | } |
| 474 | |
| 475 | void |
| 476 | resetTpmPassword() |
| 477 | { |
| 478 | return m_tpm->resetTpmPassword(); |
| 479 | } |
| 480 | |
| 481 | void |
| 482 | setInTerminal(bool inTerminal) |
| 483 | { |
| 484 | return m_tpm->setInTerminal(inTerminal); |
| 485 | } |
| 486 | |
| 487 | bool |
| 488 | getInTerminal() const |
| 489 | { |
| 490 | return m_tpm->getInTerminal(); |
| 491 | } |
| 492 | |
| 493 | bool |
| 494 | isLocked() const |
| 495 | { |
| 496 | return m_tpm->isLocked(); |
| 497 | } |
| 498 | |
| 499 | bool |
| 500 | unlockTpm(const char* password, size_t passwordLength, bool usePassword) |
| 501 | { |
| 502 | return m_tpm->unlockTpm(password, passwordLength, usePassword); |
| 503 | } |
| 504 | |
| 505 | void |
| 506 | generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize) |
| 507 | { |
| 508 | return m_tpm->generateKeyPairInTpm(keyName, keyType, keySize); |
| 509 | } |
| 510 | |
| 511 | void |
| 512 | deleteKeyPairInTpm(const Name& keyName) |
| 513 | { |
| 514 | return m_tpm->deleteKeyPairInTpm(keyName); |
| 515 | } |
| 516 | |
| 517 | shared_ptr<PublicKey> |
| 518 | getPublicKeyFromTpm(const Name& keyName) const |
| 519 | { |
| 520 | return m_tpm->getPublicKeyFromTpm(keyName); |
| 521 | } |
| 522 | |
| 523 | Block |
| 524 | signInTpm(const uint8_t* data, size_t dataLength, |
| 525 | const Name& keyName, |
| 526 | DigestAlgorithm digestAlgorithm) |
| 527 | { |
| 528 | return m_tpm->signInTpm(data, dataLength, keyName, digestAlgorithm); |
| 529 | } |
| 530 | |
| 531 | ConstBufferPtr |
| 532 | decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
| 533 | { |
| 534 | return m_tpm->decryptInTpm(data, dataLength, keyName, isSymmetric); |
| 535 | } |
| 536 | |
| 537 | ConstBufferPtr |
| 538 | encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
| 539 | { |
| 540 | return m_tpm->encryptInTpm(data, dataLength, keyName, isSymmetric); |
| 541 | } |
| 542 | |
| 543 | void |
| 544 | generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize) |
| 545 | { |
| 546 | return m_tpm->generateSymmetricKeyInTpm(keyName, keyType, keySize); |
| 547 | } |
| 548 | |
| 549 | bool |
| 550 | doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) const |
| 551 | { |
| 552 | return m_tpm->doesKeyExistInTpm(keyName, keyClass); |
| 553 | } |
| 554 | |
| 555 | bool |
| 556 | generateRandomBlock(uint8_t* res, size_t size) const |
| 557 | { |
| 558 | return m_tpm->generateRandomBlock(res, size); |
| 559 | } |
| 560 | |
| 561 | void |
| 562 | addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) |
| 563 | { |
| 564 | return m_tpm->addAppToAcl(keyName, keyClass, appPath, acl); |
| 565 | } |
| 566 | |
| 567 | ConstBufferPtr |
| 568 | exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password) |
| 569 | { |
| 570 | return m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, password); |
| 571 | } |
| 572 | |
| 573 | bool |
| 574 | importPrivateKeyPkcs5IntoTpm(const Name& keyName, |
| 575 | const uint8_t* buf, size_t size, |
| 576 | const std::string& password) |
| 577 | { |
| 578 | return m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, buf, size, password); |
| 579 | } |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 580 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 581 | private: |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 582 | /** |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 583 | * @brief Set default certificate if it is not initialized |
| 584 | */ |
| 585 | void |
| 586 | setDefaultCertificateInternal(); |
| 587 | |
| 588 | /** |
| 589 | * @brief Sign a packet using a pariticular certificate. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 590 | * |
| 591 | * @param packet The packet to be signed. |
| 592 | * @param certificate The signing certificate. |
| 593 | */ |
| 594 | template<typename T> |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 595 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 596 | sign(T& packet, const IdentityCertificate& certificate); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 597 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 598 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 599 | * @brief Generate a key pair for the specified identity. |
| 600 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 601 | * @param identityName The name of the specified identity. |
| 602 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 603 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 604 | * @param keySize The size of the key pair. |
| 605 | * @return The name of the generated key. |
| 606 | */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 607 | inline Name |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 608 | generateKeyPair(const Name& identityName, bool isKsk = false, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 609 | KeyType keyType = KEY_TYPE_RSA, int keySize = 2048); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 610 | |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 611 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 612 | * @brief Sign the data using a particular key. |
| 613 | * |
| 614 | * @param data Reference to the data packet. |
| 615 | * @param signature Signature to be added. |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 616 | * @param keyName The name of the signing key. |
| 617 | * @param digestAlgorithm the digest algorithm. |
| 618 | * @throws Tpm::Error |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 619 | */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 620 | inline void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 621 | signPacketWrapper(Data& data, const SignatureSha256WithRsa& signature, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 622 | const Name& keyName, DigestAlgorithm digestAlgorithm); |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 623 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 624 | /** |
| 625 | * @brief Sign the interest using a particular key. |
| 626 | * |
| 627 | * @param interest Reference to the interest packet. |
| 628 | * @param signature Signature to be added. |
| 629 | * @param keyName The name of the signing key. |
| 630 | * @param digestAlgorithm the digest algorithm. |
| 631 | * @throws Tpm::Error |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 632 | */ |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 633 | inline void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 634 | signPacketWrapper(Interest& interest, const SignatureSha256WithRsa& signature, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 635 | const Name& keyName, DigestAlgorithm digestAlgorithm); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 636 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 637 | |
| 638 | private: |
| 639 | SecPublicInfo* m_pib; |
| 640 | SecTpm* m_tpm; |
| 641 | }; |
| 642 | |
| 643 | template<class T> |
| 644 | inline |
| 645 | KeyChain::KeyChain(T) |
| 646 | : m_pib(new typename T::Pib) |
| 647 | , m_tpm(new typename T::Tpm) |
| 648 | { |
| 649 | } |
| 650 | |
| 651 | inline Name |
| 652 | KeyChain::createIdentity(const Name& identityName) |
| 653 | { |
| 654 | m_pib->addIdentity(identityName); |
| 655 | |
| 656 | Name keyName; |
| 657 | try |
| 658 | { |
| 659 | keyName = m_pib->getDefaultKeyNameForIdentity(identityName); |
| 660 | } |
| 661 | catch (SecPublicInfo::Error& e) |
| 662 | { |
| 663 | keyName = generateRsaKeyPairAsDefault(identityName, true); |
| 664 | } |
| 665 | |
| 666 | Name certName; |
| 667 | try |
| 668 | { |
| 669 | certName = m_pib->getDefaultCertificateNameForKey(keyName); |
| 670 | } |
| 671 | catch (SecPublicInfo::Error& e) |
| 672 | { |
| 673 | shared_ptr<IdentityCertificate> selfCert = selfSign(keyName); |
| 674 | m_pib->addCertificateAsIdentityDefault(*selfCert); |
| 675 | certName = selfCert->getName(); |
| 676 | } |
| 677 | |
| 678 | return certName; |
| 679 | } |
| 680 | |
| 681 | inline Name |
| 682 | KeyChain::generateRsaKeyPair(const Name& identityName, bool isKsk, int keySize) |
| 683 | { |
| 684 | return generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
| 685 | } |
| 686 | |
| 687 | inline Name |
| 688 | KeyChain::generateRsaKeyPairAsDefault(const Name& identityName, bool isKsk, int keySize) |
| 689 | { |
| 690 | Name keyName = generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
| 691 | |
| 692 | m_pib->setDefaultKeyNameForIdentity(keyName); |
| 693 | |
| 694 | return keyName; |
| 695 | } |
| 696 | |
| 697 | template<typename T> |
| 698 | void |
| 699 | KeyChain::sign(T& packet) |
| 700 | { |
| 701 | if (!static_cast<bool>(m_pib->defaultCertificate())) |
| 702 | setDefaultCertificateInternal(); |
| 703 | |
| 704 | sign(packet, *m_pib->defaultCertificate()); |
| 705 | } |
| 706 | |
| 707 | template<typename T> |
| 708 | void |
| 709 | KeyChain::sign(T& packet, const Name& certificateName) |
| 710 | { |
| 711 | if (!m_pib->doesCertificateExist(certificateName)) |
| 712 | throw SecPublicInfo::Error("Requested certificate [" + |
| 713 | certificateName.toUri() + "] doesn't exist"); |
| 714 | |
| 715 | SignatureSha256WithRsa signature; |
| 716 | // implicit conversion should take care |
| 717 | signature.setKeyLocator(certificateName.getPrefix(-1)); |
| 718 | |
| 719 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
| 720 | signPacketWrapper(packet, signature, |
| 721 | IdentityCertificate::certificateNameToPublicKeyName(certificateName), |
| 722 | DIGEST_ALGORITHM_SHA256); |
| 723 | } |
| 724 | |
| 725 | template<typename T> |
| 726 | void |
| 727 | KeyChain::signByIdentity(T& packet, const Name& identityName) |
| 728 | { |
| 729 | Name signingCertificateName; |
| 730 | try |
| 731 | { |
| 732 | signingCertificateName = m_pib->getDefaultCertificateNameForIdentity(identityName); |
| 733 | } |
| 734 | catch (SecPublicInfo::Error& e) |
| 735 | { |
| 736 | signingCertificateName = createIdentity(identityName); |
| 737 | // Ideally, no exception will be thrown out, unless something goes wrong in the TPM, which |
| 738 | // is a fatal error. |
| 739 | } |
| 740 | |
| 741 | // We either get or create the signing certificate, sign packet! (no exception unless fatal |
| 742 | // error in TPM) |
| 743 | sign(packet, signingCertificateName); |
| 744 | } |
| 745 | |
| 746 | inline Signature |
| 747 | KeyChain::signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName) |
| 748 | { |
| 749 | Name signingCertificateName; |
| 750 | try |
| 751 | { |
| 752 | signingCertificateName = m_pib->getDefaultCertificateNameForIdentity(identityName); |
| 753 | } |
| 754 | catch (SecPublicInfo::Error& e) |
| 755 | { |
| 756 | signingCertificateName = createIdentity(identityName); |
| 757 | // Ideally, no exception will be thrown out, unless something goes wrong in the TPM, which |
| 758 | // is a fatal error. |
| 759 | } |
| 760 | |
| 761 | // We either get or create the signing certificate, sign data! (no exception unless fatal error |
| 762 | // in TPM) |
| 763 | return sign(buffer, bufferLength, signingCertificateName); |
| 764 | } |
| 765 | |
| 766 | inline void |
| 767 | KeyChain::signWithSha256(Data& data) |
| 768 | { |
| 769 | SignatureSha256 sig; |
| 770 | data.setSignature(sig); |
| 771 | |
| 772 | Block sigValue(Tlv::SignatureValue, |
| 773 | crypto::sha256(data.wireEncode().value(), |
| 774 | data.wireEncode().value_size() - |
| 775 | data.getSignature().getValue().size())); |
| 776 | data.setSignatureValue(sigValue); |
| 777 | } |
| 778 | |
| 779 | inline void |
| 780 | KeyChain::deleteCertificate(const Name& certificateName) |
| 781 | { |
| 782 | try |
| 783 | { |
| 784 | if (m_pib->getDefaultCertificateName() == certificateName) |
| 785 | return; |
| 786 | } |
| 787 | catch (SecPublicInfo::Error& e) |
| 788 | { |
| 789 | // Not a real error, just try to delete the certificate |
| 790 | } |
| 791 | |
| 792 | m_pib->deleteCertificateInfo(certificateName); |
| 793 | } |
| 794 | |
| 795 | inline void |
| 796 | KeyChain::deleteKey(const Name& keyName) |
| 797 | { |
| 798 | try |
| 799 | { |
| 800 | if (m_pib->getDefaultKeyNameForIdentity(m_pib->getDefaultIdentity()) == keyName) |
| 801 | return; |
| 802 | } |
| 803 | catch (SecPublicInfo::Error& e) |
| 804 | { |
| 805 | // Not a real error, just try to delete the key |
| 806 | } |
| 807 | |
| 808 | m_pib->deletePublicKeyInfo(keyName); |
| 809 | m_tpm->deleteKeyPairInTpm(keyName); |
| 810 | } |
| 811 | |
| 812 | inline void |
| 813 | KeyChain::deleteIdentity(const Name& identity) |
| 814 | { |
| 815 | try |
| 816 | { |
| 817 | if (m_pib->getDefaultIdentity() == identity) |
| 818 | return; |
| 819 | } |
| 820 | catch (SecPublicInfo::Error& e) |
| 821 | { |
| 822 | // Not a real error, just try to delete the identity |
| 823 | } |
| 824 | |
| 825 | std::vector<Name> nameList; |
| 826 | m_pib->getAllKeyNamesOfIdentity(identity, nameList, true); |
| 827 | m_pib->getAllKeyNamesOfIdentity(identity, nameList, false); |
| 828 | |
| 829 | m_pib->deleteIdentityInfo(identity); |
| 830 | |
| 831 | std::vector<Name>::const_iterator it = nameList.begin(); |
| 832 | for(; it != nameList.end(); it++) |
| 833 | m_tpm->deleteKeyPairInTpm(*it); |
| 834 | } |
| 835 | |
| 836 | template<typename T> |
| 837 | void |
| 838 | KeyChain::sign(T& packet, const IdentityCertificate& certificate) |
| 839 | { |
| 840 | SignatureSha256WithRsa signature; |
| 841 | signature.setKeyLocator(certificate.getName().getPrefix(-1)); |
| 842 | |
| 843 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
| 844 | signPacketWrapper(packet, signature, certificate.getPublicKeyName(), DIGEST_ALGORITHM_SHA256); |
| 845 | } |
| 846 | |
| 847 | inline Name |
| 848 | KeyChain::generateKeyPair(const Name& identityName, bool isKsk, KeyType keyType, int keySize) |
| 849 | { |
| 850 | Name keyName = m_pib->getNewKeyName(identityName, isKsk); |
| 851 | |
| 852 | m_tpm->generateKeyPairInTpm(keyName.toUri(), keyType, keySize); |
| 853 | |
| 854 | shared_ptr<PublicKey> pubKey = m_tpm->getPublicKeyFromTpm(keyName.toUri()); |
| 855 | m_pib->addPublicKey(keyName, keyType, *pubKey); |
| 856 | |
| 857 | return keyName; |
| 858 | } |
| 859 | |
| 860 | inline void |
| 861 | KeyChain::signPacketWrapper(Data& data, const SignatureSha256WithRsa& signature, |
| 862 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 863 | { |
| 864 | data.setSignature(signature); |
| 865 | data.setSignatureValue(m_tpm->signInTpm(data.wireEncode().value(), |
| 866 | data.wireEncode().value_size() - |
| 867 | data.getSignature().getValue().size(), |
| 868 | keyName, digestAlgorithm)); |
| 869 | } |
| 870 | |
| 871 | inline void |
| 872 | KeyChain::signPacketWrapper(Interest& interest, const SignatureSha256WithRsa& signature, |
| 873 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 874 | { |
| 875 | Name signedName = interest.getName(); |
| 876 | signedName.append(signature.getInfo()); |
| 877 | |
| 878 | Block sigValue = m_tpm->signInTpm(signedName.wireEncode().value(), |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 879 | signedName.wireEncode().value_size(), |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 880 | keyName, |
| 881 | DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 882 | sigValue.encode(); |
| 883 | signedName.append(sigValue); |
| 884 | interest.setName(signedName); |
| 885 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 886 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 887 | } |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 888 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 889 | #endif // NDN_SECURITY_KEY_CHAIN_HPP |