Passed
Push — master ( f5ca5a...0e4678 )
by Thomas
07:23
created

Crl::getNextUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace MadWizard\WebAuthn\Pki;
4
5
use DateTimeImmutable;
6
use DateTimeZone;
7
use MadWizard\WebAuthn\Exception\ParseException;
8
use MadWizard\WebAuthn\Exception\VerificationException;
9
use phpseclib3\File\X509;
10
11
/**
12
 * @experimental
13
 */
14
final class Crl
15
{
16
    /**
17
     * @var X509
18
     */
19
    private $crl;
20
21
    /**
22
     * @var DateTimeImmutable|null
23
     */
24
    private $nextUpdate;
25
26
    /**
27
     * @param string $crlData CRL data as PEM or DER
28
     *
29
     * @throws ParseException        When CRL or issuer certificate could not be parsed.
30
     * @throws VerificationException When CRL signature is invalid
31
     */
32
    public function __construct(string $crlData, X509Certificate ...$caCertificates)
33
    {
34
        $crl = new X509();
35
        foreach ($caCertificates as $ca) {
36
            if ($crl->loadCA($ca->asDer()) === false) {
37
                throw new ParseException('Failed to load CA certificate for CRL.');
38
            }
39
        }
40
41
        $crlInfo = $crl->loadCRL($crlData);
42
        if ($crlInfo === false) {
0 ignored issues
show
introduced by
The condition $crlInfo === false is always true.
Loading history...
43
            throw new ParseException('Failed to load CRL data.');
44
        }
45
46
        $nextUpdate = $crlInfo['tbsCertList']['nextUpdate']['utcTime'] ?? null;
47
        if ($nextUpdate !== null) {
48
            $this->nextUpdate = new DateTimeImmutable($nextUpdate, new DateTimeZone('UTC'));
49
        }
50
51
        if (true !== $crl->validateSignature()) {
52
            throw new VerificationException('Failed to verify CRL signature.');
53
        }
54
        $this->crl = $crl;
55
    }
56
57
    public function isRevoked(string $serial): bool
58
    {
59
        return $this->crl->getRevoked($serial) !== false;
60
    }
61
62
    public function getNextUpdate(): ?DateTimeImmutable
63
    {
64
        return $this->nextUpdate;
65
    }
66
}
67