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 java.math.BigInteger; 041 042import com.unboundid.util.Debug; 043import com.unboundid.util.NotMutable; 044import com.unboundid.util.NotNull; 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 integer element that is backed by a Java 054 * {@code BigInteger} and whose value can be represented as an integer of any 055 * magnitude. For an ASN.1 integer implementation that is backed by a signed 056 * 32-bit {@code int}, see {@link ASN1Integer}. For an implementation that is 057 * backed by a signed 64-bit {@code long}, see {@link ASN1Long}. 058 */ 059@NotMutable() 060@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 061public final class ASN1BigInteger 062 extends ASN1Element 063{ 064 /** 065 * The serial version UID for this serializable class. 066 */ 067 private static final long serialVersionUID = 2631806934961821260L; 068 069 070 071 // The BigInteger value for this element. 072 @NotNull private final BigInteger value; 073 074 075 076 /** 077 * Creates a new ASN.1 big integer element with the default BER type and the 078 * provided value. 079 * 080 * @param value The value to use for this element. It must not be 081 * {@code null}. 082 */ 083 public ASN1BigInteger(@NotNull final BigInteger value) 084 { 085 this(ASN1Constants.UNIVERSAL_INTEGER_TYPE, value); 086 } 087 088 089 090 /** 091 * Creates a new ASN.1 big integer element with the specified BER type and the 092 * provided value. 093 * 094 * @param type The BER type to use for this element. 095 * @param value The value to use for this element. It must not be 096 * {@code null}. 097 */ 098 public ASN1BigInteger(final byte type, @NotNull final BigInteger value) 099 { 100 super(type, value.toByteArray()); 101 102 this.value = value; 103 } 104 105 106 107 /** 108 * Creates a new ASN.1 big integer element with the specified BER type and the 109 * provided value. 110 * 111 * @param type The BER type to use for this element. 112 * @param bigIntegerValue The value to use for this element. It must not be 113 * {@code null}. 114 * @param berValue The encoded BER value for this element. It must 115 * not be {@code null} or empty. 116 */ 117 private ASN1BigInteger(final byte type, 118 @NotNull final BigInteger bigIntegerValue, 119 @NotNull final byte[] berValue) 120 { 121 super(type, berValue); 122 value = bigIntegerValue; 123 } 124 125 126 127 /** 128 * Creates a new ASN.1 big integer element with the default BER type and the 129 * provided long value. 130 * 131 * @param value The int value to use for this element. 132 */ 133 public ASN1BigInteger(final long value) 134 { 135 this(ASN1Constants.UNIVERSAL_INTEGER_TYPE, BigInteger.valueOf(value)); 136 } 137 138 139 140 /** 141 * Creates a new ASN.1 big integer element with the specified BER type and the 142 * provided long value. 143 * 144 * @param type The BER type to use for this element. 145 * @param value The int value to use for this element. 146 */ 147 public ASN1BigInteger(final byte type, final long value) 148 { 149 this(type, BigInteger.valueOf(value)); 150 } 151 152 153 154 /** 155 * Retrieves the value for this element as a Java {@code BigInteger}. 156 * 157 * @return The value for this element as a Java {@code BigInteger}. 158 */ 159 @NotNull() 160 public BigInteger getBigIntegerValue() 161 { 162 return value; 163 } 164 165 166 167 /** 168 * Decodes the contents of the provided byte array as a big integer element. 169 * 170 * @param elementBytes The byte array to decode as an ASN.1 big integer 171 * element. 172 * 173 * @return The decoded ASN.1 big integer element. 174 * 175 * @throws ASN1Exception If the provided array cannot be decoded as a big 176 * integer element. 177 */ 178 @NotNull() 179 public static ASN1BigInteger decodeAsBigInteger( 180 @NotNull final byte[] elementBytes) 181 throws ASN1Exception 182 { 183 try 184 { 185 int valueStartPos = 2; 186 int length = (elementBytes[1] & 0x7F); 187 if (length != elementBytes[1]) 188 { 189 final int numLengthBytes = length; 190 191 length = 0; 192 for (int i=0; i < numLengthBytes; i++) 193 { 194 length <<= 8; 195 length |= (elementBytes[valueStartPos++] & 0xFF); 196 } 197 } 198 199 if ((elementBytes.length - valueStartPos) != length) 200 { 201 throw new ASN1Exception(ERR_ELEMENT_LENGTH_MISMATCH.get(length, 202 (elementBytes.length - valueStartPos))); 203 } 204 205 if (length == 0) 206 { 207 throw new ASN1Exception(ERR_BIG_INTEGER_DECODE_EMPTY_VALUE.get()); 208 } 209 210 final byte[] elementValue = new byte[length]; 211 System.arraycopy(elementBytes, valueStartPos, elementValue, 0, length); 212 213 final BigInteger bigIntegerValue = new BigInteger(elementValue); 214 return new ASN1BigInteger(elementBytes[0], bigIntegerValue, elementValue); 215 } 216 catch (final ASN1Exception ae) 217 { 218 Debug.debugException(ae); 219 throw ae; 220 } 221 catch (final Exception e) 222 { 223 Debug.debugException(e); 224 throw new ASN1Exception(ERR_ELEMENT_DECODE_EXCEPTION.get(e), e); 225 } 226 } 227 228 229 230 /** 231 * Decodes the provided ASN.1 element as a big integer element. 232 * 233 * @param element The ASN.1 element to be decoded. 234 * 235 * @return The decoded ASN.1 big integer element. 236 * 237 * @throws ASN1Exception If the provided element cannot be decoded as a big 238 * integer element. 239 */ 240 @NotNull() 241 public static ASN1BigInteger decodeAsBigInteger( 242 @NotNull final ASN1Element element) 243 throws ASN1Exception 244 { 245 final byte[] value = element.getValue(); 246 if (value.length == 0) 247 { 248 throw new ASN1Exception(ERR_BIG_INTEGER_DECODE_EMPTY_VALUE.get()); 249 } 250 251 return new ASN1BigInteger(element.getType(), new BigInteger(value), value); 252 } 253 254 255 256 /** 257 * {@inheritDoc} 258 */ 259 @Override() 260 public void toString(@NotNull final StringBuilder buffer) 261 { 262 buffer.append(value); 263 } 264}