1 | <?php |
||
10 | class Signer |
||
11 | { |
||
12 | const XMLDSIGNS = 'http://www.w3.org/2000/09/xmldsig#'; |
||
13 | const C14N = 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315'; |
||
14 | const SHA1 = 'http://www.w3.org/2000/09/xmldsig#sha1'; |
||
15 | const SHA1_SIG = 'http://www.w3.org/2000/09/xmldsig#rsa-sha1'; |
||
16 | const SHA256 = 'http://www.w3.org/2001/04/xmlenc#sha256'; |
||
17 | const SHA256_SIG = 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'; |
||
18 | |||
19 | private $algorithm; |
||
20 | private $canonical; |
||
21 | private $certificate; |
||
22 | |||
23 | 3 | public function __construct(Certificate $certificate) |
|
24 | { |
||
25 | 3 | $this->certificate = $certificate; |
|
26 | 3 | } |
|
27 | |||
28 | 3 | public function xml( |
|
29 | $content, |
||
30 | $tagName, |
||
31 | $rootName = '', |
||
32 | $algorithm = OPENSSL_ALGO_SHA1, |
||
33 | $canonical = [true, false, null, null] |
||
34 | ) { |
||
35 | 3 | $this->assertValidContent($content); |
|
36 | 2 | $this->algorithm = $algorithm; |
|
37 | 2 | $this->canonical = $canonical; |
|
38 | |||
39 | 2 | $dom = new DOMDocument('1.0', 'UTF-8'); |
|
40 | 2 | $dom->loadXML($content); |
|
41 | 2 | $dom->preserveWhiteSpace = false; |
|
42 | 2 | $dom->formatOutput = false; |
|
43 | 2 | $root = $dom->documentElement; |
|
44 | 2 | if (!empty($rootName)) { |
|
45 | $root = $dom->getElementsByTagName($rootName)->item(0); |
||
46 | } |
||
47 | 2 | $node = $dom->getElementsByTagName($tagName)->item(0); |
|
48 | 2 | if (empty($node) || empty($root)) { |
|
49 | 1 | throw SignerException::tagNotFound($tagName); |
|
50 | } |
||
51 | 1 | $dom = $this->createSignature( |
|
52 | 1 | $dom, |
|
53 | 1 | $root, |
|
54 | 1 | $node |
|
55 | ); |
||
56 | 1 | return (string) '<?xml version="1.0" encoding="UTF-8"?>' |
|
57 | 1 | . $dom->saveXML($dom->documentElement, LIBXML_NOXMLDECL); |
|
58 | } |
||
59 | |||
60 | 3 | private function assertValidContent($content) |
|
66 | |||
67 | 1 | private function createSignature( |
|
68 | DOMDocument $dom, |
||
121 | |||
122 | 1 | private function algorithmData() |
|
143 | |||
144 | /** |
||
145 | * Calculate digest value for given node |
||
146 | * @param DOMNode $node |
||
147 | * @param string $algorithm |
||
148 | * @return string |
||
149 | */ |
||
150 | 1 | private function makeDigest(DOMNode $node, $algorithm) |
|
156 | |||
157 | /** |
||
158 | * Reduced to the canonical form |
||
159 | * @param DOMNode $node |
||
160 | * @return string |
||
161 | */ |
||
162 | 1 | private function canonize(DOMNode $node) |
|
171 | } |
||
172 |