blob: 4acc54a4dfa5b3e7946d15bda85b055fb55cc7b5 [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 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 */
Yingdi Yuc8209892015-06-19 17:47:56 -0700128 operator bool() const;
129
Yingdi Yucbe72b02015-11-25 17:35:37 -0800130 /**
131 * @brief Check if the Key instance is invalid.
132 */
Yingdi Yuc8209892015-06-19 17:47:56 -0700133 bool
134 operator!() const;
135
136NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
Yingdi Yuc8209892015-06-19 17:47:56 -0700137 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800138 * @brief Add @p certificate.
139 * @throw std::invalid_argument certificate name does not match key name
140 * @throw Pib::Error a certificate with the same name already exists
Yingdi Yub8f8b342015-04-27 11:06:42 -0700141 */
142 void
Yingdi Yufe4733a2015-10-22 14:24:12 -0700143 addCertificate(const v2::Certificate& certificate) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700144
145 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800146 * @brief Remove a certificate with @p certName
147 * @throw std::invalid_argument @p certName does not match key name
Yingdi Yub8f8b342015-04-27 11:06:42 -0700148 */
149 void
Yingdi Yufe4733a2015-10-22 14:24:12 -0700150 removeCertificate(const Name& certName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700151
152 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800153 * @brief Set an existing certificate with @p certName as the default certificate
154 * @throw std::invalid_argument @p certName does not match key name
155 * @throw Pib::Error the certificate does not exist.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700156 * @return the default certificate
Yingdi Yub8f8b342015-04-27 11:06:42 -0700157 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700158 const v2::Certificate&
Yingdi Yufe4733a2015-10-22 14:24:12 -0700159 setDefaultCertificate(const Name& certName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700160
161 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800162 * @brief Add @p certificate and set it as the default certificate of the key
163 * @throw std::invalid_argument @p certificate does not match key name
164 * @throw Pib::Error the certificate with the same name already exists.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700165 * @return the default certificate
166 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700167 const v2::Certificate&
Yingdi Yufe4733a2015-10-22 14:24:12 -0700168 setDefaultCertificate(const v2::Certificate& certificate) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700169
Yingdi Yucbe72b02015-11-25 17:35:37 -0800170private:
Yingdi Yub8f8b342015-04-27 11:06:42 -0700171 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800172 * @brief Check the validity of the instance
173 * @return a shared_ptr when the instance is valid
174 * @throw std::domain_error the instance is invalid
Yingdi Yub8f8b342015-04-27 11:06:42 -0700175 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800176 shared_ptr<detail::KeyImpl>
177 lock() const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700178
179private:
Yingdi Yucbe72b02015-11-25 17:35:37 -0800180 weak_ptr<detail::KeyImpl> m_impl;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700181
182 friend class v2::KeyChain;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700183};
184
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700185} // namespace pib
186
187using pib::Key;
188
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700189namespace v2 {
190
191/**
192 * @brief Construct key name based on the appropriate naming conventions
193 */
194Name
195constructKeyName(const Name& identity, const name::Component& keyId);
196
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700197/**
198 * @brief Check if @p keyName follow the naming conventions for the key name
199 */
200bool
201isValidKeyName(const Name& keyName);
202
203/**
204 * @brief Extract identity namespace from the key name @p keyName
205 */
206Name
207extractIdentityFromKeyName(const Name& keyName);
208
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700209} // namespace v2
210
Yingdi Yub8f8b342015-04-27 11:06:42 -0700211} // namespace security
212} // namespace ndn
213
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700214#endif // NDN_SECURITY_PIB_KEY_HPP