Skip to content

Commit

Permalink
Merge pull request #493 from martenrebane/MOPPIOS-1412
Browse files Browse the repository at this point in the history
Fix Xades files handling
  • Loading branch information
Counter178 authored Jun 27, 2024
2 parents af45704 + 825ae79 commit f4cefd2
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 18 deletions.
4 changes: 2 additions & 2 deletions MoppApp/MoppApp/ContainerActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ extension ContainerActions where Self: UIViewController {
if (isAsicOrPadesContainer || isCdocContainer) && urls.count == 1 {
SiVaUtil.setIsSentToSiva(isSent: false)

if let firstUrl = urls.first, (firstUrl.pathExtension == "asics" || firstUrl.pathExtension == "scs") || firstUrl.pathExtension == "ddoc" || (firstUrl.pathExtension == "pdf" && SiVaUtil.isSignedPDF(url: firstUrl as CFURL)) || MimeTypeExtractor.isCadesContainer(filePath: firstUrl) {
if let firstUrl = urls.first, ((firstUrl.pathExtension == "asics" || firstUrl.pathExtension == "scs") && !MimeTypeExtractor.isXadesContainer(filePath: firstUrl)) || firstUrl.pathExtension == "ddoc" || (firstUrl.pathExtension == "pdf" && SiVaUtil.isSignedPDF(url: firstUrl as CFURL)) || MimeTypeExtractor.isCadesContainer(filePath: firstUrl) {
if self?.getTopViewController() is FileImportProgressViewController {
self?.dismiss(animated: true, completion: {
SiVaUtil.displaySendingToSiVaDialog { hasAgreed in
Expand Down Expand Up @@ -165,7 +165,7 @@ extension ContainerActions where Self: UIViewController {
let fileURL = URL(fileURLWithPath: newFilePath)
let fileExtension = fileURL.pathExtension
let isSignedPDF = SiVaUtil.isSignedPDF(url: fileURL as CFURL)
if (forbiddenFileExtensions.contains(fileExtension) || isSignedPDF || MimeTypeExtractor.isCadesContainer(filePath: fileURL)) {
if (forbiddenFileExtensions.contains(fileExtension) || isSignedPDF || (MimeTypeExtractor.isCadesContainer(filePath: fileURL) && !MimeTypeExtractor.isXadesContainer(filePath: fileURL))) {
self.openContainer(url: url, newFilePath: newFilePath, fileName: fileName, landingViewController: landingViewController, navController: navController, isEmptyFileImported: isEmptyFileImported, isSendingToSivaAgreed: isSendingToSivaAgreed) { error in
failure(error)
}
Expand Down
37 changes: 27 additions & 10 deletions MoppApp/MoppApp/ContainerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class ContainerViewController : MoppViewController, ContainerActions, PreviewAct
let landingViewController = LandingViewController.shared!
var isAsicContainer = LandingViewController.shared.containerType == .asic
var isEmptyFileWarningSet = false
var isAsicsFileWarningSet = false
var isCadesWarningSet = false
var isXadesWarningSet = false

var asicsSignatures = [MoppLibSignature]()
var asicsDataFiles = [MoppLibDataFile]()
Expand Down Expand Up @@ -241,8 +241,8 @@ class ContainerViewController : MoppViewController, ContainerActions, PreviewAct
let asicContainer = self.containerViewDelegate?.getContainer()
checkEmptyFilesInContainer(asicContainer: asicContainer)

if isAsicsContainer() && !isAsicsFileWarningSet {
handleAsicsContainerMessage()
if isAsicsContainer() && !isXadesWarningSet {
handleXadesContainerMessage(asicContainer: asicContainer)
}

if isAsicContainer {
Expand Down Expand Up @@ -351,12 +351,16 @@ class ContainerViewController : MoppViewController, ContainerActions, PreviewAct
}
}

private func handleAsicsContainerMessage() {
let asicsFileNotification = NotificationMessage(isSuccess: false, text: L(.containerAsicsWarning))
if !self.notifications.contains(where: { $0 == asicsFileNotification }) {
self.notifications.append(asicsFileNotification)
private func handleXadesContainerMessage(asicContainer: MoppLibContainer?) {
guard let container = asicContainer else { return }
let isXades = SignatureUtil.isXades(signatures: container.signatures)
if isXades {
let xadesNotification = NotificationMessage(isSuccess: false, text: L(.containerAsicsWarning))
if !self.notifications.contains(where: { $0 == xadesNotification }) {
self.notifications.append(xadesNotification)
}
isXadesWarningSet = true
}
isAsicsFileWarningSet = true
}

private func checkIsCades(asicContainer: MoppLibContainer?) {
Expand All @@ -370,6 +374,15 @@ class ContainerViewController : MoppViewController, ContainerActions, PreviewAct
isCadesWarningSet = true
}
}

static func isXades(signatures: [Any]) -> Bool {
return signatures.contains { signature in
if let sig = signature as? MoppLibSignature {
return sig.signatureFormat.lowercased().contains("bes")
}
return false
}
}

func setSections() {
if isSignaturesEmpty && isAsicContainer {
Expand Down Expand Up @@ -775,7 +788,8 @@ extension ContainerViewController : UITableViewDataSource {
let nserror = error as NSError?
if nserror != nil && nserror?.code == Int(MoppLibErrorCode.moppLibErrorNoInternetConnection.rawValue) {
let pathExtension = URL(string: containerFilePath)?.pathExtension
if pathExtension == "asics" || pathExtension == "scs" {
let asicContainer: MoppLibContainer? = self.containerViewDelegate?.getContainer()
if (pathExtension == "asics" || pathExtension == "scs") && !ContainerViewController.isXades(signatures: asicContainer?.signatures ?? []) {
SiVaUtil.displaySendingToSiVaDialog { hasAgreed in
if hasAgreed {
SiVaUtil.setIsSentToSiva(isSent: hasAgreed)
Expand Down Expand Up @@ -1111,7 +1125,10 @@ extension ContainerViewController : UITableViewDelegate {
}
}
else {
if isAsicsContainer() {
let asicContainer: MoppLibContainer? = self.containerViewDelegate?.getContainer()
if isAsicsContainer() && ContainerViewController.isXades(signatures: asicContainer?.signatures ?? []) {
sections = ContainerViewController.sectionsWithTimestamp
} else if isAsicsContainer() {
sections = isSendingToSivaAgreed ? ContainerViewController.sectionsWithTimestamp :
ContainerViewController.sectionsWithTimestampNoSignatures
} else {
Expand Down
26 changes: 26 additions & 0 deletions MoppApp/MoppApp/MimeTypeExtractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ class MimeTypeExtractor {
return false
}

public static func isXadesContainer(filePath: URL) -> Bool {
if isZipFile(filePath: filePath) {
return containerHasSignatureXmlFiles(filePath: filePath)
}

return false
}

public static func getMimeTypeFromContainer(filePath: URL) -> String {

var mimetype: String = ""
Expand Down Expand Up @@ -147,6 +155,24 @@ class MimeTypeExtractor {
return false
}

private static func containerHasSignatureXmlFiles(filePath: URL) -> Bool {
do {
let archive = try Archive(url: filePath, accessMode: .read)

for entry in archive {
let entryUrl = URL(fileURLWithPath: entry.path)
if entryUrl.lastPathComponent.contains("signatures.xml") {
return true
}
}
} catch (let archiveError) {
printLog("Unable to open archive: \(archiveError.localizedDescription)")
return false
}

return false
}

private static func unZipFile(filePath: URL, fileName: String) -> URL? {
let outputPath = MoppFileManager.shared.tempCacheDirectoryPath().appendingPathComponent(filePath.lastPathComponent).deletingPathExtension()

Expand Down
6 changes: 3 additions & 3 deletions MoppApp/MoppApp/PreviewActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension PreviewActions where Self: ContainerViewController {

let destinationPathURL = URL(fileURLWithPath: destinationPath)
SiVaUtil.setIsSentToSiva(isSent: false)
if SiVaUtil.isDocumentSentToSiVa(fileUrl: destinationPathURL) {
if (SiVaUtil.isDocumentSentToSiVa(fileUrl: destinationPathURL) && !MimeTypeExtractor.isXadesContainer(filePath: destinationPathURL)) {
SiVaUtil.displaySendingToSiVaDialog { hasAgreed in
if (destinationPathURL.pathExtension == "ddoc" || destinationPathURL.pathExtension == "pdf") && !hasAgreed {
self.updateState(.opened)
Expand Down Expand Up @@ -101,10 +101,10 @@ extension PreviewActions where Self: ContainerViewController {
return
}

let fileExtension = URL(fileURLWithPath: filePath).pathExtension.lowercased()
let fileExtension = url.pathExtension.lowercased()

SiVaUtil.setIsSentToSiva(isSent: false)
if fileExtension != "pdf" && SiVaUtil.isDocumentSentToSiVa(fileUrl: URL(fileURLWithPath: filePath)) {
if (fileExtension != "pdf" && SiVaUtil.isDocumentSentToSiVa(fileUrl: URL(fileURLWithPath: filePath)) && !MimeTypeExtractor.isXadesContainer(filePath: url)) {
SiVaUtil.displaySendingToSiVaDialog { hasAgreed in
if hasAgreed {
openContentPreviewDocument(filePath)
Expand Down
4 changes: 2 additions & 2 deletions MoppApp/MoppApp/RecentContainersViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ extension RecentContainersViewController : UITableViewDelegate {
if ext.isAsicContainerExtension || ext.isPdfContainerExtension {
let containerPathURL = path
SiVaUtil.setIsSentToSiva(isSent: false)
if SiVaUtil.isDocumentSentToSiVa(fileUrl: containerPathURL) || (containerPathURL.pathExtension == "asics" || containerPathURL.pathExtension == "scs") {
if (SiVaUtil.isDocumentSentToSiVa(fileUrl: containerPathURL) || (containerPathURL.pathExtension == "asics" || containerPathURL.pathExtension == "scs")) && !MimeTypeExtractor.isXadesContainer(filePath: containerPathURL) {
SiVaUtil.displaySendingToSiVaDialog { hasAgreed in
if (containerPathURL.pathExtension == "ddoc" || containerPathURL.pathExtension == "pdf" ||
MimeTypeExtractor.isCadesContainer(filePath: containerPathURL)) && !hasAgreed {
(MimeTypeExtractor.isCadesContainer(filePath: containerPathURL) && !MimeTypeExtractor.isXadesContainer(filePath: containerPathURL))) && !hasAgreed {
return
}
self.openContainer(containerPath: path.path, navController: navController, isSendingToSivaAgreed: hasAgreed)
Expand Down
9 changes: 9 additions & 0 deletions MoppApp/MoppApp/SignatureUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class SignatureUtil {
return false
}

static func isXades(signatures: [Any]) -> Bool {
return signatures.contains { signature in
if let sig = signature as? MoppLibSignature {
return sig.signatureFormat.lowercased().contains("bes")
}
return false
}
}

static func getSignatures(filePath: URL) -> [Any] {
do {
let container = try MoppLibContainerActions.sharedInstance().openContainer(withPath: filePath.path)
Expand Down
3 changes: 2 additions & 1 deletion MoppApp/MoppApp/SivaUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class SiVaUtil {
}

let isCades = MimeTypeExtractor.isCadesContainer(filePath: fileLocation)
let isSentToSiva = containerTypes.contains(containerType) || isCades
let isXades = MimeTypeExtractor.isXadesContainer(filePath: fileLocation)
let isSentToSiva = (containerTypes.contains(containerType) && !isXades) || isCades

return isSentToSiva
}
Expand Down

0 comments on commit f4cefd2

Please sign in to comment.