blob: 4954f01975c495acc3148c85702c8a5860c2ec6b [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
9#ifndef NDN_BASIC_IDENTITY_STORAGE_H
10#define NDN_BASIC_IDENTITY_STORAGE_H
11
Jeff Thompsonb7523002013-10-09 10:25:00 -070012// Only compile if ndn-cpp-config.h defines NDN_CPP_HAVE_SQLITE3.
Jeff Thompson6e229042013-10-10 11:09:49 -070013#include <ndn-cpp/ndn-cpp-config.h>
Jeff Thompson1975def2013-10-09 17:06:43 -070014#ifdef NDN_CPP_HAVE_SQLITE3
Jeff Thompson7ca11f22013-10-04 19:01:30 -070015
16#include <sqlite3.h>
17#include "../../common.hpp"
18#include "identity-storage.hpp"
19
20namespace ndn
21{
22
23/**
24 * BasicIdentityStorage extends IdentityStorage to implement a basic storage of identity, public keys and certificates
25 * using SQLite.
26 */
27class BasicIdentityStorage : public IdentityStorage {
28public:
29 BasicIdentityStorage();
30
31 /**
32 * The virtual Destructor.
33 */
34 virtual
35 ~BasicIdentityStorage();
36
37 /**
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
65 doesKeyExist(const Name& keyName);
66
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
74 addKey(const Name& keyName, KeyType keyType, const Blob& publicKeyDer);
75
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 */
81 virtual Blob
82 getKey(const Name& keyName);
83
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 */
88 virtual void
89 activateKey(const Name& keyName);
90
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 */
95 virtual void
96 deactivateKey(const Name& keyName);
97
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 */
110 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 */
Jeff Thompson3bd90bc2013-10-19 16:40:14 -0700126 virtual ptr_lib::shared_ptr<Data>
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700127 getCertificate(const Name &certificateName, bool allowAny = false);
128
129
130 /*****************************************
131 * Get/Set Default *
132 *****************************************/
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
157 /**
158 * Set the default identity. If the identityName does not exist, then clear the default identity
159 * so that getDefaultIdentity() returns an empty name.
160 * @param identityName The default identity name.
161 */
162 virtual void
163 setDefaultIdentity(const Name& identityName);
164
165 /**
166 * Set the default key name for the specified identity.
167 * @param keyName The key name.
168 * @param identityNameCheck (optional) The identity name to check the keyName.
169 */
170 virtual void
171 setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck = Name());
172
173 /**
174 * Set the default key name for the specified identity.
175 * @param keyName The key name.
176 * @param certificateName The certificate name.
177 */
178 virtual void
179 setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName);
180
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800181
182 virtual std::vector<Name>
183 getAllIdentities(bool isDefault);
184
185 virtual std::vector<Name>
186 getAllKeyNames(bool isDefault);
187
188 virtual std::vector<Name>
189 getAllKeyNamesOfIdentity(const Name& identity, bool isDefault);
190
191 virtual std::vector<Name>
192 getAllCertificateNames(bool isDefault);
193
194 virtual std::vector<Name>
195 getAllCertificateNamesOfKey(const Name& keyName, bool isDefault);
196
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700197private:
198
199 virtual void
200 updateKeyStatus(const Name& keyName, bool isActive);
201
202 sqlite3 *database_;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700203};
204
205}
206
Jeff Thompsonb7523002013-10-09 10:25:00 -0700207#endif // NDN_CPP_HAVE_SQLITE3
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700208
209#endif