<?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 gt
 */

/**
 * GT SignatureInfo implementation.
 *
 * <pre>
 * SignatureInfo ::= SEQUENCE {
 *   signatureAlgorithm   AlgorithmIdentifier,
 *   signatureValue       OCTET STRING
 *   pkiReferences        [0] IMPLICIT SET OF OCTET STRING OPTIONAL
 * }
 * </pre>
 *
 * @package asn1
 * @subpackage gt
 */
class GTSignatureInfo implements ASN1DEREncodable {

    private $signatureAlgorithm;
    private $signatureValue;
    private $pkiReferences;

    /**
     * Construct a new instance of GTSignatureInfo.
     */
    public function __construct() {
    }

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

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

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

        $algorithm = new X509AlgorithmIdentifier();
        $algorithm->decode($object->getObjectAt(0));

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

        if (!$signatureValue instanceof ASN1OctetString) {
            throw new GTException("Expecting an ASN1OctetString");
        }

        $this->signatureAlgorithm = $algorithm;
        $this->signatureValue = $signatureValue->getValue();

        if ($object->getObjectCount() == 3) {
            throw new GTException("pkiRefernces not implemented");
        }

    }

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

        $sequence = new ASN1Sequence();

        $sequence->add($this->signatureAlgorithm);
        $sequence->add(new ASN1OctetString($this->signatureValue));

        return $sequence->encodeDER();
    }

    /**
     * Gets the signature algorithm.
     *
     * @return X509AlgorithmIdentifier signature algorithm
     */
    public function getSignatureAlgorithm() {
        return $this->signatureAlgorithm;
    }

    /**
     * Gets the signature value.
     *
     * @return array byte array containing the signature value bytes
     */
    public function getSignatureValue() {
        return $this->signatureValue;
    }

}

?>
