blob: b3e4f962b9d64291e7650c03b1132f0c27214036 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_SECURITY_KEY_HPP
23#define NDN_SECURITY_KEY_HPP
24
25#include "identity-certificate.hpp"
26#include "certificate-container.hpp"
27
28namespace ndn {
29namespace security {
30
31class PibImpl;
32class Identity;
33class KeyContainer;
34
35/**
36 * @brief represents a key
37 *
38 * Key is at the second level in PIB's Identity-Key-Certificate hierarchy.
39 * An Key has a Name (identity + keyId), and contains one or more certificates,
40 * one of which is set as the default certificate of this key. A certificate
41 * can be directly accessed from a Key object.
42 *
43 * @throw PibImpl::Error when underlying implementation has non-semantic error.
44 */
45class Key
46{
47public:
48 friend class Identity;
49 friend class KeyContainer;
50
51public:
52 /**
53 * @brief Default Constructor
54 *
55 * Key created using this default constructor is just a place holder.
56 * It must obtain an actual instance from Identity::getKey(...). A typical
57 * usage would be for exception handling:
58 *
59 * Key key;
60 * try {
61 * key = Identity.getKey(...);
62 * }
63 * catch (Pib::Error&) {
64 * ...
65 * }
66 *
67 * A Key instance created using the constructor is invalid. Calling a
68 * member method on an invalid Key instance may cause an std::domain_error.
69 */
70 Key();
71
72 /// @brief Get the name of the key.
73 const Name&
74 getName() const;
75
76 /// @brief Get the name of the belonging identity.
77 const Name&
78 getIdentity() const;
79
80 /// @brief Get the key id of the key.
81 const name::Component&
82 getKeyId() const;
83
84 /// @brief Get public key
85 const PublicKey&
86 getPublicKey() const;
87
88 /**
89 * @brief Add a certificate.
90 *
91 * @param certificate The certificate to add.
92 */
93 void
94 addCertificate(const IdentityCertificate& certificate);
95
96 /**
97 * @brief Remove a certificate.
98 *
99 * @param certName The name of the certificate to delete.
100 */
101 void
102 removeCertificate(const Name& certName);
103
104 /**
105 * @brief Get a certificate.
106 *
107 * @return the certificate
108 * @throws Pib::Error if the certificate does not exist.
109 */
110 IdentityCertificate
111 getCertificate(const Name& certName);
112
113 /// @brief Get all the certificates for this key.
114 CertificateContainer
115 getCertificates();
116
117 /**
118 * @brief Set the default certificate.
119 *
120 * @param certName The name of the default certificate of the key.
121 * @return the default certificate
122 * @throws Pib::Error if the certificate does not exist.
123 */
124 const IdentityCertificate&
125 setDefaultCertificate(const Name& certName);
126
127 /**
128 * @brief Set the default certificate.
129 *
130 * If the certificate does not exist, add it and set it as the default certificate of the key.
131 * If the certificate exists, simply set it as the default certificate of the key.
132 *
133 * @param certificate The certificate to add.
134 * @return the default certificate
135 */
136 const IdentityCertificate&
137 setDefaultCertificate(const IdentityCertificate& certificate);
138
139 /**
140 * @brief Get the default certificate for this Key.
141 *
142 * @throws Pib::Error if the default certificate does not exist.
143 */
144 const IdentityCertificate&
145 getDefaultCertificate();
146
147 /// @brief Check if the Key instance is valid
148 operator bool() const;
149
150 /// @brief Check if the Key instance is invalid
151 bool
152 operator!() const;
153
154NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
155 /**
156 * @brief Create a Key with @p identityName and @p keyId.
157 *
158 * If the key/identity does not exist in the backend, create it in backend.
159 *
160 * @param identityName The name of the Identity.
161 * @param keyId The key id of the key.
162 * @param publicKey The public key to add.
163 * @param impl The actual backend implementation.
164 */
165 Key(const Name& identityName, const name::Component& keyId,
166 const PublicKey& publicKey, shared_ptr<PibImpl> impl);
167
168 /**
169 * @brief Create an KeyEntry with @p identityName and @p keyId.
170 *
171 * @param identityName The name of the Identity.
172 * @param keyId The key id of the key.
173 * @param impl The actual backend implementation.
174 * @throws Pib::Error if the key does not exist.
175 */
176 Key(const Name& identityName, const name::Component& keyId, shared_ptr<PibImpl> impl);
177
178 /**
179 * @brief Check the validity of this instance
180 *
181 * @throws std::domain_error if the instance is invalid
182 */
183 void
184 validityCheck() const;
185
186private:
187 Name m_id;
188 name::Component m_keyId;
189 Name m_keyName;
190 PublicKey m_key;
191
192 bool m_hasDefaultCertificate;
193 IdentityCertificate m_defaultCertificate;
194
195 bool m_needRefreshCerts;
196 CertificateContainer m_certificates;
197
198 shared_ptr<PibImpl> m_impl;
199};
200
201} // namespace security
202} // namespace ndn
203
204#endif // NDN_SECURITY_PIB_HPP