blob: f8a78925f700811aacbd199caecf9f6fa2797bc8 [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"
Spencer Lee308bc442015-11-24 02:59:55 -070023#include "key-chain.hpp"
Yingdi Yu1b0311c2015-06-10 14:58:47 -070024
25namespace ndn {
26namespace security {
27
28const Name SigningInfo::EMPTY_NAME;
29const SignatureInfo SigningInfo::EMPTY_SIGNATURE_INFO;
30
31SigningInfo::SigningInfo(SignerType signerType,
32 const Name& signerName,
33 const SignatureInfo& signatureInfo)
34 : m_type(signerType)
35 , m_name(signerName)
Yingdi Yu99b2a002015-08-12 12:47:44 -070036 , m_digestAlgorithm(DigestAlgorithm::SHA256)
Yingdi Yu1b0311c2015-06-10 14:58:47 -070037 , m_info(signatureInfo)
38{
39}
40
Spencer Lee308bc442015-11-24 02:59:55 -070041SigningInfo::SigningInfo(const std::string& signingStr)
42{
Spencer Lee48c291b2015-12-21 01:49:32 -070043 *this = SigningInfo();
44
Spencer Lee308bc442015-11-24 02:59:55 -070045 if (signingStr.empty()) {
Spencer Lee308bc442015-11-24 02:59:55 -070046 return;
47 }
48
49 size_t pos = signingStr.find(':');
50
51 if (pos == std::string::npos) {
52 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string cannot represent SigningInfo"));
53 }
54
55 std::string scheme = signingStr.substr(0, pos);
56 std::string nameArg = signingStr.substr(pos + 1);
57
58 if (scheme == "id") {
59 if (nameArg == KeyChain::DIGEST_SHA256_IDENTITY.toUri()) {
60 setSha256Signing();
61 }
62 else {
63 setSigningIdentity(nameArg);
64 }
65 }
66 else if (scheme == "key") {
67 setSigningKeyName(nameArg);
68 }
69 else if (scheme == "cert") {
70 setSigningCertName(nameArg);
71 }
72 else {
73 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string scheme"));
74 }
75}
76
Alexander Afanasyevc95f5642017-01-04 17:34:26 -080077SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -070078SigningInfo::setSigningIdentity(const Name& identity)
79{
80 m_type = SIGNER_TYPE_ID;
81 m_name = identity;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -080082 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -070083}
Alexander Afanasyevc95f5642017-01-04 17:34:26 -080084
85SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -070086SigningInfo::setSigningKeyName(const Name& keyName)
87{
88 m_type = SIGNER_TYPE_KEY;
89 m_name = keyName;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -080090 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -070091}
92
Alexander Afanasyevc95f5642017-01-04 17:34:26 -080093SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -070094SigningInfo::setSigningCertName(const Name& certificateName)
95{
96 m_type = SIGNER_TYPE_CERT;
97 m_name = certificateName;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -080098 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -070099}
100
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800101SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700102SigningInfo::setSha256Signing()
103{
104 m_type = SIGNER_TYPE_SHA256;
105 m_name.clear();
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800106 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700107}
108
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800109SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700110SigningInfo::setSignatureInfo(const SignatureInfo& signatureInfo)
111{
112 m_info = signatureInfo;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800113 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700114}
115
Spencer Lee308bc442015-11-24 02:59:55 -0700116std::ostream&
117operator<<(std::ostream& os, const SigningInfo& si)
118{
119 switch (si.getSignerType()) {
120 case SigningInfo::SIGNER_TYPE_NULL:
121 return os;
122 case SigningInfo::SIGNER_TYPE_ID:
123 os << "id:";
124 break;
125 case SigningInfo::SIGNER_TYPE_KEY:
126 os << "key:";
127 break;
128 case SigningInfo::SIGNER_TYPE_CERT:
129 os << "cert:";
130 break;
131 case SigningInfo::SIGNER_TYPE_SHA256:
132 os << "id:" << KeyChain::DIGEST_SHA256_IDENTITY;
133 return os;
134 default:
135 BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown signer type"));
136 }
137
138 os << si.getSignerName();
139 return os;
140}
141
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700142} // namespace security
Spencer Lee308bc442015-11-24 02:59:55 -0700143} // namespace ndn