Skip to content

Certificates #42

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

Merged
merged 5 commits into from
Dec 21, 2015
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Create a signed certificate
  • Loading branch information
matteosuppo committed Dec 17, 2015
commit 5cb9f10947700e4ba726d14076bed1d7c93e08e8
44 changes: 42 additions & 2 deletions certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ func generateSingleCertificate(isCa bool) (*x509.Certificate, error) {

func generateCertificates() {

os.Remove("ca.cert.pem")
os.Remove("ca.key.pem")
os.Remove("cert.pem")
os.Remove("key.pem")

// Create the key for the certification authority
caKey, err := generateKey("")
if err != nil {
Expand All @@ -159,12 +164,47 @@ func generateCertificates() {

derBytes, err := x509.CreateCertificate(rand.Reader, caTemplate, caTemplate, publicKey(caKey), caKey)

certOut, err := os.Create("ca.crt.pem")
certOut, err := os.Create("ca.cert.pem")
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
certOut.Close()
log.Print("written ca.cert.pem")

// Create the key for the final certificate
key, err := generateKey("")
if err != nil {
log.Error(err.Error())
os.Exit(1)
}

keyOut, err = os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
pem.Encode(keyOut, pemBlockForKey(key))
keyOut.Close()
log.Println("written key.pem")

// Create the final certificate
template, err := generateSingleCertificate(false)

if err != nil {
log.Error(err.Error())
os.Exit(1)
}

derBytes, err = x509.CreateCertificate(rand.Reader, template, caTemplate, publicKey(key), key)

certOut, err = os.Create("cert.pem")
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
certOut.Close()
log.Print("written ca.crt.pem")
log.Print("written cert.pem")
}