1<?php
2/*
3 * Copyright 2008-2010 GuardTime AS
4 *
5 * This file is part of the GuardTime PHP SDK.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/**
21 * @package asn1
22 * @subpackage gt
23 */
24
25/**
26 * GT TimeSignature implementation.
27 *
28 * <pre>
29 * TimeSignature ::= SEQUENCE {
30 *    location        OCTET STRING,
31 *    history         OCTET STRING,
32 *    publishedData   PublishedData,
33 *    pkSignature     [0] IMPLICIT SignatureInfo OPTIONAL,
34 *    pubReference    [1] IMPLICIT SET OF OCTET STRING OPTIONAL
35 * }
36 * </pre>
37 *
38 * @package asn1
39 * @subpackage gt
40 */
41class GTTimeSignature implements ASN1DEREncodable {
42
43    /**
44     * Object identifier of the GuardTime TimeSignature algorithm.
45     */
46    const OID = "1.3.6.1.4.1.27868.4.1";
47
48    private $location;
49    private $history;
50    private $publishedData;
51    private $pkSignature;
52    private $pubReference;
53
54    /**
55     * Constructs a new instance of GTTimeSignature.
56     */
57    public function __construct() {
58    }
59
60    /**
61     * Decodes the given ASN1Sequence as GTTimeSignature.
62     *
63     * @throws GTException
64     * @param  ASN1Sequence $object GTTImeSignature encoded as ASN1Sequence
65     * @return void
66     */
67    public function decode($object) {
68
69        if (is_array($object)) {
70            $object = ASN1DER::decode($object);
71        }
72
73        if (!$object instanceof ASN1Sequence) {
74            throw new GTException("Expecting an ASN1Sequence");
75        }
76
77        $size = $object->getObjectCount();
78
79        if ($size < 3 || $size > 5) {
80            throw new GTException("Invalid sequence size: {$size}");
81        }
82
83        $location = $object->getObjectAt(0);
84        $history = $object->getObjectAt(1);
85
86        if (!$location instanceof ASN1OctetString) {
87            throw new GTException("Expecting an ASN1OctetString for GTTimeSignature.location");
88        }
89
90        if (!$history instanceof ASN1OctetString) {
91            throw new GTException("Expecting an ASN1OctetString for GTTimeSignature.history");
92        }
93
94        $this->location = $location->getValue();
95        $this->history = $history->getValue();
96
97        $publishedData = new GTPublishedData();
98        $publishedData->decode($object->getObjectAt(2));
99
100        $this->publishedData = $publishedData;
101
102        for ($i = 3; $i < $object->getObjectCount(); $i++) {
103
104            $tag = $object->getObjectAt($i);
105
106            if (!$tag instanceof ASN1Tag) {
107                throw new GTException("Expecting an ASN1Tag");
108            }
109
110            switch ($tag->getTagValue()) {
111
112                case 0:
113                    //pkSignature
114                    $sequence = $tag->getObjectAs(ASN1_TAG_SEQUENCE);
115
116                    $pkSignature = new GTSignatureInfo();
117                    $pkSignature->decode($sequence);
118
119                    $this->pkSignature = $pkSignature;
120
121                    break;
122
123                case 1:
124                    // pubReference
125                    $set = $tag->getObjectAs(ASN1_TAG_SET);
126
127                    $pubReference = array();
128
129                    foreach ($set->getObjects() as $item) {
130                        array_push($pubReference, $item->getValue());
131                    }
132
133                    $this->pubReference = $pubReference;
134
135                    break;
136
137                default:
138                    throw new GTException("Unsupported ASN1Tag: " . $tag->getTagValue());
139
140            }
141
142        }
143    }
144
145    /**
146     * Encodes this GTTimeSignature using DER.
147     *
148     * @return array byte array containing the DER encoding of this GTTimeSignature
149     *
150     */
151    public function encodeDER() {
152
153        $sequence = new ASN1Sequence();
154
155        $sequence->add(new ASN1OctetString($this->location));
156        $sequence->add(new ASN1OctetString($this->history));
157        $sequence->add($this->publishedData);
158
159        if ($this->pkSignature !== null) {
160
161            $tag = new ASN1Tag();
162            $tag->setTagValue(0);
163            $tag->setExplicit(false);
164            $tag->setTagClass(ASN1_TAG_CONTEXT);
165            $tag->setTagType(ASN1_TAG_CONSTRUCTED);
166            $tag->setObject($this->pkSignature);
167
168            $sequence->add($tag);
169
170        }
171
172        if ($this->pubReference !== null) {
173
174            $set = new ASN1Set();
175
176            foreach ($this->pubReference as $item) {
177                $set->add(new ASN1OctetString($item));
178            }
179
180            $tag = new ASN1Tag();
181            $tag->setTagValue(1);
182            $tag->setExplicit(false);
183            $tag->setTagClass(ASN1_TAG_CONTEXT);
184            $tag->setTagType(ASN1_TAG_CONSTRUCTED);
185            $tag->setObject($set);
186
187            $sequence->add($tag);
188        }
189
190        return $sequence->encodeDER();
191
192    }
193
194    /**
195     * Checks if this time signature is extended.
196     *
197     * @return bool true if this time signature is extended
198     */
199    public function isExtended() {
200        return $this->pkSignature == null;
201    }
202
203    /**
204     * Gets the encoded publication references.
205     *
206     * @return string encoded publication references
207     */
208    public function getEncodedPublicationReferences() {
209
210        $string = "";
211        $string .= "[";
212
213        if (!empty($this->pubReference)) {
214            foreach ($this->pubReference as $reference) {
215                $string .= GTBase16::encodeWithSpaces($reference);
216                $string .= ',';
217            }
218        }
219
220        $string .= "]";
221
222        return $string;
223
224    }
225
226    /**
227     * Gets the location bytes.
228     *
229     * @return array byte array containing the location bytes
230     */
231    public function getLocation() {
232        return $this->location;
233    }
234
235    /**
236     * Sets the location bytes.
237     *
238     * @param  array $location byte array containing the location bytes
239     * @return void
240     */
241    public function setLocation($location) {
242        $this->location = $location;
243    }
244
245    /**
246     * Gets the history bytes.
247     *
248     * @return array byte array containing the history bytes
249     */
250    public function getHistory() {
251        return $this->history;
252    }
253
254    /**
255     * Sets the history bytes.
256     *
257     * @param  array history byte array containing the history bytes
258     * @return void
259     */
260    public function setHistory($history) {
261        $this->history = $history;
262    }
263
264    /**
265     * Gets the published data.
266     *
267     * @return GTPublishedData published data
268     */
269    public function getPublishedData() {
270        return $this->publishedData;
271    }
272
273    /**
274     * Sets the published data.
275     *
276     * @param  GTPublishedData $publishedData published data
277     * @return void
278     */
279    public function setPublishedData($publishedData) {
280        $this->publishedData = $publishedData;
281    }
282
283    /**
284     * Gets the public key signature.
285     *
286     * @return GTSignatureInfo public key signature
287     */
288    public function getPkSignature() {
289        return $this->pkSignature;
290    }
291
292    /**
293     * Sets the public key signature.
294     *
295     * @param  GTSignatureInfo $pkSignature public key signature
296     * @return void
297     */
298    public function setPkSignature($pkSignature) {
299        $this->pkSignature = $pkSignature;
300    }
301
302    /**
303     * Gets the pub reference.
304     *
305     * @return array pub reference
306     */
307    public function getPubReference() {
308        return $this->pubReference;
309    }
310
311    /**
312     * Sets the pub reference.
313     *
314     * @param  array $pubReference pub reference
315     * @return void
316     */
317    public function setPubReference($pubReference) {
318        $this->pubReference = $pubReference;
319    }
320
321}
322
323?>
324