2017-06-29 16:40:45 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-10-17 04:15:46 +03:00
|
|
|
"fmt"
|
2017-12-11 02:37:21 +03:00
|
|
|
"io"
|
2017-06-29 16:40:45 +03:00
|
|
|
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2017-06-29 16:40:45 +03:00
|
|
|
"github.com/lightningnetwork/lightning-onion"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
2017-10-11 05:31:44 +03:00
|
|
|
// ForwardingError wraps an lnwire.FailureMessage in a struct that also
|
|
|
|
// includes the source of the error.
|
|
|
|
type ForwardingError struct {
|
|
|
|
// ErrorSource is the public key of the node that sent the error. With
|
|
|
|
// this information, the dispatcher of a payment can modify their set
|
|
|
|
// of candidate routes in response to the type of error extracted.
|
|
|
|
ErrorSource *btcec.PublicKey
|
|
|
|
|
2017-10-17 04:15:46 +03:00
|
|
|
// ExtraMsg is an additional error message that callers can provide in
|
|
|
|
// order to provide context specific error details.
|
|
|
|
ExtraMsg string
|
|
|
|
|
2017-10-11 05:31:44 +03:00
|
|
|
lnwire.FailureMessage
|
|
|
|
}
|
2017-10-11 05:36:52 +03:00
|
|
|
|
2017-10-17 04:15:46 +03:00
|
|
|
// Error implements the built-in error interface. We use this method to allow
|
|
|
|
// the switch or any callers to insert additional context to the error message
|
|
|
|
// returned.
|
|
|
|
func (f *ForwardingError) Error() string {
|
|
|
|
if f.ExtraMsg == "" {
|
|
|
|
return f.FailureMessage.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v: %v", f.FailureMessage.Error(), f.ExtraMsg)
|
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// ErrorDecrypter is an interface that is used to decrypt the onion encrypted
|
2017-07-15 06:08:29 +03:00
|
|
|
// failure reason an extra out a well formed error.
|
2017-10-11 05:36:52 +03:00
|
|
|
type ErrorDecrypter interface {
|
|
|
|
// DecryptError peels off each layer of onion encryption from the first
|
2017-07-15 06:08:29 +03:00
|
|
|
// hop, to the source of the error. A fully populated
|
2017-10-11 05:36:52 +03:00
|
|
|
// lnwire.FailureMessage is returned along with the source of the
|
|
|
|
// error.
|
|
|
|
DecryptError(lnwire.OpaqueReason) (*ForwardingError, error)
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-12-11 02:37:21 +03:00
|
|
|
// EncrypterType establishes an enum used in serialization to indicate how to
|
|
|
|
// decode a concrete instance of the ErrorEncrypter interface.
|
|
|
|
type EncrypterType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
// EncrypterTypeNone signals that no error encyrpter is present, this
|
|
|
|
// can happen if the htlc is originates in the switch.
|
|
|
|
EncrypterTypeNone EncrypterType = 0
|
|
|
|
|
|
|
|
// EncrypterTypeSphinx is used to identify a sphinx onion error
|
|
|
|
// encrypter instance.
|
|
|
|
EncrypterTypeSphinx = 1
|
|
|
|
|
|
|
|
// EncrypterTypeMock is used to identify a mock obfuscator instance.
|
|
|
|
EncrypterTypeMock = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnknownEncrypterType is an error message used to signal that an unexpected
|
|
|
|
// EncrypterType was encountered during decoding.
|
|
|
|
type UnknownEncrypterType EncrypterType
|
|
|
|
|
|
|
|
// Error returns a formatted error indicating the invalid EncrypterType.
|
|
|
|
func (e UnknownEncrypterType) Error() string {
|
|
|
|
return fmt.Sprintf("unknown error encrypter type: %d", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorEncrypterExtracter defines a function signature that extracts an
|
|
|
|
// ErrorEncrypter from an sphinx OnionPacket.
|
2018-03-12 23:36:25 +03:00
|
|
|
type ErrorEncrypterExtracter func(*btcec.PublicKey) (ErrorEncrypter,
|
2017-12-11 02:37:21 +03:00
|
|
|
lnwire.FailCode)
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// ErrorEncrypter is an interface that is used to encrypt HTLC related errors
|
|
|
|
// at the source of the error, and also at each intermediate hop all the way
|
|
|
|
// back to the source of the payment.
|
|
|
|
type ErrorEncrypter interface {
|
|
|
|
// EncryptFirstHop transforms a concrete failure message into an
|
2017-07-15 06:08:29 +03:00
|
|
|
// encrypted opaque failure reason. This method will be used at the
|
2017-10-11 05:36:52 +03:00
|
|
|
// source that the error occurs. It differs from IntermediateEncrypt
|
2017-07-15 06:08:29 +03:00
|
|
|
// slightly, in that it computes a proper MAC over the error.
|
2017-10-11 05:36:52 +03:00
|
|
|
EncryptFirstHop(lnwire.FailureMessage) (lnwire.OpaqueReason, error)
|
2017-06-29 16:40:45 +03:00
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// IntermediateEncrypt wraps an already encrypted opaque reason error
|
|
|
|
// in an additional layer of onion encryption. This process repeats
|
|
|
|
// until the error arrives at the source of the payment.
|
|
|
|
IntermediateEncrypt(lnwire.OpaqueReason) lnwire.OpaqueReason
|
2017-12-11 02:37:21 +03:00
|
|
|
|
|
|
|
// Type returns an enum indicating the underlying concrete instance
|
|
|
|
// backing this interface.
|
|
|
|
Type() EncrypterType
|
|
|
|
|
2018-03-12 23:36:25 +03:00
|
|
|
// Encode serializes the encrypter's ephemeral public key to the given
|
|
|
|
// io.Writer.
|
2017-12-11 02:37:21 +03:00
|
|
|
Encode(io.Writer) error
|
|
|
|
|
2018-03-12 23:36:25 +03:00
|
|
|
// Decode deserializes the encrypter' ephemeral public key from the
|
|
|
|
// given io.Reader.
|
2017-12-11 02:37:21 +03:00
|
|
|
Decode(io.Reader) error
|
2018-03-12 23:36:25 +03:00
|
|
|
|
|
|
|
// Reextract rederives the encrypter using the extracter, performing an
|
|
|
|
// ECDH with the sphinx router's key and the ephemeral public key.
|
|
|
|
//
|
|
|
|
// NOTE: This should be called shortly after Decode to properly
|
|
|
|
// reinitialize the error encrypter.
|
|
|
|
Reextract(ErrorEncrypterExtracter) error
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// SphinxErrorEncrypter is a concrete implementation of both the ErrorEncrypter
|
|
|
|
// interface backed by an implementation of the Sphinx packet format. As a
|
|
|
|
// result, all errors handled are themselves wrapped in layers of onion
|
|
|
|
// encryption and must be treated as such accordingly.
|
|
|
|
type SphinxErrorEncrypter struct {
|
|
|
|
*sphinx.OnionErrorEncrypter
|
2017-12-11 02:37:21 +03:00
|
|
|
|
2018-03-12 23:36:25 +03:00
|
|
|
EphemeralKey *btcec.PublicKey
|
2017-12-11 02:37:21 +03:00
|
|
|
}
|
|
|
|
|
2018-03-12 23:36:25 +03:00
|
|
|
// NewSphinxErrorEncrypter initializes a blank sphinx error encrypter, that
|
|
|
|
// should be used to deserialize an encoded SphinxErrorEncrypter. Since the
|
|
|
|
// actual encrypter is not stored in plaintext while at rest, reconstructing the
|
|
|
|
// error encrypter requires:
|
|
|
|
// 1) Decode: to deserialize the ephemeral public key.
|
|
|
|
// 2) Reextract: to "unlock" the actual error encrypter using an active
|
|
|
|
// OnionProcessor.
|
2017-12-11 02:37:21 +03:00
|
|
|
func NewSphinxErrorEncrypter() *SphinxErrorEncrypter {
|
|
|
|
return &SphinxErrorEncrypter{
|
2018-03-12 23:36:25 +03:00
|
|
|
OnionErrorEncrypter: nil,
|
|
|
|
EphemeralKey: &btcec.PublicKey{},
|
2017-12-11 02:37:21 +03:00
|
|
|
}
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// EncryptFirstHop transforms a concrete failure message into an encrypted
|
2017-07-15 06:08:29 +03:00
|
|
|
// opaque failure reason. This method will be used at the source that the error
|
|
|
|
// occurs. It differs from BackwardObfuscate slightly, in that it computes a
|
|
|
|
// proper MAC over the error.
|
2017-06-29 16:40:45 +03:00
|
|
|
//
|
2017-10-11 05:36:52 +03:00
|
|
|
// NOTE: Part of the ErrorEncrypter interface.
|
2017-10-11 08:16:46 +03:00
|
|
|
func (s *SphinxErrorEncrypter) EncryptFirstHop(failure lnwire.FailureMessage) (lnwire.OpaqueReason, error) {
|
2017-06-29 16:40:45 +03:00
|
|
|
var b bytes.Buffer
|
|
|
|
if err := lnwire.EncodeFailure(&b, failure, 0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-15 06:08:29 +03:00
|
|
|
// We pass a true as the first parameter to indicate that a MAC should
|
|
|
|
// be added.
|
2017-10-11 08:16:46 +03:00
|
|
|
return s.EncryptError(true, b.Bytes()), nil
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// IntermediateEncrypt wraps an already encrypted opaque reason error in an
|
2017-07-15 06:08:29 +03:00
|
|
|
// additional layer of onion encryption. This process repeats until the error
|
|
|
|
// arrives at the source of the payment. We re-encrypt the message on the
|
|
|
|
// backwards path to ensure that the error is indistinguishable from any other
|
|
|
|
// error seen.
|
2017-06-29 16:40:45 +03:00
|
|
|
//
|
2017-10-11 05:36:52 +03:00
|
|
|
// NOTE: Part of the ErrorEncrypter interface.
|
|
|
|
func (s *SphinxErrorEncrypter) IntermediateEncrypt(reason lnwire.OpaqueReason) lnwire.OpaqueReason {
|
|
|
|
return s.EncryptError(false, reason)
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-12-11 02:37:21 +03:00
|
|
|
// Type returns the identifier for a sphinx error encrypter.
|
|
|
|
func (s *SphinxErrorEncrypter) Type() EncrypterType {
|
|
|
|
return EncrypterTypeSphinx
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:36:25 +03:00
|
|
|
// Encode serializes the error encrypter' ephemeral public key to the provided
|
|
|
|
// io.Writer.
|
2017-12-11 02:37:21 +03:00
|
|
|
func (s *SphinxErrorEncrypter) Encode(w io.Writer) error {
|
2018-03-12 23:36:25 +03:00
|
|
|
ephemeral := s.EphemeralKey.SerializeCompressed()
|
|
|
|
_, err := w.Write(ephemeral)
|
|
|
|
return err
|
2017-12-11 02:37:21 +03:00
|
|
|
}
|
|
|
|
|
2018-03-12 23:36:25 +03:00
|
|
|
// Decode reconstructs the error encrypter's ephemeral public key from the
|
|
|
|
// provided io.Reader.
|
2017-12-11 02:37:21 +03:00
|
|
|
func (s *SphinxErrorEncrypter) Decode(r io.Reader) error {
|
2018-03-12 23:36:25 +03:00
|
|
|
var ephemeral [33]byte
|
|
|
|
if _, err := io.ReadFull(r, ephemeral[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
s.EphemeralKey, err = btcec.ParsePubKey(ephemeral[:], btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reextract rederives the error encrypter from the currently held EphemeralKey.
|
|
|
|
// This intended to be used shortly after Decode, to fully initialize a
|
|
|
|
// SphinxErrorEncrypter.
|
|
|
|
func (s *SphinxErrorEncrypter) Reextract(
|
|
|
|
extract ErrorEncrypterExtracter) error {
|
|
|
|
|
|
|
|
obfuscator, failcode := extract(s.EphemeralKey)
|
|
|
|
if failcode != lnwire.CodeNone {
|
|
|
|
// This should never happen, since we already validated that
|
|
|
|
// this obfuscator can be extracted when it was received in the
|
|
|
|
// link.
|
|
|
|
return fmt.Errorf("unable to reconstruct onion "+
|
|
|
|
"obfuscator, got failcode: %d", failcode)
|
|
|
|
}
|
|
|
|
|
|
|
|
sphinxEncrypter, ok := obfuscator.(*SphinxErrorEncrypter)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("incorrect onion error extracter")
|
2017-12-11 02:37:21 +03:00
|
|
|
}
|
2018-03-12 23:36:25 +03:00
|
|
|
|
|
|
|
// Copy the freshly extracted encrypter.
|
|
|
|
s.OnionErrorEncrypter = sphinxEncrypter.OnionErrorEncrypter
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2017-12-11 02:37:21 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// A compile time check to ensure SphinxErrorEncrypter implements the
|
|
|
|
// ErrorEncrypter interface.
|
|
|
|
var _ ErrorEncrypter = (*SphinxErrorEncrypter)(nil)
|
2017-06-29 16:40:45 +03:00
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// SphinxErrorDecrypter wraps the sphinx data SphinxErrorDecrypter and maps the
|
|
|
|
// returned errors to concrete lnwire.FailureMessage instances.
|
|
|
|
type SphinxErrorDecrypter struct {
|
|
|
|
*sphinx.OnionErrorDecrypter
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// DecryptError peels off each layer of onion encryption from the first hop, to
|
|
|
|
// the source of the error. A fully populated lnwire.FailureMessage is returned
|
|
|
|
// along with the source of the error.
|
2017-06-29 16:40:45 +03:00
|
|
|
//
|
2017-10-11 05:36:52 +03:00
|
|
|
// NOTE: Part of the ErrorDecrypter interface.
|
|
|
|
func (s *SphinxErrorDecrypter) DecryptError(reason lnwire.OpaqueReason) (*ForwardingError, error) {
|
2017-07-15 06:08:29 +03:00
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
source, failureData, err := s.OnionErrorDecrypter.DecryptError(reason)
|
2017-06-29 16:40:45 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := bytes.NewReader(failureData)
|
2017-10-11 05:36:52 +03:00
|
|
|
failureMsg, err := lnwire.DecodeFailure(r, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ForwardingError{
|
|
|
|
ErrorSource: source,
|
|
|
|
FailureMessage: failureMsg,
|
|
|
|
}, nil
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// A compile time check to ensure ErrorDecrypter implements the Deobfuscator
|
|
|
|
// interface.
|
|
|
|
var _ ErrorDecrypter = (*SphinxErrorDecrypter)(nil)
|