Skip to content

Commit a91c334

Browse files
committed
[bug]:fix log change uds service
1 parent ca98485 commit a91c334

3 files changed

Lines changed: 45 additions & 19 deletions

File tree

resources/examples/uds_0x29/ecu.ts

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Util.On('Tester_can_0.authenticationConfiguration.send', async (req) => {
1818
let memorySize: number
1919
Util.On('Tester_can_0.RequestDownload520.send', async (req) => {
2020
const resp = DiagResponse.fromDiagRequest(req)
21+
// Reset transfer session state so this example can run repeatedly.
22+
fwContent = Buffer.alloc(0)
23+
encryptedContent = Buffer.alloc(0)
2124
memorySize = Number(req.diagGetParameter('memorySize'))
2225
console.log(`memorySize:${memorySize}`)
2326
resp.diagSetRaw(Buffer.from([0x74, 0x40, 0, 0, 0, 0x81]))
@@ -101,30 +104,53 @@ Util.On('Tester_can_0.proofOfOwnership.send', async (req) => {
101104
}
102105
})
103106
let fwContent: Buffer = Buffer.alloc(0)
104-
let decipher: crypto.DecipherGCM
107+
let encryptedContent: Buffer = Buffer.alloc(0)
105108

106109
Util.On('Tester_can_0.TransferData540.send', async (req) => {
107110
const raw = req.diagGetRaw()
108-
if (!decipher) {
109-
decipher = crypto.createDecipheriv('aes-256-gcm', realKey, Buffer.alloc(12, 0))
110-
}
111111
const transferRequestParameterRecord = raw.subarray(2)
112-
const decrypted = decipher.update(transferRequestParameterRecord)
113-
if (fwContent.length < memorySize) {
114-
fwContent = Buffer.concat([fwContent, decrypted])
115-
if (fwContent.length >= memorySize) {
116-
console.log(`decrypted content :${fwContent.subarray(0, memorySize).toString('ascii')}`)
117-
}
112+
console.log(`TransferData payload length:${transferRequestParameterRecord.length}`)
113+
if (encryptedContent.length < memorySize) {
114+
encryptedContent = Buffer.concat([encryptedContent, transferRequestParameterRecord])
115+
console.log(`encryptedContent length:${encryptedContent.length}/${memorySize}`)
118116
}
119117
const resp = DiagResponse.fromDiagRequest(req)
120118
resp.diagSetRaw(Buffer.from([0x76, Number(req.diagGetParameter('blockSequenceCounter'))]))
121119
await resp.outputDiag()
122120
})
123121

124122
Util.On('Tester_can_0.RequestTransferExit550.send', async (req) => {
123+
if (!realKey || encryptedContent.length === 0) {
124+
throw new Error('transfer session not initialized')
125+
}
125126
const raw = req.diagGetRaw()
126-
decipher.setAuthTag(raw.subarray(2))
127-
decipher.final()
127+
let authTag = req.diagGetParameterRaw('auth')
128+
console.log(`RequestTransferExit raw length:${raw.length}, auth length:${authTag.length}`)
129+
console.log(
130+
`RequestTransferExit encryptedContent length:${encryptedContent.length}/${memorySize}`
131+
)
132+
// Some service descriptions keep auth default at 4 bytes unless resized at runtime.
133+
// AES-GCM tag is 16 bytes, so fall back to the last 16 bytes of raw payload.
134+
if (authTag.length !== 16 && raw.length >= 17) {
135+
authTag = raw.subarray(raw.length - 16)
136+
console.log(`fallback auth length:${authTag.length}`)
137+
}
138+
if (authTag.length !== 16) {
139+
throw new Error(`invalid auth tag length:${authTag.length}`)
140+
}
141+
// Some stacks may append extra bytes in TransferData payload. Keep only expected download size.
142+
const encryptedPayload = encryptedContent.subarray(0, memorySize)
143+
if (encryptedContent.length !== memorySize) {
144+
console.log(
145+
`trim encryptedContent from ${encryptedContent.length} to ${encryptedPayload.length}`
146+
)
147+
}
148+
const decipher = crypto.createDecipheriv('aes-256-gcm', realKey, Buffer.alloc(12, 0))
149+
decipher.setAuthTag(authTag)
150+
const decrypted = Buffer.concat([decipher.update(encryptedPayload), decipher.final()])
151+
fwContent = decrypted
152+
console.log(`decrypted content :${fwContent.subarray(0, memorySize).toString('ascii')}`)
153+
encryptedContent = Buffer.alloc(0)
128154
const resp = DiagResponse.fromDiagRequest(req)
129155
resp.diagSetRaw(Buffer.from([0x77]))
130156
await resp.outputDiag()

resources/examples/uds_0x29/tester.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Util.Register('Tester_can_0.SecureDownload', async () => {
120120
const blockSequenceCounter = Buffer.alloc(1)
121121
blockSequenceCounter.writeUInt8((i + 1) & 0xff) // 使用循环计数 1-255
122122
transferRequest.diagSetParameterRaw('blockSequenceCounter', blockSequenceCounter)
123-
123+
await transferRequest.changeService()
124124
list.push(transferRequest)
125125
}
126126
//set auth tag

src/main/nodeItem.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export class NodeClass {
253253
//TODO:
254254
} else {
255255
if (data.addr.uuid != this.nodeItem.id) {
256-
const item = findService(tester, data.data, true)
256+
const item = cloneDeep(findService(tester, data.data, true))
257257
if (item) {
258258
try {
259259
applyBuffer(item, data.data, true)
@@ -277,7 +277,7 @@ export class NodeClass {
277277
//TODO:
278278
} else {
279279
if (data.addr.uuid != this.nodeItem.id) {
280-
const item = findService(tester, data.data, false)
280+
const item = cloneDeep(findService(tester, data.data, false))
281281
if (item) {
282282
try {
283283
applyBuffer(item, data.data, false)
@@ -307,7 +307,7 @@ export class NodeClass {
307307
//TODO:
308308
} else {
309309
if (data.addr.uuid != this.nodeItem.id) {
310-
const item = findService(tester, data.data, true)
310+
const item = cloneDeep(findService(tester, data.data, true))
311311
if (item) {
312312
try {
313313
applyBuffer(item, data.data, true)
@@ -327,7 +327,7 @@ export class NodeClass {
327327
//TODO:
328328
} else {
329329
if (data.addr.uuid != this.nodeItem.id) {
330-
const item = findService(tester, data.data, false)
330+
const item = cloneDeep(findService(tester, data.data, false))
331331
if (item) {
332332
try {
333333
applyBuffer(item, data.data, false)
@@ -362,7 +362,7 @@ export class NodeClass {
362362
if (data instanceof DoipError) {
363363
//TODO:
364364
} else {
365-
const item = findService(tester, data.data, true)
365+
const item = cloneDeep(findService(tester, data.data, true))
366366
if (item) {
367367
try {
368368
applyBuffer(item, data.data, true)
@@ -383,7 +383,7 @@ export class NodeClass {
383383
if (data instanceof DoipError) {
384384
//TODO:
385385
} else {
386-
const item = findService(tester, data.data, false)
386+
const item = cloneDeep(findService(tester, data.data, false))
387387
if (item) {
388388
try {
389389
applyBuffer(item, data.data, false)

0 commit comments

Comments
 (0)