/
utils.go
143 lines (133 loc) · 3.4 KB
/
utils.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package utils
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/base64"
"encoding/csv"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
)
func WriteFile(file *os.File, data [][]string) error {
writer := csv.NewWriter(file)
err := writer.WriteAll(data)
if err != nil {
return err
}
return nil
}
func GenrateRandKey(id string) ([]byte, *x509.CertificateRequest, error) {
rsaKey, _ := rsa.GenerateKey(rand.Reader, 2048)
privKey, err := x509.MarshalPKCS8PrivateKey(rsaKey)
if err != nil {
return nil, nil, err
}
csr, err := GenerateCSR(rsaKey, "rsa", id)
if err != nil {
return nil, nil, err
}
return privKey, csr, nil
}
func GenerateCSR(key interface{}, Keytype string, id string) (*x509.CertificateRequest, error) {
subj := pkix.Name{
Country: []string{"ES"},
Province: []string{"Gipuzkoa"},
Organization: []string{"IKERLAN"},
OrganizationalUnit: []string{"ZPD"},
Locality: []string{"Arrasate"},
CommonName: id,
}
rawSubject := subj.ToRDNSequence()
asn1Subj, _ := asn1.Marshal(rawSubject)
var template x509.CertificateRequest
if Keytype == "rsa" {
template = x509.CertificateRequest{
RawSubject: asn1Subj,
SignatureAlgorithm: x509.SHA512WithRSA,
}
} else {
template = x509.CertificateRequest{
RawSubject: asn1Subj,
SignatureAlgorithm: x509.ECDSAWithSHA512,
}
}
csrBytes, err := x509.CreateCertificateRequest(rand.Reader, &template, key)
if err != nil {
return nil, fmt.Errorf("failed to create certificate request: %v", err)
}
csrNew, err := x509.ParseCertificateRequest(csrBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse certificate request: %v", err)
}
return csrNew, nil
}
func WriteDataFile(id string, max float64, min float64, media float64, file *os.File) error {
var data = [][]string{
{id, fmt.Sprint(max), fmt.Sprint(min), fmt.Sprint(media)},
}
err := WriteFile(file, data)
if err != nil {
return err
}
return nil
}
func StringToCSR(s string) (*x509.CertificateRequest, error) {
csr2Str := s
data2, _ := base64.StdEncoding.DecodeString(csr2Str)
block2, _ := pem.Decode([]byte(data2))
c, err := x509.ParseCertificateRequest(block2.Bytes)
return c, err
}
func InsertCert(path string, data []byte) {
f, _ := os.Create(path)
b := pem.Block{Type: "CERTIFICATE", Bytes: data}
certPEM := pem.EncodeToMemory(&b)
f.Write(certPEM)
f.Close()
}
func InsertKey(path string, data []byte) {
f, _ := os.Create(path)
b := pem.Block{Type: "PRIVATE KEY", Bytes: data}
keyPEM := pem.EncodeToMemory(&b)
f.Write(keyPEM)
f.Close()
}
func InsertCsr(path string, data []byte) {
f, _ := os.Create(path)
b := pem.Block{Type: "CERTIFICATE REQUEST", Bytes: data}
certPEM := pem.EncodeToMemory(&b)
f.Write(certPEM)
f.Close()
}
func ReadCertPool(path string) (*x509.CertPool, error) {
caCert, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
return caCertPool, nil
}
func ReadCert(path string) (*x509.Certificate, error) {
certContent, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
cpb, _ := pem.Decode(certContent)
crt, err := x509.ParseCertificate(cpb.Bytes)
if err != nil {
return nil, err
}
return crt, nil
}
func ReadKey(path string) ([]byte, error) {
key, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return key, nil
}