Git Product home page Git Product logo

ocsp's Introduction

Tests Coverage Status Scrutinizer Code Quality Packagist Downloads

Online Certificate Status Protocol PHP Library

This repository contains a PHP library that helps you checking if HTTPS certificates are revoked, by using the Online Certificate Status Protocol (OCSP).

This library doesn't require exec calls to system utilities like OpenSSL: it's a pure PHP library.

This library doesn't include any network-related helpers: you have to use your own transport libraries (cURL, Zend HTTP, Guzzle or whatever).

Checking HTTPS certificates requires:

  • the certificate to be checked, in PEM format (that is, the text files that starts with `-----BEGIN CERTIFICATE-----), or in DER format (that is, binary files)
  • the issuer certificate, that is the certificate of the Certification Authority that provided you the HTTPS certificate
  • the URL, provided by the Certification Authority, to be used for OCSP calls (the so-called OCSR Responder URL)

Obtaining the certificate and the issuer certificate from an HTTPS URL

You can get the HTTPS certificate and the issuer certificate from an HTTPS URL by using some code like this:

$hCurl = curl_init($url);
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($hCurl, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($hCurl, CURLOPT_NOBODY, true);
curl_setopt($hCurl, CURLOPT_CERTINFO, true);
curl_exec($hCurl);
$certInfo = curl_getinfo($hCurl, CURLINFO_CERTINFO);

$certificate = $certInfo[0]['Cert'];
$issuerCertificate = $certInfo[1]['Cert'];

Obtaining the issuer certificate from a certificate

HTTPS certificates usually contain an URL where you can find the certificate of the certificate issuer.

You can use this code to extract this URL, provided that '/path/to/certificate' is the path to a local file that contains your HTTPS certificate:

$certificateLoader = new \Ocsp\CertificateLoader();
$certificate = $certificateLoader->fromFile('/path/to/certificate');
$certificateInfo = new \Ocsp\CertificateInfo();
$urlOfIssuerCertificate = $certificateInfo->extractIssuerCertificateUrl($certificate);

At this point, $urlOfIssuerCertificate will contain the URL where the issuer certificate can be downloaded from (if it's an empty string, that means that the issuer certificate URL is not included in your certificate).

Obtaining the OCSP Responder URL

To check if a certificate is valid, we need to know an URL, provided by the authority that issued the certificate, that can be called to check if the certificate has been revoked.

This URL may be included in the HTTPS certificate itself.

To get it, you can use the following code (provided that '/path/to/certificate' is the path to a local file that contains your HTTPS certificate):

$certificateLoader = new \Ocsp\CertificateLoader();
$certificate = $certificateLoader->fromFile('/path/to/certificate');
$certificateInfo = new \Ocsp\CertificateInfo();
$ocspResponderUrl = $certificateInfo->extractOcspResponderUrl($certificate);

Checking if a certificate has been revoked

Once you have the HTTPS certificate, the issuer certificate, and the OCSP Responder URL, you can check if the HTTPS certificate has been revoked, or if it's still valid.

In order to do so, you have to write some code like this (here we use cURL, but you can use any other transport library):

$certificateLoader = new \Ocsp\CertificateLoader();
$certificateInfo = new \Ocsp\CertificateInfo();
$ocsp = new \Ocsp\Ocsp();

// Load the HTTPS certificate and the issuer certificate
$certificate = $certificateLoader->fromFile('/path/to/certificate');
$issuerCertificate = $certificateLoader->fromFile('/path/to/issuer/certificate');

// Extract the relevant data from the two certificates
$requestInfo = $certificateInfo->extractRequestInfo($certificate, $issuerCertificate);

// Build the raw body to be sent to the OCSP Responder URL
$requestBody = $ocsp->buildOcspRequestBodySingle($requestInfo);

// Actually call the OCSP Responder URL (here we use cURL, you can use any library you prefer)
$hCurl = curl_init();
curl_setopt($hCurl, CURLOPT_URL, $ocspResponderUrl);
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($hCurl, CURLOPT_POST, true);
curl_setopt($hCurl, CURLOPT_HTTPHEADER, ['Content-Type: ' . \Ocsp\Ocsp::OCSP_REQUEST_MEDIATYPE]);
curl_setopt($hCurl, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($hCurl, CURLOPT_POSTFIELDS, $requestBody);
$result = curl_exec($hCurl);
$info = curl_getinfo($hCurl);
if ($info['http_code'] !== 200) {
    throw new \RuntimeException("Whoops, here we'd expect a 200 HTTP code");
}
if ($info['content_type'] !== \Ocsp\Ocsp::OCSP_RESPONSE_MEDIATYPE) {
    throw new \RuntimeException("Whoops, the Content-Type header of the response seems wrong!");
}

// Decode the raw response from the OCSP Responder
$response = $ocsp->decodeOcspResponseSingle($result);

At this point, $response contains an instance of the Ocsp\Response class:

  • the certificate is not revoked if $response->isRevoked() === false
  • the certificate is revoked if $response->isRevoked() === true (you can get the devocation date/time by calling $response->getRevokedOn())
  • in case of unknown state, $response->isRevoked() will return null

Exceptions

Problems may arise while loading the certificates, creating the request body for the OCSP Responder, analyzing the response from the OCSP Responder. To catch these errors, you can enclose your code within try/catch statements:

try {
    // code
} catch (\Ocsp\Exception\Exception $problem) {
    // handle the error cases
}

ocsp's People

Contributors

mlocati avatar splitice avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ocsp's Issues

Problem with Request/Response with revoked certificate

Hello,

thank you for this library.
I have a problem with certificate which is revoked (others works well), but it shows a error in decoding result.

Decoded URL OCSP: http://ocsp.ica.cz/2qca16_rsa - it's OK

Tested with https://decoder.link/ocsp - shows rights values.
But doesnt work with this library, could you please look?

Thank you

-----BEGIN CERTIFICATE-----
MIIHwzCCBaugAwIBAgIEALDhPjANBgkqhkiG9w0BAQsFADB/MQswCQYDVQQGEwJD
WjEoMCYGA1UEAwwfSS5DQSBRdWFsaWZpZWQgMiBDQS9SU0EgMDIvMjAxNjEtMCsG
A1UECgwkUHJ2bsOtIGNlcnRpZmlrYcSNbsOtIGF1dG9yaXRhLCBhLnMuMRcwFQYD
VQQFEw5OVFJDWi0yNjQzOTM5NTAeFw0xOTEwMTgwODA0NDFaFw0yMDEwMTcwODA0
NDFaMFwxHzAdBgNVBAMMFkRyLiBIb3LDoWsgLSBQU0VVRE9OWU0xCzAJBgNVBAYT
AkNaMRMwEQYDVQRBDApEci4gSG9yw6FrMRcwFQYDVQQFEw5JQ0EgLSAxMDUxODk0
OTCCASIwDQYJKoZIhvcNAQEBB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
TZEsESf+G4FDLzzFNz5DbCRBP55sR2G+hvvXhPU2BPYI9Ir3vgnE9w/Fxxqp6xGG
GIKlRALSletgwQod8ktwZEhfyO8OqFt6opMN8qSMHv0HRHvlYyz/3kq83/lXklT6
OBgnA/Xji2TzgGS+7477ep2pycy3wmI=
-----END CERTIFICATE-----

add nextUpdate to response

I'm writing a Kubernetes controller to automatically manage OCSP data and having access to the nextUpdate property if present would be helpful. Thanks!

Nonce extension

Hello,
While using this wondeful package, i encountered this exception :
CouldNotCheckCertificate-error-Cannot build OCSP Response 'Request unauthorized
After checking, this is about the nonce extention added in the RFC 8954 (https://datatracker.ietf.org/doc/rfc8954/)
The original RFC has been updated according to that modification (https://datatracker.ietf.org/doc/html/rfc6960#section-4.4.1)
Could it be added in the function buildOcspRequestBody located in the Ocsp class ?
Thanks in advance :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.