blob: ec17cda70ec5eb20f87a4975fd6926aee5724821 [file] [log] [blame]
Yingdi Yu1b0311c2015-06-10 14:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevc95f5642017-01-04 17:34:26 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu1b0311c2015-06-10 14:58:47 -07004 *
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#include "signing-info.hpp"
23
24namespace ndn {
25namespace security {
26
Yingdi Yufe4733a2015-10-22 14:24:12 -070027const Name&
28SigningInfo::getEmptyName()
29{
30 static Name emptyName;
31 return emptyName;
32}
33
34const SignatureInfo&
35SigningInfo::getEmptySignatureInfo()
36{
37 static SignatureInfo emptySignatureInfo;
38 return emptySignatureInfo;
39}
40
41const Name&
42SigningInfo::getDigestSha256Identity()
43{
44 static Name digestSha256Identity("/localhost/identity/digest-sha256");
45 return digestSha256Identity;
46}
Yingdi Yu1b0311c2015-06-10 14:58:47 -070047
48SigningInfo::SigningInfo(SignerType signerType,
49 const Name& signerName,
50 const SignatureInfo& signatureInfo)
51 : m_type(signerType)
52 , m_name(signerName)
Yingdi Yu99b2a002015-08-12 12:47:44 -070053 , m_digestAlgorithm(DigestAlgorithm::SHA256)
Yingdi Yu1b0311c2015-06-10 14:58:47 -070054 , m_info(signatureInfo)
55{
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -080056 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
63SigningInfo::SigningInfo(const Identity& identity)
Junxiao Shife1239a2017-01-27 20:36:12 +000064 : SigningInfo(SIGNER_TYPE_NULL)
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -080065{
Junxiao Shife1239a2017-01-27 20:36:12 +000066 this->setPibIdentity(identity);
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -080067}
68
69SigningInfo::SigningInfo(const Key& key)
Junxiao Shife1239a2017-01-27 20:36:12 +000070 : SigningInfo(SIGNER_TYPE_NULL)
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -080071{
Junxiao Shife1239a2017-01-27 20:36:12 +000072 this->setPibKey(key);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070073}
74
Spencer Lee308bc442015-11-24 02:59:55 -070075SigningInfo::SigningInfo(const std::string& signingStr)
Junxiao Shife1239a2017-01-27 20:36:12 +000076 : SigningInfo(SIGNER_TYPE_NULL)
Spencer Lee308bc442015-11-24 02:59:55 -070077{
78 if (signingStr.empty()) {
Spencer Lee308bc442015-11-24 02:59:55 -070079 return;
80 }
81
82 size_t pos = signingStr.find(':');
Spencer Lee308bc442015-11-24 02:59:55 -070083 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") {
Yingdi Yufe4733a2015-10-22 14:24:12 -070091 if (nameArg == getDigestSha256Identity().toUri()) {
Spencer Lee308bc442015-11-24 02:59:55 -070092 setSha256Signing();
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
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800109SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700110SigningInfo::setSigningIdentity(const Name& identity)
111{
112 m_type = SIGNER_TYPE_ID;
113 m_name = identity;
Junxiao Shife1239a2017-01-27 20:36:12 +0000114 m_identity = Identity();
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800115 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700116}
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800117
118SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700119SigningInfo::setSigningKeyName(const Name& keyName)
120{
121 m_type = SIGNER_TYPE_KEY;
122 m_name = keyName;
Junxiao Shife1239a2017-01-27 20:36:12 +0000123 m_key = Key();
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800124 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700125}
126
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800127SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700128SigningInfo::setSigningCertName(const Name& certificateName)
129{
130 m_type = SIGNER_TYPE_CERT;
131 m_name = certificateName;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800132 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700133}
134
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800135SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700136SigningInfo::setSha256Signing()
137{
138 m_type = SIGNER_TYPE_SHA256;
139 m_name.clear();
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800140 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700141}
142
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800143SigningInfo&
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800144SigningInfo::setPibIdentity(const Identity& identity)
145{
Junxiao Shife1239a2017-01-27 20:36:12 +0000146 m_type = SIGNER_TYPE_ID;
147 m_name = identity ? identity.getName() : Name();
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800148 m_identity = identity;
149 return *this;
150}
151
152SigningInfo&
153SigningInfo::setPibKey(const Key& key)
154{
Junxiao Shife1239a2017-01-27 20:36:12 +0000155 m_type = SIGNER_TYPE_KEY;
156 m_name = key ? key.getName() : Name();
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800157 m_key = key;
158 return *this;
159}
160
161SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700162SigningInfo::setSignatureInfo(const SignatureInfo& signatureInfo)
163{
164 m_info = signatureInfo;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800165 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700166}
167
Spencer Lee308bc442015-11-24 02:59:55 -0700168std::ostream&
169operator<<(std::ostream& os, const SigningInfo& si)
170{
171 switch (si.getSignerType()) {
172 case SigningInfo::SIGNER_TYPE_NULL:
173 return os;
174 case SigningInfo::SIGNER_TYPE_ID:
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800175 return os << "id:" << si.getSignerName();
Spencer Lee308bc442015-11-24 02:59:55 -0700176 case SigningInfo::SIGNER_TYPE_KEY:
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800177 return os << "key:" << si.getSignerName();
Spencer Lee308bc442015-11-24 02:59:55 -0700178 case SigningInfo::SIGNER_TYPE_CERT:
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800179 return os << "cert:" << si.getSignerName();
Spencer Lee308bc442015-11-24 02:59:55 -0700180 case SigningInfo::SIGNER_TYPE_SHA256:
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800181 return os << "id:" << SigningInfo::getDigestSha256Identity();
Spencer Lee308bc442015-11-24 02:59:55 -0700182 }
183
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800184 BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown signer type"));
Spencer Lee308bc442015-11-24 02:59:55 -0700185 return os;
186}
187
Nick Gordon2a6d45a2017-01-25 14:10:44 -0600188bool
189SigningInfo::operator==(const SigningInfo& rhs) const
190{
191 return getSignerType() == rhs.getSignerType() &&
192 getSignerName() == rhs.getSignerName() &&
193 getDigestAlgorithm() == rhs.getDigestAlgorithm() &&
194 getSignatureInfo() == rhs.getSignatureInfo();
195}
196
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700197} // namespace security
Spencer Lee308bc442015-11-24 02:59:55 -0700198} // namespace ndn