You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by dr...@apache.org on 2015/02/01 03:20:53 UTC

directory-kerberos git commit: DIRKRB-157 Value like 0x01 01 7F should not be decoded as TRUE with DER

Repository: directory-kerberos
Updated Branches:
  refs/heads/master 7fbebbcdf -> 377814b2d


DIRKRB-157 Value like 0x01 01 7F should not be decoded as TRUE with DER


Project: http://git-wip-us.apache.org/repos/asf/directory-kerberos/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerberos/commit/377814b2
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerberos/tree/377814b2
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerberos/diff/377814b2

Branch: refs/heads/master
Commit: 377814b2dcfda1192d77cab8705fe7c750384810
Parents: 7fbebbc
Author: Drankye <dr...@gmail.com>
Authored: Sun Feb 1 10:19:05 2015 +0800
Committer: Drankye <dr...@gmail.com>
Committed: Sun Feb 1 10:19:05 2015 +0800

----------------------------------------------------------------------
 .../org/apache/kerby/asn1/EncodingOption.java   | 12 +++++++++++
 .../kerby/asn1/type/AbstractAsn1Type.java       |  2 +-
 .../org/apache/kerby/asn1/type/Asn1Boolean.java |  9 ++++++--
 .../org/apache/kerby/asn1/TestAsn1Boolean.java  | 22 +++++++++++++-------
 4 files changed, 34 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/377814b2/kerby-asn1/src/main/java/org/apache/kerby/asn1/EncodingOption.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/EncodingOption.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/EncodingOption.java
index c15f5ce..6bc8351 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/EncodingOption.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/EncodingOption.java
@@ -64,6 +64,10 @@ public enum EncodingOption
         return this == EXPLICIT;
     }
 
+    /**
+     * Is DER encoding ?
+     * @return true if using DER otherwise false
+     */
     public boolean isDer() {
         return this == DER;
     }
@@ -72,6 +76,14 @@ public enum EncodingOption
         return this == CER;
     }
 
+    /**
+     * Is BER encoding ?
+     * @return true if using BER otherwise false
+     */
+    public boolean isBer() {
+        return this == BER;
+    }
+
     public static EncodingOption fromValue(int value) {
         for (EncodingOption e : values()) {
             if (e.getValue() == value) {

http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/377814b2/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/AbstractAsn1Type.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/AbstractAsn1Type.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/AbstractAsn1Type.java
index e06fcc9..f7df523 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/AbstractAsn1Type.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/AbstractAsn1Type.java
@@ -38,7 +38,7 @@ public abstract class AbstractAsn1Type<T> implements Asn1Type {
     private TagClass tagClass = TagClass.UNKNOWN;
     private int tagNo = -1;
     private int tagFlags = -1;
-    private EncodingOption encodingOption = EncodingOption.UNKNOWN;
+    private EncodingOption encodingOption = EncodingOption.BER;
     private int encodingLen = -1;
     // The wrapped real value.
     private T value;

http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/377814b2/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Boolean.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Boolean.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Boolean.java
index 38e0d38..e5a6188 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Boolean.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Boolean.java
@@ -58,14 +58,19 @@ public class Asn1Boolean extends Asn1Simple<Boolean>
         setBytes(getValue() ? TRUE_BYTE : FALSE_BYTE);
     }
 
+    @Override
     protected void toValue() throws IOException {
         byte[] bytes = getBytes();
         if (bytes[0] == 0) {
             setValue(false);
-        } else if (bytes[0] == 0xff) {
+        } else if ((bytes[0] & 0xff) == 0xff) {
+            // DER only accepts 0xFF as true
             setValue(true);
-        } else {
+        } else if (getEncodingOption().isBer()) {
+            // BER accepts any non-zero as true
             setValue(true);
+        } else {
+            setValue(false);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/377814b2/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Boolean.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Boolean.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Boolean.java
index 536d1a9..691c8fe 100644
--- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Boolean.java
+++ b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Boolean.java
@@ -30,27 +30,33 @@ public class TestAsn1Boolean {
 
     @Test
     public void testEncoding() {
-        testEncodingWith(true, "0x01 01 FF");
-        testEncodingWith(false, "0x01 01 00");
+        testEncodingWith(true, "0x01 01 FF", true);
+        testEncodingWith(false, "0x01 01 00", true);
     }
 
-    private void testEncodingWith(Boolean value, String expectedEncoding) {
+    private void testEncodingWith(Boolean value, String expectedEncoding,
+                                  boolean isDer) {
         byte[] expected = Util.hex2bytes(expectedEncoding);
         Asn1Boolean aValue = new Asn1Boolean(value);
-        aValue.setEncodingOption(EncodingOption.DER);
+        aValue.setEncodingOption(isDer ? EncodingOption.DER :
+                EncodingOption.BER);
         byte[] encodingBytes = aValue.encode();
         assertThat(encodingBytes).isEqualTo(expected);
     }
 
     @Test
     public void testDecoding() throws IOException {
-        testDecodingWith(true, "0x01 01 FF");
-        testDecodingWith(false, "0x01 01 00");
+        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) throws IOException {
+    private void testDecodingWith(Boolean expectedValue, String content,
+                                  boolean isDer) throws IOException {
         Asn1Boolean decoded = new Asn1Boolean();
-        decoded.setEncodingOption(EncodingOption.DER);
+        decoded.setEncodingOption(isDer ? EncodingOption.DER :
+                EncodingOption.BER);
         decoded.decode(Util.hex2bytes(content));
         assertThat(decoded.getValue()).isEqualTo(expectedValue);
     }