blob: 48b278a33c829a83df4eb746c565cb3c8e13b0ca [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{
56}
57
Spencer Lee308bc442015-11-24 02:59:55 -070058SigningInfo::SigningInfo(const std::string& signingStr)
59{
Spencer Lee48c291b2015-12-21 01:49:32 -070060 *this = SigningInfo();
61
Spencer Lee308bc442015-11-24 02:59:55 -070062 if (signingStr.empty()) {
Spencer Lee308bc442015-11-24 02:59:55 -070063 return;
64 }
65
66 size_t pos = signingStr.find(':');
67
68 if (pos == std::string::npos) {
69 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string cannot represent SigningInfo"));
70 }
71
72 std::string scheme = signingStr.substr(0, pos);
73 std::string nameArg = signingStr.substr(pos + 1);
74
75 if (scheme == "id") {
Yingdi Yufe4733a2015-10-22 14:24:12 -070076 if (nameArg == getDigestSha256Identity().toUri()) {
Spencer Lee308bc442015-11-24 02:59:55 -070077 setSha256Signing();
78 }
79 else {
80 setSigningIdentity(nameArg);
81 }
82 }
83 else if (scheme == "key") {
84 setSigningKeyName(nameArg);
85 }
86 else if (scheme == "cert") {
87 setSigningCertName(nameArg);
88 }
89 else {
90 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string scheme"));
91 }
92}
93
Alexander Afanasyevc95f5642017-01-04 17:34:26 -080094SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -070095SigningInfo::setSigningIdentity(const Name& identity)
96{
97 m_type = SIGNER_TYPE_ID;
98 m_name = identity;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -080099 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700100}
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800101
102SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700103SigningInfo::setSigningKeyName(const Name& keyName)
104{
105 m_type = SIGNER_TYPE_KEY;
106 m_name = keyName;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800107 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700108}
109
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800110SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700111SigningInfo::setSigningCertName(const Name& certificateName)
112{
113 m_type = SIGNER_TYPE_CERT;
114 m_name = certificateName;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800115 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700116}
117
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800118SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700119SigningInfo::setSha256Signing()
120{
121 m_type = SIGNER_TYPE_SHA256;
122 m_name.clear();
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800123 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700124}
125
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800126SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700127SigningInfo::setSignatureInfo(const SignatureInfo& signatureInfo)
128{
129 m_info = signatureInfo;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800130 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700131}
132
Spencer Lee308bc442015-11-24 02:59:55 -0700133std::ostream&
134operator<<(std::ostream& os, const SigningInfo& si)
135{
136 switch (si.getSignerType()) {
137 case SigningInfo::SIGNER_TYPE_NULL:
138 return os;
139 case SigningInfo::SIGNER_TYPE_ID:
140 os << "id:";
141 break;
142 case SigningInfo::SIGNER_TYPE_KEY:
143 os << "key:";
144 break;
145 case SigningInfo::SIGNER_TYPE_CERT:
146 os << "cert:";
147 break;
148 case SigningInfo::SIGNER_TYPE_SHA256:
Yingdi Yufe4733a2015-10-22 14:24:12 -0700149 os << "id:" << SigningInfo::getDigestSha256Identity();
Spencer Lee308bc442015-11-24 02:59:55 -0700150 return os;
151 default:
152 BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown signer type"));
153 }
154
155 os << si.getSignerName();
156 return os;
157}
158
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700159} // namespace security
Spencer Lee308bc442015-11-24 02:59:55 -0700160} // namespace ndn