Convert PEM-Encoded SSL Certificate to DER-Encoded using OpenSSL

Convert PEM-Encoded SSL Certificate to DER-Encoded using OpenSSL

SSL certificates play an important role in securing web communications by encrypting data exchanged between servers and clients. These certificates come in various formats, with PEM and DER being two common ones. While PEM is widely used, there are scenarios where you might need to use a DER-encoded certificate. This tutorial explains how to convert PEM-encoded SSL certificate to DER-encoded using OpenSSL.

Let's say we have the following PEM-encoded certificate:

-----BEGIN CERTIFICATE-----
MIICBzCCAbGgAwIBAgIUblsRRtsMLsqKSXHL3OeHVDTAaAkwDQYJKoZIhvcNAQEL
BQAwWDELMAkGA1UEBhMCVVMxFTATBgNVBAoMDFRlc3QgQ29tcGFueTEfMB0GA1UE
CwwWVGVzdCBPcmdhbml6YXRpb24gVW5pdDERMA8GA1UEAwwIdGVzdC5jb20wHhcN
MjIwNzE4MDEzMDMwWhcNMjMwNzE4MDEzMDMwWjBYMQswCQYDVQQGEwJVUzEVMBMG
A1UECgwMVGVzdCBDb21wYW55MR8wHQYDVQQLDBZUZXN0IE9yZ2FuaXphdGlvbiBV
bml0MREwDwYDVQQDDAh0ZXN0LmNvbTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDf
6LwJoaDTpxNlaCXlLp6RAfgsE8j2yiPFZ0Pqy2u+a4gbZn2KxnVA/Ar/6foqC/Os
cEr1/h2F55D33xF3OyJVAgMBAAGjUzBRMB0GA1UdDgQWBBR01dWXvKnNVQl97YZP
vnpvtsFb7jAfBgNVHSMEGDAWgBR01dWXvKnNVQl97YZPvnpvtsFb7jAPBgNVHRMB
Af8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA0EAOeLvRxxC+WnhnBaURnyhWM1aHP6j
WYXSGiQh6VA6vxoShAUFd56anAT7LfX6wJdnDJrtHkaK2zK6JM7mxqF79w==
-----END CERTIFICATE-----

The openssl x509 command can be used to convert a PEM-encoded certificate to DER-encoded:

openssl x509 -in test.crt -outform DER -out test_der.crt

The meaning of options:

  • -in test.crt - specifies the input file, in this case, the PEM-encoded certificate.
  • -outform DER - indicates the desired output format, which is DER in this case.
  • -out test_der.crt - specifies the output file name for the DER-encoded certificate.

Conversely, if you have a DER-encoded certificate and need to convert it back to PEM-encoded, you can use the following OpenSSL command:

openssl x509 -in test_der.crt -out test.crt

Leave a Comment

Cancel reply

Your email address will not be published.