Android2026Reverse engineering and Android app

Pieno

An unofficial Android app for Friuli Venezia Giulia's subsidized fuel card, with the QR code offline, the cheapest station nearby and a Google Wallet pass.

Pieno showing the FVG fuel discount QR code on Marco Ceccon's card, with a button to find the cheapest stationThe same QR code full screen on white, with the plate above it

In Friuli Venezia Giulia, people who live here pay less for fuel. The region takes some cents off every litre, and you get the discount by showing a QR code at the pump. The best part of that code is what's inside it: it's built exactly like a COVID green pass.

The region already has an app for the card, QRfvg Carburanti, made by Insiel. I didn't enjoy using it, to put it politely, so I built my own around the moment at the pump, when all you want is the code on the screen.

Reading the official app

One morning I pulled the official app off my phone with adb and opened it in jadx, a decompiler that turns an Android app back into readable Java, to see how it talks to the region's servers. By lunch I had a Python script that logged in and printed my card. That afternoon the first Android build was running, and the next day the code was on GitHub.

What's inside the QR code

The string in the code starts with HC1:, the prefix of the EU Digital COVID Certificate, and the layers under it are the same too.

Base45 turns bytes into the letters and digits a QR code stores most compactly. Under that is zlib compression, and under that a COSE_Sign1 message: a small payload in CBOR, a binary cousin of JSON, plus an ECDSA signature on the P-256 curve.

The payload is tiny. It holds a card id, the plate, a country code and two timestamps. The region's backend signs it with a private key that stays with the region, and anyone with the public certificate can check it.

That was the most useful thing I learned: the phone signs nothing. The official app makes two API calls, downloads that string and draws it as a QR code. A second app has nothing to compute. It has to log in as you, fetch the string and draw it again exactly as it came, so the code on Pieno's screen is the same code, module for module.

Logging in without seeing the password

Pieno uses the region's own login, LoginFVG, and never sees your password. It opens the login page in a browser tab, and you sign in there with SPID or CIE, Italy's digital identity and electronic ID card. The login server sends back a one-time code, and Pieno trades it for tokens.

That's the OAuth authorization code flow with PKCE, done with the AppAuth for Android library. PKCE is the version for apps that can't keep a secret. The app makes a random value, sends only its hash when the login starts, and shows the value itself when it trades the code. A code intercepted on the way back is useless to anyone else.

The login server knows one address to send you back to, the official app's, so Pieno registers the same one. If both apps are installed, Android asks which one should finish the login. Pick Pieno.

The access token is a JWT, a signed blob of JSON. Its subject is your fiscal code with @carbon.super on the end. In a fuel app that sounds like a premium grade, but it's what WSO2 Identity Server calls its default tenant. Pieno cuts at the @, asks the API for the beneficiary with that fiscal code, and then for that beneficiary's cards.

The tokens never sit on disk in clear text. Pieno encrypts them with AES-GCM, using a key that lives in the Android Keystore and never leaves the phone.

Two refreshes at once

Access tokens expire, and the app gets new ones with a refresh token. The region's login server rotates refresh tokens: every refresh returns a new one and kills the old one. That's good practice, and it caught me out.

Two parts of Pieno could refresh. The HTTP interceptor refreshed when a request came back 401, and the repository refreshed before its own calls. Pieno loads several things at once when it opens, so sometimes both found an expired token at the same moment and both refreshed with the same refresh token. The first one won. The second showed up with a token that had just been killed, got an error, and the app decided your session was over.

The fix is one lock. Every refresh now goes through synchronized(refreshLock), so the second caller waits, loads the state the first one saved, finds a fresh access token and doesn't refresh at all. After each refresh Pieno saves the whole AppAuth state, because the refresh token inside it may have changed.

Keeping the card offline

A signed code doesn't need the network to be valid, so Pieno keeps a copy. Every time it fetches your cards, it saves each card's string on the phone. When there's no signal, or the session can't be refreshed, it shows the saved copy and the pump gets its code anyway.

The card is the first screen. Tap the code and it opens full screen on white, and Pieno turns the window brightness up to the maximum so the reader has an easy time and your eyes have a slightly harder one. Closing it puts the brightness back where it was.

The card used to show a "valid until" date under the code. The official app's configuration gives a digital card ten days, but the date kept moving on its own and told you nothing, so I took it out.

Checking someone else's card

The region allows another person to use your code, but only to refuel the car it belongs to. The discount follows the car, so lending it to a friend only works if you lend them the car too. So Pieno can import a code for a car that isn't yours, a family member's for example. You scan it with the camera or paste the string, and it sits next to your own cards. You can name each card.

Before it saves the card, Pieno takes the code apart and checks the signature, all on the phone. I ported my Python decoder to Kotlin: Base45, then zlib, then CBOR to pull out the three parts of the COSE message and the plate. The check rebuilds the exact bytes the region signed and verifies them against the region's public certificate, the one issued to "Sistema Carburanti Agevolati FVG":

val sigStructure = CBORObject.NewArray()
    .Add("Signature1")
    .Add(d.protectedBytes)
    .Add(ByteArray(0))
    .Add(d.payloadBytes)
    .EncodeToBytes()
val verifier = Signature.getInstance("SHA256withECDSA")
verifier.initVerify(cert.publicKey)
verifier.update(sigStructure)
verifier.verify(rawToDer(d.signature))

COSE stores the signature as two numbers, r and s, back to back. Java wants them wrapped in DER, so rawToDer rewraps them before the check.

If the signature doesn't match, the import is refused. Change one letter of the plate and the payload stops matching what the region signed, and a new signature needs the region's private key. That's also why the code is safe to keep on a phone. Here is a shared card, opened up the way Pieno opens it. Edit its plate and watch the check:

The signature matches, and Pieno saves the card.

HC1:NCF390D20FFWJWG.FKX*4F7B0XK6JC/RARI3Z10LD4JIN600CF3TV3C10Y50.FKYNK0FD+P47:6X57E46KF62DCVG67W5WF6+/6 57JOCAW5XICG-CGA7YJC3UCDM6K9E.G8*96RL6JH8578CUUKOGOG49NA4O60U7VW6L*BSNP/UVB/T GB*44XX3FM7E9FU4I-IVA1V/DEO$1-80NYKE4JYS9MRM5-S/:C-8SNOHS5B45KY23*3

In the app, a code signed with a key of my own gets the same answer, which I know because I tried:

Pieno's import dialog refusing a pasted QR string with the message: Firma non valida, il QR potrebbe essere falso o alterato

Finding the cheapest station

The same API publishes the prices of every participating station. Each card in Pieno has a button, "Trova il più economico", find the cheapest, that asks for a radius between 1 and 50 km. It starts at 15 and remembers your last choice.

The fuel comes from the card. You can set it, and if you haven't, Pieno counts that car's refuels and takes the fuel it bought most often. The plates in the refuel history aren't always written the same way, so it compares them without spaces and in capitals.

Then it ranks the stations within the radius, measured as a straight line from where you are:

The last rule came from the data. In this API, 1.000 is what a station sends when it hasn't set a price, and without the rule that station would win every search it's in. Pieno drops exactly 1.000 and keeps everything below it, because LPG really does cost around 0.70.

The other trap was the fuel itself. It sits in a field called idTipoCarburante. Next to it is tipoProdotto, "product type", which sounds like the fuel too and actually says whether the price is for the base or the premium product.

Pieno's cheapest station search asking for a radius, set to 15 km for dieselThe cheapest diesel station Pieno found, with all its prices and a button to open the route

Putting the card in Google Wallet

Pieno can add a card to Google Wallet as a pass with the QR code on it. A Wallet pass has to be signed with the issuer's service account key, and a key inside an APK is a key anyone can pull out. I'm hardly the person to argue otherwise. So the key lives in a small Cloudflare Worker.

When you press the button, Pieno sends the Worker the card's plate, its QR string, and the name and color you gave it. The Worker signs a JWT for the pass and also writes the pass object through the Wallet API, so a later change of name or color reaches passes that are already saved. Pieno hands the JWT to Google's save API, and the pass lands in Wallet with a button that opens Pieno straight on the refuel history, through a pieno://storico link.

The rest of the app

Stations lists the participating stations by distance, favorites first, with every price and when it was last updated. The lowest price at each station gets the accent color. The map uses OpenStreetMap tiles, which need no API key, tinted to match the app. History groups refuels by month, with what you paid and the discount you got, so at least one number on that screen is on your side.

The type is Fraunces for titles and Hanken Grotesk for the rest, on deep green and warm paper, with one vermilion for the numbers that matter.

The legal side

Pieno has nothing to do with Insiel or the region. It gets around no protection: it uses the official APIs with your own login, reads only your own data, sends nothing to anyone else unless you put a card in Wallet, and draws the QR code exactly as the region signed it.

The code is on GitHub under the PolyForm Noncommercial license, and GitHub Actions builds an APK from every push to master as a release called nightly.

Contracting from January, and open to more: web and Android apps, and integrations.me@marco.win