blob: 0dc3e5d2b5668ce980c3452efc9a55ef44aa863e [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:
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080029 struct Error : public IdentityStorage::Error { Error(const std::string &what) : IdentityStorage::Error(what) {} };
30
Jeff Thompson7ca11f22013-10-04 19:01:30 -070031 BasicIdentityStorage();
32
33 /**
34 * The virtual Destructor.
35 */
36 virtual
37 ~BasicIdentityStorage();
38
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080039 // from IdentityStorage
Jeff Thompson7ca11f22013-10-04 19:01:30 -070040 /**
41 * Check if the specified identity already exists.
42 * @param identityName The identity name.
43 * @return true if the identity exists, otherwise false.
44 */
45 virtual bool
46 doesIdentityExist(const Name& identityName);
47
48 /**
49 * Add a new identity. An exception will be thrown if the identity already exists.
50 * @param identityName The identity name to be added.
51 */
52 virtual void
53 addIdentity(const Name& identityName);
54
55 /**
56 * Revoke the identity.
57 * @return true if the identity was revoked, false if not.
58 */
59 virtual bool
60 revokeIdentity();
61
62 /**
Jeff Thompson7ca11f22013-10-04 19:01:30 -070063 * Check if the specified key already exists.
64 * @param keyName The name of the key.
65 * @return true if the key exists, otherwise false.
66 */
67 virtual bool
68 doesKeyExist(const Name& keyName);
69
70 /**
Jeff Thompson7ca11f22013-10-04 19:01:30 -070071 * Add a public key to the identity storage.
72 * @param keyName The name of the public key to be added.
73 * @param keyType Type of the public key to be added.
74 * @param publicKeyDer A blob of the public key DER to be added.
75 */
76 virtual void
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080077 addKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070078
79 /**
80 * Get the public key DER blob from the identity storage.
81 * @param keyName The name of the requested public key.
82 * @return The DER Blob. If not found, return a Blob with a null pointer.
83 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080084 virtual ptr_lib::shared_ptr<PublicKey>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070085 getKey(const Name& keyName);
86
87 /**
88 * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing.
89 * @param keyName name of the key
90 */
91 virtual void
92 activateKey(const Name& keyName);
93
94 /**
95 * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing.
96 * @param keyName name of the key
97 */
98 virtual void
99 deactivateKey(const Name& keyName);
100
101 /**
102 * Check if the specified certificate already exists.
103 * @param certificateName The name of the certificate.
104 * @return true if the certificate exists, otherwise false.
105 */
106 virtual bool
107 doesCertificateExist(const Name& certificateName);
108
109 /**
110 * Add a certificate in to the identity storage without checking if the identity and key exists.
111 * @param certificate The certificate to be added.
112 */
113 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700114 addAnyCertificate (const IdentityCertificate& certificate);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700115
116 /**
117 * Add a certificate to the identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700118 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700119 */
120 virtual void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700121 addCertificate(const IdentityCertificate& certificate);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700122
123 /**
124 * Get a certificate from the identity storage.
125 * @param certificateName The name of the requested certificate.
126 * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded.
127 * @return The requested certificate. If not found, return a shared_ptr with a null pointer.
128 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800129 virtual ptr_lib::shared_ptr<IdentityCertificate>
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700130 getCertificate(const Name &certificateName, bool allowAny = false);
131
132
133 /*****************************************
134 * Get/Set Default *
135 *****************************************/
136
137 /**
138 * Get the default identity.
139 * @param return The name of default identity, or an empty name if there is no default.
140 */
141 virtual Name
142 getDefaultIdentity();
143
144 /**
145 * Get the default key name for the specified identity.
146 * @param identityName The identity name.
147 * @return The default key name.
148 */
149 virtual Name
150 getDefaultKeyNameForIdentity(const Name& identityName);
151
152 /**
153 * Get the default certificate name for the specified key.
154 * @param keyName The key name.
155 * @return The default certificate name.
156 */
157 virtual Name
158 getDefaultCertificateNameForKey(const Name& keyName);
159
160 /**
161 * Set the default identity. If the identityName does not exist, then clear the default identity
162 * so that getDefaultIdentity() returns an empty name.
163 * @param identityName The default identity name.
164 */
165 virtual void
166 setDefaultIdentity(const Name& identityName);
167
168 /**
169 * Set the default key name for the specified identity.
170 * @param keyName The key name.
171 * @param identityNameCheck (optional) The identity name to check the keyName.
172 */
173 virtual void
174 setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck = Name());
175
176 /**
177 * Set the default key name for the specified identity.
178 * @param keyName The key name.
179 * @param certificateName The certificate name.
180 */
181 virtual void
182 setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName);
183
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800184
185 virtual std::vector<Name>
186 getAllIdentities(bool isDefault);
187
188 virtual std::vector<Name>
189 getAllKeyNames(bool isDefault);
190
191 virtual std::vector<Name>
192 getAllKeyNamesOfIdentity(const Name& identity, bool isDefault);
193
194 virtual std::vector<Name>
195 getAllCertificateNames(bool isDefault);
196
197 virtual std::vector<Name>
198 getAllCertificateNamesOfKey(const Name& keyName, bool isDefault);
199
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700200private:
201
202 virtual void
203 updateKeyStatus(const Name& keyName, bool isActive);
204
205 sqlite3 *database_;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700206};
207
208}
209
Jeff Thompsonb7523002013-10-09 10:25:00 -0700210#endif // NDN_CPP_HAVE_SQLITE3
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700211
212#endif