blob: 69ceec8e5eafcc03ff0398dfaa4e78b80b7c0e8c [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 Yub8f8b342015-04-27 11:06:42 -070025#include "certificate-container.hpp"
Yingdi Yu6ee2d362015-07-16 21:48:05 -070026#include "../security-common.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070027
28namespace ndn {
29namespace security {
30
Yingdi Yu6ee2d362015-07-16 21:48:05 -070031class KeyChain;
32
33namespace pib {
34
Yingdi Yucbe72b02015-11-25 17:35:37 -080035namespace detail {
36class KeyImpl;
37} // namespace detail
Yingdi Yub8f8b342015-04-27 11:06:42 -070038
39/**
Yingdi Yucbe72b02015-11-25 17:35:37 -080040 * @brief A frontend handle of a key instance
Yingdi Yub8f8b342015-04-27 11:06:42 -070041 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -070042 * Key is at the second level in PIB's Identity-Key-Certificate hierarchy. A Key has a Name
43 * (identity + "KEY" + keyId), and contains one or more certificates, one of which is set as
44 * the default certificate of this key. A certificate can be directly accessed from a Key
45 * object.
Yingdi Yub8f8b342015-04-27 11:06:42 -070046 */
47class Key
48{
49public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070050 /**
51 * @brief Default Constructor
52 *
53 * Key created using this default constructor is just a place holder.
Yingdi Yucbe72b02015-11-25 17:35:37 -080054 * It can obtain an actual instance from Identity::getKey(...). A typical
Yingdi Yub8f8b342015-04-27 11:06:42 -070055 * usage would be for exception handling:
56 *
57 * Key key;
58 * try {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070059 * key = identity.getKey(...);
Yingdi Yub8f8b342015-04-27 11:06:42 -070060 * }
Yingdi Yu6ee2d362015-07-16 21:48:05 -070061 * catch (const Pib::Error&) {
Yingdi Yub8f8b342015-04-27 11:06:42 -070062 * ...
63 * }
64 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -070065 * A Key instance created using this constructor is invalid. Calling a
Yingdi Yub8f8b342015-04-27 11:06:42 -070066 * member method on an invalid Key instance may cause an std::domain_error.
67 */
68 Key();
69
Yingdi Yu6ee2d362015-07-16 21:48:05 -070070 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -080071 * @brief Create a Key with a backend implementation @p impl.
Yingdi Yu6ee2d362015-07-16 21:48:05 -070072 *
Yingdi Yucbe72b02015-11-25 17:35:37 -080073 * This method should only be used by KeyContainer.
Yingdi Yu6ee2d362015-07-16 21:48:05 -070074 */
Yingdi Yucbe72b02015-11-25 17:35:37 -080075 explicit
76 Key(weak_ptr<detail::KeyImpl> impl);
Yingdi Yu6ee2d362015-07-16 21:48:05 -070077
Yingdi Yucbe72b02015-11-25 17:35:37 -080078 /*
79 * @brief Get key name.
Yingdi Yu6ee2d362015-07-16 21:48:05 -070080 */
Yingdi Yub8f8b342015-04-27 11:06:42 -070081 const Name&
82 getName() const;
83
Yingdi Yucbe72b02015-11-25 17:35:37 -080084 /**
85 * @brief Get the name of the belonging identity.
86 */
Yingdi Yub8f8b342015-04-27 11:06:42 -070087 const Name&
88 getIdentity() const;
89
Yingdi Yucbe72b02015-11-25 17:35:37 -080090 /**
91 * @brief Get key type.
92 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -070093 KeyType
Yingdi Yucbe72b02015-11-25 17:35:37 -080094 getKeyType() const;
Yingdi Yub8f8b342015-04-27 11:06:42 -070095
Yingdi Yucbe72b02015-11-25 17:35:37 -080096 /**
97 * @brief Get public key bits.
98 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -070099 const Buffer&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700100 getPublicKey() const;
101
102 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800103 * @brief Get a certificate with @p certName
104 * @throw std::invalid_argument @p certName does not match key name
105 * @throw Pib::Error the certificate does not exist.
Yingdi Yuc8209892015-06-19 17:47:56 -0700106 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700107 v2::Certificate
Yingdi Yuc8209892015-06-19 17:47:56 -0700108 getCertificate(const Name& certName) const;
109
Yingdi Yucbe72b02015-11-25 17:35:37 -0800110 /**
111 * @brief Get all certificates for this key.
112 */
Yingdi Yuc8209892015-06-19 17:47:56 -0700113 const CertificateContainer&
114 getCertificates() const;
115
116 /**
117 * @brief Get the default certificate for this Key.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800118 * @throw Pib::Error the default certificate does not exist.
Yingdi Yuc8209892015-06-19 17:47:56 -0700119 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700120 const v2::Certificate&
Yingdi Yuc8209892015-06-19 17:47:56 -0700121 getDefaultCertificate() const;
122
Yingdi Yucbe72b02015-11-25 17:35:37 -0800123 /**
124 * @brief Check if the Key instance is valid.
125 */
Yingdi Yuc8209892015-06-19 17:47:56 -0700126 operator bool() const;
127
Yingdi Yucbe72b02015-11-25 17:35:37 -0800128 /**
129 * @brief Check if the Key instance is invalid.
130 */
Yingdi Yuc8209892015-06-19 17:47:56 -0700131 bool
132 operator!() const;
133
134NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
Yingdi Yuc8209892015-06-19 17:47:56 -0700135 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800136 * @brief Add @p certificate.
137 * @throw std::invalid_argument certificate name does not match key name
138 * @throw Pib::Error a certificate with the same name already exists
Yingdi Yub8f8b342015-04-27 11:06:42 -0700139 */
140 void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700141 addCertificate(const v2::Certificate& certificate);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700142
143 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800144 * @brief Remove a certificate with @p certName
145 * @throw std::invalid_argument @p certName does not match key name
Yingdi Yub8f8b342015-04-27 11:06:42 -0700146 */
147 void
148 removeCertificate(const Name& certName);
149
150 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800151 * @brief Set an existing certificate with @p certName as the default certificate
152 * @throw std::invalid_argument @p certName does not match key name
153 * @throw Pib::Error the certificate does not exist.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700154 * @return the default certificate
Yingdi Yub8f8b342015-04-27 11:06:42 -0700155 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700156 const v2::Certificate&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700157 setDefaultCertificate(const Name& certName);
158
159 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800160 * @brief Add @p certificate and set it as the default certificate of the key
161 * @throw std::invalid_argument @p certificate does not match key name
162 * @throw Pib::Error the certificate with the same name already exists.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700163 * @return the default certificate
164 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700165 const v2::Certificate&
166 setDefaultCertificate(const v2::Certificate& certificate);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700167
Yingdi Yucbe72b02015-11-25 17:35:37 -0800168private:
Yingdi Yub8f8b342015-04-27 11:06:42 -0700169 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800170 * @brief Check the validity of the instance
171 * @return a shared_ptr when the instance is valid
172 * @throw std::domain_error the instance is invalid
Yingdi Yub8f8b342015-04-27 11:06:42 -0700173 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800174 shared_ptr<detail::KeyImpl>
175 lock() const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700176
177private:
Yingdi Yucbe72b02015-11-25 17:35:37 -0800178 weak_ptr<detail::KeyImpl> m_impl;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700179};
180
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700181} // namespace pib
182
183using pib::Key;
184
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700185namespace v2 {
186
187/**
188 * @brief Construct key name based on the appropriate naming conventions
189 */
190Name
191constructKeyName(const Name& identity, const name::Component& keyId);
192
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700193/**
194 * @brief Check if @p keyName follow the naming conventions for the key name
195 */
196bool
197isValidKeyName(const Name& keyName);
198
199/**
200 * @brief Extract identity namespace from the key name @p keyName
201 */
202Name
203extractIdentityFromKeyName(const Name& keyName);
204
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700205} // namespace v2
206
Yingdi Yub8f8b342015-04-27 11:06:42 -0700207} // namespace security
208} // namespace ndn
209
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700210#endif // NDN_SECURITY_PIB_KEY_HPP