title: Create certificate and CSR using Windows PowerShell/.Net without using external programs or libraries
body: <p>I would like to create a Private Key and a CSR, submit the CSR to a Certificate Authority, retrieve the certificate once issued, and have the Private Key and Certificate as separate PEM files suitable for use in non-Microsoft applications (they are generally web servers).  I'd like to avoid using Java Keytool or OpenSSL to generate keys and certificate signing requests in Windows PowerShell on Windows Server 2016.  The CSRs will be submitted to a Microsoft Active Directory Certificate Services.</p>
  
  <p>OpenSSL and Java are not (and won't be) installed on the computers requiring certificates.  As the certificates are for non-Microsoft applications, I also want to avoid using the Certificate Store on the computers.  I don't mind using "certreq" to actually submit the completed CSR and retrieve the resulting certificate once approved.</p>
  
  <p>I have some code, based on <a href="https://stackoverflow.com/questions/23734792/c-sharp-export-private-public-rsa-key-from-rsacryptoserviceprovider-to-pem-strin/23739932#23739932">C# Export Private/Public RSA key from RSACryptoServiceProvider to PEM string</a>, which will extract the private key from an X509Certificate2.  So far, as an experiment, I have used this successfully with a PKCS12 keystore (where the key and CSR were created with Keytool).</p>
  
  <p>Inspired by <a href="https://stackoverflow.com/questions/52784878/automate-the-process-of-creating-a-private-key-a-csr-and-a-final-signed-certifi">Automate the process of creating a private key, a CSR and a final Signed Certificate in .NET Core</a> I knocked together the following, but ran out of inspiration, and didn't really know what I was doing.  How do I complete the process of submitting the CSR to the CA (or outputting the CSR as a file for using with certreq)?</p>
  
  <pre><code>[int]$KeyLength = 2048
  $ComputerName = "jon"
  $Domain = "domain.local"
  [string]$DistinguishedName = "CN=$($ComputerName).$($Domain),OU=Unit,O=Org,C=GB"
  $HashAlgo = [System.Security.Cryptography.HashAlgorithmName]::SHA256
  $RSASigPadding = [System.Security.Cryptography.RSASignaturePadding]::Pkcs1
  
  $RSAKey = [System.Security.Cryptography.RSA]::Create($KeyLength)
  $Certificate = [System.Security.Cryptography.X509Certificates.CertificateRequest]::new($DistinguishedName,$RSAKey,$HashAlgo,$RSASigPadding)
  
  # Add Basic Constraints
  $BasicConstraints = [System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension]::new($false,$false,0,$false)
  $BCExtension = [System.Security.Cryptography.X509Certificates.X509Extension]::new($BasicConstraints,$false)
  $Certificate.CertificateExtensions.Add($BCExtension)
  
  # Add Subject Key Identifier extension
  $SubjectKeyIdentifier = [System.Security.Cryptography.X509Certificates.X509SubjectKeyIdentifierExtension]::new($Certificate.PublicKey,$false)
  $SKIExtension = [System.Security.Cryptography.X509Certificates.X509Extension]::new($SubjectKeyIdentifier,$false)
  $Certificate.CertificateExtensions.Add($SKIExtension)
  
  # Add Key Usage
  $KeyUsageFlags = [System.Security.Cryptography.X509Certificates.X509KeyUsageFlags]::DigitalSignature -bor [System.Security.Cryptography.X509Certificates.X509KeyUsageFlags]::KeyEncipherment
  $KeyUsage = [System.Security.Cryptography.X509Certificates.X509KeyUsageExtension]::new($KeyUsageFlags,$true)
  $KUExtension = [System.Security.Cryptography.X509Certificates.X509Extension]::new($KeyUsage,$true)
  $Certificate.CertificateExtensions.Add($KUExtension)
  
  # Add EKU
  $ServerAuthentication = [System.Security.Cryptography.Oid]::New("Server Authentication")
  $EKUOidCollection = [System.Security.Cryptography.OidCollection]::new()
  $EKUOidCollection.Add($ServerAuthentication) | out-null # this outputs 0
  $EnhancedKeyUsage = [System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension]::new($EKUOidCollection,$false)
  $EKUExtension = [System.Security.Cryptography.X509Certificates.X509Extension]::new($EnhancedKeyUsage,$false)
  $Certificate.CertificateExtensions.Add($EKUExtension)
  
  # Add SAN
  $SubjectAlternateNameBuilder = [System.Security.Cryptography.X509Certificates.SubjectAlternativeNameBuilder]::new()
  $SubjectAlternateNameBuilder.AddDnsName("$($ComputerName).$($Domain)")
  $Certificate.CertificateExtensions.Add($SubjectAlternateNameBuilder.Build())
  </code></pre>