signing-info.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 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_CXX_SECURITY_SIGNING_INFO_HPP
23 #define NDN_CXX_SECURITY_SIGNING_INFO_HPP
24 
25 #include "ndn-cxx/name.hpp"
31 
32 namespace ndn {
33 namespace security {
34 
41  V03,
43  V02,
44 };
45 
53 {
54 public:
55  class Error : public std::runtime_error
56  {
57  public:
58  using std::runtime_error::runtime_error;
59  };
60 
61  enum SignerType {
74  };
75 
76 public:
86  explicit
88  const Name& signerName = Name(),
89  const SignatureInfo& signatureInfo = SignatureInfo());
90 
94  explicit
95  SigningInfo(const Identity& identity);
96 
100  explicit
101  SigningInfo(const Key& key);
102 
116  explicit
117  SigningInfo(const std::string& signingStr);
118 
123  SigningInfo&
124  setSigningIdentity(const Name& identity);
125 
130  SigningInfo&
131  setSigningKeyName(const Name& keyName);
132 
137  SigningInfo&
138  setSigningCertName(const Name& certificateName);
139 
144  SigningInfo&
145  setSigningHmacKey(const std::string& hmacKey);
146 
151  SigningInfo&
153 
158  SigningInfo&
159  setPibIdentity(const Identity& identity);
160 
165  SigningInfo&
166  setPibKey(const Key& key);
167 
171  SignerType
173  {
174  return m_type;
175  }
176 
180  const Name&
182  {
183  return m_name;
184  }
185 
191  const Identity&
193  {
194  BOOST_ASSERT(m_type == SIGNER_TYPE_ID);
195  return m_identity;
196  }
197 
202  const Key&
203  getPibKey() const
204  {
205  BOOST_ASSERT(m_type == SIGNER_TYPE_KEY);
206  return m_key;
207  }
208 
209  shared_ptr<transform::PrivateKey>
210  getHmacKey() const
211  {
212  BOOST_ASSERT(m_type == SIGNER_TYPE_HMAC);
213  return m_hmacKey;
214  }
215 
219  SigningInfo&
221  {
222  m_digestAlgorithm = algorithm;
223  return *this;
224  }
225 
231  {
232  return m_digestAlgorithm;
233  }
234 
238  SigningInfo&
239  setSignatureInfo(const SignatureInfo& signatureInfo);
240 
244  const SignatureInfo&
246  {
247  return m_info;
248  }
249 
255  SigningInfo&
257  {
258  m_signedInterestFormat = signedInterestFormat;
259  return *this;
260  }
261 
269  {
270  return m_signedInterestFormat;
271  }
272 
273 public:
277  static const Name&
279 
283  static const Name&
284  getHmacIdentity();
285 
286 private: // non-member operators
287  // NOTE: the following "hidden friend" operators are available via
288  // argument-dependent lookup only and must be defined inline.
289 
290  friend bool
291  operator==(const SigningInfo& lhs, const SigningInfo& rhs)
292  {
293  return !(lhs != rhs);
294  }
295 
296  friend bool
297  operator!=(const SigningInfo& lhs, const SigningInfo& rhs)
298  {
299  return lhs.m_type != rhs.m_type ||
300  lhs.m_name != rhs.m_name ||
301  lhs.m_digestAlgorithm != rhs.m_digestAlgorithm ||
302  lhs.m_info != rhs.m_info ||
303  lhs.m_signedInterestFormat != rhs.m_signedInterestFormat;
304  }
305 
306 private:
307  SignerType m_type;
308  Name m_name;
309  Identity m_identity;
310  Key m_key;
311  shared_ptr<transform::PrivateKey> m_hmacKey;
312  DigestAlgorithm m_digestAlgorithm;
313  SignatureInfo m_info;
314  SignedInterestFormat m_signedInterestFormat;
315 };
316 
317 std::ostream&
318 operator<<(std::ostream& os, const SigningInfo& si);
319 
320 std::ostream&
321 operator<<(std::ostream& os, const SignedInterestFormat& format);
322 
323 } // namespace security
324 } // namespace ndn
325 
326 #endif // NDN_CXX_SECURITY_SIGNING_INFO_HPP
Represents an absolute name.
Definition: name.hpp:46
Represents a SignatureInfo or InterestSignatureInfo TLV element.
Signing parameters passed to KeyChain.
const Key & getPibKey() const
SigningInfo(SignerType signerType=SIGNER_TYPE_NULL, const Name &signerName=Name(), const SignatureInfo &signatureInfo=SignatureInfo())
Constructor.
friend bool operator!=(const SigningInfo &lhs, const SigningInfo &rhs)
SigningInfo & setPibIdentity(const Identity &identity)
Set signer as a PIB identity handler identity.
const Name & getSignerName() const
SigningInfo & setSigningIdentity(const Name &identity)
Set signer as an identity with name identity.
SigningInfo & setSignedInterestFormat(SignedInterestFormat signedInterestFormat)
Set signed Interest format.
static const Name & getDigestSha256Identity()
A localhost identity to indicate that the signature is generated using SHA-256.
SigningInfo & setSha256Signing()
Set SHA-256 as the signing method.
SigningInfo & setSigningCertName(const Name &certificateName)
Set signer as a certificate with name certificateName.
SigningInfo & setSigningHmacKey(const std::string &hmacKey)
Set signer to a base64-encoded HMAC key.
static const Name & getHmacIdentity()
A localhost identity to indicate that the signature is generated using an HMAC key.
friend bool operator==(const SigningInfo &lhs, const SigningInfo &rhs)
SigningInfo & setSignatureInfo(const SignatureInfo &signatureInfo)
Set a semi-prepared SignatureInfo.
SignerType getSignerType() const
const SignatureInfo & getSignatureInfo() const
const Identity & getPibIdentity() const
@ SIGNER_TYPE_CERT
Signer is a certificate, use it directly.
@ SIGNER_TYPE_SHA256
Use a SHA-256 digest only, no signer needs to be specified.
@ SIGNER_TYPE_HMAC
Signer is a HMAC key.
@ SIGNER_TYPE_NULL
No signer is specified, use default setting or follow the trust schema.
@ SIGNER_TYPE_ID
Signer is an identity, use its default key and default certificate.
@ SIGNER_TYPE_KEY
Signer is a key, use its default certificate.
shared_ptr< transform::PrivateKey > getHmacKey() const
SignedInterestFormat getSignedInterestFormat() const
SigningInfo & setDigestAlgorithm(const DigestAlgorithm &algorithm)
Set the digest algorithm for signing operations.
DigestAlgorithm getDigestAlgorithm() const
SigningInfo & setSigningKeyName(const Name &keyName)
Set signer as a key with name keyName.
SigningInfo & setPibKey(const Key &key)
Set signer as a PIB key handler key.
A frontend handle of an Identity.
Definition: identity.hpp:48
A frontend handle of a key instance.
Definition: key.hpp:50
std::ostream & operator<<(std::ostream &os, const AdditionalDescription &desc)
@ V03
Sign Interest using Packet Specification v0.3 semantics.
@ V02
Sign Interest using Packet Specification v0.2 semantics.
@ Name
Definition: tlv.hpp:67
@ SignatureInfo
Definition: tlv.hpp:82
Definition: data.cpp:25