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 
74 {
75  this->setPibIdentity(identity);
76 }
77 
80 {
81  this->setPibKey(key);
82 }
83 
84 SigningInfo::SigningInfo(const std::string& signingStr)
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
SigningInfo & setPibIdentity(const Identity &identity)
Set signer as a PIB identity handler identity.
Definition: data.cpp:26
Represents a SignatureInfo TLV element.
unique_ptr< Transform > base64Decode(bool expectNewlineEvery64Bytes)
SigningInfo(SignerType signerType=SIGNER_TYPE_NULL, const Name &signerName=getEmptyName(), const SignatureInfo &signatureInfo=getEmptySignatureInfo())
Constructor.
SigningInfo & setDigestAlgorithm(const DigestAlgorithm &algorithm)
Set the digest algorithm for signing operations.
Use a SHA-256 digest only, no signer needs to be specified.
Name & append(const Component &component)
Append a component.
Definition: name.hpp:277
Signing parameters passed to KeyChain.
#define NDN_THROW(e)
Definition: exception.hpp:61
HMAC key, supports sign/verify operations.
const Name & getSignerName() const
A frontend handle of a key instance.
Definition: key.hpp:49
SigningInfo & setSha256Signing()
Set SHA-256 as the signing method.
No signer is specified, use default setting or follow the trust schema.
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:53
static const Name & getDigestSha256Identity()
A localhost identity to indicate that the signature is generated using SHA-256.
Use the SHA256 hash of the public key as key id.
Represents an absolute name.
Definition: name.hpp:43
const Name & getName() const
Get key name.
Definition: key.cpp:38
Signer is a certificate, use it directly.
static const SignatureInfo & getEmptySignatureInfo()
SigningInfo & setSigningCertName(const Name &certificateName)
Set signer as a certificate with name certificateName.
Signer is a key, use its default certificate.
SigningInfo & setSignatureInfo(const SignatureInfo &signatureInfo)
Set a semi-prepared SignatureInfo;.
Represents a name component.
static const Name & getEmptyName()
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
const Name & getName() const
Get the name of the identity.
Definition: identity.cpp:37
SigningInfo & setSigningIdentity(const Name &identity)
Set signer as an identity with name identity.
Signer is an identity, use its default key and default certificate.
A frontend handle of an Identity.
Definition: identity.hpp:42
implements an output stream that constructs ndn::Buffer
SigningInfo & setPibKey(const Key &key)
Set signer as a PIB key handler key.
void clear()
Remove all components.
Definition: name.cpp:280
std::ostream & operator<<(std::ostream &os, const SigningInfo &si)
static const Name & getHmacIdentity()
A localhost identity to indicate that the signature is generated using an HMAC key.
SigningInfo & setSigningHmacKey(const std::string &hmacKey)
Set signer to a base64-encoded HMAC key.
SigningInfo & setSigningKeyName(const Name &keyName)
Set signer as a key with name keyName.
SignerType getSignerType() const