blob: c4efe3c0033f3808edea6842bb84bcc4bbc26bbe [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 Yu2abd73f2014-01-08 23:34:11 -080012#include "certificate/identity-certificate.hpp"
13#include "certificate/public-key.hpp"
Yingdi Yu31b4af22014-01-14 14:13:00 -080014#include "signature/signature-sha256-with-rsa.hpp"
15
16#include "identity/sec-public-info-sqlite3.hpp"
17#include "identity/sec-public-info-memory.hpp"
18#include "identity/sec-tpm-osx.hpp"
19#include "identity/sec-tpm-memory.hpp"
Yingdi Yu2abd73f2014-01-08 23:34:11 -080020
Jeff Thompson47c93cf2013-08-09 00:38:48 -070021
22namespace ndn {
23
Jeff Thompson2ce8f492013-09-17 18:01:25 -070024/**
Yingdi Yu2abd73f2014-01-08 23:34:11 -080025 * KeyChain is one of the main classes of the security library.
Jeff Thompsonffa36f92013-09-20 08:42:41 -070026 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -080027 * The KeyChain class provides a set of interfaces of identity management and private key related operations.
Jeff Thompsonffa36f92013-09-20 08:42:41 -070028 */
Yingdi Yu31b4af22014-01-14 14:13:00 -080029template<class Info, class Tpm>
30class KeyChainImpl : public Info, public Tpm
31{
Jeff Thompson47c93cf2013-08-09 00:38:48 -070032public:
Yingdi Yu31b4af22014-01-14 14:13:00 -080033 // struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
Yingdi Yu2abd73f2014-01-08 23:34:11 -080034
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 Yu31b4af22014-01-14 14:13:00 -080041 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 Yu2abd73f2014-01-08 23:34:11 -080057
58 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -080059 * 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 Yu31b4af22014-01-14 14:13:00 -080066 generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048)
Yingdi Yu2abd73f2014-01-08 23:34:11 -080067 {
Yingdi Yu31b4af22014-01-14 14:13:00 -080068 return generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize);
Yingdi Yu2abd73f2014-01-08 23:34:11 -080069 }
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080070
Yingdi Yu2abd73f2014-01-08 23:34:11 -080071 /**
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 Yu31b4af22014-01-14 14:13:00 -080079 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);
84
85 Info::refreshDefaultCertificate();
86
87 return keyName;
88 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070089
Yingdi Yu2abd73f2014-01-08 23:34:11 -080090 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -080091 * Create an identity certificate for a public key managed by this IdentityManager.
92 * @param certificatePrefix The name of public key to be signed.
93 * @param signerCertificateName The name of signing certificate.
94 * @param notBefore The notBefore value in the validity field of the generated certificate.
95 * @param notAfter The notAfter vallue in validity field of the generated certificate.
96 * @return The name of generated identity certificate.
97 */
98 ptr_lib::shared_ptr<IdentityCertificate>
99 createIdentityCertificate
100 (const Name& certificatePrefix,
101 const Name& signerCertificateName,
102 const MillisecondsSince1970& notBefore,
Yingdi Yu31b4af22014-01-14 14:13:00 -0800103 const MillisecondsSince1970& notAfter)
104 {
105 Name keyName = getKeyNameFromCertificatePrefix(certificatePrefix);
106
107 ptr_lib::shared_ptr<PublicKey> pubKey = Info::getPublicKey(keyName);
108 if (!pubKey)
109 throw std::runtime_error("Requested public key [" + keyName.toUri() + "] doesn't exist");
110
111 ptr_lib::shared_ptr<IdentityCertificate> certificate =
112 createIdentityCertificate(certificatePrefix,
113 *pubKey,
114 signerCertificateName,
115 notBefore, notAfter);
116
117 Info::addCertificate(*certificate);
118
119 return certificate;
120 }
121
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700122
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800123 /**
124 * Create an identity certificate for a public key supplied by the caller.
125 * @param certificatePrefix The name of public key to be signed.
126 * @param publickey The public key to be signed.
127 * @param signerCertificateName The name of signing certificate.
128 * @param notBefore The notBefore value in the validity field of the generated certificate.
129 * @param notAfter The notAfter vallue in validity field of the generated certificate.
130 * @return The generated identity certificate.
131 */
132 ptr_lib::shared_ptr<IdentityCertificate>
133 createIdentityCertificate
134 (const Name& certificatePrefix,
Yingdi Yu31b4af22014-01-14 14:13:00 -0800135 const PublicKey& publicKey,
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800136 const Name& signerCertificateName,
137 const MillisecondsSince1970& notBefore,
Yingdi Yu31b4af22014-01-14 14:13:00 -0800138 const MillisecondsSince1970& notAfter)
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800139 {
Yingdi Yu31b4af22014-01-14 14:13:00 -0800140 ptr_lib::shared_ptr<IdentityCertificate> certificate (new IdentityCertificate());
141 Name keyName = getKeyNameFromCertificatePrefix(certificatePrefix);
142
143 Name certificateName = certificatePrefix;
144 certificateName.append("ID-CERT").appendVersion();
145
146 certificate->setName(certificateName);
147 certificate->setNotBefore(notBefore);
148 certificate->setNotAfter(notAfter);
149 certificate->setPublicKeyInfo(publicKey);
150 certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri()));
151 certificate->encode();
152
153 sign(*certificate, signerCertificateName);
154
155 return certificate;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800156 }
157
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800158 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800159 sign(Data &data)
160 {
161 if (!Info::defaultCertificate())
162 {
163 Info::refreshDefaultCertificate();
164
165 if(!Info::defaultCertificate())
166 throw std::runtime_error("Default IdentityCertificate cannot be determined");
167 }
168
169 sign(data, *Info::defaultCertificate());
170 }
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800171
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700172 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700173 * Wire encode the Data object, sign it and set its signature.
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700174 * @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 -0700175 * @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 -0700176 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800177 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800178 sign(Data& data, const Name& certificateName)
179 {
180 ptr_lib::shared_ptr<IdentityCertificate> cert = Info::getCertificate(certificateName);
181 if (!cert)
182 throw std::runtime_error("Requested certificate [" + certificateName.toUri() + "] doesn't exist");
183
184 SignatureSha256WithRsa signature;
185 signature.setKeyLocator(certificateName.getPrefix(-1)); // implicit conversion should take care
186 data.setSignature(signature);
187
188 // For temporary usage, we support RSA + SHA256 only, but will support more.
189 Tpm::sign(data, cert->getPublicKeyName(), DIGEST_ALGORITHM_SHA256);
190 }
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800191
192 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800193 sign(Data& data, const IdentityCertificate& certificate)
194 {
195 SignatureSha256WithRsa signature;
196 signature.setKeyLocator(certificate.getName().getPrefix(-1));
197 data.setSignature(signature);
198
199 // For temporary usage, we support RSA + SHA256 only, but will support more.
200 Tpm::sign(data, certificate.getPublicKeyName(), DIGEST_ALGORITHM_SHA256);
201 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700202
Jeff Thompson29ce3102013-09-27 11:47:48 -0700203 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700204 * Sign the byte array using a certificate name and return a Signature object.
205 * @param buffer The byte array to be signed.
206 * @param bufferLength the length of buffer.
207 * @param certificateName The certificate name used to get the signing key and which will be put into KeyLocator.
208 * @return The Signature.
209 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800210 Signature
Yingdi Yu31b4af22014-01-14 14:13:00 -0800211 sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName)
212 {
213 ptr_lib::shared_ptr<IdentityCertificate> cert = Info::getCertificate(certificateName);
214 if (!cert)
215 throw std::runtime_error("Requested certificate [" + certificateName.toUri() + "] doesn't exist");
216
217 SignatureSha256WithRsa signature;
218 signature.setKeyLocator(certificateName.getPrefix(-1)); // implicit conversion should take care
219
220 // For temporary usage, we support RSA + SHA256 only, but will support more.
221 signature.setValue(Tpm::sign(buffer, bufferLength, cert->getPublicKeyName(), DIGEST_ALGORITHM_SHA256));
222 return signature;
223 }
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700224
225 /**
Jeff Thompson29ce3102013-09-27 11:47:48 -0700226 * Wire encode the Data object, sign it and set its signature.
Jeff Thompson29ce3102013-09-27 11:47:48 -0700227 * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding.
228 * @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 -0700229 */
230 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800231 signByIdentity(Data& data, const Name& identityName = Name())
232 {
233 Name signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName);
234
235 if (signingCertificateName.getComponentCount() == 0)
236 throw std::runtime_error("No qualified certificate name found!");
237
238 sign(data, signingCertificateName);
239 }
Jeff Thompson3c73da42013-08-12 11:19:05 -0700240
241 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700242 * Sign the byte array using an identity name and return a Signature object.
243 * @param buffer The byte array to be signed.
244 * @param bufferLength the length of buffer.
245 * @param identityName The identity name.
246 * @return The Signature.
247 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800248 Signature
Yingdi Yu31b4af22014-01-14 14:13:00 -0800249 signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName = Name())
250 {
251 Name signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName);
252
253 if (signingCertificateName.size() == 0)
254 throw std::runtime_error("No qualified certificate name found!");
255
256 return sign(buffer, bufferLength, signingCertificateName);
257 }
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700258
259 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800260 * Generate a self-signed certificate for a public key.
261 * @param keyName The name of the public key.
262 * @return The generated certificate.
263 */
264 ptr_lib::shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800265 selfSign(const Name& keyName)
266 {
267 ptr_lib::shared_ptr<IdentityCertificate> certificate = ptr_lib::make_shared<IdentityCertificate>();
268
269 Name certificateName = keyName.getPrefix(-1);
270 certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
271
272 ptr_lib::shared_ptr<PublicKey> pubKey = Info::getPublicKey(keyName);
273 if (!pubKey)
274 throw std::runtime_error("Requested public key [" + keyName.toUri() + "] doesn't exist");
275
276 certificate->setName(certificateName);
277 certificate->setNotBefore(getNow());
278 certificate->setNotAfter(getNow() + 630720000 /* 20 years*/);
279 certificate->setPublicKeyInfo(*pubKey);
280 certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri()));
281 certificate->encode();
282
283 selfSign(*certificate);
284 return certificate;
285 }
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800286
287 /**
288 * @brief Self-sign the supplied identity certificate
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700289 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700290 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800291 selfSign (IdentityCertificate& cert)
292 {
293 SignatureSha256WithRsa signature;
294 signature.setKeyLocator(cert.getName().getPrefix(-1)); // implicit conversion should take care
295 cert.setSignature(signature);
296
297 // For temporary usage, we support RSA + SHA256 only, but will support more.
298 Tpm::sign(cert, cert.getPublicKeyName(), DIGEST_ALGORITHM_SHA256);
299 }
300
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700301
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800302private:
303 /**
304 * Generate a key pair for the specified identity.
305 * @param identityName The name of the specified identity.
306 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
307 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
308 * @param keySize The size of the key pair.
309 * @return The name of the generated key.
310 */
311 Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800312 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048)
313 {
314 Name keyName = Info::getNewKeyName(identityName, isKsk);
315
316 Tpm::generateKeyPairInTpm(keyName.toUri(), keyType, keySize);
317
318 ptr_lib::shared_ptr<PublicKey> pubKey = Tpm::getPublicKeyFromTpm(keyName.toUri());
319 Tpm::addPublicKey(keyName, keyType, *pubKey);
320
321 return keyName;
322 }
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800323
324 static Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800325 getKeyNameFromCertificatePrefix(const Name& certificatePrefix)
326 {
327 Name result;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800328
Yingdi Yu31b4af22014-01-14 14:13:00 -0800329 std::string keyString("KEY");
330 int i = 0;
331 for(; i < certificatePrefix.size(); i++) {
332 if (certificatePrefix.get(i).toEscapedString() == keyString)
333 break;
334 }
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800335
Yingdi Yu31b4af22014-01-14 14:13:00 -0800336 if (i >= certificatePrefix.size())
337 throw std::runtime_error("Identity Certificate Prefix does not have a KEY component");
Alexander Afanasyevbd5ba402014-01-05 22:41:09 -0800338
Yingdi Yu31b4af22014-01-14 14:13:00 -0800339 result.append(certificatePrefix.getSubName(0, i));
340 result.append(certificatePrefix.getSubName(i + 1, certificatePrefix.size()-i-1));
341
342 return result;
343 }
344
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700345};
346
Yingdi Yu31b4af22014-01-14 14:13:00 -0800347}
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800348
Yingdi Yu31b4af22014-01-14 14:13:00 -0800349#ifdef NDN_CPP_HAVE_OSX_SECURITY
350
351namespace ndn
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800352{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800353typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmOsx> KeyChain;
354};
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800355
Yingdi Yu31b4af22014-01-14 14:13:00 -0800356#else
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800357
Yingdi Yu31b4af22014-01-14 14:13:00 -0800358namespace ndn
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800359{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800360typedef KeyChainImpl<SecPublicInfoMemory, SecTpmMemory> KeyChain;
361};
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800362
Yingdi Yu31b4af22014-01-14 14:13:00 -0800363#endif //NDN_CPP_HAVE_OSX_SECURITY
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700364
365#endif