Created
February 5, 2023 01:53
-
-
Save veggiemonk/3280ff71ed044e8b8f2fa207642702ef to your computer and use it in GitHub Desktop.
Generate a self-signed certificate in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"crypto/ecdsa" | |
"crypto/elliptic" | |
"crypto/rand" | |
"crypto/rsa" | |
"crypto/x509" | |
"crypto/x509/pkix" | |
"encoding/pem" | |
"fmt" | |
"log" | |
"math/big" | |
"os" | |
"time" | |
) | |
func publicKey(priv interface{}) interface{} { | |
switch k := priv.(type) { | |
case *rsa.PrivateKey: | |
return &k.PublicKey | |
case *ecdsa.PrivateKey: | |
return &k.PublicKey | |
default: | |
return nil | |
} | |
} | |
func pemBlockForKey(priv interface{}) *pem.Block { | |
switch k := priv.(type) { | |
case *rsa.PrivateKey: | |
return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)} | |
case *ecdsa.PrivateKey: | |
b, err := x509.MarshalECPrivateKey(k) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Unable to marshal ECDSA private key: %v", err) | |
os.Exit(2) | |
} | |
return &pem.Block{Type: "EC PRIVATE KEY", Bytes: b} | |
default: | |
return nil | |
} | |
} | |
func main() { | |
// priv, err := rsa.GenerateKey(rand.Reader, *rsaBits) | |
priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) | |
if err != nil { | |
log.Fatal(err) | |
} | |
template := x509.Certificate{ | |
SerialNumber: big.NewInt(1), | |
Subject: pkix.Name{ | |
Organization: []string{"Acme Co"}, | |
}, | |
NotBefore: time.Now(), | |
NotAfter: time.Now().Add(time.Hour * 24 * 180), | |
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) | |
} | |
} | |
if *isCA { | |
template.IsCA = true | |
template.KeyUsage |= x509.KeyUsageCertSign | |
} | |
*/ | |
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv) | |
if err != nil { | |
log.Fatalf("Failed to create certificate: %s", err) | |
} | |
out := &bytes.Buffer{} | |
pem.Encode(out, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) | |
fmt.Println(out.String()) | |
out.Reset() | |
pem.Encode(out, pemBlockForKey(priv)) | |
fmt.Println(out.String()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment