blob: b691960ed9655fb3a119f7868512f75f85500930 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5759be32017-10-15 00:00:52 +00002/*
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 Yufe4733a2015-10-22 14:24:12 -070031namespace v2 {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070032class KeyChain;
Yingdi Yufe4733a2015-10-22 14:24:12 -070033} // namespace v2
Yingdi Yu6ee2d362015-07-16 21:48:05 -070034
35namespace pib {
36
Yingdi Yucbe72b02015-11-25 17:35:37 -080037namespace detail {
38class KeyImpl;
39} // namespace detail
Yingdi Yub8f8b342015-04-27 11:06:42 -070040
41/**
Yingdi Yucbe72b02015-11-25 17:35:37 -080042 * @brief A frontend handle of a key instance
Yingdi Yub8f8b342015-04-27 11:06:42 -070043 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -070044 * Key is at the second level in PIB's Identity-Key-Certificate hierarchy. A Key has a Name
45 * (identity + "KEY" + keyId), and contains one or more certificates, one of which is set as
46 * the default certificate of this key. A certificate can be directly accessed from a Key
47 * object.
Yingdi Yub8f8b342015-04-27 11:06:42 -070048 */
49class Key
50{
51public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070052 /**
53 * @brief Default Constructor
54 *
55 * Key created using this default constructor is just a place holder.
Yingdi Yucbe72b02015-11-25 17:35:37 -080056 * It can obtain an actual instance from Identity::getKey(...). A typical
Yingdi Yub8f8b342015-04-27 11:06:42 -070057 * usage would be for exception handling:
58 *
59 * Key key;
60 * try {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070061 * key = identity.getKey(...);
Yingdi Yub8f8b342015-04-27 11:06:42 -070062 * }
Yingdi Yu6ee2d362015-07-16 21:48:05 -070063 * catch (const Pib::Error&) {
Yingdi Yub8f8b342015-04-27 11:06:42 -070064 * ...
65 * }
66 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -070067 * A Key instance created using this constructor is invalid. Calling a
Yingdi Yub8f8b342015-04-27 11:06:42 -070068 * member method on an invalid Key instance may cause an std::domain_error.
69 */
70 Key();
71
Yingdi Yu6ee2d362015-07-16 21:48:05 -070072 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -080073 * @brief Create a Key with a backend implementation @p impl.
Yingdi Yu6ee2d362015-07-16 21:48:05 -070074 *
Yingdi Yucbe72b02015-11-25 17:35:37 -080075 * This method should only be used by KeyContainer.
Yingdi Yu6ee2d362015-07-16 21:48:05 -070076 */
Yingdi Yucbe72b02015-11-25 17:35:37 -080077 explicit
78 Key(weak_ptr<detail::KeyImpl> impl);
Yingdi Yu6ee2d362015-07-16 21:48:05 -070079
Davide Pesaventobdcedf42017-10-15 14:56:28 -040080 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -080081 * @brief Get key name.
Yingdi Yu6ee2d362015-07-16 21:48:05 -070082 */
Yingdi Yub8f8b342015-04-27 11:06:42 -070083 const Name&
84 getName() const;
85
Yingdi Yucbe72b02015-11-25 17:35:37 -080086 /**
87 * @brief Get the name of the belonging identity.
88 */
Yingdi Yub8f8b342015-04-27 11:06:42 -070089 const Name&
90 getIdentity() const;
91
Yingdi Yucbe72b02015-11-25 17:35:37 -080092 /**
93 * @brief Get key type.
94 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -070095 KeyType
Yingdi Yucbe72b02015-11-25 17:35:37 -080096 getKeyType() const;
Yingdi Yub8f8b342015-04-27 11:06:42 -070097
Yingdi Yucbe72b02015-11-25 17:35:37 -080098 /**
99 * @brief Get public key bits.
100 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700101 const Buffer&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700102 getPublicKey() const;
103
104 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800105 * @brief Get a certificate with @p certName
106 * @throw std::invalid_argument @p certName does not match key name
107 * @throw Pib::Error the certificate does not exist.
Yingdi Yuc8209892015-06-19 17:47:56 -0700108 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700109 v2::Certificate
Yingdi Yuc8209892015-06-19 17:47:56 -0700110 getCertificate(const Name& certName) const;
111
Yingdi Yucbe72b02015-11-25 17:35:37 -0800112 /**
113 * @brief Get all certificates for this key.
114 */
Yingdi Yuc8209892015-06-19 17:47:56 -0700115 const CertificateContainer&
116 getCertificates() const;
117
118 /**
119 * @brief Get the default certificate for this Key.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800120 * @throw Pib::Error the default certificate does not exist.
Yingdi Yuc8209892015-06-19 17:47:56 -0700121 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700122 const v2::Certificate&
Yingdi Yuc8209892015-06-19 17:47:56 -0700123 getDefaultCertificate() const;
124
Yingdi Yucbe72b02015-11-25 17:35:37 -0800125 /**
126 * @brief Check if the Key instance is valid.
127 */
Junxiao Shi5759be32017-10-15 00:00:52 +0000128 explicit
Yingdi Yuc8209892015-06-19 17:47:56 -0700129 operator bool() const;
130
Yingdi Yuc8209892015-06-19 17:47:56 -0700131NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
Yingdi Yuc8209892015-06-19 17:47:56 -0700132 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800133 * @brief Add @p certificate.
134 * @throw std::invalid_argument certificate name does not match key name
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800135 *
136 * If a certificate with the same name (without implicit digest) already exists, overwrite
137 * the certificate.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700138 */
139 void
Yingdi Yufe4733a2015-10-22 14:24:12 -0700140 addCertificate(const v2::Certificate& certificate) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700141
142 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800143 * @brief Remove a certificate with @p certName
144 * @throw std::invalid_argument @p certName does not match key name
Yingdi Yub8f8b342015-04-27 11:06:42 -0700145 */
146 void
Yingdi Yufe4733a2015-10-22 14:24:12 -0700147 removeCertificate(const Name& certName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700148
149 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800150 * @brief Set an existing certificate with @p certName as the default certificate
151 * @throw std::invalid_argument @p certName does not match key name
152 * @throw Pib::Error the certificate does not exist.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700153 * @return the default certificate
Yingdi Yub8f8b342015-04-27 11:06:42 -0700154 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700155 const v2::Certificate&
Yingdi Yufe4733a2015-10-22 14:24:12 -0700156 setDefaultCertificate(const Name& certName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700157
158 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800159 * @brief Add @p certificate and set it as the default certificate of the key
160 * @throw std::invalid_argument @p certificate does not match key name
Yingdi Yub8f8b342015-04-27 11:06:42 -0700161 * @return the default certificate
162 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700163 const v2::Certificate&
Yingdi Yufe4733a2015-10-22 14:24:12 -0700164 setDefaultCertificate(const v2::Certificate& certificate) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700165
Yingdi Yucbe72b02015-11-25 17:35:37 -0800166private:
Yingdi Yub8f8b342015-04-27 11:06:42 -0700167 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800168 * @brief Check the validity of the instance
169 * @return a shared_ptr when the instance is valid
170 * @throw std::domain_error the instance is invalid
Yingdi Yub8f8b342015-04-27 11:06:42 -0700171 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800172 shared_ptr<detail::KeyImpl>
173 lock() const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700174
175private:
Yingdi Yucbe72b02015-11-25 17:35:37 -0800176 weak_ptr<detail::KeyImpl> m_impl;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700177
178 friend class v2::KeyChain;
Junxiao Shi5759be32017-10-15 00:00:52 +0000179 friend bool operator!=(const Key&, const Key&);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700180};
181
Junxiao Shi5759be32017-10-15 00:00:52 +0000182bool
183operator!=(const Key& lhs, const Key& rhs);
184
185inline bool
186operator==(const Key& lhs, const Key& rhs)
187{
188 return !(lhs != rhs);
189}
190
191std::ostream&
192operator<<(std::ostream& os, const Key& key);
193
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700194} // namespace pib
195
196using pib::Key;
197
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700198namespace v2 {
199
200/**
201 * @brief Construct key name based on the appropriate naming conventions
202 */
203Name
204constructKeyName(const Name& identity, const name::Component& keyId);
205
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700206/**
207 * @brief Check if @p keyName follow the naming conventions for the key name
208 */
209bool
210isValidKeyName(const Name& keyName);
211
212/**
213 * @brief Extract identity namespace from the key name @p keyName
214 */
215Name
216extractIdentityFromKeyName(const Name& keyName);
217
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700218} // namespace v2
219
Yingdi Yub8f8b342015-04-27 11:06:42 -0700220} // namespace security
221} // namespace ndn
222
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700223#endif // NDN_SECURITY_PIB_KEY_HPP