rippled
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 <ripple/basics/safe_cast.h>
21 #include <ripple/conditions/Condition.h>
22 #include <ripple/conditions/Fulfillment.h>
23 #include <ripple/conditions/impl/PreimageSha256.h>
24 #include <ripple/conditions/impl/utils.h>
25 #include <type_traits>
26 #include <vector>
27 
28 namespace ripple {
29 namespace cryptoconditions {
30 
31 bool
32 match(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 
44 bool
45 validate(Fulfillment const& f, Condition const& c, Slice m)
46 {
47  return match(f, c) && f.validate(m);
48 }
49 
50 bool
51 validate(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  {
103  ec = error::large_size;
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:
139  ec = error::unknown_type;
140  return {};
141  }
142 
143  if (!s.empty())
144  {
146  return {};
147  }
148 
149  return f;
150 }
151 
152 } // namespace cryptoconditions
153 } // namespace ripple
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:80
ripple::cryptoconditions::error::buffer_empty
@ buffer_empty
ripple::cryptoconditions::Type::preimageSha256
@ preimageSha256
ripple::cryptoconditions::Fulfillment::deserialize
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Load a fulfillment from its binary form.
Definition: Fulfillment.cpp:57
ripple::cryptoconditions::Fulfillment::maxSerializedFulfillment
static constexpr std::size_t maxSerializedFulfillment
The largest binary fulfillment we support.
Definition: Fulfillment.h:41
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
vector
ripple::cryptoconditions::Fulfillment::type
virtual Type type() const =0
Returns the type of this condition.
ripple::cryptoconditions::Fulfillment::validate
virtual bool validate(Slice data) const =0
Validates a fulfillment.
ripple::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:97
ripple::Slice::length
std::size_t length() const noexcept
Definition: Slice.h:86
ripple::cryptoconditions::Condition
Definition: Condition.h:44
ripple::Slice::empty
bool empty() const noexcept
Return true if the byte range is empty.
Definition: Slice.h:69
ripple::cryptoconditions::PreimageSha256::deserialize
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Parse the payload for a PreimageSha256 condition.
Definition: PreimageSha256.h:55
std::error_code
STL class.
ripple::cryptoconditions::Type::ed25519Sha256
@ ed25519Sha256
ripple::cryptoconditions::Condition::type
Type type
Definition: Condition.h:70
ripple::cryptoconditions::error::buffer_underfull
@ buffer_underfull
ripple::cryptoconditions::Type::thresholdSha256
@ thresholdSha256
ripple::cryptoconditions::error::unknown_type
@ unknown_type
ripple::cryptoconditions::validate
bool validate(Fulfillment const &f, Condition const &c, Slice m)
Verify if the given message satisfies the fulfillment.
Definition: Fulfillment.cpp:45
ripple::cryptoconditions::Fulfillment::condition
virtual Condition condition() const =0
Returns the condition associated with the given fulfillment.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::cryptoconditions::Type::prefixSha256
@ prefixSha256
ripple::cryptoconditions::Type::rsaSha256
@ rsaSha256
ripple::cryptoconditions::match
bool match(Fulfillment const &f, Condition const &c)
Determine whether the given fulfillment and condition match.
Definition: Fulfillment.cpp:32
ripple::cryptoconditions::Fulfillment
Definition: Fulfillment.h:31
ripple::cryptoconditions::error::buffer_overfull
@ buffer_overfull
ripple::cryptoconditions::error::large_size
@ large_size
std::unique_ptr
STL class.
ripple::cryptoconditions::error::trailing_garbage
@ trailing_garbage
type_traits
ripple::cryptoconditions::error::malformed_encoding
@ malformed_encoding
ripple::cryptoconditions::error::unsupported_type
@ unsupported_type