blob: 57cf046519a89e7de9cfb8f3f0f282bce522782b [file] [log] [blame]
Yingdi Yu1b0311c2015-06-10 14:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu99b2a002015-08-12 12:47:44 -07003 * Copyright (c) 2013-2016 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
Yingdi Yu1b0311c2015-06-10 14:58:47 -070077void
78SigningInfo::setSigningIdentity(const Name& identity)
79{
80 m_type = SIGNER_TYPE_ID;
81 m_name = identity;
82}
83void
84SigningInfo::setSigningKeyName(const Name& keyName)
85{
86 m_type = SIGNER_TYPE_KEY;
87 m_name = keyName;
88}
89
90void
91SigningInfo::setSigningCertName(const Name& certificateName)
92{
93 m_type = SIGNER_TYPE_CERT;
94 m_name = certificateName;
95}
96
97void
98SigningInfo::setSha256Signing()
99{
100 m_type = SIGNER_TYPE_SHA256;
101 m_name.clear();
102}
103
104void
105SigningInfo::setSignatureInfo(const SignatureInfo& signatureInfo)
106{
107 m_info = signatureInfo;
108}
109
Spencer Lee308bc442015-11-24 02:59:55 -0700110std::ostream&
111operator<<(std::ostream& os, const SigningInfo& si)
112{
113 switch (si.getSignerType()) {
114 case SigningInfo::SIGNER_TYPE_NULL:
115 return os;
116 case SigningInfo::SIGNER_TYPE_ID:
117 os << "id:";
118 break;
119 case SigningInfo::SIGNER_TYPE_KEY:
120 os << "key:";
121 break;
122 case SigningInfo::SIGNER_TYPE_CERT:
123 os << "cert:";
124 break;
125 case SigningInfo::SIGNER_TYPE_SHA256:
126 os << "id:" << KeyChain::DIGEST_SHA256_IDENTITY;
127 return os;
128 default:
129 BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown signer type"));
130 }
131
132 os << si.getSignerName();
133 return os;
134}
135
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700136} // namespace security
Spencer Lee308bc442015-11-24 02:59:55 -0700137} // namespace ndn