Archived Forum Post

Index of archived forum posts

Question:

VBScript - Sign a File to Create a .p7m File

Sep 11 '12 at 22:55

Code:

set crypt = CreateObject("Chilkat.Crypt2")

success = crypt.UnlockComponent("30-day trial")
If (success <> 1) Then
    MsgBox "Crypt component unlock failed"
    WScript.Quit
End If

set cert = CreateObject("Chilkat.Cert")
success = cert.LoadFromFile("keystore.ks")
If (success <> 1) Then
    MsgBox cert.LastErrorText
    WScript.Quit
End If

crypt.SetSigningCert cert

success = crypt.CreateP7M("test.txt","test.p7m")
If (success = 0) Then
    MsgBox crypt.LastErrorText
    WScript.Quit
End If

My keystore is RSA 1024 bit, this is my error:



ChilkatLog:

CreateP7M:

DllDate: Aug  5 2012

UnlockPrefix: 30-day trial

Username: ITACA:ULISSE

Architecture: Little Endian; 32-bit

Language: ActiveX

VerboseLogging: 0

hcCurDate: Tue, 11 Sep 2012 19:07:56 +0200

hcExpire: 11/2012

inFilename: test.txt

outFilename: test.p7m

SigningCert:

  SubjectDN:

  SerialNumber:

--SigningCert

No private key seems to be available for signing (6)

Continuing anyway in case it's a hardware CSP...

Will use CryptoAPI for signing.

msCryptoSignature:

  bDetached: 0

  Microsoft CertCreateCertificateContext function failed.

  WindowsError: Valore tag ASN1 non valido.

  WindowsErrorCode: 0x8009310b

  Failed to get MsCertificate.

--msCryptoSignature

--CreateP7M

--ChilkatLog


Pls help me to understand what is wrong


Answer

My guess is that the keystore.ks does not actually contain a certificate. Examine the cert.LastErrorText after calling cert.LoadFromFile, even though the LoadFromFile method indicates success.


Answer

Thanks, this is the messagge:



ChilkatLog:

LoadFromFile:

DllDate: Aug  5 2012

UnlockPrefix: NONE

Username: ITACA:ULISSE

Architecture: Little Endian; 32-bit

Language: ActiveX

VerboseLogging: 0

filename: keystore.ks

fileSize: 1892

Success.

--LoadFromFile

--ChilkatLog


OK


Answer

Please send the keystore file to support@chilkatsoft.com (in a zipped attachment). Given that it's named "keystore", I suspect its not a format Chilkat should be able to read. It's probable that the LoadFromFile method should've returned an error.


Answer

The keystore.ks seems to be a signed keypair, not a certificate. Try to get the cert + private key in the form of a PFX file (also known as a .p12 file). You can then use the PFX file directly.


Answer

For example:

Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)

set crypt = CreateObject("Chilkat.Crypt2")

' Any string argument automatically begins the 30-day trial. success = crypt.UnlockComponent("30-day trial") If (success <> 1) Then outFile.WriteLine(crypt.LastErrorText) WScript.Quit End If

set certStore = CreateObject("Chilkat.CertStore")

' Load a PFX file into a certificate store object. success = certStore.LoadPfxFile("myPfx.pfx","pfxPassword") If (success <> 1) Then outFile.WriteLine(certStore.LastErrorText) WScript.Quit End If

' Get the certificate by subject common name. ' This should be the cert within the PFX that also ' has a private key (also stored within the PFX). ' cert is a Chilkat.Cert Set cert = certStore.FindCertBySubjectCN("myCert") If (cert Is Nothing ) Then outFile.WriteLine(certStore.LastErrorText) WScript.Quit End If

' Tell the crypt object to use the certificate for signing: crypt.SetSigningCert cert

' Sign a file, producing a .p7m as output. ' The input file is unchanged, the test.p7m contains the ' contents of the input file and the signature. inFile = "test.txt" outFile = "testp7m" success = crypt.CreateP7M(inFile,outFile) If (success <> 1) Then outFile.WriteLine(crypt.LastErrorText) WScript.Quit End If

MsgBox "Success!" outFile.Close