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