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 | 4147191 | 2013-09-12 16:21:50 -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 | 06e787d | 2013-09-12 19:00:55 -0700 | [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 | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
| 9 | #ifndef NDN_IDENTITY_MANAGER_HPP |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 10 | #define NDN_IDENTITY_MANAGER_HPP |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 11 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 12 | #include "../certificate/identity-certificate.hpp" |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 13 | #include "identity-storage.hpp" |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 14 | #include "../certificate/public-key.hpp" |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 15 | #include "private-key-storage.hpp" |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 16 | |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 17 | namespace ndn { |
| 18 | |
Jeff Thompson | ffa36f9 | 2013-09-20 08:42:41 -0700 | [diff] [blame] | 19 | /** |
| 20 | * An IdentityManager is the interface of operations related to identity, keys, and certificates. |
| 21 | */ |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 22 | class IdentityManager { |
| 23 | public: |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 24 | IdentityManager(const ptr_lib::shared_ptr<IdentityStorage>& identityStorage, const ptr_lib::shared_ptr<PrivateKeyStorage>& privateKeyStorage) |
| 25 | : identityStorage_(identityStorage), privateKeyStorage_(privateKeyStorage) |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 26 | { |
| 27 | } |
| 28 | |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 29 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 30 | * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK. |
| 31 | * @param identityName The name of the identity. |
| 32 | * @return The key name of the auto-generated KSK of the identity. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 33 | */ |
| 34 | Name |
| 35 | createIdentity(const Name& identityName); |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Get the default identity. |
| 39 | * @return The default identity name. |
| 40 | */ |
| 41 | Name |
| 42 | getDefaultIdentity() |
| 43 | { |
| 44 | return identityStorage_->getDefaultIdentity(); |
| 45 | } |
| 46 | |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 47 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 48 | * Generate a pair of RSA keys for the specified identity. |
| 49 | * @param identityName The name of the identity. |
| 50 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 51 | * @param keySize The size of the key. |
| 52 | * @return The generated key name. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 53 | */ |
| 54 | Name |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 55 | generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048); |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 58 | * Set a key as the default key of an identity. |
| 59 | * @param keyName The name of the key. |
| 60 | * @param identityName the name of the identity. If not specified, the identity name is inferred from the keyName. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 61 | */ |
| 62 | void |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 63 | setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name()) |
| 64 | { |
| 65 | identityStorage_->setDefaultKeyNameForIdentity(keyName, identityName); |
| 66 | } |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Jeff Thompson | 18bf631 | 2013-10-04 11:23:55 -0700 | [diff] [blame] | 69 | * Get the default key for an identity. |
| 70 | * @param identityName the name of the identity. If omitted, the identity name is inferred from the keyName. |
| 71 | * @return The default key name. |
| 72 | */ |
| 73 | Name |
| 74 | getDefaultKeyNameForIdentity(const Name& identityName = Name()) |
| 75 | { |
| 76 | return identityStorage_->getDefaultKeyNameForIdentity(identityName); |
| 77 | } |
| 78 | |
| 79 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 80 | * Generate a pair of RSA keys for the specified identity and set it as default key for the identity. |
| 81 | * @param identityName The name of the identity. |
| 82 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 83 | * @param keySize The size of the key. |
| 84 | * @return The generated key name. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 85 | */ |
| 86 | Name |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 87 | generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048); |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 88 | |
| 89 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 90 | * Get the public key with the specified name. |
| 91 | * @param keyName The name of the key. |
| 92 | * @return The public key. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 93 | */ |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 94 | ptr_lib::shared_ptr<PublicKey> |
| 95 | getPublicKey(const Name& keyName) |
| 96 | { |
| 97 | return PublicKey::fromDer(identityStorage_->getKey(keyName)); |
| 98 | } |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 101 | * Create an identity certificate for a public key managed by this IdentityManager. |
| 102 | * @param keyName The name of public key to be signed. |
| 103 | * @param signerCertificateName The name of signing certificate. |
| 104 | * @param notBefore The notBefore value in the validity field of the generated certificate. |
| 105 | * @param notAfter The notAfter vallue in validity field of the generated certificate. |
| 106 | * @return The name of generated identity certificate. |
| 107 | */ |
| 108 | Name |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 109 | createIdentityCertificate(const Name& keyName, const Name& signerCertificateName, const MillisecondsSince1970& notBefore, const MillisecondsSince1970& notAfter); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Create an identity certificate for a public key supplied by the caller. |
| 113 | * @param keyName The name of public key to be signed. |
| 114 | * @param publickey The public key to be signed. |
| 115 | * @param signerCertificateName The name of signing certificate. |
| 116 | * @param notBefore The notBefore value in the validity field of the generated certificate. |
| 117 | * @param notAfter The notAfter vallue in validity field of the generated certificate. |
| 118 | * @return The generated identity certificate. |
| 119 | */ |
| 120 | ptr_lib::shared_ptr<IdentityCertificate> |
| 121 | createIdentityCertificate |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 122 | (const Name& keyName, const PublicKey& publickey, const Name& signerCertificateName, const MillisecondsSince1970& notBefore, const MillisecondsSince1970& notAfter); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 125 | * Add a certificate into the public key identity storage. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 126 | * @param certificate The certificate to to added. This makes a copy of the certificate. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 127 | */ |
| 128 | void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 129 | addCertificate(const IdentityCertificate& certificate) |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 130 | { |
| 131 | identityStorage_->addCertificate(certificate); |
| 132 | } |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 133 | |
| 134 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 135 | * Set the certificate as the default for its corresponding key. |
| 136 | * @param certificateName The name of the certificate. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 137 | */ |
| 138 | void |
| 139 | setDefaultCertificateForKey(const Name& certificateName); |
| 140 | |
| 141 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 142 | * Add a certificate into the public key identity storage and set the certificate as the default for its corresponding identity. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 143 | * @param certificate The certificate to be added. This makes a copy of the certificate. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 144 | */ |
| 145 | void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 146 | addCertificateAsIdentityDefault(const IdentityCertificate& certificate); |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 147 | |
| 148 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 149 | * Add a certificate into the public key identity storage and set the certificate as the default of its corresponding key. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 150 | * @param certificate The certificate to be added. This makes a copy of the certificate. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 151 | */ |
| 152 | void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 153 | addCertificateAsDefault(const IdentityCertificate& certificate); |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 154 | |
| 155 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 156 | * Get a certificate with the specified name. |
| 157 | * @param certificateName The name of the requested certificate. |
| 158 | * @return the requested certificate which is valid. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 159 | */ |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 160 | ptr_lib::shared_ptr<IdentityCertificate> |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 161 | getCertificate(const Name& certificateName) |
| 162 | { |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 163 | return ptr_lib::make_shared<IdentityCertificate>(*identityStorage_->getCertificate(certificateName, false)); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 164 | } |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 165 | |
| 166 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 167 | * Get a certificate even if the certificate is not valid anymore. |
| 168 | * @param certificateName The name of the requested certificate. |
| 169 | * @return the requested certificate. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 170 | */ |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 171 | ptr_lib::shared_ptr<IdentityCertificate> |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 172 | getAnyCertificate(const Name& certificateName) |
| 173 | { |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 174 | return ptr_lib::make_shared<IdentityCertificate>(*identityStorage_->getCertificate(certificateName, true)); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 175 | } |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * Get the default certificate name for the specified identity, which will be used when signing is performed based on identity. |
| 179 | * @param identityName The name of the specified identity. |
| 180 | * @return The requested certificate name. |
| 181 | */ |
| 182 | Name |
| 183 | getDefaultCertificateNameForIdentity(const Name& identityName) |
| 184 | { |
| 185 | return identityStorage_->getDefaultCertificateNameForIdentity(identityName); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Get the default certificate name of the default identity, which will be used when signing is based on identity and |
| 190 | * the identity is not specified. |
| 191 | * @return The requested certificate name. |
| 192 | */ |
| 193 | Name |
| 194 | getDefaultCertificateName() |
| 195 | { |
| 196 | return identityStorage_->getDefaultCertificateNameForIdentity(getDefaultIdentity()); |
| 197 | } |
| 198 | |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 199 | /** |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 200 | * Sign the byte array data based on the certificate name. |
| 201 | * @param data The data to be signed. |
| 202 | * @param dataLength the length of data. |
| 203 | * @param certificateName The signing certificate name. |
| 204 | * @return The generated signature. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 205 | */ |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 206 | ptr_lib::shared_ptr<Signature> |
| 207 | signByCertificate(const uint8_t* data, size_t dataLength, const Name& certificateName); |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 208 | |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 209 | /** |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 210 | * Sign data packet based on the certificate name. |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 211 | * Note: the caller must make sure the timestamp in data is correct, for example with |
| 212 | * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0). |
| 213 | * @param data The Data object to sign and update its signature. |
| 214 | * @param certificateName The Name identifying the certificate which identifies the signing key. |
| 215 | * @param wireFormat The WireFormat for calling encodeData, or WireFormat::getDefaultWireFormat() if omitted. |
| 216 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 217 | void |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 218 | signByCertificate(Data& data, const Name& certificateName, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()); |
| 219 | |
| 220 | private: |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Generate a key pair for the specified identity. |
| 223 | * @param identityName The name of the specified identity. |
| 224 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 225 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 226 | * @param keySize The size of the key pair. |
| 227 | * @return The name of the generated key. |
| 228 | */ |
| 229 | Name |
| 230 | generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048); |
| 231 | |
| 232 | /** |
| 233 | * Generate a self-signed certificate for a public key. |
| 234 | * @param keyName The name of the public key. |
| 235 | * @return The generated certificate. |
| 236 | */ |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 237 | ptr_lib::shared_ptr<IdentityCertificate> |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 238 | selfSign(const Name& keyName); |
| 239 | |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 240 | ptr_lib::shared_ptr<IdentityStorage> identityStorage_; |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 241 | ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_; |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | } |
| 245 | |
| 246 | #endif |