Skip to content

Instantly share code, notes, and snippets.

@druzn3k
Forked from svicknesh/csr.go
Created November 9, 2022 14:49
Show Gist options
  • Save druzn3k/e6d22858a94a0e8d915fc85d453c63e1 to your computer and use it in GitHub Desktop.
Save druzn3k/e6d22858a94a0e8d915fc85d453c63e1 to your computer and use it in GitHub Desktop.
Golang - create CSR (from stack overflow)
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"os"
)
var oidEmailAddress = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}
func main() {
keyBytes, _ := rsa.GenerateKey(rand.Reader, 1024)
emailAddress := "test@example.com"
subj := pkix.Name{
CommonName: "example.com",
Country: []string{"AU"},
Province: []string{"Some-State"},
Locality: []string{"MyCity"},
Organization: []string{"Company Ltd"},
OrganizationalUnit: []string{"IT"},
}
rawSubj := subj.ToRDNSequence()
rawSubj = append(rawSubj, []pkix.AttributeTypeAndValue{
{Type: oidEmailAddress, Value: emailAddress},
})
asn1Subj, _ := asn1.Marshal(rawSubj)
template := x509.CertificateRequest{
RawSubject: asn1Subj,
EmailAddresses: []string{emailAddress},
SignatureAlgorithm: x509.SHA256WithRSA,
}
csrBytes, _ := x509.CreateCertificateRequest(rand.Reader, &template, keyBytes)
pem.Encode(os.Stdout, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment