-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
smtongsuo.go
255 lines (210 loc) · 7.24 KB
/
smtongsuo.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package crypto
import (
"bytes"
"context"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/Laisky/errors/v2"
"github.com/Laisky/zap"
gutils "github.com/Laisky/go-utils/v4"
glog "github.com/Laisky/go-utils/v4/log"
)
// Tongsuo is a wrapper of tongsuo executable binary
//
// https://github.com/Tongsuo-Project/Tongsuo
type Tongsuo struct {
exePath string
serialGenerator *DefaultX509CertSerialNumGenerator
}
// NewTongsuo new tongsuo wrapper
//
// #Args
// - exePath: path of tongsuo executable binary
func NewTongsuo(exePath string) (ins *Tongsuo, err error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
ins = &Tongsuo{exePath: exePath}
// check tongsuo executable binary
if out, err := ins.runCMD(ctx, []string{"version"}, nil); err != nil {
return nil, errors.Wrapf(err, "run `%s version` failed", exePath)
} else if !strings.Contains(string(out), "Tongsuo") {
return nil, errors.Errorf("only support Tongsuo")
}
// new serial number generator
if ins.serialGenerator, err = NewDefaultX509CertSerialNumGenerator(); err != nil {
return nil, errors.Wrap(err, "new serial number generator")
}
return ins, nil
}
func (t *Tongsuo) runCMD(ctx context.Context, args []string, stdin []byte) (output []byte, err error) {
if args, err = gutils.SanitizeCMDArgs(args); err != nil {
return nil, errors.Wrap(err, "sanitize cmd args")
}
//nolint: gosec
// G204: Subprocess launched with a potential tainted input or cmd arguments
cmd := exec.CommandContext(ctx, t.exePath, args...)
if len(stdin) != 0 {
var stdinBuf bytes.Buffer
stdinBuf.Write(stdin)
cmd.Stdin = &stdinBuf
}
if output, err = cmd.CombinedOutput(); err != nil {
return nil, errors.Wrapf(err, "run cmd failed, got %s", output)
}
return output, nil
}
// ShowCertInfo show cert info
func (t *Tongsuo) ShowCertInfo(ctx context.Context, certDer []byte) (output string, err error) {
out, err := t.runCMD(ctx, []string{"x509", "-inform", "DER", "-text"}, certDer)
if err != nil {
return "", errors.Wrap(err, "run cmd to show cert info")
}
return string(out), nil
}
// ShowCsrInfo show csr info
func (t *Tongsuo) ShowCsrInfo(ctx context.Context, csrDer []byte) (output string, err error) {
out, err := t.runCMD(ctx, []string{"req", "-inform", "DER", "-text"}, csrDer)
if err != nil {
return "", errors.Wrap(err, "run cmd to show csr info")
}
return string(out), nil
}
// NewPrikey generate new sm2 private key
func (t *Tongsuo) NewPrikey(ctx context.Context) (prikeyPem []byte, err error) {
prikeyPem, err = t.runCMD(ctx, []string{
"genpkey", "-outform", "PEM", "-algorithm", "SM2",
}, nil)
if err != nil {
return nil, errors.Wrap(err, "generate new private key")
}
return prikeyPem, nil
}
func (t *Tongsuo) removeAll(path string) {
if err := os.RemoveAll(path); err != nil {
glog.Shared.Error("remove dir", zap.String("path", path), zap.Error(err))
}
}
// NewPrikeyAndCert generate new private key and root ca
func (t *Tongsuo) NewPrikeyAndCert(ctx context.Context, opts ...X509CertOption) (prikeyPem, certDer []byte, err error) {
// new private key
if prikeyPem, err = t.NewPrikey(ctx); err != nil {
return nil, nil, errors.Wrap(err, "new private key")
}
opt, tpl, err := x509CertOption2Template(opts...)
if err != nil {
return nil, nil, errors.Wrap(err, "X509CertOption2Template")
}
opensslConf := X509Cert2OpensslConf(tpl)
dir, err := os.MkdirTemp("", "tongsuo*")
if err != nil {
return nil, nil, errors.Wrap(err, "generate tem dir")
}
defer t.removeAll(dir)
// write conf
confPath := filepath.Join(dir, "rootca.cnf")
if err = os.WriteFile(confPath, opensslConf, 0600); err != nil {
return nil, nil, errors.Wrap(err, "write openssl conf")
}
outCertPemPath := filepath.Join(dir, "rootca.pem")
// new root ca
if _, err = t.runCMD(ctx, []string{
"req", "-outform", "PEM", "-out", outCertPemPath,
"-key", "/dev/stdin",
"-set_serial", strconv.Itoa(int(t.serialGenerator.SerialNum())),
"-days", strconv.Itoa(int(time.Until(opt.notAfter) / time.Hour / 24)),
"-x509", "-new", "-nodes", "-utf8", "-batch",
"-sm3", "-sigopt", "sm2-za:no",
"-copy_extensions", "copyall",
"-extensions", "v3_ca",
"-config", confPath,
}, prikeyPem); err != nil {
return nil, nil, errors.Wrap(err, "generate new root ca")
}
certPem, err := os.ReadFile(outCertPemPath)
if err != nil {
return nil, nil, errors.Wrap(err, "read root ca")
}
if certDer, err = Pem2Der(certPem); err != nil {
return nil, nil, errors.Wrap(err, "Pem2Der")
}
return prikeyPem, certDer, nil
}
// NewX509CSR generate new x509 csr
func (t *Tongsuo) NewX509CSR(ctx context.Context, prikeyPem []byte, opts ...X509CSROption) (csrDer []byte, err error) {
dir, err := os.MkdirTemp("", "tongsuo*")
if err != nil {
return nil, errors.Wrap(err, "generate tem dir")
}
defer t.removeAll(dir)
tpl, err := X509CsrOption2Template(opts...)
if err != nil {
return nil, errors.Wrap(err, "X509CsrOption2Template")
}
opensslConf := X509Csr2OpensslConf(tpl)
confPath := filepath.Join(dir, "csr.cnf")
if err = os.WriteFile(confPath, opensslConf, 0600); err != nil {
return nil, errors.Wrap(err, "write openssl conf")
}
outCsrDerPath := filepath.Join(dir, "csr.der")
if _, err = t.runCMD(ctx, []string{
"req", "-new", "-outform", "DER", "-out", outCsrDerPath,
"-key", "/dev/stdin",
"-sm3", "-sigopt", "sm2-za:no",
"-config", confPath,
}, prikeyPem); err != nil {
return nil, errors.Wrap(err, "generate new csr")
}
if csrDer, err = os.ReadFile(outCsrDerPath); err != nil {
return nil, errors.Wrap(err, "read csr")
}
return csrDer, nil
}
// NewX509CertByCSR generate new x509 cert by csr
func (t *Tongsuo) NewX509CertByCSR(ctx context.Context,
parentCertDer []byte,
parentPrikeyPem []byte,
csrDer []byte,
opts ...SignCSROption) (certDer []byte, err error) {
opt, opensslConf, err := x509SignCsrOptions2OpensslConf(opts...)
if err != nil {
return nil, errors.Wrap(err, "X509SignCsrOptions2OpensslConf")
}
dir, err := os.MkdirTemp("", "tongsuo*")
if err != nil {
return nil, errors.Wrap(err, "generate tem dir")
}
defer t.removeAll(dir)
confPath := filepath.Join(dir, "csr.cnf")
if err = os.WriteFile(confPath, opensslConf, 0600); err != nil {
return nil, errors.Wrap(err, "write openssl conf")
}
parentCertDerPath := filepath.Join(dir, "ca.der")
if err = os.WriteFile(parentCertDerPath, parentCertDer, 0600); err != nil {
return nil, errors.Wrap(err, "write parent cert")
}
csrDerPath := filepath.Join(dir, "csr.der")
if err = os.WriteFile(csrDerPath, csrDer, 0600); err != nil {
return nil, errors.Wrap(err, "write csr")
}
outCertDerPath := filepath.Join(dir, "cert.der")
if _, err = t.runCMD(ctx, []string{
"x509", "-req", "-outform", "DER", "-out", outCertDerPath,
"-in", csrDerPath, "-inform", "DER",
"-CA", parentCertDerPath, "-CAkey", "/dev/stdin", "-CAcreateserial",
"-days", strconv.Itoa(int(time.Until(opt.notAfter) / time.Hour / 24)),
"-utf8", "-batch",
"-sm3", "-sigopt", "sm2-za:no", "-vfyopt", "sm2-za:no",
"-copy_extensions", "copyall",
"-extfile", confPath, "-extensions", "v3_ca",
}, parentPrikeyPem); err != nil {
return nil, errors.Wrap(err, "sign csr")
}
if certDer, err = os.ReadFile(outCertDerPath); err != nil {
return nil, errors.Wrap(err, "read signed cert")
}
return certDer, nil
}