@Test public void testCreationDerConstructed() throws Exception { byte[] testValue = {0x00, 0x01, 0x02, 0x03, 0x04}; Asn1Object asn1Object = new Asn1Object(DerParser.NUMERIC_STRING | DER_CONSTRUCTED, 5, testValue); assertEquals(DerParser.NUMERIC_STRING, asn1Object.getType()); assertEquals(5, asn1Object.getLength()); assertEquals(testValue, asn1Object.getValue()); assertTrue(asn1Object.isConstructed()); }
if (sequence.getType() != DerParser.SEQUENCE) { throw new IllegalArgumentException("Invalid DER: not a sequence"); //$NON-NLS-1$ parser = sequence.getParser(); BigInteger modulus = parser.read().getInteger(); BigInteger publicExp = parser.read().getInteger(); BigInteger privateExp = parser.read().getInteger(); BigInteger prime1 = parser.read().getInteger(); BigInteger prime2 = parser.read().getInteger(); BigInteger exp1 = parser.read().getInteger(); BigInteger exp2 = parser.read().getInteger(); BigInteger crtCoef = parser.read().getInteger();
@Test(expected=IOException.class) public void testGetStringBad1() throws Exception { final String TEST_STRING = "my fancy test string"; Asn1Object asn1Object = new Asn1Object(DerParser.UNIVERSAL_STRING, TEST_STRING.length(), TEST_STRING.getBytes()); asn1Object.getString(); }
@Test(expected=IOException.class) public void testGetIntegerNotInteger() throws Exception { byte[] testValue = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; Asn1Object asn1Object = new Asn1Object(DerParser.GENERAL_STRING, 8, testValue); BigInteger testBigInteger = asn1Object.getInteger(); }
@Test(expected=IOException.class) public void testGetParserNotConstructed() throws Exception { byte[] testValue = {0x00, 0x01, 0x02, 0x03, 0x04}; Asn1Object asn1Object = new Asn1Object(DerParser.NUMERIC_STRING, 5, testValue); asn1Object.getParser(); }
/** * For constructed field, return a parser for its content. * * @return A parser for the construct. * @throws IOException if DER cannot be parsed. */ public DerParser getParser() throws IOException { if (!isConstructed()) { throw new IOException("Invalid DER: can't parse primitive entity"); //$NON-NLS-1$ } return new DerParser(value); }
/** * Read next object. If it's constructed, the value holds encoded content * and it should be parsed by a new parser from * <code>Asn1Object.getParser</code>. * * @return Asn1Object read * @throws IOException if DER parsing error (format). */ public Asn1Object read() throws IOException { int tag = in.read(); if (tag == -1) { throw new IOException("Invalid DER: stream too short, missing tag"); //$NON-NLS-1$ } int length = getLength(); byte[] value = new byte[length]; int n = in.read(value); if (n < length) { throw new IOException("Invalid DER: stream too short, missing value"); //$NON-NLS-1$ } Asn1Object o = new Asn1Object(tag, length, value); return o; }
@Test public void testGetParser() throws Exception { byte[] testValue = {0x00, 0x01, 0x02, 0x03, 0x04}; Asn1Object asn1Object = new Asn1Object(DerParser.NUMERIC_STRING | DER_CONSTRUCTED, 5, testValue); assertTrue(asn1Object.getParser() instanceof DerParser); }
@Test public void testGetInteger() throws Exception { byte[] testValue = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; Asn1Object asn1Object = new Asn1Object(DerParser.INTEGER, 8, testValue); BigInteger testBigInteger = asn1Object.getInteger(); assertTrue(Arrays.equals(testValue, testBigInteger.toByteArray())); }
/** * For constructed field, return a parser for its content. * * @return A parser for the construct. * @throws IOException if DER cannot be parsed. */ public DerParser getParser() throws IOException { if (!isConstructed()) { throw new IOException("Invalid DER: can't parse primitive entity"); //$NON-NLS-1$ } return new DerParser(value); }
/** * Read next object. If it's constructed, the value holds encoded content * and it should be parsed by a new parser from * <code>Asn1Object.getParser</code>. * * @return Asn1Object read * @throws IOException if DER parsing error (format). */ public Asn1Object read() throws IOException { int tag = in.read(); if (tag == -1) { throw new IOException("Invalid DER: stream too short, missing tag"); //$NON-NLS-1$ } int length = getLength(); byte[] value = new byte[length]; int n = in.read(value); if (n < length) { throw new IOException("Invalid DER: stream too short, missing value"); //$NON-NLS-1$ } Asn1Object o = new Asn1Object(tag, length, value); return o; }
@Test public void testCreationDerNotConstructed() throws Exception { byte[] testValue = {0x00, 0x01, 0x02, 0x03, 0x04}; Asn1Object asn1Object = new Asn1Object(DerParser.NUMERIC_STRING, 5, testValue); assertEquals(DerParser.NUMERIC_STRING, asn1Object.getType()); assertEquals(5, asn1Object.getLength()); assertEquals(testValue, asn1Object.getValue()); assertFalse(asn1Object.isConstructed()); }
if (sequence.getType() != DerParser.SEQUENCE) { throw new IllegalArgumentException("Invalid DER: not a sequence"); //$NON-NLS-1$ parser = sequence.getParser(); BigInteger modulus = parser.read().getInteger(); BigInteger publicExp = parser.read().getInteger(); BigInteger privateExp = parser.read().getInteger(); BigInteger prime1 = parser.read().getInteger(); BigInteger prime2 = parser.read().getInteger(); BigInteger exp1 = parser.read().getInteger(); BigInteger exp2 = parser.read().getInteger(); BigInteger crtCoef = parser.read().getInteger();
@Test(expected=IOException.class) public void testGetStringBad2() throws Exception { final String TEST_STRING = "my fancy test string"; Asn1Object asn1Object = new Asn1Object(DerParser.INTEGER, TEST_STRING.length(), TEST_STRING.getBytes()); asn1Object.getString(); } }
@Test public void testGetStringType1() throws Exception { final String TEST_STRING = "my fancy test string"; Asn1Object asn1Object = new Asn1Object(DerParser.GENERAL_STRING, TEST_STRING.length(), TEST_STRING.getBytes("ISO-8859-1")); assertEquals(TEST_STRING, asn1Object.getString()); }
@Test public void testGetStringType2() throws Exception { final String TEST_STRING = "my fancy test string"; Asn1Object asn1Object = new Asn1Object(DerParser.BMP_STRING, TEST_STRING.length(), TEST_STRING.getBytes("UTF-16BE")); assertEquals(TEST_STRING, asn1Object.getString()); }
@Test public void testGetStringType3() throws Exception { final String TEST_STRING = "my fancy test string"; Asn1Object asn1Object = new Asn1Object(DerParser.UTF8_STRING, TEST_STRING.length(), TEST_STRING.getBytes("UTF-8")); assertEquals(TEST_STRING, asn1Object.getString()); }