blob: 2cecb04642f50c2c00ac5123b2e798c07d014080 [file] [log] [blame]
Jeff Thompson7ca11f22013-10-04 19:01:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson22285ec2013-10-22 17:43:02 -07005 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson7ca11f22013-10-04 19:01:30 -07006 * See COPYING for copyright and distribution information.
7 */
8
Yingdi Yufc40d872014-02-18 12:56:04 -08009#ifndef NDN_SECURITY_SEC_PUBLIC_INFO_SQLITE3_HPP
10#define NDN_SECURITY_SEC_PUBLIC_INFO_SQLITE3_HPP
Jeff Thompson7ca11f22013-10-04 19:01:30 -070011
Yingdi Yu4f324632014-01-15 18:10:03 -080012#include "../common.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080013#include "sec-public-info.hpp"
Jeff Thompson7ca11f22013-10-04 19:01:30 -070014
Yingdi Yu874678f2014-01-22 19:30:34 -080015struct sqlite3;
16
Yingdi Yufc40d872014-02-18 12:56:04 -080017namespace ndn {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070018
19/**
20 * BasicIdentityStorage extends IdentityStorage to implement a basic storage of identity, public keys and certificates
21 * using SQLite.
22 */
Yingdi Yu87581582014-01-14 14:28:39 -080023class SecPublicInfoSqlite3 : public SecPublicInfo {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070024public:
Yingdi Yu87581582014-01-14 14:28:39 -080025 struct Error : public SecPublicInfo::Error { Error(const std::string &what) : SecPublicInfo::Error(what) {} };
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080026
Yingdi Yu87581582014-01-14 14:28:39 -080027 SecPublicInfoSqlite3();
Jeff Thompson7ca11f22013-10-04 19:01:30 -070028
29 /**
30 * The virtual Destructor.
31 */
32 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080033 ~SecPublicInfoSqlite3();
Jeff Thompson7ca11f22013-10-04 19:01:30 -070034
Yingdi Yu87581582014-01-14 14:28:39 -080035 // from SecPublicInfo
Jeff Thompson7ca11f22013-10-04 19:01:30 -070036 /**
37 * Check if the specified identity already exists.
38 * @param identityName The identity name.
39 * @return true if the identity exists, otherwise false.
40 */
41 virtual bool
42 doesIdentityExist(const Name& identityName);
43
44 /**
45 * Add a new identity. An exception will be thrown if the identity already exists.
46 * @param identityName The identity name to be added.
47 */
48 virtual void
49 addIdentity(const Name& identityName);
50
51 /**
52 * Revoke the identity.
53 * @return true if the identity was revoked, false if not.
54 */
55 virtual bool
56 revokeIdentity();
57
58 /**
Jeff Thompson7ca11f22013-10-04 19:01:30 -070059 * Check if the specified key already exists.
60 * @param keyName The name of the key.
61 * @return true if the key exists, otherwise false.
62 */
63 virtual bool
Yingdi Yu87581582014-01-14 14:28:39 -080064 doesPublicKeyExist(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070065
66 /**
Jeff Thompson7ca11f22013-10-04 19:01:30 -070067 * Add a public key to the identity storage.
68 * @param keyName The name of the public key to be added.
69 * @param keyType Type of the public key to be added.
70 * @param publicKeyDer A blob of the public key DER to be added.
71 */
72 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080073 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070074
75 /**
76 * Get the public key DER blob from the identity storage.
77 * @param keyName The name of the requested public key.
78 * @return The DER Blob. If not found, return a Blob with a null pointer.
79 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080080 virtual ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080081 getPublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070082
83 /**
84 * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing.
85 * @param keyName name of the key
86 */
Yingdi Yu87581582014-01-14 14:28:39 -080087 virtual inline void
88 activatePublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070089
90 /**
91 * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing.
92 * @param keyName name of the key
93 */
Yingdi Yu87581582014-01-14 14:28:39 -080094 virtual inline void
95 deactivatePublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070096
97 /**
98 * Check if the specified certificate already exists.
99 * @param certificateName The name of the certificate.
100 * @return true if the certificate exists, otherwise false.
101 */
102 virtual bool
103 doesCertificateExist(const Name& certificateName);
104
105 /**
106 * Add a certificate in to the identity storage without checking if the identity and key exists.
107 * @param certificate The certificate to be added.
108 */
Yingdi Yu87581582014-01-14 14:28:39 -0800109 virtual void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700110 addAnyCertificate (const IdentityCertificate& certificate);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700111
112 /**
113 * Add a certificate to the identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700114 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700115 */
116 virtual void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700117 addCertificate(const IdentityCertificate& certificate);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700118
119 /**
120 * Get a certificate from the identity storage.
121 * @param certificateName The name of the requested certificate.
122 * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded.
123 * @return The requested certificate. If not found, return a shared_ptr with a null pointer.
124 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800125 virtual ptr_lib::shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -0800126 getCertificate(const Name &certificateName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700127
128
129 /*****************************************
Yingdi Yu87581582014-01-14 14:28:39 -0800130 * Default Getter *
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700131 *****************************************/
132
133 /**
134 * Get the default identity.
135 * @param return The name of default identity, or an empty name if there is no default.
136 */
137 virtual Name
138 getDefaultIdentity();
139
140 /**
141 * Get the default key name for the specified identity.
142 * @param identityName The identity name.
143 * @return The default key name.
144 */
145 virtual Name
146 getDefaultKeyNameForIdentity(const Name& identityName);
147
148 /**
149 * Get the default certificate name for the specified key.
150 * @param keyName The key name.
151 * @return The default certificate name.
152 */
153 virtual Name
154 getDefaultCertificateNameForKey(const Name& keyName);
155
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800156 virtual void
157 getAllIdentities(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800158
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800159 virtual void
160 getAllKeyNames(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800161
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800162 virtual void
163 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800164
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800165 virtual void
166 getAllCertificateNames(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800167
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800168 virtual void
169 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800170
Yingdi Yu87581582014-01-14 14:28:39 -0800171protected:
172 /**
173 * Set the default identity. If the identityName does not exist, then clear the default identity
174 * so that getDefaultIdentity() returns an empty name.
175 * @param identityName The default identity name.
176 */
177 virtual void
178 setDefaultIdentityInternal(const Name& identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700179
Yingdi Yu87581582014-01-14 14:28:39 -0800180 /**
181 * Set the default key name for the specified identity.
182 * @param keyName The key name.
183 * @param identityNameCheck (optional) The identity name to check the keyName.
184 */
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700185 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -0800186 setDefaultKeyNameForIdentityInternal(const Name& keyName);
187
188 /**
189 * Set the default key name for the specified identity.
190 * @param keyName The key name.
191 * @param certificateName The certificate name.
192 */
193 virtual void
194 setDefaultCertificateNameForKeyInternal(const Name& certificateName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800195
196 /**
197 * Delete a certificate.
198 * @param certificateName The certificate name.
199 */
200 virtual void
201 deleteCertificateInfo(const Name &certificateName);
202
203 /**
204 * Delete a public key and related certificates.
205 * @param keyName The key name.
206 */
207 virtual void
208 deletePublicKeyInfo(const Name &keyName);
209
210 /**
211 * Delete an identity and related public keys and certificates.
212 * @param identity The identity name.
213 */
214 virtual void
215 deleteIdentityInfo(const Name &identity);
Yingdi Yu87581582014-01-14 14:28:39 -0800216
217private:
218 void
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700219 updateKeyStatus(const Name& keyName, bool isActive);
220
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800221 sqlite3 * m_database;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700222};
223
Yingdi Yu87581582014-01-14 14:28:39 -0800224void
225SecPublicInfoSqlite3::activatePublicKey(const Name& keyName)
226{
227 updateKeyStatus(keyName, true);
228}
229
230void
231SecPublicInfoSqlite3::deactivatePublicKey(const Name& keyName)
232{
233 updateKeyStatus(keyName, false);
234}
235
Yingdi Yufc40d872014-02-18 12:56:04 -0800236} // namespace ndn
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700237
Yingdi Yufc40d872014-02-18 12:56:04 -0800238#endif //NDN_SECURITY_SEC_PUBLIC_INFO_SQLITE3_HPP