private int validateFirstOIDComponent(long value) throws ASN1Exception { if (value < 0 || value > 2) { throw log.asnInvalidValueForFirstOidComponent(); } return (int) value; }
private void validateSecondOIDComponent(long second, int first) throws ASN1Exception { if ((first < 2) && (second > 39)) { throw log.asnInvalidValueForSecondOidComponent(); } }
private int readLength() throws ASN1Exception { try { int length = bi.next(); if (length > 127) { // Long form int numOctets = length & 0x7f; if (numOctets > 4) { throw log.asnLengthEncodingExceeds4bytes(); } length = 0; int nextOctet; for (int i = 0; i < numOctets; i++) { nextOctet = bi.next(); length = (length << 8) + nextOctet; } } return length; } catch (NoSuchElementException e) { throw log.asnUnexpectedEndOfInput(); } }
@Override public boolean decodeBoolean() throws ASN1Exception { readTag(BOOLEAN_TYPE); int length = readLength(); if (length != 1) { throw log.asnInvalidLengthForBooleanTypeTag(); } if (! bi.hasNext()) { throw log.asnUnexpectedEndOfInput(); } return bi.next() != BOOLEAN_FALSE; }
private int readTag() throws ASN1Exception { try { int tag = bi.next(); int tagClass = tag & CLASS_MASK; int constructed = tag & CONSTRUCTED_MASK; int tagNumber = tag & TAG_NUMBER_MASK; if (tagNumber == 0x1f) { // High-tag-number form tagNumber = 0; int octet = bi.next(); if ((octet & 0x7f) == 0) { // Bits 7 to 1 of the first subsequent octet cannot be 0 throw log.asnInvalidHighTagNumberForm(); } while ((octet >= 0) && ((octet & 0x80) != 0)) { tagNumber |= (octet & 0x7f); tagNumber <<= 7; octet = bi.next(); } tagNumber |= (octet & 0x7f); } return (tagClass | constructed | tagNumber); } catch (NoSuchElementException e) { throw log.asnUnexpectedEndOfInput(); } }
@Override public void encodeObjectIdentifier(String objectIdentifier) throws ASN1Exception { if (objectIdentifier == null || objectIdentifier.length() == 0) { throw log.asnOidMustHaveAtLeast2Components(); continue a; } else { throw log.asnInvalidOidCharacter(); throw log.asnOidMustHaveAtLeast2Components(); t = 0L; } else { throw log.asnInvalidOidCharacter(); throw log.asnInvalidOidCharacter(); throw log.asnOidMustHaveAtLeast2Components();
@Override public String decodeBitStringAsString() throws ASN1Exception { readTag(BIT_STRING_TYPE); int length = readLength(); int numUnusedBits = bi.next(); if (numUnusedBits < 0 || numUnusedBits > 7) { throw log.asnInvalidNumberOfUnusedBits(); } int k = 0, next; int numBits = (length - 1) * 8 - numUnusedBits; StringBuilder result = new StringBuilder(numBits); for (int i = 0; i < (length - 1); i++) { next = bi.next(); for (int j = 7; j >= 0 && k < numBits; j--) { if ((next & (1 << j)) != 0) { result.append("1"); } else { result.append("0"); } k += 1; } } return result.toString(); }
@Override public void decodeNull() throws ASN1Exception { readTag(NULL_TYPE); int length = readLength(); if (length != 0) { throw log.asnNonZeroLengthForNullTypeTag(); } }
private int writeLength(int length, ByteStringBuilder dest) throws ASN1Exception { int numLengthOctets; if (length < 0) { throw log.asnInvalidLength(); } else if (length <= 127) { // Short form numLengthOctets = 1; } else { // Long form numLengthOctets = 1; int value = length; while ((value >>>= 8) != 0) { numLengthOctets += 1; } } if (length > 127) { // bit 8 of the first octet has value "1" and bits 7-1 give the number of additional length octets dest.append((byte) (numLengthOctets | 0x80)); } for (int i = (numLengthOctets - 1) * 8; i >= 0; i -= 8) { dest.append((byte) (length >> i)); } if (length > 127) { // include the first octet return 1 + numLengthOctets; } else { return numLengthOctets; } }
@Override public byte[] decodeBitString() throws ASN1Exception { readTag(BIT_STRING_TYPE); int length = readLength(); byte[] result = new byte[length - 1]; int numUnusedBits = bi.next(); if (numUnusedBits < 0 || numUnusedBits > 7) { throw log.asnInvalidNumberOfUnusedBits(); } if (numUnusedBits == 0) { for (int i = 0; i < (length -1); i++) { result[i] = (byte) bi.next(); } } else { // Any unused bits will be removed int leftShift = 8 - numUnusedBits; int previous = 0; int next; for (int i = 0; i < (length -1); i++) { next = bi.next(); if (i == 0) { result[i] = (byte) (next >>> numUnusedBits); } else { result[i] = (byte) ((next >>> numUnusedBits) | (previous << leftShift)); } previous = next; } } return result; }
private int validateFirstOIDComponent(BigInteger value) throws ASN1Exception { if ((value.compareTo(BigInteger.valueOf(0)) == -1) || (value.compareTo(BigInteger.valueOf(2)) == 1)) { throw log.asnInvalidValueForFirstOidComponent(); } return value.intValue(); }
private void validateSecondOIDComponent(BigInteger second, int first) throws ASN1Exception { if ((first < 2) && (second.compareTo(BigInteger.valueOf(39)) == 1)) { throw log.asnInvalidValueForSecondOidComponent(); } }