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#include <xrpl/basics/safe_cast.h>
25#include <type_traits>
26#include <vector>
27
28namespace ripple {
29namespace cryptoconditions {
30
31bool
32match(Fulfillment const& f, Condition const& c)
33{
34 // Fast check: the fulfillment's type must match the
35 // conditions's type:
36 if (f.type() != c.type)
37 return false;
38
39 // Derive the condition from the given fulfillment
40 // and ensure that it matches the given condition.
41 return c == f.condition();
42}
43
44bool
45validate(Fulfillment const& f, Condition const& c, Slice m)
46{
47 return match(f, c) && f.validate(m);
48}
49
50bool
51validate(Fulfillment const& f, Condition const& c)
52{
53 return validate(f, c, {});
54}
55
58{
59 // Per the RFC, in a fulfillment we choose a type based
60 // on the tag of the item we contain:
61 //
62 // Fulfillment ::= CHOICE {
63 // preimageSha256 [0] PreimageFulfillment ,
64 // prefixSha256 [1] PrefixFulfillment,
65 // thresholdSha256 [2] ThresholdFulfillment,
66 // rsaSha256 [3] RsaSha256Fulfillment,
67 // ed25519Sha256 [4] Ed25519Sha512Fulfillment
68 // }
69
70 if (s.empty())
71 {
73 return nullptr;
74 }
75
76 using namespace der;
77
78 auto const p = parsePreamble(s, ec);
79 if (ec)
80 return nullptr;
81
82 // All fulfillments are context-specific, constructed types
83 if (!isConstructed(p) || !isContextSpecific(p))
84 {
86 return nullptr;
87 }
88
89 if (p.length > s.size())
90 {
92 return {};
93 }
94
95 if (p.length < s.size())
96 {
98 return {};
99 }
100
101 if (p.length > maxSerializedFulfillment)
102 {
104 return {};
105 }
106
108
109 using TagType = decltype(p.tag);
110 switch (p.tag)
111 {
112 case safe_cast<TagType>(Type::preimageSha256):
113 f = PreimageSha256::deserialize(Slice(s.data(), p.length), ec);
114 if (ec)
115 return {};
116 s += p.length;
117 break;
118
119 case safe_cast<TagType>(Type::prefixSha256):
121 return {};
122 break;
123
124 case safe_cast<TagType>(Type::thresholdSha256):
126 return {};
127 break;
128
129 case safe_cast<TagType>(Type::rsaSha256):
131 return {};
132 break;
133
134 case safe_cast<TagType>(Type::ed25519Sha256):
136 return {};
137
138 default:
140 return {};
141 }
142
143 if (!s.empty())
144 {
146 return {};
147 }
148
149 return f;
150}
151
152} // namespace cryptoconditions
153} // namespace ripple
An immutable linear range of bytes.
Definition: Slice.h:45
bool empty() const noexcept
Return true if the byte range is empty.
Definition: Slice.h:69
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:97
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:80
std::size_t length() const noexcept
Definition: Slice.h:86
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.
Definition: Fulfillment.cpp:45
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Load a fulfillment from its binary form.
Definition: Fulfillment.cpp:57
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.