# Certificates

# How to Add the SAN to the Certificate Request

###  

**certutil -setreg policy\\EditFlags +EDITF\_ATTRIBUTESUBJECTALTNAME2**

**net stop certsvc &amp; net start certsvc**

###  

### **1. Using `certreq.exe` with a Configuration File**

This method involves creating an `.inf` file to define the certificate request details.

#### **Steps:**

<div class="b_crtrm_cnt b_show" id="bkmrk-create-a-configurati"><div class="b_cnt_resp b_md_code" data-bm="64" tabindex="0">1. Create a configuration file (e.g., `request.inf`) with the following content:
    
    ##### Plaintext
    
    <div class="b_crtrm_code_header"><div class="b_crtrm_code_tooltips"><button aria-disabled="false" class="acf-button-standard__btn" tabindex="" title="Copy code" type="button"><svg aria-hidden="true" class="acf-icon__icon" viewbox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"></svg></button><div class="acf-button-standard__label">Copy code</div></div></div>```plaintext
    [NewRequest]
    Subject = "CN=example.com"
    KeySpec = 1
    KeyLength = 2048
    Exportable = TRUE
    MachineKeySet = TRUE
    SMIME = FALSE
    PrivateKeyArchive = FALSE
    UserProtected = FALSE
    UseExistingKeySet = FALSE
    ProviderName = "Microsoft RSA SChannel Cryptographic Provider"
    ProviderType = 12
    RequestType = PKCS10
    KeyUsage = 0xa0
    
    [Extensions]
    2.5.29.17 = "{text}"
    _continue_ = "DNS=example.com&DNS=www.example.com&DNS=api.example.com"
    
    ```
2. Run the following PowerShell command:
    
    ##### Powershell
    
    <div class="b_crtrm_code_header"><div class="b_crtrm_code_tooltips"><button aria-disabled="false" class="acf-button-standard__btn" tabindex="" title="Copy code" type="button"><svg aria-hidden="true" class="acf-icon__icon" viewbox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"></svg></button><div class="acf-button-standard__label">Copy code</div></div></div>```powershell
    certreq -new request.inf request.csr
    
    ```
    
    This generates a Certificate Signing Request (CSR) file (`request.csr`).
3. Submit the CSR to your Certificate Authority (CA) to obtain the certificate.

</div></div>### **2. Using the `Get-Certificate` Cmdlet**

This method works if you have access to an Active Directory Certificate Services (AD CS) CA.

#### **Example Script:**

##### Powershell

<div class="b_crtrm_cnt b_show" id="bkmrk-copy-code"><div class="b_cnt_resp b_md_code" data-bm="64" tabindex="0"><div class="b_crtrm_code_header"><div class="b_crtrm_code_tooltips"><button aria-disabled="false" class="acf-button-standard__btn" data-bm="65" tabindex="" title="Copy code" type="button"><svg aria-hidden="true" class="acf-icon__icon" viewbox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"></svg></button><div class="acf-button-standard__label">Copy code</div></div></div></div></div>```powershell
$SANs = @("DNS=example.com", "DNS=www.example.com", "DNS=api.example.com")
$CertRequest = @{
    DnsName = $SANs
    CertStoreLocation = "Cert:\LocalMachine\My"
    Template = "WebServer"  # Replace with your CA's template name
    CA = "CA_SERVER_NAME\CA_NAME"  # Replace with your CA details
}

Get-Certificate @CertRequest

```

This will request and install the SAN certificate directly from the specified CA.

### **3. Using a Custom PowerShell Script**

You can also use a script to automate the process of generating a CSR with SANs. For example:

##### Powershell

<div class="b_crtrm_cnt b_show" id="bkmrk-copy-code-1"><div class="b_cnt_resp b_md_code" data-bm="64" tabindex="0"><div class="b_crtrm_code_header"><div class="b_crtrm_code_tooltips"><button aria-disabled="false" class="acf-button-standard__btn" tabindex="" title="Copy code" type="button"><svg aria-hidden="true" class="acf-icon__icon" viewbox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"></svg></button><div class="acf-button-standard__label">Copy code</div></div></div></div></div>```powershell
$SANs = @("DNS=example.com", "DNS=www.example.com", "DNS=api.example.com")
$Subject = "CN=example.com"
$CertFile = "C:\Path\To\request.csr"

$Config = @"
[NewRequest]
Subject = "$Subject"
KeySpec = 1
KeyLength = 2048
Exportable = TRUE
MachineKeySet = TRUE
RequestType = PKCS10
KeyUsage = 0xa0

[Extensions]
2.5.29.17 = "{text}"
_continue_ = "$(($SANs -join '&'))"
"@

Set-Content -Path "C:\Path\To\request.inf" -Value $Config
certreq -new "C:\Path\To\request.inf" $CertFile

```

This script dynamically generates the `.inf` file and creates the CSR.

<div class="b_crtrm_cus b_crtrm_ctrls" id="bkmrk-edit-perform-analysi"><div class="b_cus_fields_wrapper"><div class="b_cus_fields"><fieldset aria-labelledby="CodeAnalysis" class="b_field"><div class="b_field_options" role="group">  
</div></fieldset></div></div></div>