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