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 *
27 * CMS EncapsulatedContentInfo implementation.
28 *
29 * <pre>
30 * EncapsulatedContentInfo ::= SEQUENCE {
31 *      eContentType ContentType,
32 *      eContent [0] EXPLICIT OCTET STRING OPTIONAL
33 * }
34 * </pre>
35 *
36 * @package asn1
37 * @subpackage cms
38 *
39 * @link http://tools.ietf.org/html/rfc3852#section-5.2 RFC 3852: Cryptographic Message Syntax
40 */
41class CMSEncapsulatedContentInfo {
42
43    private $contentType;
44    private $content;
45
46    /**
47     * Constructs a new instance of CMSEncapsulatedContentInfo.
48     */
49    public function __construct() {
50    }
51
52    /**
53     * Decodes the given ASN1Sequence as CMSEncapsulatedContentInfo.
54     *
55     * @throws GTException
56     * @param  ASN1Sequence $object CMSEncapsulatedContentInfo encoded as an ASN1Sequence
57     * @return void
58     */
59    public function decode($object) {
60
61        if (!$object instanceof ASN1Sequence) {
62            throw new GTException("Expecting an ASN1Sequence");
63        }
64
65        if ($object->getObjectCount() < 1 || $object->getObjectCount() > 2) {
66            throw new GTException("Invalid sequence size: {$object->getObjectCount()}");
67        }
68
69        $contentType = $object->getObjectAt(0);
70
71        if (!$contentType instanceof ASN1ObjectId) {
72            throw new GTException("Expecting an ASN1ObjectId");
73        }
74
75        $this->contentType = $contentType->getValue();
76
77        if ($object->getObjectCount() == 2) {
78
79            $tag = $object->getObjectAt(1);
80
81            if (!$tag instanceof ASN1Tag) {
82                throw new GTException("Expecting an ASN1Tag");
83            }
84
85            if ($tag->getTagValue() != 0) {
86                throw new GTException("Unexpected tag value: {$tag->getTagValue()}");
87            }
88
89            $this->content = $tag->getObject()->getValue();
90        }
91
92    }
93
94    /**
95     * Encodes this CMSEncapsulatedContentInfo using DER.
96     *
97     * @return array byte array that contains the DER encoding of this CMSEncapsulatedContentInfo
98     */
99    public function encodeDER() {
100
101        $tag = new ASN1Tag();
102        $tag->setExplicit(true);
103        $tag->setTagType(ASN1_TAG_CONSTRUCTED);
104        $tag->setTagClass(ASN1_TAG_CONTEXT);
105        $tag->setTagValue(0);
106        $tag->setObject(new ASN1OctetString($this->content));
107
108        $sequence = new ASN1Sequence();
109        $sequence->add(new ASN1ObjectId($this->contentType));
110        $sequence->add($tag);
111
112        return $sequence->encodeDER();
113
114    }
115
116    /**
117     * Gets the encapsulated content.
118     *
119     * Currently only TSPTSTInfo is supported.
120     *
121     * @throws GTException
122     * @return TSPTSTInfo encapsulated content.
123     */
124    public function getContent() {
125
126        if (empty($this->content)) {
127            throw new GTException("Content missing");
128        }
129
130        if ($this->contentType != TSPTSTInfo::OID) {
131            throw new GTException("Invalid contentType for TSTInfo: {$this->contentType}");
132        }
133
134        $sequence = ASN1DER::decode($this->content);
135
136        $content = new TSPTSTInfo();
137        $content->decode($sequence);
138
139        return $content;
140
141    }
142
143    /**
144     * Gets the encapsulated raw content.
145     *
146     * This method doesn't perform any decoding.
147     *
148     * @see getContent
149     * @return array raw byte array encoded inside this CMSEncapsulatedContent
150     */
151    public function getContentRaw() {
152        return $this->content;
153    }
154
155    /**
156     * Sets the encapsulated raw content
157     *
158     * @param  array $content byte array containing the encoding of the encapsulated content
159     * @return void
160     */
161    public function setContent($content) {
162        $this->content = $content;
163    }
164
165}
166
167?>
168