blob: 30f28f8d686e3c7f183e2fdb869a62b0a28c7bbb [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
Jeff Thompsonb7523002013-10-09 10:25:00 -070012// Only compile if ndn-cpp-config.h defines NDN_CPP_HAVE_SQLITE3.
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080013#include "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
Yingdi Yu4f324632014-01-15 18:10:03 -080016#include "../common.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080017#include "sec-public-info.hpp"
Jeff Thompson7ca11f22013-10-04 19:01:30 -070018
Yingdi Yu874678f2014-01-22 19:30:34 -080019struct sqlite3;
20
Jeff Thompson7ca11f22013-10-04 19:01:30 -070021namespace ndn
22{
23
24/**
25 * BasicIdentityStorage extends IdentityStorage to implement a basic storage of identity, public keys and certificates
26 * using SQLite.
27 */
Yingdi Yu87581582014-01-14 14:28:39 -080028class SecPublicInfoSqlite3 : public SecPublicInfo {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070029public:
Yingdi Yu87581582014-01-14 14:28:39 -080030 struct Error : public SecPublicInfo::Error { Error(const std::string &what) : SecPublicInfo::Error(what) {} };
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080031
Yingdi Yu87581582014-01-14 14:28:39 -080032 SecPublicInfoSqlite3();
Jeff Thompson7ca11f22013-10-04 19:01:30 -070033
34 /**
35 * The virtual Destructor.
36 */
37 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080038 ~SecPublicInfoSqlite3();
Jeff Thompson7ca11f22013-10-04 19:01:30 -070039
Yingdi Yu87581582014-01-14 14:28:39 -080040 // from SecPublicInfo
Jeff Thompson7ca11f22013-10-04 19:01:30 -070041 /**
42 * Check if the specified identity already exists.
43 * @param identityName The identity name.
44 * @return true if the identity exists, otherwise false.
45 */
46 virtual bool
47 doesIdentityExist(const Name& identityName);
48
49 /**
50 * Add a new identity. An exception will be thrown if the identity already exists.
51 * @param identityName The identity name to be added.
52 */
53 virtual void
54 addIdentity(const Name& identityName);
55
56 /**
57 * Revoke the identity.
58 * @return true if the identity was revoked, false if not.
59 */
60 virtual bool
61 revokeIdentity();
62
63 /**
Jeff Thompson7ca11f22013-10-04 19:01:30 -070064 * Check if the specified key already exists.
65 * @param keyName The name of the key.
66 * @return true if the key exists, otherwise false.
67 */
68 virtual bool
Yingdi Yu87581582014-01-14 14:28:39 -080069 doesPublicKeyExist(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070070
71 /**
Jeff Thompson7ca11f22013-10-04 19:01:30 -070072 * Add a public key to the identity storage.
73 * @param keyName The name of the public key to be added.
74 * @param keyType Type of the public key to be added.
75 * @param publicKeyDer A blob of the public key DER to be added.
76 */
77 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080078 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070079
80 /**
81 * Get the public key DER blob from the identity storage.
82 * @param keyName The name of the requested public key.
83 * @return The DER Blob. If not found, return a Blob with a null pointer.
84 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080085 virtual ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080086 getPublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070087
88 /**
89 * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing.
90 * @param keyName name of the key
91 */
Yingdi Yu87581582014-01-14 14:28:39 -080092 virtual inline void
93 activatePublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070094
95 /**
96 * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing.
97 * @param keyName name of the key
98 */
Yingdi Yu87581582014-01-14 14:28:39 -080099 virtual inline void
100 deactivatePublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700101
102 /**
103 * Check if the specified certificate already exists.
104 * @param certificateName The name of the certificate.
105 * @return true if the certificate exists, otherwise false.
106 */
107 virtual bool
108 doesCertificateExist(const Name& certificateName);
109
110 /**
111 * Add a certificate in to the identity storage without checking if the identity and key exists.
112 * @param certificate The certificate to be added.
113 */
Yingdi Yu87581582014-01-14 14:28:39 -0800114 virtual void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700115 addAnyCertificate (const IdentityCertificate& certificate);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700116
117 /**
118 * Add a certificate to the identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700119 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700120 */
121 virtual void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700122 addCertificate(const IdentityCertificate& certificate);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700123
124 /**
125 * Get a certificate from the identity storage.
126 * @param certificateName The name of the requested certificate.
127 * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded.
128 * @return The requested certificate. If not found, return a shared_ptr with a null pointer.
129 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800130 virtual ptr_lib::shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -0800131 getCertificate(const Name &certificateName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700132
133
134 /*****************************************
Yingdi Yu87581582014-01-14 14:28:39 -0800135 * Default Getter *
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700136 *****************************************/
137
138 /**
139 * Get the default identity.
140 * @param return The name of default identity, or an empty name if there is no default.
141 */
142 virtual Name
143 getDefaultIdentity();
144
145 /**
146 * Get the default key name for the specified identity.
147 * @param identityName The identity name.
148 * @return The default key name.
149 */
150 virtual Name
151 getDefaultKeyNameForIdentity(const Name& identityName);
152
153 /**
154 * Get the default certificate name for the specified key.
155 * @param keyName The key name.
156 * @return The default certificate name.
157 */
158 virtual Name
159 getDefaultCertificateNameForKey(const Name& keyName);
160
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800161 virtual std::vector<Name>
162 getAllIdentities(bool isDefault);
163
164 virtual std::vector<Name>
165 getAllKeyNames(bool isDefault);
166
167 virtual std::vector<Name>
168 getAllKeyNamesOfIdentity(const Name& identity, bool isDefault);
169
170 virtual std::vector<Name>
171 getAllCertificateNames(bool isDefault);
172
173 virtual std::vector<Name>
174 getAllCertificateNamesOfKey(const Name& keyName, bool isDefault);
175
Yingdi Yu87581582014-01-14 14:28:39 -0800176protected:
177 /**
178 * Set the default identity. If the identityName does not exist, then clear the default identity
179 * so that getDefaultIdentity() returns an empty name.
180 * @param identityName The default identity name.
181 */
182 virtual void
183 setDefaultIdentityInternal(const Name& identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700184
Yingdi Yu87581582014-01-14 14:28:39 -0800185 /**
186 * Set the default key name for the specified identity.
187 * @param keyName The key name.
188 * @param identityNameCheck (optional) The identity name to check the keyName.
189 */
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700190 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -0800191 setDefaultKeyNameForIdentityInternal(const Name& keyName);
192
193 /**
194 * Set the default key name for the specified identity.
195 * @param keyName The key name.
196 * @param certificateName The certificate name.
197 */
198 virtual void
199 setDefaultCertificateNameForKeyInternal(const Name& certificateName);
200
201private:
202 void
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700203 updateKeyStatus(const Name& keyName, bool isActive);
204
205 sqlite3 *database_;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700206};
207
Yingdi Yu87581582014-01-14 14:28:39 -0800208void
209SecPublicInfoSqlite3::activatePublicKey(const Name& keyName)
210{
211 updateKeyStatus(keyName, true);
212}
213
214void
215SecPublicInfoSqlite3::deactivatePublicKey(const Name& keyName)
216{
217 updateKeyStatus(keyName, false);
218}
219
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700220}
221
Jeff Thompsonb7523002013-10-09 10:25:00 -0700222#endif // NDN_CPP_HAVE_SQLITE3
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700223
224#endif