title: How to use bouncycastle in java to generate a CSR with a subject alternative name
body: <p>After a bunch of searching around,  I came up with the following:</p>
  <pre><code>        PKCS10CertificationRequestBuilder p10Builder = new JcaPKCS10CertificationRequestBuilder(
              new X500Principal(&quot;CN=clustername&quot;), publicKey);
          
  
          ASN1Encodable[] subjectAlternativeNames2 = new ASN1Encodable[] {
                  new GeneralName(GeneralName.rfc822Name, &quot;clusteruid&quot;),
                  new GeneralName(GeneralName.dNSName, &quot;127.0.0.1&quot;)
          };
          DERSequence subjectAlternativeNamesExtension = new DERSequence(subjectAlternativeNames2);
          p10Builder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, subjectAlternativeNamesExtension);//.addExtension(Extension.subjectAlternativeName, false, subjectAlternativeNames);
          JcaContentSignerBuilder csBuilder = new JcaContentSignerBuilder(&quot;MD5WithRSA&quot;);
          ContentSigner signer = csBuilder.build(privateKey);
          PKCS10CertificationRequest thecsr = p10Builder.build(signer);
  
          out = new FileOutputStream(outFile + &quot;x.csr&quot;);
          out.write(&quot;-----BEGIN CERTIFICATE REQUEST-----\n&quot;.getBytes());
          out.write(Base64.getEncoder().encodeToString(thecsr.getEncoded()).getBytes());
          out.write(&quot;\n-----END CERTIFICATE REQUEST-----\n&quot;.getBytes());
          out.close();
  </code></pre>
  <p>The above &quot;seems&quot; to work,  and generates a file /tmp/licensingx.csr as expected.included</p>
  <p>Ive been using openssl to verify.   When I use:</p>
  <blockquote>
  <p>openssl req -in /tmp/licensingx.csr  -text -noout</p>
  </blockquote>
  <p>I was expecting to see something like the following included as part of the output:</p>
  <blockquote>
  <p>Requested Extensions:
  X509v3 Subject Alternative Name:
  EMAIL:clusterid, DNS:127.0.0.1</p>
  </blockquote>
  <p>in fact,  the requested extensions section is missing.   Can anyone suggest something?  We are already using the cluster name as the CN in the subject.</p>