Skip to content

Commit e103fd4

Browse files
Guido Gröönmrts
authored andcommitted
WE2-685 Added SubjectCertificateExpirityValidator
WE2-685 Added SubjectCertificatePolicyValidator and phpunit tests WE2-685 Added SubjectCertificatePurposeValidator and phpunit tests WE2-684 Added AuthTokenValidatorBuilder WE2-684 Added initial AuthTokenValidatorImpl class WE2-688 Added CertificateData class for read certificate properties and phpunit test WE2-688 Added initial CertificateValidator and phpunit tests WE2-688 Added initial X509 class for encode and decode X.509 certificates
1 parent 97e4cb5 commit e103fd4

36 files changed

+1370
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor
22
composer.lock
3-
.DS_Store
3+
.DS_Store
4+
.phpunit.result.cache

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@
1919
"psr-4": {
2020
"web_eid\\web_eid_authtoken_validation_php\\": ["src"]
2121
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"web_eid\\web_eid_authtoken_validation_php\\": ["tests"]
26+
}
2227
}
28+
2329
}

phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Web-eID PHP Test Suite">
10+
<directory>tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2020-2021 Estonian Information System Authority
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace web_eid\web_eid_authtoken_validation_php\certificate;
26+
27+
use web_eid\web_eid_authtoken_validation_php\util\X509;
28+
use UnexpectedValueException;
29+
use BadFunctionCallException;
30+
31+
final class CertificateData
32+
{
33+
34+
public function __construct()
35+
{
36+
throw new BadFunctionCallException('Utility class');
37+
}
38+
39+
/**
40+
* Get commonName from x509 certificate
41+
*
42+
* @throws UnexpectedValueException
43+
*/
44+
public static function getSubjectCN(X509 $certificate): string
45+
{
46+
return self::getField($certificate, 'CN');
47+
}
48+
49+
/**
50+
* Get surname from x509 certificate
51+
*
52+
* @throws UnexpectedValueException
53+
*/
54+
public static function getSubjectSurname(X509 $certificate): string
55+
{
56+
return self::getField($certificate, 'SN');
57+
}
58+
59+
/**
60+
* Get given name from x509 certificate
61+
*
62+
* @throws UnexpectedValueException
63+
*/
64+
public static function getSubjectGivenName(X509 $certificate): string
65+
{
66+
return self::getField($certificate, 'GN');
67+
}
68+
69+
/**
70+
* Get serialNumber (ID-code) from x509 certificate
71+
*
72+
* @throws UnexpectedValueException
73+
*/
74+
public static function getSubjectIdCode(X509 $certificate): string
75+
{
76+
return self::getField($certificate, 'serialNumber');
77+
}
78+
79+
/**
80+
* Get country code from x509 certificate
81+
*
82+
* @throws UnexpectedValueException
83+
*/
84+
public static function getSubjectCountryCode(X509 $certificate): string
85+
{
86+
return self::getField($certificate, 'C');
87+
}
88+
89+
/**
90+
* Get specified subject field from x509 certificate
91+
*
92+
* @throws UnexpectedValueException field identifier not found
93+
* @return string
94+
*/
95+
private static function getField(X509 $certificate, string $fieldId): string
96+
{
97+
$result = $certificate->getSubjectProp($fieldId);
98+
if ($result) {
99+
return $result;
100+
}
101+
throw new UnexpectedValueException('fieldId '.$fieldId.' not found in certificate subject');
102+
}
103+
104+
105+
}

src/certificate/CertificateLoader.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,37 @@
2424

2525
namespace web_eid\web_eid_authtoken_validation_php\certificate;
2626

27-
class CertificateLoader
27+
use web_eid\web_eid_authtoken_validation_php\exceptions\CertificateDecodingException;
28+
use web_eid\web_eid_authtoken_validation_php\util\X509;
29+
use BadFunctionCallException;
30+
31+
final class CertificateLoader
2832
{
29-
public function loadCertificatesFromPath(string $path): array
33+
34+
public function __construct()
35+
{
36+
throw new BadFunctionCallException('Utility class');
37+
}
38+
39+
/**
40+
* Loads certificate files from paths into array of OpenSSLCertificate
41+
* @param string ...$resourceNames array of certificate paths
42+
*
43+
* @return array
44+
* @throws CertificateDecodingException
45+
*/
46+
public static function loadCertificatesFromResources(string ...$resourceNames): array
3047
{
31-
return [];
48+
$caCertificates = [];
49+
foreach ($resourceNames as $resourceName) {
50+
$x509 = new X509();
51+
$certificate = $x509->loadX509(file_get_contents($resourceName));
52+
if ($certificate) {
53+
$caCertificates[] = $certificate;
54+
} else {
55+
throw new CertificateDecodingException($resourceName);
56+
}
57+
}
58+
return $caCertificates;
3259
}
3360
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2020-2021 Estonian Information System Authority
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace web_eid\web_eid_authtoken_validation_php\certificate;
26+
27+
use web_eid\web_eid_authtoken_validation_php\util\X509;
28+
use web_eid\web_eid_authtoken_validation_php\util\TrustedCertificates;
29+
use BadFunctionCallException;
30+
use DateTime;
31+
use Exception;
32+
use web_eid\web_eid_authtoken_validation_php\exceptions\CertificateExpiredException;
33+
use web_eid\web_eid_authtoken_validation_php\exceptions\CertificateNotYetValidException;
34+
35+
final class CertificateValidator
36+
{
37+
38+
public function __construct()
39+
{
40+
throw new BadFunctionCallException('Utility class');
41+
}
42+
43+
public static function certificateIsValidOnDate(X509 $subjectCertificate, DateTime $date, string $subject): void
44+
{
45+
try {
46+
$subjectCertificate->checkValidity($date);
47+
} catch (Exception $e) {
48+
switch($e->getCode()) {
49+
// Not valid yet
50+
case 1:
51+
throw new CertificateNotYetValidException($subject);
52+
break;
53+
// Certificate is expired
54+
case 2:
55+
throw new CertificateExpiredException($subject);
56+
break;
57+
default:
58+
throw new Exception($e->getMessage());
59+
}
60+
}
61+
}
62+
63+
public static function trustedCACertificatesAreValidOnDate(TrustedCertificates $trustedCertificates, DateTime $date): void
64+
{
65+
foreach($trustedCertificates->getCertificates() as $cert) {
66+
self::certificateIsValidOnDate($cert, $date, "Trusted CA");
67+
}
68+
}
69+
70+
public static function validateIsSignedByTrustedCA(X509 $certificate, TrustedCertificates $trustedCertificates)
71+
{
72+
foreach ($trustedCertificates->getCertificates() as $trustedCertificate) {
73+
//
74+
}
75+
}
76+
77+
public function buildTrustedCertificates(array $certificates): TrustedCertificates
78+
{
79+
return new TrustedCertificates($certificates);
80+
}
81+
82+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2020-2021 Estonian Information System Authority
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace web_eid\web_eid_authtoken_validation_php\exceptions;
26+
27+
class AuthTokenParseException extends AuthTokenException
28+
{
29+
public function __construct(string $message)
30+
{
31+
parent::__construct($message);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2020-2021 Estonian Information System Authority
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace web_eid\web_eid_authtoken_validation_php\exceptions;
26+
27+
class CertificateDecodingException extends AuthTokenException
28+
{
29+
public function __construct(string $resource)
30+
{
31+
parent::__construct('Certificate decoding from Base64 or parsing failed for '.$resource);
32+
}
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2020-2021 Estonian Information System Authority
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace web_eid\web_eid_authtoken_validation_php\exceptions;
26+
27+
/**
28+
* Thrown when the certificate's valid until date is in the past.
29+
*/
30+
class CertificateExpiredException extends AuthTokenException
31+
{
32+
public function __construct(string $subject)
33+
{
34+
parent::__construct($subject.' certificate has expired');
35+
}
36+
}

0 commit comments

Comments
 (0)