title: Specify certificate template in certificate request using BouncyCastle
body: <p>I'm using BouncyCastle to generate a certificate request:</p>
  
  <pre><code>using Org.BouncyCastle.Asn1.X509;
  using Org.BouncyCastle.Crypto;
  using Org.BouncyCastle.Crypto.Generators;
  using Org.BouncyCastle.Crypto.Prng;
  using Org.BouncyCastle.Pkcs;
  using Org.BouncyCastle.Security;
  using System.IO;
  
  class Program {
      static void Main(string[] args) {
          var keyGenerator = new RsaKeyPairGenerator();
          keyGenerator.Init(
              new KeyGenerationParameters(
                  new SecureRandom(new CryptoApiRandomGenerator()),
                  2048));
          var keyPair = keyGenerator.GenerateKeyPair();
          X509Name name = new X509Name("CN=test");
          Pkcs10CertificationRequest csr = new Pkcs10CertificationRequest("SHA256WITHRSA", name, keyPair.Public, null, keyPair.Private);
          using (FileStream fs = new FileStream(@"X:\tmp\tmp.csr", FileMode.Create)) {
              var req = csr.GetDerEncoded();
              fs.Write(req, 0, req.Length);
          }
      }
  }
  </code></pre>
  
  <p>How should I specify the certificate template in the request?</p>
  
  <p><em>Note</em>: decoding a request created with the certificates console using <code>certutil</code>, it looks like the certificate template should be an extension of the request; I tried creating an extension accordingly:</p>
  
  <pre><code>var extGen = new Org.BouncyCastle.Asn1.X509.X509ExtensionsGenerator();
  extGen.AddExtension(
      new DerObjectIdentifier("1.3.6.1.4.1.311.21.7"), // OID for certificate template extension 
      true,
      new DerObjectIdentifier("1.3.6.1.4.1.311.21.8.the.OID.of.the.template"));
  </code></pre>
  
  <p>But then, I don't understand how I'm supposed to attach it to the request.</p>