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 CertTokenResponse implementation.
27 *
28 * <pre>
29 * CertTokenResponse ::= SEQUENCE {
30 *    status      PKIStatusInfo,
31 *    certToken   [0] CertToken OPTIONAL
32 * }
33 * </pre>
34 *
35 * @package asn1
36 * @subpackage gt
37 */
38class GTCertTokenResponse {
39
40    private $status;
41    private $token;
42
43    /**
44     * Constructs a new instance of GTCertTokenResponse.
45     */
46    public function __construct() {
47    }
48
49    /**
50     * Decodes the given ASN1Sequence as GTCertTokenResponse.
51     *
52     * @throws GTException
53     * @param  ASN1Sequence $object GTCertTokenResponse encoded as ASN1Sequence
54     * @return void
55     */
56    public function decode($object) {
57
58        if (!$object instanceof ASN1Sequence) {
59            throw new GTException("object must be an instance of ASN1Sequence");
60        }
61
62        if ($object->getObjectCount() < 1 || $object->getObjectCount() > 2) {
63            throw new GTException("invalid sequence size");
64        }
65
66        $status = new PKIStatusInfo();
67        $status->decode($object->getObjectAt(0));
68
69        $this->status = $status;
70
71        $code = $this->getStatusCode();
72
73        if ($code == 0 || $code == 1) {
74
75            if ($object->getObjectCount() != 2) {
76                throw new GTException("token is missing");
77            }
78
79            $tag = $object->getObjectAt(1);
80
81            if ($tag->getTagValue() != 0) {
82                throw new GTException("Unexpected tag value: " . $tag->getTagValue());
83            }
84
85            $token = new GTCertToken();
86            $token->decode($tag->getObjectAs(ASN1_TAG_SEQUENCE));
87
88            $this->token = $token;
89
90        } else if ($code >= 2 && $code <= 5) {
91
92            if ($object->getObjectCount() == 2) {
93                throw new GTException("unexpected timestamp token");
94            }
95
96        } else {
97
98            throw new GTException("invalid status code: {$code}");
99        }
100
101    }
102
103    /**
104     * Gets the statsu.
105     *
106     * @return PKIStatusInfo status
107     */
108    public function getStatus() {
109        return $this->status;
110    }
111
112    /**
113     * Gets the status code.
114     *
115     * @return int status code
116     */
117    public function getStatusCode() {
118        return $this->status->getStatus();
119    }
120
121    /**
122     * Gets the failure code.
123     *
124     * @throws GTException
125     * @return string failure code
126     */
127    public function getFailCode() {
128
129        $info = $this->status->getFailInfo();
130
131        if (empty($info)) {
132            return -1;
133        }
134
135        $code = strpos($this->status->getFailInfo(), '1');
136
137        if ($code === false) {
138            return -1;
139        }
140
141        return $code;
142    }
143
144    /**
145     * Gets the cert token.
146     *
147     * @return GTCertToken token
148     */
149    public function getToken() {
150        return $this->token;
151    }
152
153}
154
155?>
156