rippled
Sign.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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/protocol/Sign.h>
21 
22 namespace ripple {
23 
24 void
26  STObject& st,
27  HashPrefix const& prefix,
28  KeyType type,
29  SecretKey const& sk,
30  SF_VL const& sigField)
31 {
32  Serializer ss;
33  ss.add32(prefix);
35  set(st, sigField, sign(type, sk, ss.slice()));
36 }
37 
38 bool
40  STObject const& st,
41  HashPrefix const& prefix,
42  PublicKey const& pk,
43  SF_VL const& sigField)
44 {
45  auto const sig = get(st, sigField);
46  if (!sig)
47  return false;
48  Serializer ss;
49  ss.add32(prefix);
51  return verify(
52  pk, Slice(ss.data(), ss.size()), Slice(sig->data(), sig->size()));
53 }
54 
55 // Questions regarding buildMultiSigningData:
56 //
57 // Why do we include the Signer.Account in the blob to be signed?
58 //
59 // Unless you include the Account which is signing in the signing blob,
60 // you could swap out any Signer.Account for any other, which may also
61 // be on the SignerList and have a RegularKey matching the
62 // Signer.SigningPubKey.
63 //
64 // That RegularKey may be set to allow some 3rd party to sign transactions
65 // on the account's behalf, and that RegularKey could be common amongst all
66 // users of the 3rd party. That's just one example of sharing the same
67 // RegularKey amongst various accounts and just one vulnerability.
68 //
69 // "When you have something that's easy to do that makes entire classes of
70 // attacks clearly and obviously impossible, you need a damn good reason
71 // not to do it." -- David Schwartz
72 //
73 // Why would we include the signingFor account in the blob to be signed?
74 //
75 // In the current signing scheme, the account that a signer is `signing
76 // for/on behalf of` is the tx_json.Account.
77 //
78 // Later we might support more levels of signing. Suppose Bob is a signer
79 // for Alice, and Carol is a signer for Bob, so Carol can sign for Bob who
80 // signs for Alice. But suppose Alice has two signers: Bob and Dave. If
81 // Carol is a signer for both Bob and Dave, then the signature needs to
82 // distinguish between Carol signing for Bob and Carol signing for Dave.
83 //
84 // So, if we support multiple levels of signing, then we'll need to
85 // incorporate the "signing for" accounts into the signing data as well.
86 Serializer
87 buildMultiSigningData(STObject const& obj, AccountID const& signingID)
88 {
90  finishMultiSigningData(signingID, s);
91  return s;
92 }
93 
94 Serializer
96 {
97  Serializer s;
100  return s;
101 }
102 
103 } // namespace ripple
ripple::TypedField
A field with a type known at compile time.
Definition: SField.h:271
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::verify
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig, bool mustBeFullyCanonical) noexcept
Verify a signature on a message.
Definition: PublicKey.cpp:272
ripple::HashPrefix::txMultiSign
@ txMultiSign
inner transaction to multi-sign
ripple::finishMultiSigningData
void finishMultiSigningData(AccountID const &signingID, Serializer &s)
Definition: Sign.h:85
ripple::Serializer::data
void const * data() const noexcept
Definition: Serializer.h:75
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
ripple::HashPrefix
HashPrefix
Prefix for hashing functions.
Definition: HashPrefix.h:54
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::startMultiSigningData
Serializer startMultiSigningData(STObject const &obj)
Break the multi-signing hash computation into 2 parts for optimization.
Definition: Sign.cpp:95
ripple::set
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
Definition: BasicConfig.h:313
ripple::Serializer::slice
Slice slice() const noexcept
Definition: Serializer.h:63
ripple::SecretKey
A secret key.
Definition: SecretKey.h:36
ripple::STObject::addWithoutSigningFields
void addWithoutSigningFields(Serializer &s) const
Definition: STObject.h:889
ripple::KeyType
KeyType
Definition: KeyType.h:28
ripple::Serializer
Definition: Serializer.h:39
ripple::STObject
Definition: STObject.h:51
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Serializer::size
std::size_t size() const noexcept
Definition: Serializer.h:69
ripple::sign
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &m)
Generate a signature for a message.
Definition: SecretKey.cpp:238
ripple::Serializer::add32
int add32(std::uint32_t i)
Definition: Serializer.cpp:38
ripple::buildMultiSigningData
Serializer buildMultiSigningData(STObject const &obj, AccountID const &signingID)
Return a Serializer suitable for computing a multisigning TxnSignature.
Definition: Sign.cpp:87
ripple::get
T & get(EitherAmount &amt)
Definition: AmountSpec.h:118