GithubHelp home page GithubHelp logo

ehn-dcc-development / ehn-sign-verify-python-trivial Goto Github PK

View Code? Open in Web Editor NEW
76.0 17.0 41.0 98 KB

Extremely minimal python implementation of the eHN-S protocol.

License: European Union Public License 1.2

Python 89.80% Shell 10.20%
issuer verifier

ehn-sign-verify-python-trivial's Introduction

Trivial/rudimentary eHN-simplified implementation

Aligned with version 1.00 / 2021-4-14 of the spec: https://github.com/ehn-digital-green-development/hcert-spec/blob/main/hcert_spec.md

For round-trip testing of hc1_sign.py and hc1_verify.py take some JSON, e.g. { "Foo" : "Bar }, CBOR package, COSE sign, compress and base45 convert it for use in a QR.

  1. COSE sign
    1. compact the JSON into CBOR
    2. wrap it into a payload (Health claim -260, add issuer/dates)
    3. sign and package as a COSE message
    4. ZLIB compress
    5. Base45 encode
  2. COSE verify
    1. Base45 decode
    2. ZLIB decompress
    3. check the signature on the COSE message
    4. unpack the CBOR into JSON
    5. unpack the payload and extract the issuer and dates
    6. unpack the health claim and output as json.

Decoding a barcode from production (i.e. a DCC in the wild)

 qrdecode photo.jpg | python3 ./hc1_verify.py -v -U -p

or

 qrdecode photo.jpg | python3 ./hc1_verify.py -v -i -p

The first will check against the Dutch copy of the eHealth trustlist; the second version, with the -i, will not check the actual signature. The typical output will look like:

Correct signature against known key (kid=3lTmAZX19GQ=)
Issuer              : NL
Experation time     : 1626966160
Issued At           : 1624546960
Health payload      : {
    "dob": "XXXX-XX-XX",
    "nam": {
        "fn": "xxx Xxxxx",
        "fnt": "XXX<XXXXX",
        "gn": "Xxxx Xxxxxx",
        "gnt": "XXXX<XXXXXX"
    },
    "v": [
        {
            "ci": "URN:UCI:01:NL:......#:",
            "co": "NL",
            "dn": 1,
            "dt": "2021-06-07",
            "is": "Ministry of Health Welfare and Sport",
            "ma": "ORG-100001417",
            "mp": "EU/1/20/1525",
            "sd": 1,
            "tg": "840539006",
            "vp": "J07BX03"
        }
    ],
    "ver": "1.3.0"
}

Test Steps

  1. Generate the CSCA and DSC with ./gen-csca-dsc.sh
  2. Run the command: echo '{"A": 1234}' | python3.8 hc1_sign.py | python3.8 hc1_verify.py
  3. You should see the output: {"A": 1234}
echo '{"Foo":1, "Bar":{"Field1":"a value", "integer":1212112121}}' | \
    python3 hc1_sign.py | \
    python3 hc1_verify.py --prettyprint-json

Which should output:

{
    "Foo": 1, 
    "Bar": {
        "Field1": "a value", 
        "integer": 1212112121
   }
}

Testing COSE from Austrian website

Testing against the AT cases:

  1. Fetch the Base45 from https://dev.a-sit.at/certservice

  2. Remove the first 2 bytes and do

    pbpaste | sed -e 's/^00//' | \
        python3 hc1_verify.py --base64 --ignore-signature --cbor

ehn-sign-verify-python-trivial's People

Contributors

abgeo avatar basicmaster avatar confiks avatar dirkx avatar henkpoley avatar jakub-kk avatar justin-fay avatar kirisakow avatar lmammino avatar panzi avatar quapka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ehn-sign-verify-python-trivial's Issues

Support RSASSA-PSS in h1_verify.py

h1_verify.py assumes that ECDSA is used for verification, see Digitaler-Impfnachweis/certification-apis#88 (comment) for context.

The CI script used in https://github.com/eu-digital-green-certificates/dgc-testdata shows how RSASSA-PSS can be supported:

        if isinstance(cert.public_key(), rsa.RSAPublicKey):
            e = int_to_bytes(cert.public_key().public_numbers().e)
            n = int_to_bytes(cert.public_key().public_numbers().n)
        elif isinstance(cert.public_key(), ec.EllipticCurvePublicKey):
            x = int_to_bytes(cert.public_key().public_numbers().x)
            y = int_to_bytes(cert.public_key().public_numbers().y)

        # ...

        if x and y:
            key = CoseKey.from_dict(
                {
                    KpKeyOps: [VerifyOp],
                    KpKty: KtyEC2,
                    EC2KpCurve: P256,  # Ought o be pk.curve - but the two libs clash
                    KpAlg: Es256,  # ECDSA using P-256 and SHA-256
                    EC2KpX: x,
                    EC2KpY: y,
                }
            )
        elif e and n:
            key = CoseKey.from_dict(
                {
                    KpKeyOps: [VerifyOp],
                    KpKty: KtyRSA,
                    KpAlg: Ps256,  # RSASSA-PSS using SHA-256 and MGF1 with SHA-256
                    RSAKpE: e,
                    RSAKpN: n,
                }
            )

Source

ModuleNotFoundError: No module named 'cose.curves'

Trying to execute echo "{'A': 1234}" | python3 hc1_sign.py | python3 hc1_verify.py
this error occurs

Traceback (most recent call last):
  File "/home/pi/ehn-sign-verify-python-trivial/hc1_sign.py", line 13, in <module>
    from cose.curves import P256
ModuleNotFoundError: No module named 'cose.curves'
Traceback (most recent call last):
  File "/home/pi/ehn-sign-verify-python-trivial/hc1_verify.py", line 16, in <module>
    from cose.curves import P256
ModuleNotFoundError: No module named 'cose.curves'

Of course module is installed via pip3 install cose
What is wrong here?

QRdecode not exist

Hi all,
QRDECODE is not present on OSX Big Sur and cannot install with "brew install qrdecode".
Any idea how pass a QRcode image png value to script?
Thanks

IndexError: index out of range

Traceback (most recent call last):
File "./hc1_verify.py", line 155, in
if (cin[0] == 0x78):
IndexError: index out of range

hc1_verify.py argument "--xy" doesn't work, tries to load certificate from file instead

When using "--xy" argument to provide X,Y elliptic curve point (public key) for hc1_verify.py to use when verifying signature of DCC, the program outputs an error (in fresh repo without dsc-worker.pem file):

Traceback (most recent call last):
  File "hc1_verify.py", line 203, in <module>
    with open(args.cert, "rb") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'dsc-worker.pem'

What should happen is that instead of trying to load public from certificate located in the file (which doesn't exist in repo and must be manually created to be present, as action not needed for signature validation of DCC generated by official means), the program should accept given argument --xy as public key and use it to verify DCC signature.

Note: using only --xy option to provide public key is not sufficient, per hc1_verify.py usage one must also provide key id (KID) using --kid argument or request that KID is ignored using --ignore-kid. At present using those arguments also results in erroneous behavior described above.

AttributeError: 'bytes' object has no attribute 'encode'

when i run: echo '(my json code)' | python3 hc1_sign.py | python3 hc1_verify.py

I get two errors

Traceback (most recent call last):
File "/Downloads/ehn-sign-verify-python-trivial/hc1_sign.py", line 149, in
out = b'HC1:' + b45encode(out).encode('ascii')
AttributeError: 'bytes' object has no attribute 'encode'

File "/Downloads/ehn-sign-verify-python-trivial/hc1_verify.py", line 153, in
if (cin[0] == 0x78):
IndexError: index out of range

IndexError: index out of range

Testing the utility i get the following traceback and error:

echo '{"A": 1234}' | python3 hc1_sign.py | python3 hc1_verify.py

Traceback (most recent call last):
File "hc1_sign.py", line 149, in
out = b'HC1:' + b45encode(out).encode('ascii')
AttributeError: 'bytes' object has no attribute 'encode'
Traceback (most recent call last):
File "hc1_verify.py", line 153, in
if (cin[0] == 0x78):
IndexError: index out of range

python3 -V
Python 3.7.3

'{"A": 1234}' not "{'A': 1234}"

Hi,

Pretty sure in the readme.md it is supposed to be
Run the command: echo '{"A": 1234}' | python3.8 hc1_sign.py | python3.8 hc1_verify.py

minor thing, but hey :)

echo "{'A': 1234}" | python3 hc1_sign.py | python3 hc1_verify.py

Traceback (most recent call last):
  File "/root/Desktop/ehn-sign-verify-python-trivial/hc1_sign.py", line 85, in <module>
    payload = json.loads(payload.decode("utf-8"))
  File "/usr/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.9/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Traceback (most recent call last):
  File "/root/Desktop/ehn-sign-verify-python-trivial/hc1_verify.py", line 153, in <module>
    if (cin[0] == 0x78):
IndexError: index out of range

Any idea whats wrong?

Permission denied

when I run: ./pem2json-kid.sh
I get: zsh: permission denied: ./pem2json-kid.sh

Verify DGC through 3rd party service

Hello @dirkx ,

I am from an airline IT team and we are creating an application to use DGC for retrieving Vaccination information. This information is needed to determine whether traveler is allowed to fly to particular destination based on destination country covid rules. We are able to decode the DGC QR code using the standard libraries but struggling with Verification process. We referred the below official docs, but couldn't figure out how a third party can verify a DGC. Our use case is to accept DGC for all countries.
https://ec.europa.eu/health/sites/default/files/ehealth/docs/digital-green-certificates_v4_en.pdf
https://ec.europa.eu/health/sites/default/files/ehealth/docs/digital-green-certificates_v5_en.pdf

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.