-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathunpack.go
262 lines (233 loc) · 5.55 KB
/
unpack.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
256
257
258
259
260
261
262
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/lwithers/minijks/jks"
"github.com/urfave/cli/v2"
)
var UnpackCommand = &cli.Command{
Name: "unpack",
Usage: "unpack a keystore file into a directory",
ArgsUsage: "keystore.jks",
Action: Unpack,
}
func init() {
UnpackCommand.Flags = addJksOptsFlags(UnpackCommand.Flags)
}
func Unpack(c *cli.Context) error {
switch c.NArg() {
case 0:
cli.ShowSubcommandHelp(c)
return errors.New("need name of file to unpack")
case 1:
// OK
default:
return errors.New("can only unpack one file")
}
out := c.String("out")
if out == "" {
out = c.Args().Get(0) + ".d"
}
opts, err := jksOptsFlags(c)
if err != nil {
return err
}
return unpack(opts, c.Args().Get(0), out)
}
func unpack(opts *jks.Options, filename, outdir string) error {
raw, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
ks, err := jks.Parse(raw, opts)
// any error will be returned below, after unpacking ks
if ks != nil {
if err = os.MkdirAll(outdir, 0700); err != nil {
return err
}
// the directory must be empty
fi, err := ioutil.ReadDir(outdir)
if err != nil {
return err
}
if len(fi) != 0 {
return errors.New("output directory not empty")
}
if err := unpackInto(opts, ks, outdir); err != nil {
return err
}
}
return err
}
func unpackInto(opts *jks.Options, ks *jks.Keystore, outdir string) error {
var retErr error
reportErr := func(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
retErr = errors.New("error(s) encountered")
}
}
if opts == nil {
opts = &jks.Options{
SkipVerifyDigest: true,
}
}
// if we have a password, then save it
if !opts.SkipVerifyDigest {
err := unpackPassword(opts.Password, outdir, "password")
reportErr(err)
}
// save the certificates
usedFilenames := make(map[string]int)
for _, cert := range ks.Certs {
if cert.CertErr != nil {
reportErr(fmt.Errorf("certificate %q: %v",
cert.Alias, cert.CertErr))
continue
}
n := uniqueName(cert.Alias, usedFilenames)
fn, err := unpackCertificate(cert.Raw,
outdir, "certs", n+".pem")
reportErr(err)
_ = os.Chtimes(fn, time.Now(), cert.Timestamp) // errors ignored
}
// save the private keys
usedFilenames = make(map[string]int)
for _, kp := range ks.Keypairs {
if kp.PrivKeyErr != nil {
reportErr(fmt.Errorf("keypair %q: %v",
kp.Alias, kp.PrivKeyErr))
continue
}
// save the private key itself
n := uniqueName(kp.Alias, usedFilenames)
fn, err := unpackPrivateKey(kp.PrivateKey, outdir, "keys", n,
"privkey.pem")
reportErr(err)
_ = os.Chtimes(fn, time.Now(), kp.Timestamp) // errors ignored
// if there is a specific password for this key, save it
passwd, ok := opts.KeyPasswords[kp.Alias]
if ok {
err = unpackPassword(passwd, outdir, "keys", n,
"password")
reportErr(err)
}
// save the certificate chain
for i, cert := range kp.CertChain {
_, err = unpackCertificate(cert.Raw, outdir, "keys", n,
fmt.Sprintf("cert-%04d.pem", i+1))
reportErr(err)
}
}
return retErr
}
func unpackPassword(password string, pathParts ...string) error {
fn, f, err := unpackOpen(0600, pathParts...)
if err != nil {
return err
}
if _, err = fmt.Fprintf(f, "%s\n", password); err != nil {
_ = f.Close() // ignore errors; return orig err only
_ = os.Remove(fn)
return err
}
if err = f.Close(); err != nil {
_ = os.Remove(fn)
return err
}
return nil
}
func unpackCertificate(der []byte, pathParts ...string) (string, error) {
fn, f, err := unpackOpen(0666, pathParts...)
if err != nil {
return "", err
}
if err = pem.Encode(f, &pem.Block{
Type: "CERTIFICATE",
Bytes: der,
}); err != nil {
_ = f.Close() // ignore errors; return orig err only
_ = os.Remove(fn)
return "", err
}
if err = f.Close(); err != nil {
_ = os.Remove(fn)
return "", err
}
return fn, nil
}
func unpackPrivateKey(key interface{}, pathParts ...string) (string, error) {
var (
err error
block pem.Block
)
switch key := key.(type) {
case *rsa.PrivateKey:
block.Type = "RSA PRIVATE KEY"
block.Bytes = x509.MarshalPKCS1PrivateKey(key)
case *ecdsa.PrivateKey:
block.Type = "EC PRIVATE KEY"
block.Bytes, err = x509.MarshalECPrivateKey(key)
if err != nil {
return "", err
}
default:
return "", fmt.Errorf("unknown private key type %T", key)
}
fn, f, err := unpackOpen(0600, pathParts...)
if err != nil {
return "", err
}
if err = pem.Encode(f, &block); err != nil {
_ = f.Close() // ignore errors; return orig err only
_ = os.Remove(fn)
return "", err
}
if err = f.Close(); err != nil {
_ = os.Remove(fn)
return "", err
}
return fn, nil
}
func uniqueName(in string, used map[string]int) string {
// only allow alphanumeric and a couple of specific punctuation chars
var b bytes.Buffer
for _, r := range in {
switch {
case r >= 'A' && r <= 'Z',
r >= 'a' && r <= 'z',
r >= '0' && r <= '9',
r == '.' && b.Len() != 0,
r == '-', r == '_':
b.WriteRune(r)
}
}
if b.Len() == 0 {
b.WriteString("XXX")
}
out := b.String()
if used[out] > 0 {
out = fmt.Sprintf("%s.%d", out, used[out])
}
used[out] = used[out] + 1
return out
}
func unpackOpen(mode os.FileMode, buildPath ...string,
) (filename string, f *os.File, err error) {
filename = filepath.Join(buildPath...)
dirname := filepath.Dir(filename)
if err = os.MkdirAll(dirname, 0777); err != nil {
return filename, nil, err
}
f, err = os.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, mode)
return filename, f, err
}