-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCertificateAttributeUtil.php
54 lines (42 loc) · 1.59 KB
/
CertificateAttributeUtil.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Sk\SmartId\Util;
use DateTimeImmutable;
use phpseclib3\File\X509;
class CertificateAttributeUtil
{
/**
* Get Date-of-birth (DoB) from a specific certificate header (if present).
*
* NB! This attribute may be present on some newer certificates (since ~ May 2021) but not all.
*
* @see NationalIdentityNumberUtil#getDateOfBirth(AuthenticationIdentity) for fallback.
*
* @param string $x509Certificate Certificate to read the date-of-birth attribute from
* @return DateTimeImmutable date-of-birth or null if this attribute is not set.
*/
public function getDateOfBirthCertificateAttribute(string $x509Certificate): ?DateTimeImmutable
{
$dobAsString = $this->getDateOfBirthFromCertificateField($x509Certificate);
if ($dobAsString == null) {
return null;
}
$timestamp = strtotime($dobAsString);
$dti = new DateTimeImmutable();
return $dti->setTimestamp($timestamp);
}
public function getDateOfBirthFromCertificateField(string $certAsString)
{
$x509 = new X509();
$csr = $x509->loadX509($certAsString);
$arrayIterator = new \RecursiveArrayIterator($csr);
$recursiveIterator = new \RecursiveIteratorIterator($arrayIterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($recursiveIterator as $key => $value) {
if (is_array($value) && array_key_exists('type', $value) && $value['type'] == '1.3.6.1.5.5.7.9.1') {
if (is_array($value['value'][0]) && array_key_exists('generalTime', $value['value'][0])) {
return $value['value'][0]['generalTime'];
}
}
}
return null;
}
}