Firma un file utilizzando il certificato caricato con dmtLoadCertificate e settato come quello da utilizzare attraverso la funzione dmtSetDefaultCertificate.
Da notare:
il file può essere firmato o firmato e cifrato
nel caso di file firmato e cifrato l'opzione fDetach (separazione del certificato del firmatario dal file firmato) non verrà presa in considerazione
il file verrà firmato utilizzando un certificato presente nella lista (dell'oggetto DiMaTek) dei propri certificati personali
il file verrà cifrato utilizzando uno o più certificati presenti nella lista (dell'oggetto DiMaTek) dei certificati dei riceventi
LONG WINAPI dmtSigningFile (
HDMTOBJ hDmt,
CHAR *szFileName,
CHAR *szAlgHash = szOID_OIWSEC_sha1,
CHAR *szAlgEncrypt = szOID_RSA_RC4,
BOOL fDetachSign = FALSE,
BOOL fEncrypt = FALSE,
BOOL fInclRecipientInMsg = FALSE,
BOOL fUseAllRecipients = FALSE,
CHAR *szPin = "",
BOOL fAddDsiTimeRef = FALSE,
PCHAR szSigningTime = "",
PCHAR szFormat = "");
External_Function dmtSigningFile "dmtSigningFile" Dimatec.dll ;
Handle hDmt ;
String szFileName ;
String szAlgHash ;
String szAlgEncrypt ;
Integer fDetachSign ;
Integer fEncrypt ;
Integer fInclRecipientInMsg ;
Integer fUseAllRecipients ;
String szPin ;
Integer fAddDsiTimeRef ;
String szSigningTime ;
String szFormat
Returns Integer
Public Declare Function dmtSigningFile Lib "Dimatec.dll" _
(ByVal hDmt As Long, _
ByVal szFileName As String, _
Optional ByVal szAlgHash As String = szOID_OIWSEC_sha1, _
Optional ByVal szAlgEncrypt As String = szOID_RSA_RC4, _
Optional ByVal fDetachSign As Boolean = False, _
Optional ByVal fEncrypt As Boolean = False, _
Optional ByVal fInclRecipientInMsg As Boolean = False, _
Optional ByVal fUseAllRecipients As Boolean = False, _
Optional ByVal szPin As String = vbNullString, _
Optional ByVal fAddDsiTimeRef As Boolean = False, _
Optional ByVal szSigningTime As String = vbNullString, _
Optional ByVal szFormat As String = vbNullString, _
As Long
[DllImport("Dimatec.dll")]
public static extern int dmtSigningFile (
int hDmt,
string szFileName,
string szAlgHash /*= szOID_OIWSEC_sha1*/,
string szAlgEncrypt /*= szOID_RSA_RC4*/,
bool fDetachSign /*= false */,
bool fEncrypt /*= false */,
bool fInclRecipientInMsg /*= false */,
bool fUseAllRecipients /*= false */,
string szPin /*= ""*/,
bool fAddDsiTimeRef /*= false */,
string szSigningTime /*= ""*/,
string szFormat /*= ""*/);
hDmt - Handle dell'oggetto DiMaTek
szFileName - File da firmare
szAlgHash - Identificativo dell'algoritmo di hash da utilizzare [vedi Algoritmi di hash e di cifratura]
szAlgEncrypt - Identificativo dell'algoritmo di cifratura da utilizzare [vedi Algoritmi di hash e di cifratura]
fDetachSign - Se TRUE verrà generato un file di firma separata (detached)
fEncrypt - Se TRUE esegue la cifratura del contenuto utilizzando la chiave privata di uno o più certificati presenti nella lista dei certificati dei riceventi
fInclRecipientInMsg - Se TRUE aggiunge i certificati dei riceventi nel messaggio
fUseAllRecipients - Se TRUE utilizza le chiavi pubbliche di tutti i certificati dei riceventi presenti nella lista dei certificati dei riceventi
szPin - Pin della smart-card
fAddDsiTimeRef - Se TRUE aggiunge un riferimento temporale nelle informazioni aggiuntive di firma ricevendolo dal server Dataflex Italia
szSigningTime - Stringa contenente un riferimento temporale che verrà aggiunto nelle informazioni aggiuntive di firma
szFormat - Stringa che specifica la formattazione del parametro szSigningTime [vedi Formato del riferimento temporale]
DMT_OK in caso di successo, altrimenti un codice di errore
Se la chiamata ha successo verrà creato un file firmato con la seguente estensione:
[szFileName] + ".p7m" - File firmato
[szFileName] + ".p7m.p7e" - File firmato e cifrato
Integer ret
Integer iDefaultPersonalCertificateCount
Integer iDefaultPersonalCertificate
Integer iDefaultRecipientCertificateCount
Integer iDefaultRecipientCertificate
String sFileDaFirmare
String sPin
Get psFileDaFirmare of oFirmaUnFile to sFileDaFirmare
// Check
If (Length(Trim(psFileDaFirmare(oFirmaUnFile))) = 0) Begin
Send Stop_Box "Any file selected to sign" "EseguiFirma"
Procedure_Return
End
Move (dmtGetCertificateCountLoaded( ;
ghDmt , ;
False)) to iDefaultPersonalCertificateCount
If (iDefaultPersonalCertificateCount <= 0) Begin
Send Stop_Box "Personal certificate not loaded" "EseguiFirma"
Procedure_Return
End
Else Begin
Move (dmtGetDefaultCertificateLoaded( ;
ghDmt , ;
False)) to iDefaultPersonalCertificate
If (iDefaultPersonalCertificate < 0) Begin
Send Stop_Box "Default personal certificate not set" "EseguiFirma"
Procedure_Return
End
End
If (Checked_State(oCheckBox_SignEncrypt)) Begin
// If I want to encrypt the content of the signed file
// I need to use a recipient certificate
Move (dmtGetCertificateCountLoaded( ;
ghDmt , ;
True)) to iDefaultRecipientCertificateCount
If (iDefaultRecipientCertificateCount <= 0) Begin
Send Stop_Box "Recipient certificate not loaded" "EseguiFirma"
Procedure_Return
End
Else Begin
If (Checked_State(oCheckBox_SignEncryptAllRecipient)) Begin
// If I don't want to use all recipient certificate
// I need to set the default recipient certificate
Move (dmtGetDefaultCertificateLoaded( ;
ghDmt , ;
True)) to iDefaultRecipientCertificate
If (iDefaultRecipientCertificate < 0) Begin
Send Stop_Box "Default recipient certificate not set" "EseguiFirma"
Procedure_Return
End
End
End
End
If (Length(Trim(Value(oForm_Pin))) > 0) Begin
// Exec signature sending the PIN
Get Value of oForm_Pin to sPin
Move (dmtSigningFile( ;
ghDmt , ;
sFileDaFirmare , ;
szOID_OIWSEC_sha1 , ;
szOID_RSA_RC4 , ;
(Checked_State(oCheckBox_Detached)) , ;
(Checked_State(oCheckBox_SignEncrypt)) , ;
False , ;
(Checked_State(oCheckBox_SignEncryptAllRecipient)) , ;
sPin , ;
False)) to ret
End
Else Begin
// Exec signature without sending the PIN - CSP will ask me for the correct smart card PIN
Move (dmtSigningFile( ;
ghDmt , ;
sFileDaFirmare , ;
szOID_OIWSEC_sha1 , ;
szOID_RSA_RC4 , ;
(Checked_State(oCheckBox_Detached)) , ;
(Checked_State(oCheckBox_SignEncrypt)) , ;
False , ;
(Checked_State(oCheckBox_SignEncryptAllRecipient)) , ;
"" , ;
False)) to ret
End
Dim iDefaultPersonalCertificateCount As Long
Dim iDefaultPersonalCertificate As Long
Dim iDefaultRecipientCertificateCount As Long
Dim iDefaultRecipientCertificate As Long
Dim res As Long
' Check
If (Len(Trim(txt_FileToSign.Text)) = 0) Then
MsgBox "Any file selected", vbCritical, "ExecSign"
Exit Sub
End If
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
If (chk_Encrypt.Value = 1) Then
' If I want to encrypt the content of the signed file
' I need to use a recipient certificate
iDefaultRecipientCertificateCount = dmtGetCertificateCountLoaded(g_hDmt, True)
If (iDefaultRecipientCertificateCount <= 0) Then
MsgBox "Recipient certificate not loaded", vbCritical, "ExecSign"
Exit Sub
Else
If (chk_UseAllRecipient.Value = 0) Then
' If I don't want to use all recipient certificate
' I need to set the default recipient certificate
iDefaultRecipientCertificate = dmtGetDefaultCertificateLoaded(g_hDmt, True)
If (iDefaultRecipientCertificate < 0) Then
MsgBox "Default recipent certificate not set", vbCritical, "ExecSign"
Exit Sub
End If
End If
End If
End If
If Len(Trim(txt_PinForSign.Text)) > 0 Then
' Exec signature sending the PIN
res = dmtSigningFile(g_hDmt, _
txt_FileToSign.Text, _
szOID_OIWSEC_sha1, _
szOID_RSA_RC4, _
False, _
chk_Encrypt.Value, _
False, _
chk_UseAllRecipient.Value, _
Trim(txt_PinForSign.Text))
Else
' Exec signature without sending the PIN - CSP will ask me for the correct smart card PIN
res = dmtSigningFile(g_hDmt, _
txt_FileToSign.Text, _
szOID_OIWSEC_sha1, _
szOID_RSA_RC4, _
False, _
chk_Encrypt.Value, _
False, _
chk_UseAllRecipient.Value)
End If
if (LabelFileToSign.Text.Length == 0)
{
MessageBox.Show(this, "Any file selected to sign", "SigningFile",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (CDIMATECLIBRARY.dmtGetCertificateCountLoaded(CDIMATECLIBRARY.ghDimatecObj, false) <= 0)
{
MessageBox.Show(this, "Personal certificate not loaded", "SigningFile",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (CDIMATECLIBRARY.dmtGetDefaultCertificateLoaded(CDIMATECLIBRARY.ghDimatecObj, false) < 0)
{
MessageBox.Show(this, "Default personal certificate not set", "SigningFile",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (CheckBoxSignEncrypt.Checked)
{
// If I want to encrypt the content of the signed file
// I need to use a recipient certificate
if (CDIMATECLIBRARY.dmtGetCertificateCountLoaded(CDIMATECLIBRARY.ghDimatecObj, true) <= 0)
{
MessageBox.Show(this, "Recipient certificate not loaded", "SigningFile",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (CDIMATECLIBRARY.dmtGetDefaultCertificateLoaded(CDIMATECLIBRARY.ghDimatecObj, true) < 0)
{
MessageBox.Show(this, "Default recipient certificate not set", "SigningFile",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
string RefTime = "";
string RefTimeFormat = "";
if (CheckBoxAddSignRefTime.Checked)
{
RefTime = UserRefTimeMaskedTextBox.Text;
RefTimeFormat = "%d/%m/%Y %H.%M";
}
int SigningFileResult = CDIMATECLIBRARY.DMT_OK;
if (SignaturePinTextBox.Text.Length > 0)
{
SigningFileResult = CDIMATECLIBRARY.dmtSigningFile(
CDIMATECLIBRARY.ghDimatecObj,
LabelFileToSign.Text,
CDIMATECLIBRARY.szOID_OIWSEC_sha1,
CDIMATECLIBRARY.szOID_RSA_RC4,
false,
CheckBoxSignEncrypt.Checked,
false,
CheckBoxSignEncryptAllRecipient.Checked,
SignaturePinTextBox.Text,
CheckBoxAddDsiSignRefTime.Checked,
RefTime,
RefTimeFormat);
}
else
{
SigningFileResult = CDIMATECLIBRARY.dmtSigningFile(
CDIMATECLIBRARY.ghDimatecObj,
LabelFileToSign.Text,
CDIMATECLIBRARY.szOID_OIWSEC_sha1,
CDIMATECLIBRARY.szOID_RSA_RC4,
false,
CheckBoxSignEncrypt.Checked,
false,
CheckBoxSignEncryptAllRecipient.Checked,
"",
CheckBoxAddDsiSignRefTime.Checked,
RefTime,
RefTimeFormat);
}