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