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 <boost/regex.hpp>
26 #include <boost/optional.hpp>
27 #include <type_traits>
28 #include <vector>
29 
30 namespace ripple {
31 namespace cryptoconditions {
32 
33 bool
35  Fulfillment const& f,
36  Condition const& c)
37 {
38  // Fast check: the fulfillment's type must match the
39  // conditions's type:
40  if (f.type() != c.type)
41  return false;
42 
43  // Derive the condition from the given fulfillment
44  // and ensure that it matches the given condition.
45  return c == f.condition();
46 }
47 
48 bool
50  Fulfillment const& f,
51  Condition const& c,
52  Slice m)
53 {
54  return match (f, c) && f.validate (m);
55 }
56 
57 bool
59  Fulfillment const& f,
60  Condition const& c)
61 {
62  return validate (f, c, {});
63 }
64 
67  Slice s,
68  std::error_code& ec)
69 {
70  // Per the RFC, in a fulfillment we choose a type based
71  // on the tag of the item we contain:
72  //
73  // Fulfillment ::= CHOICE {
74  // preimageSha256 [0] PreimageFulfillment ,
75  // prefixSha256 [1] PrefixFulfillment,
76  // thresholdSha256 [2] ThresholdFulfillment,
77  // rsaSha256 [3] RsaSha256Fulfillment,
78  // ed25519Sha256 [4] Ed25519Sha512Fulfillment
79  // }
80 
81  if (s.empty())
82  {
84  return nullptr;
85  }
86 
87  using namespace der;
88 
89  auto const p = parsePreamble(s, ec);
90  if (ec)
91  return nullptr;
92 
93  // All fulfillments are context-specific, constructed types
94  if (!isConstructed(p) || !isContextSpecific(p))
95  {
97  return nullptr;
98  }
99 
100  if (p.length > s.size())
101  {
103  return {};
104  }
105 
106  if (p.length < s.size())
107  {
109  return {};
110  }
111 
112  if (p.length > maxSerializedFulfillment)
113  {
114  ec = error::large_size;
115  return {};
116  }
117 
119 
120  using TagType = decltype(p.tag);
121  switch (p.tag)
122  {
123  case safe_cast<TagType>(Type::preimageSha256):
124  f = PreimageSha256::deserialize(Slice(s.data(), p.length), ec);
125  if (ec)
126  return {};
127  s += p.length;
128  break;
129 
130  case safe_cast<TagType>(Type::prefixSha256):
132  return {};
133  break;
134 
135  case safe_cast<TagType>(Type::thresholdSha256):
137  return {};
138  break;
139 
140  case safe_cast<TagType>(Type::rsaSha256):
142  return {};
143  break;
144 
145  case safe_cast<TagType>(Type::ed25519Sha256):
147  return {};
148 
149  default:
150  ec = error::unknown_type;
151  return {};
152  }
153 
154  if (!s.empty())
155  {
157  return {};
158  }
159 
160  return f;
161 }
162 
163 }
164 }
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:77
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:66
ripple::cryptoconditions::Fulfillment::maxSerializedFulfillment
static constexpr std::size_t maxSerializedFulfillment
The largest binary fulfillment we support.
Definition: Fulfillment.h:42
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:43
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:87
ripple::cryptoconditions::Condition
Definition: Condition.h:46
ripple::Slice::empty
bool empty() const noexcept
Return true if the byte range is empty.
Definition: Slice.h:67
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:57
std::error_code
STL class.
ripple::cryptoconditions::Type::ed25519Sha256
@ ed25519Sha256
ripple::cryptoconditions::Condition::type
Type type
Definition: Condition.h:73
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:49
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:34
ripple::cryptoconditions::Fulfillment
Definition: Fulfillment.h:32
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