directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dran...@apache.org
Subject directory-kerby git commit: ASN1. Added Asn1Util for those static functions in Asn1Object
Date Tue, 01 Dec 2015 07:23:20 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/master a74ee67ab -> f98e113b8


ASN1. Added Asn1Util for those static functions in Asn1Object


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

Branch: refs/heads/master
Commit: f98e113b8138ee13839618030119a7c0680ec2a3
Parents: a74ee67
Author: Kai Zheng <kai.zheng@intel.com>
Authored: Tue Dec 1 15:22:48 2015 +0800
Committer: Kai Zheng <kai.zheng@intel.com>
Committed: Tue Dec 1 15:22:48 2015 +0800

----------------------------------------------------------------------
 .../java/org/apache/kerby/asn1/Asn1Util.java    | 198 ++++++++++++++++++
 .../org/apache/kerby/asn1/type/Asn1Object.java  | 200 ++-----------------
 .../apache/kerby/asn1/type/Asn1OctetString.java |   3 +-
 .../org/apache/kerby/asn1/type/Asn1Simple.java  |   7 +-
 .../apache/kerby/kerberos/kerb/KrbCodec.java    |   8 +-
 5 files changed, 226 insertions(+), 190 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f98e113b/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1Util.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1Util.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1Util.java
new file mode 100644
index 0000000..caa50f3
--- /dev/null
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1Util.java
@@ -0,0 +1,198 @@
+/**
+ *  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.io.IOException;
+import java.nio.ByteBuffer;
+
+/**
+ * The abstract ASN1 object for all the ASN1 types. It provides basic
+ * encoding and decoding utilities.
+ */
+public final class Asn1Util {
+    private Asn1Util() {
+        
+    }
+
+    public static int lengthOfBodyLength(int bodyLength) {
+        int length = 1;
+
+        if (bodyLength > 127) {
+            int payload = bodyLength;
+            while (payload != 0) {
+                payload >>= 8;
+                length++;
+            }
+        }
+
+        return length;
+    }
+
+    public static int lengthOfTagLength(int tagNo) {
+        int length = 1;
+
+        if (tagNo >= 31) {
+            if (tagNo < 128) {
+                length++;
+            } else {
+                length++;
+
+                do {
+                    tagNo >>= 7;
+                    length++;
+                } while (tagNo > 127);
+            }
+        }
+
+        return length;
+    }
+
+    public static void encodeTag(ByteBuffer buffer, Tag tag) {
+        int flags = tag.tagFlags();
+        int tagNo = tag.tagNo();
+
+        if (tagNo < 31) {
+            buffer.put((byte) (flags | tagNo));
+        } else {
+            buffer.put((byte) (flags | 0x1f));
+            if (tagNo < 128) {
+                buffer.put((byte) tagNo);
+            } else {
+                byte[] tmpBytes = new byte[5]; // 5 * 7 > 32
+                int iPut = tmpBytes.length;
+
+                tmpBytes[--iPut] = (byte) (tagNo & 0x7f);
+                do {
+                    tagNo >>= 7;
+                    tmpBytes[--iPut] = (byte) (tagNo & 0x7f | 0x80);
+                } while (tagNo > 127);
+
+                buffer.put(tmpBytes, iPut, tmpBytes.length - iPut);
+            }
+        }
+    }
+
+    public static void encodeLength(ByteBuffer buffer, int bodyLength) {
+        if (bodyLength < 128) {
+            buffer.put((byte) bodyLength);
+        } else {
+            int length = 0;
+            int payload = bodyLength;
+
+            while (payload != 0) {
+                payload >>= 8;
+                length++;
+            }
+
+            buffer.put((byte) (length | 0x80));
+
+            payload = bodyLength;
+            for (int i = length - 1; i >= 0; i--) {
+                buffer.put((byte) (payload >> (i * 8)));
+            }
+        }
+    }
+
+    public static Tag readTag(ByteBuffer buffer) throws IOException {
+        int tagFlags = readTagFlags(buffer);
+        int tagNo = readTagNo(buffer, tagFlags);
+        return new Tag(tagFlags, tagNo);
+    }
+
+    private static int readTagFlags(ByteBuffer buffer) throws IOException {
+        int tagFlags = buffer.get() & 0xff;
+        if (tagFlags == 0) {
+            throw new IOException("Bad tag 0 found");
+        }
+        return tagFlags;
+    }
+
+    private static int readTagNo(ByteBuffer buffer, int tagFlags) throws IOException {
+        int tagNo = tagFlags & 0x1f;
+
+        if (tagNo == 0x1f) {
+            tagNo = 0;
+
+            int b = buffer.get() & 0xff;
+            if ((b & 0x7f) == 0) {
+                throw new IOException("Invalid high tag number found");
+            }
+
+            while (b >= 0 && (b & 0x80) != 0) {
+                tagNo |= b & 0x7f;
+                tagNo <<= 7;
+                b = buffer.get();
+            }
+
+            tagNo |= b & 0x7f;
+        }
+
+        return tagNo;
+    }
+
+    public static int readLength(ByteBuffer buffer) throws IOException {
+        int result = buffer.get() & 0xff;
+        if (result == 0x80) {
+            return -1; // non-definitive length
+        }
+
+        if (result > 127) {
+            int length = result & 0x7f;
+            if (length > 4) {
+                throw new IOException("Bad length of more than 4 bytes: " + length);
+            }
+
+            result = 0;
+            int tmp;
+            for (int i = 0; i < length; i++) {
+                tmp = buffer.get() & 0xff;
+                result = (result << 8) + tmp;
+            }
+        }
+
+        if (result < 0) {
+            throw new IOException("Invalid length " + result);
+        }
+
+        if (result > buffer.remaining()) {
+            throw new IOException("Corrupt stream - less data "
+                + buffer.remaining() + " than expected " + result);
+        }
+
+        return result;
+    }
+
+    public static ByteBuffer dupWithLength(ByteBuffer buffer, int length) {
+        try {
+            ByteBuffer result = buffer.duplicate();
+            result.limit(buffer.position() + length);
+            buffer.position(buffer.position() + length);
+            return result;
+        } catch (Exception e) {
+            throw e;
+        }
+    }
+
+    public static byte[] readAllLeftBytes(ByteBuffer buffer) {
+        byte[] result = new byte[buffer.remaining()];
+        buffer.get(result);
+        return result;
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f98e113b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Object.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Object.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Object.java
index 71d3af0..0a46d37 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Object.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Object.java
@@ -19,6 +19,7 @@
  */
 package org.apache.kerby.asn1.type;
 
+import org.apache.kerby.asn1.Asn1Util;
 import org.apache.kerby.asn1.Tag;
 import org.apache.kerby.asn1.TaggingOption;
 import org.apache.kerby.asn1.UniversalTag;
@@ -147,8 +148,8 @@ public abstract class Asn1Object implements Asn1Type {
 
     @Override
     public void encode(ByteBuffer buffer) {
-        encodeTag(buffer, tag());
-        encodeLength(buffer, encodingBodyLength());
+        Asn1Util.encodeTag(buffer, tag());
+        Asn1Util.encodeLength(buffer, encodingBodyLength());
         encodeBody(buffer);
     }
 
@@ -163,8 +164,8 @@ public abstract class Asn1Object implements Asn1Type {
     public int encodingLength() {
         if (encodingLen == -1) {
             int bodyLen = encodingBodyLength();
-            encodingLen = lengthOfTagLength(tagNo())
-                + lengthOfBodyLength(bodyLen) + bodyLen;
+            encodingLen = Asn1Util.lengthOfTagLength(tagNo())
+                + Asn1Util.lengthOfBodyLength(bodyLen) + bodyLen;
         }
         return encodingLen;
     }
@@ -197,15 +198,15 @@ public abstract class Asn1Object implements Asn1Type {
 
     @Override
     public void decode(ByteBuffer content) throws IOException {
-        Tag tmpTag = readTag(content);
-        int length = readLength(content);
+        Tag tmpTag = Asn1Util.readTag(content);
+        int length = Asn1Util.readLength(content);
 
         ByteBuffer valueBuffer;
         if (length == -1) {
             valueBuffer = content;
             useDefinitiveLength(false);
         } else {
-            valueBuffer = dupWithLength(content, length);
+            valueBuffer = Asn1Util.dupWithLength(content, length);
             useDefinitiveLength(true);
         }
 
@@ -227,8 +228,8 @@ public abstract class Asn1Object implements Asn1Type {
         int taggingTagNo = taggingOption.getTagNo();
         int taggingBodyLen = taggingOption.isImplicit() ? encodingBodyLength()
                 : encodingLength();
-        int taggingEncodingLen = lengthOfTagLength(taggingTagNo)
-                + lengthOfBodyLength(taggingBodyLen) + taggingBodyLen;
+        int taggingEncodingLen = Asn1Util.lengthOfTagLength(taggingTagNo)
+                + Asn1Util.lengthOfBodyLength(taggingBodyLen) + taggingBodyLen;
         return taggingEncodingLen;
     }
 
@@ -242,10 +243,10 @@ public abstract class Asn1Object implements Asn1Type {
     @Override
     public void taggedEncode(ByteBuffer buffer, TaggingOption taggingOption) {
         Tag taggingTag = taggingOption.getTag(!isPrimitive());
-        encodeTag(buffer, taggingTag);
+        Asn1Util.encodeTag(buffer, taggingTag);
         int taggingBodyLen = taggingOption.isImplicit() ? encodingBodyLength()
                 : encodingLength();
-        encodeLength(buffer, taggingBodyLen);
+        Asn1Util.encodeLength(buffer, taggingBodyLen);
         if (taggingOption.isImplicit()) {
             encodeBody(buffer);
         } else {
@@ -261,15 +262,15 @@ public abstract class Asn1Object implements Asn1Type {
     @Override
     public void taggedDecode(ByteBuffer content,
                              TaggingOption taggingOption) throws IOException {
-        Tag taggingTag = readTag(content);
-        int taggingLength = readLength(content);
+        Tag taggingTag = Asn1Util.readTag(content);
+        int taggingLength = Asn1Util.readLength(content);
 
         ByteBuffer valueBuffer;
         if (taggingLength == -1) {
             valueBuffer = content;
             useDefinitiveLength(false);
         } else {
-            valueBuffer = dupWithLength(content, taggingLength);
+            valueBuffer = Asn1Util.dupWithLength(content, taggingLength);
             useDefinitiveLength(true);
         }
 
@@ -292,8 +293,8 @@ public abstract class Asn1Object implements Asn1Type {
     }
 
     public static Asn1Item decodeOne(ByteBuffer content) throws IOException {
-        Tag tmpTag = readTag(content);
-        int length = readLength(content);
+        Tag tmpTag = Asn1Util.readTag(content);
+        int length = Asn1Util.readLength(content);
 
         Asn1Item result;
         ByteBuffer valueBuffer;
@@ -302,176 +303,11 @@ public abstract class Asn1Object implements Asn1Type {
             result.useDefinitiveLength(false);
             result.setBodyContent(content);
         } else {
-            valueBuffer = dupWithLength(content, length);
+            valueBuffer = Asn1Util.dupWithLength(content, length);
             result = new Asn1Item(tmpTag, valueBuffer);
             result.useDefinitiveLength(true);
         }
 
         return result;
     }
-
-    public static int lengthOfBodyLength(int bodyLength) {
-        int length = 1;
-
-        if (bodyLength > 127) {
-            int payload = bodyLength;
-            while (payload != 0) {
-                payload >>= 8;
-                length++;
-            }
-        }
-
-        return length;
-    }
-
-    public static int lengthOfTagLength(int tagNo) {
-        int length = 1;
-
-        if (tagNo >= 31) {
-            if (tagNo < 128) {
-                length++;
-            } else {
-                length++;
-
-                do {
-                    tagNo >>= 7;
-                    length++;
-                } while (tagNo > 127);
-            }
-        }
-
-        return length;
-    }
-
-    public static void encodeTag(ByteBuffer buffer, Tag tag) {
-        int flags = tag.tagFlags();
-        int tagNo = tag.tagNo();
-
-        if (tagNo < 31) {
-            buffer.put((byte) (flags | tagNo));
-        } else {
-            buffer.put((byte) (flags | 0x1f));
-            if (tagNo < 128) {
-                buffer.put((byte) tagNo);
-            } else {
-                byte[] tmpBytes = new byte[5]; // 5 * 7 > 32
-                int iPut = tmpBytes.length;
-
-                tmpBytes[--iPut] = (byte) (tagNo & 0x7f);
-                do {
-                    tagNo >>= 7;
-                    tmpBytes[--iPut] = (byte) (tagNo & 0x7f | 0x80);
-                } while (tagNo > 127);
-
-                buffer.put(tmpBytes, iPut, tmpBytes.length - iPut);
-            }
-        }
-    }
-
-    public static void encodeLength(ByteBuffer buffer, int bodyLength) {
-        if (bodyLength < 128) {
-            buffer.put((byte) bodyLength);
-        } else {
-            int length = 0;
-            int payload = bodyLength;
-
-            while (payload != 0) {
-                payload >>= 8;
-                length++;
-            }
-
-            buffer.put((byte) (length | 0x80));
-
-            payload = bodyLength;
-            for (int i = length - 1; i >= 0; i--) {
-                buffer.put((byte) (payload >> (i * 8)));
-            }
-        }
-    }
-
-    public static Tag readTag(ByteBuffer buffer) throws IOException {
-        int tagFlags = readTagFlags(buffer);
-        int tagNo = readTagNo(buffer, tagFlags);
-        return new Tag(tagFlags, tagNo);
-    }
-
-    private static int readTagFlags(ByteBuffer buffer) throws IOException {
-        int tagFlags = buffer.get() & 0xff;
-        if (tagFlags == 0) {
-            throw new IOException("Bad tag 0 found");
-        }
-        return tagFlags;
-    }
-
-    private static int readTagNo(ByteBuffer buffer, int tagFlags) throws IOException {
-        int tagNo = tagFlags & 0x1f;
-
-        if (tagNo == 0x1f) {
-            tagNo = 0;
-
-            int b = buffer.get() & 0xff;
-            if ((b & 0x7f) == 0) {
-                throw new IOException("Invalid high tag number found");
-            }
-
-            while (b >= 0 && (b & 0x80) != 0) {
-                tagNo |= b & 0x7f;
-                tagNo <<= 7;
-                b = buffer.get();
-            }
-
-            tagNo |= b & 0x7f;
-        }
-
-        return tagNo;
-    }
-
-    public static int readLength(ByteBuffer buffer) throws IOException {
-        int result = buffer.get() & 0xff;
-        if (result == 0x80) {
-            return -1; // non-definitive length
-        }
-
-        if (result > 127) {
-            int length = result & 0x7f;
-            if (length > 4) {
-                throw new IOException("Bad length of more than 4 bytes: " + length);
-            }
-
-            result = 0;
-            int tmp;
-            for (int i = 0; i < length; i++) {
-                tmp = buffer.get() & 0xff;
-                result = (result << 8) + tmp;
-            }
-        }
-
-        if (result < 0) {
-            throw new IOException("Invalid length " + result);
-        }
-
-        if (result > buffer.remaining()) {
-            throw new IOException("Corrupt stream - less data "
-                + buffer.remaining() + " than expected " + result);
-        }
-
-        return result;
-    }
-
-    public static ByteBuffer dupWithLength(ByteBuffer buffer, int length) {
-        try {
-            ByteBuffer result = buffer.duplicate();
-            result.limit(buffer.position() + length);
-            buffer.position(buffer.position() + length);
-            return result;
-        } catch (Exception e) {
-            throw e;
-        }
-    }
-
-    protected static byte[] readAllLeftBytes(ByteBuffer buffer) {
-        byte[] result = new byte[buffer.remaining()];
-        buffer.get(result);
-        return result;
-    }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f98e113b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1OctetString.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1OctetString.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1OctetString.java
index 9271fea..6523cf7 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1OctetString.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1OctetString.java
@@ -19,6 +19,7 @@
  */
 package org.apache.kerby.asn1.type;
 
+import org.apache.kerby.asn1.Asn1Util;
 import org.apache.kerby.asn1.UniversalTag;
 
 import java.io.IOException;
@@ -45,6 +46,6 @@ public class Asn1OctetString extends Asn1Simple<byte[]> {
 
     @Override
     protected void decodeBody(ByteBuffer content) throws IOException {
-        setValue(readAllLeftBytes(content));
+        setValue(Asn1Util.readAllLeftBytes(content));
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f98e113b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Simple.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Simple.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Simple.java
index 714e230..d55fb7a 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Simple.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Simple.java
@@ -19,6 +19,7 @@
  */
 package org.apache.kerby.asn1.type;
 
+import org.apache.kerby.asn1.Asn1Util;
 import org.apache.kerby.asn1.Tag;
 import org.apache.kerby.asn1.UniversalTag;
 
@@ -59,9 +60,9 @@ public abstract class Asn1Simple<T> extends AbstractAsn1Type<T>
{
 
     @Override
     public void encode(ByteBuffer buffer) {
-        encodeTag(buffer, tag());
+        Asn1Util.encodeTag(buffer, tag());
         int bodyLen = encodingBodyLength();
-        encodeLength(buffer, bodyLen);
+        Asn1Util.encodeLength(buffer, bodyLen);
         if (bodyLen > 0) {
             buffer.put(encodeBody());
         }
@@ -92,7 +93,7 @@ public abstract class Asn1Simple<T> extends AbstractAsn1Type<T>
{
 
     @Override
     protected void decodeBody(ByteBuffer content) throws IOException {
-        byte[] leftBytes = readAllLeftBytes(content);
+        byte[] leftBytes = Asn1Util.readAllLeftBytes(content);
         if (leftBytes.length > 0) {
             setBytes(leftBytes);
             toValue();

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f98e113b/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/KrbCodec.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/KrbCodec.java
b/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/KrbCodec.java
index 2197a6a..5157cdc 100644
--- a/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/KrbCodec.java
+++ b/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/KrbCodec.java
@@ -19,8 +19,8 @@
  */
 package org.apache.kerby.kerberos.kerb;
 
+import org.apache.kerby.asn1.Asn1Util;
 import org.apache.kerby.asn1.Tag;
-import org.apache.kerby.asn1.type.Asn1Object;
 import org.apache.kerby.asn1.type.Asn1Type;
 import org.apache.kerby.kerberos.kerb.type.ap.ApReq;
 import org.apache.kerby.kerberos.kerb.type.base.KrbError;
@@ -64,9 +64,9 @@ public class KrbCodec {
     }
 
     public static KrbMessage decodeMessage(ByteBuffer byteBuffer) throws IOException {
-        Tag tag = Asn1Object.readTag(byteBuffer);
-        int length = Asn1Object.readLength(byteBuffer);
-        ByteBuffer valueBuffer = Asn1Object.dupWithLength(byteBuffer, length);
+        Tag tag = Asn1Util.readTag(byteBuffer);
+        int length = Asn1Util.readLength(byteBuffer);
+        ByteBuffer valueBuffer = Asn1Util.dupWithLength(byteBuffer, length);
 
         KrbMessage msg;
         KrbMessageType msgType = KrbMessageType.fromValue(tag.tagNo());


Mime
View raw message