blob: 499f25bfd982fa4dc7e01de5a65fa42a9bab3998 [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#ifndef NDN_SECURITY_SIGNING_INFO_HPP
23#define NDN_SECURITY_SIGNING_INFO_HPP
24
25#include "../name.hpp"
26#include "../signature-info.hpp"
27#include "security-common.hpp"
28
29
30namespace ndn {
31namespace security {
32
33/**
34 * @brief Signing parameters passed to KeyChain
35 */
36class SigningInfo
37{
38public:
39 class Error : public std::runtime_error
40 {
41 public:
42 explicit
43 Error(const std::string& what)
44 : std::runtime_error(what)
45 {
46 }
47 };
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
59 SIGNER_TYPE_SHA256 = 4
60 };
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 /**
Spencer Lee308bc442015-11-24 02:59:55 -070078 * @brief Construct SigningInfo from its string representation
79 *
80 * @param signingStr The representative signing string for SigningInfo signing method
81 *
82 * Structure of the representative string is as follows:
83 * - default signing: "" (empty string)
84 * - signing with a default certificate of a default key for the identity: `id:/my-identity`
85 * - signing with a default certificate of the key: `key:/my-identity/ksk-1`
86 * - signing with the certificate: `cert:/my-identity/KEY/ksk-1/ID-CERT/%FD%01`
87 * - signing with sha256 digest: `id:/localhost/identity/digest-sha256`
88 */
89 explicit
90 SigningInfo(const std::string& signingStr);
91
92 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -070093 * @brief Set signer as an identity with name @p identity
94 * @post Change the signerType to SIGNER_TYPE_ID
95 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -080096 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -070097 setSigningIdentity(const Name& identity);
98
99 /**
100 * @brief Set signer as a key with name @p keyName
101 * @post Change the signerType to SIGNER_TYPE_KEY
102 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800103 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700104 setSigningKeyName(const Name& keyName);
105
106 /**
107 * @brief Set signer as a certificate with name @p certificateName
108 * @post Change the signerType to SIGNER_TYPE_CERT
109 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800110 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700111 setSigningCertName(const Name& certificateName);
112
113 /**
114 * @brief Set Sha256 as the signing method
115 * @post Reset signerName, also change the signerType to SIGNER_TYPE_SHA256
116 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800117 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700118 setSha256Signing();
119
120 /**
121 * @return Type of the signer
122 */
123 SignerType
124 getSignerType() const
125 {
126 return m_type;
127 }
128
129 /**
130 * @return Name of signer; interpretation differs per signerType
131 */
132 const Name&
133 getSignerName() const
134 {
135 return m_name;
136 }
137
138 /**
139 * @brief Set the digest algorithm for public key operations
140 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800141 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700142 setDigestAlgorithm(const DigestAlgorithm& algorithm)
143 {
144 m_digestAlgorithm = algorithm;
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800145 return *this;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700146 }
147
148 /**
149 * @return The digest algorithm for public key operations
150 */
151 DigestAlgorithm
152 getDigestAlgorithm() const
153 {
154 return m_digestAlgorithm;
155 }
156
157 /**
158 * @brief Set a semi-prepared SignatureInfo;
159 */
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800160 SigningInfo&
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700161 setSignatureInfo(const SignatureInfo& signatureInfo);
162
163 /**
164 * @return Semi-prepared SignatureInfo
165 */
166 const SignatureInfo&
167 getSignatureInfo() const
168 {
169 return m_info;
170 }
171
172public:
Yingdi Yufe4733a2015-10-22 14:24:12 -0700173 static const Name&
174 getEmptyName();
175
176 static const SignatureInfo&
177 getEmptySignatureInfo();
178
179 /**
180 * @brief A localhost identity to indicate that the signature is generated using SHA-256.
181 */
182 static const Name&
183 getDigestSha256Identity();
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700184
185private:
186 SignerType m_type;
187 Name m_name;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700188 DigestAlgorithm m_digestAlgorithm;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700189 SignatureInfo m_info;
190};
191
Spencer Lee308bc442015-11-24 02:59:55 -0700192std::ostream&
193operator<<(std::ostream& os, const SigningInfo& si);
194
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700195} // namespace security
196} // namespace ndn
197
198#endif // NDN_SECURITY_SIGNING_INFO_HPP