Skip to content

Add ca-key flag to enable generating multiple certs from the same CA #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ certs/leaf.pem: | $(GENERATE_TLS_CERT)
generate_cert: certs/leaf.pem | $(GENERATE_TLS_CERT)
```

There is an optional `--ca-key` flag to use an existing CA key instead of generating one.

## Client Side

Here's how to make requests that validate, using your new TLS certificates.
Expand Down
35 changes: 30 additions & 5 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"log"
"math/big"
"net"
Expand All @@ -24,9 +25,10 @@ var (
validFrom = flag.String("start-date", "", "Creation date formatted as Jan 1 15:04:05 2011 (default now)")
validFor = flag.Duration("duration", 365*24*time.Hour, "Duration that certificate is valid for")
version = flag.Bool("version", false, "Print the version string")
caKeyPath = flag.String("ca-key", "", "A root ca key that will be used to sign certificates. If omitted, a new root key will be generated.")
)

const Version = "0.1"
const Version = "0.1.1"

func main() {
flag.Parse()
Expand Down Expand Up @@ -56,11 +58,18 @@ func main() {
if err != nil {
log.Fatalf("failed to generate serial number: %s", err)
}
rootKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)

var rootKey *ecdsa.PrivateKey
if len(*caKeyPath) == 0 {
rootKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)
}
keyToFile("root.key", rootKey)
} else {
log.Printf("Using %s as the root key\n", *caKeyPath)
rootKey = keyFromFile(*caKeyPath)
}
keyToFile("root.key", rootKey)

rootTemplate := x509.Certificate{
SerialNumber: serialNumber,
Expand Down Expand Up @@ -201,6 +210,22 @@ func keyToFile(filename string, key *ecdsa.PrivateKey) {
}
}

// keyFromFile reads in a PEM serialiazed key from |filename| and parses it as a ec private key

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra 'a' in "serialized"

func keyFromFile(filename string) *ecdsa.PrivateKey {
pemBts, err := ioutil.ReadFile(*caKeyPath)
if err != nil {
panic(err)
}

block, _ := pem.Decode(pemBts)
key, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
panic(err)
}

return key
}

func certToFile(filename string, derBytes []byte) {
certOut, err := os.Create(filename)
if err != nil {
Expand Down