blob: ee1a886306407854853552d48a0a9eb6b6977615 [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>
5 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_BASIC_IDENTITY_STORAGE_H
9#define NDN_BASIC_IDENTITY_STORAGE_H
10
Jeff Thompsonb7523002013-10-09 10:25:00 -070011// Only compile if ndn-cpp-config.h defines NDN_CPP_HAVE_SQLITE3.
Jeff Thompson6e229042013-10-10 11:09:49 -070012#include <ndn-cpp/ndn-cpp-config.h>
Jeff Thompson1975def2013-10-09 17:06:43 -070013#ifdef NDN_CPP_HAVE_SQLITE3
Jeff Thompson7ca11f22013-10-04 19:01:30 -070014
15#include <sqlite3.h>
16#include "../../common.hpp"
17#include "identity-storage.hpp"
18
19namespace ndn
20{
21
22/**
23 * BasicIdentityStorage extends IdentityStorage to implement a basic storage of identity, public keys and certificates
24 * using SQLite.
25 */
26class BasicIdentityStorage : public IdentityStorage {
27public:
28 BasicIdentityStorage();
29
30 /**
31 * The virtual Destructor.
32 */
33 virtual
34 ~BasicIdentityStorage();
35
36 /**
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 /**
59 * Generate a name for a new key belonging to the identity.
60 * @param identityName The identity name.
61 * @param useKsk If true, generate a KSK name, otherwise a DSK name.
62 * @return The generated key name.
63 */
64 virtual Name
65 getNewKeyName(const Name& identityName, bool useKsk);
66
67 /**
68 * Check if the specified key already exists.
69 * @param keyName The name of the key.
70 * @return true if the key exists, otherwise false.
71 */
72 virtual bool
73 doesKeyExist(const Name& keyName);
74
75 /**
76 * Extract the key name from the certificate name.
77 * @param certificateName The certificate name to be processed.
78 */
79 virtual Name
80 getKeyNameForCertificate(const Name& certificateName);
81
82 /**
83 * Add a public key to the identity storage.
84 * @param keyName The name of the public key to be added.
85 * @param keyType Type of the public key to be added.
86 * @param publicKeyDer A blob of the public key DER to be added.
87 */
88 virtual void
89 addKey(const Name& keyName, KeyType keyType, const Blob& publicKeyDer);
90
91 /**
92 * Get the public key DER blob from the identity storage.
93 * @param keyName The name of the requested public key.
94 * @return The DER Blob. If not found, return a Blob with a null pointer.
95 */
96 virtual Blob
97 getKey(const Name& keyName);
98
99 /**
100 * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing.
101 * @param keyName name of the key
102 */
103 virtual void
104 activateKey(const Name& keyName);
105
106 /**
107 * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing.
108 * @param keyName name of the key
109 */
110 virtual void
111 deactivateKey(const Name& keyName);
112
113 /**
114 * Check if the specified certificate already exists.
115 * @param certificateName The name of the certificate.
116 * @return true if the certificate exists, otherwise false.
117 */
118 virtual bool
119 doesCertificateExist(const Name& certificateName);
120
121 /**
122 * Add a certificate in to the identity storage without checking if the identity and key exists.
123 * @param certificate The certificate to be added.
124 */
125 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700126 addAnyCertificate (const IdentityCertificate& certificate);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700127
128 /**
129 * Add a certificate to the identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700130 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700131 */
132 virtual void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700133 addCertificate(const IdentityCertificate& certificate);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700134
135 /**
136 * Get a certificate from the identity storage.
137 * @param certificateName The name of the requested certificate.
138 * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded.
139 * @return The requested certificate. If not found, return a shared_ptr with a null pointer.
140 */
141 virtual ptr_lib::shared_ptr<Certificate>
142 getCertificate(const Name &certificateName, bool allowAny = false);
143
144
145 /*****************************************
146 * Get/Set Default *
147 *****************************************/
148
149 /**
150 * Get the default identity.
151 * @param return The name of default identity, or an empty name if there is no default.
152 */
153 virtual Name
154 getDefaultIdentity();
155
156 /**
157 * Get the default key name for the specified identity.
158 * @param identityName The identity name.
159 * @return The default key name.
160 */
161 virtual Name
162 getDefaultKeyNameForIdentity(const Name& identityName);
163
164 /**
165 * Get the default certificate name for the specified key.
166 * @param keyName The key name.
167 * @return The default certificate name.
168 */
169 virtual Name
170 getDefaultCertificateNameForKey(const Name& keyName);
171
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 setDefaultIdentity(const Name& identityName);
179
180 /**
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 */
185 virtual void
186 setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck = Name());
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 setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName);
195
196private:
197
198 virtual void
199 updateKeyStatus(const Name& keyName, bool isActive);
200
201 sqlite3 *database_;
202#if 0
203 Time lastUpdated_;
204#endif
205};
206
207}
208
Jeff Thompsonb7523002013-10-09 10:25:00 -0700209#endif // NDN_CPP_HAVE_SQLITE3
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700210
211#endif