Salve a tutti.
Sto provando ad apporre la firma digitale a documenti utilizzando CAPICOM.
Utlizzo una procedura che porta correttamente a termine il processo, creando il file .p7m.
Il codice è il seguente:
Sub Signfile(ByVal InputFileName As String, ByVal OutputFileName As String)
On Error GoTo ErrorHandler
Dim c As String
Dim s As String
Dim MyStore As New Store
Dim Signobj As New SignedData
Dim Signer As New Signer
Dim SigningTime As New CAPICOM.Attribute
MyStore.Open CAPICOM_SMART_CARD_USER_STORE
Signer.Certificate = MyStore.Certificates.Item(1)
Open InputFileName For Input As #1
Input #1, c
Close #1
Signobj.Content = c
SigningTime.Name = CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME
SigningTime.Value = Now
Signer.AuthenticatedAttributes.Add SigningTime
s = Signobj.Sign(Signer, False)
Open OutputFileName For Output As #2
Write #2, s
Close #2
MsgBox ("Firma effettuata – File salvato in "& OutputFileName)
Set Signobj = Nothing
Set MyStore = Nothing
Set Signer = Nothing
Set SigningTime = Nothing
Exit Sub
ErrorHandler:
If Err.Number > 0 Then
MsgBox "Trovato un errore di Visual Basic:” & Err.Description"
Else
MsgBox "Trovato un errore di CAPICOM:” & Err.Number"
End If
End Sub
Tuttavia la critografia impiegata è di tipo SHA1. Purtroppo dal 1.7.2011 le firme digitali create con questo standard non hanno più valore legale.
Mi occorre, dunque, che la firma sia generata impiegando l'algoritmo SHA256.
Su Capicom non sono riuscito a trovare documentazione e, pertanto, sto provando a tentoni, anche facendo riferimento ad esperienze ed esperimenti altrui postati su web.
analizzando attributi e proprietà di capicom ho vista che è possibile fare riferimento allo standard che mi occorre utilizzando il parametro CAPICOM_HASH_ALGORITHM_SHA_256.
Ho provato, pertanto, ad utilizzarlo dichiarando Dim Signhash As New CAPICOM.HashedData
e poi utilizzando Signhash.Algorithm = CAPICOM_HASH_ALGORITHM_SHA_256.
Il codice, quindi è diventato il seguente:
Sub Signfile(ByVal InputFileName As String, ByVal OutputFileName As String)
On Error GoTo ErrorHandler
Dim c As String
Dim s As String
Dim MyStore As New Store
Dim Signobj As New SignedData
Dim Signer As New Signer
Dim SigningTime As New CAPICOM.Attribute
Dim Signhash As New CAPICOM.HashedData
MyStore.Open CAPICOM_SMART_CARD_USER_STORE
Signer.Certificate = MyStore.Certificates.Item(1)
Open InputFileName For Input As #1
Input #1, c
Close #1
Signobj.Content = c
Signhash.Algorithm = CAPICOM_HASH_ALGORITHM_SHA_256
SigningTime.Name = CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME
SigningTime.Value = Now
Signer.AuthenticatedAttributes.Add SigningTime
s = Signobj.Sign(Signer, False)
Open OutputFileName For Output As #2
Write #2, s
Close #2
MsgBox ("Firma effettuata – File salvato in "& OutputFileName)
Set Signobj = Nothing
Set MyStore = Nothing
Set Signer = Nothing
Set SigningTime = Nothing
Exit Sub
ErrorHandler:
If Err.Number > 0 Then
MsgBox "Trovato un errore di Visual Basic:” & Err.Description"
Else
MsgBox "Trovato un errore di CAPICOM:” & Err.Number"
End If
End Sub
Il risultato è sempre lo stesso: la firma generata è fuori corso legale.
Dove sbaglio?
Altra domanda: il certificato di firma è collocato su smart card; l'esecuzione del codice apre in automatico la finestra di richiesta del PIN.
Una volta eseguita l'apposizione di firma, il pin resta memorizzato. Come azzero il valore? devo accedere alla clip board?