Return-Path: X-Original-To: apmail-directory-commits-archive@www.apache.org Delivered-To: apmail-directory-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id F35321801E for ; Sat, 16 Jan 2016 09:38:29 +0000 (UTC) Received: (qmail 81046 invoked by uid 500); 16 Jan 2016 09:38:29 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 80927 invoked by uid 500); 16 Jan 2016 09:38:29 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 80351 invoked by uid 99); 16 Jan 2016 09:38:29 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 16 Jan 2016 09:38:29 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 423D0E0B4C; Sat, 16 Jan 2016 09:38:29 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: drankye@apache.org To: commits@directory.apache.org Date: Sat, 16 Jan 2016 09:38:38 -0000 Message-Id: <0313b06048a846ffa8969d87c47f4667@git.apache.org> In-Reply-To: <8acd522df7754e07bcb4c2ee1596d918@git.apache.org> References: <8acd522df7754e07bcb4c2ee1596d918@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [10/13] directory-kerby git commit: Consolidated facility and support modules into kerby-common http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/HexUtil.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/HexUtil.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/HexUtil.java deleted file mode 100644 index 66a0ee8..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/HexUtil.java +++ /dev/null @@ -1,113 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1.util; - -/** - * This is only for test, be careful when use in production codes. - */ -public class HexUtil { - - static final String HEX_CHARS_STR = "0123456789ABCDEF"; - static final char[] HEX_CHARS = HEX_CHARS_STR.toCharArray(); - - /** - * Convert bytes into friendly format as: - * 0x02 02 00 80 - */ - public static String bytesToHexFriendly(byte[] bytes) { - int len = bytes.length * 2; - len += bytes.length; // for ' ' appended for each char - len += 2; // for '0x' prefix - char[] hexChars = new char[len]; - hexChars[0] = '0'; - hexChars[1] = 'x'; - for (int j = 0; j < bytes.length; j++) { - int v = bytes[j] & 0xFF; - hexChars[j * 3 + 2] = HEX_CHARS[v >>> 4]; - hexChars[j * 3 + 3] = HEX_CHARS[v & 0x0F]; - hexChars[j * 3 + 4] = ' '; - } - - return new String(hexChars); - } - - /** - * Convert friendly hex string like follows into byte array - * 0x02 02 00 80 - */ - public static byte[] hex2bytesFriendly(String hexString) { - hexString = hexString.toUpperCase(); - String hexStr = hexString; - if (hexString.startsWith("0X")) { - hexStr = hexString.substring(2); - } - String[] hexParts = hexStr.split(" "); - - byte[] bytes = new byte[hexParts.length]; - char[] hexPart; - for (int i = 0; i < hexParts.length; ++i) { - hexPart = hexParts[i].toCharArray(); - if (hexPart.length != 2) { - throw new IllegalArgumentException("Invalid hex string to convert"); - } - bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexPart[0]) << 4) - + HEX_CHARS_STR.indexOf(hexPart[1])); - } - - return bytes; - } - - /** - * Convert bytes into format as: - * 02020080 - * @param bytes The bytes - * @return The hex string - */ - public static String bytesToHex(byte[] bytes) { - int len = bytes.length * 2; - char[] hexChars = new char[len]; - for (int j = 0; j < bytes.length; j++) { - int v = bytes[j] & 0xFF; - hexChars[j * 2] = HEX_CHARS[v >>> 4]; - hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F]; - } - - return new String(hexChars); - } - - /** - * Convert hex string like follows into byte array - * 02020080 - * @param hexString The hex string - * @return The bytes - */ - public static byte[] hex2bytes(String hexString) { - hexString = hexString.toUpperCase(); - int len = hexString.length() / 2; - byte[] bytes = new byte[len]; - char[] hexChars = hexString.toCharArray(); - for (int i = 0, j = 0; i < len; ++i) { - bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexChars[j++]) << 4) - + HEX_CHARS_STR.indexOf(hexChars[j++])); - } - - return bytes; - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/IOUtil.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/IOUtil.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/IOUtil.java deleted file mode 100644 index 02d1531..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/IOUtil.java +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1.util; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; - -/** - * Some IO and file related utilities. - */ -public final class IOUtil { - private IOUtil() { } - - public static byte[] readInputStream(InputStream in) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int length = 0; - while ((length = in.read(buffer)) != -1) { - baos.write(buffer, 0, length); - } - in.close(); - return baos.toByteArray(); - } - - public static void readInputStream(InputStream in, - byte[] buf) throws IOException { - int toRead = buf.length; - int off = 0; - while (toRead > 0) { - int ret = in.read(buf, off, toRead); - if (ret < 0) { - throw new IOException("Bad inputStream, premature EOF"); - } - toRead -= ret; - off += ret; - } - in.close(); - } - - /** - * Read an input stream and return the content as string assuming UTF8. - * @param in The input stream - * @return The content - * @throws IOException e - */ - public static String readInput(InputStream in) throws IOException { - byte[] content = readInputStream(in); - return Utf8.toString(content); - } - - /** - * Read a file and return the content as string assuming UTF8. - * @param file The file to read - * @return The content - * @throws IOException e - */ - public static String readFile(File file) throws IOException { - long len = 0; - if (file.length() >= Integer.MAX_VALUE) { - throw new IOException("Too large file, unexpected!"); - } else { - len = file.length(); - } - byte[] buf = new byte[(int) len]; - - InputStream is = new FileInputStream(file); - readInputStream(is, buf); - - return Utf8.toString(buf); - } - - /** - * Write a file with the content assuming UTF8. - * @param content The content - * @param file The file to write - * @throws IOException e - */ - public static void writeFile(String content, File file) throws IOException { - FileOutputStream outputStream = new FileOutputStream(file); - FileChannel fc = outputStream.getChannel(); - - ByteBuffer buffer = ByteBuffer.wrap(Utf8.toBytes(content)); - fc.write(buffer); - outputStream.close(); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/Utf8.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/Utf8.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/Utf8.java deleted file mode 100644 index d24b12b..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/Utf8.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1.util; - -import java.nio.charset.StandardCharsets; - -public final class Utf8 { - private Utf8() { } - - public static String toString(byte[] bytes) { - return new String(bytes, StandardCharsets.UTF_8); - } - - public static byte[] toBytes(String s) { - return s.getBytes(StandardCharsets.UTF_8); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1BooleanTest.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1BooleanTest.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1BooleanTest.java deleted file mode 100644 index 5557975..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1BooleanTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.apache.kerby.asn1.type.Asn1Boolean; -import org.apache.kerby.asn1.util.HexUtil; -import org.junit.Test; - -import java.io.IOException; - -import static org.assertj.core.api.Assertions.assertThat; - -public class Asn1BooleanTest { - - @Test - public void testEncoding() throws IOException { - testEncodingWith(true, "0x01 01 FF", true); - testEncodingWith(false, "0x01 01 00", true); - } - - private void testEncodingWith(Boolean value, String expectedEncoding, - boolean isDer) throws IOException { - byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding); - Asn1Boolean aValue = new Asn1Boolean(value); - if (isDer) { - aValue.useDER(); - } else { - aValue.useBER(); - } - byte[] encodingBytes = aValue.encode(); - assertThat(encodingBytes).isEqualTo(expected); - } - - @Test - public void testDecoding() throws IOException { - testDecodingWith(true, "0x01 01 FF", true); - testDecodingWith(false, "0x01 01 7F", true); - testDecodingWith(true, "0x01 01 7F", false); - testDecodingWith(false, "0x01 01 00", true); - } - - private void testDecodingWith(Boolean expectedValue, String content, - boolean isDer) throws IOException { - Asn1Boolean decoded = new Asn1Boolean(); - if (isDer) { - decoded.useDER(); - } else { - decoded.useBER(); - } - decoded.decode(HexUtil.hex2bytesFriendly(content)); - assertThat(decoded.getValue()).isEqualTo(expectedValue); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1CollectionTest.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1CollectionTest.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1CollectionTest.java deleted file mode 100644 index 50d51af..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1CollectionTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.apache.kerby.asn1.type.Asn1Boolean; -import org.apache.kerby.asn1.type.Asn1IA5String; -import org.apache.kerby.asn1.type.Asn1Sequence; -import org.apache.kerby.asn1.type.Asn1String; -import org.junit.Test; - -import java.io.IOException; - -import static org.assertj.core.api.Assertions.assertThat; - -public class Asn1CollectionTest { - static final String TEST_STR = "Jones"; - static final Boolean TEST_BOOL = true; - static final byte[] EXPECTED_BYTES = new byte[] {(byte) 0x30, (byte) 0x0A, - (byte) 0x16, (byte) 0x05, (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, - (byte) 0x65, (byte) 0x73, (byte) 0x01, (byte) 0x01, (byte) 0xFF - }; - - @Test - public void testSequenceEncoding() throws IOException { - Asn1Sequence seq = new Asn1Sequence(); - seq.addItem(new Asn1IA5String(TEST_STR)); - seq.addItem(new Asn1Boolean(TEST_BOOL)); - - assertThat(seq.encode()).isEqualTo(EXPECTED_BYTES); - } - - @Test - public void testSequenceDecoding() throws IOException { - Asn1Sequence seq = new Asn1Sequence(); - seq.decode(EXPECTED_BYTES); - Asn1String field = - (Asn1String) seq.getValue().get(0); - assertThat(field.getValue()).isEqualTo(TEST_STR); - - Asn1Boolean field2 = (Asn1Boolean) seq.getValue().get(1); - assertThat(field2.getValue()).isEqualTo(TEST_BOOL); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1ConstructedOctetStringTest.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1ConstructedOctetStringTest.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1ConstructedOctetStringTest.java deleted file mode 100644 index 7b110a9..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1ConstructedOctetStringTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.apache.kerby.asn1.type.Asn1OctetString; -import org.junit.Test; - -import java.io.IOException; - -public class Asn1ConstructedOctetStringTest { - - @Test - public void testDecoding() throws IOException { - byte[] data = TestUtil.readBytesFromTxtFile("/constructed-octet-string.txt"); - Asn1OctetString octetString = new Asn1OctetString(); - octetString.decode(data); - Asn1.dump(octetString); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1DumpTest.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1DumpTest.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1DumpTest.java deleted file mode 100644 index fb83fc0..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1DumpTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; - -public class Asn1DumpTest { - - @Test - @org.junit.Ignore - public void testDumpWithPersonnelRecord() throws IOException { - try { - PersonnelRecord pr = DataTest.createSamplePersonnel(); - Asn1.dump(pr); - - byte[] data = DataTest.createSammplePersonnelEncodingData(); - Asn1.parseAndDump(data); - Asn1.decodeAndDump(data); - } catch (Exception e) { - e.printStackTrace(); - Assert.fail(); - } - } - - @Test - @org.junit.Ignore - public void testDumpWithCompressedData() throws IOException { - String hexStr = TestUtil.readStringFromTxtFile("/compressed-data.txt"); - try { - Asn1.parseAndDump(hexStr); - Asn1.decodeAndDump(hexStr); - } catch (Exception e) { - e.printStackTrace(); - Assert.fail(); - } - } - - @Test - @org.junit.Ignore - public void testDumpWithSignedData() throws IOException { - String hexStr = TestUtil.readStringFromTxtFile("/signed-data.txt"); - try { - Asn1.parseAndDump(hexStr); - Asn1.decodeAndDump(hexStr); - } catch (Exception e) { - e.printStackTrace(); - Assert.fail(); - } - } - - @Test - @org.junit.Ignore - public void testDumpWithDerData() throws IOException { - byte[] data = TestUtil.readBytesFromBinFile("/der-data.dat"); - try { - Asn1.parseAndDump(data); - Asn1.decodeAndDump(data); - } catch (Exception e) { - e.printStackTrace(); - Assert.fail(); - } - } - - @Test - public void testDumpWithEmptyContainer() throws IOException { - byte[] data = TestUtil.readBytesFromTxtFile("/empty-container.txt"); - try { - Asn1.parseAndDump(data); - Asn1.decodeAndDump(data); - } catch (Exception e) { - e.printStackTrace(); - Assert.fail(); - } - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1FlagsTest.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1FlagsTest.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1FlagsTest.java deleted file mode 100644 index 3182811..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1FlagsTest.java +++ /dev/null @@ -1,158 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.apache.kerby.asn1.type.Asn1Flags; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import java.io.IOException; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class Asn1FlagsTest { - - public static final int FLAG_0 = 0b00000000000000000000000000000001; - public static final int FLAG_1 = 0b00000000000000000000000000000010; - public static final int FLAG_2 = 0x00000004; - public static final int FLAG_3 = 0x00000008; - public static final int FLAG_4 = 16; - public static final int FLAG_5 = 32; - - public enum TestEnum implements EnumType { - FLAG_0(0x00000001), - FLAG_1(0x00000002), - FLAG_2(0x00000004), - FLAG_3(0x00000008), - FLAG_4(0x00000010), - FLAG_5(0x00000020); - - private int value; - - private TestEnum(int value) { - this.value = value; - } - - @Override - public int getValue() { - return value; - } - - - @Override - public String getName() { - return name(); - } - } - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - private Asn1Flags flags; - - @Before - public void setUp() { - flags = new Asn1Flags(FLAG_5 | FLAG_3 | FLAG_1); - } - - @Test - public void testToValue() throws IOException { - byte[] value = {(byte) 0xDE, (byte) 0xAD, (byte) 0xBE, (byte) 0xEF}; - flags.setValue(value); - assertEquals(0b11011110101011011011111011101111, flags.getFlags()); - } - - @Test - public void testFlags() { - flags = new Asn1Flags(); - assertEquals(0b00000000000000000000000000000000, flags.getFlags()); - } - - @Test - public void testFlagsInt() { - flags = new Asn1Flags(FLAG_4 | FLAG_2 | FLAG_0); - assertEquals(0b00000000000000000000000000010101, flags.getFlags()); - } - - @Test - public void testSetFlags() { - flags.setFlags(FLAG_4 | FLAG_2 | FLAG_0); - assertEquals(0b00000000000000000000000000010101, flags.getFlags()); - } - - @Test - public void testGetFlags() { - assertEquals(0b00000000000000000000000000101010, flags.getFlags()); - } - - @Test - public void testIsFlagSetInt() { - assertTrue(flags.isFlagSet(FLAG_5)); - assertFalse(flags.isFlagSet(FLAG_4)); - } - - @Test - public void testSetFlagInt() { - flags.setFlag(FLAG_4); - assertEquals(0b00000000000000000000000000111010, flags.getFlags()); - } - - @Test - public void testClearFlagInt() { - flags.clearFlag(FLAG_3); - assertEquals(0b00000000000000000000000000100010, flags.getFlags()); - } - - @Test - public void testClear() { - flags.clear(); - assertEquals(0b00000000000000000000000000000000, flags.getFlags()); - } - - @Test - public void testIsFlagSetEnum() { - assertTrue(flags.isFlagSet(TestEnum.FLAG_5)); - assertFalse(flags.isFlagSet(TestEnum.FLAG_4)); - } - - @Test - public void testSetFlagEnum() { - flags.setFlag(TestEnum.FLAG_4); - assertEquals(0b00000000000000000000000000111010, flags.getFlags()); - } - - @Test - public void testSetFlagEnumBoolean() { - flags.setFlag(TestEnum.FLAG_4, true); - assertEquals(0b00000000000000000000000000111010, flags.getFlags()); - flags.setFlag(TestEnum.FLAG_4, false); - assertEquals(0b00000000000000000000000000101010, flags.getFlags()); - } - - @Test - public void testClearFlagEnum() { - flags.clearFlag(TestEnum.FLAG_3); - assertEquals(0b00000000000000000000000000100010, flags.getFlags()); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1IntegerTest.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1IntegerTest.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1IntegerTest.java deleted file mode 100644 index 04bb11f..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1IntegerTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.apache.kerby.asn1.type.Asn1Integer; -import org.apache.kerby.asn1.util.HexUtil; -import org.junit.Test; - -import java.io.IOException; - -import static org.assertj.core.api.Assertions.assertThat; - -public class Asn1IntegerTest { - - @Test - public void testEncoding() throws IOException { - testEncodingWith(0, "0x02 01 00"); - testEncodingWith(1, "0x02 01 01"); - testEncodingWith(2, "0x02 01 02"); - testEncodingWith(127, "0x02 01 7F"); - testEncodingWith(128, "0x02 02 00 80"); - testEncodingWith(-1, "0x02 01 FF"); - testEncodingWith(-128, "0x02 01 80"); - testEncodingWith(-32768, "0x02 02 80 00"); - testEncodingWith(1234567890, "0x02 04 49 96 02 D2"); - } - - private void testEncodingWith(int value, String expectedEncoding) throws IOException { - byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding); - Asn1Integer aValue = new Asn1Integer(value); - aValue.useDER(); - byte[] encodingBytes = aValue.encode(); - assertThat(encodingBytes).isEqualTo(expected); - } - - @Test - public void testDecoding() throws IOException { - testDecodingWith(0, "0x02 01 00"); - testDecodingWith(1, "0x02 01 01"); - testDecodingWith(2, "0x02 01 02"); - testDecodingWith(127, "0x02 01 7F"); - testDecodingWith(128, "0x02 02 00 80"); - testDecodingWith(-1, "0x02 01 FF"); - testDecodingWith(-128, "0x02 01 80"); - testDecodingWith(-32768, "0x02 02 80 00"); - testDecodingWith(1234567890, "0x02 04 49 96 02 D2"); - } - - private void testDecodingWith(int expectedValue, String content) throws IOException { - Asn1Integer decoded = new Asn1Integer(); - decoded.useDER(); - decoded.decode(HexUtil.hex2bytesFriendly(content)); - assertThat(decoded.getValue().intValue()).isEqualTo(expectedValue); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1ObjectIdentifierTest.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1ObjectIdentifierTest.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1ObjectIdentifierTest.java deleted file mode 100644 index 9106dc7..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1ObjectIdentifierTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.apache.kerby.asn1.type.Asn1ObjectIdentifier; -import org.apache.kerby.asn1.util.HexUtil; -import org.junit.Test; - -import java.io.IOException; - -import static org.assertj.core.api.Assertions.assertThat; - -public class Asn1ObjectIdentifierTest { - - @Test - public void testEncoding() throws Exception { - /** - * Cryptography for Developers -> ASN.1 UTCTIME Type - * Applying this to the MD5 OID, we first transform the dotted decimal form into the - * array of words.Thus, 1.2.840.113549.2.5 becomes {42, 840, 113549, 2, 5}, and then further - * 404_CRYPTO_02.qxd 10/27/06 3:40 PM Page 36split into seven-bit digits with the proper most significant bits as - * {{0x2A}, {0x86, 0x48},{0x86, 0xF7, 0x0D}, {0x02}, {0x05}}.Therefore, the full encoding for MD5 is 0x06 08 2A - * 86 48 86 F7 0D 02 05. - */ - testEncodingWith("1.2.840.113549.2.5", - "0x06 08 2A 86 48 86 F7 0D 02 05"); - } - - private void testEncodingWith(String oid, String expectedEncoding) throws IOException { - byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding); - Asn1ObjectIdentifier aValue = new Asn1ObjectIdentifier(oid); - aValue.useDER(); - byte[] encodingBytes = aValue.encode(); - assertThat(encodingBytes).isEqualTo(expected); - } - - @Test - public void testDecoding() throws Exception { - testDecodingWith("1.2.840.113549.2.5", - "0x06 08 2A 86 48 86 F7 0D 02 05"); - } - - private void testDecodingWith(String expectedValue, String content) throws IOException { - Asn1ObjectIdentifier decoded = new Asn1ObjectIdentifier(); - decoded.useDER(); - decoded.decode(HexUtil.hex2bytesFriendly(content)); - assertThat(decoded.getValue()).isEqualTo(expectedValue); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1UtcTimeTest.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1UtcTimeTest.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1UtcTimeTest.java deleted file mode 100644 index 271abff..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1UtcTimeTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.apache.kerby.asn1.type.Asn1UtcTime; -import org.apache.kerby.asn1.util.HexUtil; -import org.junit.Test; - -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.SimpleTimeZone; - -import static org.assertj.core.api.Assertions.assertThat; - -public class Asn1UtcTimeTest { - - @Test - public void testEncoding() throws Exception { - /** - * Cryptography for Developers -> ASN.1 UTCTIME Type - * the encoding of July 4, 2003 at 11:33 and 28 seconds would be - “030704113328Z” and be encoded as 0x17 0D 30 33 30 37 30 34 31 31 33 33 32 38 5A. - */ - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - sdf.setTimeZone(new SimpleTimeZone(0, "Z")); - String dateInString = "2003-07-04 11:33:28"; - Date date = sdf.parse(dateInString); - testEncodingWith(date, "0x17 0D 30 33 30 37 30 34 31 31 33 33 32 38 5A"); - } - - private void testEncodingWith(Date value, String expectedEncoding) throws IOException { - byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding); - Asn1UtcTime aValue = new Asn1UtcTime(value); - aValue.useDER(); - byte[] encodingBytes = aValue.encode(); - assertThat(encodingBytes).isEqualTo(expected); - } - - @Test - public void testDecoding() throws Exception { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String dateInString = "2003-07-04 11:33:28"; - sdf.setTimeZone(new SimpleTimeZone(0, "Z")); - Date date = sdf.parse(dateInString); - testDecodingWith(date, "0x17 0D 30 33 30 37 30 34 31 31 33 33 32 38 5A"); - } - - private void testDecodingWith(Date expectedValue, String content) throws IOException { - Asn1UtcTime decoded = new Asn1UtcTime(); - decoded.useDER(); - decoded.decode(HexUtil.hex2bytesFriendly(content)); - assertThat(decoded.getValue()).isEqualTo(expectedValue); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/DataTest.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/DataTest.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/DataTest.java deleted file mode 100644 index 4e5c0a5..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/DataTest.java +++ /dev/null @@ -1,127 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; - -public class DataTest { - - public static PersonnelRecord createSamplePersonnel() { - PersonnelRecord pr = new PersonnelRecord(); - - pr.setName(new PersonnelRecord.Name("John", "P", "Smith")); - - pr.setTitle("Director"); - - pr.setEmployeeNumber(new PersonnelRecord.EmployeeNumber(51)); - - pr.setDateOfHire(new PersonnelRecord.Date("19710917")); - - pr.setNameOfSpouse(new PersonnelRecord.Name("Mary", "T", "Smith")); - - PersonnelRecord.ChildInformation child1 = new PersonnelRecord.ChildInformation(); - child1.setName(new PersonnelRecord.Name("Ralph", "T", "Smith")); - child1.setDateOfBirth(new PersonnelRecord.Date("19571111")); - - PersonnelRecord.ChildInformation child2 = new PersonnelRecord.ChildInformation(); - child2.setName(new PersonnelRecord.Name("Susan", "B", "Jones")); - child2.setDateOfBirth(new PersonnelRecord.Date("19590717")); - - pr.setChildren(new PersonnelRecord.Children(child1, child2)); - - return pr; - } - - public static byte[] createSammplePersonnelEncodingData() { - class BufferOutput { - ByteBuffer buffer; - - void put(byte ... bytes) { - buffer.put(bytes); - } - - void put(String s) { - byte[] bytes = s.getBytes(StandardCharsets.US_ASCII); - buffer.put(bytes); - } - - public byte[] output() { - int len = (int) 0x85 + 3; - buffer = ByteBuffer.allocate(len); - - // personnel record - put((byte) 0x60, (byte) 0x81, (byte) 0x85); - - // -name - put((byte) 0x61, (byte) 0x10); - put((byte) 0x1A, (byte) 0x04); put("John"); - put((byte) 0x1A, (byte) 0x01); put("P"); - put((byte) 0x1A, (byte) 0x05); put("Smith"); - - //-title - put((byte) 0xA0, (byte) 0x0A); - put((byte) 0x1A, (byte) 0x08); put("Director"); - - //-employee number - put((byte) 0x42, (byte) 0x01, (byte) 0x33); - - //-date of hire - put((byte) 0xA1, (byte) 0x0A); - put((byte) 0x43, (byte) 0x08); put("19710917"); - - //-spouse - put((byte) 0xA2, (byte) 0x12); - put((byte) 0x61, (byte) 0x10); - put((byte) 0x1A, (byte) 0x04); put("Mary"); - put((byte) 0x1A, (byte) 0x01); put("T"); - put((byte) 0x1A, (byte) 0x05); put("Smith"); - - //-children - put((byte) 0xA3, (byte) 0x42); - //--child 1 - put((byte) 0x31, (byte) 0x1F); - //---name - put((byte) 0x61, (byte) 0x11); - put((byte) 0x1A, (byte) 0x05); put("Ralph"); - put((byte) 0x1A, (byte) 0x01); put("T"); - put((byte) 0x1A, (byte) 0x05); put("Smith"); - //-date of birth - put((byte) 0xA0, (byte) 0x0A); - put((byte) 0x43, (byte) 0x08); put("19571111"); - //--child 2 - put((byte) 0x31, (byte) 0x1F); - //---name - put((byte) 0x61, (byte) 0x11); - put((byte) 0x1A, (byte) 0x05); put("Susan"); - put((byte) 0x1A, (byte) 0x01); put("B"); - put((byte) 0x1A, (byte) 0x05); put("Jones"); - //-date of birth - put((byte) 0xA0, (byte) 0x0A); - put((byte) 0x43, (byte) 0x08); put("19590717"); - - return buffer.array(); - } - } - - BufferOutput buffer = new BufferOutput(); - return buffer.output(); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/PersonnelRecord.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/PersonnelRecord.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/PersonnelRecord.java deleted file mode 100644 index f5c287a..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/PersonnelRecord.java +++ /dev/null @@ -1,248 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.apache.kerby.asn1.type.Asn1Integer; -import org.apache.kerby.asn1.type.Asn1SequenceOf; -import org.apache.kerby.asn1.type.Asn1SetType; -import org.apache.kerby.asn1.type.Asn1Tagging; -import org.apache.kerby.asn1.type.Asn1TaggingSequence; -import org.apache.kerby.asn1.type.Asn1TaggingSet; -import org.apache.kerby.asn1.type.Asn1VisibleString; - -/** - * Ref. X.690-0207(http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf), - * Annex A, A.1 ASN.1 description of the record structure - */ -public class PersonnelRecord extends Asn1TaggingSet { - protected enum PersonnelRecordField implements EnumType { - NAME, - TITLE, - NUMBER, - DATE_OF_HIRE, - NAME_OF_SPOUSE, - CHILDREN; - - @Override - public int getValue() { - return ordinal(); - } - - @Override - public String getName() { - return name(); - } - } - - static Asn1FieldInfo[] fieldInfos = new Asn1FieldInfo[] { - new ExplicitField(PersonnelRecordField.NAME, -1, Name.class), - new ExplicitField(PersonnelRecordField.TITLE, 0, Asn1VisibleString.class), - new ExplicitField(PersonnelRecordField.NUMBER, -1, EmployeeNumber.class), - new ExplicitField(PersonnelRecordField.DATE_OF_HIRE, 1, Date.class), - new ExplicitField(PersonnelRecordField.NAME_OF_SPOUSE, 2, Name.class), - new ImplicitField(PersonnelRecordField.CHILDREN, 3, Children.class) - }; - - public PersonnelRecord() { - super(0, fieldInfos, true, true); - } - - public void setName(Name name) { - setFieldAs(PersonnelRecordField.NAME, name); - } - - public Name getName() { - return getFieldAs(PersonnelRecordField.NAME, Name.class); - } - - public void setTitle(String title) { - setFieldAs(PersonnelRecordField.TITLE, new Asn1VisibleString(title)); - } - - public String getTitle() { - return getFieldAsString(PersonnelRecordField.TITLE); - } - - public void setEmployeeNumber(EmployeeNumber employeeNumber) { - setFieldAs(PersonnelRecordField.NUMBER, employeeNumber); - } - - public EmployeeNumber getEmployeeNumber() { - return getFieldAs(PersonnelRecordField.NUMBER, EmployeeNumber.class); - } - - public void setDateOfHire(Date dateOfHire) { - setFieldAs(PersonnelRecordField.DATE_OF_HIRE, dateOfHire); - } - - public Date getDateOfHire() { - return getFieldAs(PersonnelRecordField.DATE_OF_HIRE, Date.class); - } - - public void setNameOfSpouse(Name spouse) { - setFieldAs(PersonnelRecordField.NAME_OF_SPOUSE, spouse); - } - - public Name getNameOfSpouse() { - return getFieldAs(PersonnelRecordField.NAME_OF_SPOUSE, Name.class); - } - - public void setChildren(Children children) { - setFieldAs(PersonnelRecordField.CHILDREN, children); - } - - public Children getChildren() { - return getFieldAs(PersonnelRecordField.CHILDREN, Children.class); - } - - public static class Children extends Asn1SequenceOf { - public Children(ChildInformation ... children) { - super(); - for (ChildInformation child : children) { - addElement(child); - } - } - - public Children() { - super(); - } - } - - public static class ChildInformation extends Asn1SetType { - protected enum ChildInformationField implements EnumType { - CHILD_NAME, - DATE_OF_BIRTH; - - @Override - public int getValue() { - return ordinal(); - } - - @Override - public String getName() { - return name(); - } - } - - static Asn1FieldInfo[] tags = new Asn1FieldInfo[] { - new ExplicitField(ChildInformationField.CHILD_NAME, -1, Name.class), - new ExplicitField(ChildInformationField.DATE_OF_BIRTH, 0, Date.class) - }; - - public ChildInformation() { - super(tags); - } - - public void setName(Name name) { - setFieldAs(ChildInformationField.CHILD_NAME, name); - } - - public Name getName() { - return getFieldAs(ChildInformationField.CHILD_NAME, Name.class); - } - - public void setDateOfBirth(Date date) { - setFieldAs(ChildInformationField.DATE_OF_BIRTH, date); - } - - public Date getDateOfBirth() { - return getFieldAs(ChildInformationField.DATE_OF_BIRTH, Date.class); - } - } - - public static class Name extends Asn1TaggingSequence { - - protected enum NameField implements EnumType { - GIVENNAME, - INITIAL, - FAMILYNAME; - - @Override - public int getValue() { - return ordinal(); - } - - @Override - public String getName() { - return name(); - } - } - - static Asn1FieldInfo[] tags = new Asn1FieldInfo[] { - new ExplicitField(NameField.GIVENNAME, -1, Asn1VisibleString.class), - new ExplicitField(NameField.INITIAL, -1, Asn1VisibleString.class), - new ExplicitField(NameField.FAMILYNAME, -1, Asn1VisibleString.class) - }; - - public Name() { - super(1, tags, true, true); - } - - public Name(String givenName, String initial, String familyName) { - this(); - setGivenName(givenName); - setInitial(initial); - setFamilyName(familyName); - } - - public void setGivenName(String givenName) { - setFieldAs(NameField.GIVENNAME, new Asn1VisibleString(givenName)); - } - - public String getGivenName() { - return getFieldAsString(NameField.GIVENNAME); - } - - public void setInitial(String initial) { - setFieldAs(NameField.INITIAL, new Asn1VisibleString(initial)); - } - - public String getInitial() { - return getFieldAsString(NameField.INITIAL); - } - - public void setFamilyName(String familyName) { - setFieldAs(NameField.FAMILYNAME, new Asn1VisibleString(familyName)); - } - - public String getFamilyName() { - return getFieldAsString(NameField.FAMILYNAME); - } - } - - public static class EmployeeNumber extends Asn1Tagging { - public EmployeeNumber(Integer value) { - super(2, new Asn1Integer(value), true, true); - } - - public EmployeeNumber() { - super(2, new Asn1Integer(), true, true); - } - } - - public static class Date extends Asn1Tagging { - public Date(String value) { - super(3, new Asn1VisibleString(value), true, true); - } - public Date() { - this(null); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/PersonnelRecordTest.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/PersonnelRecordTest.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/PersonnelRecordTest.java deleted file mode 100644 index 514b82b..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/PersonnelRecordTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.apache.kerby.asn1.util.HexUtil; -import org.junit.Test; - -import java.io.IOException; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * Ref. X.690-0207(http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf), - * Annex A, A.1 ASN.1 description of the record structure - */ -public class PersonnelRecordTest { - - static boolean verbose = false; - - @Test - public void testEncoding() throws IOException { - PersonnelRecord pr = DataTest.createSamplePersonnel(); - - if (verbose) { - System.out.println("Name:"); - System.out.println(HexUtil.bytesToHexFriendly(pr.getName().encode())); - - System.out.println("DateOfHire:"); - System.out.println(HexUtil.bytesToHexFriendly(pr.getDateOfHire().encode())); - - System.out.println("SpouseName:"); - System.out.println(HexUtil.bytesToHexFriendly(pr.getNameOfSpouse().encode())); - - System.out.println("Child1:"); - System.out.println(HexUtil.bytesToHexFriendly(pr.getChildren().getElements().get(0).encode())); - - System.out.println("Child2:"); - System.out.println(HexUtil.bytesToHexFriendly(pr.getChildren().getElements().get(1).encode())); - - System.out.println("Children:"); - System.out.println(HexUtil.bytesToHexFriendly(pr.getChildren().encode())); - } - - byte[] data = DataTest.createSammplePersonnelEncodingData(); - byte[] encoded = pr.encode(); - - if (verbose) { - System.out.println("ExpectedData:"); - System.out.println(HexUtil.bytesToHexFriendly(data)); - - System.out.println("Encoded:"); - System.out.println(HexUtil.bytesToHexFriendly(encoded)); - } - - assertThat(encoded).isEqualTo(data); - } - - @Test - public void testDecoding() throws IOException { - PersonnelRecord expected = DataTest.createSamplePersonnel(); - byte[] data = DataTest.createSammplePersonnelEncodingData(); - Asn1.parseAndDump(data); - PersonnelRecord decoded = new PersonnelRecord(); - decoded.decode(data); - Asn1.dump(decoded); - - assertThat(decoded.getName().getGivenName()) - .isEqualTo(expected.getName().getGivenName()); - assertThat(decoded.getName().getInitial()) - .isEqualTo(expected.getName().getInitial()); - assertThat(decoded.getName().getFamilyName()) - .isEqualTo(expected.getName().getFamilyName()); - assertThat(decoded.getDateOfHire().getValue().getValue()) - .isEqualTo(expected.getDateOfHire().getValue().getValue()); - assertThat(decoded.getTitle()) - .isEqualTo(expected.getTitle()); - assertThat(decoded.getEmployeeNumber().getValue().getValue()) - .isEqualTo(expected.getEmployeeNumber().getValue().getValue()); - assertThat(decoded.getNameOfSpouse().getGivenName()) - .isEqualTo(expected.getNameOfSpouse().getGivenName()); - assertThat(decoded.getNameOfSpouse().getInitial()) - .isEqualTo(expected.getNameOfSpouse().getInitial()); - assertThat(decoded.getNameOfSpouse().getFamilyName()) - .isEqualTo(expected.getNameOfSpouse().getFamilyName()); - assertThat(decoded.getChildren().getElements().size()) - .isEqualTo(expected.getChildren().getElements().size()); - assertThat(decoded.getChildren().getElements().get(0).getName().getGivenName()) - .isEqualTo(expected.getChildren().getElements().get(0).getName().getGivenName()); - assertThat(decoded.getChildren().getElements().get(0).getName().getInitial()) - .isEqualTo(expected.getChildren().getElements().get(0).getName().getInitial()); - assertThat(decoded.getChildren().getElements().get(0).getName().getFamilyName()) - .isEqualTo(expected.getChildren().getElements().get(0).getName().getFamilyName()); - assertThat(decoded.getChildren().getElements().get(0).getDateOfBirth().getValue().getValue()) - .isEqualTo(expected.getChildren().getElements().get(0).getDateOfBirth().getValue().getValue()); - assertThat(decoded.getChildren().getElements().get(1).getName().getGivenName()) - .isEqualTo(expected.getChildren().getElements().get(1).getName().getGivenName()); - assertThat(decoded.getChildren().getElements().get(1).getName().getInitial()) - .isEqualTo(expected.getChildren().getElements().get(1).getName().getInitial()); - assertThat(decoded.getChildren().getElements().get(1).getName().getFamilyName()) - .isEqualTo(expected.getChildren().getElements().get(1).getName().getFamilyName()); - assertThat(decoded.getChildren().getElements().get(1).getDateOfBirth().getValue().getValue()) - .isEqualTo(expected.getChildren().getElements().get(1).getDateOfBirth().getValue().getValue()); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/TaggingEncodingTest.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TaggingEncodingTest.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TaggingEncodingTest.java deleted file mode 100644 index ad9be01..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TaggingEncodingTest.java +++ /dev/null @@ -1,205 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.apache.kerby.asn1.type.Asn1Tagging; -import org.apache.kerby.asn1.type.Asn1VisibleString; -import org.junit.Test; - -import java.io.IOException; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - Ref. X.690-0207 8.14 Encoding of a tagged value - EXAMPLE - With ASN.1 type definitions (in an explicit tagging environment) of: - Type1 ::= VisibleString - Type2 ::= [APPLICATION 3] IMPLICIT Type1 - Type3 ::= [2] Type2 - Type4 ::= [APPLICATION 7] IMPLICIT Type3 - Type5 ::= [2] IMPLICIT Type2 - a value of: - "Jones" - is encoded as follows: - For Type1: - VisibleString Length Contents - 1A16 0516 4A6F6E657316 - For Type2: - [Application 3] Length Contents - 4316 0516 4A6F6E657316 - For Type3: - [2] Length Contents - A216 0716 - [APPLICATION 3] Length Contents - 4316 0516 4A6F6E657316 - For Type4: - [Application 7] Length Contents - 6716 0716 - [APPLICATION 3] Length Contents - 4316 0516 4A6F6E657316 - For Type5: - [2] Length Contents - 8216 0516 4A6F6E657316 - */ - -public class TaggingEncodingTest { - static final String TEST_STRING = "Jones"; - static final byte[] TYPE1_EXPECTED_BYTES = new byte[] {(byte) 0x1A, (byte) 0x05, - (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x73}; - static final byte[] TYPE2_EXPECTED_BYTES = new byte[] {(byte) 0x43, (byte) 0x05, - (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x73}; - static final byte[] TYPE3_EXPECTED_BYTES = new byte[] {(byte) 0xA2, (byte) 0x07, - (byte) 0x43, (byte) 0x05, (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, - (byte) 0x65, (byte) 0x73}; - static final byte[] TYPE4_EXPECTED_BYTES = new byte[] {(byte) 0x67, (byte) 0x07, - (byte) 0x43, (byte) 0x05, (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, - (byte) 0x65, (byte) 0x73}; - static final byte[] TYPE5_EXPECTED_BYTES = new byte[] {(byte) 0x82, (byte) 0x05, - (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x73}; - - - public static class Type1 extends Asn1VisibleString { - public Type1(String value) { - super(value); - } - public Type1() { - this(null); - } - } - - public static class Type2 extends Asn1Tagging { - public Type2(Type1 value) { - super(3, value, true, true); - } - public Type2() { - this(null); - } - } - - public static class Type3 extends Asn1Tagging { - public Type3(Type2 value) { - super(2, value, false, false); - } - public Type3() { - this(null); - } - } - - public static class Type4 extends Asn1Tagging { - public Type4(Type3 value) { - super(7, value, true, true); - } - public Type4() { - this(null); - } - } - - public static class Type5 extends Asn1Tagging { - public Type5(Type2 value) { - super(2, value, false, true); - } - public Type5() { - this(null); - } - } - - @Test - public void testAsn1TaggingEncoding() throws IOException { - Type1 aType1 = new Type1(TEST_STRING); - Type2 aType2 = new Type2(aType1); - Type3 aType3 = new Type3(aType2); - Type4 aType4 = new Type4(aType3); - Type5 aType5 = new Type5(aType2); - - assertThat(aType1.encode()).isEqualTo(TYPE1_EXPECTED_BYTES); - assertThat(aType2.encode()).isEqualTo(TYPE2_EXPECTED_BYTES); - assertThat(aType3.encode()).isEqualTo(TYPE3_EXPECTED_BYTES); - assertThat(aType4.encode()).isEqualTo(TYPE4_EXPECTED_BYTES); - assertThat(aType5.encode()).isEqualTo(TYPE5_EXPECTED_BYTES); - } - - @Test - public void testAsn1TaggingDecoding() throws IOException { - Type1 aType1 = new Type1(); - aType1.decode(TYPE1_EXPECTED_BYTES); - assertThat(aType1.getValue()).isEqualTo(TEST_STRING); - - Type2 aType2 = new Type2(); - aType2.decode(TYPE2_EXPECTED_BYTES); - assertThat(aType2.getValue().getValue()).isEqualTo(TEST_STRING); - - Type3 aType3 = new Type3(); - aType3.decode(TYPE3_EXPECTED_BYTES); - assertThat(aType3.getValue().getValue().getValue()).isEqualTo(TEST_STRING); - - Type4 aType4 = new Type4(); - aType4.decode(TYPE4_EXPECTED_BYTES); - assertThat(aType4.getValue().getValue().getValue().getValue()).isEqualTo(TEST_STRING); - - Type5 aType5 = new Type5(); - aType5.decode(TYPE5_EXPECTED_BYTES); - assertThat(aType5.getValue().getValue().getValue()).isEqualTo(TEST_STRING); - } - - @Test - public void testTaggingEncodingOption() throws IOException { - Type1 aType1 = new Type1(TEST_STRING); - Type2 aType2 = new Type2(aType1); - Type3 aType3 = new Type3(aType2); - - assertThat(aType1.encode()).isEqualTo(TYPE1_EXPECTED_BYTES); - assertThat(TYPE2_EXPECTED_BYTES) - .isEqualTo(aType1.taggedEncode(TaggingOption.newImplicitAppSpecific(3))); // for Type2 - assertThat(TYPE3_EXPECTED_BYTES) - .isEqualTo(aType2.taggedEncode(TaggingOption.newExplicitContextSpecific(2))); // for Type3 - assertThat(TYPE4_EXPECTED_BYTES) - .isEqualTo(aType3.taggedEncode(TaggingOption.newImplicitAppSpecific(7))); // for Type4 - assertThat(TYPE5_EXPECTED_BYTES) - .isEqualTo(aType2.taggedEncode(TaggingOption.newImplicitContextSpecific(2))); // for Type5 - } - - @Test - public void testTaggingDecodingOption() throws IOException { - Type1 aType1 = new Type1(); - aType1.decode(TYPE1_EXPECTED_BYTES); - assertThat(aType1.getValue()).isEqualTo(TEST_STRING); - - // for Type2 - aType1 = new Type1(); - aType1.taggedDecode(TYPE2_EXPECTED_BYTES, TaggingOption.newImplicitAppSpecific(3)); - assertThat(aType1.getValue()).isEqualTo(TEST_STRING); - - // for Type3 - Type2 aType2 = new Type2(); - aType2.taggedDecode(TYPE3_EXPECTED_BYTES, TaggingOption.newExplicitContextSpecific(2)); - assertThat(aType2.getValue().getValue()).isEqualTo(TEST_STRING); - - // for Type4 - Type3 aType3 = new Type3(); - aType3.taggedDecode(TYPE4_EXPECTED_BYTES, TaggingOption.newImplicitAppSpecific(7)); - assertThat(aType3.getValue().getValue().getValue()).isEqualTo(TEST_STRING); - - // for Type5 - aType2 = new Type2(); - aType2.taggedDecode(TYPE5_EXPECTED_BYTES, TaggingOption.newImplicitContextSpecific(2)); - assertThat(aType2.getValue().getValue()).isEqualTo(TEST_STRING); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestUtil.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestUtil.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestUtil.java deleted file mode 100644 index 619af7b..0000000 --- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestUtil.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.kerby.asn1; - -import org.apache.kerby.asn1.util.HexUtil; -import org.apache.kerby.asn1.util.IOUtil; - -import java.io.IOException; -import java.io.InputStream; - -public final class TestUtil { - private TestUtil() { - - } - - static byte[] readBytesFromTxtFile(String resource) throws IOException { - String hexStr = readStringFromTxtFile(resource); - return HexUtil.hex2bytes(hexStr); - } - - static String readStringFromTxtFile(String resource) throws IOException { - InputStream is = TestUtil.class.getResourceAsStream(resource); - return IOUtil.readInput(is); - } - - static byte[] readBytesFromBinFile(String resource) throws IOException { - InputStream is = TestUtil.class.getResourceAsStream(resource); - return IOUtil.readInputStream(is); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/resources/compressed-data.txt ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/resources/compressed-data.txt b/kerby-asn1/src/test/resources/compressed-data.txt deleted file mode 100644 index 3012abe..0000000 --- a/kerby-asn1/src/test/resources/compressed-data.txt +++ /dev/null @@ -1 +0,0 @@ -3080060B2A864886F70D0109100109A0803080020100300D060B2A864886F70D0109100308308006092A864886F70D010701A08024800482021E789CED945D6FDA301486EF2BF93FE4FA9560F15718AD76618C9BA15140492A75BD0B591A22D1002644E3DFD71E8516ED2FF4B573FC11FBB17D4E62BD69DAB2697BD9715BDE06F976BBAE8BBCAD37CD37339EF49E28BB0B9AFCB5FC11DBCD61CBFA7F292337FA3CC7E6CDFEA5B43DD3149B3F7553DD06CBBAC9EDF163C8B8DE6F37FBDA036F83BA59D74D7917BCD4EBF23F28B999A40A6188FD45BE652FC2F333F46F3D9DCF4CF02EDFB550493633C9A56B18D1301C808594E3D111988384EFA2089101244EB19823A5570909BD4A57A43341E0C9D57928439266F82E4F6CE1D9646462BFE39101CB2138678271D149C9F805C5DB427447263AB6E29DCFFE041333453844629C25FA3101A5C8921964242344D29511260FF78E1DBD834862EE1129BF88A505B75CB85C704ED9AE68AB420A24A58B4BD91465303BBC2E4B4B16268152F8E92216C487D605077A01DAE78CB987F78513BC6C5589B2EA442B48A69E20DAAA5DB6A2B52E239EA25A552B9C154393FBF908D9025AE1D1C78F613C7F80D66E07D5619DDB60BA397D4FFB605BDA202BEDEB9E6837616CA09D3B85E460D2F90FE32152E709C61915CE8A93FF9CF34C0AC198F075922A0 D85911FAF0CB814125CB8176178F6DC201A60E04D24C9624E5D17EDE5790E7A0A20548A61D8F7711A43FDC2BF2564C72517BBE8C5179DF056F26EC7779CE1438AE2D8DAD517F58BFA4575FF70064A5277D9B14F77606C403F2E4B3231EA539B923773F79734000000000000000000000000 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/resources/constructed-octet-string.txt ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/resources/constructed-octet-string.txt b/kerby-asn1/src/test/resources/constructed-octet-string.txt deleted file mode 100644 index 66f28f1..0000000 --- a/kerby-asn1/src/test/resources/constructed-octet-string.txt +++ /dev/null @@ -1 +0,0 @@ -24800482021E789CED945D6FDA301486EF2BF93FE4FA9560F15718AD76618C9BA15140492A75BD0B591A22D1002644E3DFD71E8516ED2FF4B573FC11FBB17D4E62BD69DAB2697BD9715BDE06F976BBAE8BBCAD37CD37339EF49E28BB0B9AFCB5FC11DBCD61CBFA7F292337FA3CC7E6CDFEA5B43DD3149B3F7553DD06CBBAC9EDF163C8B8DE6F37FBDA036F83BA59D74D7917BCD4EBF23F28B999A40A6188FD45BE652FC2F333F46F3D9DCF4CF02EDFB550493633C9A56B18D1301C808594E3D111988384EFA2089101244EB19823A5570909BD4A57A43341E0C9D57928439266F82E4F6CE1D9646462BFE39101CB2138678271D149C9F805C5DB427447263AB6E29DCFFE041333453844629C25FA3101A5C8921964242344D29511260FF78E1DBD834862EE1129BF88A505B75CB85C704ED9AE68AB420A24A58B4BD91465303BBC2E4B4B16268152F8E92216C487D605077A01DAE78CB987F78513BC6C5589B2EA442B48A69E20DAAA5DB6A2B52E239EA25A552B9C154393FBF908D9025AE1D1C78F613C7F80D66E07D5619DDB60BA397D4FFB605BDA202BEDEB9E6837616CA09D3B85E460D2F90FE32152E709C61915CE8A93FF9CF34C0AC198F075922A0D85911FAF0CB814125CB8176178F6DC201A60E04D24C9624E5D17EDE5790E7A0A20548A61D8F7711A43FDC2BF2564C72517BBE8C 5179DF056F26EC7779CE1438AE2D8DAD517F58BFA4575FF70064A5277D9B14F77606C403F2E4B3231EA539B923773F797340000 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/resources/der-data.dat ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/resources/der-data.dat b/kerby-asn1/src/test/resources/der-data.dat deleted file mode 100644 index 6d503b3..0000000 Binary files a/kerby-asn1/src/test/resources/der-data.dat and /dev/null differ http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/resources/empty-container.txt ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/resources/empty-container.txt b/kerby-asn1/src/test/resources/empty-container.txt deleted file mode 100644 index 40945ee..0000000 --- a/kerby-asn1/src/test/resources/empty-container.txt +++ /dev/null @@ -1 +0,0 @@ -6A819F30819CA103020105A20302010AA3023000A4818B308188A00703050050000010A1143012A003020101A10B30091B076472616E6B7965A20A1B08544553542E434F4DA31D301BA003020102A11430121B066B72627467741B08544553542E434F4DA411180F32303135313231303130323831305AA511180F32303135313231303138323831305AA706020434D354A9A80E300C020111020110020101020103 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/test/resources/signed-data.txt ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/test/resources/signed-data.txt b/kerby-asn1/src/test/resources/signed-data.txt deleted file mode 100644 index bf018ef..0000000 --- a/kerby-asn1/src/test/resources/signed-data.txt +++ /dev/null @@ -1 +0,0 @@ -308006092A864886F70D010702A0803080020101310B300906052B0E03021A0500308006092A864886F70D010701A0802480040C48656C6C6F20576F726C6421000000000000A08204623082020D30820176A003020102020101300D06092A864886F70D0101040500302531163014060355040A130D426F756E637920436173746C65310B3009060355040613024155301E170D3034313032343034333035385A170D3035303230313034333035385A302531163014060355040A130D426F756E637920436173746C65310B300906035504061302415530819F300D06092A864886F70D010101050003818D003081890281810098F7380B2100E80398F7186758DD352BA1387447F55842FCF1B5EC5762227546736BA52611227C44206BFBAA642A527759C7B095192A6F64D5B567796CC2D3DE358BD6677F181BDA39D75EC6B99300762845444AB396B118D246D9D888B82A046D2BCE968C9F90399EDBBB374E34E847AC0617B5F5C8D901A81E1071990DBB9F0203010001A34D304B301D0603551D0E041604147F88734A3A8E9FE01C96145CB044D67AE574ABD6301F0603551D230418301680147F88734A3A8E9FE01C96145CB044D67AE574ABD630090603551D1304023000300D06092A864886F70D010104050003818100530927B40EA47A37D1B9E5438372DCF0A72BE49EF86C374 E96981A9F38F9AF1F183F4399EEFC1BA344B20BE91464F2A0BC6F278081F382B3CACFB5A761DD5F3A198B25C07ABE52E08C29B44DD3C711702B28F6F1C46B33A74AE3B0C3C97524574AB004030D96EC2793F9E8E0AEE297337631993997BC17C8D6BDC89E821D6E9D3082024D308201B6A003020102020102300D06092A864886F70D0101040500302531163014060355040A130D426F756E637920436173746C65310B3009060355040613024155301E170D3034313032343034333035395A170D3035303230313034333035395A3065311830160603550403130F4572696320482E2045636869646E613124302206092A864886F70D01090116156572696340626F756E6379636173746C652E6F726731163014060355040A130D426F756E637920436173746C65310B300906035504061302415530819F300D06092A864886F70D010101050003818D00308189028181009BEE429C653A5B8E625290AC686927E600EBB99BF78FFA3B37A6A09916618A468B1B6245E8409A5F5DE28A85497E6CC1B0F2B1000096C2E4D70A84925C6F2F88AFEAA521F02697D01D8121B5C0B40B122A96796DBF7290006C21FCF770A523EF48D9E44011AFB45ABEFBD8275C305BCA77CFF1BD8BD848ACD17F54DB2EA016230203010001A34D304B301D0603551D0E0416041440263A4B2717AE85A109BA21 005537AEAF268D53301F0603551D230418301680147F88734A3A8E9FE01C96145CB044D67AE574ABD630090603551D1304023000300D06092A864886F70D01010405000381810011E223BCD90A30F53F6580AED53AA31993C4AA2FA0967B60DA10BF085D281B21C5A4CB86B4C7A9177A6E5BEBB328CD6CEB56ABDDA862769DD7161AFF18453F5F12B6E00A25237C858313FC3D18145A6422CD3417A56526865B017B5B8829511F340CC282D61F250F072482AC50271D59E08A54AB388D18A89EC7A4D6BA3390F13182012F3082012B020101302A302531163014060355040A130D426F756E637920436173746C65310B3009060355040613024155020102300906052B0E03021A0500A05D301806092A864886F70D010903310B06092A864886F70D010701301C06092A864886F70D010905310F170D3034313032343034333035395A302306092A864886F70D010904311604142EF7BDE608CE5404E97D5F042F95F89F1C232871300D06092A864886F70D010101050004818061DB7B7FE3719BBA6FF7AB4617373C48E23143BC98421188D601AF9FEF14D864AF2F13AC67CEA48E399BB82ED4C4B4FEF75AB3BCA4BE2765FEC3597CED06A6110851A2233114F753ACAE4D617868BA5AB496DB46C21EA493BF07691D2006D5E5209B6605432483CE476AA7AEE1CDDB02C56BE7C4BF827CC10 F17C2F9340BC88C000000000000 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-common/kerby-asn1/README.md ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-asn1/README.md b/kerby-common/kerby-asn1/README.md new file mode 100644 index 0000000..05f5502 --- /dev/null +++ b/kerby-common/kerby-asn1/README.md @@ -0,0 +1,389 @@ + +kerby-asn1 +========= + +### A ASN1 parser with easy and simple API + +``` +// encoding +Asn1Integer aValue = new Asn1Integer(8899); +byte[] encoded = aValue.encode(); + +// decoding +byte[] contentToDecode = ... +Asn1Integer decodedValue = new Asn1Integer(); +decodedValue.decode(contentToDecode); +Integer value = decodedValue.getValue(); +``` + +### Data-driven ASN1 encoding/decoding framework and parser + +With the following definition from Kerberos protocol +``` + AuthorizationData ::= SEQUENCE OF SEQUENCE { + ad-type [0] Int32, + ad-data [1] OCTET STRING + } + ``` + +You can model AuthzDataEntry as follows +```java +public class AuthorizationDataEntry extends KrbSequenceType { + /** + * The possible fields + */ + protected enum AuthorizationDataEntryField implements EnumType { + AD_TYPE, + AD_DATA; + + /** + * {@inheritDoc} + */ + @Override + public int getValue() { + return ordinal(); + } + + /** + * {@inheritDoc} + */ + @Override + public String getName() { + return name(); + } + } + + /** The AuthorizationDataEntry's fields */ + private static Asn1FieldInfo[] fieldInfos = new Asn1FieldInfo[] { + new ExplicitField(AuthorizationDataEntryField.AD_TYPE, Asn1Integer.class), + new ExplicitField(AuthorizationDataEntryField.AD_DATA, Asn1OctetString.class) + }; + + /** + * Creates an AuthorizationDataEntry instance + */ + public AuthorizationDataEntry() { + super(fieldInfos); + } + + /** + * @return The AuthorizationType (AD_TYPE) field + */ + public AuthorizationType getAuthzType() { + Integer value = getFieldAsInteger(AuthorizationDataEntryField.AD_TYPE); + return AuthorizationType.fromValue(value); + } + + /** + * Sets the AuthorizationType (AD_TYPE) field + * @param authzType The AuthorizationType to set + */ + public void setAuthzType(AuthorizationType authzType) { + setFieldAsInt(AuthorizationDataEntryField.AD_TYPE, authzType.getValue()); + } + + /** + * @return The AuthorizationType (AD_DATA) field + */ + public byte[] getAuthzData() { + return getFieldAsOctets(AuthorizationDataEntryField.AD_DATA); + } + + /** + * Sets the AuthorizationData (AD_DATA) field + * @param authzData The AuthorizationData to set + */ + public void setAuthzData(byte[] authzData) { + setFieldAsOctets(AuthorizationDataEntryField.AD_DATA, authzData); + } +} +``` + +And then define AuthorizationData simply +```java +public class AuthorizationData extends KrbSequenceOfType { + +} +``` + +Then you can process with above definitions, encode and decode, without caring about the details. + +Think about how to implement the following more complex and pratical sample from [ITU-T Rec. X.680 ISO/IEC 8824-1](http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf): +``` +A.1 ASN.1 description of the record structure +The structure of the hypothetical personnel record is formally described below using ASN.1 specified in +ITU-T Rec. X.680 | ISO/IEC 8824-1 for defining types. + +PersonnelRecord ::= [APPLICATION 0] IMPLICIT SET { + Name Name, + title [0] VisibleString, + number EmployeeNumber, + dateOfHire [1] Date, + nameOfSpouse [2] Name, + children [3] IMPLICIT + SEQUENCE OF ChildInformation DEFAULT {} +} + +ChildInformation ::= SET { + name Name, + dateOfBirth [0] Date +} + +Name ::= [APPLICATION 1] IMPLICIT SEQUENCE { + givenName VisibleString, + initial VisibleString, + familyName VisibleString +} + +EmployeeNumber ::= [APPLICATION 2] IMPLICIT INTEGER +Date ::= [APPLICATION 3] IMPLICIT VisibleString -- YYYYMMDD +``` +Similarly as above, we can have (from the unit test codes): +```java +public class PersonnelRecord extends Asn1TaggingSet { + protected enum PersonnelRecordField implements EnumType { + NAME, + TITLE, + NUMBER, + DATE_OF_HIRE, + NAME_OF_SPOUSE, + CHILDREN; + + @Override + public int getValue() { + return ordinal(); + } + + @Override + public String getName() { + return name(); + } + } + + static Asn1FieldInfo[] fieldInfos = new Asn1FieldInfo[] { + new ExplicitField(PersonnelRecordField.NAME, -1, Name.class), + new ExplicitField(PersonnelRecordField.TITLE, 0, Asn1VisibleString.class), + new ExplicitField(PersonnelRecordField.NUMBER, -1, EmployeeNumber.class), + new ExplicitField(PersonnelRecordField.DATE_OF_HIRE, 1, Date.class), + new ExplicitField(PersonnelRecordField.NAME_OF_SPOUSE, 2, Name.class), + new ImplicitField(PersonnelRecordField.CHILDREN, 3, Children.class) + }; + + public PersonnelRecord() { + super(0, fieldInfos, true, true); + } + + public void setName(Name name) { + setFieldAs(PersonnelRecordField.NAME, name); + } + + public Name getName() { + return getFieldAs(PersonnelRecordField.NAME, Name.class); + } + + public void setTitle(String title) { + setFieldAs(PersonnelRecordField.TITLE, new Asn1VisibleString(title)); + } + + public String getTitle() { + return getFieldAsString(PersonnelRecordField.TITLE); + } + + public void setEmployeeNumber(EmployeeNumber employeeNumber) { + setFieldAs(PersonnelRecordField.NUMBER, employeeNumber); + } + + public EmployeeNumber getEmployeeNumber() { + return getFieldAs(PersonnelRecordField.NUMBER, EmployeeNumber.class); + } + + public void setDateOfHire(Date dateOfHire) { + setFieldAs(PersonnelRecordField.DATE_OF_HIRE, dateOfHire); + } + + public Date getDateOfHire() { + return getFieldAs(PersonnelRecordField.DATE_OF_HIRE, Date.class); + } + + public void setNameOfSpouse(Name spouse) { + setFieldAs(PersonnelRecordField.NAME_OF_SPOUSE, spouse); + } + + public Name getNameOfSpouse() { + return getFieldAs(PersonnelRecordField.NAME_OF_SPOUSE, Name.class); + } + + public void setChildren(Children children) { + setFieldAs(PersonnelRecordField.CHILDREN, children); + } + + public Children getChildren() { + return getFieldAs(PersonnelRecordField.CHILDREN, Children.class); + } + + public static class Children extends Asn1SequenceOf { + public Children(ChildInformation ... children) { + super(); + for (ChildInformation child : children) { + addElement(child); + } + } + + public Children() { + super(); + } + } + + public static class ChildInformation extends Asn1SetType { + protected enum ChildInformationField implements EnumType { + CHILD_NAME, + DATE_OF_BIRTH; + + @Override + public int getValue() { + return ordinal(); + } + + @Override + public String getName() { + return name(); + } + } + + static Asn1FieldInfo[] tags = new Asn1FieldInfo[] { + new ExplicitField(ChildInformationField.CHILD_NAME, -1, Name.class), + new ExplicitField(ChildInformationField.DATE_OF_BIRTH, 0, Date.class) + }; + + public ChildInformation() { + super(tags); + } + + public void setName(Name name) { + setFieldAs(ChildInformationField.CHILD_NAME, name); + } + + public Name getName() { + return getFieldAs(ChildInformationField.CHILD_NAME, Name.class); + } + + public void setDateOfBirth(Date date) { + setFieldAs(ChildInformationField.DATE_OF_BIRTH, date); + } + + public Date getDateOfBirth() { + return getFieldAs(ChildInformationField.DATE_OF_BIRTH, Date.class); + } + } + + public static class Name extends Asn1TaggingSequence { + + protected enum NameField implements EnumType { + GIVENNAME, + INITIAL, + FAMILYNAME; + + @Override + public int getValue() { + return ordinal(); + } + + @Override + public String getName() { + return name(); + } + } + + static Asn1FieldInfo[] tags = new Asn1FieldInfo[] { + new ExplicitField(NameField.GIVENNAME, -1, Asn1VisibleString.class), + new ExplicitField(NameField.INITIAL, -1, Asn1VisibleString.class), + new ExplicitField(NameField.FAMILYNAME, -1, Asn1VisibleString.class) + }; + + public Name() { + super(1, tags, true, true); + } + + public Name(String givenName, String initial, String familyName) { + this(); + setGivenName(givenName); + setInitial(initial); + setFamilyName(familyName); + } + + public void setGivenName(String givenName) { + setFieldAs(NameField.GIVENNAME, new Asn1VisibleString(givenName)); + } + + public String getGivenName() { + return getFieldAsString(NameField.GIVENNAME); + } + + public void setInitial(String initial) { + setFieldAs(NameField.INITIAL, new Asn1VisibleString(initial)); + } + + public String getInitial() { + return getFieldAsString(NameField.INITIAL); + } + + public void setFamilyName(String familyName) { + setFieldAs(NameField.FAMILYNAME, new Asn1VisibleString(familyName)); + } + + public String getFamilyName() { + return getFieldAsString(NameField.FAMILYNAME); + } + } + + public static class EmployeeNumber extends Asn1Tagging { + public EmployeeNumber(Integer value) { + super(2, new Asn1Integer(value), true, true); + } + + public EmployeeNumber() { + super(2, new Asn1Integer(), true, true); + } + } + + public static class Date extends Asn1Tagging { + public Date(String value) { + super(3, new Asn1VisibleString(value), true, true); + } + public Date() { + this(null); + } + } +} +``` +### ASN1 dumping tool +* ASN1 dumping tool to help analyze ASN1 encoding stream or packet. It can be used to exercise the framework with all kinds of testing binary inputs. +* The shortcut API for ASN1 encoding, decoding and dumping in Asn1.java + +### Notes +* 90% tests coverage for DER encoding +* For BER & CER encoding, to be fully supported +* No extra dependency + +### License +Apache V2 License + + + http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-common/kerby-asn1/pom.xml ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-asn1/pom.xml b/kerby-common/kerby-asn1/pom.xml new file mode 100644 index 0000000..2e44285 --- /dev/null +++ b/kerby-common/kerby-asn1/pom.xml @@ -0,0 +1,32 @@ + + + + + + org.apache.kerby + kerby-common + 1.0.0-RC2-SNAPSHOT + + + 4.0.0 + + kerby-asn1 + Kerby ASN1 Project + Kerby ASN1 Project + + + + +