summaryrefslogtreecommitdiff
path: root/verify.py
blob: fe873b3f1573d874ee13c34e7f9ecec8e7ea94c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import argparse
import base64
import json

from pathlib import Path

from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding

from PIL import Image
from pyzbar import pyzbar


def cert(name):
    return Path(__file__).absolute().parent / 'certs' / name


def verify(qr_code_bytes):
    b64, payload = qr_code_bytes.split(b'#', maxsplit=1)
    sig = base64.decodebytes(b64)

    h = hashes.Hash(hashes.SHA256())
    h.update(payload.decode().encode('utf8'))
    digest = h.finalize()

    with open(cert("RamzorQRPubKey.pem"), "rb") as f:
        k = serialization.load_pem_public_key(f.read())
        k.verify(
            sig,
            digest,
            padding.PKCS1v15(),
            hashes.SHA256(),
        )

    print("Valid signature!")

    data = json.loads(payload)
    if data['ct'] == 1:
        for i in range(len(data['p'])):
            print(f"Details of person number {i+1}:")
            print(f"\tIsraeli ID Number {data['p'][i]['idl']}")
            print(f"\tID valid by {data['p'][i]['e']}")
        print(f"Cert Unique ID {data['id']}")
    elif data['ct'] == 2:
        print(f"Israeli ID Number {data['idl']}")
        print(f"ID valid by {data['e']}")
        print(f"Cert Unique ID {data['id']}")
    else:
        print("Unsupported certificate type")


def read_qr_code(image_path):
    return pyzbar.decode(Image.open(image_path))[0].data


def create_arg_parser():
    parser = argparse.ArgumentParser("GreenPass QR code verifier")
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument(
        "-i", "--image_path", type=Path, help="Path to an image with the QR code", default=None)
    group.add_argument(
        "-t", "--txt_path", type=Path, help="Path to decoded QR code textual content", default=None)
    return parser


if __name__ == '__main__':
    # Parse arguments
    parser = create_arg_parser()
    args = parser.parse_args()

    # Choose correct input
    if args.image_path:
        verify(read_qr_code(args.image_path))
    elif args.txt_path:
        with open(args.txt_path, 'rb') as f:
            verify(f.read().strip())