blob: d02b0bde6c289faca7384d2cb121fea415f992d8 [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)
64 : m_type(SIGNER_TYPE_PIB_ID)
65 , m_identity(identity)
66 , m_digestAlgorithm(DigestAlgorithm::SHA256)
67{
68}
69
70SigningInfo::SigningInfo(const Key& key)
71 : m_type(SIGNER_TYPE_PIB_KEY)
72 , m_key(key)
73 , m_digestAlgorithm(DigestAlgorithm::SHA256)
74{
Yingdi Yu1b0311c2015-06-10 14:58:47 -070075}
76
Spencer Lee308bc442015-11-24 02:59:55 -070077SigningInfo::SigningInfo(const std::string& signingStr)
78{
Spencer Lee48c291b2015-12-21 01:49:32 -070079 *this = SigningInfo();
80
Spencer Lee308bc442015-11-24 02:59:55 -070081 if (signingStr.empty()) {
Spencer Lee308bc442015-11-24 02:59:55 -070082 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") {
Yingdi Yufe4733a2015-10-22 14:24:12 -070095 if (nameArg == getDigestSha256Identity().toUri()) {
Spencer Lee308bc442015-11-24 02:59:55 -070096 setSha256Signing();
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
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800113SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700114SigningInfo::setSigningIdentity(const Name& identity)
115{
116 m_type = SIGNER_TYPE_ID;
117 m_name = identity;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800118 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700119}
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800120
121SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700122SigningInfo::setSigningKeyName(const Name& keyName)
123{
124 m_type = SIGNER_TYPE_KEY;
125 m_name = keyName;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800126 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700127}
128
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800129SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700130SigningInfo::setSigningCertName(const Name& certificateName)
131{
132 m_type = SIGNER_TYPE_CERT;
133 m_name = certificateName;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800134 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700135}
136
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800137SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700138SigningInfo::setSha256Signing()
139{
140 m_type = SIGNER_TYPE_SHA256;
141 m_name.clear();
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800142 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700143}
144
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800145SigningInfo&
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800146SigningInfo::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
154SigningInfo&
155SigningInfo::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
163SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700164SigningInfo::setSignatureInfo(const SignatureInfo& signatureInfo)
165{
166 m_info = signatureInfo;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800167 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700168}
169
Spencer Lee308bc442015-11-24 02:59:55 -0700170std::ostream&
171operator<<(std::ostream& os, const SigningInfo& si)
172{
173 switch (si.getSignerType()) {
174 case SigningInfo::SIGNER_TYPE_NULL:
175 return os;
176 case SigningInfo::SIGNER_TYPE_ID:
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800177 return os << "id:" << si.getSignerName();
Spencer Lee308bc442015-11-24 02:59:55 -0700178 case SigningInfo::SIGNER_TYPE_KEY:
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800179 return os << "key:" << si.getSignerName();
Spencer Lee308bc442015-11-24 02:59:55 -0700180 case SigningInfo::SIGNER_TYPE_CERT:
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800181 return os << "cert:" << si.getSignerName();
Spencer Lee308bc442015-11-24 02:59:55 -0700182 case SigningInfo::SIGNER_TYPE_SHA256:
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800183 return os << "id:" << SigningInfo::getDigestSha256Identity();
184 case SigningInfo::SIGNER_TYPE_PIB_ID:
185 return os << "id:" << si.getPibIdentity().getName();
186 case SigningInfo::SIGNER_TYPE_PIB_KEY:
187 return os << "key:" << si.getPibKey().getName();
Spencer Lee308bc442015-11-24 02:59:55 -0700188 }
189
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800190 BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown signer type"));
Spencer Lee308bc442015-11-24 02:59:55 -0700191 return os;
192}
193
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700194} // namespace security
Spencer Lee308bc442015-11-24 02:59:55 -0700195} // namespace ndn