001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.crypto.asn1;
019
020 import java.io.ByteArrayOutputStream;
021 import java.io.IOException;
022
023 /**
024 * DER UniversalString object.
025 */
026 public class DERUniversalString
027 extends DERObject
028 implements DERString
029 {
030 private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
031 private byte[] string;
032
033 /**
034 * return a Universal String from the passed in object.
035 *
036 * @exception IllegalArgumentException if the object cannot be converted.
037 */
038 public static DERUniversalString getInstance(
039 Object obj)
040 {
041 if (obj == null || obj instanceof DERUniversalString)
042 {
043 return (DERUniversalString)obj;
044 }
045
046 if (obj instanceof ASN1OctetString)
047 {
048 return new DERUniversalString(((ASN1OctetString)obj).getOctets());
049 }
050
051 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
052 }
053
054 /**
055 * return a Universal String from a tagged object.
056 *
057 * @param obj the tagged object holding the object we want
058 * @param explicit true if the object is meant to be explicitly
059 * tagged false otherwise.
060 * @exception IllegalArgumentException if the tagged object cannot
061 * be converted.
062 */
063 public static DERUniversalString getInstance(
064 ASN1TaggedObject obj,
065 boolean explicit)
066 {
067 return getInstance(obj.getObject());
068 }
069
070 /**
071 * basic constructor - byte encoded string.
072 */
073 public DERUniversalString(
074 byte[] string)
075 {
076 this.string = string;
077 }
078
079 public String getString()
080 {
081 StringBuffer buf = new StringBuffer("#");
082 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
083 ASN1OutputStream aOut = new ASN1OutputStream(bOut);
084
085 try
086 {
087 aOut.writeObject(this);
088 }
089 catch (IOException e)
090 {
091 throw new RuntimeException("internal error encoding BitString", e);
092 }
093
094 byte[] string = bOut.toByteArray();
095
096 for (int i = 0; i != string.length; i++)
097 {
098 buf.append(table[(string[i] >>> 4) % 0xf]);
099 buf.append(table[string[i] & 0xf]);
100 }
101
102 return buf.toString();
103 }
104
105 public byte[] getOctets()
106 {
107 return string;
108 }
109
110 void encode(
111 DEROutputStream out)
112 throws IOException
113 {
114 out.writeEncoded(UNIVERSAL_STRING, this.getOctets());
115 }
116
117 public boolean equals(
118 Object o)
119 {
120 if ((o == null) || !(o instanceof DERUniversalString))
121 {
122 return false;
123 }
124
125 return this.getString().equals(((DERUniversalString)o).getString());
126 }
127
128 public int hashCode()
129 {
130 return this.getString().hashCode();
131 }
132 }