QualifiedCertificateTest::testSubject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bip70\Test\X509;
6
7
use Bip70\Exception\X509Exception;
8
use Bip70\Protobuf\Proto\X509Certificates;
9
use Bip70\X509\QualifiedCertificate;
10
use Bip70\X509\RequestValidation;
11
use Bip70\X509\TrustStoreLoader;
12
use PHPUnit\Framework\TestCase;
13
use Sop\CryptoEncoding\PEM;
14
use Sop\CryptoEncoding\PEMBundle;
15
use X509\Certificate\Certificate;
16
use X509\Certificate\CertificateBundle;
17
use X509\CertificationPath\CertificationPath;
18
use X509\CertificationPath\PathValidation\PathValidationConfig;
19
20
class QualifiedCertificateTest extends TestCase
21
{
22
    public function testCertificatesMustMatch()
23
    {
24
        $bundle = CertificateBundle::fromPEMBundle(PEMBundle::fromFile(__DIR__ . "/../../data/testnet-only-cert-not-valid.cabundle.pem"));
25
        $x509 = new X509Certificates();
26
        foreach ($bundle->all() as $it) {
27
            $x509->addCertificate($it->toDER());
28
        }
29
30
        // 10/12/2017 ish
31
        $now = new \DateTimeImmutable();
32
        $now = $now->setTimestamp(1509692666);
33
34
        $validationConfig = new PathValidationConfig($now, 10);
35
        $validator = new RequestValidation($validationConfig, TrustStoreLoader::fromSystem());
36
        $qualified = $validator->validateCertificateChain($x509);
37
38
        $selfCert = Certificate::fromPEM(PEM::fromFile(__DIR__ . "/../../data/selfsigned.cert.pem"));
39
        $selfBundle = new CertificateBundle($selfCert);
40
        $selfSignedPath = CertificationPath::toTarget($selfCert, $selfBundle);
41
42
        $this->expectExceptionMessage("CertificationPath entity certificate must match PathValidationResult certificate");
43
        $this->expectException(X509Exception::class);
44
45
        new QualifiedCertificate($selfSignedPath, $qualified->getValidationResult());
46
    }
47
48
    public function testSubject()
49
    {
50
        $bundle = CertificateBundle::fromPEMBundle(PEMBundle::fromFile(__DIR__ . "/../../data/testnet-only-cert-not-valid.cabundle.pem"));
51
        $x509 = new X509Certificates();
52
        foreach ($bundle->all() as $it) {
53
            $x509->addCertificate($it->toDER());
54
        }
55
56
        // 10/12/2017 ish
57
        $now = new \DateTimeImmutable();
58
        $now = $now->setTimestamp(1509692666);
59
        $validationConfig = new PathValidationConfig($now, 10);
60
61
        $validator = new RequestValidation($validationConfig, TrustStoreLoader::fromSystem());
62
        $qualified = $validator->validateCertificateChain($x509);
63
        $this->assertTrue(Certificate::fromDER($x509->getCertificate(0))->tbsCertificate()->subject()->equals($qualified->subject()));
64
        $this->assertTrue($qualified->getPath()->endEntityCertificate()->tbsCertificate()->subject()->equals($qualified->subject()));
65
    }
66
}
67