Let’s make a COVID Certificate [ENCODING]

The EU Digital COVID Certificate is just a QR code, is it really that hard to make it? My good old friend Homer Simpson doesn’t want to get the vaccine, so they don’t let him enter the bar, could we make him one? Let’s find out.

In a previous post I described in detail how to decode the EU Digital COVID Certificate, now we go the other way, to generate such a QR code. Here is a brief overview taken from that previous post. If you want to try this code, you should definitely visit the previous post and copy all the maven dependencies, because I will use them here as well.

Data → JSON

This is how I generated the JSON, again with ObjectMapper.

List<VaccinationPayload> vaccinations = new ArrayList<>();
VaccinationPayload vaccine = new VaccinationPayload();
vaccine.setVaccine("1119349007"); // COVID-19 mRNA vaccine
vaccine.setAuthorizationHolder("ORG-100030215"); // Biontech
vaccine.setDoseNumber(2); // Dose number: 2
vaccine.setDoseTotalNumber(2); // Expected doses: 2

PersonPayload person = new PersonPayload();
CertificatePayload certificate = new CertificatePayload();
ObjectMapper mapper  = new ObjectMapper();

String json = mapper.writeValueAsString(certificate);

Of course, this JSON can be generated in various ways, even manually, the most important thing is that in the end it looks like this.

{"v":[{"ci":"URN:UVCI:01:DE:187/37512422923","co":"DE","dn":2,"dt":"2021-09-24","is":"Robert Koch-Institut","ma":"ORG-100030215","mp":"EU/1/20/1528","sd":2,"tg":"840539006","vp":"1119349007"}],"dob":"1956-05-12","nam":{"fn":"Simpson","gn":"Homer","fnt":"SIMPSON","gnt":"HOMER"},"ver":"1.0.0"}


You may have seen the country code DE, which means that Homer is now German, this is for a specific reason and will be revealed at the end. Now let’s make these CBOR bytes.

long issuedAtSec = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000L;
long expirationSec = LocalDateTime.now().plusYears(1).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000L;

CBORObject map = CBORObject.NewMap();
map.set(CBORObject.FromObject(1), CBORObject.FromObject("DE"));
map.set(CBORObject.FromObject(6), CBORObject.FromObject(issuedAtSec));
map.set(CBORObject.FromObject(4), CBORObject.FromObject(expirationSec));
CBORObject hcertVersion = CBORObject.NewMap();
CBORObject hcert = CBORObject.FromJSONString(json);
hcertVersion.set(CBORObject.FromObject(1), hcert);
map.set(CBORObject.FromObject(-260), hcertVersion);

byte[] cbor = map.EncodeToBytes();

This is what it looks like when encoded in CBOR.



Now comes the complicated part, we have to sign these bytes with a key issued by the German certification authority in charge of COVID certificates. So, let’s hack them…
Just kidding, we’ll make a new key and sign it with him.

OneKey privateKey = OneKey.generateKey(AlgorithmID.ECDSA_256);
byte[] kid = UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8);

Sign1Message msg = new Sign1Message();
msg.addAttribute(HeaderKeys.Algorithm, privateKey.get(KeyKeys.Algorithm), Attribute.PROTECTED);
msg.addAttribute(HeaderKeys.KID, CBORObject.FromObject(kid), Attribute.PROTECTED);

byte[] cose = msg.EncodeToBytes();

This was the most complicated part, but now we have COSE bytes.


Do not be surprised if you got different COSE bytes, in this process a new key is generated and it is almost impossible for these keys to be the same/to generate the same signature.


Compression and the rest are much simpler.

ByteArrayOutputStream stream = new ByteArrayOutputStream();
try (CompressorOutputStream deflateOut = new CompressorStreamFactory()
	stream)) {
byte[] zip = stream.toByteArray();

All zipped up and we get some bytes again that I won’t write down, because they don’t matter, they will look different every time this code is executed.


Base45 encoding goes like this.

String base45 = Base45.getEncoder().encodeToString(zip);


Final touch, adding the prefix.

String hc1 = "HC1:" + base45;

And this is what the certificate looks like, for now.


PREFIX → QR Code Image 

All that remains is to make a QR code out of this.

QRCodeWriter qrCodeWriter = new QRCodeWriter();
int width = 300, height = 300; //px
BitMatrix bitMatrix = qrCodeWriter.encode(hc1,
	BarcodeFormat.QR_CODE, width, height);
// Write to file
MatrixToImageWriter.writeToPath(bitMatrix, "PNG",
	new File(elHomo.png).toPath());

And here it is.


The only thing left is to scan the code with the CovPas app. And… D’oh! It doesn’t work. That app was made by IBM and they are not so naive.

But there is hope, and it all lies in the fact that we made Homer a German. To uncover the secret, the Corona-Warn-App, before version 2.7, did not perform a digital signature check at all. This means that it will accept our certificate without any problems. You don’t believe me, download the Corona-Warn-App 2.6.1 and scan this previously generated QR code.

Guess who’s now staying in the bar until dawn, let’s hope no one will actually check his certificate.

4 thoughts on “Let’s make a COVID Certificate [ENCODING]

  1. Daniel Harmann says:

    Excellent Article
    Small question at the JSON – CBOR step.
    You have converted the JSON data to CBOR before the COSE signing stage.
    Do you also need to convert the protected/unprotected headers to CBOR BEFORE the COSE signing stage?

  2. Daniel Harmann says:

    Hi, me again.
    Thanks for your reply to the last comment.
    At the CBOR-COSE step, I have found that decoding a QR is not possible if the cbor.tag “18” has not been encoded at the COSE stage.
    The COSE step would look like this {18: [[headers],[payload],[signature]]} and not just {[[headers],[payload],[signature]]}
    Do you have any idea how you would encode the cbor.tag of 18 into the COSE step.


Leave a Reply

Your email address will not be published. Required fields are marked *