rippled
Loading...
Searching...
No Matches
Condition.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2016 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/conditions/Condition.h>
21#include <xrpld/conditions/detail/PreimageSha256.h>
22#include <xrpld/conditions/detail/utils.h>
23
24namespace ripple {
25namespace cryptoconditions {
26
27namespace detail {
28// The binary encoding of conditions differs based on their
29// type. All types define at least a fingerprint and cost
30// sub-field. Some types, such as the compound condition
31// types, define additional sub-fields that are required to
32// convey essential properties of the cryptocondition (such
33// as the sub-types used by sub-conditions in the case of
34// the compound types).
35//
36// Conditions are encoded as follows:
37//
38// Condition ::= CHOICE {
39// preimageSha256 [0] SimpleSha256Condition,
40// prefixSha256 [1] CompoundSha256Condition,
41// thresholdSha256 [2] CompoundSha256Condition,
42// rsaSha256 [3] SimpleSha256Condition,
43// ed25519Sha256 [4] SimpleSha256Condition
44// }
45//
46// SimpleSha256Condition ::= SEQUENCE {
47// fingerprint OCTET STRING (SIZE(32)),
48// cost INTEGER (0..4294967295)
49// }
50//
51// CompoundSha256Condition ::= SEQUENCE {
52// fingerprint OCTET STRING (SIZE(32)),
53// cost INTEGER (0..4294967295),
54// subtypes ConditionTypes
55// }
56//
57// ConditionTypes ::= BIT STRING {
58// preImageSha256 (0),
59// prefixSha256 (1),
60// thresholdSha256 (2),
61// rsaSha256 (3),
62// ed25519Sha256 (4)
63// }
64
66
69{
70 using namespace der;
71
72 auto p = parsePreamble(s, ec);
73
74 if (ec)
75 return {};
76
77 if (!isPrimitive(p) || !isContextSpecific(p))
78 {
80 return {};
81 }
82
83 if (p.tag != 0)
84 {
86 return {};
87 }
88
89 if (p.length != fingerprintSize)
90 {
92 return {};
93 }
94
95 Buffer b = parseOctetString(s, p.length, ec);
96
97 if (ec)
98 return {};
99
100 p = parsePreamble(s, ec);
101
102 if (ec)
103 return {};
104
105 if (!isPrimitive(p) || !isContextSpecific(p))
106 {
108 return {};
109 }
110
111 if (p.tag != 1)
112 {
114 return {};
115 }
116
117 auto cost = parseInteger<std::uint32_t>(s, p.length, ec);
118
119 if (ec)
120 return {};
121
122 if (!s.empty())
123 {
125 return {};
126 }
127
128 switch (type)
129 {
132 {
134 return {};
135 }
136 break;
137
138 default:
139 break;
140 }
141
142 return std::make_unique<Condition>(type, cost, std::move(b));
143}
144
145} // namespace detail
146
149{
150 // Per the RFC, in a condition we choose a type based
151 // on the tag of the item we contain:
152 //
153 // Condition ::= CHOICE {
154 // preimageSha256 [0] SimpleSha256Condition,
155 // prefixSha256 [1] CompoundSha256Condition,
156 // thresholdSha256 [2] CompoundSha256Condition,
157 // rsaSha256 [3] SimpleSha256Condition,
158 // ed25519Sha256 [4] SimpleSha256Condition
159 // }
160 if (s.empty())
161 {
163 return {};
164 }
165
166 using namespace der;
167
168 auto const p = parsePreamble(s, ec);
169 if (ec)
170 return {};
171
172 // All fulfillments are context-specific, constructed
173 // types
174 if (!isConstructed(p) || !isContextSpecific(p))
175 {
177 return {};
178 }
179
180 if (p.length > s.size())
181 {
183 return {};
184 }
185
187 {
189 return {};
190 }
191
193
194 switch (p.tag)
195 {
196 case 0: // PreimageSha256
198 Type::preimageSha256, Slice(s.data(), p.length), ec);
199 if (!ec)
200 s += p.length;
201 break;
202
203 case 1: // PrefixSha256
205 return {};
206
207 case 2: // ThresholdSha256
209 return {};
210
211 case 3: // RsaSha256
213 return {};
214
215 case 4: // Ed25519Sha256
217 return {};
218
219 default:
221 return {};
222 }
223
224 if (!s.empty())
225 {
227 return {};
228 }
229
230 return c;
231}
232
233} // namespace cryptoconditions
234} // namespace ripple
Like std::vector<char> but better.
Definition Buffer.h:36
An immutable linear range of bytes.
Definition Slice.h:46
bool empty() const noexcept
Return true if the byte range is empty.
Definition Slice.h:70
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:98
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:81
std::size_t length() const noexcept
Definition Slice.h:87
static std::unique_ptr< Condition > deserialize(Slice s, std::error_code &ec)
Load a condition from its binary form.
static constexpr std::size_t maxSerializedCondition
The largest binary condition we support.
Definition Condition.h:52
static constexpr std::size_t maxPreimageLength
The maximum allowed length of a preimage.
T is_same_v
constexpr std::size_t fingerprintSize
Definition Condition.cpp:65
std::unique_ptr< Condition > loadSimpleSha256(Type type, Slice s, std::error_code &ec)
Definition Condition.cpp:68
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25