<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TestXSignFF.aspx.vb" Inherits="TestWebApp.TestXSignFF" ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Lizard Labs</title>
</head>
<body>
    <form id="form1" runat="server">
    <script language=javascript>

    function installXSignXpi() { xpi={'application/xsignlite':'./LizardLabsXSignLiteFireFox.xpi'}; InstallTrigger.install(xpi); } 
    
    function sign()
    {
        try
        {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            alert("1");
            
			const cid = "@lizardl.com/xsignlite;1";
			var oXSignXmlSignatureComponent = Components.classes[cid].createInstance();
			
			if ( oXSignXmlSignatureComponent == null)
			{
			    alert("Error loading XSign Lite for Firefox");
			    return false;
			}
			
			oXSignXmlSignatureComponent = oXSignXmlSignatureComponent.QueryInterface(Components.interfaces.ILizardLabsXSignLiteFireFox);

            // oXSignXmlSignatureComponent.Test("Test"); // For DEBUG test if control is loaded
            
            alert(document.getElementById("hiddenXml").value);

            oXSignXmlSignatureComponent.SelectCertificate();
            //oSignComponent.SelectCertificateByThumbprint("ae 3c 32 52 79 a8 eb 1d fd 8c ae 90 80 0f e2 57 7a d4 f2 6f");

            if( ! oXSignXmlSignatureComponent.IsCertificateSelected)
            {
                alert("Please select valid certificate.");
                return false;
            }
            else
            {
                if (oXSignXmlSignatureComponent.Sign(document.getElementById("hiddenXml").value))
                {
                    alert(oXSignXmlSignatureComponent.SignedXml);
                    // alert(oXSignXmlSignatureComponent.Signature);
                    // alert(oXSignXmlSignatureComponent.SignatureWithoutKeyInfo);
                }
                else alert("Error");
           }

            document.getElementById("hiddenSignedXml").value = oXSignXmlSignatureComponent.SignedXml;
        }
        catch(e)
        {
            alert("JavaScript Error: " + e.message);
            return false;
        }
        return true;
    }
    </script>
    
    <div>
        <h1>
            Lizard Labs: Test XSign for Firefox (XML signature component)</h1>
        <input id="hiddenXml" type="hidden" runat=server />
        <input id="hiddenSignedXml" type="hidden"  runat=server/>
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~\Default.aspx">Back</asp:HyperLink>
    <p>
    <ul>
       <li>This sample works only in Firefox</li>
       <li>To enable use of unsigned XPCOM Enable <strong>codebase_principal_support</strong> in browser preference In Firefox location bar, type <strong>about:config</strong>. When the new window comes up, type "signed.applets.codebase_principal_support" in the filter bar. The property named: "signed.applets.codebase_principal_support" will be displayed. Select the property and then "right click" on it, select toggle to convert (the boolean) value to <strong>"true"</strong>. This option adds support for interactive security configuration for signed applets and JavaScript in your browser.</li>
       <li>To enable instalation and usage of signed XPCOM you have to install Lizard Labs 
           CA Certificate in Firefx Certificate store (trusted certificates)</li>
       <li>
            To install the XSign Lite plugin for the FireFox browser open this URL in the FireFox browser and :<br />
                <a href="javascript:installXSignXpi();"> Click here</a> or
                <a href="./LizardLabsXSignLiteFireFox.xpi"> download the .xpi</a> (right click and Save as). <br /> After downloading .xpi, open it with the FireFox browser.<br />
       </li>
       <li>To test the XSign Lite for Firefox click on the "Test" button demonstrating how to digitally sign XML documents using JavaScript in Firefox. Examine the source code of this page to look for a sample code. </li>
   </ul>

<asp:Button ID="Button1" runat="server" Text="Test" OnClientClick="return sign();" />

    </p>
    
        <p>
            <strong>XML:</strong></p>
            <asp:Label ID="lblXml" runat="server"></asp:Label>
            <p>
                <strong>Signed XML:</strong>
            </p>
            <asp:Label ID="lblSignedXml" runat="server"></asp:Label>
            <p><strong>Validation results:</strong></p>
            <asp:Label ID="lblsignatureValidation" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>
