' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero 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 Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Imports System.Security.Cryptography.X509Certificates
Imports System.Text
Imports System.Security.Cryptography.Pkcs
Imports System.Xml
Imports System
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Public Class Licence_Controle

    Public Shared Function VerifyXmlFile(Name As [String], Key As RSA) As [Boolean]
        ' Create a new XML document.
        Dim xmlDocument As New XmlDocument()

        ' Load the passed XML file into the document. 
        xmlDocument.Load(Name)

        ' Create a new SignedXml object and pass it
        ' the XML document class.
        Dim signedXml As New SignedXml(xmlDocument)

        ' Find the "Signature" node and create a new
        ' XmlNodeList object.
        Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature")

        ' Load the signature node.
        signedXml.LoadXml(CType(nodeList(0), XmlElement))

        ' Check the signature and return the result.
        Return signedXml.CheckSignature(Key)
    End Function

    Private Function get_certif(ByVal NumPharma) As X509Certificate2

        Dim store As New X509Store(StoreLocation.CurrentUser)
        store.Open(OpenFlags.ReadOnly)
        Dim certCollection As X509Certificate2Collection = store.Certificates
        Dim certificate As X509Certificate2 = Nothing
        Dim c As X509Certificate2
        For Each c In certCollection
            If c.GetNameInfo(X509NameType.SimpleName, False) = "License Officine " & NumPharma.ToString Then
                certificate = c
                Exit For
            End If
        Next c
        store.Close()
     
        Return certificate
    End Function

    Public Function Verify_licence(ByVal NumPharma As String) As Boolean

        Dim fichier = Application.StartupPath & "\Licences\" & NumPharma & ".signed.xml"
        If My.Computer.FileSystem.FileExists(fichier) = False Then
            MyMsg_Box.Presentation("Officine !", "Le fichier licence produit n'a pas été trouvé.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Return False
        End If

        Dim certificat = get_certif(NumPharma)
        If certificat Is Nothing Then
            MyMsg_Box.Presentation("Officine !", "Le certificat associé à la licence produit n'a pas été trouvé.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Return False
        End If

        If VerifyXmlFile(Application.StartupPath & "\Licences\" & NumPharma & ".signed.xml", get_certif(NumPharma).PublicKey.Key) = False Then
            MyMsg_Box.Presentation("Officine !", "La licence produit n'a pas été validée.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Return False
        End If
        Dim LicenceXml As New XmlDocument
        Dim LicenceXmlelementliste As XmlNodeList
        LicenceXml.Load(Application.StartupPath & "\Licences\" & NumPharma & ".signed.xml")
        LicenceXmlelementliste = LicenceXml.GetElementsByTagName("Licence")
        If LicenceXmlelementliste.Item(0).Item("NumPharma").InnerText <> NumPharma Then
            MyMsg_Box.Presentation("Officine !", "La licence produit n'est pas associée à cette CPS.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Return False
        End If

        Return True
    End Function
End Class