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-2023 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::security {
33 
40  V03,
42  V02,
43 };
44 
52 {
53 public:
54  class Error : public std::runtime_error
55  {
56  public:
57  using std::runtime_error::runtime_error;
58  };
59 
60  enum SignerType {
73  };
74 
75 public:
85  explicit
87  const Name& signerName = Name(),
88  const SignatureInfo& signatureInfo = SignatureInfo());
89 
93  explicit
94  SigningInfo(const Identity& identity);
95 
99  explicit
100  SigningInfo(const Key& key);
101 
115  explicit
116  SigningInfo(std::string_view signingStr);
117 
122  SigningInfo&
123  setSigningIdentity(const Name& identity);
124 
129  SigningInfo&
130  setSigningKeyName(const Name& keyName);
131 
136  SigningInfo&
137  setSigningCertName(const Name& certificateName);
138 
143  SigningInfo&
144  setSigningHmacKey(std::string_view hmacKey);
145 
150  SigningInfo&
152 
157  SigningInfo&
158  setPibIdentity(const Identity& identity);
159 
164  SigningInfo&
165  setPibKey(const Key& key);
166 
170  SignerType
172  {
173  return m_type;
174  }
175 
179  const Name&
181  {
182  return m_name;
183  }
184 
190  const Identity&
192  {
193  BOOST_ASSERT(m_type == SIGNER_TYPE_ID);
194  return m_identity;
195  }
196 
201  const Key&
202  getPibKey() const
203  {
204  BOOST_ASSERT(m_type == SIGNER_TYPE_KEY);
205  return m_key;
206  }
207 
208  shared_ptr<transform::PrivateKey>
209  getHmacKey() const
210  {
211  BOOST_ASSERT(m_type == SIGNER_TYPE_HMAC);
212  return m_hmacKey;
213  }
214 
218  SigningInfo&
220  {
221  m_digestAlgorithm = algorithm;
222  return *this;
223  }
224 
230  {
231  return m_digestAlgorithm;
232  }
233 
237  SigningInfo&
238  setSignatureInfo(const SignatureInfo& signatureInfo);
239 
243  const SignatureInfo&
245  {
246  return m_info;
247  }
248 
254  SigningInfo&
256  {
257  m_signedInterestFormat = signedInterestFormat;
258  return *this;
259  }
260 
268  {
269  return m_signedInterestFormat;
270  }
271 
272 public:
276  static const Name&
278 
282  static const Name&
283  getHmacIdentity();
284 
285 private: // non-member operators
286  // NOTE: the following "hidden friend" operators are available via
287  // argument-dependent lookup only and must be defined inline.
288 
289  friend bool
290  operator==(const SigningInfo& lhs, const SigningInfo& rhs)
291  {
292  return !(lhs != rhs);
293  }
294 
295  friend bool
296  operator!=(const SigningInfo& lhs, const SigningInfo& rhs)
297  {
298  return lhs.m_type != rhs.m_type ||
299  lhs.m_name != rhs.m_name ||
300  lhs.m_digestAlgorithm != rhs.m_digestAlgorithm ||
301  lhs.m_info != rhs.m_info ||
302  lhs.m_signedInterestFormat != rhs.m_signedInterestFormat;
303  }
304 
305 private:
306  SignerType m_type;
307  Name m_name;
308  Identity m_identity;
309  Key m_key;
310  shared_ptr<transform::PrivateKey> m_hmacKey;
311  DigestAlgorithm m_digestAlgorithm;
312  SignatureInfo m_info;
313  SignedInterestFormat m_signedInterestFormat;
314 };
315 
316 std::ostream&
317 operator<<(std::ostream& os, const SigningInfo& si);
318 
319 std::ostream&
320 operator<<(std::ostream& os, const SignedInterestFormat& format);
321 
322 } // namespace ndn::security
323 
324 #endif // NDN_CXX_SECURITY_SIGNING_INFO_HPP
Represents an absolute name.
Definition: name.hpp:45
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 handle identity.
const Name & getSignerName() const
SigningInfo & setSigningIdentity(const Name &identity)
Set signer as an identity with name identity.
SigningInfo & setSignedInterestFormat(SignedInterestFormat signedInterestFormat)
Set the 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.
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
Return the signer type.
const SignatureInfo & getSignatureInfo() const
Get a semi-prepared SignatureInfo.
SigningInfo & setSigningHmacKey(std::string_view hmacKey)
Set signer to a base64-encoded HMAC key.
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
Get the signed Interest format.
SigningInfo & setDigestAlgorithm(const DigestAlgorithm &algorithm)
Set the digest algorithm for signing operations.
DigestAlgorithm getDigestAlgorithm() const
Get the digest algorithm for signing operations.
SigningInfo & setSigningKeyName(const Name &keyName)
Set signer as a key with name keyName.
SigningInfo & setPibKey(const Key &key)
Set signer as a PIB key handle key.
Frontend handle for an identity in the PIB.
Definition: identity.hpp:44
Frontend handle for a key in the PIB.
Definition: key.hpp:45
Contains the ndn-cxx security framework.
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:71
@ SignatureInfo
Definition: tlv.hpp:94