-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx509_lists_gen.go
166 lines (142 loc) · 3.28 KB
/
x509_lists_gen.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
//go:build ignore
// +build ignore
package main
import (
"bytes"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
)
const (
outputFile = "x509_lists.go"
)
var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
package pcert
import (
"crypto/x509"
)
var KeyUsages = map[string]x509.KeyUsage{
{{- range $key, $value := .KeyUsage }}
{{ printf "%q" $key }}: x509.{{ $value }},
{{- end }}
}
var ExtKeyUsages = map[string]x509.ExtKeyUsage{
{{- range $key, $value := .ExtKeyUsage }}
{{ printf "%q" $key }}: x509.{{ $value }},
{{- end }}
}
var SignatureAlgorithms = []x509.SignatureAlgorithm{
{{- range $key, $value := .SignAlgs }}
x509.{{ $value }},
{{- end }}
}
`))
func main() {
// detect GOROOT
goroot := os.Getenv("GOROOT")
if goroot == "" {
cmd := exec.Command("go", "env", "GOROOT")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
goroot = strings.TrimSpace(string(out))
}
x509SrcFile := filepath.Join(goroot, "src/crypto/x509/x509.go")
usage, err := getEnum("KeyUsage", x509SrcFile)
if err != nil {
log.Fatal(err)
}
extUsage, err := getEnum("ExtKeyUsage", x509SrcFile)
if err != nil {
log.Fatal(err)
}
signAlgs, err := getEnum("SignatureAlgorithm", x509SrcFile)
if err != nil {
log.Fatal(err)
}
usageStringMapping := generateStringMapping("KeyUsage", usage)
extUsageStringMapping := generateStringMapping("ExtKeyUsage", extUsage)
signAlgMapping := generateSignatureAlgorithmMapping(signAlgs)
output := &bytes.Buffer{}
err = packageTemplate.Execute(output, struct {
KeyUsage map[string]string
ExtKeyUsage map[string]string
SignAlgs map[string]string
}{
KeyUsage: usageStringMapping,
ExtKeyUsage: extUsageStringMapping,
SignAlgs: signAlgMapping,
})
if err != nil {
log.Fatal(err)
}
formattedOutput, err := format.Source(output.Bytes())
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(outputFile, formattedOutput, 0o644)
if err != nil {
log.Fatal(err)
}
}
func generateSignatureAlgorithmMapping(names []string) map[string]string {
out := map[string]string{}
for _, name := range names {
if strings.Contains(name, "Unknown") {
continue
}
newName := strings.ReplaceAll(name, "With", "-")
out[newName] = name
}
return out
}
func generateStringMapping(typeName string, names []string) map[string]string {
out := map[string]string{}
for _, name := range names {
newName := name[len(typeName):]
out[newName] = name
}
return out
}
func getEnum(typ, file string) ([]string, error) {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, file, nil, 0)
if err != nil {
return nil, err
}
var names []string
ast.Inspect(node, func(n ast.Node) bool {
decl, ok := n.(*ast.GenDecl)
if !ok || decl.Tok != token.CONST {
return true
}
if len(decl.Specs) == 0 {
return true
}
vspec := decl.Specs[0].(*ast.ValueSpec)
ident, ok := vspec.Type.(*ast.Ident)
if !ok {
return true
}
if ident.Name != typ {
return true
}
for _, spec := range decl.Specs {
vspec := spec.(*ast.ValueSpec) // Guaranteed to succeed as this is CONST.
for _, n := range vspec.Names {
names = append(names, n.Name)
}
}
return true
})
return names, nil
}