<%@ LANGUAGE=VBScript %> <% Option Explicit %> <HTML><HEAD> <TITLE>CAPICOM 2 SignedData Demo</TITLE> <% Dim iebrowser :iebrowser = True iebrowser = InStr(1,Request.ServerVariables("HTTP_USER_AGENT"), "msie", 1) If iebrowser then %> <script language="VBScript"> Option Explicit Const CAPICOM_VERIFY_SIGNATURE_AND_CERTIFICATE = 1 Const info = "Signature and Certificate verification " Dim SignedData, Certificate, pkcs7message, contWin Dim verifyStatus : verifyStatus = False Sub btnVerify_onclick() 'always verifies If isSigVerified Then MsgBox info & " succeeded", vbInformation Else MsgBox info & " failed: ", vbCritical End If End Sub Sub btnContent_onclick() If NOT verifyStatus Then 'check if possibly clicked first isSigVerified End If If verifyStatus Then pkcs7message = pkcs7.innerText Set SignedData = CreateObject("CAPICOM.SignedData") SignedData.Verify pkcs7message, False, CAPICOM_VERIFY_SIGNATURE_AND_CERTIFICATE showContent SignedData.Content Set SignedData = nothing Else MsgBox info & " failed: ", vbCritical End If End Sub Sub btnCertificates_onclick() If NOT verifyStatus Then isSigVerified End If If verifyStatus Then pkcs7message = pkcs7.innerText Set SignedData = CreateObject("CAPICOM.SignedData") SignedData.Verify pkcs7message, False, CAPICOM_VERIFY_SIGNATURE_AND_CERTIFICATE For Each Certificate In SignedData.Certificates Certificate.Display Next Set SignedData = nothing Else MsgBox info & " failed: ", vbCritical End If End Sub Function isSigVerified() pkcs7message = pkcs7.innerText Set SignedData = CreateObject("CAPICOM.SignedData") On Error Resume Next SignedData.Verify pkcs7message, False, CAPICOM_VERIFY_SIGNATURE_AND_CERTIFICATE If Err.Number <>0 Then verifyStatus = False Else verifyStatus = True End If isSigVerified = verifyStatus On Error Goto 0 End Function Sub showContent(content) Set contWin = window.open("","ContentWin","width=640,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes") contWin.document.writeln "<html><head>" contWin.document.writeln "<title>Content</title>" contWin.document.writeln "<XMP>" & content & "<" & /XMP>" contWin.document.writeln "</body></html>" End Sub </script> <% End If %> </head> <BODY bgcolor="#CCCCCC"> <h2 align=center><font color=blue>CAPICOM 2 SignedData Demo</font></h2> <div align=right>M. Gallant 06/23/2002</div> <hr size=1> <% Dim Signer, Store, Certificates, Certificate Dim fso, oContentFile Dim SignedData, Message Dim DataToSign : DataToSign = "" Const MAX_DATA = 2700 Const CAPICOM_ENCODE_BASE64 = 0 Const CAPICOM_LOCAL_MACHINE_STORE = 1 Const CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME = 1 Const SubjectName = "Cert Name Substring" Const FileToSign = "C:\Temp\contenttosign.txt" If Request.TotalBytes > MAX_DATA Then EndAsp("Too much data sent (" & Request.TotalBytes & " bytes).<br> Limit is 2.7 kb") End If If Request.Form("datatosign") = "" Then EndAsp("No data submitted.") End If DataToSign = Request.Form("datatosign") Set fso = Server.CreateObject("Scripting.FileSystemObject") ' Write content to sign to archive file. Set oContentFile = fso.OpenTextFile(FileToSign, 2, True) oContentFile.write DataToSign oContentFile.close Set oContentFile = Nothing Set Store = Server.CreateObject("CAPICOM.Store") Set Store = Server.CreateObject("CAPICOM.Store") Store.Open CAPICOM_LOCAL_MACHINE_STORE, "MY" ,0 'Open LocalMachine store for read Set Certificates = Store.Certificates.Find(CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME, SubjectName) 'Response.Write("<br>Certificate found: " & Certificates.Count & Certificates(1).SubjectName) Set Signer = Server.CreateObject("CAPICOM.Signer") Signer.Certificate = Certificates(1) Signer.Options = 0 Set Certificates = Nothing Set Store = Nothing Set SignedData = CreateObject("CAPICOM.SignedData") SignedData.Content = DataToSign On Error Resume Next 'Try to sign data-string with *attached* content and BASE64 endoced pkcs7 Message = SignedData.Sign(Signer, False, CAPICOM_ENCODE_BASE64) If Err.Number <> 0 Then Response.Write("<br>Sign Error: " & Err.Description & " " & Hex(Err.Number)) Else If iebrowser Then Response.Write("<br><button ID='btnVerify'>Verify Signature and Certs</button>") Response.Write("&nbsp;&nbsp;<button ID='btnContent'>Show Content</button>") Response.Write("&nbsp;&nbsp;<button ID='btnCertificates'>Show Certs</button>") Else Response.Write(vbCrLf & "&nbsp;&nbsp;<font color=red size=+1 >Signed Data:</font>") End If Response.Write(vbCrLf & "<br><pre id='pkcs7'>" & Message & "</pre>") End If Set SignedData = Nothing Set Signer = Nothing SUB EndAsp(message) Response.Write(message) Response.Write("</body></html>") Response.End End Sub %> </BODY> </HTML>