forked from teslamotors/vehicle-command
/
vcsec.go
227 lines (198 loc) · 7.05 KB
/
vcsec.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
package vehicle
// This file implements commands targeting the Vehicle Security Controller
// domain (VCSEC). VCSEC handles key management and most vehicle actuators,
// such as the trunk and door locks.
import (
"context"
"crypto/ecdh"
"fmt"
"time"
"github.com/greenmission/vehicle-command/pkg/connector"
"github.com/greenmission/vehicle-command/pkg/protocol"
"google.golang.org/protobuf/proto"
"github.com/greenmission/vehicle-command/pkg/protocol/protobuf/keys"
universal "github.com/greenmission/vehicle-command/pkg/protocol/protobuf/universalmessage"
"github.com/greenmission/vehicle-command/pkg/protocol/protobuf/vcsec"
)
func unmarshalVCSECResponse(message *universal.RoutableMessage) (*vcsec.FromVCSECMessage, error) {
// Handle protocol-level errors
if err := protocol.GetError(message); err != nil {
return nil, err
}
switch message.Payload.(type) {
case *universal.RoutableMessage_ProtobufMessageAsBytes:
// Continue
case nil:
return &vcsec.FromVCSECMessage{}, nil
default:
return nil, protocol.NewError("payload missing from vehicle respone", true, false)
}
encodedMessage := message.GetProtobufMessageAsBytes()
// Handle application-layer errors
var fromVCSEC vcsec.FromVCSECMessage
if err := proto.Unmarshal(encodedMessage, &fromVCSEC); err != nil {
return nil, &protocol.CommandError{Err: fmt.Errorf("%w: %s", protocol.ErrBadResponse, err), PossibleSuccess: true, PossibleTemporary: false}
}
if errMsg := fromVCSEC.GetNominalError(); errMsg != nil {
return nil, &protocol.NominalError{Details: &protocol.NominalVCSECError{Details: errMsg}}
}
if status := fromVCSEC.GetCommandStatus(); status != nil {
switch status.GetOperationStatus() {
case vcsec.OperationStatus_E_OPERATIONSTATUS_OK:
case vcsec.OperationStatus_E_OPERATIONSTATUS_WAIT:
return nil, protocol.ErrBusy
case vcsec.OperationStatus_E_OPERATIONSTATUS_ERROR:
if code := status.GetWhitelistOperationStatus().GetWhitelistOperationInformation(); code != vcsec.WhitelistOperationInformation_E_WHITELISTOPERATION_INFORMATION_NONE {
return nil, &protocol.KeychainError{Code: code}
}
if status.GetSignedMessageStatus() == nil {
return nil, protocol.ErrUnknown
}
}
}
return &fromVCSEC, nil
}
type isTerminalTest func(fromVCSEC *vcsec.FromVCSECMessage) (bool, error)
// readUntil reads messages from VCSEC until one of them causes done to return true.
func readUntil(ctx context.Context, recv protocol.Receiver, done isTerminalTest) (*vcsec.FromVCSECMessage, error) {
for {
select {
case reply := <-recv.Recv():
fromVCSEC, err := unmarshalVCSECResponse(reply)
if err != nil {
return nil, err
}
if ok, err := done(fromVCSEC); ok {
return fromVCSEC, err
}
case <-ctx.Done():
return nil, &protocol.CommandError{Err: ctx.Err(), PossibleSuccess: true, PossibleTemporary: true}
}
}
}
// getVCSECResult sends a payload to VCSEC, retrying as appropriate, and returns nil if the command succeeded.
func (v *Vehicle) getVCSECResult(ctx context.Context, payload []byte, auth connector.AuthMethod, done isTerminalTest) (*vcsec.FromVCSECMessage, error) {
var fromVCSEC *vcsec.FromVCSECMessage
for {
recv, err := v.getReceiver(ctx, universal.Domain_DOMAIN_VEHICLE_SECURITY, payload, auth)
if err == nil {
fromVCSEC, err = readUntil(ctx, recv, done)
recv.Close()
}
if !protocol.ShouldRetry(err) {
return fromVCSEC, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(v.dispatcher.RetryInterval()):
continue
}
}
}
func isWhitelistOperationComplete(fromVCSEC *vcsec.FromVCSECMessage) (bool, error) {
if opStatus := fromVCSEC.GetCommandStatus().GetWhitelistOperationStatus(); opStatus != nil {
status := opStatus.GetWhitelistOperationInformation()
if status == vcsec.WhitelistOperationInformation_E_WHITELISTOPERATION_INFORMATION_NONE {
return true, nil
}
// This code should be unreachable if VCSEC sends correctly formed
// messages (i.e., if the operation status is set to error whenever
// code indicates a fault).
return true, &protocol.KeychainError{Code: status}
}
return false, nil
}
func (v *Vehicle) executeWhitelistOperation(ctx context.Context, payload []byte) error {
_, err := v.getVCSECResult(ctx, payload, v.authMethod, isWhitelistOperationComplete)
return err
}
func addKeyPayload(publicKey *ecdh.PublicKey, isOwner bool, formFactor vcsec.KeyFormFactor) *vcsec.UnsignedMessage {
var role keys.Role
if isOwner {
role = keys.Role_ROLE_OWNER
} else {
role = keys.Role_ROLE_DRIVER
}
return &vcsec.UnsignedMessage{
SubMessage: &vcsec.UnsignedMessage_WhitelistOperation{
WhitelistOperation: &vcsec.WhitelistOperation{
SubMessage: &vcsec.WhitelistOperation_AddKeyToWhitelistAndAddPermissions{
AddKeyToWhitelistAndAddPermissions: &vcsec.PermissionChange{
Key: &vcsec.PublicKey{
PublicKeyRaw: publicKey.Bytes(),
},
KeyRole: role,
},
},
MetadataForKey: &vcsec.KeyMetadata{
KeyFormFactor: formFactor,
},
},
},
}
}
// executeRKEAction sends an RKE action command to the vehicle. (RKE originally
// referred to "Remote Keyless Entry" but now refers more generally to commands
// that can be sent by a keyfob).
func (v *Vehicle) executeRKEAction(ctx context.Context, action vcsec.RKEAction_E) error {
done := func(fromVCSEC *vcsec.FromVCSECMessage) (bool, error) {
if fromVCSEC.GetCommandStatus() == nil {
return true, nil
}
return false, nil
}
payload := vcsec.UnsignedMessage{
SubMessage: &vcsec.UnsignedMessage_RKEAction{
RKEAction: action,
},
}
encodedPayload, err := proto.Marshal(&payload)
if err != nil {
return err
}
_, err = v.getVCSECResult(ctx, encodedPayload, v.authMethod, done)
return err
}
// Not exported. Use v.Wakeup instead, which chooses the correct wake method based on available transport.
func (v *Vehicle) wakeupRKE(ctx context.Context) error {
return v.executeRKEAction(ctx, vcsec.RKEAction_E_RKE_ACTION_WAKE_VEHICLE)
}
func (v *Vehicle) RemoteDrive(ctx context.Context) error {
return v.executeRKEAction(ctx, vcsec.RKEAction_E_RKE_ACTION_REMOTE_DRIVE)
}
func (v *Vehicle) AutoSecureVehicle(ctx context.Context) error {
return v.executeRKEAction(ctx, vcsec.RKEAction_E_RKE_ACTION_AUTO_SECURE_VEHICLE)
}
type Closure string
const (
ClosureTrunk Closure = "trunk"
ClosureFrunk Closure = "frunk"
)
func (v *Vehicle) executeClosureAction(ctx context.Context, action vcsec.ClosureMoveType_E, closure Closure) error {
done := func(fromVCSEC *vcsec.FromVCSECMessage) (bool, error) {
if fromVCSEC.GetCommandStatus() == nil {
return true, nil
}
return false, nil
}
// Not all actions are meaningful for all closures. Exported methods restrict combinations.
var request vcsec.ClosureMoveRequest
switch closure {
case ClosureTrunk:
request.RearTrunk = action
case ClosureFrunk:
request.FrontTrunk = action
}
payload := vcsec.UnsignedMessage{
SubMessage: &vcsec.UnsignedMessage_ClosureMoveRequest{
ClosureMoveRequest: &request,
},
}
encodedPayload, err := proto.Marshal(&payload)
if err != nil {
return err
}
_, err = v.getVCSECResult(ctx, encodedPayload, v.authMethod, done)
return err
}