Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
refactor: add AsnUtil::decodeNonceExtension()
Browse files Browse the repository at this point in the history
WE2-879

Signed-off-by: Mart Somermaa <[email protected]>
  • Loading branch information
mrts committed Mar 12, 2024
1 parent 7d60575 commit 371943e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.phpunit.result.cache
.phpunit.cache/
.vscode/
vendor
build
.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"prettier src/**/* --write",
"prettier src/* --write"
],
"test": "phpunit --no-coverage",
"test": "phpunit --no-coverage --display-warnings",
"test-coverage": [
"@putenv XDEBUG_MODE=coverage",
"phpunit --coverage-html coverage"
Expand Down
19 changes: 1 addition & 18 deletions src/OcspBasicResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,7 @@ public function getSignatureAlgorithm(): string

public function getNonceExtension(): ?string
{
$filter = array_filter(
$this->ocspBasicResponse["tbsResponseData"]["responseExtensions"],
function ($extension) {
return AsnUtil::ID_PKIX_OCSP_NONCE ==
ASN1::getOID($extension["extnId"]);
}
);

if (isset($filter[0]["extnValue"])) {
$decoded = ASN1::decodeBER($filter[0]["extnValue"]);
if(is_array($decoded)) {
return ASN1::asn1map($decoded[0], ['type' => ASN1::TYPE_OCTET_STRING]);
} else {
return $filter[0]["extnValue"];
}
}

return null;
return AsnUtil::decodeNonceExtension($this->ocspBasicResponse["tbsResponseData"]["responseExtensions"]);
}

public function getCertID(): array
Expand Down
12 changes: 2 additions & 10 deletions src/OcspRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,8 @@ public function addNonceExtension(string $nonce): void
*/
public function getNonceExtension(): string
{
$nonce = current(
array_filter(
$this->ocspRequest["tbsRequest"]["requestExtensions"],
function ($extension) {
return AsnUtil::ID_PKIX_OCSP_NONCE == $extension["extnId"];
}
)
)["extnValue"];
$decoded = ASN1::decodeBER($nonce);
return ASN1::asn1map($decoded[0], ['type' => ASN1::TYPE_OCTET_STRING]);
// TODO: the ?? '' is here only for v1.0 API compatibility. Remove this in version 1.2 and change the return type to ?string.
return AsnUtil::decodeNonceExtension($this->ocspRequest["tbsRequest"]["requestExtensions"]) ?? '';
}

public function getEncodeDer(): string
Expand Down
28 changes: 28 additions & 0 deletions src/util/AsnUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,32 @@ public static function extractKeyData(string $publicKey): string
// Remove first byte
return pack("c*", ...array_slice(unpack("c*", $subjectPublicKey), 1));
}

public static function decodeNonceExtension(array $ocspExtensions): ?string
{
$nonceExtension = current(
array_filter(
$ocspExtensions,
function ($extension) {
return self::ID_PKIX_OCSP_NONCE == ASN1::getOID($extension["extnId"]);
}
)
);
if (!$nonceExtension || !isset($nonceExtension["extnValue"])) {
return null;
}

$nonceValue = $nonceExtension["extnValue"];

$decoded = ASN1::decodeBER($nonceValue);
if (is_array($decoded)) {
// The value was DER-encoded, it is required to be an octet string.
$nonceString = ASN1::asn1map($decoded[0], ['type' => ASN1::TYPE_OCTET_STRING]);
return is_string($nonceString) ? $nonceString : null;
}

// The value was not DER-encoded, return it as-is.
return $nonceValue;
}

}

0 comments on commit 371943e

Please sign in to comment.