2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// +build ignore
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Generate a self-signed X.509 certificate for a TLS server. Outputs to
|
|
|
|
// 'cert.pem' and 'key.pem' and will overwrite existing files.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2011-01-21 19:19:03 +01:00
|
|
|
"crypto/rand"
|
2011-09-16 17:47:21 +02:00
|
|
|
"crypto/rsa"
|
2010-12-03 05:34:57 +01:00
|
|
|
"crypto/x509"
|
2011-12-07 02:11:29 +01:00
|
|
|
"crypto/x509/pkix"
|
2010-12-03 05:34:57 +01:00
|
|
|
"encoding/pem"
|
|
|
|
"flag"
|
|
|
|
"log"
|
2011-12-07 02:11:29 +01:00
|
|
|
"math/big"
|
2010-12-03 05:34:57 +01:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var hostName *string = flag.String("host", "127.0.0.1", "Hostname to generate a certificate for")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 1024)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
2011-03-25 00:46:17 +01:00
|
|
|
log.Fatalf("failed to generate private key: %s", err)
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
now := time.Now()
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
template := x509.Certificate{
|
2011-09-16 17:47:21 +02:00
|
|
|
SerialNumber: new(big.Int).SetInt64(0),
|
|
|
|
Subject: pkix.Name{
|
2010-12-03 05:34:57 +01:00
|
|
|
CommonName: *hostName,
|
2011-01-21 19:19:03 +01:00
|
|
|
Organization: []string{"Acme Co"},
|
2010-12-03 05:34:57 +01:00
|
|
|
},
|
2012-01-25 21:56:26 +01:00
|
|
|
NotBefore: now.Add(-5 * time.Minute).UTC(),
|
|
|
|
NotAfter: now.AddDate(1, 0, 0).UTC(), // valid for 1 year.
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
SubjectKeyId: []byte{1, 2, 3, 4},
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
}
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
2011-03-25 00:46:17 +01:00
|
|
|
log.Fatalf("Failed to create certificate: %s", err)
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
certOut, err := os.Create("cert.pem")
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
2011-03-25 00:46:17 +01:00
|
|
|
log.Fatalf("failed to open cert.pem for writing: %s", err)
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
|
|
certOut.Close()
|
|
|
|
log.Print("written cert.pem\n")
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Print("failed to open key.pem for writing:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
|
|
|
|
keyOut.Close()
|
|
|
|
log.Print("written key.pem\n")
|
|
|
|
}
|