blob: a7e683f250fcbc8e1436ac6681dcd92ece8f1199 [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 *
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800110 * If a certificate with the same name (without implicit digest) already exists, overwrite
111 * the certificate.
112 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800113 * @throw std::invalid_argument certificate name does not match key name
Yingdi Yucbe72b02015-11-25 17:35:37 -0800114 */
115 void
116 addCertificate(const v2::Certificate& certificate);
117
118 /**
119 * @brief Remove a certificate with @p certName
120 * @throw std::invalid_argument @p certName does not match key name
121 */
122 void
123 removeCertificate(const Name& certName);
124
125 /**
126 * @brief Get a certificate with @p certName
127 * @throw std::invalid_argument @p certName does not match key name
128 * @throw Pib::Error the certificate does not exist.
129 */
130 v2::Certificate
131 getCertificate(const Name& certName) const;
132
133 /// @brief Get all the certificates for this key.
134 const CertificateContainer&
135 getCertificates() const;
136
137 /**
138 * @brief Set an existing one with @p certName as the default certificate
139 * @throw std::invalid_argument @p certName does not match key name
140 * @throw Pib::Error the certificate does not exist.
141 * @return the default certificate
142 */
143 const v2::Certificate&
144 setDefaultCertificate(const Name& certName);
145
146 /**
147 * @brief Add @p certificate and set it as the default certificate of the key
148 * @throw std::invalid_argument @p certificate does not match key name
149 * @throw Pib::Error the certificate with the same name already exists.
150 * @return the default certificate
151 */
152 const v2::Certificate&
153 setDefaultCertificate(const v2::Certificate& certificate);
154
155 /**
156 * @brief Get the default certificate for this Key.
157 * @throw Pib::Error the default certificate does not exist.
158 */
159 const v2::Certificate&
160 getDefaultCertificate() const;
161
162private:
163 Name m_identity;
164 Name m_keyName;
165 Buffer m_key;
166 KeyType m_keyType;
167
168 mutable bool m_isDefaultCertificateLoaded;
169 mutable v2::Certificate m_defaultCertificate;
170
171 CertificateContainer m_certificates;
172
173 shared_ptr<PibImpl> m_impl;
174};
175
176} // namespace detail
177} // namespace pib
178} // namespace security
179} // namespace ndn
180
181#endif // NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP