blob: c05e77effdc07e1cdc08041004e89b4fa908aa24 [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
25#include "../../../data.hpp"
26#include "../certificate-container.hpp"
27#include "../../security-common.hpp"
28
29namespace ndn {
30namespace security {
31namespace pib {
32
33class PibImpl;
34
35namespace detail {
36
37/**
38 * @brief Backend instance of Key
39 *
40 * An Key has only one backend instance, but may have multiple frontend handles.
41 * Each frontend handle is associated with the only one backend KeyImpl.
42 *
43 * @throw PibImpl::Error when underlying implementation has non-semantic error.
44 */
45class KeyImpl : noncopyable
46{
47public:
48 /**
49 * @brief Create a KeyImpl with @p keyName.
50 *
51 * If the key does not exist in the backend, create it in backend.
52 *
53 * @param keyName The name of the key.
54 * @param key The public key to add.
55 * @param keyLen The length of the key.
56 * @param impl The Pib backend implementation.
57 * @throw Pib::Error a key with the same @p keyName already exists.
58 */
59 KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> impl);
60
61 /**
62 * @brief Create a KeyImpl with @p keyName.
63 *
64 * @param keyName The name of the key.
65 * @param impl The Pib backend implementation.
66 * @throw Pib::Error the key does not exist.
67 */
68 KeyImpl(const Name& keyName, shared_ptr<PibImpl> impl);
69
70 /// @brief Get the name of the key.
71 const Name&
72 getName() const
73 {
74 return m_keyName;
75 }
76
77 /**
78 * @brief Get the name of the belonging identity.
79 */
80 const Name&
81 getIdentity() const
82 {
83 return m_identity;
84 }
85
86 /**
87 * @brief Get key type
88 */
89 KeyType
90 getKeyType() const
91 {
92 return m_keyType;
93 }
94
95 /**
96 * @brief Get public key bits
97 */
98 const Buffer&
99 getPublicKey() const
100 {
101 return m_key;
102 }
103
104 /**
105 * @brief Add @p certificate.
106 *
107 * If no default certificate is set before, the new certificate will be set as the default
108 * certificate of the key.
109 *
110 * @throw std::invalid_argument certificate name does not match key name
111 * @throw Pib::Error a certificate with the same name already exists
112 */
113 void
114 addCertificate(const v2::Certificate& certificate);
115
116 /**
117 * @brief Remove a certificate with @p certName
118 * @throw std::invalid_argument @p certName does not match key name
119 */
120 void
121 removeCertificate(const Name& certName);
122
123 /**
124 * @brief Get a certificate with @p certName
125 * @throw std::invalid_argument @p certName does not match key name
126 * @throw Pib::Error the certificate does not exist.
127 */
128 v2::Certificate
129 getCertificate(const Name& certName) const;
130
131 /// @brief Get all the certificates for this key.
132 const CertificateContainer&
133 getCertificates() const;
134
135 /**
136 * @brief Set an existing one with @p certName as the default certificate
137 * @throw std::invalid_argument @p certName does not match key name
138 * @throw Pib::Error the certificate does not exist.
139 * @return the default certificate
140 */
141 const v2::Certificate&
142 setDefaultCertificate(const Name& certName);
143
144 /**
145 * @brief Add @p certificate and set it as the default certificate of the key
146 * @throw std::invalid_argument @p certificate does not match key name
147 * @throw Pib::Error the certificate with the same name already exists.
148 * @return the default certificate
149 */
150 const v2::Certificate&
151 setDefaultCertificate(const v2::Certificate& certificate);
152
153 /**
154 * @brief Get the default certificate for this Key.
155 * @throw Pib::Error the default certificate does not exist.
156 */
157 const v2::Certificate&
158 getDefaultCertificate() const;
159
160private:
161 Name m_identity;
162 Name m_keyName;
163 Buffer m_key;
164 KeyType m_keyType;
165
166 mutable bool m_isDefaultCertificateLoaded;
167 mutable v2::Certificate m_defaultCertificate;
168
169 CertificateContainer m_certificates;
170
171 shared_ptr<PibImpl> m_impl;
172};
173
174} // namespace detail
175} // namespace pib
176} // namespace security
177} // namespace ndn
178
179#endif // NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP