blob: efc69b2f2a9c45cdc781c692b793ba3f8d7a54c3 [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 *
50 * If the key does not exist in the backend, create it in backend.
51 *
52 * @param keyName The name of the key.
53 * @param key The public key to add.
54 * @param keyLen The length of the key.
Davide Pesavento50f66752017-05-15 20:57:12 -040055 * @param pibImpl The Pib backend implementation.
Yingdi Yucbe72b02015-11-25 17:35:37 -080056 * @throw Pib::Error a key with the same @p keyName already exists.
57 */
Davide Pesavento50f66752017-05-15 20:57:12 -040058 KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -080059
60 /**
61 * @brief Create a KeyImpl with @p keyName.
62 *
63 * @param keyName The name of the key.
Davide Pesavento50f66752017-05-15 20:57:12 -040064 * @param pibImpl The Pib backend implementation.
Yingdi Yucbe72b02015-11-25 17:35:37 -080065 * @throw Pib::Error the key does not exist.
66 */
Davide Pesavento50f66752017-05-15 20:57:12 -040067 KeyImpl(const Name& keyName, shared_ptr<PibImpl> pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -080068
Davide Pesavento50f66752017-05-15 20:57:12 -040069 /**
70 * @brief Get the name of the key.
71 */
Yingdi Yucbe72b02015-11-25 17:35:37 -080072 const Name&
73 getName() const
74 {
75 return m_keyName;
76 }
77
78 /**
79 * @brief Get the name of the belonging identity.
80 */
81 const Name&
82 getIdentity() const
83 {
84 return m_identity;
85 }
86
87 /**
88 * @brief Get key type
89 */
90 KeyType
91 getKeyType() const
92 {
93 return m_keyType;
94 }
95
96 /**
97 * @brief Get public key bits
98 */
99 const Buffer&
100 getPublicKey() const
101 {
102 return m_key;
103 }
104
105 /**
106 * @brief Add @p certificate.
107 *
108 * If no default certificate is set before, the new certificate will be set as the default
109 * certificate of the key.
110 *
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800111 * If a certificate with the same name (without implicit digest) already exists, overwrite
112 * the certificate.
113 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800114 * @throw std::invalid_argument certificate name does not match key name
Yingdi Yucbe72b02015-11-25 17:35:37 -0800115 */
116 void
117 addCertificate(const v2::Certificate& certificate);
118
119 /**
120 * @brief Remove a certificate with @p certName
121 * @throw std::invalid_argument @p certName does not match key name
122 */
123 void
124 removeCertificate(const Name& certName);
125
126 /**
127 * @brief Get a certificate with @p certName
128 * @throw std::invalid_argument @p certName does not match key name
129 * @throw Pib::Error the certificate does not exist.
130 */
131 v2::Certificate
132 getCertificate(const Name& certName) const;
133
Davide Pesavento50f66752017-05-15 20:57:12 -0400134 /**
135 * @brief Get all the certificates for this key.
136 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800137 const CertificateContainer&
138 getCertificates() const;
139
140 /**
141 * @brief Set an existing one with @p certName as the default certificate
142 * @throw std::invalid_argument @p certName does not match key name
143 * @throw Pib::Error the certificate does not exist.
144 * @return the default certificate
145 */
146 const v2::Certificate&
147 setDefaultCertificate(const Name& certName);
148
149 /**
150 * @brief Add @p certificate and set it as the default certificate of the key
151 * @throw std::invalid_argument @p certificate does not match key name
152 * @throw Pib::Error the certificate with the same name already exists.
153 * @return the default certificate
154 */
155 const v2::Certificate&
156 setDefaultCertificate(const v2::Certificate& certificate);
157
158 /**
159 * @brief Get the default certificate for this Key.
160 * @throw Pib::Error the default certificate does not exist.
161 */
162 const v2::Certificate&
163 getDefaultCertificate() const;
164
165private:
166 Name m_identity;
167 Name m_keyName;
168 Buffer m_key;
169 KeyType m_keyType;
170
Davide Pesavento50f66752017-05-15 20:57:12 -0400171 shared_ptr<PibImpl> m_pib;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800172
173 CertificateContainer m_certificates;
Davide Pesavento50f66752017-05-15 20:57:12 -0400174 mutable bool m_isDefaultCertificateLoaded;
175 mutable v2::Certificate m_defaultCertificate;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800176};
177
178} // namespace detail
179} // namespace pib
180} // namespace security
181} // namespace ndn
182
183#endif // NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP