forked from berdav/greenpass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreenpass.py
executable file
·248 lines (184 loc) · 6.91 KB
/
greenpass.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python3
# Green Pass Parser
# Copyright (C) 2021 Davide Berardi -- <berardi.dav@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from greenpass.api import CertificateUpdater
from greenpass.api import ForcedCertificateUpdater
from greenpass.api import CachedCertificateUpdater
from greenpass.input import InputTransformer
from greenpass.output import OutputManager, NoneOutput
from greenpass.logic import GreenPassParser, LogicManager
from greenpass.settings import SettingsManager
import os
import sys
import shutil
import locale
import argparse
import platform
import colorama
import functools
# Cache Directory
DEFAULT_CACHE_DIR = functools.reduce(
os.path.join,
[os.path.expanduser("~"), ".local", "greenpass"]
)
def setup_argparse():
parser = argparse.ArgumentParser()
command = parser.add_mutually_exclusive_group(required=True)
command.add_argument("--settings",
action="store_true",
help="Dump VerificaC19 settings")
command.add_argument("--qr",
help="qrcodefile, png format")
command.add_argument("--pdf",
help="qrcodefile, pdf format")
command.add_argument("--txt",
help="read qrcode content from file")
# Optional Parameters
parser.add_argument("--cachedir",
default=DEFAULT_CACHE_DIR,
help="Cache Dir, default:{}".format(DEFAULT_CACHE_DIR))
parser.add_argument("--raw",
action="store_true",
help="print raw data of certificate in json format")
# On windows disable colors by default (CMD currently do not parse)
parser.add_argument("--no-color",
action="store_true",
help="Disable color output")
parser.add_argument("--force-color",
action="store_true",
help="Force color output")
parser.add_argument("--no-cache",
action="store_true",
help="Do not use cache")
parser.add_argument("--clear-cache",
action="store_true",
help="Remove the cache directory")
parser.add_argument("--key",
help="Public certificate to verify the greenpass with")
parser.add_argument("--verbose",
action="store_true",
help="Print more information")
parser.add_argument("--dump-sign",
action="store_true",
help="Dump the signature included in the certificate")
parser.add_argument("--no-block-list",
action="store_true",
help="Do not consider block list")
parser.add_argument("--at-date",
help="Use AT_DATE instead of the current date")
parser.add_argument("--recovery-expiration",
action="store_true",
help="Consider the recovery certificate expiration")
parser.add_argument("--batch",
action="store_true",
help="Do not print anything")
parser.add_argument("--language",
help="Select the language, use two letter code")
return parser.parse_args()
def manage_cache(cachedir, no_cache, clear_cache):
outcachedir = cachedir
if no_cache:
outcachedir = ''
elif clear_cache:
shutil.rmtree(outcachedir)
return outcachedir
def _setup_colors():
colorama.init()
from termcolor import colored
return colored
def _disable_colors():
def _uncolor(x, y):
return x
return _uncolor
def _is_windows():
return platform.system == "Windows"
def init_colors(no_color, force_color):
if _is_windows():
# On windows, CMD do not parse colors, disable them by default
no_color = True
# On other systems, terminals usually parse colors, use the
# passed settings
if force_color:
# If Forced, use colored output
colored = _setup_colors()
elif no_color:
# If selected no color do not use colors
colored = _disable_colors()
else:
colored = _setup_colors()
return colored
def get_filetype(args):
(path, filetype) = (None, None)
if args.qr is not None:
(path, filetype) = (args.qr, "png")
if args.pdf is not None:
(path, filetype) = (args.pdf, "pdf")
if args.txt is not None and args.txt != "":
(path, filetype) = (args.txt, "txt")
return (path, filetype)
def get_language(lang):
return lang.split("_")[0]
def main():
# Get the arguments
args = setup_argparse()
# Configure the cache directory
cachedir = manage_cache(args.cachedir, args.no_cache, args.clear_cache)
# Configure colored output
colored = init_colors(args.no_color, args.force_color)
sm = SettingsManager(cachedir, args.recovery_expiration)
language = get_language(locale.getdefaultlocale()[0])
if args.language is not None:
language = args.language
if args.at_date is not None:
sm.set_at_date(args.at_date)
(path, filetype) = get_filetype(args)
if args.batch:
om = NoneOutput
else:
om = OutputManager
if args.settings:
out = om(colored)
out.dump_settings(sm)
return 1
data = InputTransformer(path, filetype).get_data()
gpp = GreenPassParser(data)
out = om(colored)
if args.raw:
gpp.dump(out)
return 1
logic = LogicManager(cachedir)
if args.key is not None:
cup = ForcedCertificateUpdater(args.key)
elif cachedir != '':
cup = CachedCertificateUpdater(cachedir)
else:
cup = CertificateUpdater()
if args.verbose:
cup.set_verbose()
if args.dump_sign:
signature = gpp.get_sign_from_cose()
phdr, uhdr = gpp.get_headers_from_cose()
out.dump_cose(phdr, uhdr, signature)
return 1
cert = gpp.get_certificate()
res = logic.verify_certificate(cert, sm, cup,
enable_blocklist=not args.no_block_list)
out.print_cert(cert, cachedir=cachedir, language=language)
out.dump()
# Unix return code is inverted
return not res
if __name__ == "__main__":
sys.exit(main())