EldoS | Feel safer!

Software components for data protection, secure storage and transfer

I am pretty ungry

Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.
#469
Posted: 06/16/2006 15:36:39
by Rafael Hernández (Basic support level)
Joined: 06/16/2006
Posts: 26

I think I have tried using all possible methods. The issue is that when I use

TElX509Certificate.DetectCertFileFormat(Nothing, TempFile)

I get "0". That is, as if the file has nothing inside. But it has. Do you know any other way to introduce the exported certificate into a file?
#470
Posted: 06/16/2006 15:37:58
by Rafael Hernández (Basic support level)
Joined: 06/16/2006
Posts: 26

May be the issue is that your code does not understand the chunk of code exported with CAPICOM into the data field.
#471
Posted: 06/16/2006 16:06:37
by Ken Ivanov (EldoS Corp.)

The '0' value stands for cfUnknown certificate format. That is, TElX509Certificate does not understand the format your certificate is saved in.

If your certificates does not contain a private key (and other sensitive information), you can send it to support[at]eldos.com so that we could check its format manually.

Please also note, that System.IO.TextWriter class may corrupt certificates stored in binary formats, such as DER, SPC or PFX.
#472
Posted: 06/18/2006 17:24:08
by Rafael Hernández (Basic support level)
Joined: 06/16/2006
Posts: 26

Ok,

I have changed code and now I use the following

Dim Cert As TElX509Certificate
Cert = New TElX509Certificate

Dim CertStorage As TElMemoryCertStorage
CertStorage = New TElMemoryCertStorage

Dim TempFile As String = ""
TempFile = Path.GetTempFileName

Dim sr As New StringReader(Dtb2.Rows.Item(0).Item("firma"))
Dim filerec As New FileStream(TempFile, FileMode.OpenOrCreate)
Dim bw As New BinaryWriter(filerec)
bw.BaseStream.Seek(0, SeekOrigin.Begin)
Do While sr.Peek <> -1
bw.BaseStream.WriteByte(sr.Read)
Loop
bw.Flush()
bw.Close()
sr.Close()
filerec.Close()

Dim Firma As FileStream
Firma = New FileStream(TempFile, FileMode.Open, FileAccess.Read, FileShare.Read)

Try
Cert.LoadFromStream(Firma, 0)
CertStorage.Clear()
CertStorage.Add(Cert, False)
Catch ex1 As Exception
Try
Cert.LoadFromStreamPEM(Firma, "", 0)
CertStorage.Clear()
CertStorage.Add(Cert, False)
Catch ex2 As Exception
Try
Cert.LoadFromStreamPFX(Firma, "", 0)
CertStorage.Clear()
CertStorage.Add(Cert, False)
Catch ex3 As Exception
Try
Cert.LoadFromStreamSPC(Firma, 0)
CertStorage.Clear()
CertStorage.Add(Cert, False)
Catch ex4 As Exception
Try
Cert.LoadKeyFromStream(Firma, 0)
CertStorage.Clear()
CertStorage.Add(Cert, False)
Catch ex5 As Exception
Try
Cert.LoadKeyFromStreamMS(Firma, 0)
CertStorage.Clear()
CertStorage.Add(Cert, False)
Catch ex6 As Exception
Try
Cert.LoadKeyFromStreamNET(Firma, "", 0)
CertStorage.Clear()
CertStorage.Add(Cert, False)
Catch ex7 As Exception
Try
Cert.LoadKeyFromStreamPEM(Firma, "", 0)
CertStorage.Clear()
CertStorage.Add(Cert, False)
Catch ex8 As Exception
Try
Hola = Cert.DetectCertFileFormat(Nothing, Firma)
CertStorage.Clear()
CertStorage.Add(Cert, False)
Catch ex9 As Exception
Errorvar = "everything has failed trying to add the certificate"
End Try
End Try
End Try

End Try

End Try

End Try

End Try

End Try
End Try


getting the same "No signing certificate found" and "Invalid certificate data" for Cert.DetectCertFileFormat(Nothing, Firma).

I export the certificate with the certificate.export(CAPICOM_ENCODE_BINARY) of CAPICOM and i get the chunk of exported certificate written down to the TempFile with no problems. I have checked that and it seems to work.

As the certificate is a test certificate I have no problem sending the exported code to you and I attach it here in the prueba.txt file.

Please help me solve this problem for it is getting me crazy.


[ Download ]
#473
Posted: 06/19/2006 00:37:23
by Ken Ivanov (EldoS Corp.)

Your certificate is stored in format that is not understood by SecureBlackbox (actually, it is stored in PEM format, but no PEM headers are present). A small conversion is needed to make SecureBlackbox understand it. Please use the following code to perform the conversion:
Code
Dim F As System.IO.FileStream
Dim Encoded() As Byte
Dim Decoded() As Byte
Dim DecodedSize As Integer
Dim R As Integer
Dim Cert As New TElX509Certificate

F = New FileStream(<path to your certificate>, FileMode.Open)
Try
    ReDim Encoded(F.Length - 1)
    F.Read(Encoded, 0, Encoded.Length)
Finally
    F.Close()
End Try
DecodedSize = Encoded.Length
ReDim Decoded(DecodedSize)
R = SBUtils.Unit.Base64Decode(Encoded, Decoded, DecodedSize)
If R <> SBUtils.Unit.BASE64_DECODE_OK Then
    Throw New Exception("Failed to load certificate")
End If
Cert.LoadFromBuffer(Decoded, 0, DecodedSize)
#476
Posted: 06/19/2006 10:29:08
by Rafael Hernández (Basic support level)
Joined: 06/16/2006
Posts: 26

Ok, Now I get one certificate in CertStorage (CertStorage.Count = 1) but when I try to close the Document (Document.Close(Success) with the new signature, I get the "No signing certificate found" message.

¿What is the problem now? The code is

TempFile = Path.GetTempFileName

Dim sr As New StringReader(Dtb2.Rows.Item(0).Item("firma"))

Filerec = New FileStream(TempFile, FileMode.OpenOrCreate)
Dim bw As New BinaryWriter(Filerec)
bw.BaseStream.Seek(0, SeekOrigin.Begin)
Try
Do While sr.Peek <> -1
bw.BaseStream.WriteByte(sr.Read)
Loop
Catch ex As Exception
bw.Flush()
bw.Close()
Filerec.Close()
Throw New Exception("Error. Hola: " & Hola & " . Mensaje: " & ex.Message)
End Try
bw.Flush()
bw.Close()
sr.Close()
Filerec.Close()

TempPath = Session("rutadocemail") & "prueba.pdf"
System.IO.File.Copy(nombrepdf, TempPath, True)
' opening the temporary file
Success = False
F = New FileStream(TempPath, FileMode.Open, FileAccess.ReadWrite)
Try

Document.Open(F)
Try
' checking if the document is already encrypted
If (Document.Encrypted) Then
Throw New Exception("El documento esta encriptado y no puede ser firmado y enviado a proveedor!! Avise al administrador ")
End If

' adding the signature and setting up property values
index = Document.AddSignature()
Sig = Document.Signatures(index)
Sig.Handler = PublicKeyHandler
Sig.AuthorName = "Jefe del Area"
Sig.SigningTime = DateTime.Now
Sig.Reason = "Firmado de Pedido"
' configuring signature type
Sig.Invisible = False
' Si se desea que la firma sea CERTIFICATION (MDP) SIGNATURE -en caso contrario quitar la siguiente línea-
'Sig.SignatureType = SBPDF.Unit.stMDP
' loading certificate
Dim Encoded() As Byte
Dim Decoded() As Byte
Dim DecodedSize As Integer
Dim R As Integer

Firma = New FileStream(TempFile, FileMode.Open)
Try
ReDim Encoded(Firma.Length - 1)
Firma.Read(Encoded, 0, Encoded.Length)
Finally
Firma.Close()
End Try
DecodedSize = Encoded.Length
ReDim Decoded(DecodedSize)
R = SBUtils.Unit.Base64Decode(Encoded, Decoded, DecodedSize)
If R <> SBUtils.Unit.BASE64_DECODE_OK Then
Throw New Exception("Failed to load certificate. New code: " & R & " // SBUtils.Unit.BASE64_DECODE_OK: " & SBUtils.Unit.BASE64_DECODE_OK)
End If
Cert.LoadFromBuffer(Decoded, 0, DecodedSize)
' adding certificate to certificate storage
CertStorage.Clear()
CertStorage.Add(Cert, True)

PublicKeyHandler.CertStorage = CertStorage
PublicKeyHandler.SignatureType = TSBPDFPublicKeySignatureType.pstPKCS7SHA1
PublicKeyHandler.CustomName = "Adobe.PPKMS"
Success = True
Finally
' closing the document
Document.Close(Success)
End Try
Finally
F.Close()
End Try
#477
Posted: 06/19/2006 10:40:04
by Ken Ivanov (EldoS Corp.)

Most likely, the private key associated with the certificate was not loaded into the certificate object. Please check that the private key is loaded (and no error is returned by LoadKeyFromBuffer() method).
#478
Posted: 06/19/2006 11:03:24
by Rafael Hernández (Basic support level)
Joined: 06/16/2006
Posts: 26

The "Cert.LoadKeyFromBuffer(Decoded, 0, DecodedSize)" method does seem working with no problems.
#479
Posted: 06/19/2006 11:04:14
by Rafael Hernández (Basic support level)
Joined: 06/16/2006
Posts: 26

Is there anything I can do to check that the key is there in the CertStorage ?
#482
Posted: 06/19/2006 11:54:15
by Ken Ivanov (EldoS Corp.)

Yes -- please consider using TElX509Certificate.PrivateKeyExists property.
Also by EldoS: BizCrypto
Components for BizTalk® and SQL Server® Integration Services that let you securely store and transfer information in your business automation solutions.

Reply

Statistics

Topic viewed 36678 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!