Skip to main content

x509_certificate resource

[edit on GitHub]

Use the x509_certificate Chef InSpec audit resource to test the fields and validity of an x.509 certificate.

X.509 certificates use public/private key pairs to sign and encrypt documents or communications over a network. They may also be used for authentication.

Examples include SSL certificates, S/MIME certificates and VPN authentication certificates.



This resource is distributed along with Chef InSpec itself. You can use it automatically.


This resource first became available in v1.18.0 of InSpec.


An x509_certificate resource block declares a certificate key file to be tested.

describe x509_certificate('mycertificate.pem') do
  its('validity_in_days') { should be > 30 }

The filepath property can also be used.

describe x509_certificate(filepath: 'mycertificate.pem') do
  its('validity_in_days') { should be > 30 }

The resource also supports passing in the certificate content.

cert_content = file('certificate.pem').content

describe x509_certificate(content: cert_content) do
  its('validity_in_days') { should be > 30 }

If both content and filepath is given, the value passed in content is used.



subject property makes it easier to access individual subject elements.

describe x509_certificate('/etc/pki/') do
  its('subject.CN') { should eq "" }

subject_dn (String)

The subject_dn string returns the distinguished name of the subject field. It contains several fields separated by forward slashes. The field identifiers are the same ones used by OpenSSL to generate CSR’s and certs. Use subject.XX instead to access the parsed version.

e.g. /C=US/L=Seattle/O=Chef Software Inc/OU=Chefs/CN=Richard Nixon

describe x509_certificate('/etc/pki/') do
  its('subject_dn') { should match "" }


issuer makes it easier to access individual issuer elements.

describe x509_certificate('/etc/pki/') do
  its('issuer.CN') { should eq "Acme Trust CA" }

issuer_dn (String)

The issuer_dn is the distinguished name from a CA (certificate authority) during the certificate signing process. It describes which authority is guaranteeing the identity of our certificate.

e.g. /C=US/L=Seattle/CN=Acme Trust CA/

describe x509_certificate('/etc/pki/') do
  its('issuer_cn') { should match "CN=Acme Trust CA" }

public_key (String)

The public_key property returns a base64 encoded public key in PEM format.

describe x509_certificate('/etc/pki/') do
  its('public_key') { should match "-----BEGIN PUBLIC KEY-----\nblah blah blah..." }

key_length (Integer)

The key_length property calculates the number of bits in the public key. More bits increase security, but at the cost of speed and in extreme cases, compatibility.

describe x509_certificate('/etc/pki/') do
  its('key_length') { should be 2048 }

signature_algorithm (String)

The signature_algorithm property describes which hash function was used by the CA to sign the certificate.

describe x509_certificate('/etc/pki/') do
  its('signature_algorithm') { should be 'sha256WithRSAEncryption' }

validity_in_days (Float)

The validity_in_days property can be used to check that certificates are not in danger of expiring soon.

describe x509_certificate('/etc/pki/') do
  its('validity_in_days') { should be > 30 }

not_before and not_after (Time)

The not_before and not_after properties expose the start and end dates of certificate validity. They are exposed as ruby Time class so that date arithmetic can be easily performed.

describe x509_certificate('/etc/pki/') do
  its('not_before') { should be <= }
  its('not_after')  { should be >= }

serial (Integer)

The serial property exposes the serial number of the certificate. The serial number is set by the CA during the signing process and should be unique within that CA.

describe x509_certificate('/etc/pki/') do
  its('serial') { should eq 9623283588743302433 }

version (Integer)

The version property exposes the certificate version.

describe x509_certificate('/etc/pki/') do
  its('version') { should eq 2 }

extensions (Hash)

The extensions hash property is mainly used to determine what the certificate can be used for.

describe x509_certificate('/etc/pki/') do
  # Check what extension categories we have
  its('extensions') { should include 'keyUsage' }
  its('extensions') { should include 'extendedKeyUsage' }
  its('extensions') { should include 'subjectAltName' }

  # Check examples of basic 'keyUsage'
  its('extensions.keyUsage') { should include 'Digital Signature' }
  its('extensions.keyUsage') { should include 'Non Repudiation' }
  its('extensions.keyUsage') { should include 'Data Encipherment' }

  # Check examples of newer 'extendedKeyUsage'
  its('extensions.extendedKeyUsage') { should include 'TLS Web Server Authentication' }
  its('extensions.extendedKeyUsage') { should include 'Code Signing' }

  # Check examples of 'subjectAltName'
  its('extensions.subjectAltName') { should include '' }

Was this page helpful?

Search Results