<?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 IssuerAndSerialNumber implementation.
 *
 * <pre>
 * IssuerAndSerialNumber :: = SEQUENCE {
 *      issuer Name,
 *      serialNumber CertificateSerialNumber
 * }
 * </pre>
 *
 * <pre>
 * CertificateSerialNumber :: = INTEGER
 * </pre>
 *
 * @package asn1
 * @subpackage cms
 *
 * @link http://tools.ietf.org/html/rfc3852#section-10.2.4 RFC 3852: Cryptographic Message Syntax
 */
class CMSIssuerAndSerialNumber implements ASN1DEREncodable {

    private $issuer;
    private $serialNumber;

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

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

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

        $size = $object->getObjectCount();

        if ($size != 2) {
            throw new GTException("Invalid sequence size: {$size}");
        }

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

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

        if ($issuer->getObjectCount() == 0) {
            throw new GTException("Issuer content missing");
        }

        $this->issuer = $issuer;

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

        if (!$serialNumber instanceof ASN1Integer) {
            throw new GTException("Expecting an ASN1Integer");
        }

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

    }

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

        $sequence = new ASN1Sequence();
        $sequence->add($this->issuer);
        $sequence->add(new ASN1Integer(new GTBigInteger($this->serialNumber)));

        return $sequence->encodeDER();
    }

    /**
     * Gets the issuer.
     *
     * @return ASN1Sequence unparsed issuer as ASN1Sequence
     */
    public function getIssuer() {
        return $this->issuer;
    }

    /**
     * Sets the issuer.
     *
     * @param  ASN1Sequence $issuer unparsed issuer as ASN1Sequence
     * @return void
     */
    public function setIssuer($issuer) {
        $this->issuer = $issuer;
    }

    /**
     * Gets the serial number.
     *
     * @return string serial number
     */
    public function getSerialNumber() {
        return $this->serialNumber;
    }

    /**
     * Sets the serial number.
     *
     * @param  string $serialNumber the serial number
     * @return void
     */
    public function setSerialNumber($serialNumber) {
        $this->serialNumber = $serialNumber;
    }
}

?>
