-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
pair.go
278 lines (254 loc) · 7.84 KB
/
pair.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package ios
import (
"bytes"
"crypto"
"errors"
"fmt"
"github.com/fullsailor/pkcs7"
"github.com/google/uuid"
"golang.org/x/crypto/pkcs12"
plist "howett.net/plist"
"strings"
)
//PairSupervised uses an organization id from apple configurator so you can pair
//a supervised device without the need for user interaction (the trust popup)
//Arguments are the device, the p12 files raw contents and the password used for the p12
//file.
// I basically got this from cfgutil:
// https://configautomation.com/cfgutil-man-page.html
// here is how to turn a p12 into crt and key:
// openssl pkcs12 -in organization.p12 -out organization.pem -nodes -password pass:a
// openssl x509 -outform DER -out organization.crt -in organization.pem
// openssl rsa -outform DER -out organization.key -in organization.pem
// then you can run:
// cfgutil -K organization.key -C organization.crt pair
func PairSupervised(device DeviceEntry, p12bytes []byte, p12Password string) error {
supervisedPrivateKey, cert, err := pkcs12.Decode(p12bytes, p12Password)
if err != nil {
return err
}
usbmuxConn, err := NewUsbMuxConnectionSimple()
if err != nil {
return err
}
defer usbmuxConn.Close()
buid, err := usbmuxConn.ReadBuid()
if err != nil {
return err
}
lockdown, err := usbmuxConn.ConnectLockdown(device.DeviceID)
if err != nil {
return err
}
publicKey, err := lockdown.GetValue("DevicePublicKey")
if err != nil {
return err
}
wifiMac, err := lockdown.GetValue("WiFiAddress")
if err != nil {
return err
}
rootCert, hostCert, deviceCert, rootPrivateKey, hostPrivateKey, err := createRootCertificate(publicKey.([]byte))
if err != nil {
return fmt.Errorf("Failed creating pair record with error: %v", err)
}
pairRecordData := newFullPairRecordData(buid, hostCert, rootCert, deviceCert)
options := map[string]interface{}{"SupervisorCertificate": cert.Raw, "ExtendedPairingErrors": true}
request := map[string]interface{}{"Label": "go-ios", "ProtocolVersion": "2", "Request": "Pair", "PairRecord": pairRecordData, "PairingOptions": options}
err = lockdown.Send(request)
if err != nil {
return err
}
resp, err := lockdown.ReadMessage()
if err != nil {
return err
}
challengeBytes, err := extractPairingChallenge(resp)
if err != nil {
return err
}
sd, err := pkcs7.NewSignedData(challengeBytes)
if err != nil {
return err
}
err = sd.AddSigner(cert, supervisedPrivateKey.(crypto.Signer), pkcs7.SignerInfoConfig{})
if err != nil {
return err
}
der, err := sd.Finish()
if err != nil {
return err
}
options2 := map[string]interface{}{"ChallengeResponse": der}
request = map[string]interface{}{"Label": "go-ios", "ProtocolVersion": "2", "Request": "Pair", "PairRecord": pairRecordData, "PairingOptions": options2}
err = lockdown.Send(request)
if err != nil {
return err
}
resp, err = lockdown.ReadMessage()
if err != nil {
return err
}
respMap, err := ParsePlist(resp)
if err != nil {
return err
}
escrow := respMap["EscrowBag"].([]byte)
usbmuxConn, err = NewUsbMuxConnectionSimple()
defer usbmuxConn.Close()
if err != nil {
return err
}
success, err := usbmuxConn.savePair(device.Properties.SerialNumber, deviceCert, hostPrivateKey, hostCert, rootPrivateKey, rootCert, escrow, wifiMac.(string), pairRecordData.HostID, buid)
if err != nil {
return err
}
if !success {
return errors.New("pairing failed unexpectedly")
}
return nil
}
func extractPairingChallenge(resp []byte) ([]byte, error) {
respPlist, err := ParsePlist(resp)
if err != nil {
return []byte{}, err
}
errormsgintf, ok := respPlist["Error"]
if !ok {
return []byte{}, fmt.Errorf("the response is missign the Error key: %+v", respPlist)
}
errormsg, ok := errormsgintf.(string)
if !ok {
return []byte{}, fmt.Errorf("error should have been a string: %+v", respPlist)
}
if "MCChallengeRequired" != errormsg {
return []byte{},
fmt.Errorf("received wrong error message '%s' error message should have been 'McChallengeRequired' : %+v", errormsg, respPlist)
}
respdictintf, ok := respPlist["ExtendedResponse"]
if !ok {
return []byte{}, fmt.Errorf("ExtendedResponse key was missing from: %+v", respPlist)
}
respdict, ok := respdictintf.(map[string]interface{})
if !ok {
return []byte{}, fmt.Errorf("ExtendedResponse should have been a map[string]innterface{}: %+v", respPlist)
}
challengeintf, ok := respdict["PairingChallenge"]
if !ok {
return []byte{}, fmt.Errorf("PairingChallenge key is missing: %+v", respPlist)
}
challenge, ok := challengeintf.([]byte)
if !ok {
return []byte{}, fmt.Errorf("PairingChallenge should have been a byte array: %+v", respPlist)
}
return challenge, nil
}
//Pair tries to pair with a device. The first time usually
//fails because the user has to accept a trust pop up on the iOS device.
// What you have to do to pair is:
// 1. run the Pair() function
// 2. accept the trust pop up on the device
// 3. run the Pair() function a second time
func Pair(device DeviceEntry) error {
usbmuxConn, err := NewUsbMuxConnectionSimple()
if err != nil {
return err
}
defer usbmuxConn.Close()
buid, err := usbmuxConn.ReadBuid()
if err != nil {
return err
}
lockdown, err := usbmuxConn.ConnectLockdown(device.DeviceID)
if err != nil {
return err
}
publicKey, err := lockdown.GetValue("DevicePublicKey")
if err != nil {
return err
}
wifiMac, err := lockdown.GetValue("WiFiAddress")
if err != nil {
return err
}
rootCert, hostCert, deviceCert, rootPrivateKey, hostPrivateKey, err := createRootCertificate(publicKey.([]byte))
if err != nil {
return fmt.Errorf("Failed creating pair record with error: %v", err)
}
pairRecordData := newFullPairRecordData(buid, hostCert, rootCert, deviceCert)
request := newLockDownPairRequest(pairRecordData)
err = lockdown.Send(request)
if err != nil {
return err
}
resp, err := lockdown.ReadMessage()
if err != nil {
return err
}
response := getLockdownPairResponsefromBytes(resp)
if isPairingDialogOpen(response) {
return fmt.Errorf("Please accept the PairingDialog on the device and run pairing again!")
}
if response.Error != "" {
return fmt.Errorf("Lockdown error: %s", response.Error)
}
usbmuxConn, err = NewUsbMuxConnectionSimple()
defer usbmuxConn.Close()
if err != nil {
return err
}
success, err := usbmuxConn.savePair(device.Properties.SerialNumber, deviceCert, hostPrivateKey, hostCert, rootPrivateKey, rootCert, response.EscrowBag, wifiMac.(string), pairRecordData.HostID, buid)
if !success || err != nil {
return errors.New("Saving the PairRecord to usbmux failed")
}
return nil
}
type FullPairRecordData struct {
DeviceCertificate []byte
HostCertificate []byte
RootCertificate []byte
SystemBUID string
HostID string
}
type PairingOptions struct {
ExtendedPairingErrors bool
}
type LockDownPairRequest struct {
Label string
PairRecord FullPairRecordData
Request string
ProtocolVersion string
PairingOptions PairingOptions
}
type LockdownPairResponse struct {
Error string
Request string
EscrowBag []byte
}
func getLockdownPairResponsefromBytes(plistBytes []byte) LockdownPairResponse {
decoder := plist.NewDecoder(bytes.NewReader(plistBytes))
var data LockdownPairResponse
_ = decoder.Decode(&data)
return data
}
func isPairingDialogOpen(resp LockdownPairResponse) bool {
return resp.Error == "PairingDialogResponsePending"
}
func newLockDownPairRequest(pairRecord FullPairRecordData) LockDownPairRequest {
var req LockDownPairRequest
req.Label = "go-ios"
req.PairingOptions = PairingOptions{true}
req.Request = "Pair"
req.ProtocolVersion = "2"
req.PairRecord = pairRecord
return req
}
func newFullPairRecordData(systemBuid string, hostCert []byte, rootCert []byte, deviceCert []byte) FullPairRecordData {
var data FullPairRecordData
data.SystemBUID = systemBuid
data.HostID = strings.ToUpper(uuid.New().String())
data.RootCertificate = rootCert
data.HostCertificate = hostCert
data.DeviceCertificate = deviceCert
return data
}