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 AlgorithmIdentifier implementation.
27 *
28 * <pre>
29 * AlgorithmIdentifier  ::=  SEQUENCE  {
30 *  algorithm   OBJECT IDENTIFIER,
31 *  parameters  ANY DEFINED BY algorithm OPTIONAL
32 * }
33 * </pre>
34 *
35 * @package asn1
36 * @subpackage x509
37 */
38class X509AlgorithmIdentifier implements ASN1DEREncodable {
39
40    private $algorithm;
41
42    /**
43     * Constructs a new instance of X509AlgorithmIdentifier.
44     */
45    public function __construct() {
46    }
47
48    /**
49     * Decodes the given ASN1Sequence as X509AlgorithmIdentifier.
50     *
51     * @throws GTException
52     * @param  ASN1Sequence $object X509AlgorithmIdentifier encoded as ASN1Sequence
53     * @return void
54     */
55    public function decode($object) {
56
57        if (!$object instanceof ASN1Sequence) {
58            throw new GTException("object must be an instance of ASN1Sequence");
59        }
60
61        if ($object->getObjectCount() < 1) {
62            throw new GTException("sequence must contain at least 1 object");
63        }
64
65        if ($object->getObjectCount() > 2) {
66            throw new GTException("sequence must not contain more than 2 objects");
67        }
68
69        if ($object->getObjectCount() == 2 && !($object->getObjectAt(1) instanceof ASN1Null)) {
70            throw new GTException("parameters not implemented");
71        }
72
73        $algorithm = $object->getObjectAt(0);
74
75        if (!$algorithm instanceof ASN1ObjectId) {
76            throw new GTException("algorithm must be an instance of ASN1ObjectId");
77        }
78
79        $this->algorithm = $algorithm->getValue();
80
81    }
82
83    /**
84     * Encodes this X509AlgorithmIdentifier using DER.
85     *
86     * @return array byte array that contains the DER encoding of this X509AlgorithmIdentifier
87     */
88    public function encodeDER() {
89
90        $sequence = new ASN1Sequence();
91        $sequence->add(new ASN1ObjectId($this->algorithm));
92        $sequence->add(new ASN1Null());
93
94        return $sequence->encodeDER();
95    }
96
97    /**
98     * Sets the algorithm.
99     *
100     * @param  string $algorithm oid
101     * @return void
102     */
103    public function setAlgorithm($algorithm) {
104        $this->algorithm = $algorithm;
105
106    }
107
108    /**
109     * Gets the algorithm.
110     *
111     * @return string
112     */
113    public function getAlgorithm() {
114        return $this->algorithm;
115
116    }
117
118}
119
120?>
121