-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
215 lines (204 loc) · 5.79 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"encoding/xml"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"
)
type TCert struct {
Data string `xml:"Данные"`
NotBefore string `xml:"ПериодДействияС"`
NotAfter string `xml:"ПериодДействияДо"`
}
type TKey struct {
Id string `xml:"ИдентификаторКлюча"`
Certs []TCert `xml:"Сертификаты>ДанныеСертификата"`
}
type TSWHWSystem struct {
Alias string `xml:"Псевдоним"`
Keys []TKey `xml:"КлючиУполномоченныхЛиц>Ключ"`
}
type TCertificateAuthority struct {
Name string `xml:"Название"`
SWHWSystems []TSWHWSystem `xml:"ПрограммноАппаратныеКомплексы>ПрограммноАппаратныйКомплекс"`
}
type TStat struct {
MaxSWHWs int
MaxKeys int
MaxCerts int
MaxSWHWsName string
MaxKeysName string
MaxCertsName string
}
func main() {
var caVersion, caDateString string
var caDate time.Time
var stat TStat
caCertsDir := flag.String("d", "certs", "Result directory")
caXMLfilename := flag.String("x", "CA.xml", "Gosuslugi XML file")
caFilename := flag.String("c", "ca-certificates.pem", "Full CA filename")
flag.Parse()
argsWithoutProg := flag.Args()
if len(argsWithoutProg) > 0 {
caXMLfilename = &argsWithoutProg[0]
}
if _fstat, err := os.Stat(*caCertsDir); os.IsNotExist(err) {
fmt.Printf("Directory: %s is not exist: %s\n", *caCertsDir, err.Error())
os.Exit(1)
} else {
if !_fstat.IsDir() {
fmt.Printf("File: %s is not directory\n", *caCertsDir)
os.Exit(1)
} else {
files, err := ioutil.ReadDir(*caCertsDir)
if err != nil {
fmt.Printf("Can't read directory: %s\n", *caCertsDir)
os.Exit(1)
} else if len(files) > 0 {
fmt.Printf("Directory: %s is not empty\n", *caCertsDir)
os.Exit(1)
}
}
}
caXML, err := os.Open(*caXMLfilename)
if err != nil {
fmt.Println("Error opening file:", err)
os.Exit(2)
}
defer caXML.Close()
caCerts, err := os.OpenFile(*caCertsDir+"/"+*caFilename, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
fmt.Println("Error opening file:", err)
os.Exit(2)
}
defer caCerts.Close()
decoder := xml.NewDecoder(caXML)
for {
t, err := decoder.Token()
if t == nil {
if err != io.EOF {
fmt.Printf("Error: %s\n", err.Error())
os.Exit(3)
}
break
}
switch _e := t.(type) {
case xml.StartElement:
_name := _e.Name.Local
switch _name {
case "Версия":
var v string
err := decoder.DecodeElement(&v, &_e)
if err != nil {
fmt.Printf("Decode Error: %s\n", err.Error())
os.Exit(4)
} else {
caVersion = v
fmt.Printf("%v\n", caVersion)
}
case "Дата":
var v string
err := decoder.DecodeElement(&v, &_e)
if err != nil {
fmt.Printf("Decode Error: %s\n", err.Error())
os.Exit(4)
} else {
caDate, err = time.Parse(time.RFC3339, v)
if err != nil {
fmt.Printf("Date parse Error: %s\n", err.Error())
os.Exit(4)
} else {
caDateString = v
fmt.Printf("%s %d\n", caDateString, int(caDate.Unix()))
}
}
case "УдостоверяющийЦентр":
var ca TCertificateAuthority
var cswhw, ckey, ccrt int
err := decoder.DecodeElement(&ca, &_e)
if err != nil {
fmt.Printf("Decode Error: %s\n", err.Error())
os.Exit(4)
} else {
fmt.Printf("%s\n", ca.Name)
for _, swhw := range ca.SWHWSystems {
fmt.Printf("\t%s\n", swhw.Alias)
cswhw++
for _, key := range swhw.Keys {
fmt.Printf("\t\t%s\n", key.Id)
ckey++
for _, crt := range key.Certs {
_name := fmt.Sprintf("%s_%s_%s", swhw.Alias, crt.NotBefore, crt.NotAfter)
_name = strings.Replace(_name, "«", "_", -1)
_name = strings.Replace(_name, "»", "_", -1)
_name = strings.Replace(_name, "\"", "_", -1)
_name = strings.Replace(_name, " ", "_", -1)
_name = strings.Replace(_name, ":", "-", -1)
_name = strings.Replace(_name, "(", "_", -1)
_name = strings.Replace(_name, ")", "_", -1)
if len([]byte(_name)) > 200 {
_name = _name[:200]
}
_name += ".pem"
_cert := "-----BEGIN CERTIFICATE-----\n"
for i := 76; i-76 < len(crt.Data); i += 76 {
if i > len(crt.Data) {
_cert += crt.Data[i-76:] + "\n"
} else {
_cert += crt.Data[i-76:i] + "\n"
}
}
_cert += "-----END CERTIFICATE-----\n"
_, err = caCerts.Write([]byte(_cert))
if err != nil {
fmt.Printf("Write Error: %s\n", err.Error())
os.Exit(4)
}
f, err := os.OpenFile(*caCertsDir+"/"+_name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
fmt.Println("Error opening file:", err)
os.Exit(4)
}
defer f.Close()
_, err = f.Write([]byte(_cert))
if err != nil {
fmt.Printf("Write Error: %s\n", err.Error())
os.Exit(4)
}
err = f.Sync()
if err != nil {
fmt.Printf("Write Error: %s\n", err.Error())
os.Exit(4)
}
f.Close()
fmt.Printf("\t\t\t%s : %s - %s\n", _name, crt.NotBefore, crt.NotAfter)
ccrt++
}
if stat.MaxCerts < ccrt {
stat.MaxCerts = ccrt
stat.MaxCertsName = ca.Name
ccrt = 0
}
}
if stat.MaxKeys < ckey {
stat.MaxKeys = ckey
stat.MaxKeysName = ca.Name
ckey = 0
}
}
if stat.MaxSWHWs < cswhw {
stat.MaxSWHWs = cswhw
stat.MaxSWHWsName = ca.Name
}
}
}
default:
//fmt.Printf("%v\n", _e)
}
}
fmt.Printf("\nMaxSWHW = %d (%s), MaxKeys = %d (%s), MaxCerts = %d (%s)\n", stat.MaxSWHWs, stat.MaxSWHWsName, stat.MaxKeys, stat.MaxKeysName, stat.MaxCerts, stat.MaxCertsName)
}