<?xml version="1.0"?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <HTML>
     <HEAD>
       <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
       <META http-equiv="Expires" content="0"/>
       <STYLE TYPE="text/css">
           <!-- Create some CSS patterns that will match the
                HTML elements created below.  This illustrates
               that CSS can be used with XSL. -->
          BODY { font-size:10pt; }
          .cell { font-size:10pt; }
          .listingCaptions { font-size:18pt;}
          .tableHead { font:bold;}
        </STYLE>
     </HEAD>
      <BODY>
        <DIV>
	  <h1>X.509 Certificate</h1>
          <table border="1">
          <xsl:apply-templates select="root..Certificate/Certificate.tbsCertificate"/>
          <xsl:apply-templates select="root..Certificate/Certificate.signature"/>
          </table>
        </DIV>
      </BODY>
    </HTML>
  </xsl:template>
    
  <xsl:template match="TBSCertificate.version">
    <tr><td>Version:</td><td><xsl:apply-templates/></td></tr>
  </xsl:template>

  <xsl:template match="TBSCertificate.serialNumber">
    <tr><td>SerialNumber:</td><td><xsl:apply-templates/></td></tr>
  </xsl:template>

  <xsl:template match="TBSCertificate.signature">
    <tr><td>Signature Algorithm</td><td><xsl:apply-templates/></td></tr>
  </xsl:template>
 
  <xsl:template match="TBSCertificate.issuer">
    <tr><td>Issuer</td><td><xsl:apply-templates/></td></tr>
  </xsl:template>
 
  <xsl:template match="TBSCertificate.subject">
    <tr><td>Subject Name:</td><td><xsl:apply-templates/></td></tr>
  </xsl:template>
 
  <xsl:template match="TBSCertificate.subjectPublicKeyInfo">
    <tr>
      <td>Public Key:</td>
      <td><xsl:apply-templates select="SubjectPublicKeyInfo.algorithm"/>,
          "<xsl:value-of select="substring(.//SubjectPublicKeyInfo.subjectPublicKey,1,25)"/>...",
          <xsl:value-of select="SubjectPublicKeyInfo.subjectPublicKey/@length"/>
          <!--<xsl:value-of select="string-length(.//SubjectPublicKeyInfo.subjectPublicKey)"/>-->
          bits
      </td>
    </tr>
  </xsl:template>
 
  <xsl:template match="TBSCertificate.validity">
    <tr><td>Validity (UTCTime)</td><td>
       <xsl:value-of select="Validity.notBefore"/> -
       <xsl:value-of select="Validity.notAfter"/>
    </td></tr>
  </xsl:template>
 
  <xsl:template match="TBSCertificate.signature">
    <tr><td>Signature Algorithm</td><td><xsl:apply-templates/></td></tr>
  </xsl:template>
 
  <xsl:template match="Certificate.signature">
    <tr>
      <td>Signature</td>
      <td>
        "<xsl:value-of select="substring(.,1,25)"/>...",
        <xsl:value-of select="@length"/>
        <!--<xsl:value-of select="string-length(.)"/> -->
        bits
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="Name.rdnSequence">
    <!-- Construct a Distinguatished Name from a Sequence of RDNs-->
    <xsl:for-each select="RDNSequence._">
      <xsl:apply-templates select="RelativeDistinguishedName._/AttributeTypeAndValue.type"/>
      =
      <xsl:value-of select="RelativeDistinguishedName._/AttributeTypeAndValue.value"/>
      ,
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="TBSCertificate.extensions">
    <tr>
      <td>Extensions:</td>
      <td>
        <xsl:for-each select="Extensions._">
          <table>
           <tr>
             <td>extnId:</td>
             <td><xsl:apply-templates select="Extension.extnId"/></td>
           </tr>
           <tr>
             <td>criticality:</td>
             <td><xsl:apply-templates select="Extension.critical"/></td>
           </tr>
           <tr>
             <td>value: </td>
             <td><xsl:apply-templates select="Extension.extnValue"/></td>
           </tr>
          </table>
          <br></br> 
        </xsl:for-each>
      </td>
     </tr> 

  </xsl:template>

  <xsl:template match='*[@type="OBJECT IDENTIFIER"]'>
    <!-- Conversion Table from OID to String -->
    <xsl:variable name="this" select="."/>
    <xsl:choose>
      <xsl:when test='$this="1.2.840.113549.1.1.2"'>md2WithRSAEncryption</xsl:when>
      <xsl:when test='$this="1.2.840.113549.1.1.4"'>md5WithRSAEncryption</xsl:when>
      <xsl:when test='$this="1.2.840.113549.1.1.5"'>sha1WithRSAEncryption</xsl:when>
      <xsl:when test='$this="1.2.840.10040.4.3"'>dsa-with-sha1</xsl:when>
      <xsl:when test='$this="1.2.840.113549.1.1.1"'>rsaEncryption</xsl:when>
      <xsl:when test='$this="1.2.840.10040.4.1"'>DSA</xsl:when>
      <xsl:when test='$this="2.5.4.3"'>CN</xsl:when>
      <xsl:when test='$this="2.5.4.6"'>C</xsl:when>
      <xsl:when test='$this="2.5.4.8"'>ST</xsl:when>
      <xsl:when test='$this="2.5.4.10"'>O</xsl:when>
      <xsl:when test='$this="2.5.4.11"'>OU</xsl:when>
      <!-- Standard Extension OID -->
      <xsl:when test='$this="2.5.29.9"'>subjectDirectoryAttributes</xsl:when>
      <xsl:when test='$this="2.5.29.14"'>subjectKeyIdentifier</xsl:when>
      <xsl:when test='$this="2.5.29.15"'>keyUsage</xsl:when>
      <xsl:when test='$this="2.5.29.16"'>privateKeyUsagePeriod</xsl:when>
      <xsl:when test='$this="2.5.29.17"'>subjectAltName</xsl:when>
      <xsl:when test='$this="2.5.29.18"'>issuerAltName</xsl:when>
      <xsl:when test='$this="2.5.29.19"'>basicConstraints</xsl:when>
      <!-- 2.5.29.20-2.5.29.29 CRL extensions -->
      <xsl:when test='$this="2.5.29.30"'>nameConstraints</xsl:when>
      <xsl:when test='$this="2.5.29.31"'>cRLDistributionPoints</xsl:when>
      <xsl:when test='$this="2.5.29.32"'>certificatePolicies</xsl:when>
      <xsl:when test='$this="2.5.29.33"'>policyMappings</xsl:when>
      <xsl:when test='$this="2.5.29.35"'>authorityKeyIdentifier</xsl:when>
      <xsl:when test='$this="2.5.29.36"'>policyConstraints</xsl:when>
      <xsl:when test='$this="2.5.29.37"'>extKeyUsage</xsl:when>
      <xsl:otherwise>OID<xsl:apply-templates/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- extension fields -->

  <xsl:template match="root..BasicConstraints">
          <table>
           <tr>
             <td>CA flag: </td> 
             <td><xsl:apply-templates select="BasicConstraints.cA"/></td>
           </tr>
           <tr>
             <td>pathLenConstraint: </td>
             <td><xsl:apply-templates select="BasicConstraints.pathLenConstraint"/></td>
           </tr>
          </table>
  </xsl:template>

</xsl:stylesheet>
