1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /** 19 * @author Vladimir N. Molotkov, Stepan M. Mishura 20 * @version $Revision$ 21 */ 22 23 package org.apache.harmony.security.asn1; 24 25 import java.io.IOException; 26 import java.nio.charset.Charsets; 27 28 /** 29 * This class is the super class for all string ASN.1 types 30 * 31 * @see <a href="http://asn1.elibel.tm.fr/en/standards/index.htm">ASN.1</a> 32 */ 33 public abstract class ASN1StringType extends ASN1Type { 34 35 // TODO: what about defining them as separate classes? 36 // TODO: check decoded/encoded characters 37 public static final ASN1StringType BMPSTRING = new ASN1StringType( 38 TAG_BMPSTRING) { 39 }; 40 41 public static final ASN1StringType IA5STRING = new ASN1StringType( 42 TAG_IA5STRING) { 43 }; 44 45 public static final ASN1StringType GENERALSTRING = new ASN1StringType( 46 TAG_GENERALSTRING) { 47 }; 48 49 public static final ASN1StringType PRINTABLESTRING = new ASN1StringType( 50 TAG_PRINTABLESTRING) { 51 }; 52 53 public static final ASN1StringType TELETEXSTRING = new ASN1StringType( 54 TAG_TELETEXSTRING) { 55 }; 56 57 public static final ASN1StringType UNIVERSALSTRING = new ASN1StringType( 58 TAG_UNIVERSALSTRING) { 59 }; 60 61 public static final ASN1StringType UTF8STRING = new ASN1StringType(TAG_UTF8STRING) { 62 @Override public Object getDecodedObject(BerInputStream in) throws IOException { 63 return new String(in.buffer, in.contentOffset, in.length, Charsets.UTF_8); 64 } 65 66 @Override public void setEncodingContent(BerOutputStream out) { 67 byte[] bytes = ((String) out.content).getBytes(Charsets.UTF_8); 68 out.content = bytes; 69 out.length = bytes.length; 70 } 71 }; 72 73 public ASN1StringType(int tagNumber) { 74 super(tagNumber); 75 } 76 77 /** 78 * Tests provided identifier. 79 * 80 * @param identifier identifier to be verified 81 * @return true if identifier correspond to primitive or constructed 82 * identifier of this ASN.1 string type, otherwise false 83 */ 84 public final boolean checkTag(int identifier) { 85 return this.id == identifier || this.constrId == identifier; 86 } 87 88 public Object decode(BerInputStream in) throws IOException { 89 in.readString(this); 90 91 if (in.isVerify) { 92 return null; 93 } 94 return getDecodedObject(in); 95 } 96 97 /** 98 * Extracts String object from BER input stream. 99 */ 100 public Object getDecodedObject(BerInputStream in) throws IOException { 101 /* To ensure we get the correct encoding on non-ASCII platforms, specify 102 that we wish to convert from ASCII to the default platform encoding */ 103 return new String(in.buffer, in.contentOffset, in.length, Charsets.ISO_8859_1); 104 } 105 106 public void encodeASN(BerOutputStream out) { 107 out.encodeTag(id); 108 encodeContent(out); 109 } 110 111 public void encodeContent(BerOutputStream out) { 112 out.encodeString(); 113 } 114 115 public void setEncodingContent(BerOutputStream out) { 116 byte[] bytes = ((String) out.content).getBytes(Charsets.UTF_8); 117 out.content = bytes; 118 out.length = bytes.length; 119 } 120 } 121