blob: 1dc8e7b94e47b73d5f235566301a95414da283bd [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"
14#include "identity/identity-storage.hpp"
15#include "identity/private-key-storage.hpp"
16
Jeff Thompson47c93cf2013-08-09 00:38:48 -070017
18namespace ndn {
19
Jeff Thompson2ce8f492013-09-17 18:01:25 -070020/**
Yingdi Yu2abd73f2014-01-08 23:34:11 -080021 * KeyChain is one of the main classes of the security library.
Jeff Thompsonffa36f92013-09-20 08:42:41 -070022 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -080023 * The KeyChain class provides a set of interfaces of identity management and private key related operations.
Jeff Thompsonffa36f92013-09-20 08:42:41 -070024 */
Jeff Thompson47c93cf2013-08-09 00:38:48 -070025class KeyChain {
26public:
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080027 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
Jeff Thompson2ce8f492013-09-17 18:01:25 -070028
Alexander Afanasyevbd5ba402014-01-05 22:41:09 -080029 KeyChain(const ptr_lib::shared_ptr<IdentityStorage> &identityStorage = DefaultIdentityStorage,
Yingdi Yu2abd73f2014-01-08 23:34:11 -080030 const ptr_lib::shared_ptr<PrivateKeyStorage> &privateKeyStorage = DefaultPrivateKeyStorage);
31
32 inline IdentityStorage&
33 info();
34
35 inline const IdentityStorage&
36 info() const;
37
38 inline PrivateKeyStorage&
39 tpm();
40
41 inline const PrivateKeyStorage&
42 tpm() const;
43
44
45 /**
46 * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK.
47 * @param identityName The name of the identity.
48 * @return The key name of the auto-generated KSK of the identity.
49 */
50 Name
51 createIdentity(const Name& identityName);
52
53 /**
54 * Get the default identity.
55 * @return The default identity name.
56 */
57 Name
58 getDefaultIdentity()
59 {
60 return info().getDefaultIdentity();
61 }
62
63 /**
64 * Generate a pair of RSA keys for the specified identity.
65 * @param identityName The name of the identity.
66 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
67 * @param keySize The size of the key.
68 * @return The generated key name.
69 */
70 Name
71 generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048);
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080072
73 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -080074 * Set a key as the default key of an identity.
75 * @param keyName The name of the key.
76 * @param identityName the name of the identity. If not specified, the identity name is inferred from the keyName.
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080077 */
78 void
Yingdi Yu2abd73f2014-01-08 23:34:11 -080079 setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name())
80 {
81 info().setDefaultKeyNameForIdentity(keyName, identityName);
82 defaultCertificate_.reset();
83 }
84
85 /**
86 * Get the default key for an identity.
87 * @param identityName the name of the identity. If omitted, the identity name is inferred from the keyName.
88 * @return The default key name.
89 */
90 Name
91 getDefaultKeyNameForIdentity(const Name& identityName = Name())
92 {
93 return info().getDefaultKeyNameForIdentity(identityName);
94 }
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080095
Yingdi Yu2abd73f2014-01-08 23:34:11 -080096 /**
97 * Generate a pair of RSA keys for the specified identity and set it as default key for the identity.
98 * @param identityName The name of the identity.
99 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
100 * @param keySize The size of the key.
101 * @return The generated key name.
102 */
103 Name
104 generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700105
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800106 /**
107 * Get the public key with the specified name.
108 * @param keyName The name of the key.
109 * @return The public key.
110 */
111 ptr_lib::shared_ptr<PublicKey>
112 getPublicKey(const Name& keyName)
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700113 {
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800114 return info().getKey(keyName);
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700115 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700116
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800117 /**
118 * Create an identity certificate for a public key managed by this IdentityManager.
119 * @param certificatePrefix The name of public key to be signed.
120 * @param signerCertificateName The name of signing certificate.
121 * @param notBefore The notBefore value in the validity field of the generated certificate.
122 * @param notAfter The notAfter vallue in validity field of the generated certificate.
123 * @return The name of generated identity certificate.
124 */
125 ptr_lib::shared_ptr<IdentityCertificate>
126 createIdentityCertificate
127 (const Name& certificatePrefix,
128 const Name& signerCertificateName,
129 const MillisecondsSince1970& notBefore,
130 const MillisecondsSince1970& notAfter);
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700131
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800132 /**
133 * Create an identity certificate for a public key supplied by the caller.
134 * @param certificatePrefix The name of public key to be signed.
135 * @param publickey The public key to be signed.
136 * @param signerCertificateName The name of signing certificate.
137 * @param notBefore The notBefore value in the validity field of the generated certificate.
138 * @param notAfter The notAfter vallue in validity field of the generated certificate.
139 * @return The generated identity certificate.
140 */
141 ptr_lib::shared_ptr<IdentityCertificate>
142 createIdentityCertificate
143 (const Name& certificatePrefix,
144 const PublicKey& publickey,
145 const Name& signerCertificateName,
146 const MillisecondsSince1970& notBefore,
147 const MillisecondsSince1970& notAfter);
148
149 /**
150 * Add a certificate into the public key identity storage.
151 * @param certificate The certificate to to added. This makes a copy of the certificate.
152 */
153 void
154 addCertificate(const IdentityCertificate& certificate)
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800155 {
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800156 info().addCertificate(certificate);
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800157 }
158
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800159 /**
160 * Set the certificate as the default for its corresponding key.
161 * @param certificateName The certificate.
162 */
163 void
164 setDefaultCertificateForKey(const IdentityCertificate& certificate);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800165
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800166 /**
167 * Add a certificate into the public key identity storage and set the certificate as the default for its corresponding identity.
168 * @param certificate The certificate to be added. This makes a copy of the certificate.
169 */
170 void
171 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
172
173 /**
174 * Add a certificate into the public key identity storage and set the certificate as the default of its corresponding key.
175 * @param certificate The certificate to be added. This makes a copy of the certificate.
176 */
177 void
178 addCertificateAsDefault(const IdentityCertificate& certificate);
179
180 /**
181 * Get a certificate with the specified name.
182 * @param certificateName The name of the requested certificate.
183 * @return the requested certificate which is valid.
184 */
185 ptr_lib::shared_ptr<IdentityCertificate>
186 getCertificate(const Name& certificateName)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800187 {
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800188 return info().getCertificate(certificateName, false);
189 }
190
191 /**
192 * Get a certificate even if the certificate is not valid anymore.
193 * @param certificateName The name of the requested certificate.
194 * @return the requested certificate.
195 */
196 ptr_lib::shared_ptr<IdentityCertificate>
197 getAnyCertificate(const Name& certificateName)
198 {
199 return info().getCertificate(certificateName, true);
200 }
201
202 /**
203 * Get the default certificate name for the specified identity, which will be used when signing is performed based on identity.
204 * @param identityName The name of the specified identity.
205 * @return The requested certificate name.
206 */
207 Name
208 getDefaultCertificateNameForIdentity(const Name& identityName)
209 {
210 return info().getDefaultCertificateNameForIdentity(identityName);
211 }
212
213 /**
214 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
215 * the identity is not specified.
216 * @return The requested certificate name.
217 */
218 Name
219 getDefaultCertificateName()
220 {
221 return info().getDefaultCertificateNameForIdentity(getDefaultIdentity());
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800222 }
223
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800224 void
225 sign(Data &data);
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800226
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700227 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700228 * Wire encode the Data object, sign it and set its signature.
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700229 * @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 -0700230 * @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 -0700231 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800232 void
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800233 sign(Data& data, const Name& certificateName);
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800234
235 void
236 sign(Data& data, const IdentityCertificate& certificate);
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700237
Jeff Thompson29ce3102013-09-27 11:47:48 -0700238 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700239 * Sign the byte array using a certificate name and return a Signature object.
240 * @param buffer The byte array to be signed.
241 * @param bufferLength the length of buffer.
242 * @param certificateName The certificate name used to get the signing key and which will be put into KeyLocator.
243 * @return The Signature.
244 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800245 Signature
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700246 sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
247
248 /**
Jeff Thompson29ce3102013-09-27 11:47:48 -0700249 * Wire encode the Data object, sign it and set its signature.
Jeff Thompson29ce3102013-09-27 11:47:48 -0700250 * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding.
251 * @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 -0700252 */
253 void
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800254 signByIdentity(Data& data, const Name& identityName = Name());
Jeff Thompson3c73da42013-08-12 11:19:05 -0700255
256 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700257 * Sign the byte array using an identity name and return a Signature object.
258 * @param buffer The byte array to be signed.
259 * @param bufferLength the length of buffer.
260 * @param identityName The identity name.
261 * @return The Signature.
262 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800263 Signature
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800264 signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName = Name());
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700265
266 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800267 * Generate a self-signed certificate for a public key.
268 * @param keyName The name of the public key.
269 * @return The generated certificate.
270 */
271 ptr_lib::shared_ptr<IdentityCertificate>
272 selfSign(const Name& keyName);
273
274 /**
275 * @brief Self-sign the supplied identity certificate
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700276 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700277 void
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800278 selfSign (IdentityCertificate& cert);
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700279
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800280private:
281 /**
282 * Generate a key pair for the specified identity.
283 * @param identityName The name of the specified identity.
284 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
285 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
286 * @param keySize The size of the key pair.
287 * @return The name of the generated key.
288 */
289 Name
290 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048);
291
292 static Name
293 getKeyNameFromCertificatePrefix(const Name& certificatePrefix);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800294
295public:
Alexander Afanasyevbd5ba402014-01-05 22:41:09 -0800296 static const ptr_lib::shared_ptr<IdentityStorage> DefaultIdentityStorage;
297 static const ptr_lib::shared_ptr<PrivateKeyStorage> DefaultPrivateKeyStorage;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800298
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700299private:
Alexander Afanasyevbd5ba402014-01-05 22:41:09 -0800300 ptr_lib::shared_ptr<IdentityStorage> publicInfoStorage_;
301 ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_;
Alexander Afanasyevbd5ba402014-01-05 22:41:09 -0800302
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800303 ptr_lib::shared_ptr<IdentityCertificate> defaultCertificate_;
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700304};
305
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800306
307inline IdentityStorage&
308KeyChain::info()
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800309{
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800310 if (!publicInfoStorage_)
311 throw Error("IdentityStorage is not assigned to IdentityManager");
312
313 return *publicInfoStorage_;
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800314}
315
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800316inline const IdentityStorage&
317KeyChain::info() const
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800318{
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800319 if (!publicInfoStorage_)
320 throw Error("IdentityStorage is not assigned to IdentityManager");
321
322 return *publicInfoStorage_;
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800323}
324
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800325inline PrivateKeyStorage&
326KeyChain::tpm()
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800327{
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800328 if (!privateKeyStorage_)
329 throw Error("PrivateKeyStorage is not assigned to IdentityManager");
330
331 return *privateKeyStorage_;
332}
333
334inline const PrivateKeyStorage&
335KeyChain::tpm() const
336{
337 if (!privateKeyStorage_)
338 throw Error("PrivateKeyStorage is not assigned to IdentityManager");
339 return *privateKeyStorage_;
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800340}
341
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700342}
343
344#endif