github.com/keybase/client/go@v0.0.0-20240520164431-4f512a4c85a3/client/cmd_test_crypto.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/keybase/cli"
     7  	"github.com/keybase/client/go/libcmdline"
     8  	"github.com/keybase/client/go/libkb"
     9  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    10  	"github.com/keybase/go-framed-msgpack-rpc/rpc"
    11  )
    12  
    13  func newCmdTestCrypto(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    14  	return cli.Command{
    15  		Name:  "test-crypto",
    16  		Usage: "Test the frontend saltpack protocol",
    17  		Action: func(c *cli.Context) {
    18  			cl.ChooseCommand(&CmdTestCrypto{Contextified: libkb.NewContextified(g)}, "test-crypto", c)
    19  		},
    20  		Flags: []cli.Flag{
    21  			cli.StringFlag{
    22  				Name:  "f",
    23  				Usage: "filename to encrypt",
    24  			},
    25  		},
    26  	}
    27  }
    28  
    29  type CmdTestCrypto struct {
    30  	libkb.Contextified
    31  	recipients []string
    32  	filename   string
    33  }
    34  
    35  func (s *CmdTestCrypto) ParseArgv(ctx *cli.Context) error {
    36  	s.recipients = ctx.Args()
    37  	if len(s.recipients) == 0 {
    38  		return errors.New("need at least one recipient")
    39  	}
    40  	s.filename = ctx.String("f")
    41  	return nil
    42  }
    43  
    44  func (s *CmdTestCrypto) GetUsage() libkb.Usage {
    45  	return libkb.Usage{}
    46  }
    47  
    48  func (s *CmdTestCrypto) Run() (err error) {
    49  	mctx := libkb.NewMetaContextBackground(s.G())
    50  	cli, err := GetSaltpackClient(s.G())
    51  	if err != nil {
    52  		return err
    53  	}
    54  	protocols := []rpc.Protocol{
    55  		NewSecretUIProtocol(s.G()),
    56  	}
    57  	if err := RegisterProtocolsWithContext(protocols, s.G()); err != nil {
    58  		return err
    59  	}
    60  
    61  	plaintext := "hello goodbye"
    62  	dui := s.G().UI.GetDumbOutputUI()
    63  
    64  	dui.Printf("encrypting string %q for %v\n", plaintext, s.recipients)
    65  	arg := keybase1.SaltpackEncryptStringArg{
    66  		Plaintext: plaintext,
    67  		Opts: keybase1.SaltpackFrontendEncryptOptions{
    68  			Recipients:  s.recipients,
    69  			IncludeSelf: true,
    70  			Signed:      true,
    71  		},
    72  	}
    73  	sres, err := cli.SaltpackEncryptString(mctx.Ctx(), arg)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	dui.Printf("ciphertext:\n\n")
    78  	dui.Printf("%s\n\n", sres.Ciphertext)
    79  
    80  	dui.Printf("decrypting ciphertext\n")
    81  	decArg := keybase1.SaltpackDecryptStringArg{
    82  		Ciphertext: sres.Ciphertext,
    83  	}
    84  	res, err := cli.SaltpackDecryptString(mctx.Ctx(), decArg)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	dui.Printf("plaintext: %q\n\n", res.Plaintext)
    89  	dui.Printf("info: %+v\n\n", res.Info)
    90  	dui.Printf("signed: %v\n", res.Signed)
    91  
    92  	dui.Printf("signing string %q\n", plaintext)
    93  	signArg := keybase1.SaltpackSignStringArg{Plaintext: plaintext}
    94  	signed, err := cli.SaltpackSignString(mctx.Ctx(), signArg)
    95  	if err != nil {
    96  		return err
    97  	}
    98  	dui.Printf("signed:\n\n")
    99  	dui.Printf("%s\n\n", signed)
   100  
   101  	dui.Printf("verifying signed msg\n")
   102  	verifyArg := keybase1.SaltpackVerifyStringArg{SignedMsg: signed}
   103  	vres, err := cli.SaltpackVerifyString(mctx.Ctx(), verifyArg)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	dui.Printf("verify result: %+v\n\n", vres)
   108  
   109  	if s.filename == "" {
   110  		dui.Printf("no filename specified, done.\n")
   111  		return nil
   112  	}
   113  
   114  	dui.Printf("encrypting file %s\n", s.filename)
   115  	efArg := keybase1.SaltpackEncryptFileArg{
   116  		Filename: s.filename,
   117  		Opts: keybase1.SaltpackFrontendEncryptOptions{
   118  			Recipients:  s.recipients,
   119  			IncludeSelf: true,
   120  			Signed:      true,
   121  		},
   122  	}
   123  	efres, err := cli.SaltpackEncryptFile(mctx.Ctx(), efArg)
   124  	if err != nil {
   125  		return err
   126  	}
   127  	dui.Printf("encrypted file result: %+v\n", efres)
   128  
   129  	dui.Printf("decrypting file: %s\n", efres.Filename)
   130  	dfArg := keybase1.SaltpackDecryptFileArg{EncryptedFilename: efres.Filename}
   131  	dfres, err := cli.SaltpackDecryptFile(mctx.Ctx(), dfArg)
   132  	if err != nil {
   133  		return err
   134  	}
   135  	dui.Printf("decrypt result: %+v\n", dfres)
   136  
   137  	dui.Printf("signing file %s\n", s.filename)
   138  	sfArg := keybase1.SaltpackSignFileArg{Filename: s.filename}
   139  	sfPath, err := cli.SaltpackSignFile(mctx.Ctx(), sfArg)
   140  	if err != nil {
   141  		return err
   142  	}
   143  	dui.Printf("signed file: %s\n", sfPath)
   144  
   145  	dui.Printf("verifying file %s\n", sfPath)
   146  	vfArg := keybase1.SaltpackVerifyFileArg{SignedFilename: sfPath}
   147  	vfres, err := cli.SaltpackVerifyFile(mctx.Ctx(), vfArg)
   148  	if err != nil {
   149  		return err
   150  	}
   151  	dui.Printf("verified result: %+v\n", vfres)
   152  
   153  	return nil
   154  }