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 cms
23 */
24
25/**
26 * CMS SignerIdentifier implementation.
27 *
28 * <pre>
29 * SignerIdentifier ::= CHOICE {
30 *      issuerAndSerialNumber IssuerAndSerialNumber,
31 *      subjectKeyIdentifier [0] SubjectKeyIdentifier
32 * }
33 * </pre>
34 *
35 * <pre>
36 * SubjectKeyIdentifier ::= OCTET STRING
37 * </pre>
38 *
39 * @package asn1
40 * @subpackage cms
41 *
42 * @link http://tools.ietf.org/html/rfc3852#section-5.3 RFC 3852: Cryptographic Message Syntax
43 */
44class CMSSignerIdentifier implements ASN1DEREncodable {
45
46    private $issuerAndSerialNumber;
47    private $subjectKeyIdentifier;
48
49    /**
50     * Constructs a new instance of CMSSignerIdentifier.
51     */
52    public function __construct() {
53    }
54
55    /**
56     * Decodes the given ASN1Sequence as CMSSignerIdentifier.
57     *
58     * @throws GTException
59     * @param  ASN1Sequence $object CMSSignerIdentifier encoded as ASN1Sequence
60     * @return void
61     */
62    public function decode($object) {
63
64        if ($object instanceof ASN1Sequence) {
65
66            $issuerAndSerialNumber = new CMSIssuerAndSerialNumber();
67            $issuerAndSerialNumber->decode($object);
68
69            $this->issuerAndSerialNumber = $issuerAndSerialNumber;
70
71        } else if ($object instanceof ASN1Tag) {
72
73            if ($object->getTagValue() != 0) {
74                throw new GTException("Unexpected TAG value: " . $object->getTagValue());
75            }
76
77            $subjectKeyIdentifier = $object->getObjectAs(ASN1_TAG_OCTET_STRING);
78
79            $this->subjectKeyIdentifier = $subjectKeyIdentifier->getValue();
80
81        } else {
82            throw new GTException("Unexpected ASN1 type: " . get_class($object));
83        }
84
85    }
86
87    /**
88     * Encodes the given CMSSignerIdentifier using DER.
89     *
90     * @throws GTException
91     * @return array byte array that contains the DER encoding of this CMSSignerIdentifier
92     */
93    public function encodeDER() {
94
95        if (!is_null($this->issuerAndSerialNumber)) {
96
97            return $this->issuerAndSerialNumber->encodeDER();
98
99        } else if (!is_null($this->subjectKeyIdentifier)) {
100
101            $tag = new ASN1Tag();
102            $tag->setExplicit(false);
103            $tag->setTagValue(0);
104            $tag->setTagClass(ASN1_TAG_CONTEXT);
105            $tag->setTagType(ASN1_TAG_CONSTRUCTED);
106            $tag->setObject(new ASN1OctetString($this->subjectKeyIdentifier));
107
108            return $tag->encodeDER();
109
110        } else {
111            throw new GTException("Unable to encode CMSSignerIdentifier, both issuerAndSerialNumber and subjectKeyIdentifier are null");
112        }
113
114    }
115
116    /**
117     * Gets the issuer and serial number.
118     *
119     * @return CMSIssuerAndSerialNumber issuer and serial number
120     */
121    public function getIssuerAndSerialNumber() {
122        return $this->issuerAndSerialNumber;
123    }
124
125    /**
126     * Sets the issuer and serial number.
127     *
128     * @param  CMSIssuerAndSerialNumber $issuerAndSerialNumber issuer and serial number
129     * @return void
130     */
131    public function setIssuerAndSerialNumber($issuerAndSerialNumber) {
132        $this->issuerAndSerialNumber = $issuerAndSerialNumber;
133    }
134
135
136    /**
137     * Gets the subject key identifier.
138     *
139     * @return array byte array containing the subject key identifier
140     */
141    public function getSubjectKeyIdentifier() {
142        return $this->subjectKeyIdentifier;
143    }
144
145    /**
146     * Sets the subject key identifier.
147     *
148     * @param  array $subjectKeyIdentifier byte array containing the subject key identifier
149     * @return void
150     */
151    public function setSubjectKeyIdentifier($subjectKeyIdentifier) {
152        $this->subjectKeyIdentifier = $subjectKeyIdentifier;
153    }
154
155}
156
157?>
158