Skip to content

Commit

Permalink
add support for generating client certs
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana committed Mar 1, 2022
1 parent 304c332 commit 944dbb4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ certgen
*~
public.crt
private.key
client.crt
client.key
37 changes: 24 additions & 13 deletions certgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
ed25519Key = flag.Bool("ed25519", true, "Generate an Ed25519 key")
orgName = flag.String("org-name", "Acme Co", "Organization name used when generating the certs")
isNoCA = flag.Bool("no-ca", false, "whether this cert should not be its own Certificate Authority")
isClient = flag.Bool("client", false, "whether this cert is a client certificate")
validFrom = flag.String("start-date", "", "Creation date formatted as Jan 1 15:04:05 2011")
validFor = flag.Duration("duration", 365*24*time.Hour, "Duration that certificate is valid for")
)
Expand Down Expand Up @@ -121,7 +122,9 @@ func main() {
}
}

if !*isNoCA {
if *isClient {
template.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}
} else if !*isNoCA {
template.IsCA = true
template.KeyUsage |= x509.KeyUsageCertSign
}
Expand All @@ -136,32 +139,40 @@ func main() {
log.Fatalf("Failed to create certificate: %v", err)
}

certOut, err := os.Create("public.crt")
certName := "public.crt"
if *isClient {
certName = "client.crt"
}
certOut, err := os.Create(certName)
if err != nil {
log.Fatalf("Failed to open public.crt for writing: %v", err)
log.Fatalf("Failed to open %s for writing: %v", certName, err)
}
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
log.Fatalf("Failed to write data to public.crt: %v", err)
log.Fatalf("Failed to write data to %s: %v", certName, err)
}
if err := certOut.Close(); err != nil {
log.Fatalf("Error closing public.crt: %v", err)
log.Fatalf("Error closing %s: %v", certName, err)
}
log.Println("wrote", certName)

certKey := "private.key"
if *isClient {
certKey = "client.key"
}
log.Println("wrote public.crt")

keyOut, err := os.OpenFile("private.key", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
keyOut, err := os.OpenFile(certKey, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
log.Fatalf("Failed to open private.key for writing: %v", err)
return
log.Fatalf("Failed to open %s for writing: %v", certKey, err)
}
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
log.Fatalf("Unable to marshal private key: %v", err)
log.Fatalf("Unable to marshal %s: %v", certKey, err)
}
if err := pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}); err != nil {
log.Fatalf("Failed to write data to private.key: %v", err)
log.Fatalf("Failed to write data to %s: %v", certKey, err)
}
if err := keyOut.Close(); err != nil {
log.Fatalf("Error closing private.key: %v", err)
log.Fatalf("Error closing %s: %v", certKey, err)
}
log.Println("wrote private.key")
log.Println("wrote", certKey)
}

0 comments on commit 944dbb4

Please sign in to comment.