package main import ( "crypto/rand" "crypto/rsa" "crypto/x509" "crypto/x509/pkix" "encoding/pem" "html/template" "math/big" "net/http" "os" "time" ) func genPem() { privateKey, err := rsa.GenerateKey(rand.Reader, 2048) checkError(err) SNLimit := new(big.Int).Lsh(big.NewInt(1), 128) SN, err := rand.Int(rand.Reader, SNLimit) checkError(err) template := x509.Certificate{ SerialNumber: SN, Subject: pkix.Name{ Organization: []string{"test"}, }, NotBefore: time.Now(), NotAfter: time.Now().Add(365 * 24 * time.Hour), KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, BasicConstraintsValid: true, } template.DNSNames = append(template.DNSNames, "localhost") template.EmailAddresses = append(template.EmailAddresses, "test@test.com") certBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey) checkError(err) certFile, err := os.Create("cert.pem") checkError(err) checkError(pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes})) checkError(certFile.Close()) keyFile, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) checkError(err) // pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv.(*rsa.PrivateKey))}) checkError(pem.Encode(keyFile, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})) checkError(keyFile.Close()) } func web(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { t, _ := template.ParseFiles("sfu.html") checkError(t.Execute(w, nil)) } }