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 x509
23 */
24
25/**
26 * X.509 GeneralName implementation.
27 *
28 * <pre>
29 * GeneralName ::= CHOICE {
30 *  otherName                       [0]     OtherName,
31 *  rfc822Name                      [1]     IA5String,
32 *  dNSName                         [2]     IA5String,
33 *  x400Address                     [3]     ORAddress,
34 *  directoryName                   [4]     Name,
35 *  ediPartyName                    [5]     EDIPartyName,
36 *  uniformResourceIdentifier       [6]     IA5String,
37 *  iPAddress                       [7]     OCTET STRING,
38 *  registeredID                    [8]     OBJECT IDENTIFIER}
39 * </pre>
40 *
41 * <pre>
42 * OtherName ::= SEQUENCE {
43 *  type-id    OBJECT IDENTIFIER,
44 *  value      [0] EXPLICIT ANY DEFINED BY type-id }
45 * </pre>
46 *
47 * <pre>
48 * EDIPartyName ::= SEQUENCE {
49 *  nameAssigner            [0]     DirectoryString OPTIONAL,
50 *  partyName               [1]     DirectoryString
51 * }
52 * </pre>
53 *
54 * <pre>
55 * Name ::= CHOICE { RDNSequence }
56 * </pre>
57 *
58 * @package asn1
59 * @subpackage x509
60 */
61class X509GeneralName {
62
63    private $object;
64
65    /**
66     * Construct a new instance of X509GeneralName.
67     */
68    public function __construct() {
69    }
70
71    /**
72     * Decodes the given ASN1Tag as X509GeneralName.
73     *
74     * @throws GTException
75     * @param  ASN1Tag $object X509GeneralName encoded as ASN1Tag
76     * @return void
77     */
78    public function decode($object) {
79
80        if (!$object instanceof ASN1Tag) {
81            throw new GTException("Expecting an ASN1Tag");
82        }
83
84        if ($object->getTagValue() < 0 || $object->getTagValue() > 8) {
85            throw new GTException("Unknown TAG value: {$object->getTagValue()}");
86        }
87
88        $this->object = $object;
89
90    }
91
92    /**
93     * Gets the formatted name.
94     *
95     * @throws GTException
96     * @return string formatted name used for timestamps.
97     */
98    public function getFormatted() {
99
100        if ($this->object == null) {
101            return null;
102        }
103
104        switch ($this->object->getTagValue()) {
105
106            case 0:
107                throw new GTException("[0] not implemented");
108
109            case 1:
110                return "RFC:" . $this->object->getObjectAs(ASN1_TAG_IA5_STRING)->getValue();
111
112            case 2:
113                return "DNS:" . $this->object->getObjectAs(ASN1_TAG_IA5_STRING)->getValue();
114
115            case 3:
116                throw new GTException("[3] not implemented");
117
118            case 4:
119                throw new GTException("[4] not implemented");
120
121            case 5:
122                throw new GTException("[5] not implemented");
123
124            case 6:
125                return "URI:" . $this->object->getObjectAs(ASN1_TAG_IA5_STRING)->getValue();
126
127            case 7:
128                throw new GTException("[7] not implemented");
129
130            case 8:
131                return "RID: " . $this->object->getObjectAs(ASN1_TAG_OBJECT_ID)->getValue();
132
133            default:
134                throw new GTException("Unknown TAG value {$this->object->getTagValue()}");
135
136        }
137
138    }
139
140}
141
142?>
143