blob: 99dac54d89e9065642bcc8750a8f7f4aebf3164a [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.
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"
Yingdi Yu87581582014-01-14 14:28:39 -080018#include "sec-public-info.hpp"
Jeff Thompson7ca11f22013-10-04 19:01:30 -070019
20namespace ndn
21{
22
23/**
24 * BasicIdentityStorage extends IdentityStorage to implement a basic storage of identity, public keys and certificates
25 * using SQLite.
26 */
Yingdi Yu87581582014-01-14 14:28:39 -080027class SecPublicInfoSqlite3 : public SecPublicInfo {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070028public:
Yingdi Yu87581582014-01-14 14:28:39 -080029 struct Error : public SecPublicInfo::Error { Error(const std::string &what) : SecPublicInfo::Error(what) {} };
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080030
Yingdi Yu87581582014-01-14 14:28:39 -080031 SecPublicInfoSqlite3();
Jeff Thompson7ca11f22013-10-04 19:01:30 -070032
33 /**
34 * The virtual Destructor.
35 */
36 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080037 ~SecPublicInfoSqlite3();
Jeff Thompson7ca11f22013-10-04 19:01:30 -070038
Yingdi Yu87581582014-01-14 14:28:39 -080039 // from SecPublicInfo
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
Yingdi Yu87581582014-01-14 14:28:39 -080068 doesPublicKeyExist(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070069
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
Yingdi Yu87581582014-01-14 14:28:39 -080077 addPublicKey(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>
Yingdi Yu87581582014-01-14 14:28:39 -080085 getPublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070086
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 */
Yingdi Yu87581582014-01-14 14:28:39 -080091 virtual inline void
92 activatePublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070093
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 */
Yingdi Yu87581582014-01-14 14:28:39 -080098 virtual inline void
99 deactivatePublicKey(const Name& keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700100
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 */
Yingdi Yu87581582014-01-14 14:28:39 -0800113 virtual 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>
Yingdi Yu88663af2014-01-15 15:21:38 -0800130 getCertificate(const Name &certificateName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700131
132
133 /*****************************************
Yingdi Yu87581582014-01-14 14:28:39 -0800134 * Default Getter *
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700135 *****************************************/
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
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800160 virtual std::vector<Name>
161 getAllIdentities(bool isDefault);
162
163 virtual std::vector<Name>
164 getAllKeyNames(bool isDefault);
165
166 virtual std::vector<Name>
167 getAllKeyNamesOfIdentity(const Name& identity, bool isDefault);
168
169 virtual std::vector<Name>
170 getAllCertificateNames(bool isDefault);
171
172 virtual std::vector<Name>
173 getAllCertificateNamesOfKey(const Name& keyName, bool isDefault);
174
Yingdi Yu87581582014-01-14 14:28:39 -0800175protected:
176 /**
177 * Set the default identity. If the identityName does not exist, then clear the default identity
178 * so that getDefaultIdentity() returns an empty name.
179 * @param identityName The default identity name.
180 */
181 virtual void
182 setDefaultIdentityInternal(const Name& identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700183
Yingdi Yu87581582014-01-14 14:28:39 -0800184 /**
185 * Set the default key name for the specified identity.
186 * @param keyName The key name.
187 * @param identityNameCheck (optional) The identity name to check the keyName.
188 */
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700189 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -0800190 setDefaultKeyNameForIdentityInternal(const Name& keyName);
191
192 /**
193 * Set the default key name for the specified identity.
194 * @param keyName The key name.
195 * @param certificateName The certificate name.
196 */
197 virtual void
198 setDefaultCertificateNameForKeyInternal(const Name& certificateName);
199
200private:
201 void
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700202 updateKeyStatus(const Name& keyName, bool isActive);
203
204 sqlite3 *database_;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700205};
206
Yingdi Yu87581582014-01-14 14:28:39 -0800207void
208SecPublicInfoSqlite3::activatePublicKey(const Name& keyName)
209{
210 updateKeyStatus(keyName, true);
211}
212
213void
214SecPublicInfoSqlite3::deactivatePublicKey(const Name& keyName)
215{
216 updateKeyStatus(keyName, false);
217}
218
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700219}
220
Jeff Thompsonb7523002013-10-09 10:25:00 -0700221#endif // NDN_CPP_HAVE_SQLITE3
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700222
223#endif