Skip to content

Instantly share code, notes, and snippets.

View dajiaji's full-sized avatar
🏠
Working from home

Ajitomi Daisuke dajiaji

🏠
Working from home
  • Toshiba Corporation / bibital LLC
  • Tokyo, Japan
  • X @dajiaji
View GitHub Profile
@dajiaji
dajiaji / python_cwt_sample_eudcc_01.py
Last active October 16, 2021 13:20
A simple example of EUDCC verification with Python CWT.
import cwt
from cwt import Claims, load_pem_hcert_dsc
# A DSC(Document Signing Certificate) issued by a CSCA (Certificate Signing Certificate Authority)
# quoted from: https://github.com/eu-digital-green-certificates/dgc-testdata/blob/main/AT/2DCode/raw/1.json
dsc = "-----BEGIN CERTIFICATE-----\nMIIBvTCCAWOgAwIBAgIKAXk8i88OleLsuTAKBggqhkjOPQQDAjA2MRYwFAYDVQQDDA1BVCBER0MgQ1NDQSAxMQswCQYDVQQGEwJBVDEPMA0GA1UECgwGQk1TR1BLMB4XDTIxMDUwNTEyNDEwNloXDTIzMDUwNTEyNDEwNlowPTERMA8GA1UEAwwIQVQgRFNDIDExCzAJBgNVBAYTAkFUMQ8wDQYDVQQKDAZCTVNHUEsxCjAIBgNVBAUTATEwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASt1Vz1rRuW1HqObUE9MDe7RzIk1gq4XW5GTyHuHTj5cFEn2Rge37+hINfCZZcozpwQKdyaporPUP1TE7UWl0F3o1IwUDAOBgNVHQ8BAf8EBAMCB4AwHQYDVR0OBBYEFO49y1ISb6cvXshLcp8UUp9VoGLQMB8GA1UdIwQYMBaAFP7JKEOflGEvef2iMdtopsetwGGeMAoGCCqGSM49BAMCA0gAMEUCIQDG2opotWG8tJXN84ZZqT6wUBz9KF8D+z9NukYvnUEQ3QIgdBLFSTSiDt0UJaDF6St2bkUQuVHW6fQbONd731/M4nc=\n-----END CERTIFICATE-----"
# An EUDCC (EU Digital COVID Certificate)
# quoted from: https://github.com/eu-digital-green-cer
@dajiaji
dajiaji / python_cwt_sample_eudcc_02.py
Last active November 17, 2021 11:50
A simple EUDCC verifier implementation with Python CWT.
import json
import os
import zlib
import requests
from base45 import b45decode
import cwt
from cwt import load_pem_hcert_dsc
@dajiaji
dajiaji / python_cwt_sample_eudcc_02_1.py
Last active July 12, 2021 10:50
A simple EUDCC verifier implementation with Python CWT: refresh_trustlist
def refresh_trustlist(self):
status = 200
headers = None
# Get new DSCs
x_resume_token = (
self._trustlist[len(self._trustlist) - 1]["x_resume_token"]
if self._trustlist
else ""
)
@dajiaji
dajiaji / python_cwt_sample_eudcc_02_2.py
Last active July 12, 2021 10:50
A simple EUDCC verifier implementation with Python CWT: verify_and_decode
def verify_and_decode(self, eudcc: bytes) -> bytes:
if eudcc.startswith(b"HC1:"):
# Decode Base45 data.
eudcc = b45decode(eudcc[4:])
# Decompress with zlib.
eudcc = zlib.decompress(eudcc)
# Verify and decode CWT.
return cwt.decode(eudcc, keys=self._dscs)
@dajiaji
dajiaji / python_cwt_sample_eudcc_02_3.py
Last active July 12, 2021 10:49
A simple EUDCC verifier implementation with Python CWT: main
# An endpoint of Digital Green Certificate Verifier Service compliant with:
# https://eu-digital-green-certificates.github.io/dgca-verifier-service/
BASE_URL = os.environ["CWT_SAMPLES_EUDCC_BASE_URL"]
# e.g., "./dscs.json"
TRUSTLIST_STORE_PATH = os.environ["CWT_SAMPLES_EUDCC_TRUSTLIST_STORE_PATH"]
# quoted from https://github.com/eu-digital-green-certificates/dgc-testdata/blob/main/AT/2DCode/raw/1.json
BASE45_FORMATTED_EUDCC = b"HC1:NCFOXN%TS3DH3ZSUZK+.V0ETD%65NL-AH-R6IOOK.IR9B+9G4G50PHZF0AT4V22F/8X*G3M9JUPY0BX/KR96R/S09T./0LWTKD33236J3TA3M*4VV2 73-E3GG396B-43O058YIB73A*G3W19UEBY5:PI0EGSP4*2DN43U*0CEBQ/GXQFY73CIBC:G 7376BXBJBAJ UNFMJCRN0H3PQN*E33H3OA70M3FMJIJN523.K5QZ4A+2XEN QT QTHC31M3+E32R44$28A9H0D3ZCL4JMYAZ+S-A5$XKX6T2YC 35H/ITX8GL2-LH/CJTK96L6SR9MU9RFGJA6Q3QR$P2OIC0JVLA8J3ET3:H3A+2+33U SAAUOT3TPTO4UBZIC0JKQTL*QDKBO.AI9BVYTOCFOPS4IJCOT0$89NT2V457U8+9W2KQ-7LF9-DF07U$B97JJ1D7WKP/HLIJL8JF8JFHJP7NVDEBU1J*Z222E.GJ457661CFFTWM-8P2IUE7K*SSW613:9/:TT5IYQBTBU16R4I1A/9VRPJ-TS.7ZEM7MSVOCD4RG2L-TQJROXL2J:52J7F0Q10S
@dajiaji
dajiaji / python_cwt_sample_eudcc_03.py
Last active February 23, 2022 23:03
A simple EUDCC verifier implementation using public trust list provided on dgcg.covidbevis.se with Python CWT.
import json
import os
import zlib
import jwt
import requests
from base45 import b45decode
import cwt
from cwt import COSEKey
@dajiaji
dajiaji / python_cwt_sample_01.py
Last active July 12, 2021 10:56
A simple sample of signed CWT.
import cwt
from cwt import COSEKey
# The sender side:
private_key = COSEKey.from_jwk({
"kid": "01",
"kty": "OKP",
"key_ops": ["sign"],
"alg": "EdDSA",
"crv": "Ed25519",
@dajiaji
dajiaji / python_cwt_sample_eudcc_03_1.py
Created July 12, 2021 23:13
A simple EUDCC verifier implementation with Python CWT: refresh_trustlist
def refresh_trustlist(self):
self._dscs = []
self._trustlist = []
# Get a trust-list signer certificate.
r = requests.get(self._base_url + "/cert")
if r.status_code != 200:
raise Exception(f"Received {r.status_code} from /cert")
key = r.text
cose_key = COSEKey.from_pem(key)