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 x509
23 */
24
25/**
26 * X.509 Extension implementation.
27 *
28 * <pre>
29 * Extension ::= SEQUENCE {
30 *   extnid                  OBJECT IDENTIFIER,
31 *   critical                BOOLEAN DEFAULT FALSE,
32 *   extnValue               OCTETSTRING
33 * }
34 * </pre>
35 *
36 * @package asn1
37 * @subpackage x509
38 */
39class X509Extension implements ASN1DEREncodable {
40
41    private $id;
42    private $critical;
43    private $value;
44
45    /**
46     * Constructs a new instance of X509Extension.
47     */
48    public function __construct() {
49    }
50
51    /**
52     * Decodes the given ASN1Sequence as X509Extension.
53     *
54     * @throws GTException
55     * @param  ASN1Sequence $object X509Extension encoded as ASN1Sequence
56     * @return void
57     */
58    public function decode($object) {
59
60        if (!$object instanceof ASN1Sequence) {
61            throw new GTException("Expecting an ASN1Sequence");
62        }
63
64        $size = $object->getObjectCount();
65
66        if ($size < 1) {
67            throw new GTException("Invalid sequence size: {$size}");
68        }
69
70        $id = $object->getObjectAt(0);
71
72        if (!$id instanceof ASN1ObjectId) {
73            throw new GTException("Expecting an ASN1ObjectId");
74        }
75
76        $this->id = $id->getValue();
77
78        if ($size == 2) {
79
80            $value = $object->getObjectAt(1);
81
82            if (!$value instanceof ASN1OctetString) {
83                throw new GTException("Expecting an ASN1OctetString");
84            }
85
86            $this->value = $value->getValue();
87            $this->critical = false;
88
89        } else if ($size == 3) {
90
91            $critical = $object->getObjectAt(1);
92
93            if (!$critical instanceof ASN1Boolean) {
94                throw new GTException("Expecting an ASN1Boolean");
95            }
96
97            $this->critical = $critical->getValue();
98
99            $value = $object->getObjectAt(2);
100
101            if (!$value instanceof ASN1OctetString) {
102                throw new GTException("Expecting an ASN1OctetString");
103            }
104
105            $this->value = $value->getValue();
106
107        } else {
108            throw new GTException("Invalid sequence size: {$size}");
109
110        }
111
112    }
113
114    /**
115     * Encodes this X509Extension using DER.
116     *
117     * @return array byte array that contains the DER encoding of this X509Extension
118     */
119    public function encodeDER() {
120
121        $sequence = new ASN1Sequence();
122
123        $sequence->add(new ASN1ObjectId($this->id));
124
125        if ($this->isCritical() === true) {
126            $sequence->add(new ASN1Boolean(true));
127        }
128
129        $sequence->add(new ASN1OctetString($this->value));
130
131        return $sequence->encodeDER();
132    }
133
134    /**
135     * Gets the id.
136     *
137     * @return string oid
138     */
139    public function getId() {
140        return $this->id;
141    }
142
143    /**
144     * Sets the id.
145     *
146     * @param  string $id oid
147     * @return void
148     */
149    public function setId($id) {
150        $this->id = $id;
151    }
152
153    /**
154     * Checks if this extension is critical.
155     *
156     * @return bool true if this extension is critical
157     */
158    public function isCritical() {
159        return $this->critical;
160    }
161
162    /**
163     * Sets the critical status of this extension.
164     *
165     * @param  bool $critical true if this extension is critical
166     * @return void
167     */
168    public function setCritical($critical) {
169        $this->critical = $critical;
170    }
171
172    /**
173     * Gets the value.
174     *
175     * @return array byte array containing the value
176     */
177    public function getValue() {
178        return $this->value;
179    }
180
181    /**
182     * Sets the value.
183     *
184     * @param  array $value byte array the containing the value
185     * @return void
186     */
187    public function setValue($value) {
188        $this->value = $value;
189    }
190}
191
192?>
193