<?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 SignerIdentifier implementation.
 *
 * <pre>
 * SignerIdentifier ::= CHOICE {
 *      issuerAndSerialNumber IssuerAndSerialNumber,
 *      subjectKeyIdentifier [0] SubjectKeyIdentifier
 * }
 * </pre>
 *
 * <pre>
 * SubjectKeyIdentifier ::= OCTET STRING
 * </pre>
 *
 * @package asn1
 * @subpackage cms
 *
 * @link http://tools.ietf.org/html/rfc3852#section-5.3 RFC 3852: Cryptographic Message Syntax
 */
class CMSSignerIdentifier implements ASN1DEREncodable {

    private $issuerAndSerialNumber;
    private $subjectKeyIdentifier;

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

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

        if ($object instanceof ASN1Sequence) {

            $issuerAndSerialNumber = new CMSIssuerAndSerialNumber();
            $issuerAndSerialNumber->decode($object);

            $this->issuerAndSerialNumber = $issuerAndSerialNumber;

        } else if ($object instanceof ASN1Tag) {

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

            $subjectKeyIdentifier = $object->getObjectAs(ASN1_TAG_OCTET_STRING);

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

        } else {
            throw new GTException("Unexpected ASN1 type: " . get_class($object));
        }

    }

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

        if (!is_null($this->issuerAndSerialNumber)) {

            return $this->issuerAndSerialNumber->encodeDER();

        } else if (!is_null($this->subjectKeyIdentifier)) {

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

            return $tag->encodeDER();

        } else {
            throw new GTException("Unable to encode CMSSignerIdentifier, both issuerAndSerialNumber and subjectKeyIdentifier are null");
        }

    }

    /**
     * Gets the issuer and serial number.
     *
     * @return CMSIssuerAndSerialNumber issuer and serial number
     */
    public function getIssuerAndSerialNumber() {
        return $this->issuerAndSerialNumber;
    }

    /**
     * Sets the issuer and serial number.
     *
     * @param  CMSIssuerAndSerialNumber $issuerAndSerialNumber issuer and serial number
     * @return void
     */
    public function setIssuerAndSerialNumber($issuerAndSerialNumber) {
        $this->issuerAndSerialNumber = $issuerAndSerialNumber;
    }


    /**
     * Gets the subject key identifier.
     *
     * @return array byte array containing the subject key identifier
     */
    public function getSubjectKeyIdentifier() {
        return $this->subjectKeyIdentifier;
    }

    /**
     * Sets the subject key identifier.
     *
     * @param  array $subjectKeyIdentifier byte array containing the subject key identifier
     * @return void
     */
    public function setSubjectKeyIdentifier($subjectKeyIdentifier) {
        $this->subjectKeyIdentifier = $subjectKeyIdentifier;
    }

}

?>
