blob: aaa277023e2d86ef8399f56dcb1688f0cf58268b [file] [log] [blame]
Yingdi Yu1b0311c2015-06-10 14:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
4 *
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,
74 const Name& signerName = EMPTY_NAME,
75 const SignatureInfo& signatureInfo = EMPTY_SIGNATURE_INFO);
76
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 */
96 void
97 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 */
103 void
104 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 */
110 void
111 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 */
117 void
118 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 */
141 void
142 setDigestAlgorithm(const DigestAlgorithm& algorithm)
143 {
144 m_digestAlgorithm = algorithm;
145 }
146
147 /**
148 * @return The digest algorithm for public key operations
149 */
150 DigestAlgorithm
151 getDigestAlgorithm() const
152 {
153 return m_digestAlgorithm;
154 }
155
156 /**
157 * @brief Set a semi-prepared SignatureInfo;
158 */
159 void
160 setSignatureInfo(const SignatureInfo& signatureInfo);
161
162 /**
163 * @return Semi-prepared SignatureInfo
164 */
165 const SignatureInfo&
166 getSignatureInfo() const
167 {
168 return m_info;
169 }
170
171public:
172 static const Name EMPTY_NAME;
173 static const SignatureInfo EMPTY_SIGNATURE_INFO;
174
175private:
176 SignerType m_type;
177 Name m_name;
178
179 DigestAlgorithm m_digestAlgorithm;
180
181 SignatureInfo m_info;
182};
183
Spencer Lee308bc442015-11-24 02:59:55 -0700184std::ostream&
185operator<<(std::ostream& os, const SigningInfo& si);
186
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700187} // namespace security
188} // namespace ndn
189
190#endif // NDN_SECURITY_SIGNING_INFO_HPP