简体   繁体   中英

How to verify DER certificate with openssl?

PEM works fine

openssl verify -CAfile CA/ca.crt leaf.cert.pem

But DER generated with openssl x509 -in leaf.cert.pem -outform der -out leaf.cert.der could not be verified

openssl verify -CAfile CA/ca.crt leaf.cert.der

produces

unable to load certificate
4613703104:error:0909006C:PEM routines:get_name:no start line:crypto/pem/pem_lib.c:745:Expecting: TRUSTED CERTIFICATE

openssl verify does not accept -inform der as other openssl commands.

Does this mean there is no way to verify DER directly and I need to convert it to PEM and then verify?

People normal use piping to pipe the output from one command into another command.

So to verify a DER format you could do:

openssl x509 -inform der -in .\leaf.cert.cer -outform pem | openssl
verify -CAfile CA/ca.crt

This assumes that "leaf.cert.cer" is in DER format and "CA/ca.crt" is in PEM format.

To break it down:

openssl x509 -inform der -in .\leaf.cert.cer -outform pem

Converts the DER certificate to PEM format with the output to the stdout.

openssl verify -CAfile CA/ca.crt

Verifies the PEM certificate from stdin.

And you combine the two with the pipe '|'command which pipes the stdout from the first command to the stdin for the second command.

Create bash script to check certificate status. Convert to PEM format if needed.

#!/bin/bash

#
# Check certificate revocation status.
# Convert certificate to PEM format if needed.

CERT=$1

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

CERT_PEM_TMP=$(mktemp  -p $DIR -t "cert_pem.XXXXXXXXXX")
ISSUER_CRT_TMP=$(mktemp  -p $DIR -t "issuer_crt.XXXXXXXXXX")
ISSUER_PEM_TMP=$(mktemp  -p $DIR -t "issuer_pem.XXXXXXXXXX")


cert_to_pem () {
    _CertFn=$1
    _PemCertFn=$2
    _Res=$(grep -c '\-\-BEGIN CERTIFICATE\-\-' $_CertFn)
    echo "is_pem RES: $_Res"
    if [ $_Res == "0" ]; then
        echo "DER format"
        openssl x509 -inform DER -in $_CertFn -out $_PemCertFn
    else
        ech "PEM Format"
        openssl x509 -inform PEM -in $_CertFn -out $_PemCertFn
    fi
}

cert_to_pem $CERT $CERT_PEM_TMP

CRT_URI=$( openssl x509 -in $CERT_PEM_TMP -text -noout | grep 'CA Issuers' | sed -e "s/^.*CA Issuers - URI://" )
echo "CRT_URI: $CRT_URI"
curl --silent $CRT_URI > $ISSUER_CRT_TMP
#./export_to_pem.tcl $ISSUER_CRT_TMP $ISSUER_PEM_TMP
cert_to_pem $ISSUER_CRT_TMP $ISSUER_PEM_TMP

OSCP_URI=$(openssl x509 -in $CERT_PEM_TMP -ocsp_uri -noout)
OSCP_HOST=$(echo $OSCP_URI | sed -e 's|^[^/]*//||' -e 's|/.*$||')

echo "check certificate: $CERT"
echo "ISSUER_PEM_TMP: $ISSUER_PEM_TMP"
echo "CERT_PEM_TMP: $CERT_PEM_TMP"
echo "OSCP_URI: $OSCP_URI"
echo "OSCP_HOST: $OSCP_HOST"
echo "Server response:"
openssl ocsp -no_nonce -issuer $ISSUER_PEM_TMP -cert $CERT_PEM_TMP -url   $OSCP_URI -header Host=$OSCP_HOST

echo "Server response end:"
rm $CERT_PEM_TMP
rm $ISSUER_CRT_TMP
rm $ISSUER_PEM_TMP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM