Skip to content

Commit

Permalink
feat: add support for saving public key to file using -output flag
Browse files Browse the repository at this point in the history
- Introduced the `-output` flag to allow users to specify a file path for saving the public key.
- Updated the `printPublicKey` function to write the public key to the specified file, or to stdout if no file is provided.
- Modified the `create` and `migrate` cases to support the `-output` flag when printing the public key.
- Ensured backward compatibility by retaining stdout output when `-output` is not used.
  • Loading branch information
pmpbaptista committed Oct 3, 2024
1 parent e8634aa commit b3a206f
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions cmd/tesla-keygen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ const usageText = `
Creates or deletes a private key and saves it in the system keyring, or migrates a key from a
plaintext file into the system keyring.
The program writes the public key to stdout (except when deleting a key). When using the create
option, the program will not overwrite an existing unless invoked with -f.
The program writes the public key to stdout (except when deleting a key or setting the output
location with -output). When using the create option, the program will not overwrite an existing
unless invoked with -f.
The type of keyring and name of the key inside that keyring are controlled by the command-line
options below, or through the corresponding environment variables.`
Expand All @@ -47,7 +48,7 @@ func usage(w io.Writer) {
flag.PrintDefaults()
}

func printPublicKey(skey protocol.ECDHPrivateKey) bool {
func printPublicKey(skey protocol.ECDHPrivateKey, outputFile string) bool {
pkey := ecdsa.PublicKey{Curve: elliptic.P256()}
pkey.X, pkey.Y = elliptic.Unmarshal(elliptic.P256(), skey.PublicBytes())
if pkey.X == nil {
Expand All @@ -57,7 +58,20 @@ func printPublicKey(skey protocol.ECDHPrivateKey) bool {
if err != nil {
return false
}
pem.Encode(os.Stdout, &pem.Block{Type: "PUBLIC KEY", Bytes: derPublicKey})

// If outputFile is provided, write to file, else write to stdout
var out io.Writer = os.Stdout
if outputFile != "" {
file, err := os.Create(outputFile)
if err != nil {
writeErr("Failed to create output file: %s", err)
return false
}
defer file.Close()
out = file
}

pem.Encode(out, &pem.Block{Type: "PUBLIC KEY", Bytes: derPublicKey})
return true
}

Expand All @@ -77,9 +91,10 @@ func printPrivateKey(skey protocol.ECDHPrivateKey) error {
func main() {
// Command-line variables
var (
overwrite bool
skey protocol.ECDHPrivateKey
err error
overwrite bool
outputFile string
skey protocol.ECDHPrivateKey
err error
)
status := 1
defer func() {
Expand All @@ -90,7 +105,9 @@ func main() {
config.RegisterCommandLineFlags()
flag.Usage = cliUsage
flag.BoolVar(&overwrite, "f", false, "Overwrite existing key if it exists")
flag.StringVar(&outputFile, "output", "", "Save public key to `file`. Defaults to stdout.")
flag.Parse()

if config.Debug {
log.SetLevel(log.LevelDebug)
}
Expand Down Expand Up @@ -130,7 +147,7 @@ func main() {
// Print key and exit if it already exists
skey, err = config.PrivateKey()
if err == nil {
if ok := printPublicKey(skey); !ok {
if ok := printPublicKey(skey, outputFile); !ok {
writeErr("Failed to parse key. The keyring may be corrupted. Run with -f to generate new key.")
return
}
Expand Down Expand Up @@ -164,7 +181,7 @@ func main() {
return
}

if ok := printPublicKey(skey); !ok {
if ok := printPublicKey(skey, outputFile); !ok {
writeErr("Failed to extract public key. Run with -f to generate new key pair.")
return
}
Expand Down

0 comments on commit b3a206f

Please sign in to comment.