001/* 002 * Copyright 2017-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2017-2020 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2017-2020 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.asn1; 037 038 039 040import com.unboundid.util.Debug; 041import com.unboundid.util.NotMutable; 042import com.unboundid.util.StaticUtils; 043import com.unboundid.util.ThreadSafety; 044import com.unboundid.util.ThreadSafetyLevel; 045 046import static com.unboundid.asn1.ASN1Messages.*; 047 048 049 050/** 051 * This class provides an ASN.1 IA5 string element that can hold any empty or 052 * non-empty string comprised only of the ASCII characters (including ASCII 053 * control characters). 054 */ 055@NotMutable() 056@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 057public final class ASN1IA5String 058 extends ASN1Element 059{ 060 /** 061 * The serial version UID for this serializable class. 062 */ 063 private static final long serialVersionUID = -9112411497688179053L; 064 065 066 067 // The string value for this element. 068 private final String stringValue; 069 070 071 072 /** 073 * Creates a new ASN.1 IA5 string element with the default BER type and the 074 * provided value. 075 * 076 * @param stringValue The string value to use for this element. It may be 077 * {@code null} or empty if the value should be empty. 078 * It must only contain characters from the ASCII 079 * character set (including control characters). 080 * 081 * @throws ASN1Exception If the provided string does not represent a valid 082 * IA5 string. 083 */ 084 public ASN1IA5String(final String stringValue) 085 throws ASN1Exception 086 { 087 this(ASN1Constants.UNIVERSAL_IA5_STRING_TYPE, stringValue); 088 } 089 090 091 092 /** 093 * Creates a new ASN.1 IA5 string element with the specified BER type and the 094 * provided value. 095 * 096 * @param type The BER type for this element. 097 * @param stringValue The string value to use for this element. It may be 098 * {@code null} or empty if the value should be empty. 099 * It must only contain characters from the ASCII 100 * character set (including control characters). 101 * 102 * @throws ASN1Exception If the provided string does not represent a valid 103 * IA5 string. 104 */ 105 public ASN1IA5String(final byte type, final String stringValue) 106 throws ASN1Exception 107 { 108 this(type, stringValue, StaticUtils.getBytes(stringValue)); 109 } 110 111 112 113 /** 114 * Creates a new ASN.1 IA5 string element with the specified BER type and the 115 * provided value. 116 * 117 * @param type The BER type for this element. 118 * @param stringValue The string value to use for this element. It may be 119 * {@code null} or empty if the value should be empty. 120 * It must only contain characters from the ASCII 121 * character set (including control characters). 122 * @param encodedValue The bytes that comprise the encoded element value. 123 * 124 * @throws ASN1Exception If the provided string does not represent a valid 125 * IA5 string. 126 */ 127 private ASN1IA5String(final byte type, final String stringValue, 128 final byte[] encodedValue) 129 throws ASN1Exception 130 { 131 super(type, encodedValue); 132 133 if (stringValue == null) 134 { 135 this.stringValue = ""; 136 } 137 else 138 { 139 this.stringValue = stringValue; 140 141 for (final byte b : encodedValue) 142 { 143 if ((b & 0x7F) != (b & 0xFF)) 144 { 145 throw new ASN1Exception(ERR_IA5_STRING_DECODE_VALUE_NOT_IA5.get()); 146 } 147 } 148 } 149 } 150 151 152 153 /** 154 * Retrieves the string value for this element. 155 * 156 * @return The string value for this element. 157 */ 158 public String stringValue() 159 { 160 return stringValue; 161 } 162 163 164 165 /** 166 * Decodes the contents of the provided byte array as an IA5 string element. 167 * 168 * @param elementBytes The byte array to decode as an ASN.1 IA5 string 169 * element. 170 * 171 * @return The decoded ASN.1 IA5 string element. 172 * 173 * @throws ASN1Exception If the provided array cannot be decoded as an 174 * IA5 string element. 175 */ 176 public static ASN1IA5String decodeAsIA5String(final byte[] elementBytes) 177 throws ASN1Exception 178 { 179 try 180 { 181 int valueStartPos = 2; 182 int length = (elementBytes[1] & 0x7F); 183 if (length != elementBytes[1]) 184 { 185 final int numLengthBytes = length; 186 187 length = 0; 188 for (int i=0; i < numLengthBytes; i++) 189 { 190 length <<= 8; 191 length |= (elementBytes[valueStartPos++] & 0xFF); 192 } 193 } 194 195 if ((elementBytes.length - valueStartPos) != length) 196 { 197 throw new ASN1Exception(ERR_ELEMENT_LENGTH_MISMATCH.get(length, 198 (elementBytes.length - valueStartPos))); 199 } 200 201 final byte[] elementValue = new byte[length]; 202 System.arraycopy(elementBytes, valueStartPos, elementValue, 0, length); 203 204 return new ASN1IA5String(elementBytes[0], 205 StaticUtils.toUTF8String(elementValue), elementValue); 206 } 207 catch (final ASN1Exception ae) 208 { 209 Debug.debugException(ae); 210 throw ae; 211 } 212 catch (final Exception e) 213 { 214 Debug.debugException(e); 215 throw new ASN1Exception(ERR_ELEMENT_DECODE_EXCEPTION.get(e), e); 216 } 217 } 218 219 220 221 /** 222 * Decodes the provided ASN.1 element as an IA5 string element. 223 * 224 * @param element The ASN.1 element to be decoded. 225 * 226 * @return The decoded ASN.1 IA5 string element. 227 * 228 * @throws ASN1Exception If the provided element cannot be decoded as an 229 * IA5 string element. 230 */ 231 public static ASN1IA5String decodeAsIA5String(final ASN1Element element) 232 throws ASN1Exception 233 { 234 final byte[] elementValue = element.getValue(); 235 return new ASN1IA5String(element.getType(), 236 StaticUtils.toUTF8String(elementValue), elementValue); 237 } 238 239 240 241 /** 242 * {@inheritDoc} 243 */ 244 @Override() 245 public void toString(final StringBuilder buffer) 246 { 247 buffer.append(stringValue); 248 } 249}