blob: 94dc0faabec593250e313ee86f3566b9c7f92834 [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 Yu87581582014-01-14 14:28:39 -08009#ifndef NDN_SEC_PUBLIC_INFO_SQLITE3_HPP
10#define NDN_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
Jeff Thompson7ca11f22013-10-04 19:01:30 -070017namespace ndn
18{
19
20/**
21 * BasicIdentityStorage extends IdentityStorage to implement a basic storage of identity, public keys and certificates
22 * using SQLite.
23 */
Yingdi Yu87581582014-01-14 14:28:39 -080024class SecPublicInfoSqlite3 : public SecPublicInfo {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070025public:
Yingdi Yu87581582014-01-14 14:28:39 -080026 struct Error : public SecPublicInfo::Error { Error(const std::string &what) : SecPublicInfo::Error(what) {} };
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080027
Yingdi Yu87581582014-01-14 14:28:39 -080028 SecPublicInfoSqlite3();
Jeff Thompson7ca11f22013-10-04 19:01:30 -070029
30 /**
31 * The virtual Destructor.
32 */
33 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080034 ~SecPublicInfoSqlite3();
Jeff Thompson7ca11f22013-10-04 19:01:30 -070035
Yingdi Yu87581582014-01-14 14:28:39 -080036 // from SecPublicInfo
Jeff Thompson7ca11f22013-10-04 19:01:30 -070037 /**
38 * Check if the specified identity already exists.
39 * @param identityName The identity name.
40 * @return true if the identity exists, otherwise false.
41 */
42 virtual bool
43 doesIdentityExist(const Name& identityName);
44
45 /**
46 * Add a new identity. An exception will be thrown if the identity already exists.
47 * @param identityName The identity name to be added.
48 */
49 virtual void
50 addIdentity(const Name& identityName);
51
52 /**
53 * Revoke the identity.
54 * @return true if the identity was revoked, false if not.
55 */
56 virtual bool
57 revokeIdentity();
58
59 /**
Jeff Thompson7ca11f22013-10-04 19:01:30 -070060 * Check if the specified key already exists.
61 * @param keyName The name of the key.
62 * @return true if the key exists, otherwise false.
63 */
64 virtual bool
Yingdi Yu87581582014-01-14 14:28:39 -080065 doesPublicKeyExist(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070066
67 /**
Jeff Thompson7ca11f22013-10-04 19:01:30 -070068 * Add a public key to the identity storage.
69 * @param keyName The name of the public key to be added.
70 * @param keyType Type of the public key to be added.
71 * @param publicKeyDer A blob of the public key DER to be added.
72 */
73 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080074 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070075
76 /**
77 * Get the public key DER blob from the identity storage.
78 * @param keyName The name of the requested public key.
79 * @return The DER Blob. If not found, return a Blob with a null pointer.
80 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080081 virtual ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080082 getPublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070083
84 /**
85 * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing.
86 * @param keyName name of the key
87 */
Yingdi Yu87581582014-01-14 14:28:39 -080088 virtual inline void
89 activatePublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070090
91 /**
92 * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing.
93 * @param keyName name of the key
94 */
Yingdi Yu87581582014-01-14 14:28:39 -080095 virtual inline void
96 deactivatePublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070097
98 /**
99 * Check if the specified certificate already exists.
100 * @param certificateName The name of the certificate.
101 * @return true if the certificate exists, otherwise false.
102 */
103 virtual bool
104 doesCertificateExist(const Name& certificateName);
105
106 /**
107 * Add a certificate in to the identity storage without checking if the identity and key exists.
108 * @param certificate The certificate to be added.
109 */
Yingdi Yu87581582014-01-14 14:28:39 -0800110 virtual void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700111 addAnyCertificate (const IdentityCertificate& certificate);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700112
113 /**
114 * Add a certificate to the identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700115 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700116 */
117 virtual void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700118 addCertificate(const IdentityCertificate& certificate);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700119
120 /**
121 * Get a certificate from the identity storage.
122 * @param certificateName The name of the requested certificate.
123 * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded.
124 * @return The requested certificate. If not found, return a shared_ptr with a null pointer.
125 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800126 virtual ptr_lib::shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -0800127 getCertificate(const Name &certificateName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700128
129
130 /*****************************************
Yingdi Yu87581582014-01-14 14:28:39 -0800131 * Default Getter *
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700132 *****************************************/
133
134 /**
135 * Get the default identity.
136 * @param return The name of default identity, or an empty name if there is no default.
137 */
138 virtual Name
139 getDefaultIdentity();
140
141 /**
142 * Get the default key name for the specified identity.
143 * @param identityName The identity name.
144 * @return The default key name.
145 */
146 virtual Name
147 getDefaultKeyNameForIdentity(const Name& identityName);
148
149 /**
150 * Get the default certificate name for the specified key.
151 * @param keyName The key name.
152 * @return The default certificate name.
153 */
154 virtual Name
155 getDefaultCertificateNameForKey(const Name& keyName);
156
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800157 virtual void
158 getAllIdentities(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800159
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800160 virtual void
161 getAllKeyNames(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800162
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800163 virtual void
164 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800165
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800166 virtual void
167 getAllCertificateNames(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800168
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800169 virtual void
170 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800171
Yingdi Yu87581582014-01-14 14:28:39 -0800172protected:
173 /**
174 * Set the default identity. If the identityName does not exist, then clear the default identity
175 * so that getDefaultIdentity() returns an empty name.
176 * @param identityName The default identity name.
177 */
178 virtual void
179 setDefaultIdentityInternal(const Name& identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700180
Yingdi Yu87581582014-01-14 14:28:39 -0800181 /**
182 * Set the default key name for the specified identity.
183 * @param keyName The key name.
184 * @param identityNameCheck (optional) The identity name to check the keyName.
185 */
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700186 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -0800187 setDefaultKeyNameForIdentityInternal(const Name& keyName);
188
189 /**
190 * Set the default key name for the specified identity.
191 * @param keyName The key name.
192 * @param certificateName The certificate name.
193 */
194 virtual void
195 setDefaultCertificateNameForKeyInternal(const Name& certificateName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800196
197 /**
198 * Delete a certificate.
199 * @param certificateName The certificate name.
200 */
201 virtual void
202 deleteCertificateInfo(const Name &certificateName);
203
204 /**
205 * Delete a public key and related certificates.
206 * @param keyName The key name.
207 */
208 virtual void
209 deletePublicKeyInfo(const Name &keyName);
210
211 /**
212 * Delete an identity and related public keys and certificates.
213 * @param identity The identity name.
214 */
215 virtual void
216 deleteIdentityInfo(const Name &identity);
Yingdi Yu87581582014-01-14 14:28:39 -0800217
218private:
219 void
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700220 updateKeyStatus(const Name& keyName, bool isActive);
221
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800222 sqlite3 * m_database;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700223};
224
Yingdi Yu87581582014-01-14 14:28:39 -0800225void
226SecPublicInfoSqlite3::activatePublicKey(const Name& keyName)
227{
228 updateKeyStatus(keyName, true);
229}
230
231void
232SecPublicInfoSqlite3::deactivatePublicKey(const Name& keyName)
233{
234 updateKeyStatus(keyName, false);
235}
236
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700237}
238
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700239#endif