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