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 | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 68 | * Generate a pair of RSA keys for the specified identity and set it as default key for the identity. |
| 69 | * @param identityName The name of the identity. |
| 70 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 71 | * @param keySize The size of the key. |
| 72 | * @return The generated key name. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 73 | */ |
| 74 | Name |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 75 | generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048); |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 78 | * Get the public key with the specified name. |
| 79 | * @param keyName The name of the key. |
| 80 | * @return The public key. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 81 | */ |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 82 | ptr_lib::shared_ptr<PublicKey> |
| 83 | getPublicKey(const Name& keyName) |
| 84 | { |
| 85 | return PublicKey::fromDer(identityStorage_->getKey(keyName)); |
| 86 | } |
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 | * Add a certificate into the public key identity storage. |
| 90 | * @param certificate The certificate to to added. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 91 | */ |
| 92 | void |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 93 | addCertificate(const Certificate& certificate) |
| 94 | { |
| 95 | identityStorage_->addCertificate(certificate); |
| 96 | } |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 99 | * Set the certificate as the default for its corresponding key. |
| 100 | * @param certificateName The name of the certificate. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 101 | */ |
| 102 | void |
| 103 | setDefaultCertificateForKey(const Name& certificateName); |
| 104 | |
| 105 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 106 | * Add a certificate into the public key identity storage and set the certificate as the default for its corresponding identity. |
| 107 | * @param certificate The certificate to be added. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 108 | */ |
| 109 | void |
| 110 | addCertificateAsIdentityDefault(const Certificate& certificate); |
| 111 | |
| 112 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 113 | * 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] | 114 | * certificate the certificate to be added |
| 115 | */ |
| 116 | void |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 117 | addCertificateAsDefault(const Certificate& certificate) |
| 118 | { |
| 119 | identityStorage_->addCertificate(certificate); |
| 120 | setDefaultCertificateForKey(certificate.getName()); |
| 121 | } |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 122 | |
| 123 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 124 | * Get a certificate with the specified name. |
| 125 | * @param certificateName The name of the requested certificate. |
| 126 | * @return the requested certificate which is valid. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 127 | */ |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 128 | ptr_lib::shared_ptr<Certificate> |
| 129 | getCertificate(const Name& certificateName) |
| 130 | { |
| 131 | return identityStorage_->getCertificate(certificateName, false); |
| 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 even if the certificate is not valid anymore. |
| 136 | * @param certificateName The name of the requested certificate. |
| 137 | * @return the requested certificate. |
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 | getAnyCertificate(const Name& certificateName) |
| 141 | { |
| 142 | return identityStorage_->getCertificate(certificateName, true); |
| 143 | } |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Get the default certificate name for the specified identity, which will be used when signing is performed based on identity. |
| 147 | * @param identityName The name of the specified identity. |
| 148 | * @return The requested certificate name. |
| 149 | */ |
| 150 | Name |
| 151 | getDefaultCertificateNameForIdentity(const Name& identityName) |
| 152 | { |
| 153 | return identityStorage_->getDefaultCertificateNameForIdentity(identityName); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get the default certificate name of the default identity, which will be used when signing is based on identity and |
| 158 | * the identity is not specified. |
| 159 | * @return The requested certificate name. |
| 160 | */ |
| 161 | Name |
| 162 | getDefaultCertificateName() |
| 163 | { |
| 164 | return identityStorage_->getDefaultCertificateNameForIdentity(getDefaultIdentity()); |
| 165 | } |
| 166 | |
| 167 | #if 0 |
| 168 | /** |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 169 | * sign blob based on certificate name |
| 170 | * @param blob the blob to be signed |
| 171 | * @param certificateName the signing certificate name |
| 172 | * @return the generated signature |
| 173 | */ |
| 174 | Ptr<Signature> |
| 175 | signByCertificate(const Blob& blob, const Name& certificateName); |
| 176 | #endif |
| 177 | |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 178 | /** |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 179 | * Sign data packet based on the certificate name. |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 180 | * Note: the caller must make sure the timestamp in data is correct, for example with |
| 181 | * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0). |
| 182 | * @param data The Data object to sign and update its signature. |
| 183 | * @param certificateName The Name identifying the certificate which identifies the signing key. |
| 184 | * @param wireFormat The WireFormat for calling encodeData, or WireFormat::getDefaultWireFormat() if omitted. |
| 185 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 186 | void |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 187 | signByCertificate(Data& data, const Name& certificateName, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()); |
| 188 | |
| 189 | private: |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame^] | 190 | /** |
| 191 | * Generate a key pair for the specified identity. |
| 192 | * @param identityName The name of the specified identity. |
| 193 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 194 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 195 | * @param keySize The size of the key pair. |
| 196 | * @return The name of the generated key. |
| 197 | */ |
| 198 | Name |
| 199 | generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048); |
| 200 | |
| 201 | /** |
| 202 | * Generate a self-signed certificate for a public key. |
| 203 | * @param keyName The name of the public key. |
| 204 | * @return The generated certificate. |
| 205 | */ |
| 206 | ptr_lib::shared_ptr<Certificate> |
| 207 | selfSign(const Name& keyName); |
| 208 | |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 209 | ptr_lib::shared_ptr<IdentityStorage> identityStorage_; |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 210 | ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_; |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | } |
| 214 | |
| 215 | #endif |