blob: 21898e662b7925792edddfacb95cbcc44e33cd5f [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47c93cf2013-08-09 00:38:48 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompsonba16b8f2013-12-16 13:11:47 -08004 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson7687dc02013-09-13 11:54:07 -07005 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47c93cf2013-08-09 00:38:48 -07006 * See COPYING for copyright and distribution information.
7 */
8
9#ifndef NDN_KEY_CHAIN_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070010#define NDN_KEY_CHAIN_HPP
Jeff Thompson47c93cf2013-08-09 00:38:48 -070011
Yingdi Yu4f324632014-01-15 18:10:03 -080012#include "identity-certificate.hpp"
13#include "public-key.hpp"
14#include "signature-sha256-with-rsa.hpp"
Yingdi Yu31b4af22014-01-14 14:13:00 -080015
Yingdi Yu04020922014-01-22 12:46:53 -080016//PublicInfo
Yingdi Yu4f324632014-01-15 18:10:03 -080017#include "sec-public-info-sqlite3.hpp"
18#include "sec-public-info-memory.hpp"
Yingdi Yu04020922014-01-22 12:46:53 -080019//TPM
20#include "sec-tpm-file.hpp"
Yingdi Yu4f324632014-01-15 18:10:03 -080021#include "sec-tpm-memory.hpp"
Yingdi Yu2abd73f2014-01-08 23:34:11 -080022
Yingdi Yu04020922014-01-22 12:46:53 -080023#ifdef NDN_CPP_HAVE_OSX_SECURITY
24#include "sec-tpm-osx.hpp"
25#endif
26
Jeff Thompson47c93cf2013-08-09 00:38:48 -070027
28namespace ndn {
29
Jeff Thompson2ce8f492013-09-17 18:01:25 -070030/**
Yingdi Yu2abd73f2014-01-08 23:34:11 -080031 * KeyChain is one of the main classes of the security library.
Jeff Thompsonffa36f92013-09-20 08:42:41 -070032 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -080033 * The KeyChain class provides a set of interfaces of identity management and private key related operations.
Jeff Thompsonffa36f92013-09-20 08:42:41 -070034 */
Yingdi Yu31b4af22014-01-14 14:13:00 -080035template<class Info, class Tpm>
36class KeyChainImpl : public Info, public Tpm
37{
Jeff Thompson47c93cf2013-08-09 00:38:48 -070038public:
Yingdi Yu31b4af22014-01-14 14:13:00 -080039 // struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
Yingdi Yu2abd73f2014-01-08 23:34:11 -080040
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 Yu31b4af22014-01-14 14:13:00 -080047 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 Yu2abd73f2014-01-08 23:34:11 -080063
64 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -080065 * 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 Yu31b4af22014-01-14 14:13:00 -080072 generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048)
Yingdi Yu2abd73f2014-01-08 23:34:11 -080073 {
Yingdi Yu31b4af22014-01-14 14:13:00 -080074 return generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize);
Yingdi Yu2abd73f2014-01-08 23:34:11 -080075 }
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080076
Yingdi Yu2abd73f2014-01-08 23:34:11 -080077 /**
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 Yu31b4af22014-01-14 14:13:00 -080085 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 Yu31b4af22014-01-14 14:13:00 -080090
91 return keyName;
92 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070093
Yingdi Yu2abd73f2014-01-08 23:34:11 -080094 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -080095 * 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 Yu31b4af22014-01-14 14:13:00 -0800107 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 Thompson79a2d5d2013-09-27 14:32:23 -0700126
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800127 /**
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 Yu31b4af22014-01-14 14:13:00 -0800139 const PublicKey& publicKey,
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800140 const Name& signerCertificateName,
141 const MillisecondsSince1970& notBefore,
Yingdi Yu31b4af22014-01-14 14:13:00 -0800142 const MillisecondsSince1970& notAfter)
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800143 {
Yingdi Yu31b4af22014-01-14 14:13:00 -0800144 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 Afanasyevbf1a67a2014-01-05 23:36:13 -0800160 }
161
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800162 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800163 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 Afanasyeve64788e2014-01-05 22:38:21 -0800175
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700176 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700177 * Wire encode the Data object, sign it and set its signature.
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700178 * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700179 * @param certificateName The certificate name of the key to use for signing. If omitted, infer the signing identity from the data packet name.
Jeff Thompson3c73da42013-08-12 11:19:05 -0700180 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800181 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800182 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 Yu8726f652014-01-23 10:35:12 -0800193 signDataInTpm(data, cert->getPublicKeyName(), DIGEST_ALGORITHM_SHA256);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800194 }
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800195
196 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800197 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 Yu8726f652014-01-23 10:35:12 -0800204 signDataInTpm(data, certificate.getPublicKeyName(), DIGEST_ALGORITHM_SHA256);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800205 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700206
Jeff Thompson29ce3102013-09-27 11:47:48 -0700207 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700208 * 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 Yu2abd73f2014-01-08 23:34:11 -0800214 Signature
Yingdi Yu31b4af22014-01-14 14:13:00 -0800215 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 Yub4bb85a2014-01-16 10:11:04 -0800225 signature.setValue(Tpm::signInTpm(buffer, bufferLength, cert->getPublicKeyName(), DIGEST_ALGORITHM_SHA256));
Yingdi Yu31b4af22014-01-14 14:13:00 -0800226 return signature;
227 }
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700228
229 /**
Jeff Thompson29ce3102013-09-27 11:47:48 -0700230 * Wire encode the Data object, sign it and set its signature.
Jeff Thompson29ce3102013-09-27 11:47:48 -0700231 * @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 Thompson29ce3102013-09-27 11:47:48 -0700233 */
234 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800235 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 Thompson3c73da42013-08-12 11:19:05 -0700244
245 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700246 * 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 Afanasyev64a3d812014-01-05 23:35:05 -0800252 Signature
Yingdi Yu31b4af22014-01-14 14:13:00 -0800253 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 Thompsonc01e1782013-10-21 14:08:42 -0700262
263 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800264 * 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 Yu31b4af22014-01-14 14:13:00 -0800269 selfSign(const Name& keyName)
270 {
Yingdi Yu7ea69502014-01-15 17:21:29 -0800271 if(keyName.empty())
272 throw std::runtime_error("Incorrect key name: " + keyName.toUri());
273
Yingdi Yu31b4af22014-01-14 14:13:00 -0800274 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 Yu2abd73f2014-01-08 23:34:11 -0800293
294 /**
295 * @brief Self-sign the supplied identity certificate
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700296 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700297 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800298 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 Yu8726f652014-01-23 10:35:12 -0800305 signDataInTpm(cert, cert.getPublicKeyName(), DIGEST_ALGORITHM_SHA256);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800306 }
307
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700308
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800309private:
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 Yu31b4af22014-01-14 14:13:00 -0800319 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 Yuef26ee32014-01-15 16:41:14 -0800326 Info::addPublicKey(keyName, keyType, *pubKey);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800327
328 return keyName;
329 }
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800330
331 static Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800332 getKeyNameFromCertificatePrefix(const Name& certificatePrefix)
333 {
334 Name result;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800335
Yingdi Yu31b4af22014-01-14 14:13:00 -0800336 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 Afanasyevbf1a67a2014-01-05 23:36:13 -0800342
Yingdi Yu31b4af22014-01-14 14:13:00 -0800343 if (i >= certificatePrefix.size())
344 throw std::runtime_error("Identity Certificate Prefix does not have a KEY component");
Alexander Afanasyevbd5ba402014-01-05 22:41:09 -0800345
Yingdi Yu31b4af22014-01-14 14:13:00 -0800346 result.append(certificatePrefix.getSubName(0, i));
347 result.append(certificatePrefix.getSubName(i + 1, certificatePrefix.size()-i-1));
348
349 return result;
350 }
351
Yingdi Yu8726f652014-01-23 10:35:12 -0800352 /**
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 Thompson47c93cf2013-08-09 00:38:48 -0700368};
369
Yingdi Yu31b4af22014-01-14 14:13:00 -0800370}
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800371
Yingdi Yu04020922014-01-22 12:46:53 -0800372
373
Yingdi Yu31b4af22014-01-14 14:13:00 -0800374#ifdef NDN_CPP_HAVE_OSX_SECURITY
375
376namespace ndn
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800377{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800378typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmOsx> KeyChain;
379};
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800380
Yingdi Yu31b4af22014-01-14 14:13:00 -0800381#else
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800382
Yingdi Yu31b4af22014-01-14 14:13:00 -0800383namespace ndn
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800384{
Yingdi Yu04020922014-01-22 12:46:53 -0800385typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> KeyChain;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800386};
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800387
Yingdi Yu31b4af22014-01-14 14:13:00 -0800388#endif //NDN_CPP_HAVE_OSX_SECURITY
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700389
390#endif