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 
63 SigningInfo::SigningInfo(const Identity& identity)
64  : m_type(SIGNER_TYPE_PIB_ID)
65  , m_identity(identity)
66  , m_digestAlgorithm(DigestAlgorithm::SHA256)
67 {
68 }
69 
70 SigningInfo::SigningInfo(const Key& key)
71  : m_type(SIGNER_TYPE_PIB_KEY)
72  , m_key(key)
73  , m_digestAlgorithm(DigestAlgorithm::SHA256)
74 {
75 }
76 
77 SigningInfo::SigningInfo(const std::string& signingStr)
78 {
79  *this = SigningInfo();
80 
81  if (signingStr.empty()) {
82  return;
83  }
84 
85  size_t pos = signingStr.find(':');
86 
87  if (pos == std::string::npos) {
88  BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string cannot represent SigningInfo"));
89  }
90 
91  std::string scheme = signingStr.substr(0, pos);
92  std::string nameArg = signingStr.substr(pos + 1);
93 
94  if (scheme == "id") {
95  if (nameArg == getDigestSha256Identity().toUri()) {
97  }
98  else {
99  setSigningIdentity(nameArg);
100  }
101  }
102  else if (scheme == "key") {
103  setSigningKeyName(nameArg);
104  }
105  else if (scheme == "cert") {
106  setSigningCertName(nameArg);
107  }
108  else {
109  BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string scheme"));
110  }
111 }
112 
115 {
116  m_type = SIGNER_TYPE_ID;
117  m_name = identity;
118  return *this;
119 }
120 
123 {
124  m_type = SIGNER_TYPE_KEY;
125  m_name = keyName;
126  return *this;
127 }
128 
130 SigningInfo::setSigningCertName(const Name& certificateName)
131 {
132  m_type = SIGNER_TYPE_CERT;
133  m_name = certificateName;
134  return *this;
135 }
136 
139 {
140  m_type = SIGNER_TYPE_SHA256;
141  m_name.clear();
142  return *this;
143 }
144 
146 SigningInfo::setPibIdentity(const Identity& identity)
147 {
148  m_type = SIGNER_TYPE_PIB_ID;
149  m_name.clear();
150  m_identity = identity;
151  return *this;
152 }
153 
155 SigningInfo::setPibKey(const Key& key)
156 {
157  m_type = SIGNER_TYPE_PIB_KEY;
158  m_name.clear();
159  m_key = key;
160  return *this;
161 }
162 
165 {
166  m_info = signatureInfo;
167  return *this;
168 }
169 
170 std::ostream&
171 operator<<(std::ostream& os, const SigningInfo& si)
172 {
173  switch (si.getSignerType()) {
175  return os;
177  return os << "id:" << si.getSignerName();
179  return os << "key:" << si.getSignerName();
181  return os << "cert:" << si.getSignerName();
183  return os << "id:" << SigningInfo::getDigestSha256Identity();
185  return os << "id:" << si.getPibIdentity().getName();
187  return os << "key:" << si.getPibKey().getName();
188  }
189 
190  BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown signer type"));
191  return os;
192 }
193 
194 } // namespace security
195 } // namespace ndn
SigningInfo & setPibIdentity(const Identity &identity)
Set signer as a PIB identity handler identity.
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
given PIB identity handle, use its default key and default certificate
const Name & getSignerName() const
SigningInfo(SignerType signerType=SIGNER_TYPE_NULL, const Name &signerName=getEmptyName(), const SignatureInfo &signatureInfo=getEmptySignatureInfo())
Constructor.
use sha256 digest, no signer needs to be specified
Signing parameters passed to KeyChain.
std::ostream & operator<<(std::ostream &os, CommandInterestValidator::ErrorCode error)
const Identity & getPibIdentity() const
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.
Name abstraction to represent an absolute name.
Definition: name.hpp:46
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;.
given PIB key handle, use its default certificate
static const Name & getEmptyName()
SigningInfo & setSigningIdentity(const Name &identity)
Set signer as an identity with name identity.
signer is an identity, use its default key and default certificate
const Key & getPibKey() const
SigningInfo & setPibKey(const Key &key)
Set signer as a PIB key handler key.
void clear()
Clear all the components.
Definition: name.hpp:210
SignerType getSignerType() const
SigningInfo & setSigningKeyName(const Name &keyName)
Set signer as a key with name keyName.