-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (96 loc) · 2.74 KB
/
main.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
var oidEmailAddress = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}
type Infile struct {
Host string `yaml:"Host"`
EMail string `yaml:"EMail"`
CName string `yaml:"CName"`
COuntry string `yaml:"COuntry"`
STate string `yaml:"STate"`
CIty string `yaml:"CIty"`
O string `yaml:"O"`
OU string `yaml:"OU"`
}
func main() {
var infile Infile
DefaultInFile := "~/file.yml"
DefaultOutputDir := "/tmp/"
File := flag.String("f", DefaultInFile, fmt.Sprintf("Path for file, default = %s", DefaultInFile))
OutputDir := flag.String("o", DefaultOutputDir, fmt.Sprintf("Output directory, default = %s", DefaultOutputDir))
flag.Parse()
// Create directory if not exist.
if err := os.Mkdir(*OutputDir, 0755); err != nil && !os.IsExist(err) {
fmt.Println("Error to create directory.")
}
F, err := ioutil.ReadFile(*File)
if err != nil {
fmt.Println("Error to get file.")
}
err = yaml.Unmarshal(F, &infile)
if err != nil {
fmt.Println("YAML error.")
}
// Create private key
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println("Can't generate Key.")
}
var outKey = *OutputDir + infile.Host + ".key"
keyOut, err := os.OpenFile(outKey, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
fmt.Println("Can't write Key file.")
}
defer keyOut.Close()
if err := pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}); err != nil {
fmt.Println("PEM error for Key.")
}
// Create template for CSR
emailAddress := infile.EMail
subj := pkix.Name{
CommonName: infile.CName,
Country: []string{infile.COuntry},
Province: []string{infile.STate},
Locality: []string{infile.CIty},
Organization: []string{infile.O},
OrganizationalUnit: []string{infile.OU},
ExtraNames: []pkix.AttributeTypeAndValue{
{
Type: oidEmailAddress,
Value: asn1.RawValue{
Tag: asn1.TagIA5String,
Bytes: []byte(emailAddress),
},
},
},
}
template := x509.CertificateRequest{
Subject: subj,
SignatureAlgorithm: x509.SHA256WithRSA,
}
// Create CSR
var outCSR = *OutputDir + infile.Host + ".csr"
csrOut, err := os.OpenFile(outCSR, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
fmt.Println("Can't write CSR file.")
}
defer csrOut.Close()
csr, err := x509.CreateCertificateRequest(rand.Reader, &template, priv)
if err != nil {
fmt.Println("Can't create CSR.")
}
if err := pem.Encode(csrOut, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csr}); err != nil {
fmt.Println("PEM error for CSR.")
}
}