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 tsp
23 */
24
25/**
26 * TSP MessageImprint implementatino.
27 *
28 * <pre>
29 * MessageImprint ::= SEQUENCE  {
30 *  hashAlgorithm                AlgorithmIdentifier,
31 *  hashedMessage                OCTET STRING
32 * }
33 * </pre>
34 *
35 * @see X509AlgorithmIdentifier
36 *
37 * @package asn1
38 * @subpackage tsp
39 *
40 * @link http://tools.ietf.org/html/rfc3161#section-2.4.1 RFC 3161: Time-Stamp Protocol
41 */
42class TSPMessageImprint implements ASN1DEREncodable {
43
44    private $hashAlgorithm;
45    private $hashedMessage;
46
47    /**
48     * Constructs a new instance of TSPMessageImprint.
49     */
50    public function __construct() {
51    }
52
53    /**
54     * Decodes the given ASN1Sequence as TSPMessageImprint.
55     *
56     * @throws GTException
57     * @param  ASN1Sequence $object TSPMessageImprint encoded as ASN1Sequence
58     * @return void
59     */
60    public function decode($object) {
61
62        if (!$object instanceof ASN1Sequence) {
63            throw new GTException("object must be an instance of ASN1Sequence");
64        }
65
66        if ($object->getObjectCount() != 2) {
67            throw new GTException("object must have the size of 2");
68        }
69
70        $hashAlgorithm = new X509AlgorithmIdentifier();
71        $hashAlgorithm->decode($object->getObjectAt(0));
72
73        $hashedMessage = $object->getObjectAt(1);
74
75        if (!$hashedMessage instanceof ASN1OctetString) {
76            throw new GTException("Expecting an ASN1OctetString");
77        }
78
79        $hashedMessage = $hashedMessage->getValue();
80
81        $this->setHashAlgorithm($hashAlgorithm);
82        $this->setHashedMessage($hashedMessage);
83
84    }
85
86    /**
87     * Encodes this TSPMessageImprint using DER.
88     *
89     * @return array byte array that contains the DER encoding of this TSPMessageImprint
90     */
91    public function encodeDER() {
92
93        $sequence = new ASN1Sequence();
94
95        $sequence->add($this->hashAlgorithm);
96        $sequence->add(new ASN1OctetString($this->hashedMessage));
97
98        return $sequence->encodeDER();
99    }
100
101    /**
102     * Sets the hash algorithm.
103     *
104     * @throws GTException
105     * @param  X509AlgorithmIdentifier $hashAlgorithm the hash algorithm used
106     * @return void
107     */
108    public function setHashAlgorithm($hashAlgorithm) {
109
110        if (!$hashAlgorithm instanceof X509AlgorithmIdentifier) {
111            throw new GTException("hashAlgorithm must be an instance of X509AlgorithmIdentifier");
112        }
113
114        $this->hashAlgorithm = $hashAlgorithm;
115    }
116
117    /**
118     * Gets the hash algorithm.
119     *
120     * @return X509AlgorithmIdentifier the hash algorithm used
121     */
122    public function getHashAlgorithm() {
123        return $this->hashAlgorithm;
124    }
125
126    /**
127     * Sets the hased message
128     *
129     * @throws GTException
130     * @param  array $hashedMessage byte array containing the message bytes
131     * @return void
132     */
133    public function setHashedMessage($hashedMessage) {
134
135        if (!is_array($hashedMessage)) {
136            throw new GTException("hashedMessage must be an array of bytes");
137        }
138
139        $this->hashedMessage = $hashedMessage;
140    }
141
142    /**
143     * Gets the hashed message.
144     *
145     * @return array byte array containing the message bytes
146     */
147    public function getHashedMessage() {
148        return $this->hashedMessage;
149    }
150
151}
152
153?>
154