github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/service/crypto.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package service
     5  
     6  import (
     7  	"github.com/keybase/client/go/engine"
     8  	"github.com/keybase/client/go/libkb"
     9  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    10  	"golang.org/x/net/context"
    11  )
    12  
    13  type CryptoHandler struct {
    14  	libkb.Contextified
    15  }
    16  
    17  func NewCryptoHandler(g *libkb.GlobalContext) *CryptoHandler {
    18  	return &CryptoHandler{
    19  		Contextified: libkb.NewContextified(g),
    20  	}
    21  }
    22  
    23  func (c *CryptoHandler) getDelegatedSecretUI(sessionID int) libkb.SecretUI {
    24  	// We should only ever be called in service mode, so UIRouter
    25  	// should be non-nil.
    26  
    27  	// sessionID 0 is special for desktop UI and should be used in this
    28  	// situation for one-off passphrase requests.
    29  	ui, err := c.G().UIRouter.GetSecretUI(0)
    30  	if err != nil {
    31  		c.G().Log.Debug("UIRouter.GetSecretUI() returned an error %v", err)
    32  		return nil
    33  	}
    34  
    35  	if ui == nil {
    36  		c.G().Log.Debug("UIRouter.GetSecretUI() returned nil")
    37  	}
    38  
    39  	c.G().Log.Debug("CryptoHandler: using delegated SecretUI")
    40  
    41  	return ui
    42  }
    43  
    44  // A libkb.SecretUI implementation that always returns a LoginRequiredError.
    45  type errorSecretUI struct {
    46  	reason string
    47  }
    48  
    49  var _ libkb.SecretUI = errorSecretUI{}
    50  
    51  func (e errorSecretUI) GetPassphrase(keybase1.GUIEntryArg, *keybase1.SecretEntryArg) (keybase1.GetPassphraseRes, error) {
    52  	return keybase1.GetPassphraseRes{}, libkb.LoginRequiredError{Context: e.reason}
    53  }
    54  
    55  func (c *CryptoHandler) getSecretUI(sessionID int, reason string) libkb.SecretUI {
    56  	secretUI := c.getDelegatedSecretUI(sessionID)
    57  	if secretUI != nil {
    58  		return secretUI
    59  	}
    60  
    61  	// Return an errorSecretUI instead of triggering an error
    62  	// since we may not need a SecretUI at all.
    63  	return errorSecretUI{reason}
    64  }
    65  
    66  func (c *CryptoHandler) secretUIMaker(sessionID int, reason string) func() libkb.SecretUI {
    67  	f := func() libkb.SecretUI {
    68  		return c.getSecretUI(sessionID, reason)
    69  	}
    70  	return f
    71  }
    72  
    73  func (c *CryptoHandler) SignED25519(ctx context.Context, arg keybase1.SignED25519Arg) (keybase1.ED25519SignatureInfo, error) {
    74  	return engine.SignED25519(ctx, c.G(), arg)
    75  }
    76  
    77  func (c *CryptoHandler) SignED25519ForKBFS(ctx context.Context, arg keybase1.SignED25519ForKBFSArg) (keybase1.ED25519SignatureInfo, error) {
    78  	return engine.SignED25519ForKBFS(ctx, c.G(), arg)
    79  }
    80  
    81  func (c *CryptoHandler) SignToString(ctx context.Context, arg keybase1.SignToStringArg) (string, error) {
    82  	return engine.SignToString(ctx, c.G(), arg)
    83  }
    84  
    85  func (c *CryptoHandler) UnboxBytes32(ctx context.Context, arg keybase1.UnboxBytes32Arg) (keybase1.Bytes32, error) {
    86  	return engine.UnboxBytes32(ctx, c.G(), arg)
    87  }
    88  
    89  func (c *CryptoHandler) UnboxBytes32Any(ctx context.Context, arg keybase1.UnboxBytes32AnyArg) (keybase1.UnboxAnyRes, error) {
    90  	return engine.UnboxBytes32Any(libkb.NewMetaContext(ctx, c.G()), c.secretUIMaker(arg.SessionID, arg.Reason), arg)
    91  }