/** * Creates a new oid type. * * @param item to DER encode */ public OidType(final int[] item) { super(UniversalDERTag.OID); derItem = toBytes(item); }
/** * Converts bytes in the buffer to an OID by reading from the current position to the limit, which assumes the bytes * of the integer are in big-endian order. * * @param encoded buffer containing DER-encoded data where the buffer is positioned at the start of OID bytes and * the limit is set beyond the last byte of OID data. * * @return decoded bytes as an OID. */ public static String decode(final ByteBuffer encoded) { final ByteBuffer buffer = ByteBuffer.wrap(readBuffer(encoded)); final StringBuilder sb = new StringBuilder(); final int firstId = buffer.get(); // CheckStyle:MagicNumber OFF if (firstId < 40) { sb.append("0").append(".").append(firstId).append("."); } else if (firstId < 80) { sb.append("1").append(".").append(firstId - 40).append("."); } else { sb.append("2").append(".").append(firstId - 80).append("."); } // CheckStyle:MagicNumber ON while (buffer.hasRemaining()) { sb.append(readInt(buffer)).append("."); } sb.setLength(sb.length() - 1); return sb.toString(); }
/** * @param bytes to decode. * @param expected oid to compare. * * @throws Exception On test failure. */ @Test(groups = {"asn1"}, dataProvider = "oids") public void decode(final byte[] bytes, final String expected) throws Exception { Assert.assertEquals(OidType.decode(ByteBuffer.wrap(bytes)), expected); }
/** @param oid to encode. */ @Test(groups = {"asn1"}, dataProvider = "invalid-oids") public void invalid(final String oid) { try { new OidType(oid); } catch (Exception e) { AssertJUnit.assertEquals(IllegalArgumentException.class, e.getClass()); } } }
@Override public byte[] encode() { return encode(derItem); }
/** * Converts the supplied oid into an array on integers. * * @param oid to parse * * @return array of oid components * * @throws IllegalArgumentException if the oid is not valid. See {@link #isValid(int[])} */ public static int[] parse(final String oid) { if (oid == null) { throw new IllegalArgumentException("OID cannot be null"); } final StringTokenizer st = new StringTokenizer(oid, "."); final int[] oids = new int[st.countTokens()]; int i = 0; while (st.hasMoreTokens()) { try { oids[i++] = Integer.parseInt(st.nextToken()); } catch (NumberFormatException e) { throw new IllegalArgumentException(e); } } isValid(oids); return oids; } }
/** * Creates a new oid type. * * @param item to DER encode */ public OidType(final int[] item) { super(UniversalDERTag.OID); derItem = toBytes(item); }
/** * Converts bytes in the buffer to an OID by reading from the current position to the limit, which assumes the bytes * of the integer are in big-endian order. * * @param encoded buffer containing DER-encoded data where the buffer is positioned at the start of OID bytes and * the limit is set beyond the last byte of OID data. * * @return decoded bytes as an OID. */ public static String decode(final ByteBuffer encoded) { final ByteBuffer buffer = ByteBuffer.wrap(readBuffer(encoded)); final StringBuilder sb = new StringBuilder(); final int firstId = buffer.get(); // CheckStyle:MagicNumber OFF if (firstId < 40) { sb.append("0").append(".").append(firstId).append("."); } else if (firstId < 80) { sb.append("1").append(".").append(firstId - 40).append("."); } else { sb.append("2").append(".").append(firstId - 80).append("."); } // CheckStyle:MagicNumber ON while (buffer.hasRemaining()) { sb.append(readInt(buffer)).append("."); } sb.setLength(sb.length() - 1); return sb.toString(); }
@Override public void handle(final DERParser parser, final ByteBuffer encoded) { if (UniversalDERTag.OID.getTagNo() != parser.readTag(encoded).getTagNo()) { throw new IllegalArgumentException("Expected OID tag"); } final int seqLimit = encoded.limit(); final int oidLength = parser.readLength(encoded); encoded.limit(encoded.position() + oidLength); final String oid = OidType.decode(encoded); encoded.limit(seqLimit); final DERTag tag = parser.readTag(encoded); parser.readLength(encoded); assertions.add(new AttributeValueAssertion(oid, new Value(tag, readBuffer(encoded)))); } });
@Override public byte[] encode() { return encode(derItem); }
/** * Converts the supplied oid into an array on integers. * * @param oid to parse * * @return array of oid components * * @throws IllegalArgumentException if the oid is not valid. See {@link #isValid(int[])} */ public static int[] parse(final String oid) { if (oid == null) { throw new IllegalArgumentException("OID cannot be null"); } final StringTokenizer st = new StringTokenizer(oid, "."); final int[] oids = new int[st.countTokens()]; int i = 0; while (st.hasMoreTokens()) { try { oids[i++] = Integer.parseInt(st.nextToken()); } catch (NumberFormatException e) { throw new IllegalArgumentException(e); } } isValid(oids); return oids; } }
/** * Creates a new oid type. * * @param item to DER encode */ public OidType(final int[] item) { super(UniversalDERTag.OID); derItem = toBytes(item); }
/** * Converts bytes in the buffer to an OID by reading from the current position to the limit, which assumes the bytes * of the integer are in big-endian order. * * @param encoded buffer containing DER-encoded data where the buffer is positioned at the start of OID bytes and * the limit is set beyond the last byte of OID data. * * @return decoded bytes as an OID. */ public static String decode(final ByteBuffer encoded) { final ByteBuffer buffer = ByteBuffer.wrap(readBuffer(encoded)); final StringBuilder sb = new StringBuilder(); final int firstId = buffer.get(); // CheckStyle:MagicNumber OFF if (firstId < 40) { sb.append("0").append(".").append(firstId).append("."); } else if (firstId < 80) { sb.append("1").append(".").append(firstId - 40).append("."); } else { sb.append("2").append(".").append(firstId - 80).append("."); } // CheckStyle:MagicNumber ON while (buffer.hasRemaining()) { sb.append(readInt(buffer)).append("."); } sb.setLength(sb.length() - 1); return sb.toString(); }