Skip to content

Commit

Permalink
Merge branch 'yakuter:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
makifdb authored Apr 7, 2022
2 parents c5b04c4 + a6d6480 commit 81ccc62
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ go run main.go verify --cafile ./testdata/ca-cert.pem ./testdata/server-cert.pem
go run main.go verify --hostname 127.0.0.1 --cafile ./testdata/ca-cert.pem ./testdata/server-cert.pem
```

### key
Key command helps you to generate RSA private key with provided bit size.

```bash
go run main.go key --help
```
```bash
go run main.go key 2048
```
```bash
go run main.go key -out private.key 2048
```

### TODO
1. Implement this logger: https://github.com/binalyze/httpreq/blob/main/logger.go
2. Add generate command for generating private key, root ca and x509 certificates
110 changes: 110 additions & 0 deletions commands/key/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package key

import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"log"
"os"
"strconv"

"github.com/pkg/errors"

"github.com/urfave/cli/v2"
)

// Remote commands
const (
CmdKey = "key"
)

const (
flagOut = "out"
)

func Command() *cli.Command {
return &cli.Command{
Name: CmdKey,
HelpName: CmdKey,
Action: Action,
ArgsUsage: `[numbits]`,
Usage: `generates RSA private key.`,
Description: `Generates RSA private key with provided number of bits.`,
Flags: Flags(),
}
}

func Flags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: flagOut,
Usage: "Output file name (optional)",
Required: false,
},
}
}

func Action(c *cli.Context) error {
log.Printf("Key command args: %q\n", c.Args().Slice())

// Check if numbits argument is provided
if c.Args().Len() < 1 {
err := errors.New("numbits is not provided")
return errors.Wrap(err, "error")
}

// Set numbits as int
numbitsArg := c.Args().First()
numbits, err := strconv.Atoi(numbitsArg)
if err != nil {
return errors.Wrapf(err, "failed to convert numbits %q to int error", numbitsArg)
}

// Set output
var output *os.File = os.Stdout

// If output file is provided, then create it and set as output
if c.IsSet(flagOut) {
outputFilePath := c.String(flagOut)
outputFile, err := os.Create(outputFilePath)
if err != nil {
return errors.Wrapf(err, "failed to create output file %q error", outputFilePath)
}

defer func() {
if err := outputFile.Close(); err != nil {
log.Printf("failed to close output file %q error %v", outputFilePath, err)
}
}()

output = outputFile
}

// Generate private key
privKey, err := rsa.GenerateKey(rand.Reader, numbits)
if err != nil {
return errors.Wrapf(err, "failed to generate rsa key error")
}

// Encode private key as PEM format
privKeyPEM := bytes.NewBuffer(nil)
err = pem.Encode(privKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privKey),
})
if err != nil {
return errors.Wrapf(err, "failed to encode private key to pem error")
}

// Write private key to output
_, err = output.WriteString(privKeyPEM.String())
if err != nil {
return errors.Wrapf(err, "failed to write output error")
}

log.Printf("Private key generated")

return nil
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/yakuter/gossl/commands/help"
"github.com/yakuter/gossl/commands/key"
"github.com/yakuter/gossl/commands/verify"

"github.com/urfave/cli/v2"
Expand All @@ -28,5 +29,6 @@ func Commands() []*cli.Command {
return []*cli.Command{
help.Command(),
verify.Command(),
key.Command(),
}
}

0 comments on commit 81ccc62

Please sign in to comment.