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 cms
23 */
24
25/**
26 * CMS Attribute implementation.
27 *
28 * <pre>
29 * Attribute ::= SEQUENCE {
30 *      attrType OBJECT IDENTIFIER,
31 *      attrValues SET OF AttributeValue
32 * }
33 * </pre>
34 *
35 * <pre>
36 * AttributeValue ::= ANY
37 * </pre>
38 *
39 * @package asn1
40 * @subpackage cms
41 *
42 * @link http://tools.ietf.org/html/rfc3852#section-5.3 RFC 3852: Cryptographic Message Syntax
43 */
44class CMSAttribute implements ASN1DEREncodable {
45
46    private $type;
47    private $values;
48
49    /**
50     * Construct a new instance of CMSAttribute.
51     */
52    public function __construct() {
53    }
54
55    /**
56     * Decodes the given ASN1Sequence as CMSAttribute.
57     *
58     * @throws GTException
59     * @param  ASN1Sequence $object CMSAttribute encoded as an ASN1Sequence
60     * @return void
61     */
62    public function decode($object) {
63
64        if (!$object instanceof ASN1Sequence) {
65            throw new GTException("Expecting an ASN1Sequence");
66        }
67
68        $size = $object->getObjectCount();
69
70        if ($size != 2) {
71            throw new GTException("Invalid sequence size: {$size}");
72        }
73
74        $type = $object->getObjectAt(0);
75
76        if (!$type instanceof ASN1ObjectId) {
77            throw new GTException("Expecting an ASN1ObjectId");
78        }
79
80        $this->type = $type->getValue();
81
82        $this->values = array();
83
84        $values = $object->getObjectAt(1);
85
86        if (!$values instanceof ASN1Set) {
87            throw new GTException("Expecting an ASN1Set");
88        }
89
90        $size = $values->getObjectCount();
91
92        if ($size < 1) {
93            throw new GTException("Missing attribute values");
94        }
95
96        for ($i = 0; $i < $size; $i++) {
97            array_push($this->values, $values->getObjectAt(0));
98        }
99
100    }
101
102    /**
103     * Encodes this CMSAttribute using DER.
104     *
105     * @return array byte array that contains the DER encoding of this CMSAttribute
106     */
107    public function encodeDER() {
108
109        $values = new ASN1Set();
110
111        foreach ($this->values as $value) {
112            $values->add($value);
113        }
114
115        $sequence = new ASN1Sequence();
116        $sequence->add(new ASN1ObjectId($this->type));
117        $sequence->add($values);
118
119        return $sequence->encodeDER();
120    }
121
122    /**
123     * Gets the attribute type.
124     *
125     * @return string object id
126     */
127    public function getType() {
128        return $this->type;
129    }
130
131    /**
132     * Gets the attribute values.
133     *
134     * @return array attribute values
135     */
136    public function getValues() {
137        return $this->values;
138    }
139
140}
141
142?>
143