<?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 EncapsulatedContentInfo implementation.
 *
 * <pre>
 * EncapsulatedContentInfo ::= SEQUENCE {
 *      eContentType ContentType,
 *      eContent [0] EXPLICIT OCTET STRING OPTIONAL
 * }
 * </pre>
 *
 * @package asn1
 * @subpackage cms
 *
 * @link http://tools.ietf.org/html/rfc3852#section-5.2 RFC 3852: Cryptographic Message Syntax
 */
class CMSEncapsulatedContentInfo {

    private $contentType;
    private $content;

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

    /**
     * Decodes the given ASN1Sequence as CMSEncapsulatedContentInfo.
     *
     * @throws GTException
     * @param  ASN1Sequence $object CMSEncapsulatedContentInfo encoded as an ASN1Sequence
     * @return void
     */
    public function decode($object) {

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

        if ($object->getObjectCount() < 1 || $object->getObjectCount() > 2) {
            throw new GTException("Invalid sequence size: {$object->getObjectCount()}");
        }

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

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

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

        if ($object->getObjectCount() == 2) {

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

            if (!$tag instanceof ASN1Tag) {
                throw new GTException("Expecting an ASN1Tag");
            }

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

            $this->content = $tag->getObject()->getValue();
        }

    }

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

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

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

        return $sequence->encodeDER();

    }

    /**
     * Gets the encapsulated content.
     *
     * Currently only TSPTSTInfo is supported.
     *
     * @throws GTException
     * @return TSPTSTInfo encapsulated content.
     */
    public function getContent() {

        if (empty($this->content)) {
            throw new GTException("Content missing");
        }

        if ($this->contentType != TSPTSTInfo::OID) {
            throw new GTException("Invalid contentType for TSTInfo: {$this->contentType}");
        }

        $sequence = ASN1DER::decode($this->content);

        $content = new TSPTSTInfo();
        $content->decode($sequence);

        return $content;

    }

    /**
     * Gets the encapsulated raw content.
     *
     * This method doesn't perform any decoding.
     *
     * @see getContent
     * @return array raw byte array encoded inside this CMSEncapsulatedContent
     */
    public function getContentRaw() {
        return $this->content;
    }

    /**
     * Sets the encapsulated raw content
     *
     * @param  array $content byte array containing the encoding of the encapsulated content
     * @return void
     */
    public function setContent($content) {
        $this->content = $content;
    }

}

?>
