Skip to content

feat(HSM-69): have our very own BerTlv parser. - #5

Merged
koekiebox merged 4 commits into
mainfrom
jasonb/hsm-69-inhouse-tlv
Nov 4, 2026
Merged

koekiebox merged 4 commits into
mainfrom
jasonb/hsm-69-inhouse-tlv

Conversation

@koekiebox

@koekiebox koekiebox commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

moov-io/bertlv can panic on malformed input, and we parse untrusted payloads such as decrypted SRED data. This PR replaces it with our own parser in tlv/ber.go, written against EMV Book 3 Annex B and ISO 8825-1.

OpenEMV served as a reference only and is not a dependency.

What’s in it

  • API: Decode, Encode and FindFirstTag. They never panic and return typed errors (*DecodeError{Offset, Err}, which work with errors.Is and errors.As).
  • Rejected input:
    • truncated tags, lengths or values
    • indefinite (0x80) and reserved (0xFF) length forms
    • lengths over 4 bytes
    • tags over 4 bytes
    • nesting deeper than MaxDepth (16)
    • input larger than MaxInputSize (64 KiB)
  • EMV padding: 00 bytes between TLVs are skipped. FF is not treated as padding.
  • No data in errors: error messages carry only the offset, so a misframed PAN never ends up in logs.
  • Encode guarantee: Encode checks the depth and size limits, and a raw value under a constructed tag must decode, so whatever we encode we can decode again.
  • FindFirstTag fix: it now searches depth-first, which fixes moov’s bug of missing tags that come after a template.
  • ValidateAmount: now uses the new parser.
  • Dependencies: moov-io/bertlv is removed from go.mod.

@koekiebox koekiebox self-assigned this Nov 2, 2026
@koekiebox
koekiebox marked this pull request as ready for review November 4, 2026 12:16
@koekiebox
koekiebox requested a review from golobitch November 4, 2026 12:20
@koekiebox
koekiebox merged commit baff812 into main Nov 4, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants