rippled
Loading...
Searching...
No Matches
Fulfillment.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/Fulfillment.h>
22#include <xrpld/conditions/detail/PreimageSha256.h>
23#include <xrpld/conditions/detail/utils.h>
24
25#include <xrpl/basics/safe_cast.h>
26
27namespace ripple {
28namespace cryptoconditions {
29
30bool
31match(Fulfillment const& f, Condition const& c)
32{
33 // Fast check: the fulfillment's type must match the
34 // conditions's type:
35 if (f.type() != c.type)
36 return false;
37
38 // Derive the condition from the given fulfillment
39 // and ensure that it matches the given condition.
40 return c == f.condition();
41}
42
43bool
44validate(Fulfillment const& f, Condition const& c, Slice m)
45{
46 return match(f, c) && f.validate(m);
47}
48
49bool
50validate(Fulfillment const& f, Condition const& c)
51{
52 return validate(f, c, {});
53}
54
57{
58 // Per the RFC, in a fulfillment we choose a type based
59 // on the tag of the item we contain:
60 //
61 // Fulfillment ::= CHOICE {
62 // preimageSha256 [0] PreimageFulfillment ,
63 // prefixSha256 [1] PrefixFulfillment,
64 // thresholdSha256 [2] ThresholdFulfillment,
65 // rsaSha256 [3] RsaSha256Fulfillment,
66 // ed25519Sha256 [4] Ed25519Sha512Fulfillment
67 // }
68
69 if (s.empty())
70 {
72 return nullptr;
73 }
74
75 using namespace der;
76
77 auto const p = parsePreamble(s, ec);
78 if (ec)
79 return nullptr;
80
81 // All fulfillments are context-specific, constructed types
82 if (!isConstructed(p) || !isContextSpecific(p))
83 {
85 return nullptr;
86 }
87
88 if (p.length > s.size())
89 {
91 return {};
92 }
93
94 if (p.length < s.size())
95 {
97 return {};
98 }
99
100 if (p.length > maxSerializedFulfillment)
101 {
103 return {};
104 }
105
107
108 using TagType = decltype(p.tag);
109 switch (p.tag)
110 {
111 case safe_cast<TagType>(Type::preimageSha256):
112 f = PreimageSha256::deserialize(Slice(s.data(), p.length), ec);
113 if (ec)
114 return {};
115 s += p.length;
116 break;
117
118 case safe_cast<TagType>(Type::prefixSha256):
120 return {};
121 break;
122
123 case safe_cast<TagType>(Type::thresholdSha256):
125 return {};
126 break;
127
128 case safe_cast<TagType>(Type::rsaSha256):
130 return {};
131 break;
132
133 case safe_cast<TagType>(Type::ed25519Sha256):
135 return {};
136
137 default:
139 return {};
140 }
141
142 if (!s.empty())
143 {
145 return {};
146 }
147
148 return f;
149}
150
151} // namespace cryptoconditions
152} // namespace ripple
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< Fulfillment > deserialize(Slice s, std::error_code &ec)
Parse the payload for a PreimageSha256 condition.
bool validate(Fulfillment const &f, Condition const &c, Slice m)
Verify if the given message satisfies the fulfillment.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Load a fulfillment from its binary form.
virtual Type type() const =0
Returns the type of this condition.
virtual bool validate(Slice data) const =0
Validates a fulfillment.
static constexpr std::size_t maxSerializedFulfillment
The largest binary fulfillment we support.
Definition Fulfillment.h:41
virtual Condition condition() const =0
Returns the condition associated with the given fulfillment.