File: trustedlist.py

package info (click to toggle)
python-a38 0.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 440 kB
  • sloc: python: 4,065; xml: 174; makefile: 80; sh: 14
file content (256 lines) | stat: -rw-r--r-- 9,678 bytes parent folder | download | duplicates (2)
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
249
250
251
252
253
254
255
256
import base64
import logging
import re
import subprocess

try:
    from defusedxml import ElementTree as ET
except ModuleNotFoundError:
    import xml.etree.ElementTree as ET

from collections import defaultdict
from pathlib import Path
from typing import Dict

from cryptography import x509

from . import fields, models

log = logging.getLogger("__name__")

NS = "http://uri.etsi.org/02231/v2#"
NS_XMLDSIG = "http://www.w3.org/2000/09/xmldsig#"
NS_ADDTYPES = "http://uri.etsi.org/02231/v2/additionaltypes#"


class OtherInformation(models.Model):
    __xmlns__ = NS
    tsl_type = fields.NotImplementedField(xmltag="TSLType", xmlns=NS)
    scheme_territory = fields.StringField(null=True, xmlns=NS)
    mime_type = fields.StringField(null=True, xmlns=NS_ADDTYPES)
    scheme_operator_name = fields.NotImplementedField(xmlns=NS)
    scheme_type_community_rules = fields.NotImplementedField(xmlns=NS)


class AdditionalInformation(models.Model):
    __xmlns__ = NS
    other_information = fields.ModelListField(OtherInformation)


class OtherTSLPointer(models.Model):
    __xmlns__ = NS
    tsl_location = fields.StringField(xmltag="TSLLocation", xmlns=NS)
    service_digital_identities = fields.NotImplementedField(xmlns=NS)
    additional_information = AdditionalInformation


class PointersToOtherTSL(models.Model):
    __xmlns__ = NS
    other_tsl_pointer = fields.ModelListField(OtherTSLPointer)


class SchemeInformation(models.Model):
    __xmlns__ = NS
    pointers_to_other_tsl = fields.ModelField(PointersToOtherTSL)
    tsl_version_identifier = fields.NotImplementedField(xmltag="TSLVersionIdentifier", xmlns=NS)
    tsl_sequence_number = fields.NotImplementedField(xmltag="TSLSequenceNumber", xmlns=NS)
    tsl_type = fields.NotImplementedField(xmltag="TSLType", xmlns=NS)
    scheme_operator_name = fields.NotImplementedField(xmlns=NS)
    scheme_operator_address = fields.NotImplementedField(xmlns=NS)
    scheme_information_uri = fields.NotImplementedField(xmltag="SchemeInformationURI", xmlns=NS)
    scheme_name = fields.NotImplementedField(xmlns=NS)
    status_determination_approach = fields.NotImplementedField(xmlns=NS)
    scheme_type_community_rules = fields.NotImplementedField(xmlns=NS)
    scheme_territory = fields.NotImplementedField(xmlns=NS)
    policy_or_legal_notice = fields.NotImplementedField(xmlns=NS)
    historical_information_period = fields.NotImplementedField(xmlns=NS)
    list_issue_date_time = fields.NotImplementedField(xmlns=NS)
    next_update = fields.NotImplementedField(xmlns=NS)
    distribution_points = fields.NotImplementedField(xmlns=NS)


class TSPInformation(models.Model):
    __xmlns__ = NS
    tsp_name = fields.NotImplementedField(xmltag="TSPName", xmlns=NS)
    tsp_trade_name = fields.NotImplementedField(xmltag="TSPTradeName", xmlns=NS)
    tsp_address = fields.NotImplementedField(xmltag="TSPAddress", xmlns=NS)
    tsp_information_url = fields.NotImplementedField(xmltag="TSPInformationURI", xmlns=NS)


class DigitalId(models.Model):
    __xmlns__ = NS
    x509_subject_name = fields.StringField(xmltag="X509SubjectName", xmlns=NS, null=True)
    x509_ski = fields.StringField(xmltag="X509SKI", xmlns=NS, null=True)
    x509_certificate = fields.StringField(xmltag="X509Certificate", xmlns=NS, null=True)


class ServiceDigitalIdentity(models.Model):
    __xmlns__ = NS
    digital_id = fields.ModelListField(DigitalId)


class ServiceInformation(models.Model):
    __xmlns__ = NS
    service_type_identifier = fields.StringField(xmlns=NS)
    service_name = fields.NotImplementedField(xmlns=NS)
    service_digital_identity = ServiceDigitalIdentity
    service_status = fields.StringField(xmlns=NS)
    status_starting_time = fields.NotImplementedField(xmlns=NS)
    service_information_extensions = fields.NotImplementedField(xmlns=NS)


class TSPService(models.Model):
    __xmlns__ = NS
    service_information = ServiceInformation
    service_history = fields.NotImplementedField(xmlns=NS)


class TSPServices(models.Model):
    __xmlns__ = NS
    tsp_service = fields.ModelListField(TSPService)


class TrustServiceProvider(models.Model):
    __xmlns__ = NS
    tsp_information = TSPInformation
    tsp_services = TSPServices


class TrustServiceProviderList(models.Model):
    __xmlns__ = NS
    trust_service_provider = fields.ModelListField(TrustServiceProvider)


class TrustServiceStatusList(models.Model):
    __xmlns__ = NS
    scheme_information = SchemeInformation
    signature = fields.NotImplementedField(xmlns=NS_XMLDSIG)
    trust_service_provider_list = TrustServiceProviderList

    def get_tsl_pointer_by_territory(self, territory):
        for other_tsl_pointer in self.scheme_information.pointers_to_other_tsl.other_tsl_pointer:
            territory = None
            for oi in other_tsl_pointer.additional_information.other_information:
                if oi.scheme_territory is not None:
                    territory = oi.scheme_territory
                    break
            if territory != "IT":
                continue
            return other_tsl_pointer.tsl_location


def auto_from_etree(root):
    expected_tag = "{{{}}}TrustServiceStatusList".format(NS)
    if root.tag != expected_tag:
        raise RuntimeError("Root element {} is not {}".format(root.tag, expected_tag))

    res = TrustServiceStatusList()
    res.from_etree(root)
    return res


def load_url(url: str):
    """
    Return a TrustedServiceStatusList instance from the XML downloaded from the
    given URL
    """
    import requests
    res = requests.get(url)
    res.raise_for_status()
    root = ET.fromstring(res.content)
    return auto_from_etree(root)


def load_certs() -> Dict[str, x509.Certificate]:
    """
    Download trusted list certificates for Italy, parse them and return a dict
    mapping certificate names good for use as file names to cryptography.x509
    certificates
    """
    re_clean_fname = re.compile(r"[^A-Za-z0-9_-]")

    eu_url = "https://ec.europa.eu/information_society/policy/esignature/trusted-list/tl-mp.xml"
    log.info("Downloading EU index from %s", eu_url)
    eu_tl = load_url(eu_url)
    it_url = eu_tl.get_tsl_pointer_by_territory("IT")
    log.info("Downloading IT data from %s", it_url)
    trust_service_status_list = load_url(it_url)

    by_name = defaultdict(list)
    for tsp in trust_service_status_list.trust_service_provider_list.trust_service_provider:
        for tsp_service in tsp.tsp_services.tsp_service:
            si = tsp_service.service_information
            if si.service_status not in (
                    "http://uri.etsi.org/TrstSvc/TrustedList/Svcstatus/recognisedatnationallevel",
                    "http://uri.etsi.org/TrstSvc/TrustedList/Svcstatus/granted"):
                continue
            if si.service_type_identifier not in (
                    "http://uri.etsi.org/TrstSvc/Svctype/CA/QC",):
                continue
            # print("identifier", si.service_type_identifier)
            # print("status", si.service_status)
            cert = []
            sn = []
            for di in si.service_digital_identity.digital_id:
                if di.x509_subject_name is not None:
                    sn.append(di.x509_subject_name)
                # if di.x509_ski is not None:
                #    print("  SKI:", di.x509_ski)
                if di.x509_certificate is not None:
                    from cryptography import x509
                    from cryptography.hazmat.backends import default_backend
                    der = base64.b64decode(di.x509_certificate)
                    cert.append(x509.load_der_x509_certificate(der, default_backend()))

            if len(cert) == 0:
                raise RuntimeError("{} has no certificates".format(sn))
            elif len(cert) > 1:
                raise RuntimeError("{} has {} certificates".format(sn, len(cert)))
            else:
                from cryptography.x509.oid import NameOID
                cert = cert[0]
                cn = cert.subject.get_attributes_for_oid(NameOID.COMMON_NAME)[0].value
                # print("sn", sn)
                # print(cert)
                # print("full cn", cert.subject.get_attributes_for_oid(NameOID.COMMON_NAME))
                # print("cn", cn)
                fname = re_clean_fname.sub("_", cn)
                by_name[fname].append(cert)

    res = {}
    for name, certs in by_name.items():
        if len(certs) == 1:
            if name in res:
                raise RuntimeError("{} already in result".format(name))
            res[name] = certs[0]
        else:
            for idx, cert in enumerate(certs, start=1):
                idxname = name + "_a38_{}".format(idx)
                if idxname in res:
                    raise RuntimeError("{} already in result".format(name))
                res[idxname] = cert
    return res


def update_capath(destdir: Path, remove_old=False):
    from cryptography.hazmat.primitives import serialization
    certs = load_certs()
    if destdir.is_dir():
        current = set(c.name for c in destdir.iterdir() if c.name.endswith(".crt"))
    else:
        current = set()
        destdir.mkdir(parents=True)
    for name, cert in certs.items():
        fname = name + ".crt"
        current.discard(fname)
        pathname = destdir / fname
        with pathname.open(mode="wb") as fd:
            fd.write(cert.public_bytes(serialization.Encoding.PEM))
            log.info("%s: written", pathname)
    if remove_old:
        for fname in current:
            pathname = destdir / fname
            pathname.unlink()
            log.info("%s: removed", pathname)

    subprocess.run(["openssl", "rehash", destdir], check=True)