1: <?php
2: /**
3: * SealVerification.php
4: *
5: * Copyright © 2017-2026 - Thierry Thiers <webcoder31@gmail.com>
6: *
7: * This software is governed by the CeCILL-C license under French law and
8: * abiding by the rules of distribution of free software. You can use, modify
9: * and/or redistribute the software under the terms of the CeCILL-C license as
10: * circulated by CEA, CNRS and INRIA at the following URL:
11: *
12: * http://www.cecill.info
13: *
14: * As a counterpart to the access to the source code and rights to copy, modify
15: * and redistribute granted by the license, users are provided only with a
16: * limited warranty and the software's author, the holder of the economic
17: * rights, and the successive licensors have only limited liability.
18: *
19: * In this respect, the user's attention is drawn to the risks associated with
20: * loading, using, modifying and/or developing or reproducing the software by
21: * the user in light of its specific status of free software, that may mean that
22: * it is complicated to manipulate, and that also therefore means that it is
23: * reserved for developers and experienced professionals having in-depth
24: * computer knowledge. Users are therefore encouraged to load and test the
25: * software's suitability as regards their requirements in conditions enabling
26: * the security of their systems and/or data to be ensured and, more generally,
27: * to use and operate it in the same conditions as regards security.
28: *
29: * The fact that you are presently reading this means that you have had
30: * knowledge of the CeCILL-C license and that you accept its terms.
31: *
32: * @author Thierry Thiers <webcoder31@gmail.com>
33: * @copyright 2017-2026 - Thierry Thiers <webcoder31@gmail.com>
34: * @license http://www.cecill.info CeCILL-C License
35: * @version 2.1.0
36: */
37:
38:
39: // Namespace.
40: namespace webcoder31\ezxmldsig\seal;
41:
42: // Use.
43: use DateTimeImmutable;
44: use DateTimeZone;
45: use webcoder31\ezxmldsig\core\X509Cert;
46:
47: /**
48: * The result of the verification of a seal (see Seal::verifySeal()).
49: *
50: * The seals are an experimental feature of the 2.x versions: their API may
51: * change in a minor version.
52: */
53: class SealVerification
54: {
55: /** @var string|null */
56: private $error;
57:
58: /** @var array */
59: private $files;
60:
61: /** @var int|float|null */
62: private $signingTime;
63:
64: /** @var X509Cert|null */
65: private $signer;
66:
67: /** @var boolean */
68: private $revocationChecked;
69:
70: /** @var int|float|null */
71: private $timestampTime;
72:
73: /** @var string|null */
74: private $level;
75:
76:
77: /**
78: * @param array $result 'error', and, for a valid seal, 'files',
79: * 'signingTime', 'signer', 'revocationChecked', 'timestampTime',
80: * 'level'.
81: *
82: * @internal
83: */
84: public function __construct(array $result)
85: {
86: $result += ['files' => [], 'signingTime' => null, 'signer' => null, 'revocationChecked' => false, 'timestampTime' => null, 'level' => null];
87: $valid = null === $result['error'];
88: $this->error = $result['error'];
89: $this->level = $valid ? $result['level'] : null;
90: $this->timestampTime = $valid ? $result['timestampTime'] : null;
91: $this->files = $valid ? $result['files'] : [];
92: $this->signingTime = $result['signingTime'];
93: $this->signer = $result['signer'];
94: $this->revocationChecked = $result['revocationChecked'];
95: }
96:
97:
98: /**
99: * Indicates whether the seal is valid: structure, algorithms, digests,
100: * signature, trust, revocation.
101: *
102: * @return boolean
103: */
104: public function isValid()
105: {
106: return null === $this->error;
107: }
108:
109:
110: /**
111: * Why the seal is not valid, or NULL.
112: *
113: * @return string|null
114: */
115: public function getError()
116: {
117: return $this->error;
118: }
119:
120:
121: /**
122: * The level of the seal: "B"; "T" if it has a signature time-stamp;
123: * "LT" if it also embeds the data that validate it (checked to suffice,
124: * unless verified with 'revocation' => FALSE); NULL if it is not valid.
125: *
126: * @return string|null
127: */
128: public function getLevel()
129: {
130: return $this->level;
131: }
132:
133:
134: /**
135: * The time the signer claims (SigningTime): not a proven one.
136: *
137: * @return DateTimeImmutable|null The time, in UTC.
138: */
139: public function getSigningTime()
140: {
141: return self::date($this->signingTime);
142: }
143:
144:
145: /**
146: * The proven time of the seal: the time of its signature time-stamp
147: * (level T), or NULL.
148: *
149: * @return DateTimeImmutable|null The time, in UTC.
150: */
151: public function getTimestampTime()
152: {
153: return self::date($this->timestampTime);
154: }
155:
156:
157: /**
158: * The files the seal covers, in the order of its references; empty if
159: * it is not valid.
160: *
161: * @return array Each file: 'name' (its path in the container) and
162: * 'mimeType' (the media type the signature gives it).
163: */
164: public function getFiles()
165: {
166: return $this->files;
167: }
168:
169:
170: /**
171: * The signing certificate, once it has been found.
172: *
173: * @return X509Cert|null
174: */
175: public function getSigningCertificate()
176: {
177: return $this->signer;
178: }
179:
180:
181: /**
182: * Indicates whether the revocation of the certificates was checked.
183: *
184: * @return boolean
185: */
186: public function isRevocationChecked()
187: {
188: return $this->revocationChecked;
189: }
190:
191:
192: private static function date($time)
193: {
194: if (null === $time)
195: {
196: return null;
197: }
198: $date = DateTimeImmutable::createFromFormat('U.u', sprintf('%d.%06d', (int) floor($time), (int) round(($time - floor($time)) * 1000) * 1000), new DateTimeZone('UTC'));
199:
200: return $date->setTimezone(new DateTimeZone('UTC'));
201: }
202: }
203: