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 ContentInfo implementation.
27 *
28 * <pre>
29 * ContentInfo ::= SEQUENCE {
30 *  contentType ContentType,
31 *  content [0] EXPLICIT ANY DEFINED BY contentType
32 * }
33 * </pre>
34 *
35 * @package asn1
36 * @subpackage cms
37 *
38 * @link http://tools.ietf.org/html/rfc3852#section-3 RFC 3852: Cryptographic Message Syntax
39 */
40class CMSContentInfo implements ASN1DEREncodable {
41
42    private $contentType;
43    private $content;
44
45    /**
46     * Constructs a new instance of CMSContentInfo.
47     */
48    public function __construct() {
49
50    }
51
52    /**
53     * Decodes the given ASN1Sequence as CMSContentInfo.
54     *
55     * Currently only CMSSignedData is supported.
56     *
57     * @throws GTException
58     * @param  ASN1Sequence $object CMSContentInfo encoded as an ASN1Sequence
59     * @return void
60     */
61    public function decode($object) {
62
63        if (!$object instanceof ASN1Sequence) {
64            throw new GTException("Expecting an ASN1Sequence");
65        }
66
67        if ($object->getObjectCount() != 2) {
68            throw new GTException("Bad sequence size: {$object->getObjectCount()}");
69        }
70
71        $contentType = $object->getObjectAt(0);
72
73        if (!$contentType instanceof ASN1ObjectId) {
74            throw new GTException("Expecting an ASN1ObjectId");
75        }
76
77        $this->contentType = $contentType->getValue();
78
79        $tag = $object->getObjectAt(1);
80
81        if ($tag->getTagValue() != 0) {
82            throw new GTException("Bad tag value for content: " . $tag->getTagValue());
83        }
84
85        switch ($this->contentType) {
86
87            case CMSSignedData::OID:
88
89                $content = new CMSSignedData();
90                $content->decode($tag->getObject());
91
92                $this->content = $content;
93
94                break;
95
96            default:
97
98                throw new GTException("Unsupported content type: {$this->contentType}");
99
100        }
101    }
102
103    /**
104     * Encodes this CMSContentInfo using DER.
105     *
106     * @return array byte array that contains the DER encoding of this CMSContentInfo
107     */
108    public function encodeDER() {
109
110        $sequence = new ASN1Sequence();
111        $sequence->add(new ASN1ObjectId($this->contentType));
112
113        $tag = new ASN1Tag();
114
115        $tag->setExplicit(true);
116        $tag->setTagValue(0);
117        $tag->setTagType(ASN1_TAG_CONSTRUCTED);
118        $tag->setTagClass(ASN1_TAG_CONTEXT);
119        $tag->setObject($this->content);
120
121        $sequence->add($tag);
122
123        return $sequence->encodeDER();
124
125    }
126
127    /**
128     * Gets the content inside this CMSContentInfo.
129     *
130     * @return object content inside this CMSContentInfo
131     */
132    public function getContent() {
133        return $this->content;
134    }
135
136    /**
137     * Gets the type of the content inside this CMSContentInfo.
138     *
139     * @return string oid
140     */
141    public function getContentType() {
142        return $this->contentType;
143    }
144
145}
146
147?>
148