1<?php
2/*
3 * Copyright 2008-2010 GuardTime AS
4 *
5 * This file is part of the GuardTime PHP SDK.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/**
21 * @package asn1
22 * @subpackage gt
23 */
24
25/**
26 * GT SignatureInfo implementation.
27 *
28 * <pre>
29 * SignatureInfo ::= SEQUENCE {
30 *   signatureAlgorithm   AlgorithmIdentifier,
31 *   signatureValue       OCTET STRING
32 *   pkiReferences        [0] IMPLICIT SET OF OCTET STRING OPTIONAL
33 * }
34 * </pre>
35 *
36 * @package asn1
37 * @subpackage gt
38 */
39class GTSignatureInfo implements ASN1DEREncodable {
40
41    private $signatureAlgorithm;
42    private $signatureValue;
43    private $pkiReferences;
44
45    /**
46     * Construct a new instance of GTSignatureInfo.
47     */
48    public function __construct() {
49    }
50
51    /**
52     * Decodes the given ASN1Sequence as GTSignatureInfo.
53     *
54     * @throws GTException
55     * @param  ASN1Sequence $object GTSignatureInfo encoded as ASN1Sequence
56     * @return void
57     */
58    public function decode($object) {
59
60        if (!$object instanceof ASN1Sequence) {
61            throw new GTException("Expecting an ASN1Sequence");
62        }
63
64        if ($object->getObjectCount() < 2 || $object->getObjectCount() > 3) {
65            throw new GTException("Invalid sequence size: " . $object->getObjectCount());
66        }
67
68        $algorithm = new X509AlgorithmIdentifier();
69        $algorithm->decode($object->getObjectAt(0));
70
71        $signatureValue = $object->getObjectAt(1);
72
73        if (!$signatureValue instanceof ASN1OctetString) {
74            throw new GTException("Expecting an ASN1OctetString");
75        }
76
77        $this->signatureAlgorithm = $algorithm;
78        $this->signatureValue = $signatureValue->getValue();
79
80        if ($object->getObjectCount() == 3) {
81            throw new GTException("pkiRefernces not implemented");
82        }
83
84    }
85
86    /**
87     * Encodes this GTSignatureInfo using DER.
88     *
89     * @return array byte array containing the DER encoding of this GTSignatureInfo
90     */
91    public function encodeDER() {
92
93        $sequence = new ASN1Sequence();
94
95        $sequence->add($this->signatureAlgorithm);
96        $sequence->add(new ASN1OctetString($this->signatureValue));
97
98        return $sequence->encodeDER();
99    }
100
101    /**
102     * Gets the signature algorithm.
103     *
104     * @return X509AlgorithmIdentifier signature algorithm
105     */
106    public function getSignatureAlgorithm() {
107        return $this->signatureAlgorithm;
108    }
109
110    /**
111     * Gets the signature value.
112     *
113     * @return array byte array containing the signature value bytes
114     */
115    public function getSignatureValue() {
116        return $this->signatureValue;
117    }
118
119}
120
121?>
122