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