blob: fbbed246cd47e92a992e8f35da93b5c4e5aa9991 [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
Yingdi Yucbe72b02015-11-25 17:35:37 -080080 /*
81 * @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 Yucbe72b02015-11-25 17:35:37 -0800131 /**
132 * @brief Check if the Key instance is invalid.
133 */
Yingdi Yuc8209892015-06-19 17:47:56 -0700134 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 Yucbe72b02015-11-25 17:35:37 -0800139 * @brief Add @p certificate.
140 * @throw std::invalid_argument certificate name does not match key name
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800141 *
142 * If a certificate with the same name (without implicit digest) already exists, overwrite
143 * the certificate.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700144 */
145 void
Yingdi Yufe4733a2015-10-22 14:24:12 -0700146 addCertificate(const v2::Certificate& certificate) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700147
148 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800149 * @brief Remove a certificate with @p certName
150 * @throw std::invalid_argument @p certName does not match key name
Yingdi Yub8f8b342015-04-27 11:06:42 -0700151 */
152 void
Yingdi Yufe4733a2015-10-22 14:24:12 -0700153 removeCertificate(const Name& certName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700154
155 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800156 * @brief Set an existing certificate with @p certName as the default certificate
157 * @throw std::invalid_argument @p certName does not match key name
158 * @throw Pib::Error the certificate does not exist.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700159 * @return the default certificate
Yingdi Yub8f8b342015-04-27 11:06:42 -0700160 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700161 const v2::Certificate&
Yingdi Yufe4733a2015-10-22 14:24:12 -0700162 setDefaultCertificate(const Name& certName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700163
164 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800165 * @brief Add @p certificate and set it as the default certificate of the key
166 * @throw std::invalid_argument @p certificate does not match key name
Yingdi Yub8f8b342015-04-27 11:06:42 -0700167 * @return the default certificate
168 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700169 const v2::Certificate&
Yingdi Yufe4733a2015-10-22 14:24:12 -0700170 setDefaultCertificate(const v2::Certificate& certificate) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700171
Yingdi Yucbe72b02015-11-25 17:35:37 -0800172private:
Yingdi Yub8f8b342015-04-27 11:06:42 -0700173 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800174 * @brief Check the validity of the instance
175 * @return a shared_ptr when the instance is valid
176 * @throw std::domain_error the instance is invalid
Yingdi Yub8f8b342015-04-27 11:06:42 -0700177 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800178 shared_ptr<detail::KeyImpl>
179 lock() const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700180
181private:
Yingdi Yucbe72b02015-11-25 17:35:37 -0800182 weak_ptr<detail::KeyImpl> m_impl;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700183
184 friend class v2::KeyChain;
Junxiao Shi5759be32017-10-15 00:00:52 +0000185 friend bool operator!=(const Key&, const Key&);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700186};
187
Junxiao Shi5759be32017-10-15 00:00:52 +0000188bool
189operator!=(const Key& lhs, const Key& rhs);
190
191inline bool
192operator==(const Key& lhs, const Key& rhs)
193{
194 return !(lhs != rhs);
195}
196
197std::ostream&
198operator<<(std::ostream& os, const Key& key);
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