Skip to content

Commit ff286bd

Browse files
committed
chore: improve the return from TSA parser
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent f438326 commit ff286bd

1 file changed

Lines changed: 52 additions & 10 deletions

File tree

lib/Handler/SignEngine/TSA.php

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ public function extract(array $root): array {
173173

174174
$tst = null;
175175
if ($tstNode && ($tstNode['type'] ?? null) === ASN1::TYPE_SEQUENCE) {
176-
// Use phpseclib3 optimized time format handling
177176
ASN1::setTimeFormat('Y-m-d\TH:i:s\Z');
178177
$tst = ASN1::asn1map($tstNode, self::$timestampInfoStructure);
179178

@@ -187,13 +186,14 @@ public function extract(array $root): array {
187186

188187
if (is_array($tst)) {
189188
$tsa['genTime'] = $tst['genTime'] ?? null;
190-
$tsa['policy'] = $tst['policy'] ?? null;
189+
$policyOid = $tst['policy'] ?? null;
190+
$tsa['policy'] = $policyOid;
191+
$tsa['policyName'] = $this->resolveTsaPolicyName($policyOid);
191192
$tsa['serialNumber'] = $this->bigToString($tst['serialNumber'] ?? null);
192193

193194
if (!empty($tst['messageImprint'])) {
194195
$algOid = $tst['messageImprint']['hashAlgorithm']['algorithm'] ?? null;
195196
$tsa['hashAlgorithmOID'] = $algOid;
196-
// Use phpseclib3 OID resolution with fallback
197197
$tsa['hashAlgorithm'] = $this->resolveHashAlgorithm($algOid);
198198

199199
$hashed = $tst['messageImprint']['hashedMessage'] ?? null;
@@ -434,13 +434,55 @@ private function bigToString($v): ?string {
434434
}
435435

436436
private function resolveHashAlgorithm(?string $oid): ?string {
437-
return $oid ? (ASN1::getOID($oid) ?? [
438-
'1.3.14.3.2.26' => 'sha1',
439-
'2.16.840.1.101.3.4.2.1' => 'sha256',
440-
'2.16.840.1.101.3.4.2.2' => 'sha384',
441-
'2.16.840.1.101.3.4.2.3' => 'sha512',
442-
'1.2.840.113549.2.5' => 'md5',
443-
][$oid] ?? $oid) : null;
437+
if (!$oid) {
438+
return null;
439+
}
440+
441+
$resolved = ASN1::getOID($oid);
442+
if ($resolved && $resolved !== $oid) {
443+
return match (strtolower($resolved)) {
444+
'sha1withrsaencryption', 'ecdsa-with-sha1', 'id-dsa-with-sha1' => 'SHA-1',
445+
'sha224withrsaencryption', 'ecdsa-with-sha224', 'id-dsa-with-sha224' => 'SHA-224',
446+
'sha256withrsaencryption', 'ecdsa-with-sha256', 'id-dsa-with-sha256' => 'SHA-256',
447+
'sha384withrsaencryption', 'ecdsa-with-sha384' => 'SHA-384',
448+
'sha512withrsaencryption', 'ecdsa-with-sha512' => 'SHA-512',
449+
'md2withrsaencryption' => 'MD2',
450+
'md5withrsaencryption' => 'MD5',
451+
default => strtoupper($resolved),
452+
};
453+
}
454+
455+
return match ($oid) {
456+
'1.3.14.3.2.26' => 'SHA-1',
457+
'2.16.840.1.101.3.4.2.4' => 'SHA-224',
458+
'2.16.840.1.101.3.4.2.1' => 'SHA-256',
459+
'2.16.840.1.101.3.4.2.2' => 'SHA-384',
460+
'2.16.840.1.101.3.4.2.3' => 'SHA-512',
461+
'1.2.840.113549.2.5' => 'MD5',
462+
'1.2.840.113549.2.2' => 'MD2',
463+
default => $oid,
464+
};
465+
}
466+
467+
private function resolveTsaPolicyName(?string $policyOid): ?string {
468+
if (!$policyOid) {
469+
return null;
470+
}
471+
472+
$resolved = ASN1::getOID($policyOid);
473+
if ($resolved && $resolved !== $policyOid) {
474+
return $resolved;
475+
}
476+
477+
return match ($policyOid) {
478+
'1.2.3.4.1' => 'FreeTSA Policy',
479+
'1.3.6.1.4.1.601.10.3.1' => 'VeriSign TSA Policy',
480+
'1.3.6.1.4.1.311.3.2.1' => 'Microsoft TSA Policy',
481+
'2.16.840.1.114412.7.1' => 'DigiCert TSA Policy',
482+
'1.3.6.1.4.1.8302.3.1' => 'Comodo TSA Policy',
483+
'2.16.840.1.113733.1.7.23.3' => 'Symantec TSA Policy',
484+
default => null,
485+
};
444486
}
445487

446488
private function decodeWithCache(string $asn1Data): array {

0 commit comments

Comments
 (0)