github.com/keybase/client/go@v0.0.0-20240520164431-4f512a4c85a3/client/cmd_pgp_import.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 client
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  
    10  	"golang.org/x/net/context"
    11  
    12  	"github.com/keybase/cli"
    13  	"github.com/keybase/client/go/libcmdline"
    14  	"github.com/keybase/client/go/libkb"
    15  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    16  	"github.com/keybase/go-framed-msgpack-rpc/rpc"
    17  )
    18  
    19  func NewCmdPGPImport(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    20  	return cli.Command{
    21  		Name:  "import",
    22  		Usage: "Import a PGP key into keybase",
    23  		Action: func(c *cli.Context) {
    24  			cl.ChooseCommand(&CmdPGPImport{Contextified: libkb.NewContextified(g)}, "import", c)
    25  		},
    26  		Flags: []cli.Flag{
    27  			cli.StringFlag{
    28  				Name:  "i, infile",
    29  				Usage: "Specify an infile (stdin by default).",
    30  			},
    31  			cli.BoolFlag{
    32  				Name:  "push-secret",
    33  				Usage: "Push an encrypted copy of the secret key to the server.",
    34  			},
    35  		},
    36  		Description: `"keybase pgp import" imports a PGP secret key for use with Keybase.
    37     It accepts that secret key via file (with the "--infile" flag) or
    38     otherwise via standard input. The secret key is used to sign the
    39     public PGP key into the user's Keybase sigchain. The secret key
    40     is also imported into the local Keybase keyring and encrypted with
    41     the local key security protocol.
    42  
    43     If (and only if) the "--push-secret" flag is specified, this command
    44     pushes the PGP secret key to the Keybase server, encrypted with the
    45     user's passphrase. The server, in this case, could theoretically
    46     recover the PGP secret key by cracking the user's passphrase.`,
    47  	}
    48  }
    49  
    50  type CmdPGPImport struct {
    51  	UnixFilter
    52  	arg    keybase1.PGPImportArg
    53  	infile string
    54  	libkb.Contextified
    55  }
    56  
    57  func (s *CmdPGPImport) ParseArgv(ctx *cli.Context) error {
    58  	if len(ctx.Args()) > 0 {
    59  		return fmt.Errorf("Invalid arguments")
    60  	}
    61  
    62  	s.arg.PushSecret = ctx.Bool("push-secret")
    63  	s.infile = ctx.String("infile")
    64  	return nil
    65  }
    66  
    67  func (s *CmdPGPImport) Run() error {
    68  	if err := s.readKeyData(); err != nil {
    69  		return err
    70  	}
    71  
    72  	protocols := []rpc.Protocol{
    73  		NewSecretUIProtocol(s.G()),
    74  	}
    75  
    76  	cli, err := GetPGPClient(s.G())
    77  	if err != nil {
    78  		return err
    79  	}
    80  	if err = RegisterProtocolsWithContext(protocols, s.G()); err != nil {
    81  		return err
    82  	}
    83  	return cli.PGPImport(context.TODO(), s.arg)
    84  }
    85  
    86  func (s *CmdPGPImport) readKeyData() error {
    87  	src, err := initSource("", s.infile)
    88  	if err != nil {
    89  		return err
    90  	}
    91  	if err = src.Open(); err != nil {
    92  		return err
    93  	}
    94  	defer src.Close()
    95  	s.arg.Key, err = io.ReadAll(src)
    96  	return err
    97  }
    98  
    99  func (s *CmdPGPImport) GetUsage() libkb.Usage {
   100  	return libkb.Usage{
   101  		Config:    true,
   102  		API:       true,
   103  		KbKeyring: true,
   104  	}
   105  }