<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>unitstep.net &#187; certificates</title>
	<atom:link href="http://unitstep.net/blog/category/certificates/feed/" rel="self" type="application/rss+xml" />
	<link>http://unitstep.net</link>
	<description>the home of peter chng</description>
	<lastBuildDate>Mon, 19 Mar 2012 01:49:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Using the Basic Constraints extension in X.509 v3 certificates for intermediate CAs</title>
		<link>http://unitstep.net/blog/2009/03/16/using-the-basic-constraints-extension-in-x509-v3-certificates-for-intermediate-cas/</link>
		<comments>http://unitstep.net/blog/2009/03/16/using-the-basic-constraints-extension-in-x509-v3-certificates-for-intermediate-cas/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 03:15:36 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[certificates]]></category>
		<category><![CDATA[cryptography]]></category>
		<category><![CDATA[guides]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pki]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[X.509]]></category>
		<category><![CDATA[bouncy castle]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[extensions]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=773</guid>
		<description><![CDATA[It&#8217;s not often that you&#8217;ll be creating your own X.509 certificates for a web server, since any certificates that you create (self-signed or signed by your own CA) will not be trusted by most browsers (IE, Firefox, etc.) since they were not signed by one of the many Certificate Authorities (CAs) that have been automatically [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s not often that you&#8217;ll be creating your own X.509 certificates for a web server, since any certificates that you create (self-signed or signed by your own CA) will not be trusted by most browsers (IE, Firefox, etc.) since they were not signed by one of the many Certificate Authorities (CAs) that have been automatically trusted by the browser.  If you do decide to use one of these certificates on your web server, you&#8217;ll have to navigate through <a href="http://blog.ivanristic.com/2008/04/firefox-3-ssl-i.html">a Byzantine series of screens to &#8220;confirm&#8221; that you trust the server&#8217;s certificate</a>.  (Though this is annoying, it may be ultimately beneficial in today&#8217;s era of phishing and other malicious behaviour.)</p>
<h2>A bit of background</h2>
<p>However, what I want to discuss today relates to <em>certificate chains</em>.  At the top of every certificate chain is a <strong>root CA</strong>, whose certificate is <em>self-signed</em>.  This sort of certificate can be considered a &#8220;God certificate&#8221; because it essentially says, <em>&#8220;Trust me, because I say so&#8221;</em>.  As you can imagine, that&#8217;s not much of an argument for trusting someone, so that is why your browser has a list of <em>default root CAs</em> that it automatically trusts.  </p>
<p class="image">
<a href="http://unitstep.net/wordpress/wp-content/uploads/2009/03/basic-constraints-0.jpg"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/03/basic-constraints-0-300x207.jpg" alt="basic-constraints-0" title="basic-constraints-0" width="300" height="207" class="alignnone size-medium wp-image-775" /></a><br />
Some default trusted CAs in Firefox.
</p>
<p>These root CAs are owned and operated by companies that are in the business of <em>issuing certificates</em> to other people for use on their servers.  They have been added to the default trusted list of most browsers so that an end user doesn&#8217;t need to manually add all of them; doing so would be a usability nightmare.  Essentially, these root CAs provide a <strong>trust anchor point</strong>, as not only are they trusted, but <em>any certificates they issue will also be automatically trusted by the browser</em>.  Attempting to visit a HTTPS/SSL website that does not have a trusted certificates results in a <a href="http://jeremy.visser.name/2008/01/26/firefox-3-ssl-error/">nasty warning from modern browsers</a>.</p>
<p>Rarely is the root CA certificate directly used for a web server, but instead it is used to <strong>sign or issue other certificates</strong> that are then used on a web server to confirm its identity and provide for secure end-to-end communication.</p>
<p>As you can imagine, operating a CA is an immense responsibility, so that is why these default lists have been setup: Essentially these companies have to vet entities that purchase certificates from them, to make sure they actually own the domain that they are trying to buy a certificate for, otherwise phishing would become too easy!  Even so, these companies sometimes still have <a href="http://www.win.tue.nl/hashclash/rogue-ca/">lapses due to use of outdated technologies and poor security practices</a>, but that is another complicated issue for another day.</p>
<h2>Issuing a certificate &#8211; An example</h2>
<p>The act of <strong>issuing a certificate</strong> essential entails a CA using its public-private key pair to sign the contents of the certificate that is being issued.  This ties the identity information in the certificate to its key pair and provides confirmation that the CA has affirmed the authenticity of the certificate, I.E., that it has truly issued this certificate and that it has not been forged.</p>
<p>Going back to a certificate chains, it was previously mentioned that the root CA certificate is at the top of the chain.  Any certificates it issues are directly below it, so if these certificates are directly used on a web server, then the chain is of length two.  However, certificate chains can be longer.  If a certificate chain is longer than two, then this indicates the presence of an <strong>intermediate CA</strong>.</p>
<p>An intermediate CA is a CA that does not have a self-signed certificate but still has the capability to issue certificates that are trusted.  For an example of the root CA to intermediate CA relationship, we can look at the certificate chain returned from <a href="https://mail.google.com">https://mail.google.com</a>:</p>
<p class="image">
<a href="http://unitstep.net/wordpress/wp-content/uploads/2009/03/basic-constraints-1.jpg"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/03/basic-constraints-1-254x300.jpg" alt="basic-constraints-1" title="basic-constraints-1" width="254" height="300" class="alignnone size-medium wp-image-785" /></a><br />
The Root CA certificate from VeriSign, an X.509 v1 certificate.
</p>
<p>Above we see the <em>root CA certificate</em>, a self-signed certificate created/issued by <strong>VeriSign</strong>.  I&#8217;ve highlighted the fact that it is an X.509 <strong>version 1</strong> certificate, which also means it doesn&#8217;t have any <strong>certificate extensions</strong>.  This may not mean much right now, but we&#8217;ll get back to it soon.</p>
<p class="image">
<a href="http://unitstep.net/wordpress/wp-content/uploads/2009/03/basic-constraints-2.jpg"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/03/basic-constraints-2-254x300.jpg" alt="basic-constraints-2" title="basic-constraints-2" width="254" height="300" class="alignnone size-medium wp-image-787" /></a><br />
The Intermediate CA certificate from Thawte, an X.509 v3 certificate.
</p>
<p>This next shot shows the <em>intermediate CA certificate</em> that was issued by the root CA.  This certificate has been issued to <strong><a href="http://en.wikipedia.org/wiki/Thawte">Thawte</a></strong>, a company coincidentally founded by Mark Shuttleworth, the South African man behind Canonical/Ubuntu.  Thawte was acquired by VeriSign during the dot-com craze for US $575 million.</p>
<p class="image">
<a href="http://unitstep.net/wordpress/wp-content/uploads/2009/03/basic-constraints-3.jpg"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/03/basic-constraints-3-254x300.jpg" alt="basic-constraints-3" title="basic-constraints-3" width="254" height="300" class="alignnone size-medium wp-image-788" /></a><br />
The &#8220;Basic Constraints&#8221; extension of the intermediate CA.
</p>
<p>We can clearly see that this certificate is an X.509 <strong>version 3</strong> certificate, meaning it does support certificate extensions.  One of its extensions is a <strong><a href="http://www.alvestrand.no/objectid/2.5.29.19.html">Basic Constraints</a></strong> extension, which has been set to signify that this is indeed a Certificate Authority.  It also specifies one other parameter, which is the maximum number of intermediate CAs allowed <em>beneath</em> this one in the certificate chain hierarchy.  Since this value is set to 0, this means this intermediate CA <strong>cannot</strong> issue any more CA certificates, but instead can only issue <strong>client certificates</strong>.  Any attempt will to use a client certificate from this CA as a CA or signing certificate will fail, when consumed by a conforming client.</p>
<h2>The client certificate</h2>
<p>The last screenshot shows the <strong>client certificate</strong>, which is the last certificate in the chain.  This is the certificate that is used by the server at <code>mail.google.com</code> to secure HTTPS traffic, and as we can see, it is also an X.509 v3 certificate (has extensions) and one of those extensions is the &#8220;Basic Constraints&#8221; extension.  This time it is set to indicate that this is <strong>not</strong> a CA certificate.</p>
<p class="image">
<a href="http://unitstep.net/wordpress/wp-content/uploads/2009/03/basic-constraints-4.jpg"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/03/basic-constraints-4-254x300.jpg" alt="basic-constraints-4" title="basic-constraints-4" width="254" height="300" class="alignnone size-medium wp-image-789" /></a><br />
The Basic Constraints of the client certificate, indicating it is <strong>not</strong> a CA certificate.
</p>
<h2>Basic Contraints &#8211; Why it&#8217;s needed</h2>
<p>The &#8220;<strong>Basic Constraints</strong>&#8221; extension is one way for a CA to control the usage of the certificates it issues.  For instance, when the root CA certificate in the example above issued the intermediate CA certificate, it set the Basic Constraints extension to signify that:</p>
<ul>
<li>The issued certificate is for a Certificate Authority, i.e. an intermediate CA.</li>
<li>This certificate <strong>may not</strong> be used to create further CA certificates</li>
</ul>
<p>In turn, the intermediate CA certificate was used to create the client certificate for <code>mail.google.com</code>, and it attached a Basic Constraints extension to signify that this certificate <strong>was not</strong> a CA certificate.  By doing this, it was indicating that this certificate should not be used to sign/create further certificates.</p>
<p>This is necessary because of the how trust relationship works in X.509 PKI.  Someone who trusts the root CA implicitly trusts all the intermediate CAs, and then by extension, all the client certificates issued by those intermediate CAs! (Note how this creates a single point-of-failure at the root CA as well)</p>
<p>If the CA could not control what the certificates it issued were used for, then someone could purchase a VeriSign certificate and use it to sign/create other certificates which would also be trusted by default! Clearly, this is not desirably from a security or financial point of view, if you are VeriSign.  By using extensions such as the Basic Constraints one, the signing CA can enact fine-grained control over how the certificate is used.  If the client certificate was used to sign another certificate, that certificate would be rejected by a browser that conformed to the X.509 v3 specifications.</p>
<h2>The Grey Area</h2>
<p>However, we run into a &#8220;grey area&#8221; of sorts when faced with a certificate that <strong>does not have a Basic Constraints extension</strong>.  In this case, it is not indicated whether this is a CA certificate or not.  How do the browsers respond in this scenario? In this case, it seems to depend on whether the CA is a root CA or an intermediate one.</p>
<p>For root CA certificates, it seems that the Basic Constraints extension is not required in order for the CA certificate to be viewed as valid from the browser&#8217;s point of view.  (I&#8217;ve observed this in Firefox and Internet Explorer)  This most likely stems from the fact that there are root CAs that were created and put into operation well before X.509 v3 extensions were in wide use.  The VeriSign root CA in our example is an X.509 v1 certificate with a starting validity date of 1996-01-28.</p>
<p>However, for intermediate CAs, it seems that the Basic Constraints extension <strong>is required</strong> if you want things to work, at least in Firefox and Internet Explorer.  I encountered this situation when working with a Private Root CA of my own.  I was trying to create an intermediate CA (without any Basic Constraints extension) from this root CA, and was running into problems when using this intermediate CA to create client certificates.  Any of the client certificates from the intermediate CA were being essentially rejected by the browser when attempting to visit the website they were being used for.</p>
<p>Because this was a &#8220;grey area&#8221;, the results were mixed.  In Firefox, the site would load correctly, however when attempting to view the certificate chain (by double-clicking the lock icon in the lower right), only the client certificate could be viewed, not the fully certificate chain.  Internet Explorer would show the full certificate chain but simply failed to load the page.  Neither browser gave any indication as to why things were failing.</p>
<p>However, once I created an Intermediate CA with a Basic Constraints extension set to explicitly signify that this was indeed a CA, everything worked as expected.  I don&#8217;t believe this is well-documented, though this is understandable since most people will not be creating their own Private CAs unless it&#8217;s for a very specialized purpose.</p>
<h2>How to do this using the Bouncy Castle APIs</h2>
<p>I&#8217;ve talked about the <a href="/blog/2008/10/27/extracting-x509-extensions-from-a-csr-using-the-bouncy-castle-apis/">Bouncy Castle Java APIs</a> before, and they have been an invaluable resource for simplifying the creation of a Private CA and for issuing certificates.</p>
<p>When issuing a certificate it&#8217;s fairly easy to set the Basic Constraints extension to indicate you want the certificate to be a CA certificate.  First, take a look at this <a href="http://www.bouncycastle.org/wiki/display/JA1/X.509+Public+Key+Certificate+and+Certification+Request+Generation">guide to under the fundamentals of certificate creation</a> with the Bouncy Castle APIs, then look at this code fragment:</p>
<pre><code>private static final int NUM_ALLOWED_INTERMEDIATE_CAS = 0;
...

// Construct the certificate.
final X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();

...

// Need this extension to signify that this certificate is a CA and
// can issue certificates. (Extension is marked as critical)
certGen.addExtension( X509Extensions.BasicConstraints, true, new BasicConstraints(
  NUM_ALLOWED_INTERMEDIATE_CAS ) );

...

final X509Certificate intermediateCaCert = certGen.generate( signingCaPrivateKey, "SunRsaSign" );</code></pre>
<p>By doing this you ensure that the intermediate CA certificate has the proper Basic Constraints extension to work correctly with modern web browsers.</p>
<h2>Conclusion</h2>
<p>I hope you found this helpful.  Certainly if you&#8217;re here, you&#8217;ve been puzzled over the same issues that I struggled through!</p>
<h3>References</h3>
<ol class="note less">
<li><a href="http://www.bouncycastle.org/wiki/display/JA1/X.509+Public+Key+Certificate+and+Certification+Request+Generation">X.509 Public Key Certificate and Certification Request Generation</a></li>
<li><a href="http://www.alvestrand.no/objectid/2.5.29.19.html">OID 2.5.29.19 &#8211; Basic Constraints</a></li>
<li><a href="http://www.oid-info.com/get/2.5.29.19">OID Repository &#8211; basicConstraints(19)</a></li>
</ol>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2009/03/16/using-the-basic-constraints-extension-in-x509-v3-certificates-for-intermediate-cas/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Extracting X509 Extensions from a CSR using the Bouncy Castle APIs</title>
		<link>http://unitstep.net/blog/2008/10/27/extracting-x509-extensions-from-a-csr-using-the-bouncy-castle-apis/</link>
		<comments>http://unitstep.net/blog/2008/10/27/extracting-x509-extensions-from-a-csr-using-the-bouncy-castle-apis/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 02:01:07 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[certificates]]></category>
		<category><![CDATA[cryptography]]></category>
		<category><![CDATA[guides]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pki]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[X.509]]></category>
		<category><![CDATA[ca]]></category>
		<category><![CDATA[certificate]]></category>
		<category><![CDATA[certificate request]]></category>
		<category><![CDATA[csr]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=407</guid>
		<description><![CDATA[The Bouncy Castle Cryptography Java APIs are an excellent set of APIs that act as a provider for JCE and JCA. Additionally, they take care of the mundane and tedious (some would say overly complicated) details involved in reading and creating the data structures associated with the X.500 and PKCS standards. (The APIs are also [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://bouncycastle.org/">Bouncy Castle Cryptography Java APIs</a> are an excellent set of APIs that act as a provider for JCE and JCA.  Additionally, they take care of the mundane and tedious (some would say overly complicated) details involved in reading and creating the data structures associated with the X.500 and PKCS standards. (The APIs are also available in C#, for .NET developers out there)</p>
<p>One thing they handle well is the concept of certificate extensions.  X.509 v3 certificates introduced the concept of these extensions, which are basically additional (potentially optional) fields containing information not contained in the older original X.509 specifications.  Each extension is specified by an OID (Object Identifier); a good <a href="http://www.alvestrand.no/objectid/2.5.29.html">list of these extensions</a> is available.</p>
<p>While it&#8217;s easy to read these extensions from an existing X.509 v3 certificate using the Bouncy Castle APIs it is a bit more involved to read these extensions from a <a href="http://en.wikipedia.org/wiki/Certificate_signing_request">Certificate Signing Request</a>, or CSR; this is the data structure that is sent to a CA to request a certificate.  The CA then reads the data from this and creates a signed certificate issued to the requester.  In this guide I&#8217;ll present a brief way to extract X.509 <a href="http://www.alvestrand.no/objectid/submissions/1.2.840.113549.1.9.14.html">extensions request</a> from a CSR so that they may be included in the resulting issued certificate.</p>
<h3>Code: The good stuff</h3>
<p>Assuming you have added the Bouncy Castle JARs to your classpath, you should have access to the classes used here.  </p>
<p>You must first have the CSR in the format of a Bouncy Castle data object, namely the <a href="http://www.cs.berkeley.edu/~jonah/bc/org/bouncycastle/jce/PKCS10CertificationRequest.html"><code>PKCS10CertificationRequest</code></a>. If all you have is the PEM-format of the CSR (i.e. Base64-encoded contents delimited by headers like <code>----- BEGIN CERTIFICATE REQUEST -----</code> and <code>----- END CERTIFICATE REQUEST -----</code>) then you will need to convert  this to the proper data structure using something like<br />
<a href="http://juliusdavies.ca/commons-ssl/javadocs/org/apache/commons/ssl/PEMUtil.html">PEMUtil</a> from Commons-SSL like I have done below.  (BC has a <a href="http://www.eecs.berkeley.edu/~jonah/javadoc/org/bouncycastle/jce/provider/PEMUtil.html">PEMUtil</a> class as well, but it appears to be only for internal use)</p>
<pre><code>// NOTE: Commons-SSL doesn't support generics.
final List pemItems = PEMUtil.decode( csrContent.getBytes() );

// Verify list isn't empty - uses Apache Commons Lang.
Validate.isTrue( !pemItems.isEmpty() );

// No support for generics, so have to cast. (Could have cast the entire List)
final PEMItem csrPemFormat = (PEMItem) pemItems.get( 0 );

// Verify the type.
Validate.isTrue( csrPemFormat.pemType.equals( "CERTIFICATE REQUEST" ),
  "This is not a CSR" );

final PKCS10CertificationRequest csr = new PKCS10CertificationRequest(
  csrPemFormat.getDerBytes() );</code></pre>
<p>We first decode the PEM (Base64) CSR into <code>List</code> of <a href="http://juliusdavies.ca/commons-ssl/javadocs/org/apache/commons/ssl/PEMItem.html"><code>PEMItem</code></a>s. Note that Commons-SSL doesn&#8217;t support <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html">generics</a>, so you are going to get a cast warning somewhere in the code, no matter what.  When calling <code>getBytes()</code> on the CSR string, you may want to specify the <code>US-ASCII</code> character set, since the no-arg method uses the platform default character set, which might give inconsistent results across different systems when converting from characters to bytes. </p>
<p>We then grab the first entry in the list, checking if it is a CSR.  We can now convert this into the proper data structure by supplying the raw bytes (i.e. the DER-encoded format) to the constructor of <code>PKCS10CertificationRequest</code>.</p>
<p>The method to extract the <a href="http://www.cs.berkeley.edu/~jonah/bc/org/bouncycastle/asn1/x509/X509Extensions.html"><code>X509Extensions</code></a> structure from the <code>PKCS10CertificationRequest</code> is shown below.</p>
<pre><code>   /**
    * Gets the X509 Extensions contained in a CSR (Certificate Signing Request).
    *
    * @param certificateSigningRequest the CSR.
    * @return the X509 Extensions in the request.
    * @throws CertificateException if the extensions could not be found.
    */
   X509Extensions getX509ExtensionsFromCsr(
         final PKCS10CertificationRequest certificateSigningRequest ) throws CertificateException
   {
      final CertificationRequestInfo certificationRequestInfo = certificateSigningRequest
            .getCertificationRequestInfo();

      final ASN1Set attributesAsn1Set = certificationRequestInfo.getAttributes();

      // The `Extension Request` attribute is contained within an ASN.1 Set,
      // usually as the first element.
      X509Extensions certificateRequestExtensions = null;
      for (int i = 0; i &lt; attributesAsn1Set.size(); ++i)
      {
         // There should be only only one attribute in the set. (that is, only
         // the `Extension Request`, but loop through to find it properly)
         final DEREncodable derEncodable = attributesAsn1Set.getObjectAt( i );
         if (derEncodable instanceof DERSequence)
         {
            final Attribute attribute = new Attribute( (DERSequence) attributesAsn1Set
                  .getObjectAt( i ) );

            if (attribute.getAttrType().equals( PKCSObjectIdentifiers.pkcs_9_at_extensionRequest ))
            {
               // The `Extension Request` attribute is present.
               final ASN1Set attributeValues = attribute.getAttrValues();

               // The X509Extensions are contained as a value of the ASN.1 Set.
               // Assume that it is the first value of the set.
               if (attributeValues.size() &gt;= 1)
               {
                  certificateRequestExtensions = new X509Extensions( (ASN1Sequence) attributeValues
                        .getObjectAt( 0 ) );

                  // No need to search any more.
                  break;
               }
            }
         }
      }

      if (null == certificateRequestExtensions)
      {
         throw new CertificateException( "Could not obtain X509 Extensions from the CSR" );
      }

      return certificateRequestExtensions;
   }</code></pre>
<p>Basically, we get the certificate request info from the CSR structure and then extract attributes from it.  Then, we loop through to find the attribute with the <a href="http://www.alvestrand.no/objectid/submissions/1.2.840.113549.1.9.14.html">&#8220;Extension Request&#8221; OID</a>.</p>
<p>After that, I make an assumption that the actual extensions are contained in the first value of the place of the ASN.1 Set that makes up the &#8220;Extensions Request&#8221; structure &#8211; not a big assumption, and in my testing I haven&#8217;t encountered a situation where this wasn&#8217;t the case.  It&#8217;s worthwhile to keep in mind that ASN.1 often prescribes Set or multi-value structures in places where the underlying data can  only be single-valued. </p>
<p>After running through that code, we&#8217;ll have either found the extensions, and be returning them in a <a href="http://www.cs.berkeley.edu/~jonah/bc/org/bouncycastle/asn1/x509/X509Extensions.html"><code>X509Extensions</code></a> structure, or an exception will be thrown.  You could modify the code to return <code>null</code> if that suits your style or purpose better.</p>
<h3>A few more notes</h3>
<p>Once you have the <code>X509Extensions</code> structure you can use the extensions contained within to create/issue a certificate with them.  Check out the <a href="http://www.bouncycastle.org/wiki/display/JA1/X.509+Public+Key+Certificate+and+Certification+Request+Generation">Bouncy Castle Guide on Certificate Generation</a> for more details.</p>
<p>Note that a CA is <em>not required</em> to use any of the extension requests present in a CSR &#8211; hence the name &#8220;requests&#8221;.  It is entirely up to the CA to decide what extensions are appropriate, along with their values, for the certificates that it issues.  </p>
<h3>Code Review</h3>
<p>The code is a little complicated and could probably benefit from some refactoring.  However, a lot of the complexity derives from the fact that the X.509 and associated standards are quite complex themselves.  This is a reflection on the vision that the designers of X.509 had for the future of the standard.  However, the <a href="http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt">complexity of X.509</a> is another topic for another article.  </p>
<p>I hope you found this article useful, as while I found lots of information for <a href="http://www.bouncycastle.org/wiki/display/JA1/X.509+Public+Key+Certificate+and+Certification+Request+Generation">generating CSRs</a>, information on parsing and working with them was a little sparse.  Please feel free to leave your comments below!</p>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/10/27/extracting-x509-extensions-from-a-csr-using-the-bouncy-castle-apis/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

