rippled
Loading...
Searching...
No Matches
Condition.cpp
1#include <xrpld/conditions/Condition.h>
2#include <xrpld/conditions/detail/PreimageSha256.h>
3#include <xrpld/conditions/detail/utils.h>
4
5namespace ripple {
6namespace cryptoconditions {
7
8namespace detail {
9// The binary encoding of conditions differs based on their
10// type. All types define at least a fingerprint and cost
11// sub-field. Some types, such as the compound condition
12// types, define additional sub-fields that are required to
13// convey essential properties of the cryptocondition (such
14// as the sub-types used by sub-conditions in the case of
15// the compound types).
16//
17// Conditions are encoded as follows:
18//
19// Condition ::= CHOICE {
20// preimageSha256 [0] SimpleSha256Condition,
21// prefixSha256 [1] CompoundSha256Condition,
22// thresholdSha256 [2] CompoundSha256Condition,
23// rsaSha256 [3] SimpleSha256Condition,
24// ed25519Sha256 [4] SimpleSha256Condition
25// }
26//
27// SimpleSha256Condition ::= SEQUENCE {
28// fingerprint OCTET STRING (SIZE(32)),
29// cost INTEGER (0..4294967295)
30// }
31//
32// CompoundSha256Condition ::= SEQUENCE {
33// fingerprint OCTET STRING (SIZE(32)),
34// cost INTEGER (0..4294967295),
35// subtypes ConditionTypes
36// }
37//
38// ConditionTypes ::= BIT STRING {
39// preImageSha256 (0),
40// prefixSha256 (1),
41// thresholdSha256 (2),
42// rsaSha256 (3),
43// ed25519Sha256 (4)
44// }
45
47
50{
51 using namespace der;
52
53 auto p = parsePreamble(s, ec);
54
55 if (ec)
56 return {};
57
58 if (!isPrimitive(p) || !isContextSpecific(p))
59 {
61 return {};
62 }
63
64 if (p.tag != 0)
65 {
67 return {};
68 }
69
70 if (p.length != fingerprintSize)
71 {
73 return {};
74 }
75
76 Buffer b = parseOctetString(s, p.length, ec);
77
78 if (ec)
79 return {};
80
81 p = parsePreamble(s, ec);
82
83 if (ec)
84 return {};
85
86 if (!isPrimitive(p) || !isContextSpecific(p))
87 {
89 return {};
90 }
91
92 if (p.tag != 1)
93 {
95 return {};
96 }
97
98 auto cost = parseInteger<std::uint32_t>(s, p.length, ec);
99
100 if (ec)
101 return {};
102
103 if (!s.empty())
104 {
106 return {};
107 }
108
109 switch (type)
110 {
113 {
115 return {};
116 }
117 break;
118
119 default:
120 break;
121 }
122
123 return std::make_unique<Condition>(type, cost, std::move(b));
124}
125
126} // namespace detail
127
130{
131 // Per the RFC, in a condition we choose a type based
132 // on the tag of the item we contain:
133 //
134 // Condition ::= CHOICE {
135 // preimageSha256 [0] SimpleSha256Condition,
136 // prefixSha256 [1] CompoundSha256Condition,
137 // thresholdSha256 [2] CompoundSha256Condition,
138 // rsaSha256 [3] SimpleSha256Condition,
139 // ed25519Sha256 [4] SimpleSha256Condition
140 // }
141 if (s.empty())
142 {
144 return {};
145 }
146
147 using namespace der;
148
149 auto const p = parsePreamble(s, ec);
150 if (ec)
151 return {};
152
153 // All fulfillments are context-specific, constructed
154 // types
155 if (!isConstructed(p) || !isContextSpecific(p))
156 {
158 return {};
159 }
160
161 if (p.length > s.size())
162 {
164 return {};
165 }
166
168 {
170 return {};
171 }
172
174
175 switch (p.tag)
176 {
177 case 0: // PreimageSha256
179 Type::preimageSha256, Slice(s.data(), p.length), ec);
180 if (!ec)
181 s += p.length;
182 break;
183
184 case 1: // PrefixSha256
186 return {};
187
188 case 2: // ThresholdSha256
190 return {};
191
192 case 3: // RsaSha256
194 return {};
195
196 case 4: // Ed25519Sha256
198 return {};
199
200 default:
202 return {};
203 }
204
205 if (!s.empty())
206 {
208 return {};
209 }
210
211 return c;
212}
213
214} // namespace cryptoconditions
215} // namespace ripple
Like std::vector<char> but better.
Definition Buffer.h:17
An immutable linear range of bytes.
Definition Slice.h:27
bool empty() const noexcept
Return true if the byte range is empty.
Definition Slice.h:51
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:79
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:62
std::size_t length() const noexcept
Definition Slice.h:68
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:33
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:46
std::unique_ptr< Condition > loadSimpleSha256(Type type, Slice s, std::error_code &ec)
Definition Condition.cpp:49
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6