Skip to content

Commit

Permalink
fix detached payload diagnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
OR13 committed Nov 13, 2023
1 parent f65b393 commit 57da658
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 163 deletions.
4 changes: 2 additions & 2 deletions src/detachPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import * as cbor from 'cbor-web'

import { DetachedSignature } from './types/DetachedSignature'

import { typedArrayToBuffer } from './utils'


const detachPayload = async (attachedSignature: Uint8Array): Promise<DetachedSignature> => {
const decoded = cbor.decodeFirstSync(attachedSignature)
const payload = decoded.value[2]
decoded.value[2] = typedArrayToBuffer(new Uint8Array())
decoded.value[2] = null
cbor.encode(decoded)
const signature = new Uint8Array(await cbor.encodeAsync(decoded))
return { payload, signature }
Expand Down
118 changes: 23 additions & 95 deletions src/diagnostic.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,35 @@
import * as cbor from 'cbor-web'

// const COSE_Sign1_TAG = 18
function toHexString(byteArray: Uint8Array) {
return Array.prototype.map
.call(byteArray, function (byte) {
return ('0' + (byte & 0xff).toString(16)).slice(-2)
const alternateDiagnostic = async (
data: any
) => {
const decoded = await cbor.decode(data)
if (decoded.tag === 18 && decoded.value[2] === null) {
decoded.value[2] = 'nil'
const data2 = await cbor.encode(decoded)
let text = await cbor.diagnose(data2, {
separator: '\n'
})
.join('')
}
text = text.replace(/"nil"/gm, 'nil')

const prettyHeaderKey = (k: string) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return ({
[`1`]: 'alg',
[`3`]: 'content_type',
[`4`]: 'kid',
// new
[`100`]: 'inclusion-proof',
[`200`]: 'consistency-proof',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)[`${k}`]
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const prettyHeaderValue = (v: any) => {
const value = ({
[`-7`]: '"ES256"',
[`-35`]: '"ES384"',
[`-36`]: '"ES512"',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)[`${v}`]
return value ? value : `h'${toHexString(new TextEncoder().encode(v))}'`
}
text = text.replace(/\(/gm, '(\n')
text = text.replace(/\)/gm, '\n)')

const diagnosticProtectedHeader = (data: Uint8Array) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const decoded = cbor.decode(data, { dictionary: 'map' } as any)
const lines = []
for (const [k, v] of decoded.entries()) {
lines.push(` # "${prettyHeaderKey(k)}" : ${prettyHeaderValue(v)}`)
lines.push(` # ${k} : ${v}`)
}
return ` # Protected Header
h'${toHexString(data)}',
# {
${lines.join(',\n')}
# }
`
}
text = text.replace(/\[/gm, '[\n')
text = text.replace(/\]/gm, '\n]')

const diagnosticData = (data: Uint8Array) => {
return `h'${toHexString(data)}'`
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const diagnosticUnprotectedHeader = (decoded: any) => {
if (!decoded.entries) {
return ' # Unprotected Header\n {},\n'
}
const lines = []
for (const [k, v] of decoded.entries()) {
lines.push(
` # "${prettyHeaderKey(k)}" : "${prettyHeaderValue(v)}"
${k} : ${prettyHeaderValue(v)} `,
)
}
return ` # Unprotected Header
{
${lines.join(',\n')}
},
`
}
text = text.replace(/, /gm, ',\n')

const default_options = {
decode_payload: true,
detached_payload: false,
}
const alternateDiagnostic = async (
data: Uint8Array,
options = default_options,
) => {
let diagnostic = ''
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { tag, value } = cbor.decode(data, { dictionary: 'map' } as any)
const unprotectedHeader = diagnosticUnprotectedHeader(value[1])
diagnostic += `# COSE_Sign1\n${tag}([\n\n`
diagnostic += diagnosticProtectedHeader(value[0])
diagnostic += '\n'
diagnostic += unprotectedHeader
diagnostic += '\n'
if (options.detached_payload) {
diagnostic += ' ' + '# Detached Payload\n'
} else {
diagnostic += ' ' + '# Protected Payload\n'
diagnostic += ' ' + diagnosticData(value[2]) + ',\n'
if (options.decode_payload) {
diagnostic += ' ' + '# ' + new TextDecoder().decode(value[2]) + '\n'
}
return text
}
let text = await cbor.diagnose(data, {
separator: '\n'
})
text = text.replace(/\[/gm, '[\n')
text = text.replace(/, /gm, ',\n')
text = text.replace(/\]/gm, '\n]')
return text

diagnostic += '\n'
diagnostic += ' ' + '# Signature\n'
diagnostic += ' ' + diagnosticData(value[3]) + '\n'
diagnostic += `])`
return diagnostic
}

export default alternateDiagnostic
2 changes: 1 addition & 1 deletion src/rfc/beautify/beautifyCoseSign1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ${addComment(`18(`, 'COSE Sign 1')}
[
${addComment(protectedHeaderLine, 'Protected')}
${unprotectedHeaderLines}
${addComment(payloadLine, decoded.value[2].length > 0 ? `Payload` : `Detached payload`)}
${addComment(payloadLine, decoded.value[2] !== null ? `Payload` : `Detached payload`)}
${addComment(signatureLine, 'Signature')}
]
)
Expand Down
3 changes: 3 additions & 0 deletions src/rfc/beautify/bufferToTruncatedBstr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@


export const bufferToTruncatedBstr = (thing: ArrayBuffer | Buffer | any) => {
if (thing === null) {
return 'nil'
}
const buf = Buffer.from(thing)
const line = `h'${buf.toString('hex').toLowerCase()}'`
return line.replace(/h'(.{8}).+(.{8})'/g, `h'$1...$2'`).trim()
Expand Down
2 changes: 1 addition & 1 deletion test/cometre/consistency-proof.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
},
h'430b6fd7...f74c7fc4', / Payload /
h'287e1eaa...105ffdd1' / Signature /
h'46c9ddd5...3b7ccc71' / Signature /
]
)
~~~~
Expand Down
4 changes: 2 additions & 2 deletions test/cometre/inclusion-proof.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
]
},
},
h'', / Detached payload /
h'f0bca9d5...fbecebb4' / Signature /
nil, / Detached payload /
h'22c282ce...dcc30d44' / Signature /
]
)
~~~~
Expand Down
36 changes: 17 additions & 19 deletions test/hpke/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# JOSE

🔥 Experimental 🔥

Inspired by https://datatracker.ietf.org/doc/html/rfc7516#section-7.2.1

~~~~ text
Expand Down Expand Up @@ -29,8 +27,8 @@ Inspired by https://datatracker.ietf.org/doc/html/rfc7516#section-7.2.1
"crv": "P-256",
"alg": "HPKE-Base-P256-SHA256-AES128GCM",
"kid": "test-key-42",
"x": "xXCWZk-jG9Tjd7M361sAEUi8JvKBxFIQgqhqkZa5cgs",
"y": "y-9jpXy5gNhxl9BV4smqO36MXRlbkrC3PvvjDOrpOgU",
"x": "BFzUqQqyrg5c3wkZN7Qs6shAxKRJXqlN2MVSoLprpmE",
"y": "UCvYMCp-7MCOl7JRRYUXdP7pPNRVFLrc2RZ_yLp5cqM",
"use": "enc",
"key_ops": [
"deriveBits"
Expand All @@ -46,9 +44,9 @@ Inspired by https://datatracker.ietf.org/doc/html/rfc7516#section-7.2.1
"crv": "P-256",
"alg": "HPKE-Base-P256-SHA256-AES128GCM",
"kid": "test-key-42",
"x": "xXCWZk-jG9Tjd7M361sAEUi8JvKBxFIQgqhqkZa5cgs",
"y": "y-9jpXy5gNhxl9BV4smqO36MXRlbkrC3PvvjDOrpOgU",
"d": "aRDKVwA-kwySgg9Z8jd9GsV-Kq2KoKlFAP79vbU7TRQ",
"x": "BFzUqQqyrg5c3wkZN7Qs6shAxKRJXqlN2MVSoLprpmE",
"y": "UCvYMCp-7MCOl7JRRYUXdP7pPNRVFLrc2RZ_yLp5cqM",
"d": "87Vyg4ckRaep1giXTZMG_q6dBISG3Wn8Whnp3HPn4fQ",
"key_ops": [
"deriveBits"
]
Expand All @@ -61,8 +59,8 @@ https://datatracker.ietf.org/doc/html/draft-rha-jose-hpke-encrypt-01#section-4.1

~~~~ json
{
"protected": "eyJhbGciOiJIUEtFLUJhc2UtUDI1Ni1TSEEyNTYtQUVTMTI4R0NNIiwiZW5jIjoiQkIyaUpkbzZQM3ZYVVBQbUNoYlo5UjdUd0Y3dVEzTUpySWFIaWZUQnQyczZUZmgwNG9MWi1KOUtpX19QOXhfakZEYW9yZ0xwLUJUSG1OSi1FdmpVeHU4Iiwia2lkIjoidGVzdC1rZXktNDIifQ",
"ciphertext": "3239ChirVmfriy3SwN7m2SIpP8VgFoXxm1ZQ"
"protected": "eyJhbGciOiJIUEtFLUJhc2UtUDI1Ni1TSEEyNTYtQUVTMTI4R0NNIiwiZW5jIjoiQkpPV1JGNHRWZmVWZkRxUjBRTExuM2FlWnh6ankzeW8xaDNhVjNFbnh6LVpwamxJdzZWY3BLZ1lPR2FweEdOd2c1N0s5b3FFUnF0MjVqSWp0R0lJSFZzIiwia2lkIjoidGVzdC1rZXktNDIifQ",
"ciphertext": "-yHU4AniXNB1CvQqC-9whZ3vpsNiw_PqLlM_"
}
~~~~

Expand All @@ -77,13 +75,13 @@ https://datatracker.ietf.org/doc/html/draft-rha-jose-hpke-encrypt-01#section-4.1
"recipients": [
{
"kid": "test-key-42",
"enc": "BGKO2-0oc4lsB4u2E3IK9kI0AVNqyZpDwssNBEkDr1MYjSTS6yB9rS8A6hrRUdXVPzj05UiierQAMojlgySoJgk",
"encrypted_key": "RaN5dZFZR8Hr_CibbRiOPFkMW8ylue6lQ6T5cU7ZPtc"
"enc": "BLO6UFa5-QKzLAIbsy8I7J9hy6eD0X0iPxHkPLPWkAiPejDZQA89SlJQM9g-6scp3XjedYATtXC3xr71ot7DiXI",
"encrypted_key": "2i2tygZC46UFgwIZbdGoS2RGKgKOMDz1xWKWcw42Jrs"
}
]
},
"iv": "DojoBowHRBbJLffK",
"ciphertext": "Vuw6ChVjGp7inNdI-AiAQYq_3JvvqJUXAjjF"
"iv": "4TTSFSPxE3lsq7v_",
"ciphertext": "0tSAg4olHnqs5Cb_1e-M8OJdBaTY2ekQo7Ev"
}
~~~~

Expand Down Expand Up @@ -127,8 +125,8 @@ Enc_structure = [
2: h'74657374...792d3432', / Identifier /
3: TBD, / Algorithm /
-1: 1, / Curve /
-2: h'c5709666...96b9720b', / x public key component /
-3: h'cbef63a5...eae93a05', / y public key component /
-2: h'045cd4a9...ba6ba661', / x public key component /
-3: h'502bd830...ba7972a3', / y public key component /
}
~~~~

Expand All @@ -140,9 +138,9 @@ Enc_structure = [
2: h'74657374...792d3432', / Identifier /
3: TBD, / Algorithm /
-1: 1, / Curve /
-2: h'c5709666...96b9720b', / x public key component /
-3: h'cbef63a5...eae93a05', / y public key component /
-4: h'6910ca57...b53b4d14', / d private key component /
-2: h'045cd4a9...ba6ba661', / x public key component /
-3: h'502bd830...ba7972a3', / y public key component /
-4: h'f3b57283...73e7e1f4', / d private key component /
}
~~~~

Expand Down Expand Up @@ -171,7 +169,7 @@ See https://datatracker.ietf.org/doc/html/draft-ietf-cose-hpke-07#section-3.1.2
h'4F1EE8FE6B430057B83500FDC807EE679E0FE59F34B462CAC4AA8A', / encrypted content /
[
[
h'A10139D902', / protected header /
h'A10139D902', / protected header (repeated why?) /
{
-22222: h'04E8A00C...273E9D83',
4: h'746573742D6B65792D3432', / recipient kid /
Expand Down
2 changes: 1 addition & 1 deletion test/hpke/ct.cose
Original file line number Diff line number Diff line change
@@ -1 +1 @@
�E�9��Ktest-key-429V�XA-�y<X�:��~�J'�����z�ڈ3N^� BtBa�i��F�B�j��i'H�e�v#*c�,�AXʧ�Q<��Ƣ�����[���U�%�
�E�9��Ktest-key-429V�XA�����l�G%�oHmqnEx�H ��āGCh�� ��0�@��M����\pW����/*��b�X��l�����m8����y�Y�����9c{
Binary file modified test/hpke/ct.multi.cose
Binary file not shown.
Loading

0 comments on commit 57da658

Please sign in to comment.