Verifica la firma di un file firmato secondo lo standard CAdES può estrarre il file originale e uno dei certificati con cui e' stato firmato.
int WINAPI
dmtVerifySignedFileWithCadesAttributes(
HDMTOBJ hDmt,
PCHAR szFileSigned,
DWORD dwIndex /*= 0*/,
PCHAR outOriginalFile /*= ""*/,
PCHAR outCertificateFile /*= ""*/,
PCHAR outXMLCertificateFileInfo /*= ""*/,
DIMATEK_SIGNED_INFO* pDimatekSignedInfo /*= NULL*/
External_Function dmtVerifySignedFileWithCadesAttributes "dmtVerifySignedFileWithCadesAttributes " Dimatec.dll ;
integer hdmt;
String szFileNameSigned ;
Integer dwIndex ;
String outOriginalFile ;
String outCertificateFile ;
String outXMLCertificateFileInfo;
address pDimatekSignedInfo ;
Returns Integer
Public Declare Function dmtVerifySignedFileWithCadesAttributes Lib "Dimatec.dll" _
(hdmt as Long, ByVal szFileNameSigned As String, _
Optional ByVal dwIndex As Long = 0, _
Optional ByVal outOriginalFile As String = vbNullString, _
Optional ByVal outCertificateFile As String = vbNullString, _
Optional ByVal outXMLCertificateFileInfo As String = vbNullString , _
optional pDimatekSignedInfo as DIMATEK_SIGNED_INFO ) _
As Long
[DllImport("Dimatec.dll")]
public static extern int dmtVerifySignedFileWithCadesAttributes (
int hdmt,
string szFileNameSigned,
int dwIndex /*= 0*/,
string szoutOriginalFile /*= ""*/,
string szoutCertificateFile /*= ""*/,
string szoutXMLCertificateFileInfo ,
IntPtr pDimatekSignedInfo);
hdmt - Handle della sessione
szFileNameSigned - File firmato da verificare
dwIndex - Indice del certificato da estrarre
outOriginalFile - Se non NULL verrà estratto il file originale
outCertificateFile - Se non NULL verrà estratto il certificato selezionato in un file .cer
outXMLCertificateFileInfo - Se non NULL verranno estratte le informazioni del certificato selezionato in un file Xml
pDimatekSignedInfo Puntatore alla struttura DIMATEK_SIGNED_INFO contenente le informazioni del certificato e della firma . Usa NULL o (IntPrt.Zero in c#) se non ti occorrono le informazioni del certificato
DMT_OK in caso di successo, altrimenti un codice di errore.
C'è una eccezione con il valore E_VALID_DIKE_CADES_SIGNED_FILE che può essere restituito nel caso in cui la verifica è positiva tranne che la sequenza_0_0_0_1 che dovrebbe avere almeno 2 elementi nella firma a Norma Italiana ma in alcuni casi ne ha uno solo, ma la firma per il resto è da considerare valida. Quindi in questo caso la funzione restituisce il codice E_VALID_DIKE_CADES_SIGNED_FILE che indicherà che il formato è corretto, ma che ha questa anomalia.. per il resto la verifica ha avuto successo. Sta al programmatore decidere che fare
Integer ret
String sFileFirmato
String sFileOriginaleDetached
String sPin
String sCertificatoEstratto
String sFileOriginaleEstratto
String sCertificatoEstrattoXml
address pDimatekSignedInfo
Integer iDefaultPersonalCertificateCount
Integer iDefaultPersonalCertificate
Get psFileFirmato of oVerificaFileFirmato to sFileFirmato
Get psFileOriginaleDetached of oVerificaFileFirmato to sFileOriginaleDetached
// Check
If (Length(Trim(psFileFirmato(oVerificaFileFirmato))) = 0) Begin
Send Stop_Box "Any file selected for verification" "EseguiVerificaFirma"
Procedure_Return
End
If (Checked_State(oCheckBox_Detached)) Begin
If (Length(Trim(psFileOriginaleDetached(oVerificaFileFirmato))) = 0) Begin
Send Stop_Box "Any file selected for verification" "EseguiVerificaFirma"
Procedure_Return
End
End
If (Checked_State(oCheckBox_EstraiCertificato)) Begin
Get Label of oTextBox_EstraiCertificato to sCertificatoEstratto
End
Else Begin
Move "" to sCertificatoEstratto
End
If (Checked_State(oCheckBox_EstraiOriginale)) Begin
Get Label of oTextBox_EstraiOriginale to sFileOriginaleEstratto
End
Else Begin
Move "" to sFileOriginaleEstratto
End
If (Checked_State(oCheckBox_EstraiXml)) Begin
Get Label of oTextBox_EstraiXml to sCertificatoEstrattoXml
End
Else Begin
Move "" to sCertificatoEstrattoXml
End
If (Checked_State(oCheckBox_CryptedSignedFile)) Begin
// If the file was encrypted I need the corresponding private
// certificate associated with the public key used during encryption
Move (dmtGetCertificateCountLoaded( ;
ghDmt , ;
False)) to iDefaultPersonalCertificateCount
If (iDefaultPersonalCertificateCount <= 0) Begin
Send Stop_Box "Personal certificate not loaded" "EseguiVerificaFirma"
Procedure_Return
End
Else Begin
Move (dmtGetDefaultCertificateLoaded( ;
ghDmt , ;
False)) to iDefaultPersonalCertificate
If (iDefaultPersonalCertificate < 0) Begin
Send Stop_Box "Default personal certificate not set" "EseguiVerificaFirma"
Procedure_Return
End
End
End
If (Checked_State(oCheckBox_Detached)) Begin
Move (dmtVerifySignedFileWithCadesAttributes( ;
sFileOriginaleDetached , ;
sFileFirmato , ;
0, ;
sCertificatoEstratto , ;
sCertificatoEstrattoXml)) to ret
End
Else Begin
If (Checked_State(oCheckBox_CryptedSignedFile)) Begin
// Exec verification of the signed and ecrypted file
If (Length(Trim(Value(oForm_Pin))) > 0) Begin
Get Value of oForm_Pin to sPin
Move (dmtVerifyCryptedSignedFileWithCadesAttributes( ;
ghDmt , ;
sFileFirmato , ;
0, ;
sFileOriginaleEstratto, ;
sCertificatoEstratto, ;
sPin)) to ret
End
Else Begin
Move (dmtVerifyCryptedSignedFileWithCadesAttributes( ;
ghDmt , ;
sFileFirmato , ;
0, ;
sFileOriginaleEstratto, ;
sCertificatoEstratto , ;
"")) to ret
End
End
Else Begin
// Exec verification of the signed file
Move (dmtVerifySignedFileWithCadesAttributes( ghDmt ;
sFileFirmato , ;
0, ;
sFileOriginaleEstratto, ;
sCertificatoEstratto , ;
sCertificatoEstrattoXml,
pDimatekSignedInfo,0 )) to ret
End
End
Dim sFileOriginaleEstratto As String
Dim sCertificatoEstratto As String
Dim iDefaultPersonalCertificateCount As Long
Dim iDefaultPersonalCertificate As Long
Dim res As Long
' Check
If (Len(Trim(txt_FileToVerifySign.Text)) = 0) Then
MsgBox "Any file selected for verification", vbCritical, "ExecSign"
Exit Sub
End If
If (chk_EstraiCertificato.Value = 1) Then
sCertificatoEstratto = txt_CertificatoEstratto.Text
Else
sCertificatoEstratto = ""
End If
If (chk_EstraiFileOriginale.Value = 1) Then
sFileOriginaleEstratto = txt_FileOriginaleEstratto.Text
Else
sFileOriginaleEstratto = ""
End If
If (chk_CryptedSignedFile.Value = 1) Then
' If the file was encrypted I need the corresponding private
' certificate associated with the public key used during encryption
iDefaultPersonalCertificateCount = dmtGetCertificateCountLoaded(g_hDmt, False)
If (iDefaultPersonalCertificateCount <= 0) Then
MsgBox "Personal certificate not loaded", vbCritical, "ExecSign"
Exit Sub
Else
iDefaultPersonalCertificate = dmtGetDefaultCertificateLoaded(g_hDmt, False)
If (iDefaultPersonalCertificate < 0) Then
MsgBox "Default personal certificate not set", vbCritical, "ExecSign"
Exit Sub
End If
End If
End If
If (chk_CryptedSignedFile.Value = 0) Then
' Exec verification of the signed file
res = dmtVerifySignedFileWithCadesAttributes(txt_FileToVerifySign.Text, _
0, _
sFileOriginaleEstratto, _
sCertificatoEstratto,0)
Else
' Exec verification of the signed and ecrypted file
If Len(Trim(txt_PinForDecryptSigned.Text)) > 0 Then
res = dmtVerifyCryptedSignedFileWithCadesAttributes(g_hDmt, _
txt_FileToVerifySign.Text, _
0, _
sFileOriginaleEstratto, _
sCertificatoEstratto, _
Trim(txt_PinForDecryptSigned.Text))
Else
res = dmtVerifyCryptedSignedFileWithCadesAttributes(g_hDmt, _
txt_FileToVerifySign.Text, _
0, _
sFileOriginaleEstratto, _
sCertificatoEstratto)
End If
End If
// Check
if (labelSignedFileToVer.Text.Length == 0)
{
MessageBox.Show(this, "Any file selected to verify", "VerifySignedFile",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string sCertificatoEstratto = "";
if (checkBoxExtractSignerCert.Checked)
sCertificatoEstratto = labelExtractSignerCert.Text;
string sFileOriginaleEstratto = "";
if (checkBoxExtractOriginal.Checked)
sFileOriginaleEstratto = labelExtractOriginal.Text;
if (checkBoxEncryptedSigned.Checked)
{
// If the file was encrypted I need the corresponding private
// certificate associated with the public key used during encryption
if (CDIMATECLIBRARY.dmtGetCertificateCountLoaded(CDIMATECLIBRARY.ghDimatecObj, false) <= 0)
{
MessageBox.Show(this, "Personal certificate not loaded", "VerifySignedFile",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (CDIMATECLIBRARY.dmtGetDefaultCertificateLoaded(CDIMATECLIBRARY.ghDimatecObj, false) < 0)
{
MessageBox.Show(this, "Default personal certificate not set", "VerifySignedFile",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
int VerifySignedFileResult = CDIMATECLIBRARY.DMT_OK;
if (checkBoxEncryptedSigned.Checked)
{
DIMATEK_CERTIFICATE_INFO dimatekCertInfo = new DIMATEK_CERTIFICATE_INFO();
int dimatekCertInfoStructSize = Marshal.SizeOf(dimatekCertInfo);
IntPtr dimatekCertInfoIntPtr = Marshal.AllocHGlobal(dimatekCertInfoStructSize);
// Exec verification of the signed and ecrypted file
if (textBoxVerifySignedEncryptedPin.Text.Length > 0)
{
VerifySignedFileResult = CDIMATECLIBRARY.dmtVerifyCryptedSignedFileWithCadesAttributes(
CDIMATECLIBRARY.ghDimatecObj,
labelSignedFileToVer.Text,
0,
sFileOriginaleEstratto,
sCertificatoEstratto,
textBoxVerifySignedEncryptedPin.Text);
}
else
{
VerifySignedFileResult = CDIMATECLIBRARY.dmtVerifyCryptedSignedFileWithCadesAttributes(
CDIMATECLIBRARY.ghDimatecObj,
labelSignedFileToVer.Text,
0,
sFileOriginaleEstratto,
sCertificatoEstratto,
"");
}
}
else
{
// Exec verification of the signed file
VerifySignedFileResult = CDIMATECLIBRARY.dmtVerifySignedFileWithCadesAttributes(
labelSignedFileToVer.Text,
0,
sFileOriginaleEstratto,
sCertificatoEstratto,
"",dimatekCertInfoIntPtr);
// get all information about certificate
dimatekCertInfo = (DIMATEK_CERTIFICATE_INFO)Marshal.PtrToStructure(
dimatekCertInfoIntPtr, typeof(DIMATEK_CERTIFICATE_INFO));
}