Save a sorted CRT file

While using a Let’s Encrypt certificate exported to a .CRT file, I encountered an error stating, ”Server sent unsorted certificate chain in violation of the TLS specification,” upon connecting to my Filezilla FTP Server. To resolve this, I crafted a function that reorders the original file and saves it with the correct certificate chain sequence.

  1. Read the CRT File:
    • Use Get-Content -Path $OriginalCRTFilepath -Raw  to read the raw content of the .crt  file into a single string.
  2. Regex Pattern Matching:
    • The $regexPattern  defines a regular expression to match individual certificate blocks within the file. It captures attributes like subject, issuer, and the certificate itself.
  3. Finding the Root CA Certificate:
    • Identify the root CA certificate by comparing the subject and issuer fields. If they match, it’s the root certificate.
  4. Finding the Intermediate CA Certificate:
    • The intermediate CA certificate is identified based on the fact that it’s issued by the root CA certificate. You exclude any matches where the subject and issuer are the same (since the root CA certificate is self-issued).
  5. Identifying the End-Entity Certificate (Certificate):
    • The end-entity certificate (your actual certificate) is identified based on the fact that it’s issued by the intermediate CA certificate.
  6. Rename the Original CRT File:

    • Move the original file to $unSortedCRTFilePath filepath.
  7. Saving the Sorted CRT File:
    • Concatenate the certificates in the desired order (certificate, intermediate, root) and save them to the Original filepath $OriginalCRTFilepath .

 

Ett svar på ”Save a sorted CRT file

  1. Pingback: A regular expression to match a certificate in a CRT file | My Raspberry Wordpress

Lämna ett svar

Din e-postadress kommer inte publiceras. Obligatoriska fält är märkta *