forked from FiloSottile/mkcert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guide.go
200 lines (171 loc) · 4.23 KB
/
guide.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
package main
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/sha1"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"log"
"os"
"strings"
"time"
)
type Guide struct {
prompt Prompt
}
func guideRun() {
(&Guide{&prompt{}}).Run()
}
func (g *Guide) Run() {
//检查是否已经拥有根证书
m := mkcert{}
m.CAROOT = "./"
if caInit {
loadCA(&m)
// 母客户端菜单
rootIndex := g.prompt.RootMenu()
//生成子证书
if rootIndex == 0 {
m.makeCert(g.prompt.InputHost())
} else if rootIndex == 1 { //导出根证书
keyContent, _ := selffs.ReadFile(rootKeyName)
os.WriteFile(rootKeyName, keyContent, 0666)
pemContent, _ := selffs.ReadFile(rootName)
os.WriteFile(rootName, pemContent, 0666)
log.Println(i18nText.
scan46,
)
} else if rootIndex == 2 { //导出客户端
//生成html或客户端
//解压客户端
os.Mkdir("dist", os.ModePerm)
files, err := staticFs.ReadDir("dist")
if err != nil {
panic(err)
}
for _, file := range files {
if !file.IsDir() {
b, _ := staticFs.ReadFile("dist/" + file.Name())
if strings.Contains(file.Name(), "certClient") {
//根证书公钥文件注入
var buffer bytes.Buffer
buffer.Write(b)
b2, _ := selffs.ReadFile(rootName)
buffer.Write(b2)
buffer.Write(IntToBytes(len(b2)))
buffer.Write([]byte("selffs"))
os.WriteFile("dist/"+file.Name(), buffer.Bytes(), os.ModePerm)
} else {
os.WriteFile("dist/"+file.Name(), b, os.ModePerm)
}
}
}
b2, _ := selffs.ReadFile(rootName)
os.WriteFile("dist/"+rootName, b2, os.ModePerm)
log.Println(i18nText.
scan47,
)
}
//退出
} else {
if g.prompt.GenRootCert() {
newCA(m)
loadCA(&m)
division()
if m.checkPlatform() {
log.Print(i18nText.
scan48,
)
} else {
m.ignoreCheckFailure = true // TODO: replace with a check for a successful install
}
}
}
}
// 初始化ca
func newCA(m mkcert) {
priv, err := m.generateKey(true)
fatalIfErr(err, i18nMkcertText.failedGenCaKey)
pub := priv.(crypto.Signer).Public()
spkiASN1, err := x509.MarshalPKIXPublicKey(pub)
fatalIfErr(err, i18nMkcertText.failedEnCodePublicKey)
var spki struct {
Algorithm pkix.AlgorithmIdentifier
SubjectPublicKey asn1.BitString
}
_, err = asn1.Unmarshal(spkiASN1, &spki)
fatalIfErr(err, i18nMkcertText.failedDeCodePublicKey)
skid := sha1.Sum(spki.SubjectPublicKey.Bytes)
tpl := &x509.Certificate{
SerialNumber: randomSerialNumber(),
Subject: pkix.Name{
Organization: []string{"mkcert development CA"},
OrganizationalUnit: []string{userAndHostname},
// The CommonName is required by iOS to show the certificate in the
// "Certificate Trust Settings" menu.
// https://github.com/FiloSottile/mkcert/issues/47
CommonName: "mkcert " + userAndHostname,
},
SubjectKeyId: skid[:],
NotAfter: time.Now().AddDate(10, 0, 0),
NotBefore: time.Now(),
KeyUsage: x509.KeyUsageCertSign,
BasicConstraintsValid: true,
IsCA: true,
MaxPathLenZero: true,
}
cert, err := x509.CreateCertificate(rand.Reader, tpl, tpl, pub, priv)
fatalIfErr(err, i18nText.
scan41,
)
privDER, err := x509.MarshalPKCS8PrivateKey(priv)
fatalIfErr(err, i18nText.
scan42,
)
selffs.WriteFile(rootKeyName, string(pem.EncodeToMemory(
&pem.Block{Type: "PRIVATE KEY", Bytes: privDER})))
fatalIfErr(err, i18nText.
scan43,
)
selffs.WriteFile(rootName, string(pem.EncodeToMemory(
&pem.Block{Type: "CERTIFICATE", Bytes: cert})))
fatalIfErr(err, i18nText.
scan44,
)
log.Printf(i18nText.
scan49,
)
}
func loadCA(m *mkcert) {
certPEMBlock, err := selffs.ReadFile(rootName)
fatalIfErr(err, i18nText.
scan32,
)
certDERBlock, _ := pem.Decode(certPEMBlock)
if certDERBlock == nil || certDERBlock.Type != "CERTIFICATE" {
log.Fatalln(i18nText.
scan33,
)
}
m.caCert, err = x509.ParseCertificate(certDERBlock.Bytes)
fatalIfErr(err, i18nText.
scan34,
)
keyPEMBlock, err := selffs.ReadFile(rootKeyName)
fatalIfErr(err, i18nText.
scan35,
)
keyDERBlock, _ := pem.Decode(keyPEMBlock)
if keyDERBlock == nil || keyDERBlock.Type != "PRIVATE KEY" {
log.Fatalln(i18nText.
scan36,
)
}
m.caKey, err = x509.ParsePKCS8PrivateKey(keyDERBlock.Bytes)
fatalIfErr(err, i18nText.
scan37,
)
}