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 IssuerAndSerialNumber implementation.
27 *
28 * <pre>
29 * IssuerAndSerialNumber :: = SEQUENCE {
30 *      issuer Name,
31 *      serialNumber CertificateSerialNumber
32 * }
33 * </pre>
34 *
35 * <pre>
36 * CertificateSerialNumber :: = INTEGER
37 * </pre>
38 *
39 * @package asn1
40 * @subpackage cms
41 *
42 * @link http://tools.ietf.org/html/rfc3852#section-10.2.4 RFC 3852: Cryptographic Message Syntax
43 */
44class CMSIssuerAndSerialNumber implements ASN1DEREncodable {
45
46    private $issuer;
47    private $serialNumber;
48
49    /**
50     * Constructs a new instance of CMSIssuerAndSerialNumber.
51     */
52    public function __construct() {
53    }
54
55    /**
56     * Decodes the given ASN1Sequence as CMSIssuerAndSerialNumber.
57     *
58     * @throws GTException
59     * @param  ASN1Sequence $object CMSIssuerAndSerialNumber encoded as an ASN1Sequence.
60     * @return void
61     */
62    public function decode($object) {
63
64        if (!$object instanceof ASN1Sequence) {
65            throw new GTException("Expecting an ASN1Sequence");
66        }
67
68        $size = $object->getObjectCount();
69
70        if ($size != 2) {
71            throw new GTException("Invalid sequence size: {$size}");
72        }
73
74        $issuer = $object->getObjectAt(0);
75
76        if (!$issuer instanceof ASN1Sequence) {
77            throw new GTException("Expecting an ASN1Sequence");
78        }
79
80        if ($issuer->getObjectCount() == 0) {
81            throw new GTException("Issuer content missing");
82        }
83
84        $this->issuer = $issuer;
85
86        $serialNumber = $object->getObjectAt(1);
87
88        if (!$serialNumber instanceof ASN1Integer) {
89            throw new GTException("Expecting an ASN1Integer");
90        }
91
92        $this->serialNumber = $serialNumber->getValue();
93
94    }
95
96    /**
97     * Encodes this CMSIssuerAndSerialNumber using DER.
98     *
99     * @return array byte array that contains the DER encoding of this CMSIssuerAndSerialNumber
100     */
101    public function encodeDER() {
102
103        $sequence = new ASN1Sequence();
104        $sequence->add($this->issuer);
105        $sequence->add(new ASN1Integer(new GTBigInteger($this->serialNumber)));
106
107        return $sequence->encodeDER();
108    }
109
110    /**
111     * Gets the issuer.
112     *
113     * @return ASN1Sequence unparsed issuer as ASN1Sequence
114     */
115    public function getIssuer() {
116        return $this->issuer;
117    }
118
119    /**
120     * Sets the issuer.
121     *
122     * @param  ASN1Sequence $issuer unparsed issuer as ASN1Sequence
123     * @return void
124     */
125    public function setIssuer($issuer) {
126        $this->issuer = $issuer;
127    }
128
129    /**
130     * Gets the serial number.
131     *
132     * @return string serial number
133     */
134    public function getSerialNumber() {
135        return $this->serialNumber;
136    }
137
138    /**
139     * Sets the serial number.
140     *
141     * @param  string $serialNumber the serial number
142     * @return void
143     */
144    public function setSerialNumber($serialNumber) {
145        $this->serialNumber = $serialNumber;
146    }
147}
148
149?>
150