How We Built Automatic Signature Verification for Digital Democracy in Brazil — And Why It Changes Everything
Petição Brasil asks people for two things most websites never see: their CPF, Brazil's individual taxpayer number, and a digital signature made with a government-issued identity. Digital Petitions and the Legal Validity of Popular Will explains why that signature matters in law. What follows is how the platform decides that a signature counts.
That decision rests on one sentence, which opens the platform's public security page: a counted signature is a valid digital signature, made through Gov.br (the federal government's citizen account) or with an ICP-Brasil personal certificate (ICP-Brasil is Brazil's national public-key infrastructure), by the person it names, on the petition's current text. Each check below exists to make one part of that sentence true. None of them makes the signer a registered voter, because the platform does not check that.
I built and maintain the platform, and I wrote the page. The page is the public reference; here the same design is followed from the inside, one uploaded file at a time. Like the page, I leave out thresholds, time limits and detection rules.
A file arrives
A signer downloads the petition's official PDF, signs it with Gov.br or an ICP-Brasil certificate, and uploads it. The upload is checked by size, magic number and MIME type, encrypted PDFs are refused, and the file's SHA-256 is recorded.
Verification does not happen in the request. It runs in a background worker, and that brings problems of its own: queues deliver messages more than once, workers die mid-task, retries overlap. So each run starts by claiming the signature with a conditional update that succeeds only if the signature is waiting, and the moment of the claim becomes that run's token. Every later write (approve, reject, notify, publish) is guarded by the token. A run that was superseded while it worked cannot record a verdict, and a duplicate message finds nothing to claim.
The first thing the run does with the file is hash it again. If the bytes in storage no longer match the hash taken at upload, something replaced the file between upload and verification. The signature is rejected, the event is logged as critical, and nothing in the file is read.
Nothing parses the file before the gate
The PDF is the platform's most exposed input, and the format has a long history of parser bugs. A file may be built to exploit the parser, exhaust memory, carry active content, hide changes made after signing, or carry a perfectly valid signature over a different document.
So the first parse happens in a separate interpreter with memory and time limits. It inspects every object in the file for active content (JavaScript, launch actions, form submission, data import, remote go-to actions, rich media, attachments), whatever role the object claims, and two independent parsers must both read the file as clean. The second parser is there because parsers disagree about malformed files, and a file that only one of them reads as clean is exactly the ambiguity an attack exploits.
How that child process ends matters too. If it is killed for running out of memory, or crashes, the file did that, and the file is rejected. If it is stopped because the server restarted, the file is not at fault, and the signature waits for another attempt. Either way, the process that verifies signatures stays up.
Whose signature is it?
Only after the gate does the cryptography begin. The file must carry exactly one signer signature (a document timestamp does not count as one), validated as PKCS#7/CMS against the ICP-Brasil and Gov.br roots. That signature must also cover the entire file. A PDF can be extended after signing with an incremental update, a new revision appended to the end, and a signature that covers only the earlier revision stays mathematically valid while the reader displays the appended content.
A valid signature still leaves a question: valid for whom? A CMS signature carries a set of certificates, and that set is not signed. The validator finds the signer through the signature's own signer information, but code that later reads "the signer's certificate" out of the set may simply take the first personal certificate it finds. The verifier did, and a review found it. The review used two fictitious identities from the test certificate set, named after mathematicians: a signature made by Turing, with Fermat's certificate placed first in the set. The cryptography was sound, and the verifier approved the signature as Fermat's.
The fix was to make every later check read the certificate the validator actually used; if that certificate is not in the set, the file was crafted, and it goes to a person. The lesson reaches beyond PDFs: in a signed format, whatever the signature does not cover is attacker input, however official it looks.
Is it this petition's text?
Each petition's official PDF carries its identifier and the SHA-256 of its current text, and the signed file must carry the same hash. Its full page text is then compared with the text the platform generated. The comparison is on text, not bytes, because signing tools rewrite the file: a byte comparison would reject every honest signature.
The petition PDF is generated as PDF/A-1b, the archival profile: fonts embedded and nothing fetched from outside the file, so it renders the same years from now. Testing a redesign showed that, in the archival conversion, any transparency in the layout, such as a logo with an alpha channel, makes the conversion flatten every page into a single image. The file still passes as valid PDF/A, but it no longer contains any text: the identifier cannot be found, and the text comparison would reject every honest signature. The petition PDF now has no transparency anywhere, and a test fails if any appears in the output.
Is the certificate still good?
The chain is built through the certificate's AIA addresses and checked for validity and revocation. Revocation fails closed: if the platform cannot establish that a certificate is unrevoked, approval waits, because accepting a revoked certificate is worse than delaying one. Every revocation list and every OCSP response has its own signature, issuer and validity checked before use, since revocation lists usually travel over plain HTTP, where anyone on the path could hand over a stale or forged one.
Those AIA, revocation-list and OCSP addresses come out of the uploaded certificate, which means they come from whoever uploaded it. A verifier that fetches whatever a certificate points to is a server-side request forgery waiting to happen: point the AIA field at an internal address and the verifier makes the request from inside the network. So they are fetched only through a restricted client: HTTP or HTTPS, public destinations only, no redirects, a bounded total time and response size.
Failing closed has a cost, and the design has to pay it somewhere. When a certificate authority's revocation service is down, the signature is not rejected. It is set aside, the signer is told it is waiting, and a periodic sweep checks it again until the service answers, then approves or rejects it without staff. The sweep claims the signature with the token from the attempt that set it aside, so a signature a person has acted on in the meantime is never touched.
One person, once
On a petition platform, the count is the obvious target, and defending it starts with knowing who signed. The certificate answers that, not the form: the CPF the signer types is only a supporting detail and has to match the certificate's, and certificates issued to companies (against a CNPJ, the company registration number) are turned away. To stop anyone signing twice, the platform looks for repeats in two ways, by the certificate's CPF and by the file's hash, and it does not leave that to the code alone: the database itself refuses a second signature from the same CPF on the same petition. Once a signature is approved, the name shown is the one in the certificate; what the person typed stays only in the evidence. And only approved signatures make it into the public count.
When the verifier does not know
A verification can fail for two very different reasons: a problem with the signature, or a problem with the verifier itself. If the verifier breaks along the way, meets a certificate type it does not recognise or cannot read the CPF in the certificate, the citizen is not at fault: the signature is neither approved nor rejected automatically, and goes to a person.
What an approval leaves behind
Every verification leaves evidence behind: the certificate data, the result of each step, the file's size and hash, the duplicate check. That record is sealed by a SHA-256 computed over it, and each signature gets a log of custody events that can only be appended to. The custody certificate is rendered once and stored with its SHA-256; once published, it is read back, and publication completes only if the digest read back matches the one stored.
The custody certificate, which shows the CPF masked, carries a PAdES certification signature with the DocMDP "no changes" permission, so any later modification breaks the seal in a PDF reader, and in production no certificate is issued without it. The seal uses the platform's own key, with its fingerprint printed on the certificate and published on the site. It attests the integrity of a document the platform issued, not a person's identity, so it does not carry the legal presumption of authenticity that ICP-Brasil signatures have. Its value comes from anyone being able to compare the fingerprint.
The custody certificate is PDF/A too, and the order matters: the archival conversion runs before the seal, and nothing touches the bytes after it, since any change would break the seal. After sealing, the producer recorded in the file's two metadata records no longer matched, and PDF/A requires them to agree; removing it from both before sealing lets the seal write the same value to each, and the sealed file stays PDF/A.
The petition's creator receives a package with MANIFEST.csv, SHA256SUMS.txt and a sealed signature list that includes the manifest's SHA-256, so sha256sum -c SHA256SUMS.txt checks the whole bundle offline. The citizen's own signature never depends on the platform at all: whoever holds the signed PDF can validate it with the validator run by ITI, the federal agency behind ICP-Brasil.
A new version of the package is under construction: structured evidence, long-term preservation of the proof and independent verification, so that a stranger can check the whole package years later, without the platform and without believing what the package says about itself.
Around the verifier
The CPF at rest. A plain SHA-256 of a CPF protects nothing: a CPF has only 10⁹ possible values, and one consumer GPU tries them all in under a minute. Lookups and the duplicate check use the CPF as an HMAC-SHA256 under a secret key that is not in the database, which makes that enumeration infeasible for someone holding only a database dump. The certificate data holds the CPF too, and there it could not be hashed or masked, because the evidence seal is computed over that data. It is stored encrypted with AES-256-GCM instead, each ciphertext bound to its row and field so it cannot be copied into another record, with versioned keys so they can be rotated.
The signed PDFs. In Gov.br's format, the file carries the signer's name, CPF, date of birth and e-mail in clear. The platform cannot touch a file whose signature it must preserve, so what is left is controlling who can reach it. The PDFs sit in a bucket of their own, encrypted at rest, with no public access and file names that reveal no personal data. Only the petition's creator and staff with a verified second factor may download them, always through the same path, which records each download and hands out a link that expires within minutes. And no page lists a person's signatures: under Brazil's data-protection law, political opinion is sensitive personal data, and the list of petitions someone signed reveals exactly that.
The audit trail. An audit log that is append-only by convention proves nothing to someone who distrusts the operator. So PostgreSQL triggers refuse any update or deletion of an audit record, with a few legitimate exceptions: setting or releasing a legal hold, unlinking a deleted account, and purging unheld records after ten years. That stops the application from rewriting the trail, but not someone with direct access to the database. The checkpoints cover that case: periodically, every record is hashed, and the result is chained to the previous checkpoint, signed with a dedicated key and written to object storage under Object Lock in compliance mode, which not even the operator can delete or overwrite before the period ends. A change that gets around the triggers, even one made by the operator, shows up against the checkpoints.
A monthly archive also keeps the records' content outside the database, encrypted with a key the application can only encrypt with. If the trail has to serve as evidence, records can be exported with selective disclosure, together with the signed checkpoints, an independent verifier and a byte-by-byte specification of the format with test vectors, so an expert can check it without the platform's code or database. The limit is omission: if the code fails to write a record, nothing flags the gap, and a false record written before its checkpoint becomes part of the trail.
The browser and the edge. The CSP is nonce-based, with 'strict-dynamic', object-src 'none', base-uri 'none', form-action 'self' and frame-ancestors 'none', and no template has an inline event handler. A host allowlist with 'unsafe-inline' blocks almost nothing, since any HTML injection could run inline script; with a nonce, only script the server put on the page runs. A nonce only works if the browser receives the HTML the origin produced, so every Cloudflare feature that rewrites pages or injects scripts is off, and HTML goes out with Cache-Control: no-transform. The origin also refuses any request that did not come through Cloudflare, since the WAF and the edge rate limits count only if nothing goes around them.
Staff. Every staff permission requires a TOTP second factor, inside and outside the admin, because the most sensitive one, downloading a signed PDF, happens outside it. E-mail codes were ruled out because NIST SP 800-63B does not accept e-mail as an out-of-band authenticator.
Petitions. The site itself can be padded with fake petitions. So a new petition stays out of the lists, the search, the sitemap and search engines until a signature on it is approved, and it is removed if none arrives. Creating one means getting past Turnstile.
The AI reviewer. Every new or edited petition gets a first read by an AI reviewer against the Terms of Use, but a person makes the decision. The reviewer has no way to change a petition, and a static scan of the code in the test suite keeps it that way. So if someone hides a prompt injection in a petition's text, the worst that can happen is a wrong suggestion reaching a person.
What it does not do yet
The custody certificate's seal is self-signed, so a PDF reader shows the sealer as unknown until someone compares the fingerprint with the published one. The seal's time comes from the server's clock, with no RFC 3161 timestamp, and there is no long-term validation yet, because revocation evidence is not archived with the signature. PDF/A keeps the documents readable over time, but not a signature verifiable; that is exactly what long-term validation would add. Some trust cannot be removed either: the operator is trusted with the database, the encryption keys and the seal key, and TLS terminates at Cloudflare's edge, which handles request contents in transit, uploads included. Staff's TOTP does not resist real-time phishing, and the platform has not yet had a third-party penetration test.
The mechanical claims on the security page are pinned by tests (the CSP, HSTS, strict revocation in production, the required second factor, a valid security.txt, no trackers on the page), and the page is reviewed every quarter. A claim that stops being true is corrected in the same change that alters the code. If you find one that is wrong, or a flaw it does not mention, the vulnerability policy says how to report it.