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 | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 12 | #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 Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 17 | |
| 18 | namespace ndn { |
| 19 | |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 20 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 21 | * KeyChain is one of the main classes of the security library. |
Jeff Thompson | ffa36f9 | 2013-09-20 08:42:41 -0700 | [diff] [blame] | 22 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 23 | * 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] | 24 | */ |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 25 | class KeyChain { |
| 26 | public: |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 27 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 29 | KeyChain(const ptr_lib::shared_ptr<IdentityStorage> &identityStorage = DefaultIdentityStorage, |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 30 | 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 Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 54 | * 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 Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 62 | |
| 63 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 64 | * 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 Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 67 | */ |
| 68 | void |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 69 | setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name()) |
| 70 | { |
| 71 | info().setDefaultKeyNameForIdentity(keyName, identityName); |
| 72 | defaultCertificate_.reset(); |
| 73 | } |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 74 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 75 | /** |
| 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 Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 84 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 85 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 86 | * 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 Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 99 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 100 | /** |
| 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 Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 118 | * Set the certificate as the default for its corresponding key. |
| 119 | * @param certificateName The certificate. |
| 120 | */ |
| 121 | void |
| 122 | setDefaultCertificateForKey(const IdentityCertificate& certificate); |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 123 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 124 | /** |
| 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 Yu | 462688f | 2014-01-13 16:34:52 -0800 | [diff] [blame^] | 137 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 138 | /** |
| 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 Yu | 462688f | 2014-01-13 16:34:52 -0800 | [diff] [blame^] | 146 | return info().getDefaultCertificateNameForIdentity(info().getDefaultIdentity()); |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 149 | void |
| 150 | sign(Data &data); |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 151 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 152 | /** |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 153 | * Wire encode the Data object, sign it and set its signature. |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 154 | * @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] | 155 | * @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] | 156 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 157 | void |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 158 | sign(Data& data, const Name& certificateName); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 159 | |
| 160 | void |
| 161 | sign(Data& data, const IdentityCertificate& certificate); |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 162 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 163 | /** |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 164 | * 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 Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 170 | Signature |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 171 | sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName); |
| 172 | |
| 173 | /** |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 174 | * Wire encode the Data object, sign it and set its signature. |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 175 | * @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 Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 177 | */ |
| 178 | void |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 179 | signByIdentity(Data& data, const Name& identityName = Name()); |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 180 | |
| 181 | /** |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 182 | * 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 Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 188 | Signature |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 189 | signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName = Name()); |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 190 | |
| 191 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 192 | * 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 Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 201 | */ |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 202 | void |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 203 | selfSign (IdentityCertificate& cert); |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 204 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 205 | private: |
| 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 Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 219 | |
| 220 | public: |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 221 | static const ptr_lib::shared_ptr<IdentityStorage> DefaultIdentityStorage; |
| 222 | static const ptr_lib::shared_ptr<PrivateKeyStorage> DefaultPrivateKeyStorage; |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 223 | |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 224 | private: |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 225 | ptr_lib::shared_ptr<IdentityStorage> publicInfoStorage_; |
| 226 | ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_; |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 227 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 228 | ptr_lib::shared_ptr<IdentityCertificate> defaultCertificate_; |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 229 | }; |
| 230 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 231 | |
| 232 | inline IdentityStorage& |
| 233 | KeyChain::info() |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 234 | { |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 235 | if (!publicInfoStorage_) |
| 236 | throw Error("IdentityStorage is not assigned to IdentityManager"); |
| 237 | |
| 238 | return *publicInfoStorage_; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 241 | inline const IdentityStorage& |
| 242 | KeyChain::info() const |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 243 | { |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 244 | if (!publicInfoStorage_) |
| 245 | throw Error("IdentityStorage is not assigned to IdentityManager"); |
| 246 | |
| 247 | return *publicInfoStorage_; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 250 | inline PrivateKeyStorage& |
| 251 | KeyChain::tpm() |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 252 | { |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 253 | if (!privateKeyStorage_) |
| 254 | throw Error("PrivateKeyStorage is not assigned to IdentityManager"); |
| 255 | |
| 256 | return *privateKeyStorage_; |
| 257 | } |
| 258 | |
| 259 | inline const PrivateKeyStorage& |
| 260 | KeyChain::tpm() const |
| 261 | { |
| 262 | if (!privateKeyStorage_) |
| 263 | throw Error("PrivateKeyStorage is not assigned to IdentityManager"); |
| 264 | return *privateKeyStorage_; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | #endif |