001/* 002 * Copyright 2017-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2017-2024 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-2024 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.NotNull; 043import com.unboundid.util.Nullable; 044import com.unboundid.util.StaticUtils; 045import com.unboundid.util.ThreadSafety; 046import com.unboundid.util.ThreadSafetyLevel; 047 048import static com.unboundid.asn1.ASN1Messages.*; 049 050 051 052/** 053 * This class provides an ASN.1 IA5 string element that can hold any empty or 054 * non-empty string comprised only of the ASCII characters (including ASCII 055 * control characters). 056 */ 057@NotMutable() 058@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 059public final class ASN1IA5String 060 extends ASN1Element 061{ 062 /** 063 * The serial version UID for this serializable class. 064 */ 065 private static final long serialVersionUID = -9112411497688179053L; 066 067 068 069 // The string value for this element. 070 @NotNull private final String stringValue; 071 072 073 074 /** 075 * Creates a new ASN.1 IA5 string element with the default BER type and the 076 * provided value. 077 * 078 * @param stringValue The string value to use for this element. It may be 079 * {@code null} or empty if the value should be empty. 080 * It must only contain characters from the ASCII 081 * character set (including control characters). 082 * 083 * @throws ASN1Exception If the provided string does not represent a valid 084 * IA5 string. 085 */ 086 public ASN1IA5String(@Nullable final String stringValue) 087 throws ASN1Exception 088 { 089 this(ASN1Constants.UNIVERSAL_IA5_STRING_TYPE, stringValue); 090 } 091 092 093 094 /** 095 * Creates a new ASN.1 IA5 string element with the specified BER type and the 096 * provided value. 097 * 098 * @param type The BER type for this element. 099 * @param stringValue The string value to use for this element. It may be 100 * {@code null} or empty if the value should be empty. 101 * It must only contain characters from the ASCII 102 * character set (including control characters). 103 * 104 * @throws ASN1Exception If the provided string does not represent a valid 105 * IA5 string. 106 */ 107 public ASN1IA5String(final byte type, @Nullable final String stringValue) 108 throws ASN1Exception 109 { 110 this(type, stringValue, StaticUtils.getBytes(stringValue)); 111 } 112 113 114 115 /** 116 * Creates a new ASN.1 IA5 string element with the specified BER type and the 117 * provided value. 118 * 119 * @param type The BER type for this element. 120 * @param stringValue The string value to use for this element. It may be 121 * {@code null} or empty if the value should be empty. 122 * It must only contain characters from the ASCII 123 * character set (including control characters). 124 * @param encodedValue The bytes that comprise the encoded element value. 125 * 126 * @throws ASN1Exception If the provided string does not represent a valid 127 * IA5 string. 128 */ 129 private ASN1IA5String(final byte type, @Nullable final String stringValue, 130 @NotNull final byte[] encodedValue) 131 throws ASN1Exception 132 { 133 super(type, encodedValue); 134 135 if (stringValue == null) 136 { 137 this.stringValue = ""; 138 } 139 else 140 { 141 this.stringValue = stringValue; 142 143 for (final byte b : encodedValue) 144 { 145 if ((b & 0x7F) != (b & 0xFF)) 146 { 147 throw new ASN1Exception(ERR_IA5_STRING_DECODE_VALUE_NOT_IA5.get()); 148 } 149 } 150 } 151 } 152 153 154 155 /** 156 * Retrieves the string value for this element. 157 * 158 * @return The string value for this element. 159 */ 160 @NotNull() 161 public String stringValue() 162 { 163 return stringValue; 164 } 165 166 167 168 /** 169 * Decodes the contents of the provided byte array as an IA5 string element. 170 * 171 * @param elementBytes The byte array to decode as an ASN.1 IA5 string 172 * element. 173 * 174 * @return The decoded ASN.1 IA5 string element. 175 * 176 * @throws ASN1Exception If the provided array cannot be decoded as an 177 * IA5 string element. 178 */ 179 @NotNull() 180 public static ASN1IA5String decodeAsIA5String( 181 @NotNull final byte[] elementBytes) 182 throws ASN1Exception 183 { 184 try 185 { 186 int valueStartPos = 2; 187 int length = (elementBytes[1] & 0x7F); 188 if (length != elementBytes[1]) 189 { 190 final int numLengthBytes = length; 191 192 length = 0; 193 for (int i=0; i < numLengthBytes; i++) 194 { 195 length <<= 8; 196 length |= (elementBytes[valueStartPos++] & 0xFF); 197 } 198 } 199 200 if ((elementBytes.length - valueStartPos) != length) 201 { 202 throw new ASN1Exception(ERR_ELEMENT_LENGTH_MISMATCH.get(length, 203 (elementBytes.length - valueStartPos))); 204 } 205 206 final byte[] elementValue = new byte[length]; 207 System.arraycopy(elementBytes, valueStartPos, elementValue, 0, length); 208 209 return new ASN1IA5String(elementBytes[0], 210 StaticUtils.toUTF8String(elementValue), elementValue); 211 } 212 catch (final ASN1Exception ae) 213 { 214 Debug.debugException(ae); 215 throw ae; 216 } 217 catch (final Exception e) 218 { 219 Debug.debugException(e); 220 throw new ASN1Exception(ERR_ELEMENT_DECODE_EXCEPTION.get(e), e); 221 } 222 } 223 224 225 226 /** 227 * Decodes the provided ASN.1 element as an IA5 string element. 228 * 229 * @param element The ASN.1 element to be decoded. 230 * 231 * @return The decoded ASN.1 IA5 string element. 232 * 233 * @throws ASN1Exception If the provided element cannot be decoded as an 234 * IA5 string element. 235 */ 236 @NotNull() 237 public static ASN1IA5String decodeAsIA5String( 238 @NotNull final ASN1Element element) 239 throws ASN1Exception 240 { 241 final byte[] elementValue = element.getValue(); 242 return new ASN1IA5String(element.getType(), 243 StaticUtils.toUTF8String(elementValue), elementValue); 244 } 245 246 247 248 /** 249 * {@inheritDoc} 250 */ 251 @Override() 252 public void toString(@NotNull final StringBuilder buffer) 253 { 254 buffer.append(stringValue); 255 } 256}