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 tsp
23 */
24
25/**
26 * TSP TimeStampResp implementation.
27 *
28 * <pre>
29 * TimeStampResp ::= SEQUENCE  {
30 *      status                  PKIStatusInfo,
31 *      timeStampToken          TimeStampToken     OPTIONAL
32 * }
33 * </pre>
34 *
35 * @package asn1
36 * @subpackage tsp
37 *
38 * @link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161: Time-Stamp Protocol
39 */
40class TSPTimeStampResp {
41
42    private $status;
43    private $token;
44
45    /**
46     * Constructs a new instance of TSPTimeStampResp.
47     */
48    public function __construct() {
49    }
50
51    /**
52     * Decodes the given ASN1Sequence as TSPTimeStampResp.
53     *
54     * @throws GTException
55     * @param  ASN1Sequence $object TSPTimeStampResp encoded as ASN1Sequence
56     * @return void
57     */
58    public function decode($object) {
59
60        if (!$object instanceof ASN1Sequence) {
61            throw new GTException("object must be an instance of ASN1Sequence");
62        }
63
64        if ($object->getObjectCount() < 1 || $object->getObjectCount() > 2) {
65            throw new GTException("invalid sequence size");
66        }
67
68        $status = new PKIStatusInfo();
69        $status->decode($object->getObjectAt(0));
70
71        $this->status = $status;
72
73        $code = $this->getStatusCode();
74
75        if ($code == 0 || $code == 1) {
76
77            if ($object->getObjectCount() != 2) {
78                throw new GTException("timestamp token is missing");
79            }
80
81            $token = new CMSContentInfo();
82            $token->decode($object->getObjectAt(1));
83
84            $this->token = $token;
85
86        } else if ($code >= 2 && $code <= 5) {
87
88            if ($object->getObjectCount() == 2) {
89                throw new GTException("unexpected timestamp token");
90            }
91
92        } else {
93
94            throw new GTException("invalid status code: {$code}");
95        }
96    }
97
98    /**
99     * Gets the status.
100     *
101     * @return PKIStatusInfo status
102     */
103    public function getStatus() {
104        return $this->status;
105    }
106
107    /**
108     * Gets the status code.
109     *
110     * @return int status code
111     */
112    public function getStatusCode() {
113        return $this->status->getStatus();
114    }
115
116    /**
117     * Gets the timestamp token.
118     *
119     * @return CMSContentInfo timestamp token
120     */
121    public function getToken() {
122        return $this->token;
123    }
124
125}
126
127?>
128