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; -*- */
22 #include "signing-info.hpp"
23 
24 namespace ndn {
25 namespace security {
26 
27 const Name&
29 {
30  static Name emptyName;
31  return emptyName;
32 }
33 
34 const SignatureInfo&
36 {
37  static SignatureInfo emptySignatureInfo;
38  return emptySignatureInfo;
39 }
40 
41 const Name&
43 {
44  static Name digestSha256Identity("/localhost/identity/digest-sha256");
45  return digestSha256Identity;
46 }
47 
49  const Name& signerName,
50  const SignatureInfo& signatureInfo)
51  : m_type(signerType)
52  , m_name(signerName)
53  , m_digestAlgorithm(DigestAlgorithm::SHA256)
54  , m_info(signatureInfo)
55 {
56  BOOST_ASSERT(signerType == SIGNER_TYPE_NULL ||
57  signerType == SIGNER_TYPE_ID ||
58  signerType == SIGNER_TYPE_KEY ||
59  signerType == SIGNER_TYPE_CERT ||
60  signerType == SIGNER_TYPE_SHA256);
61 }
62 
64  : SigningInfo(SIGNER_TYPE_NULL)
65 {
66  this->setPibIdentity(identity);
67 }
68 
70  : SigningInfo(SIGNER_TYPE_NULL)
71 {
72  this->setPibKey(key);
73 }
74 
75 SigningInfo::SigningInfo(const std::string& signingStr)
76  : SigningInfo(SIGNER_TYPE_NULL)
77 {
78  if (signingStr.empty()) {
79  return;
80  }
81 
82  size_t pos = signingStr.find(':');
83  if (pos == std::string::npos) {
84  BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string cannot represent SigningInfo"));
85  }
86 
87  std::string scheme = signingStr.substr(0, pos);
88  std::string nameArg = signingStr.substr(pos + 1);
89 
90  if (scheme == "id") {
91  if (nameArg == getDigestSha256Identity().toUri()) {
93  }
94  else {
95  setSigningIdentity(nameArg);
96  }
97  }
98  else if (scheme == "key") {
99  setSigningKeyName(nameArg);
100  }
101  else if (scheme == "cert") {
102  setSigningCertName(nameArg);
103  }
104  else {
105  BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string scheme"));
106  }
107 }
108 
111 {
112  m_type = SIGNER_TYPE_ID;
113  m_name = identity;
114  m_identity = Identity();
115  return *this;
116 }
117 
120 {
121  m_type = SIGNER_TYPE_KEY;
122  m_name = keyName;
123  m_key = Key();
124  return *this;
125 }
126 
128 SigningInfo::setSigningCertName(const Name& certificateName)
129 {
130  m_type = SIGNER_TYPE_CERT;
131  m_name = certificateName;
132  return *this;
133 }
134 
137 {
138  m_type = SIGNER_TYPE_SHA256;
139  m_name.clear();
140  return *this;
141 }
142 
145 {
146  m_type = SIGNER_TYPE_ID;
147  m_name = identity ? identity.getName() : Name();
148  m_identity = identity;
149  return *this;
150 }
151 
154 {
155  m_type = SIGNER_TYPE_KEY;
156  m_name = key ? key.getName() : Name();
157  m_key = key;
158  return *this;
159 }
160 
163 {
164  m_info = signatureInfo;
165  return *this;
166 }
167 
168 std::ostream&
169 operator<<(std::ostream& os, const SigningInfo& si)
170 {
171  switch (si.getSignerType()) {
173  return os;
175  return os << "id:" << si.getSignerName();
177  return os << "key:" << si.getSignerName();
179  return os << "cert:" << si.getSignerName();
181  return os << "id:" << SigningInfo::getDigestSha256Identity();
182  }
183 
184  BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown signer type"));
185  return os;
186 }
187 
188 bool
190 {
191  return getSignerType() == rhs.getSignerType() &&
192  getSignerName() == rhs.getSignerName() &&
195 }
196 
197 } // namespace security
198 } // namespace ndn
SigningInfo & setPibIdentity(const Identity &identity)
Set signer as a PIB identity handler identity.
Copyright (c) 2011-2015 Regents of the University of California.
Represents a SignatureInfo TLV element.
const SignatureInfo & getSignatureInfo() const
DigestAlgorithm getDigestAlgorithm() const
SigningInfo(SignerType signerType=SIGNER_TYPE_NULL, const Name &signerName=getEmptyName(), const SignatureInfo &signatureInfo=getEmptySignatureInfo())
Constructor.
bool operator==(const SigningInfo &rhs) const
use sha256 digest, no signer needs to be specified
Signing parameters passed to KeyChain.
const Name & getSignerName() const
A frontend handle of a key instance.
Definition: key.hpp:49
SigningInfo & setSha256Signing()
Set Sha256 as the signing method.
no signer is specified, use default setting or follow the trust schema
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 the key id.
Represents an absolute name.
Definition: name.hpp:42
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;.
static const Name & getEmptyName()
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
SigningInfo & setPibKey(const Key &key)
Set signer as a PIB key handler key.
void clear()
Remove all components.
Definition: name.hpp:470
std::ostream & operator<<(std::ostream &os, const SigningInfo &si)
SigningInfo & setSigningKeyName(const Name &keyName)
Set signer as a key with name keyName.
SignerType getSignerType() const