CertAttributes: Accessing X509 Certificate Attributes


CertAttributes is a .NET assembly utility using P/Invoke to CryptoAPI which demonstrates the rudimentary steps in extracting standard X509 certificate extensions. It is meant to demonstrate techniques to supplement currently shipping .NET crypto capability.

The CertAttributes code:

While it is possible to declare a rather large CERT_CONTEXT structure with its many in-lined substructures in managed code, the technique used here involves simply calculating offsets of the necessary fields (in an OS independent manner) and manually advancing an IntPtr to access the required fields. This technique is used extensively in advanced P/Invoke and is important to understand if you plan to use P/Invoke regularly.

Because certificate extensions decode to a wide variety of different types of structures, marshaling of decoded data to useable user-friendly data must be handled on an OID case-by-case basis. CertAttributes demonstrates a simple approach using a String based switch/case structure.

CertAttributes takes a single argument, a certificate SubjectName substring.

C# Source


Michel I. Gallant
neutron@istar.ca