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 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | ba16b8f | 2013-12-16 13:11:47 -0800 | [diff] [blame] | 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
| 9 | #ifndef NDN_KEY_CHAIN_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 10 | #define NDN_KEY_CHAIN_HPP |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 11 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 12 | #include "identity-certificate.hpp" |
| 13 | #include "public-key.hpp" |
| 14 | #include "signature-sha256-with-rsa.hpp" |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 15 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 16 | //PublicInfo |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 17 | #include "sec-public-info-sqlite3.hpp" |
| 18 | #include "sec-public-info-memory.hpp" |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 19 | //TPM |
| 20 | #include "sec-tpm-file.hpp" |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 21 | #include "sec-tpm-memory.hpp" |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 22 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 23 | #ifdef NDN_CPP_HAVE_OSX_SECURITY |
| 24 | #include "sec-tpm-osx.hpp" |
| 25 | #endif |
| 26 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 30 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 31 | * KeyChain is one of the main classes of the security library. |
Jeff Thompson | ffa36f9 | 2013-09-20 08:42:41 -0700 | [diff] [blame] | 32 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 33 | * The KeyChain class provides a set of interfaces of identity management and private key related operations. |
Jeff Thompson | ffa36f9 | 2013-09-20 08:42:41 -0700 | [diff] [blame] | 34 | */ |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 35 | template<class Info, class Tpm> |
| 36 | class KeyChainImpl : public Info, public Tpm |
| 37 | { |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 38 | public: |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 39 | // struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK. |
| 43 | * @param identityName The name of the identity. |
| 44 | * @return The key name of the auto-generated KSK of the identity. |
| 45 | */ |
| 46 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 47 | createIdentity(const Name& identityName) |
| 48 | { |
| 49 | if (!Info::doesIdentityExist(identityName)) { |
| 50 | Info::addIdentity(identityName); |
| 51 | |
| 52 | Name keyName = generateRSAKeyPairAsDefault(identityName, true); |
| 53 | |
| 54 | ptr_lib::shared_ptr<IdentityCertificate> selfCert = selfSign(keyName); |
| 55 | |
| 56 | Info::addCertificateAsDefault(*selfCert); |
| 57 | |
| 58 | return keyName; |
| 59 | } |
| 60 | else |
| 61 | return Name(); |
| 62 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 65 | * Generate a pair of RSA keys for the specified identity. |
| 66 | * @param identityName The name of the identity. |
| 67 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 68 | * @param keySize The size of the key. |
| 69 | * @return The generated key name. |
| 70 | */ |
| 71 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 72 | generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048) |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 73 | { |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 74 | return generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 75 | } |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 76 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Generate a pair of RSA keys for the specified identity and set it as default key for the identity. |
| 79 | * @param identityName The name of the identity. |
| 80 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 81 | * @param keySize The size of the key. |
| 82 | * @return The generated key name. |
| 83 | */ |
| 84 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 85 | generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048) |
| 86 | { |
| 87 | Name keyName = generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
| 88 | |
| 89 | Info::setDefaultKeyNameForIdentity(keyName); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 90 | |
| 91 | return keyName; |
| 92 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 93 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 94 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 95 | * Create an identity certificate for a public key managed by this IdentityManager. |
| 96 | * @param certificatePrefix The name of public key to be signed. |
| 97 | * @param signerCertificateName The name of signing certificate. |
| 98 | * @param notBefore The notBefore value in the validity field of the generated certificate. |
| 99 | * @param notAfter The notAfter vallue in validity field of the generated certificate. |
| 100 | * @return The name of generated identity certificate. |
| 101 | */ |
| 102 | ptr_lib::shared_ptr<IdentityCertificate> |
| 103 | createIdentityCertificate |
| 104 | (const Name& certificatePrefix, |
| 105 | const Name& signerCertificateName, |
| 106 | const MillisecondsSince1970& notBefore, |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 107 | const MillisecondsSince1970& notAfter) |
| 108 | { |
| 109 | Name keyName = getKeyNameFromCertificatePrefix(certificatePrefix); |
| 110 | |
| 111 | ptr_lib::shared_ptr<PublicKey> pubKey = Info::getPublicKey(keyName); |
| 112 | if (!pubKey) |
| 113 | throw std::runtime_error("Requested public key [" + keyName.toUri() + "] doesn't exist"); |
| 114 | |
| 115 | ptr_lib::shared_ptr<IdentityCertificate> certificate = |
| 116 | createIdentityCertificate(certificatePrefix, |
| 117 | *pubKey, |
| 118 | signerCertificateName, |
| 119 | notBefore, notAfter); |
| 120 | |
| 121 | Info::addCertificate(*certificate); |
| 122 | |
| 123 | return certificate; |
| 124 | } |
| 125 | |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 126 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 127 | /** |
| 128 | * Create an identity certificate for a public key supplied by the caller. |
| 129 | * @param certificatePrefix The name of public key to be signed. |
| 130 | * @param publickey The public key to be signed. |
| 131 | * @param signerCertificateName The name of signing certificate. |
| 132 | * @param notBefore The notBefore value in the validity field of the generated certificate. |
| 133 | * @param notAfter The notAfter vallue in validity field of the generated certificate. |
| 134 | * @return The generated identity certificate. |
| 135 | */ |
| 136 | ptr_lib::shared_ptr<IdentityCertificate> |
| 137 | createIdentityCertificate |
| 138 | (const Name& certificatePrefix, |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 139 | const PublicKey& publicKey, |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 140 | const Name& signerCertificateName, |
| 141 | const MillisecondsSince1970& notBefore, |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 142 | const MillisecondsSince1970& notAfter) |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 143 | { |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 144 | ptr_lib::shared_ptr<IdentityCertificate> certificate (new IdentityCertificate()); |
| 145 | Name keyName = getKeyNameFromCertificatePrefix(certificatePrefix); |
| 146 | |
| 147 | Name certificateName = certificatePrefix; |
| 148 | certificateName.append("ID-CERT").appendVersion(); |
| 149 | |
| 150 | certificate->setName(certificateName); |
| 151 | certificate->setNotBefore(notBefore); |
| 152 | certificate->setNotAfter(notAfter); |
| 153 | certificate->setPublicKeyInfo(publicKey); |
| 154 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
| 155 | certificate->encode(); |
| 156 | |
| 157 | sign(*certificate, signerCertificateName); |
| 158 | |
| 159 | return certificate; |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 162 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 163 | sign(Data &data) |
| 164 | { |
| 165 | if (!Info::defaultCertificate()) |
| 166 | { |
| 167 | Info::refreshDefaultCertificate(); |
| 168 | |
| 169 | if(!Info::defaultCertificate()) |
| 170 | throw std::runtime_error("Default IdentityCertificate cannot be determined"); |
| 171 | } |
| 172 | |
| 173 | sign(data, *Info::defaultCertificate()); |
| 174 | } |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 175 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 176 | /** |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 177 | * Wire encode the Data object, sign it and set its signature. |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 178 | * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 179 | * @param certificateName The certificate name of the key to use for signing. If omitted, infer the signing identity from the data packet name. |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 180 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 181 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 182 | sign(Data& data, const Name& certificateName) |
| 183 | { |
| 184 | ptr_lib::shared_ptr<IdentityCertificate> cert = Info::getCertificate(certificateName); |
| 185 | if (!cert) |
| 186 | throw std::runtime_error("Requested certificate [" + certificateName.toUri() + "] doesn't exist"); |
| 187 | |
| 188 | SignatureSha256WithRsa signature; |
| 189 | signature.setKeyLocator(certificateName.getPrefix(-1)); // implicit conversion should take care |
| 190 | data.setSignature(signature); |
| 191 | |
| 192 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame^] | 193 | signDataInTpm(data, cert->getPublicKeyName(), DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 194 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 195 | |
| 196 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 197 | sign(Data& data, const IdentityCertificate& certificate) |
| 198 | { |
| 199 | SignatureSha256WithRsa signature; |
| 200 | signature.setKeyLocator(certificate.getName().getPrefix(-1)); |
| 201 | data.setSignature(signature); |
| 202 | |
| 203 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame^] | 204 | signDataInTpm(data, certificate.getPublicKeyName(), DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 205 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 206 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 207 | /** |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 208 | * Sign the byte array using a certificate name and return a Signature object. |
| 209 | * @param buffer The byte array to be signed. |
| 210 | * @param bufferLength the length of buffer. |
| 211 | * @param certificateName The certificate name used to get the signing key and which will be put into KeyLocator. |
| 212 | * @return The Signature. |
| 213 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 214 | Signature |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 215 | sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName) |
| 216 | { |
| 217 | ptr_lib::shared_ptr<IdentityCertificate> cert = Info::getCertificate(certificateName); |
| 218 | if (!cert) |
| 219 | throw std::runtime_error("Requested certificate [" + certificateName.toUri() + "] doesn't exist"); |
| 220 | |
| 221 | SignatureSha256WithRsa signature; |
| 222 | signature.setKeyLocator(certificateName.getPrefix(-1)); // implicit conversion should take care |
| 223 | |
| 224 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 225 | signature.setValue(Tpm::signInTpm(buffer, bufferLength, cert->getPublicKeyName(), DIGEST_ALGORITHM_SHA256)); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 226 | return signature; |
| 227 | } |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 228 | |
| 229 | /** |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 230 | * Wire encode the Data object, sign it and set its signature. |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 231 | * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding. |
| 232 | * @param identityName The identity name for the key to use for signing. If omitted, infer the signing identity from the data packet name. |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 233 | */ |
| 234 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 235 | signByIdentity(Data& data, const Name& identityName = Name()) |
| 236 | { |
| 237 | Name signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName); |
| 238 | |
| 239 | if (signingCertificateName.getComponentCount() == 0) |
| 240 | throw std::runtime_error("No qualified certificate name found!"); |
| 241 | |
| 242 | sign(data, signingCertificateName); |
| 243 | } |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 244 | |
| 245 | /** |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 246 | * Sign the byte array using an identity name and return a Signature object. |
| 247 | * @param buffer The byte array to be signed. |
| 248 | * @param bufferLength the length of buffer. |
| 249 | * @param identityName The identity name. |
| 250 | * @return The Signature. |
| 251 | */ |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 252 | Signature |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 253 | signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName = Name()) |
| 254 | { |
| 255 | Name signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName); |
| 256 | |
| 257 | if (signingCertificateName.size() == 0) |
| 258 | throw std::runtime_error("No qualified certificate name found!"); |
| 259 | |
| 260 | return sign(buffer, bufferLength, signingCertificateName); |
| 261 | } |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 262 | |
| 263 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 264 | * Generate a self-signed certificate for a public key. |
| 265 | * @param keyName The name of the public key. |
| 266 | * @return The generated certificate. |
| 267 | */ |
| 268 | ptr_lib::shared_ptr<IdentityCertificate> |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 269 | selfSign(const Name& keyName) |
| 270 | { |
Yingdi Yu | 7ea6950 | 2014-01-15 17:21:29 -0800 | [diff] [blame] | 271 | if(keyName.empty()) |
| 272 | throw std::runtime_error("Incorrect key name: " + keyName.toUri()); |
| 273 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 274 | ptr_lib::shared_ptr<IdentityCertificate> certificate = ptr_lib::make_shared<IdentityCertificate>(); |
| 275 | |
| 276 | Name certificateName = keyName.getPrefix(-1); |
| 277 | certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion(); |
| 278 | |
| 279 | ptr_lib::shared_ptr<PublicKey> pubKey = Info::getPublicKey(keyName); |
| 280 | if (!pubKey) |
| 281 | throw std::runtime_error("Requested public key [" + keyName.toUri() + "] doesn't exist"); |
| 282 | |
| 283 | certificate->setName(certificateName); |
| 284 | certificate->setNotBefore(getNow()); |
| 285 | certificate->setNotAfter(getNow() + 630720000 /* 20 years*/); |
| 286 | certificate->setPublicKeyInfo(*pubKey); |
| 287 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
| 288 | certificate->encode(); |
| 289 | |
| 290 | selfSign(*certificate); |
| 291 | return certificate; |
| 292 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 293 | |
| 294 | /** |
| 295 | * @brief Self-sign the supplied identity certificate |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 296 | */ |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 297 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 298 | selfSign (IdentityCertificate& cert) |
| 299 | { |
| 300 | SignatureSha256WithRsa signature; |
| 301 | signature.setKeyLocator(cert.getName().getPrefix(-1)); // implicit conversion should take care |
| 302 | cert.setSignature(signature); |
| 303 | |
| 304 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame^] | 305 | signDataInTpm(cert, cert.getPublicKeyName(), DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 308 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 309 | private: |
| 310 | /** |
| 311 | * Generate a key pair for the specified identity. |
| 312 | * @param identityName The name of the specified identity. |
| 313 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 314 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 315 | * @param keySize The size of the key pair. |
| 316 | * @return The name of the generated key. |
| 317 | */ |
| 318 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 319 | generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048) |
| 320 | { |
| 321 | Name keyName = Info::getNewKeyName(identityName, isKsk); |
| 322 | |
| 323 | Tpm::generateKeyPairInTpm(keyName.toUri(), keyType, keySize); |
| 324 | |
| 325 | ptr_lib::shared_ptr<PublicKey> pubKey = Tpm::getPublicKeyFromTpm(keyName.toUri()); |
Yingdi Yu | ef26ee3 | 2014-01-15 16:41:14 -0800 | [diff] [blame] | 326 | Info::addPublicKey(keyName, keyType, *pubKey); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 327 | |
| 328 | return keyName; |
| 329 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 330 | |
| 331 | static Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 332 | getKeyNameFromCertificatePrefix(const Name& certificatePrefix) |
| 333 | { |
| 334 | Name result; |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 335 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 336 | std::string keyString("KEY"); |
| 337 | int i = 0; |
| 338 | for(; i < certificatePrefix.size(); i++) { |
| 339 | if (certificatePrefix.get(i).toEscapedString() == keyString) |
| 340 | break; |
| 341 | } |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 342 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 343 | if (i >= certificatePrefix.size()) |
| 344 | throw std::runtime_error("Identity Certificate Prefix does not have a KEY component"); |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 345 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 346 | result.append(certificatePrefix.getSubName(0, i)); |
| 347 | result.append(certificatePrefix.getSubName(i + 1, certificatePrefix.size()-i-1)); |
| 348 | |
| 349 | return result; |
| 350 | } |
| 351 | |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame^] | 352 | /** |
| 353 | * Fetch the private key for keyName and sign the data, and set the signature block of the data packet. |
| 354 | * @param data Reference to the input data packet. |
| 355 | * @param keyName The name of the signing key. |
| 356 | * @param digestAlgorithm the digest algorithm. |
| 357 | * @throws Tpm::Error |
| 358 | */ |
| 359 | void |
| 360 | signDataInTpm(Data &data, const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 361 | { |
| 362 | data.setSignatureValue |
| 363 | (Tpm::signInTpm(data.wireEncode().value(), |
| 364 | data.wireEncode().value_size() - data.getSignature().getValue().size(), |
| 365 | keyName, digestAlgorithm)); |
| 366 | } |
| 367 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 370 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 371 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 372 | |
| 373 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 374 | #ifdef NDN_CPP_HAVE_OSX_SECURITY |
| 375 | |
| 376 | namespace ndn |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 377 | { |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 378 | typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmOsx> KeyChain; |
| 379 | }; |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 380 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 381 | #else |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 382 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 383 | namespace ndn |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 384 | { |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 385 | typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> KeyChain; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 386 | }; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 387 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 388 | #endif //NDN_CPP_HAVE_OSX_SECURITY |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 389 | |
| 390 | #endif |