001/* 002 * Copyright 2007-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2007-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) 2007-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.StaticUtils; 044import com.unboundid.util.ThreadSafety; 045import com.unboundid.util.ThreadSafetyLevel; 046 047import static com.unboundid.asn1.ASN1Messages.*; 048 049 050 051/** 052 * This class provides an ASN.1 null element, which does not hold a value. Null 053 * elements are generally used as placeholders that can be substituted for other 054 * types of elements. 055 */ 056@NotMutable() 057@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 058public final class ASN1Null 059 extends ASN1Element 060{ 061 /** 062 * A pre-allocated ASN.1 null element with the universal null BER type. 063 */ 064 @NotNull public static final ASN1Null UNIVERSAL_NULL_ELEMENT = new ASN1Null(); 065 066 067 068 /** 069 * The serial version UID for this serializable class. 070 */ 071 private static final long serialVersionUID = -3264450066845549348L; 072 073 074 075 /** 076 * Creates a new ASN.1 null element with the default BER type. 077 */ 078 public ASN1Null() 079 { 080 super(ASN1Constants.UNIVERSAL_NULL_TYPE); 081 } 082 083 084 085 /** 086 * Creates a new ASN.1 null element with the specified BER type. 087 * 088 * @param type The BER type to use for this ASN.1 null element. 089 */ 090 public ASN1Null(final byte type) 091 { 092 super(type); 093 } 094 095 096 097 /** 098 * Decodes the contents of the provided byte array as a null element. 099 * 100 * @param elementBytes The byte array to decode as an ASN.1 null element. 101 * 102 * @return The decoded ASN.1 null element. 103 * 104 * @throws ASN1Exception If the provided array cannot be decoded as a null 105 * element. 106 */ 107 @NotNull() 108 public static ASN1Null decodeAsNull(@NotNull final byte[] elementBytes) 109 throws ASN1Exception 110 { 111 try 112 { 113 int valueStartPos = 2; 114 int length = (elementBytes[1] & 0x7F); 115 if (length != elementBytes[1]) 116 { 117 final int numLengthBytes = length; 118 119 length = 0; 120 for (int i=0; i < numLengthBytes; i++) 121 { 122 length <<= 8; 123 length |= (elementBytes[valueStartPos++] & 0xFF); 124 } 125 } 126 127 if ((elementBytes.length - valueStartPos) != length) 128 { 129 throw new ASN1Exception(ERR_ELEMENT_LENGTH_MISMATCH.get(length, 130 (elementBytes.length - valueStartPos))); 131 } 132 133 if (length != 0) 134 { 135 throw new ASN1Exception(ERR_NULL_HAS_VALUE.get()); 136 } 137 138 return new ASN1Null(elementBytes[0]); 139 } 140 catch (final ASN1Exception ae) 141 { 142 Debug.debugException(ae); 143 throw ae; 144 } 145 catch (final Exception e) 146 { 147 Debug.debugException(e); 148 throw new ASN1Exception(ERR_ELEMENT_DECODE_EXCEPTION.get(e), e); 149 } 150 } 151 152 153 154 /** 155 * Decodes the provided ASN.1 element as a null element. 156 * 157 * @param element The ASN.1 element to be decoded. 158 * 159 * @return The decoded ASN.1 null element. 160 * 161 * @throws ASN1Exception If the provided element cannot be decoded as a null 162 * element. 163 */ 164 @NotNull() 165 public static ASN1Null decodeAsNull(@NotNull final ASN1Element element) 166 throws ASN1Exception 167 { 168 if (element.getValue().length != 0) 169 { 170 throw new ASN1Exception(ERR_NULL_HAS_VALUE.get()); 171 } 172 173 return new ASN1Null(element.getType()); 174 } 175 176 177 178 /** 179 * {@inheritDoc} 180 */ 181 @Override() 182 public void toString(@NotNull final StringBuilder buffer) 183 { 184 buffer.append("ASN1Null(type="); 185 StaticUtils.toHex(getType(), buffer); 186 buffer.append(')'); 187 } 188}