Skip to content

Commit

Permalink
Added uzi server cert functionality (#23)
Browse files Browse the repository at this point in the history
* Added validator callback

* fix phpci

* Added serial number for server certs

* fixed line length
  • Loading branch information
jaytaph authored Feb 20, 2023
1 parent fea60bb commit 11042aa
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 21 deletions.
36 changes: 16 additions & 20 deletions src/UziReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ public function getDataFromRequest(Request $request): ?UziUser
return null;
}

$uziInfo = new UziUser();

// Check if the certificate is a UZI certificate
$surName = null;
$givenName = null;
foreach ($cert['tbsCertificate']['subject']['rdnSequence'] as $sequence) {
$data = reset($sequence);
if ($data['type'] === 'id-at-surname') {
$surName = $data['value']['utf8String'];
$uziInfo->setSurName($data['value']['utf8String']);
}
if ($data['type'] === 'id-at-givenName') {
$givenName = $data['value']['utf8String'];
$uziInfo->setGivenName($data['value']['utf8String']);
}
if ($givenName && $surName) {
break;
if ($data['type'] === 'id-at-serialNumber') {
$uziInfo->setSerialNumber($data['value']['printableString']);
}
}

Expand All @@ -53,7 +53,7 @@ public function getDataFromRequest(Request $request): ?UziUser

foreach ($extension['extnValue'] as $value) {
if (!isset($value['otherName']) || $value['otherName']['type-id'] !== UziConstants::OID_IA5STRING) {
continue;
return null;
}

if (!isset($value['otherName']['value']['ia5String'])) {
Expand All @@ -78,20 +78,16 @@ public function getDataFromRequest(Request $request): ?UziUser
if (!is_array($data) || count($data) < 6) {
return null;
}

$user = new UziUser();
$user->setGivenName($givenName ?? "");
$user->setSurName($surName ?? "");
$user->setOidCa($data[0]);
$user->setUziVersion($data[1]);
$user->setUziNumber($data[2]);
$user->setCardType($data[3]);
$user->setSubscriberNumber($data[4]);
$user->setRole($data[5]);
$user->setAgbCode($data[6]);

return $user;
$uziInfo->setOidCa($data[0]);
$uziInfo->setUziVersion($data[1]);
$uziInfo->setUziNumber($data[2]);
$uziInfo->setCardType($data[3]);
$uziInfo->setSubscriberNumber($data[4]);
$uziInfo->setRole($data[5]);
$uziInfo->setAgbCode($data[6]);
}

return $uziInfo;
}

return null;
Expand Down
20 changes: 19 additions & 1 deletion src/UziUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class UziUser implements \JsonSerializable
protected string $sur_name = "";
protected string $uzi_number = "";
protected string $uzi_version = "";
protected string $serial_number = "";

/**
* @return string
Expand Down Expand Up @@ -163,6 +164,22 @@ public function setUziVersion(string $uzi_version): void
$this->uzi_version = $uzi_version;
}

/**
* @return string
*/
public function getSerialNumber(): string
{
return $this->serial_number;
}

/**
* @param string $serial_number
*/
public function setSerialNumber(string $serial_number): void
{
$this->serial_number = $serial_number;
}

#[\ReturnTypeWillChange]
public function jsonSerialize()
{
Expand All @@ -183,7 +200,8 @@ public function toArray(): array
'subscriber_number' => $this->getSubscriberNumber(),
'sur_name' => $this->getSurName(),
'uzi_number' => $this->getUziNumber(),
'uzi_version' => $this->getUziVersion()
'uzi_version' => $this->getUziVersion(),
'serial_number' => $this->getSerialNumber(),
];
}
}
28 changes: 28 additions & 0 deletions tests/UziReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function testCheckValidCert(): void
$this->assertEquals('doe-12345678', $uziInfo->getSurName());
$this->assertEquals('12345678', $uziInfo->getUziNumber());
$this->assertEquals('1', $uziInfo->getUziVersion());
$this->assertEquals('', $uziInfo->getSerialNumber());
}

public function testCheckValidAdminCert(): void
Expand All @@ -155,5 +156,32 @@ public function testCheckValidAdminCert(): void
$this->assertEquals('doe-11111111', $uziInfo->getSurName());
$this->assertEquals('11111111', $uziInfo->getUziNumber());
$this->assertEquals('1', $uziInfo->getUziVersion());
$this->assertEquals('', $uziInfo->getSerialNumber());
}

public function testCheckValidServerCert(): void
{
$uzi = new UziReader();

$request = new Request();
$request->server->set('SSL_CLIENT_VERIFY', "SUCCESS");
$request->server->set(
'SSL_CLIENT_CERT',
file_get_contents(__DIR__ . '/certs/mock-022-correct-server-cert.cert')
);

/** @var UziUser $uziInfo */
$uziInfo = $uzi->getDataFromRequest($request);

$this->assertEquals('00000000', $uziInfo->getAgbCode());
$this->assertEquals('S', $uziInfo->getCardType());
$this->assertEquals('', $uziInfo->getGivenName());
$this->assertEquals('2.16.528.1.1003.1.3.5.5.2', $uziInfo->getOidCa());
$this->assertEquals('00.000', $uziInfo->getRole());
$this->assertEquals('90000123', $uziInfo->getSubscriberNumber());
$this->assertEquals('', $uziInfo->getSurName());
$this->assertEquals('12345678', $uziInfo->getUziNumber());
$this->assertEquals('1', $uziInfo->getUziVersion());
$this->assertEquals('1234ABCD', $uziInfo->getSerialNumber());
}
}
8 changes: 8 additions & 0 deletions tests/certs/generate-mock-certs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,11 @@ openssl req -x509 \
-days 3650 \
-subj "/C=NL/O=MockTest Cert/title=physician/SN=doe-11111111/GN=john/CN=john doe-11111111" \
-addext "subjectAltName = otherName:2.5.5.5;IA5STRING:2.16.528.1.1003.1.3.5.5.2-1252-11111111-N-90000111-01.015-00000000"

openssl req -x509 \
-nodes \
-keyout dummy.key \
-out mock-022-correct-server-cert.cert \
-days 3650 \
-subj "/C=NL/O=MockTest Cert/CN=test.example.org/serialNumber=1234ABCD" \
-addext "subjectAltName = otherName:2.5.5.5;IA5STRING:2.16.528.1.1003.1.3.5.5.2-1-12345678-S-90000123-00.000-00000000"
23 changes: 23 additions & 0 deletions tests/certs/mock-022-correct-server-cert.cert
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-----BEGIN CERTIFICATE-----
MIID3jCCAsagAwIBAgIUMsVos4XdwMecyyLBt9+T4HyZNe4wDQYJKoZIhvcNAQEL
BQAwUzELMAkGA1UEBhMCTkwxFjAUBgNVBAoMDU1vY2tUZXN0IENlcnQxGTAXBgNV
BAMMEHRlc3QuZXhhbXBsZS5vcmcxETAPBgNVBAUTCDEyMzRBQkNEMB4XDTIzMDIy
MDExMTQzMFoXDTMzMDIxNzExMTQzMFowUzELMAkGA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