This tutorial explains all about X.509 certificates and its current formats and shows how it can be implemented in .NET environment.
A public key certificate, usually just called a digital certificate or certs is a digitally signed document that is commonly used for authentication and secure exchange of information on open networks, such as the Internet, extranets, and intranets. A certificate securely binds a public key to the entity that holds the corresponding private key. Certificates are digitally signed by the issuing certification authority (CA) and can be issued for a user, a computer, or a service. This creates a trust relationship between two unknown entities. The CA is the Grand Pooh-bah of Validation in an organization, which everyone trusts, and in some public key environments, no certificate is considered valid unless it has been attested to by a CA. Example of a popular CA’s authority is http://www.verisign.com/ .
Certificates can be issued for a variety of functions such as Web user authentication, Web server authentication, secure e-mail (Secure/Multipurpose Internet Mail Extensions, or S/MIME), and Internet Protocol security (IPSec), Transport Layer Security (TLS), and code signing. For example, when using the Internet for online banking, it is important to know that your Web browser is communicating directly and securely with your bank's Web server. Your Web browser must be able to achieve Web server authentication before a safe transaction can occur. Microsoft Internet Explorer uses Secure Sockets Layer (SSL) to encrypt messages and transmit them securely across the Internet, as do most other modern Web browsers and Web servers.
An X.509 certificate includes the public key and information about the person or entity to whom the certificate is issued, information about the certificate, plus optional information about the certification authority (CA) issuing the certificate.
It is a public key exchange framework and OSI standard specified by ISO/IEC 9594-8.
Formats of X.509 Certificates:
- DER Encoded Binary X.509 (.cer)
- Base64 Encoded X.509 (.cer)
- PKCS#7 / Cryptographic Message Syntax Standard (.p7b)
- PKCS#12 / Personal Information Exchange (.pfx)
DER Encoded Binary X.509
DER (Distinguished Encoding Rules) for ASN.1, as defined in ITU-T Recommendation X.509, is a more restrictive encoding standard than the alternative BER (Basic Encoding Rules) for ASN.1, as defined in ITU-T Recommendation X.209, upon which DER is based. Both BER and DER provide a platform-independent method of encoding objects (such as certificates and messages) for transmission between devices and applications. During certificate encoding, most applications use DER because a portion of the certificate (the Certification Request's Certification Request Info) must be DER-encoded to be signed. This format might be used by certification authorities that are not on Windows2000 servers, so it is supported for interoperability. DER certificate files use the .cer extension.
Base64 Encoded X.509
This is an encoding method developed for use with Secure/Multipurpose Internet Mail Extensions (S/MIME) which is a popular, standard method for transferring binary attachments over the Internet. Base64 encodes files into ASCII text format, making corruption less likely as the files are sent through Internet gateways, while S/MIME provides some cryptographic security services for electronic messaging applications, including non-repudiation of origin using digital signatures, privacy and data security using encryption, authentication, and message integrity. The MIME (Multipurpose Internet Mail Extensions) specification (RFC1341 and successors) defines a mechanism for encoding arbitrary binary information for transmission by electronic mail. Because all MIME-compliant clients can decode Base64 files, this format might be used by certification authorities that are not on Windows2000 servers, so it is supported for interoperability. Base64 certificate files use the .cer extension.
Cryptographic Message Syntax Standard (PKCS#7)
The PKCS#7 format enables the transfer of a certificate and all the certificates in its certification path from one computer to another or from a computer to removable media. PKCS#7 files typically use the .p7b extension and are compatible with the ITU-TX.509 standard. PKCS#7 allows for attributes such as countersignatures to be associated with signatures. Attributes such as signing time can be authenticated along with message content. For information on PKCS #7 see ww.rsa.com.
Personal Information Exchange (PKCS#12)
The Personal Information Exchange format (.pfx, also called PKCS#12) enables the transfer of certificates and their corresponding private keys from one computer to another or from a computer to removable media. PKCS#12 (Public Key Cryptography Standard#12) is an industry format that is suitable for transport or backup and restoration of a certificate and its associated private key. This can be between products from the same vendor or different vendors. To use the PKCS#12 format, the cryptographic service provider (CSP) must recognize the certificate and keys as exportable. If a certificate was issued from a Windows2000 certification authority, the private key for that certificate is only exportable if one of the following is true: The certificate is for EFS (encrypting file system) or EFS recovery. The certificate was requested through the Advanced Certificate Request certification authority Web page with the Mark keys as exportable check box selected. Because exporting a private key might expose it to unintended parties, the PKCS#12 format is the only format supported in WindowsXP for exporting a certificate and its associated private key. For information on PKCS #12 see ww.rsa.com.
Let us make a new X.509 certificate and implement in .NET environment. However remember .NET 1.0 / 1.1 supports only DER Encoded Binary X.509 format only. (However at the bottom of article I will told you a way to use latest certificate formats such as PKSC #12 (.pfx) in .NET).
The most widely accepted format for certificates is defined by the ITU-T X.509 version 3 international standards. The certificates are encoded using OSI ASN.1 DER. Some primary fields in X.509 certificate are indicated below:
Which version of X.509
This number plus the CA’s name uniquely identifies the certificate
The algorithm used to sign certificate
X.500 name of CA
The starting and ending period
The entity whose key being certified
The subject’s pubic key and ID of algorithm using it.
Microsoft provides many security tools in .NET SDK. Most of them are consoled based utilities applications. That is:
- Permission and assembly management tools
- Certificate management tools
We are concerned with only certificate management tools. They are
Meaning (See MSDN for more detail options of each application)
Generate a X.509 certificate for testing purpose only
Assembles certificates into CTL (certificate trust list) and can also be used for revoking lists (CRLs).
Verifies the validity of a file signed with an X.509 certificate
Creates, for test purposes only, a Software Publisher's Certificate (SPC) from one or more X.509 certificates.
Let us create a DER based X.509 certificate (.cer) with following command on the Visual Studio 03 command prompt.
makecert -sk Adnan -n "CN=Adnan Company" Adnan.cer
- -sk is Subject name
- –n is Specifies the subject's key container location, which contains the private key. If a key container does not exist, it will be created.
This will create a X.509 certificate (Adnan.cer) in personal folder directory of user currently logged. Now we can retrieve its properties using System.Security.Cryptography.X509Certificates class. The Certificate is also included in source code.
Now we will test this certificate and creates a Software Publisher's Certificate (SPC) file (PCKS # 7) from following command on VS command prompt.
cert2spc Adnan.cer Adnanspc.spc
This will create (Adnanspc.spc) file in personal folder directory of user currently logged. However this is for test purposes only. You can obtain a valid SPC from a Certification Authority such as VeriSign or Thawte. The spc file is included in source code.
Use Chktrust to checks the validity of a file signed with an Authenticode certificate.
The signed file could be any valid application (exe). If application does not have a valid
signature, the tool displays the Security Warning dialog box. The dialog gives you the option
to install and run the PE file even though an Authenticode signature could not be found.
Similarly use Certmgr to manages certificates, certificate trust lists (CTLs), and certificate
revocation lists (CRLs). The following command displays a default system store called my
with verbose output (/v).
certmgr /v /s my
Code to access X.509 properties in .NET:
Start a new windows application and give it a meaningful name and you have a form name FORM1 in project.
Next add a Button on this form and write code in that button event handler as shown below:
Private Sub Button1_Click (ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
‘get certificate in Bin directory
Dim Cert As X509Certificate = X509Certificate.CreateFromCertFile(
Directory.GetCurrentDirectory & "\Adnan.cer")
‘Now retrieve its properties in output window using ToString Mehtod. Since this
‘methods that do the same job
‘Get the most important values in string format.
Dim resultsTrue As String = Cert.ToString(True)
‘Display the value in output window
‘Now display Serial number in bytes with GetSerialNumber() method
Dim SerialNumber() As Byte = Cert.GetSerialNumber()
Dim Sr As Byte
Debug.Write("Serial Number in Bytes: ")
For Each Sr In SerialNumber
PKCS #12 Certificates in .NET.
- We can use PKCS #12 (.pfx) certificates in .NET using latest CAPICOM wrappers available from Microsoft site http://www.microsoft.com/msdownload/platformsdk/sdkupdate/psdkredist.htm.
- Download file pvkimprt.exe directly from http://office.microsoft.com/downloads/2000/pvkimprt.aspx needed to generate PFX file (PKCS #12).
How to create PKCS #12 Certificate in .NET
The following commands in Visual studio 03 command prompt can be used to create a PFX file (CodeProject.pfx)with a self-signed certificate and the associated private key (.pvk):
- makecert -r -n "CN=CodeProject" -b <date year="2004" day="20" month="12" />20/12/2004</date /> -e <date year="2099" day="1" month="1" />01/01/2099</date /> -eku 126.96.36.199.188.8.131.52.7 -sv CodeProject.pvk CodeProject.cer
- cert2spc CodeProject.cer CodeProject.spc
- pvkimprt -pfx CodeProject.spc CodeProject.pvk
How to use PKCS #12 Certificate in .NET
You can use PKCS #12 (.pfx) certificates in .NET using latest CAPICOM wrappers available from Microsoft site http://www.microsoft.com/msdownload/platformsdk/sdkupdate/psdkredist.htm. Search MSDN 2004 in Platform SDK -> Security for article “PKCS #12 File Types: Portable Protected Keys in .NET” or see online www.msdn.microsoft.com for detail. The article shows how to use CAPICOM to access PKSC #12 files in C# code however it can be easily converted into Vb .NET code.
We have seen what amazing power .NET have for management of certificates.