-
-
Save druzn3k/e6d22858a94a0e8d915fc85d453c63e1 to your computer and use it in GitHub Desktop.
Golang - create CSR (from stack overflow)
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 ( | |
"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 := "[email protected]" | |
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