blob: 4856a493089f14db271116117968f919cf427816 [file] [log] [blame]
Yingdi Yu1b0311c2015-06-10 14:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi68b53852018-07-25 13:56:38 -06002/*
3 * Copyright (c) 2013-2018 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#ifndef NDN_SECURITY_SIGNING_INFO_HPP
23#define NDN_SECURITY_SIGNING_INFO_HPP
24
25#include "../name.hpp"
26#include "../signature-info.hpp"
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -080027#include "pib/identity.hpp"
28#include "pib/key.hpp"
Yingdi Yu1b0311c2015-06-10 14:58:47 -070029#include "security-common.hpp"
30
Yingdi Yu1b0311c2015-06-10 14:58:47 -070031namespace ndn {
32namespace security {
33
34/**
35 * @brief Signing parameters passed to KeyChain
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -080036 *
37 * A SigningInfo is invalid if the specified identity/key/certificate does not exist,
38 * or the PIB Identity or Key instance is not valid.
Yingdi Yu1b0311c2015-06-10 14:58:47 -070039 */
40class SigningInfo
41{
42public:
43 class Error : public std::runtime_error
44 {
45 public:
Junxiao Shi68b53852018-07-25 13:56:38 -060046 using std::runtime_error::runtime_error;
Yingdi Yu1b0311c2015-06-10 14:58:47 -070047 };
48
49 enum SignerType {
50 /// @brief no signer is specified, use default setting or follow the trust schema
51 SIGNER_TYPE_NULL = 0,
52 /// @brief signer is an identity, use its default key and default certificate
53 SIGNER_TYPE_ID = 1,
54 /// @brief signer is a key, use its default certificate
55 SIGNER_TYPE_KEY = 2,
56 /// @brief signer is a certificate, use it directly
57 SIGNER_TYPE_CERT = 3,
58 /// @brief use sha256 digest, no signer needs to be specified
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -080059 SIGNER_TYPE_SHA256 = 4,
Yingdi Yu1b0311c2015-06-10 14:58:47 -070060 };
61
62public:
63 /**
64 * @brief Constructor
65 *
66 * @param signerType The type of signer
67 * @param signerName The name of signer; interpretation differs per signerType
68 * @param signatureInfo A semi-prepared SignatureInfo which contains other information except
69 * SignatureType and KeyLocator. If SignatureType and KeyLocator are
70 * specified, they may be overwritten by KeyChain.
71 */
72 explicit
73 SigningInfo(SignerType signerType = SIGNER_TYPE_NULL,
Yingdi Yufe4733a2015-10-22 14:24:12 -070074 const Name& signerName = getEmptyName(),
75 const SignatureInfo& signatureInfo = getEmptySignatureInfo());
Yingdi Yu1b0311c2015-06-10 14:58:47 -070076
77 /**
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -080078 * @brief Create a signingInfo using pib identity;
79 */
80 explicit
81 SigningInfo(const Identity& identity);
82
83 /**
84 * @brief Create a signingInfo using pib key;
85 */
86 explicit
87 SigningInfo(const Key& key);
88
89 /**
Spencer Lee308bc442015-11-24 02:59:55 -070090 * @brief Construct SigningInfo from its string representation
91 *
92 * @param signingStr The representative signing string for SigningInfo signing method
93 *
94 * Structure of the representative string is as follows:
95 * - default signing: "" (empty string)
96 * - signing with a default certificate of a default key for the identity: `id:/my-identity`
97 * - signing with a default certificate of the key: `key:/my-identity/ksk-1`
98 * - signing with the certificate: `cert:/my-identity/KEY/ksk-1/ID-CERT/%FD%01`
99 * - signing with sha256 digest: `id:/localhost/identity/digest-sha256`
100 */
101 explicit
102 SigningInfo(const std::string& signingStr);
103
104 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700105 * @brief Set signer as an identity with name @p identity
106 * @post Change the signerType to SIGNER_TYPE_ID
107 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800108 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700109 setSigningIdentity(const Name& identity);
110
111 /**
112 * @brief Set signer as a key with name @p keyName
113 * @post Change the signerType to SIGNER_TYPE_KEY
114 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800115 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700116 setSigningKeyName(const Name& keyName);
117
118 /**
119 * @brief Set signer as a certificate with name @p certificateName
120 * @post Change the signerType to SIGNER_TYPE_CERT
121 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800122 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700123 setSigningCertName(const Name& certificateName);
124
125 /**
126 * @brief Set Sha256 as the signing method
127 * @post Reset signerName, also change the signerType to SIGNER_TYPE_SHA256
128 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800129 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700130 setSha256Signing();
131
132 /**
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800133 * @brief Set signer as a PIB identity handler @p identity
Junxiao Shife1239a2017-01-27 20:36:12 +0000134 * @post Change the signerType to SIGNER_TYPE_ID
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800135 */
136 SigningInfo&
137 setPibIdentity(const Identity& identity);
138
139 /**
140 * @brief Set signer as a PIB key handler @p key
Junxiao Shife1239a2017-01-27 20:36:12 +0000141 * @post Change the signerType to SIGNER_TYPE_KEY
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800142 */
143 SigningInfo&
144 setPibKey(const Key& key);
145
146 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700147 * @return Type of the signer
148 */
149 SignerType
150 getSignerType() const
151 {
152 return m_type;
153 }
154
155 /**
156 * @return Name of signer; interpretation differs per signerType
157 */
158 const Name&
159 getSignerName() const
160 {
161 return m_name;
162 }
163
164 /**
Junxiao Shife1239a2017-01-27 20:36:12 +0000165 * @pre signerType must be SIGNER_TYPE_ID
166 * @return the identity handler of signer, or Identity() if getSignerName() should be used
167 * to find the identity
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800168 */
169 const Identity&
170 getPibIdentity() const
171 {
Junxiao Shife1239a2017-01-27 20:36:12 +0000172 BOOST_ASSERT(m_type == SIGNER_TYPE_ID);
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800173 return m_identity;
174 }
175
176 /**
Junxiao Shife1239a2017-01-27 20:36:12 +0000177 * @pre signerType must be SIGNER_TYPE_KEY
178 * @return the key handler of signer, or Key() if getSignerName() should be used to find the key
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800179 */
180 const Key&
181 getPibKey() const
182 {
Junxiao Shife1239a2017-01-27 20:36:12 +0000183 BOOST_ASSERT(m_type == SIGNER_TYPE_KEY);
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800184 return m_key;
185 }
186
187 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700188 * @brief Set the digest algorithm for public key operations
189 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800190 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700191 setDigestAlgorithm(const DigestAlgorithm& algorithm)
192 {
193 m_digestAlgorithm = algorithm;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800194 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700195 }
196
197 /**
198 * @return The digest algorithm for public key operations
199 */
200 DigestAlgorithm
201 getDigestAlgorithm() const
202 {
203 return m_digestAlgorithm;
204 }
205
206 /**
207 * @brief Set a semi-prepared SignatureInfo;
208 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800209 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700210 setSignatureInfo(const SignatureInfo& signatureInfo);
211
212 /**
213 * @return Semi-prepared SignatureInfo
214 */
215 const SignatureInfo&
216 getSignatureInfo() const
217 {
218 return m_info;
219 }
220
221public:
Yingdi Yufe4733a2015-10-22 14:24:12 -0700222 static const Name&
223 getEmptyName();
224
225 static const SignatureInfo&
226 getEmptySignatureInfo();
227
228 /**
229 * @brief A localhost identity to indicate that the signature is generated using SHA-256.
230 */
231 static const Name&
232 getDigestSha256Identity();
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700233
Nick Gordon2a6d45a2017-01-25 14:10:44 -0600234 bool
235 operator==(const SigningInfo& rhs) const;
236
237 bool
238 operator!=(const SigningInfo& rhs) const
239 {
240 return !(*this == rhs);
241 }
242
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700243private:
244 SignerType m_type;
245 Name m_name;
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800246 Identity m_identity;
247 Key m_key;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700248 DigestAlgorithm m_digestAlgorithm;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700249 SignatureInfo m_info;
250};
251
Spencer Lee308bc442015-11-24 02:59:55 -0700252std::ostream&
253operator<<(std::ostream& os, const SigningInfo& si);
254
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700255} // namespace security
256} // namespace ndn
257
258#endif // NDN_SECURITY_SIGNING_INFO_HPP