blob: c889a761040be5d4d97e159b8bfe5cfd28dc8781 [file] [log] [blame]
Yingdi Yucbe72b02015-11-25 17:35:37 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
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
22#ifndef NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP
23#define NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP
24
Yingdi Yucbe72b02015-11-25 17:35:37 -080025#include "../../security-common.hpp"
Davide Pesavento50f66752017-05-15 20:57:12 -040026#include "../certificate-container.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080027
28namespace ndn {
29namespace security {
30namespace pib {
31
32class PibImpl;
33
34namespace detail {
35
36/**
37 * @brief Backend instance of Key
38 *
39 * An Key has only one backend instance, but may have multiple frontend handles.
40 * Each frontend handle is associated with the only one backend KeyImpl.
41 *
42 * @throw PibImpl::Error when underlying implementation has non-semantic error.
43 */
44class KeyImpl : noncopyable
45{
46public:
47 /**
48 * @brief Create a KeyImpl with @p keyName.
49 *
Davide Pesavento92856862017-05-15 21:35:08 -040050 * If the key does not exist in the backend, it will be added.
51 * If a key with the same name already exists, it will be overwritten.
Yingdi Yucbe72b02015-11-25 17:35:37 -080052 *
53 * @param keyName The name of the key.
54 * @param key The public key to add.
55 * @param keyLen The length of the key.
Davide Pesavento50f66752017-05-15 20:57:12 -040056 * @param pibImpl The Pib backend implementation.
Davide Pesavento92856862017-05-15 21:35:08 -040057 * @throw std::invalid_argument @p key is invalid.
Yingdi Yucbe72b02015-11-25 17:35:37 -080058 */
Davide Pesavento50f66752017-05-15 20:57:12 -040059 KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -080060
61 /**
62 * @brief Create a KeyImpl with @p keyName.
63 *
64 * @param keyName The name of the key.
Davide Pesavento50f66752017-05-15 20:57:12 -040065 * @param pibImpl The Pib backend implementation.
Yingdi Yucbe72b02015-11-25 17:35:37 -080066 * @throw Pib::Error the key does not exist.
67 */
Davide Pesavento50f66752017-05-15 20:57:12 -040068 KeyImpl(const Name& keyName, shared_ptr<PibImpl> pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -080069
Davide Pesavento50f66752017-05-15 20:57:12 -040070 /**
71 * @brief Get the name of the key.
72 */
Yingdi Yucbe72b02015-11-25 17:35:37 -080073 const Name&
74 getName() const
75 {
76 return m_keyName;
77 }
78
79 /**
80 * @brief Get the name of the belonging identity.
81 */
82 const Name&
83 getIdentity() const
84 {
85 return m_identity;
86 }
87
88 /**
Davide Pesavento92856862017-05-15 21:35:08 -040089 * @brief Get key type.
Yingdi Yucbe72b02015-11-25 17:35:37 -080090 */
91 KeyType
92 getKeyType() const
93 {
94 return m_keyType;
95 }
96
97 /**
Davide Pesavento92856862017-05-15 21:35:08 -040098 * @brief Get public key bits.
Yingdi Yucbe72b02015-11-25 17:35:37 -080099 */
100 const Buffer&
101 getPublicKey() const
102 {
103 return m_key;
104 }
105
106 /**
107 * @brief Add @p certificate.
108 *
109 * If no default certificate is set before, the new certificate will be set as the default
110 * certificate of the key.
111 *
Davide Pesavento92856862017-05-15 21:35:08 -0400112 * If a certificate with the same name (without implicit digest) already exists, it will
113 * be overwritten.
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800114 *
Davide Pesavento92856862017-05-15 21:35:08 -0400115 * @throw std::invalid_argument the certificate name does not match the key name.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800116 */
117 void
118 addCertificate(const v2::Certificate& certificate);
119
120 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400121 * @brief Remove a certificate with @p certName.
122 * @throw std::invalid_argument @p certName does not match the key name.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800123 */
124 void
125 removeCertificate(const Name& certName);
126
127 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400128 * @brief Get a certificate with @p certName.
129 * @throw std::invalid_argument @p certName does not match the key name.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800130 * @throw Pib::Error the certificate does not exist.
131 */
132 v2::Certificate
133 getCertificate(const Name& certName) const;
134
Davide Pesavento50f66752017-05-15 20:57:12 -0400135 /**
136 * @brief Get all the certificates for this key.
137 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800138 const CertificateContainer&
139 getCertificates() const;
140
141 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400142 * @brief Set an existing certificate with name @p certName as the default certificate.
143 * @throw std::invalid_argument @p certName does not match the key name.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800144 * @throw Pib::Error the certificate does not exist.
145 * @return the default certificate
146 */
147 const v2::Certificate&
148 setDefaultCertificate(const Name& certName);
149
150 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400151 * @brief Add @p certificate and set it as the default certificate for this key.
152 *
153 * If a certificate with the same name (without implicit digest) already exists, it will
154 * be overwritten.
155 *
156 * @throw std::invalid_argument @p certificate does not match the key name.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800157 * @return the default certificate
158 */
159 const v2::Certificate&
160 setDefaultCertificate(const v2::Certificate& certificate);
161
162 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400163 * @brief Get the default certificate for this key.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800164 * @throw Pib::Error the default certificate does not exist.
165 */
166 const v2::Certificate&
167 getDefaultCertificate() const;
168
169private:
170 Name m_identity;
171 Name m_keyName;
172 Buffer m_key;
173 KeyType m_keyType;
174
Davide Pesavento50f66752017-05-15 20:57:12 -0400175 shared_ptr<PibImpl> m_pib;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800176
177 CertificateContainer m_certificates;
Davide Pesavento50f66752017-05-15 20:57:12 -0400178 mutable bool m_isDefaultCertificateLoaded;
179 mutable v2::Certificate m_defaultCertificate;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800180};
181
182} // namespace detail
183} // namespace pib
184} // namespace security
185} // namespace ndn
186
187#endif // NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP