<?php
/*
 * Copyright 2008-2010 GuardTime AS
 *
 * This file is part of the GuardTime PHP SDK.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @package asn1
 * @subpackage cms
 */

/**
 * CMS ContentInfo implementation.
 *
 * <pre>
 * ContentInfo ::= SEQUENCE {
 *  contentType ContentType,
 *  content [0] EXPLICIT ANY DEFINED BY contentType
 * }
 * </pre>
 *
 * @package asn1
 * @subpackage cms
 *
 * @link http://tools.ietf.org/html/rfc3852#section-3 RFC 3852: Cryptographic Message Syntax
 */
class CMSContentInfo implements ASN1DEREncodable {

    private $contentType;
    private $content;

    /**
     * Constructs a new instance of CMSContentInfo.
     */
    public function __construct() {

    }

    /**
     * Decodes the given ASN1Sequence as CMSContentInfo.
     *
     * Currently only CMSSignedData is supported.
     *
     * @throws GTException
     * @param  ASN1Sequence $object CMSContentInfo encoded as an ASN1Sequence
     * @return void
     */
    public function decode($object) {

        if (!$object instanceof ASN1Sequence) {
            throw new GTException("Expecting an ASN1Sequence");
        }

        if ($object->getObjectCount() != 2) {
            throw new GTException("Bad sequence size: {$object->getObjectCount()}");
        }

        $contentType = $object->getObjectAt(0);

        if (!$contentType instanceof ASN1ObjectId) {
            throw new GTException("Expecting an ASN1ObjectId");
        }

        $this->contentType = $contentType->getValue();

        $tag = $object->getObjectAt(1);

        if ($tag->getTagValue() != 0) {
            throw new GTException("Bad tag value for content: " . $tag->getTagValue());
        }

        switch ($this->contentType) {

            case CMSSignedData::OID:

                $content = new CMSSignedData();
                $content->decode($tag->getObject());

                $this->content = $content;

                break;

            default:

                throw new GTException("Unsupported content type: {$this->contentType}");

        }
    }

    /**
     * Encodes this CMSContentInfo using DER.
     *
     * @return array byte array that contains the DER encoding of this CMSContentInfo
     */
    public function encodeDER() {

        $sequence = new ASN1Sequence();
        $sequence->add(new ASN1ObjectId($this->contentType));

        $tag = new ASN1Tag();

        $tag->setExplicit(true);
        $tag->setTagValue(0);
        $tag->setTagType(ASN1_TAG_CONSTRUCTED);
        $tag->setTagClass(ASN1_TAG_CONTEXT);
        $tag->setObject($this->content);

        $sequence->add($tag);

        return $sequence->encodeDER();

    }

    /**
     * Gets the content inside this CMSContentInfo.
     *
     * @return object content inside this CMSContentInfo
     */
    public function getContent() {
        return $this->content;
    }

    /**
     * Gets the type of the content inside this CMSContentInfo.
     *
     * @return string oid
     */
    public function getContentType() {
        return $this->contentType;
    }

}

?>
