blob: d99e427ad662da9e2844fd077825f0ca78d786c1 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -07004 *
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
Alexander Afanasyev97709c02016-08-25 19:58:30 -070022#ifndef NDN_SECURITY_PIB_KEY_HPP
23#define NDN_SECURITY_PIB_KEY_HPP
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
Yingdi Yu6ee2d362015-07-16 21:48:05 -070025#include "../../data.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070026#include "certificate-container.hpp"
Yingdi Yu6ee2d362015-07-16 21:48:05 -070027#include "../security-common.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070028
29namespace ndn {
30namespace security {
31
Yingdi Yu6ee2d362015-07-16 21:48:05 -070032class KeyChain;
33
34namespace pib {
35
Yingdi Yub8f8b342015-04-27 11:06:42 -070036class PibImpl;
Yingdi Yub8f8b342015-04-27 11:06:42 -070037
38/**
39 * @brief represents a key
40 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -070041 * Key is at the second level in PIB's Identity-Key-Certificate hierarchy. A Key has a Name
42 * (identity + "KEY" + keyId), and contains one or more certificates, one of which is set as
43 * the default certificate of this key. A certificate can be directly accessed from a Key
44 * object.
Yingdi Yub8f8b342015-04-27 11:06:42 -070045 */
46class Key
47{
48public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070049 /**
50 * @brief Default Constructor
51 *
52 * Key created using this default constructor is just a place holder.
53 * It must obtain an actual instance from Identity::getKey(...). A typical
54 * usage would be for exception handling:
55 *
56 * Key key;
57 * try {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070058 * key = identity.getKey(...);
Yingdi Yub8f8b342015-04-27 11:06:42 -070059 * }
Yingdi Yu6ee2d362015-07-16 21:48:05 -070060 * catch (const Pib::Error&) {
Yingdi Yub8f8b342015-04-27 11:06:42 -070061 * ...
62 * }
63 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -070064 * A Key instance created using this constructor is invalid. Calling a
Yingdi Yub8f8b342015-04-27 11:06:42 -070065 * member method on an invalid Key instance may cause an std::domain_error.
66 */
67 Key();
68
Yingdi Yu6ee2d362015-07-16 21:48:05 -070069 /**
70 * @brief Create a Key with @p keyName
71 *
72 * If the key/identity does not exist in the backend, create it in backend.
73 *
74 * @param keyName Key name
75 * @param key The public key to add.
76 * @param keyLen The length of the key.
77 * @param impl The actual backend implementation.
78 */
79 Key(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> impl);
80
81 /**
82 * @brief Create a Key with @p keyName
83 *
84 * @param keyName Key name
85 * @param impl The actual backend implementation.
86 * @throws Pib::Error if the key does not exist.
87 */
88 Key(const Name& keyName, shared_ptr<PibImpl> impl);
89
90 /// @brief Get the key name.
Yingdi Yub8f8b342015-04-27 11:06:42 -070091 const Name&
92 getName() const;
93
94 /// @brief Get the name of the belonging identity.
95 const Name&
96 getIdentity() const;
97
Yingdi Yu6ee2d362015-07-16 21:48:05 -070098 /// @brief Get key type.
99 KeyType
100 getKeyType() const
101 {
102 return m_keyType;
103 }
Yingdi Yub8f8b342015-04-27 11:06:42 -0700104
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700105 /// @brief Get public key.
106 const Buffer&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700107 getPublicKey() const;
108
109 /**
Yingdi Yuc8209892015-06-19 17:47:56 -0700110 * @brief Get a certificate.
111 *
112 * @return the certificate
113 * @throws Pib::Error if the certificate does not exist.
114 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700115 v2::Certificate
Yingdi Yuc8209892015-06-19 17:47:56 -0700116 getCertificate(const Name& certName) const;
117
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700118 /// @brief Get all certificates for this key.
Yingdi Yuc8209892015-06-19 17:47:56 -0700119 const CertificateContainer&
120 getCertificates() const;
121
122 /**
123 * @brief Get the default certificate for this Key.
124 *
125 * @throws Pib::Error if the default certificate does not exist.
126 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700127 const v2::Certificate&
Yingdi Yuc8209892015-06-19 17:47:56 -0700128 getDefaultCertificate() const;
129
130 /// @brief Check if the Key instance is valid
131 operator bool() const;
132
133 /// @brief Check if the Key instance is invalid
134 bool
135 operator!() const;
136
137NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
Yingdi Yuc8209892015-06-19 17:47:56 -0700138 /**
Yingdi Yub8f8b342015-04-27 11:06:42 -0700139 * @brief Add a certificate.
140 *
141 * @param certificate The certificate to add.
142 */
143 void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700144 addCertificate(const v2::Certificate& certificate);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700145
146 /**
147 * @brief Remove a certificate.
148 *
149 * @param certName The name of the certificate to delete.
150 */
151 void
152 removeCertificate(const Name& certName);
153
154 /**
Yingdi Yub8f8b342015-04-27 11:06:42 -0700155 * @brief Set the default certificate.
156 *
157 * @param certName The name of the default certificate of the key.
158 * @return the default certificate
159 * @throws Pib::Error if the certificate does not exist.
160 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700161 const v2::Certificate&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700162 setDefaultCertificate(const Name& certName);
163
164 /**
165 * @brief Set the default certificate.
166 *
167 * If the certificate does not exist, add it and set it as the default certificate of the key.
168 * If the certificate exists, simply set it as the default certificate of the key.
169 *
170 * @param certificate The certificate to add.
171 * @return the default certificate
172 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700173 const v2::Certificate&
174 setDefaultCertificate(const v2::Certificate& certificate);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700175
Yingdi Yub8f8b342015-04-27 11:06:42 -0700176NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
177 /**
Yingdi Yub8f8b342015-04-27 11:06:42 -0700178 * @brief Check the validity of this instance
179 *
180 * @throws std::domain_error if the instance is invalid
181 */
182 void
183 validityCheck() const;
184
185private:
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700186 Name m_identity;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700187 Name m_keyName;
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700188 Buffer m_key;
189 KeyType m_keyType;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700190
Yingdi Yuc8209892015-06-19 17:47:56 -0700191 mutable bool m_hasDefaultCertificate;
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700192 mutable v2::Certificate m_defaultCertificate;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700193
Yingdi Yuc8209892015-06-19 17:47:56 -0700194 mutable bool m_needRefreshCerts;
195 mutable CertificateContainer m_certificates;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700196
197 shared_ptr<PibImpl> m_impl;
198};
199
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700200} // namespace pib
201
202using pib::Key;
203
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700204namespace v2 {
205
206/**
207 * @brief Construct key name based on the appropriate naming conventions
208 */
209Name
210constructKeyName(const Name& identity, const name::Component& keyId);
211
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700212/**
213 * @brief Check if @p keyName follow the naming conventions for the key name
214 */
215bool
216isValidKeyName(const Name& keyName);
217
218/**
219 * @brief Extract identity namespace from the key name @p keyName
220 */
221Name
222extractIdentityFromKeyName(const Name& keyName);
223
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700224} // namespace v2
225
Yingdi Yub8f8b342015-04-27 11:06:42 -0700226} // namespace security
227} // namespace ndn
228
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700229#endif // NDN_SECURITY_PIB_KEY_HPP