NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
signing-info.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 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 
23 
29 
30 namespace ndn {
31 namespace security {
32 
33 const Name&
35 {
36  static Name emptyName;
37  return emptyName;
38 }
39 
40 const SignatureInfo&
42 {
43  static SignatureInfo emptySignatureInfo;
44  return emptySignatureInfo;
45 }
46 
47 const Name&
49 {
50  static Name digestSha256Identity("/localhost/identity/digest-sha256");
51  return digestSha256Identity;
52 }
53 
54 const Name&
56 {
57  static Name hmacIdentity("/localhost/identity/hmac");
58  return hmacIdentity;
59 }
60 
62  const Name& signerName,
63  const SignatureInfo& signatureInfo)
64  : m_type(signerType)
65  , m_name(signerName)
66  , m_digestAlgorithm(DigestAlgorithm::SHA256)
67  , m_info(signatureInfo)
68 {
69  BOOST_ASSERT(signerType >= SIGNER_TYPE_NULL && signerType <= SIGNER_TYPE_HMAC);
70 }
71 
73  : SigningInfo(SIGNER_TYPE_NULL)
74 {
75  this->setPibIdentity(identity);
76 }
77 
79  : SigningInfo(SIGNER_TYPE_NULL)
80 {
81  this->setPibKey(key);
82 }
83 
84 SigningInfo::SigningInfo(const std::string& signingStr)
85  : SigningInfo(SIGNER_TYPE_NULL)
86 {
87  if (signingStr.empty()) {
88  return;
89  }
90 
91  size_t pos = signingStr.find(':');
92  if (pos == std::string::npos) {
93  NDN_THROW(std::invalid_argument("Invalid signing string cannot represent SigningInfo"));
94  }
95 
96  std::string scheme = signingStr.substr(0, pos);
97  std::string nameArg = signingStr.substr(pos + 1);
98 
99  if (scheme == "id") {
100  if (nameArg == getDigestSha256Identity().toUri()) {
102  }
103  else {
104  setSigningIdentity(nameArg);
105  }
106  }
107  else if (scheme == "key") {
108  setSigningKeyName(nameArg);
109  }
110  else if (scheme == "cert") {
111  setSigningCertName(nameArg);
112  }
113  else if (scheme == "hmac-sha256") {
114  setSigningHmacKey(nameArg);
116  }
117  else {
118  NDN_THROW(std::invalid_argument("Invalid signing string scheme"));
119  }
120 }
121 
124 {
125  m_type = SIGNER_TYPE_ID;
126  m_name = identity;
127  m_identity = Identity();
128  return *this;
129 }
130 
133 {
134  m_type = SIGNER_TYPE_KEY;
135  m_name = keyName;
136  m_key = Key();
137  return *this;
138 }
139 
141 SigningInfo::setSigningCertName(const Name& certificateName)
142 {
143  m_type = SIGNER_TYPE_CERT;
144  m_name = certificateName;
145  return *this;
146 }
147 
149 SigningInfo::setSigningHmacKey(const std::string& hmacKey)
150 {
151  m_type = SIGNER_TYPE_HMAC;
152 
153  OBufferStream os;
154  transform::bufferSource(hmacKey) >>
155  transform::base64Decode(false) >>
157  m_hmacKey = make_shared<transform::PrivateKey>();
158  m_hmacKey->loadRaw(KeyType::HMAC, os.buf()->data(), os.buf()->size());
159 
160  // generate key name
161  m_name = getHmacIdentity();
162  m_name.append(name::Component(m_hmacKey->getKeyDigest(DigestAlgorithm::SHA256)));
163 
164  return *this;
165 }
166 
169 {
170  m_type = SIGNER_TYPE_SHA256;
171  m_name.clear();
172  return *this;
173 }
174 
177 {
178  m_type = SIGNER_TYPE_ID;
179  m_name = identity ? identity.getName() : Name();
180  m_identity = identity;
181  return *this;
182 }
183 
186 {
187  m_type = SIGNER_TYPE_KEY;
188  m_name = key ? key.getName() : Name();
189  m_key = key;
190  return *this;
191 }
192 
195 {
196  m_info = signatureInfo;
197  return *this;
198 }
199 
200 std::ostream&
201 operator<<(std::ostream& os, const SigningInfo& si)
202 {
203  switch (si.getSignerType()) {
205  return os;
207  return os << "id:" << si.getSignerName();
209  return os << "key:" << si.getSignerName();
211  return os << "cert:" << si.getSignerName();
213  return os << "id:" << SigningInfo::getDigestSha256Identity();
215  return os << "id:" << si.getSignerName();
216  }
217  NDN_THROW(std::invalid_argument("Unknown signer type"));
218  return os;
219 }
220 
221 } // namespace security
222 } // namespace ndn
ndn::security::SigningInfo::SigningInfo
SigningInfo(SignerType signerType=SIGNER_TYPE_NULL, const Name &signerName=getEmptyName(), const SignatureInfo &signatureInfo=getEmptySignatureInfo())
Constructor.
Definition: signing-info.cpp:61
ndn::security::SigningInfo::SignerType
SignerType
Definition: signing-info.hpp:50
ndn::security::SigningInfo::setSigningIdentity
SigningInfo & setSigningIdentity(const Name &identity)
Set signer as an identity with name identity.
Definition: signing-info.cpp:123
buffer-source.hpp
ndn::DigestAlgorithm::SHA256
@ SHA256
ndn::security::transform::base64Decode
unique_ptr< Transform > base64Decode(bool expectNewlineEvery64Bytes)
Definition: base64-decode.cpp:129
ndn::security::SigningInfo
Signing parameters passed to KeyChain.
Definition: signing-info.hpp:42
ndn::security::SigningInfo::getHmacIdentity
static const Name & getHmacIdentity()
A localhost identity to indicate that the signature is generated using an HMAC key.
Definition: signing-info.cpp:55
ndn::security::transform::bufferSource
BufferSource bufferSource
Definition: buffer-source.hpp:73
ndn::security::pib::Identity::getName
const Name & getName() const
Get the name of the identity.
Definition: identity.cpp:37
base64-decode.hpp
ndn::DigestAlgorithm
DigestAlgorithm
Definition: security-common.hpp:96
ndn::security::SigningInfo::setSigningHmacKey
SigningInfo & setSigningHmacKey(const std::string &hmacKey)
Set signer to a base64-encoded HMAC key.
Definition: signing-info.cpp:149
ndn::security::SigningInfo::setDigestAlgorithm
SigningInfo & setDigestAlgorithm(const DigestAlgorithm &algorithm)
Set the digest algorithm for signing operations.
Definition: signing-info.hpp:209
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
digest-filter.hpp
ndn::security::SigningInfo::setSha256Signing
SigningInfo & setSha256Signing()
Set SHA-256 as the signing method.
Definition: signing-info.cpp:168
ndn::security::SigningInfo::getEmptyName
static const Name & getEmptyName()
Definition: signing-info.cpp:34
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
ndn::security::SigningInfo::setSigningCertName
SigningInfo & setSigningCertName(const Name &certificateName)
Set signer as a certificate with name certificateName.
Definition: signing-info.cpp:141
ndn::security::SigningInfo::SIGNER_TYPE_ID
@ SIGNER_TYPE_ID
Signer is an identity, use its default key and default certificate.
Definition: signing-info.hpp:54
ndn::security::SigningInfo::setPibIdentity
SigningInfo & setPibIdentity(const Identity &identity)
Set signer as a PIB identity handler identity.
Definition: signing-info.cpp:176
ndn::KeyIdType::SHA256
@ SHA256
Use the SHA256 hash of the public key as key id.
ndn::security::operator<<
std::ostream & operator<<(std::ostream &os, const SigningInfo &si)
Definition: signing-info.cpp:201
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::Name::append
Name & append(const Component &component)
Append a component.
Definition: name.hpp:277
ndn::SignatureInfo
Represents a SignatureInfo TLV element.
Definition: signature-info.hpp:35
ndn::KeyType::HMAC
@ HMAC
HMAC key, supports sign/verify operations.
ndn::security::SigningInfo::SIGNER_TYPE_KEY
@ SIGNER_TYPE_KEY
Signer is a key, use its default certificate.
Definition: signing-info.hpp:56
ndn::security::SigningInfo::setSignatureInfo
SigningInfo & setSignatureInfo(const SignatureInfo &signatureInfo)
Set a semi-prepared SignatureInfo;.
Definition: signing-info.cpp:194
stream-sink.hpp
ndn::security::SigningInfo::getSignerType
SignerType getSignerType() const
Definition: signing-info.hpp:161
ndn::OBufferStream
implements an output stream that constructs ndn::Buffer
Definition: buffer-stream.hpp:71
ndn::security::SigningInfo::getSignerName
const Name & getSignerName() const
Definition: signing-info.hpp:170
ndn::name::Component
Represents a name component.
Definition: name-component.hpp:94
ndn::security::pib::Key::getName
const Name & getName() const
Get key name.
Definition: key.cpp:38
ndn::security::pib::Key
A frontend handle of a key instance.
Definition: key.hpp:50
ndn::security::SigningInfo::getEmptySignatureInfo
static const SignatureInfo & getEmptySignatureInfo()
Definition: signing-info.cpp:41
ndn::security::transform::streamSink
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:53
ndn::security::SigningInfo::SIGNER_TYPE_CERT
@ SIGNER_TYPE_CERT
Signer is a certificate, use it directly.
Definition: signing-info.hpp:58
ndn::security::SigningInfo::setPibKey
SigningInfo & setPibKey(const Key &key)
Set signer as a PIB key handler key.
Definition: signing-info.cpp:185
ndn::security::SigningInfo::SIGNER_TYPE_HMAC
@ SIGNER_TYPE_HMAC
Signer is a HMAC key.
Definition: signing-info.hpp:62
ndn::security::SigningInfo::setSigningKeyName
SigningInfo & setSigningKeyName(const Name &keyName)
Set signer as a key with name keyName.
Definition: signing-info.cpp:132
ndn::Name::clear
void clear()
Remove all components.
Definition: name.cpp:280
ndn::security::pib::Identity
A frontend handle of an Identity.
Definition: identity.hpp:43
buffer-stream.hpp
ndn::OBufferStream::buf
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Definition: buffer-stream.cpp:54
ndn::security::SigningInfo::getDigestSha256Identity
static const Name & getDigestSha256Identity()
A localhost identity to indicate that the signature is generated using SHA-256.
Definition: signing-info.cpp:48
signing-info.hpp
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::security::SigningInfo::SIGNER_TYPE_NULL
@ SIGNER_TYPE_NULL
No signer is specified, use default setting or follow the trust schema.
Definition: signing-info.hpp:52
ndn::security::SigningInfo::SIGNER_TYPE_SHA256
@ SIGNER_TYPE_SHA256
Use a SHA-256 digest only, no signer needs to be specified.
Definition: signing-info.hpp:60