-
Notifications
You must be signed in to change notification settings - Fork 16
/
pass.go
238 lines (207 loc) · 4.95 KB
/
pass.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
package pkpass
import (
"archive/zip"
"bytes"
"crypto/sha1"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)
// New will create a new Apple pass given the directory of companion files, the
// password needed to open the certificate, and the certificate. You should read
// the returned reader into a file, this file is your Apple pass and can be opened
// from iOS and macOS devices.
func New(passDir, password string, cert io.Reader) (io.Reader, error) {
// Create a temporary directory that we will use as a scratch pad for our
// openssl commands.
tempDir, err := ioutil.TempDir("", "")
if err != nil {
return nil, err
}
defer os.RemoveAll(tempDir)
// Copy the certificate into a file so that it can be used by
// os.Exec
c, err := os.Create(fmt.Sprintf("%s/certificates.p12", tempDir))
if err != nil {
return nil, err
}
defer c.Close()
_, err = io.Copy(c, cert)
if err != nil {
return nil, err
}
// Create the certificate.pem file
err = pem(tempDir, password, cert)
if err != nil {
return nil, err
}
// Create the key.pem file
err = key(tempDir, password, cert)
if err != nil {
return nil, err
}
// Create a new zip buffer that we will use to produce
// the final output
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
defer w.Close()
// Create the bundle of files, this will include everything
// in the directory
err = bundle(w, passDir, tempDir)
if err != nil {
return nil, err
}
// Sign the manifest.json
err = sign(w, tempDir, password)
if err != nil {
return nil, err
}
return buf, nil
}
// key will generate the private key that is needed in the openssl smime command
func key(tempDir, password string, cert io.Reader) error {
cmd := exec.Command(
"openssl",
"pkcs12",
"-in",
fmt.Sprintf("%s/certificates.p12", tempDir),
"-nocerts",
"-out",
fmt.Sprintf("%s/key.pem", tempDir),
"-passin",
fmt.Sprintf("pass:%s", password),
"-passout",
fmt.Sprintf("pass:%s1234", password),
)
return cmd.Run()
}
// key will generate the certificate's pem file that is needed in the openssl smime command
func pem(tempDir, password string, cert io.Reader) error {
cmd := exec.Command(
"openssl",
"pkcs12",
"-in",
fmt.Sprintf("%s/certificates.p12", tempDir),
"-clcerts",
"-nokeys",
"-out",
fmt.Sprintf("%s/certificate.pem", tempDir),
"-passin",
fmt.Sprintf("pass:%s", password),
)
return cmd.Run()
}
// bundle will read all of the files in the passDir, create a manifest.json, and
// add all files to the zip archive.
func bundle(w *zip.Writer, passDir, tempDir string) error {
files, err := ioutil.ReadDir(passDir)
if err != nil {
return err
}
var m = make(map[string]string)
for _, fi := range files {
// Skip directories, they are meaningless in this situation
if fi.IsDir() {
continue
}
// Open the file in the directory
f, err := os.Open(filepath.Join(passDir, fi.Name()))
if err != nil {
return err
}
// Create the sha writer
hw := sha1.New()
// Create the zip writer
zw, err := w.Create(fi.Name())
if err != nil {
return err
}
mw := io.MultiWriter(hw, zw)
// Write the file to the zip writer
_, err = io.Copy(mw, f)
if err != nil {
return err
}
// Add the hash to a map, later we will json.Marshal this to make manifest.json
sha := hw.Sum(nil)
m[fi.Name()] = fmt.Sprintf("%x", sha)
}
// Create the file writer
f, err := os.Create(filepath.Join(tempDir, "manifest.json"))
if err != nil {
return err
}
defer f.Close()
// Create the zip writer
zw, err := w.Create("manifest.json")
if err != nil {
return err
}
mw := io.MultiWriter(f, zw)
// Write the JSON to the file, and zip
err = json.NewEncoder(mw).Encode(m)
if err != nil {
return err
}
return nil
}
// sign will sign the manifest json using the keys and certificates created
// in key and pem respectively. It will then write the signature file to the zip
// archive so that we can open the pass.
func sign(w *zip.Writer, tempDir, password string) error {
// Copy the wwdr certificate into a file so that it can be used
// by openssl
f, err := os.Create(fmt.Sprintf("%s/wwdr.pem", tempDir))
if err != nil {
return err
}
defer f.Close()
_, err = f.Write([]byte(wwdr))
if err != nil {
return err
}
// Sign the bundle
cmd := exec.Command(
"openssl",
"smime",
"-sign",
"-signer",
fmt.Sprintf("%s/certificate.pem", tempDir),
"-inkey",
fmt.Sprintf("%s/key.pem", tempDir),
"-certfile",
fmt.Sprintf("%s/wwdr.pem", tempDir),
"-in",
fmt.Sprintf("%s/manifest.json", tempDir),
"-out",
fmt.Sprintf("%s/signature", tempDir),
"-outform",
"der",
"-binary",
"-passin",
fmt.Sprintf("pass:%s1234", password),
)
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return err
}
sig, err := os.Open(fmt.Sprintf("%s/signature", tempDir))
if err != nil {
return err
}
defer f.Close()
zw, err := w.Create("signature")
if err != nil {
return err
}
_, err = io.Copy(zw, sig)
if err != nil {
return err
}
return nil
}