1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\CryptoTypes\Signature; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
8
|
|
|
use Sop\ASN1\Type\Primitive\BitString; |
9
|
|
|
use Sop\ASN1\Type\Primitive\Integer; |
10
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Implements ECDSA signature value. |
14
|
|
|
* |
15
|
|
|
* ECDSA signature is represented as a `ECDSA-Sig-Value` ASN.1 type. |
16
|
|
|
* |
17
|
|
|
* @see https://tools.ietf.org/html/rfc3278#section-8.2 |
18
|
|
|
*/ |
19
|
|
|
class ECSignature extends Signature |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* r-value as a base 10 integer. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $_r; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* s-value as a base 10 integer. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $_s; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
* |
38
|
|
|
* @param int|string $r Signature's `r` value |
39
|
|
|
* @param int|string $s Signature's `s` value |
40
|
|
|
*/ |
41
|
3 |
|
public function __construct($r, $s) |
42
|
|
|
{ |
43
|
3 |
|
$this->_r = strval($r); |
44
|
3 |
|
$this->_s = strval($s); |
45
|
3 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initialize from ASN.1. |
49
|
|
|
* |
50
|
|
|
* @param Sequence $seq |
51
|
|
|
* |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
2 |
|
public static function fromASN1(Sequence $seq): self |
55
|
|
|
{ |
56
|
2 |
|
$r = $seq->at(0)->asInteger()->number(); |
57
|
2 |
|
$s = $seq->at(1)->asInteger()->number(); |
58
|
2 |
|
return new self($r, $s); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Initialize from DER. |
63
|
|
|
* |
64
|
|
|
* @param string $data |
65
|
|
|
* |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
2 |
|
public static function fromDER(string $data): self |
69
|
|
|
{ |
70
|
2 |
|
return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the r-value. |
75
|
|
|
* |
76
|
|
|
* @return string Base 10 integer string |
77
|
|
|
*/ |
78
|
1 |
|
public function r(): string |
79
|
|
|
{ |
80
|
1 |
|
return $this->_r; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the s-value. |
85
|
|
|
* |
86
|
|
|
* @return string Base 10 integer string |
87
|
|
|
*/ |
88
|
1 |
|
public function s(): string |
89
|
|
|
{ |
90
|
1 |
|
return $this->_s; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Generate ASN.1 structure. |
95
|
|
|
* |
96
|
|
|
* @return Sequence |
97
|
|
|
*/ |
98
|
3 |
|
public function toASN1(): Sequence |
99
|
|
|
{ |
100
|
3 |
|
return new Sequence(new Integer($this->_r), new Integer($this->_s)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get DER encoding of the signature. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
2 |
|
public function toDER(): string |
109
|
|
|
{ |
110
|
2 |
|
return $this->toASN1()->toDER(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
1 |
|
public function bitString(): BitString |
117
|
|
|
{ |
118
|
1 |
|
return new BitString($this->toDER()); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|