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"
|
2013-07-16 08:54:42 +02:00
|
|
|
"fmt"
|
2010-12-03 05:34:57 +01:00
|
|
|
"log"
|
2011-12-07 02:11:29 +01:00
|
|
|
"math/big"
|
2013-07-16 08:54:42 +02:00
|
|
|
"net"
|
2010-12-03 05:34:57 +01:00
|
|
|
"os"
|
2013-07-16 08:54:42 +02:00
|
|
|
"strings"
|
2010-12-03 05:34:57 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
var (
|
|
|
|
host = flag.String("host", "", "Comma-separated hostnames and IPs to generate a certificate for")
|
|
|
|
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")
|
|
|
|
isCA = flag.Bool("ca", false, "whether this cert should be its own Certificate Authority")
|
2013-11-06 20:49:01 +01:00
|
|
|
rsaBits = flag.Int("rsa-bits", 2048, "Size of RSA key to generate")
|
2013-07-16 08:54:42 +02:00
|
|
|
)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
if len(*host) == 0 {
|
|
|
|
log.Fatalf("Missing required --host parameter")
|
|
|
|
}
|
|
|
|
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, *rsaBits)
|
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
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
var notBefore time.Time
|
|
|
|
if len(*validFrom) == 0 {
|
|
|
|
notBefore = time.Now()
|
|
|
|
} else {
|
|
|
|
notBefore, err = time.Parse("Jan 2 15:04:05 2006", *validFrom)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to parse creation date: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notAfter := notBefore.Add(*validFor)
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to generate serial number: %s", err)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
template := x509.Certificate{
|
2014-06-05 01:15:33 +02:00
|
|
|
SerialNumber: serialNumber,
|
2011-09-16 17:47:21 +02:00
|
|
|
Subject: pkix.Name{
|
2011-01-21 19:19:03 +01:00
|
|
|
Organization: []string{"Acme Co"},
|
2010-12-03 05:34:57 +01:00
|
|
|
},
|
2013-07-16 08:54:42 +02:00
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts := strings.Split(*host, ",")
|
|
|
|
for _, h := range hosts {
|
|
|
|
if ip := net.ParseIP(h); ip != nil {
|
|
|
|
template.IPAddresses = append(template.IPAddresses, ip)
|
|
|
|
} else {
|
|
|
|
template.DNSNames = append(template.DNSNames, h)
|
|
|
|
}
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
if *isCA {
|
|
|
|
template.IsCA = true
|
|
|
|
template.KeyUsage |= x509.KeyUsageCertSign
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|