| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Core\Util; |
| 6 |
|
| 7 |
use InvalidArgumentException; |
| 8 |
use function is_string; |
| 9 |
use const STR_PAD_LEFT; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
final class ECSignature |
| 15 |
{ |
| 16 |
private const ASN1_SEQUENCE = '30'; |
| 17 |
|
| 18 |
private const ASN1_INTEGER = '02'; |
| 19 |
|
| 20 |
private const ASN1_MAX_SINGLE_BYTE = 128; |
| 21 |
|
| 22 |
private const ASN1_LENGTH_2BYTES = '81'; |
| 23 |
|
| 24 |
private const ASN1_BIG_INTEGER_LIMIT = '7f'; |
| 25 |
|
| 26 |
private const ASN1_NEGATIVE_INTEGER = '00'; |
| 27 |
|
| 28 |
private const BYTE_SIZE = 2; |
| 29 |
|
| 30 |
public static function toAsn1(string $signature, int $length): string |
| 31 |
{ |
| 32 |
$signature = bin2hex($signature); |
| 33 |
|
| 34 |
if (self::octetLength($signature) !== $length) { |
| 35 |
throw new InvalidArgumentException('Invalid signature length.'); |
| 36 |
} |
| 37 |
|
| 38 |
$pointR = self::preparePositiveInteger(mb_substr($signature, 0, $length, '8bit')); |
| 39 |
$pointS = self::preparePositiveInteger(mb_substr($signature, $length, null, '8bit')); |
| 40 |
|
| 41 |
$lengthR = self::octetLength($pointR); |
| 42 |
$lengthS = self::octetLength($pointS); |
| 43 |
|
| 44 |
$totalLength = $lengthR + $lengthS + self::BYTE_SIZE + self::BYTE_SIZE; |
| 45 |
$lengthPrefix = $totalLength > self::ASN1_MAX_SINGLE_BYTE ? self::ASN1_LENGTH_2BYTES : ''; |
| 46 |
|
| 47 |
$bin = hex2bin( |
| 48 |
self::ASN1_SEQUENCE |
| 49 |
. $lengthPrefix . dechex($totalLength) |
| 50 |
. self::ASN1_INTEGER . dechex($lengthR) . $pointR |
| 51 |
. self::ASN1_INTEGER . dechex($lengthS) . $pointS |
| 52 |
); |
| 53 |
if (! is_string($bin)) { |
| 54 |
throw new InvalidArgumentException('Unable to parse the data'); |
| 55 |
} |
| 56 |
|
| 57 |
return $bin; |
| 58 |
} |
| 59 |
|
| 60 |
public static function fromAsn1(string $signature, int $length): string |
| 61 |
{ |
| 62 |
$message = bin2hex($signature); |
| 63 |
$position = 0; |
| 64 |
|
| 65 |
if (self::readAsn1Content($message, $position, self::BYTE_SIZE) !== self::ASN1_SEQUENCE) { |
| 66 |
throw new InvalidArgumentException('Invalid data. Should start with a sequence.'); |
| 67 |
} |
| 68 |
|
| 69 |
if (self::readAsn1Content($message, $position, self::BYTE_SIZE) === self::ASN1_LENGTH_2BYTES) { |
| 70 |
$position += self::BYTE_SIZE; |
| 71 |
} |
| 72 |
|
| 73 |
$pointR = self::retrievePositiveInteger(self::readAsn1Integer($message, $position)); |
| 74 |
$pointS = self::retrievePositiveInteger(self::readAsn1Integer($message, $position)); |
| 75 |
|
| 76 |
$bin = hex2bin( |
| 77 |
str_pad($pointR, $length, '0', STR_PAD_LEFT) . str_pad($pointS, $length, '0', STR_PAD_LEFT) |
| 78 |
); |
| 79 |
if (! is_string($bin)) { |
| 80 |
throw new InvalidArgumentException('Unable to parse the data'); |
| 81 |
} |
| 82 |
|
| 83 |
return $bin; |
| 84 |
} |
| 85 |
|
| 86 |
private static function octetLength(string $data): int |
| 87 |
{ |
| 88 |
return (int) (mb_strlen($data, '8bit') / self::BYTE_SIZE); |
| 89 |
} |
| 90 |
|
| 91 |
private static function preparePositiveInteger(string $data): string |
| 92 |
{ |
| 93 |
if (mb_substr($data, 0, self::BYTE_SIZE, '8bit') > self::ASN1_BIG_INTEGER_LIMIT) { |
| 94 |
return self::ASN1_NEGATIVE_INTEGER . $data; |
| 95 |
} |
| 96 |
|
| 97 |
while (mb_strpos($data, self::ASN1_NEGATIVE_INTEGER, 0, '8bit') === 0 |
| 98 |
&& mb_substr($data, 2, self::BYTE_SIZE, '8bit') <= self::ASN1_BIG_INTEGER_LIMIT) { |
| 99 |
$data = mb_substr($data, 2, null, '8bit'); |
| 100 |
} |
| 101 |
|
| 102 |
return $data; |
| 103 |
} |
| 104 |
|
| 105 |
private static function readAsn1Content(string $message, int &$position, int $length): string |
| 106 |
{ |
| 107 |
$content = mb_substr($message, $position, $length, '8bit'); |
| 108 |
$position += $length; |
| 109 |
|
| 110 |
return $content; |
| 111 |
} |
| 112 |
|
| 113 |
private static function readAsn1Integer(string $message, int &$position): string |
| 114 |
{ |
| 115 |
if (self::readAsn1Content($message, $position, self::BYTE_SIZE) !== self::ASN1_INTEGER) { |
| 116 |
throw new InvalidArgumentException('Invalid data. Should contain an integer.'); |
| 117 |
} |
| 118 |
|
| 119 |
$length = (int) hexdec(self::readAsn1Content($message, $position, self::BYTE_SIZE)); |
| 120 |
|
| 121 |
return self::readAsn1Content($message, $position, $length * self::BYTE_SIZE); |
| 122 |
} |
| 123 |
|
| 124 |
private static function retrievePositiveInteger(string $data): string |
| 125 |
{ |
| 126 |
while (mb_strpos($data, self::ASN1_NEGATIVE_INTEGER, 0, '8bit') === 0 |
| 127 |
&& mb_substr($data, 2, self::BYTE_SIZE, '8bit') > self::ASN1_BIG_INTEGER_LIMIT) { |
| 128 |
$data = mb_substr($data, 2, null, '8bit'); |
| 129 |
} |
| 130 |
|
| 131 |
return $data; |
| 132 |
} |
| 133 |
} |