2020-10-12 20:34:52 +00:00
|
|
|
// +build ignore
|
2020-09-27 17:50:48 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-13 21:33:14 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2020-09-27 17:50:48 +00:00
|
|
|
"log"
|
2020-10-13 20:50:59 +00:00
|
|
|
"os"
|
2020-09-28 03:49:41 +00:00
|
|
|
"time"
|
2020-09-27 17:50:48 +00:00
|
|
|
|
2020-09-28 22:19:59 +00:00
|
|
|
"git.sr.ht/~adnano/gmi"
|
2020-09-27 17:50:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
host := "localhost"
|
2020-10-13 20:50:59 +00:00
|
|
|
duration := 365 * 24 * time.Hour
|
2020-10-13 21:33:14 +00:00
|
|
|
cert, err := gmi.NewCertificate(host, duration)
|
2020-09-27 17:50:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-13 21:33:14 +00:00
|
|
|
if err := writeCertificate(host, cert); err != nil {
|
2020-09-27 17:50:48 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 20:50:59 +00:00
|
|
|
|
2020-10-13 21:33:14 +00:00
|
|
|
// writeCertificate writes the provided certificate and private key
|
2020-10-13 20:50:59 +00:00
|
|
|
// to path.crt and path.key respectively.
|
2020-10-13 21:33:14 +00:00
|
|
|
func writeCertificate(path string, cert tls.Certificate) error {
|
|
|
|
crt, err := marshalX509Certificate(cert.Leaf.Raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key, err := marshalPrivateKey(cert.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-13 20:50:59 +00:00
|
|
|
// Write the certificate
|
|
|
|
crtPath := path + ".crt"
|
|
|
|
crtOut, err := os.OpenFile(crtPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := crtOut.Write(crt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the private key
|
|
|
|
keyPath := path + ".key"
|
|
|
|
keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := keyOut.Write(key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-13 21:33:14 +00:00
|
|
|
|
|
|
|
// marshalX509Certificate returns a PEM-encoded version of the given raw certificate.
|
|
|
|
func marshalX509Certificate(cert []byte) ([]byte, error) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := pem.Encode(&b, &pem.Block{Type: "CERTIFICATE", Bytes: cert}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshalPrivateKey returns PEM encoded versions of the given certificate and private key.
|
|
|
|
func marshalPrivateKey(priv interface{}) ([]byte, error) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := pem.Encode(&b, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|