Skip to content

Commit

Permalink
Merge pull request #68 from liamdennehy/ocsp-requests
Browse files Browse the repository at this point in the history
OCSP requests
  • Loading branch information
liamdennehy authored Jun 1, 2020
2 parents b068187 + c09d78f commit 4e95664
Show file tree
Hide file tree
Showing 41 changed files with 1,508 additions and 131 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ php:
- '7.1'
- '7.2'
- '7.3'
- '7.4'
install:
- composer config -g github-oauth.github.com "$GITHUB_API_KEY"
- cat composer.json
- composer install
# - mkdir -p ./data
# - wget -O data/eu-lotl.xml https://ec.europa.eu/tools/lotl/eu-lotl.xml
- bash -c tools/get-tls.sh
- scripts/showtests.sh
cache:
directories:
- $HOME/.composer/cache/files
- $HOME/data/tl
12 changes: 12 additions & 0 deletions src/ASN1Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace eIDASCertificate;

/**
*
*/
interface ASN1Interface
{
public function getBinary();
public function getASN1();
}
110 changes: 110 additions & 0 deletions src/AlgorithmIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace eIDASCertificate;

use eIDASCertificate\OID;
use eIDASCertificate\ParseException;
use ASN1\Type\UnspecifiedType;
use ASN1\Type\Constructed\Sequence;
use ASN1\Type\Primitive\ObjectIdentifier;
use ASN1\Type\Primitive\NullType;

class AlgorithmIdentifier implements ASN1Interface
{
private $binary;
private $algorithmName;
private $algorithmOID;
private $parametersIncluded;
private $parameters = [];

public function __construct($id, $parameters = null, $parametersIncluded = true)
{
if (is_array($parameters)) {
foreach ($parameters as $parameter) {
$this->parameters[] = $parameter;
}
}

$this->parametersIncluded = $parametersIncluded;
if (is_object($id)) {
if (get_class($id) == 'eIDASCertificate\AlgorithmIdentifier') {
$this->algorithmName = $id->getAlgorithmName();
$this->algorithmOID = $id->getAlgorithmOID();
$this->parameters = $id->getParameters();
return;
}
} elseif (is_string($id)) {
if (strpos($id, ".")) {
$this->algorithmName = OID::getName($id);
if ($this->algorithmName == 'unknown') {
throw new ParseException("Unknown algorithm OID '$id'", 1);
}
$this->algorithmOID = OID::getOID($this->algorithmName);
} else {
$this->algorithmOID = OID::getOID($id);
if ($this->algorithmOID == 'unknown') {
throw new ParseException("Unknown algorithm name '$id'", 1);
}
$this->algorithmName = OID::getName($this->algorithmOID);
}
}
}

public static function fromDER($der)
{
$obj = UnspecifiedType::fromDER($der)->asSequence();
if ($obj->has(1) && $obj->at(1)->tag() == 16) {
$parameters = [];
foreach ($obj->at(1)->asSequence()->elements() as $parameter) {
$parameters[] = $parameter->toDER();
}
} else {
$parameters = null;
}
$aid = new AlgorithmIdentifier(
$obj->at(0)->asObjectIdentifier()->oid(),
$parameters
);
return $aid;
}

public function getASN1()
{
$oid = new ObjectIdentifier($this->algorithmOID);
if (empty($this->parameters && $this->parametersIncluded)) {
if ($this->parametersIncluded) {
return (new Sequence($oid, new NullType));
} else {
return (new Sequence($oid));
}
} else {
foreach ($this->parameters as $parameterDER) {
$parameters[] = UnspecifiedType::fromDER($parameterDER)->asTagged();
}
return (new Sequence($oid, new Sequence(...$parameters)));
}
}

public function getBinary($value='')
{
return $this->getASN1()->toDER();
}
public function getAlgorithmName()
{
return $this->algorithmName;
}

public function getAlgorithmOID()
{
return $this->algorithmOID;
}

public function getParameters()
{
$parameters = [];
foreach ($this->parameters as $parameter) {
$parameters[] = base64_encode($parameter);
}
return $parameters;
}
}
2 changes: 1 addition & 1 deletion src/Certificate/AuthorityInformationAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace eIDASCertificate\Certificate;

use eIDASCertificate\Certificate\ExtensionInterface;
use eIDASCertificate\ExtensionInterface;
use eIDASCertificate\Certificate\CertificateException;
use eIDASCertificate\Certificate\X509Certificate;
use eIDASCertificate\OID;
Expand Down
4 changes: 2 additions & 2 deletions src/Certificate/AuthorityKeyIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace eIDASCertificate\Certificate;

use eIDASCertificate\Certificate\ExtensionInterface;
use eIDASCertificate\CertificateException;
use eIDASCertificate\ExtensionInterface;
use eIDASCertificate\Certificate\CertificateException;
use eIDASCertificate\Finding;
use eIDASCertificate\Certificate\X509Certificate;
use ASN1\Type\UnspecifiedType;
Expand Down
4 changes: 2 additions & 2 deletions src/Certificate/BasicConstraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace eIDASCertificate\Certificate;

use eIDASCertificate\Certificate\ExtensionInterface;
use eIDASCertificate\Certificate\ExtensionException;
use eIDASCertificate\ExtensionInterface;
use eIDASCertificate\ExtensionException;
use eIDASCertificate\Certificate\X509Certificate;
use eIDASCertificate\OID;
use eIDASCertificate\Finding;
Expand Down
4 changes: 2 additions & 2 deletions src/Certificate/CRLDistributionPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace eIDASCertificate\Certificate;

use eIDASCertificate\Certificate\ExtensionInterface;
use eIDASCertificate\Certificate\ExtensionException;
use eIDASCertificate\ExtensionInterface;
use eIDASCertificate\ExtensionException;
use eIDASCertificate\Certificate\X509Certificate;
use eIDASCertificate\OID;
use eIDASCertificate\Finding;
Expand Down
2 changes: 1 addition & 1 deletion src/Certificate/CertificatePolicies.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace eIDASCertificate\Certificate;

use eIDASCertificate\Certificate\ExtensionInterface;
use eIDASCertificate\ExtensionInterface;
use eIDASCertificate\CertificateException;
use eIDASCertificate\ParseException;
use eIDASCertificate\Finding;
Expand Down
2 changes: 1 addition & 1 deletion src/Certificate/CertificateRevocationList.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use ASN1\Type\UnspecifiedType;
use eIDASCertificate\Certificate\CRLException;
use eIDASCertificate\Certificate\DistinguishedName;
use eIDASCertificate\DistinguishedName;
use eIDASCertificate\OID;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Certificate/ExtendedKeyUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace eIDASCertificate\Certificate;

use eIDASCertificate\Certificate\ExtensionInterface;
use eIDASCertificate\ExtensionInterface;
use eIDASCertificate\Certificate\X509Certificate;
use eIDASCertificate\OID;
use eIDASCertificate\Finding;
Expand Down
4 changes: 2 additions & 2 deletions src/Certificate/KeyUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace eIDASCertificate\Certificate;

use eIDASCertificate\Certificate\ExtensionInterface;
use eIDASCertificate\Certificate\ExtensionException;
use eIDASCertificate\ExtensionInterface;
use eIDASCertificate\ExtensionException;
use eIDASCertificate\Certificate\X509Certificate;
use eIDASCertificate\OID;
use ASN1\Type\UnspecifiedType;
Expand Down
2 changes: 1 addition & 1 deletion src/Certificate/PreCertPoison.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace eIDASCertificate\Certificate;

use eIDASCertificate\Certificate\ExtensionInterface;
use eIDASCertificate\ExtensionInterface;
use eIDASCertificate\Certificate\X509Certificate;
use eIDASCertificate\Finding;

Expand Down
4 changes: 2 additions & 2 deletions src/Certificate/SubjectAltName.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace eIDASCertificate\Certificate;

use eIDASCertificate\Certificate\ExtensionInterface;
use eIDASCertificate\CertificateException;
use eIDASCertificate\ExtensionInterface;
use eIDASCertificate\Certificate\CertificateException;
use eIDASCertificate\ParseException;
use eIDASCertificate\Finding;
use eIDASCertificate\Certificate\X509Certificate;
Expand Down
2 changes: 1 addition & 1 deletion src/Certificate/SubjectKeyIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace eIDASCertificate\Certificate;

use eIDASCertificate\Certificate\ExtensionInterface;
use eIDASCertificate\ExtensionInterface;
use eIDASCertificate\Certificate\X509Certificate;
use eIDASCertificate\CertificateException;
use ASN1\Type\UnspecifiedType;
Expand Down
73 changes: 66 additions & 7 deletions src/Certificate/X509Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use eIDASCertificate\AttributeInterface;
use eIDASCertificate\Finding;
use eIDASCertificate\OID;
use eIDASCertificate\Extensions;
use eIDASCertificate\QCStatements;
use eIDASCertificate\Certificate\DistinguishedName;
use eIDASCertificate\ASN1Interface;
use eIDASCertificate\AlgorithmIdentifier;
use eIDASCertificate\DistinguishedName;
use eIDASCertificate\DigitalIdentity\DigitalIdInterface;
use eIDASCertificate\TSPService\TSPServiceException;
use ASN1\Type\UnspecifiedType;
Expand All @@ -17,7 +20,11 @@
/**
*
*/
class X509Certificate implements DigitalIdInterface, RFC5280ProfileInterface, AttributeInterface
class X509Certificate implements
DigitalIdInterface,
RFC5280ProfileInterface,
AttributeInterface,
ASN1Interface
{
private $x509;
private $crtResource;
Expand All @@ -38,14 +45,13 @@ class X509Certificate implements DigitalIdInterface, RFC5280ProfileInterface, At
private $notBefore;
private $notAfter;
private $signature;
private $signatureAlgrothimIdentifier;
public function __construct($candidate)
{
$this->x509 = new X509();
$this->crtBinary = X509Certificate::emit($candidate);
$this->crtResource = $this->x509->loadX509($this->crtBinary);
$crtASN1 = UnspecifiedType::fromDER($this->crtBinary)->asSequence();
$tbsCertificate = $crtASN1->at(0)->asSequence();
$signatureAlgorithm = $crtASN1->at(1)->asSequence();
$this->signatureAlgorithmIdentifier = AlgorithmIdentifier::fromDER($crtASN1->at(1)->asSequence()->toDER());
$signatureValue = $crtASN1->at(2)->asBitString()->string();
$idx = 0;
if ($tbsCertificate->hasTagged(0)) {
Expand All @@ -57,7 +63,7 @@ public function __construct($candidate)
// return null;
//
}
$this->serialNumber = $tbsCertificate->at($idx++)->asInteger()->number();
$this->serialNumber = gmp_strval($tbsCertificate->at($idx++)->asInteger()->number(), 16);
$this->signature = $tbsCertificate->at($idx++)->asSequence();
$this->issuer = new DistinguishedName($tbsCertificate->at($idx++));
$dates = $tbsCertificate->at($idx++)->asSequence();
Expand Down Expand Up @@ -304,7 +310,7 @@ public function getCRL()
}
}

public function getSerial()
public function getSerialNumber()
{
return $this->serialNumber;
}
Expand All @@ -328,16 +334,36 @@ public function toPEM()
return self::base64ToPEM(base64_encode($this->crtBinary));
}

public function getSubjectASN1()
{
return $this->subject->getASN1();
}

public function getSubjectDN()
{
return $this->subject->getDN();
}

public function getIssuerASN1()
{
return $this->issuer->getASN1();
}

public function getIssuerDN()
{
return $this->issuer->getDN();
}

public function getSubjectNameHash($algo = 'sha256')
{
return $this->subject->getHash($algo);
}

public function getIssuerNameHash($algo = 'sha256')
{
return $this->issuer->getHash($algo);
}

public function getSubjectExpanded()
{
return $this->subject->getExpanded();
Expand Down Expand Up @@ -511,4 +537,37 @@ public function getExtensionsBinary()
{
return $this->extensions->getBinary();
}

public function getSignatureAlgorithmIdentifier()
{
return $this->signatureAlgorithmIdentifier;
}

public function getSignatureAlgorithmName()
{
return $this->signatureAlgorithmIdentifier->getalgorithmName();
}

public function getSignatureAlgorithmParameters()
{
return $this->signatureAlgorithmIdentifier->getParameters();
}

public function getASN1()
{
throw new \Exception("getASN1 not implemented", 1);
}

public function getSubjectPublicKeyHash($algo = 'sha256')
{
return hash(
$algo,
UnspecifiedType::fromDER($this->publicKey)
->asSequence()
->at(1)
->asBitString()
->string(),
true
);
}
}
Loading

0 comments on commit 4e95664

Please sign in to comment.