blob: 5f9854ed7725e1f01df55ffdc4507b9a96ba9d30 [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 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -080054 * Generate a pair of RSA keys for the specified identity.
55 * @param identityName The name of the identity.
56 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
57 * @param keySize The size of the key.
58 * @return The generated key name.
59 */
60 Name
61 generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048);
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080062
63 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -080064 * Set a key as the default key of an identity.
65 * @param keyName The name of the key.
66 * @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 -080067 */
68 void
Yingdi Yu2abd73f2014-01-08 23:34:11 -080069 setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name())
70 {
71 info().setDefaultKeyNameForIdentity(keyName, identityName);
72 defaultCertificate_.reset();
73 }
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080074
Yingdi Yu2abd73f2014-01-08 23:34:11 -080075 /**
76 * Generate a pair of RSA keys for the specified identity and set it as default key for the identity.
77 * @param identityName The name of the identity.
78 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
79 * @param keySize The size of the key.
80 * @return The generated key name.
81 */
82 Name
83 generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070084
Yingdi Yu2abd73f2014-01-08 23:34:11 -080085 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -080086 * Create an identity certificate for a public key managed by this IdentityManager.
87 * @param certificatePrefix The name of public key to be signed.
88 * @param signerCertificateName The name of signing certificate.
89 * @param notBefore The notBefore value in the validity field of the generated certificate.
90 * @param notAfter The notAfter vallue in validity field of the generated certificate.
91 * @return The name of generated identity certificate.
92 */
93 ptr_lib::shared_ptr<IdentityCertificate>
94 createIdentityCertificate
95 (const Name& certificatePrefix,
96 const Name& signerCertificateName,
97 const MillisecondsSince1970& notBefore,
98 const MillisecondsSince1970& notAfter);
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070099
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800100 /**
101 * Create an identity certificate for a public key supplied by the caller.
102 * @param certificatePrefix The name of public key to be signed.
103 * @param publickey The public key to be signed.
104 * @param signerCertificateName The name of signing certificate.
105 * @param notBefore The notBefore value in the validity field of the generated certificate.
106 * @param notAfter The notAfter vallue in validity field of the generated certificate.
107 * @return The generated identity certificate.
108 */
109 ptr_lib::shared_ptr<IdentityCertificate>
110 createIdentityCertificate
111 (const Name& certificatePrefix,
112 const PublicKey& publickey,
113 const Name& signerCertificateName,
114 const MillisecondsSince1970& notBefore,
115 const MillisecondsSince1970& notAfter);
116
117 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800118 * Set the certificate as the default for its corresponding key.
119 * @param certificateName The certificate.
120 */
121 void
122 setDefaultCertificateForKey(const IdentityCertificate& certificate);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800123
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800124 /**
125 * Add a certificate into the public key identity storage and set the certificate as the default for its corresponding identity.
126 * @param certificate The certificate to be added. This makes a copy of the certificate.
127 */
128 void
129 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
130
131 /**
132 * Add a certificate into the public key identity storage and set the certificate as the default of its corresponding key.
133 * @param certificate The certificate to be added. This makes a copy of the certificate.
134 */
135 void
136 addCertificateAsDefault(const IdentityCertificate& certificate);
Yingdi Yu462688f2014-01-13 16:34:52 -0800137
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800138 /**
139 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
140 * the identity is not specified.
141 * @return The requested certificate name.
142 */
143 Name
144 getDefaultCertificateName()
145 {
Yingdi Yu462688f2014-01-13 16:34:52 -0800146 return info().getDefaultCertificateNameForIdentity(info().getDefaultIdentity());
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800147 }
148
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800149 void
150 sign(Data &data);
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800151
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700152 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700153 * Wire encode the Data object, sign it and set its signature.
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700154 * @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 -0700155 * @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 -0700156 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800157 void
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800158 sign(Data& data, const Name& certificateName);
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800159
160 void
161 sign(Data& data, const IdentityCertificate& certificate);
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700162
Jeff Thompson29ce3102013-09-27 11:47:48 -0700163 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700164 * Sign the byte array using a certificate name and return a Signature object.
165 * @param buffer The byte array to be signed.
166 * @param bufferLength the length of buffer.
167 * @param certificateName The certificate name used to get the signing key and which will be put into KeyLocator.
168 * @return The Signature.
169 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800170 Signature
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700171 sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
172
173 /**
Jeff Thompson29ce3102013-09-27 11:47:48 -0700174 * Wire encode the Data object, sign it and set its signature.
Jeff Thompson29ce3102013-09-27 11:47:48 -0700175 * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding.
176 * @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 -0700177 */
178 void
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800179 signByIdentity(Data& data, const Name& identityName = Name());
Jeff Thompson3c73da42013-08-12 11:19:05 -0700180
181 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700182 * Sign the byte array using an identity name and return a Signature object.
183 * @param buffer The byte array to be signed.
184 * @param bufferLength the length of buffer.
185 * @param identityName The identity name.
186 * @return The Signature.
187 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800188 Signature
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800189 signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName = Name());
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700190
191 /**
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800192 * Generate a self-signed certificate for a public key.
193 * @param keyName The name of the public key.
194 * @return The generated certificate.
195 */
196 ptr_lib::shared_ptr<IdentityCertificate>
197 selfSign(const Name& keyName);
198
199 /**
200 * @brief Self-sign the supplied identity certificate
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700201 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700202 void
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800203 selfSign (IdentityCertificate& cert);
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700204
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800205private:
206 /**
207 * Generate a key pair for the specified identity.
208 * @param identityName The name of the specified identity.
209 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
210 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
211 * @param keySize The size of the key pair.
212 * @return The name of the generated key.
213 */
214 Name
215 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048);
216
217 static Name
218 getKeyNameFromCertificatePrefix(const Name& certificatePrefix);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800219
220public:
Alexander Afanasyevbd5ba402014-01-05 22:41:09 -0800221 static const ptr_lib::shared_ptr<IdentityStorage> DefaultIdentityStorage;
222 static const ptr_lib::shared_ptr<PrivateKeyStorage> DefaultPrivateKeyStorage;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800223
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700224private:
Alexander Afanasyevbd5ba402014-01-05 22:41:09 -0800225 ptr_lib::shared_ptr<IdentityStorage> publicInfoStorage_;
226 ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_;
Alexander Afanasyevbd5ba402014-01-05 22:41:09 -0800227
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800228 ptr_lib::shared_ptr<IdentityCertificate> defaultCertificate_;
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700229};
230
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800231
232inline IdentityStorage&
233KeyChain::info()
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800234{
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800235 if (!publicInfoStorage_)
236 throw Error("IdentityStorage is not assigned to IdentityManager");
237
238 return *publicInfoStorage_;
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800239}
240
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800241inline const IdentityStorage&
242KeyChain::info() const
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800243{
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800244 if (!publicInfoStorage_)
245 throw Error("IdentityStorage is not assigned to IdentityManager");
246
247 return *publicInfoStorage_;
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800248}
249
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800250inline PrivateKeyStorage&
251KeyChain::tpm()
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800252{
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800253 if (!privateKeyStorage_)
254 throw Error("PrivateKeyStorage is not assigned to IdentityManager");
255
256 return *privateKeyStorage_;
257}
258
259inline const PrivateKeyStorage&
260KeyChain::tpm() const
261{
262 if (!privateKeyStorage_)
263 throw Error("PrivateKeyStorage is not assigned to IdentityManager");
264 return *privateKeyStorage_;
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800265}
266
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700267}
268
269#endif