1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"fmt"
"os"
)
var oidEmailAddress = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}
func main() {
printCsr("email@example.com", "example.com")
}
//func Write(output *os.File) { output.Write }
func printCsr(emailAddress string, commonName string) {
keyBytes, _ := rsa.GenerateKey(rand.Reader, 1024)
//emailAddress := "test@example.com"
subj := pkix.Name{
CommonName: commonName,
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})
var f, err = os.Create(emailAddress + ".csr")
if nil != err {
fmt.Print("Failed to create file")
}
pem.Encode(f, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes})
}
|