def Parse(self): if (self.Status != ListStatus.Success): Logger.LogInfo("Will not parse list {0}: Staus is {1} ".format( self.LocalName, self.Status)) return False if (self.MimeType == MimeType.Pdf): self.TSLType = TrustListType.Pdf Logger.LogInfo("Ignoring file {0}".format(self.LocalName)) return False try: tree = ET.parse(self.LocalPath) node = tree.find(TrustList.xpOperatorTeritory) self.OperatorTeritory = node.text node = tree.find(TrustList.xpVersion) self.TypeVersion = node.text if (self.TypeVersion != "5"): Logger.logError( "Will not parse list {0}. it has an unknown type version {1}" .format(self.LocalName, self.TypeVersion)) self.Status = ListStatus.StructureError return False node = tree.find(TrustList.xpSqNumber) self.SeqNumber = node.text # After Brexit, UK lists have an empty NextUpdate node = tree.find(TrustList.xpNextUpdate) self.NextUpdate = node.text if node is not None else "" node = tree.find(TrustList.xpType) self.TSLType = TrustListType.get_type_from_string(node.text) node = tree.find(TrustList.xpOperatorName) self.OperatorName = node.text except AttributeError as ex: Logger.LogException( "Failed to parse list {0}".format(self.LocalName), ex) self.Status = ListStatus.StructureError return False if (self.TSLType == TrustListType.ListOfTheLists): self.__parse_list_of_lists(tree) elif (self.TSLType == TrustListType.Generic): self.__parse_list_of_generic(tree) return True
def ValidateWithSchema(self): if (self.Status != ListStatus.Success): Logger.LogInfo("Will not validate list {0}: Staus is {1} ".format( self.LocalName, self.Status)) return False valid = self.eutlschema.is_valid(str(self.LocalPath)) self.Status = ListStatus.Success if self.eutlschema.is_valid( str(self.LocalPath)) else ListStatus.SchemaError Logger.LogInfo( "Validation result for list {0} with etsi schema is {1}".format( self.LocalName, self.Status)) return self.Status is ListStatus.Success
def __save_xml_certificates_cache(self, path): if (self.AllServices is None or len(self.AllServices) == 0): return False elem_root = ET.Element('trustservicesdigitalids', {"nextupdate": self.NextUpdate}) for svc in self.AllServices: if (not svc.Certificates): Logger.LogInfo( "This service {0} '{1}' of type {2} has no certificate". format(svc.CC, svc.ServiceName, svc.ServiceTypeId.name)) continue for certificate in svc.Certificates: svc_attrs = { "cc": svc.CC, "tspname": svc.TrustServiceProviderName, "tsptradename": svc.TrustServiceProviderTradeName, "servicename": svc.ServiceName, "type": svc.ServiceTypeId.value, "isqualified": IsQualifiedService(svc.ServiceTypeId.value), "status": svc.ServiceCurrentStatusId.value, "begin": svc.ServiceCurrentStatusStartTime, "x509fingerprintsha1": certificate.FingerprintSHA1, # "x509valueb64": svc.Certificates[0].Value if svc.Certificates else "" } elem_svc = ET.SubElement(elem_root, 'digitalid', svc_attrs) save_xml_to_file(elem_root, path / "trust_services.xml")
def __save_xml_list_cache(self, path): if (self.ListsOfTrust is None): return False elem_root = ET.Element('trustlists') elem_root.set('version', self.TypeVersion) elem_root.set('sequence', self.SeqNumber) for tl in self.ListsOfTrust: if (tl.Status is not ListStatus.Success): continue if (not tl.OperatorTeritory or not tl.OperatorName or not tl.LocalName or not tl.UrlLocation or not tl.NextUpdate or not tl.TypeVersion or not tl.SeqNumber): Logger.LogInfo("Some informations are missing in list " + tl.LocalName) tl_attrs = { "cc": tl.OperatorTeritory, "operator": tl.OperatorName, "name": tl.LocalName, "uri": tl.UrlLocation, "nextupdate": tl.NextUpdate if tl.NextUpdate is not None else "", "type": tl.TypeVersion, "version": tl.SeqNumber } elem_tl = ET.SubElement(elem_root, 'list', tl_attrs) save_xml_to_file(elem_root, path / "trust_lists.xml")
def delete_all_files(folder_path, pattern): if (folder_path is None or not folder_path.exists()): Logger.LogError("Folder does not exist: {0}".folder_path) return Logger.LogInfo( "Deleting existing certificate files in directory {0}".format( folder_path)) cert_files = list(folder_path.glob(pattern)) for cert_file in cert_files: os.remove(cert_file) Logger.LogInfo("Deleted {0} {1} files".format(len(cert_files), pattern))
def main(argv): options = Options() options.workingDir = pathlib.Path("./.download") options.parseCommandLine(argv) eutl_schema_path = options.xsdDir / "ts_119612v020201_201601xsd.xsd" if options.printVersionAndExit: printVersion() sys.exit() if not check_preconditions(options): Logger.LogError("Preconditions not satisfied, exiting") return False try: EuTL = TrustList(options.urlLotl, MimeType.Xml, "EU", str(eutl_schema_path)) EuTL.Update(options.localTListPath(), options.force) EuTL.DownloadChildren() EuTL.PostProcess(options.localCachePath()) EuTL.SaveCetificatesOnDisk(options.localTrustCertPath()) # EuTL.Print() Logger.LogInfo( "Found {0} trust service providers and {1} services.".format( sum(len(x.TrustServiceProviders) for x in EuTL.ListsOfTrust), len(EuTL.AllServices))) return True except Exception as ex: Logger.LogExceptionTrace("main: Got an error", ex)
def Download(self, localwd, force): if (self.UrlLocation is None): raise "TrustList.Download failed: Url is empty" self.ForceDownload = force self.LocalWD = localwd self.LocalPath = localwd / self.LocalName try: Logger.LogInfo("Downloading file {0} ...".format(self.UrlLocation)) download_file(self.UrlLocation, self.LocalPath, force) self.Status = ListStatus.Success except urllib.error.URLError as ex: self.Status = ListStatus.NotDownloaded Logger.LogException( "Failed to download list {0}".format(self.UrlLocation), ex)
def download_file(rPath, lPath, force): if (lPath.exists()): msg = "File {0} exists; {1}".format( lPath, "it will be overwritten" if force else "it will NOT be downloaded again") Logger.LogInfo(msg) if (force): os.remove(lPath) else: return True req = urllib.request.Request(rPath) req.add_header( 'User-Agent', "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.2 Safari/605.1.15" ) sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS) resp = urllib.request.urlopen(url=req, context=sslcontext) data = resp.read() lFile = open(lPath, 'xb') lFile.write(data)