directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dran...@apache.org
Subject [1/2] directory-kerby git commit: ASN1. Use Tag class to simplify
Date Tue, 01 Dec 2015 03:53:14 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/master dbc73db7a -> 8b0ef7194


ASN1. Use Tag class to simplify


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

Branch: refs/heads/master
Commit: 66fce42a067fb371e6ce56fe24b28274db597c5b
Parents: 27038b8
Author: Kai Zheng <kai.zheng@intel.com>
Authored: Tue Dec 1 11:51:52 2015 +0800
Committer: Kai Zheng <kai.zheng@intel.com>
Committed: Tue Dec 1 11:51:52 2015 +0800

----------------------------------------------------------------------
 .../main/java/org/apache/kerby/asn1/Tag.java    | 140 +++++++++++++++
 .../java/org/apache/kerby/asn1/TagClass.java    |   2 +-
 .../org/apache/kerby/asn1/TaggingOption.java    |   8 +-
 .../kerby/asn1/type/AbstractAsn1Type.java       |  47 +++--
 .../org/apache/kerby/asn1/type/Asn1Any.java     |   3 +-
 .../org/apache/kerby/asn1/type/Asn1Choice.java  |   8 +-
 .../apache/kerby/asn1/type/Asn1Collection.java  |  15 +-
 .../kerby/asn1/type/Asn1CollectionOf.java       |   6 +-
 .../kerby/asn1/type/Asn1CollectionType.java     |  15 +-
 .../org/apache/kerby/asn1/type/Asn1Item.java    |  27 +--
 .../org/apache/kerby/asn1/type/Asn1Object.java  | 170 ++++++++-----------
 .../apache/kerby/asn1/type/Asn1Sequence.java    |   3 +-
 .../apache/kerby/asn1/type/Asn1SequenceOf.java  |   3 +-
 .../kerby/asn1/type/Asn1SequenceType.java       |   2 +-
 .../org/apache/kerby/asn1/type/Asn1Set.java     |   3 +-
 .../org/apache/kerby/asn1/type/Asn1SetOf.java   |   3 +-
 .../org/apache/kerby/asn1/type/Asn1SetType.java |   2 +-
 .../org/apache/kerby/asn1/type/Asn1Simple.java  |  18 +-
 .../org/apache/kerby/asn1/type/Asn1String.java  |   4 +-
 .../org/apache/kerby/asn1/type/Asn1Tagging.java |  13 +-
 .../org/apache/kerby/asn1/type/Asn1Type.java    |  24 +--
 .../kerby/asn1/type/TaggingCollection.java      |  13 +-
 .../org/apache/kerby/asn1/type/TaggingSet.java  |   3 +-
 .../kerby/kerberos/kerb/codec/CodecTest.java    |   3 +-
 .../apache/kerby/kerberos/kerb/KrbCodec.java    |   9 +-
 25 files changed, 312 insertions(+), 232 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/Tag.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/Tag.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/Tag.java
new file mode 100644
index 0000000..bea843f
--- /dev/null
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/Tag.java
@@ -0,0 +1,140 @@
+/**
+ *  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;
+
+/**
+ * ASN1, a tagged system.
+ */
+public class Tag {
+    private int tagFlags = 0;
+    private int tagNo = 0;
+
+    public Tag(int tag) {
+        this.tagFlags = tag & 0xE0;
+        this.tagNo = tag & 0x1F;
+    }
+
+    public Tag(UniversalTag tag) {
+        this.tagFlags = TagClass.UNIVERSAL.getValue();
+        this.tagNo = tag.getValue();
+    }
+
+    public Tag(int tagFlags, int tagNo) {
+        this.tagFlags = tagFlags & 0xE0;
+        this.tagNo = tagNo;
+    }
+
+    public Tag(TagClass tagClass, int tagNo) {
+        this.tagFlags = tagClass.getValue();
+        this.tagNo = tagNo;
+    }
+
+    public Tag(Tag other) {
+        this(other.tagFlags, other.tagNo);
+    }
+
+    public TagClass tagClass() {
+        return TagClass.fromTag(tagFlags);
+    }
+
+    public void usePrimitive(boolean isPrimitive) {
+        if (isPrimitive) {
+            tagFlags &= ~0x20;
+        } else {
+            tagFlags |= 0x20;
+        }
+    }
+
+    public boolean isPrimitive() {
+        return (tagFlags & 0x20) == 0;
+    }
+
+    public int tagFlags() {
+        return tagFlags;
+    }
+
+    public int tagNo() {
+        return tagNo;
+    }
+
+    public UniversalTag universalTag() {
+        if (isUniversal()) {
+            return UniversalTag.fromValue(tagNo());
+        }
+        return UniversalTag.UNKNOWN;
+    }
+
+    public boolean isUniversal() {
+        return tagClass().isUniversal();
+    }
+
+    public boolean isAppSpecific() {
+        return tagClass().isAppSpecific();
+    }
+
+    public boolean isContextSpecific() {
+        return tagClass().isContextSpecific();
+    }
+
+    public boolean isTagged() {
+        return tagClass().isTagged();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        Tag tag = (Tag) o;
+
+        if (tagFlags != tag.tagFlags) {
+            return false;
+        }
+
+        return tagNo == tag.tagNo;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = tagFlags;
+        result = 31 * result + tagNo;
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        return "Tag["
+            + "tagFlags=" + tagFlags
+            + ", tagNo=" + tagNo
+            + ']';
+    }
+
+    public static Tag newAppTag(int tagNo) {
+        return new Tag(TagClass.APPLICATION, tagNo);
+    }
+
+    public static Tag newCtxTag(int tagNo) {
+        return new Tag(TagClass.CONTEXT_SPECIFIC, tagNo);
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/TagClass.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/TagClass.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/TagClass.java
index cbd02fc..8070692 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/TagClass.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/TagClass.java
@@ -93,7 +93,7 @@ public enum TagClass {
      * @param tag The int value of tag class
      * @return tag class
      */
-    public static TagClass fromTagFlags(int tag) {
+    public static TagClass fromTag(int tag) {
         return fromValue(tag & 0xC0);
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/TaggingOption.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/TaggingOption.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/TaggingOption.java
index 8bb0419..898c25e 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/TaggingOption.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/TaggingOption.java
@@ -19,8 +19,6 @@
  */
 package org.apache.kerby.asn1;
 
-import org.apache.kerby.asn1.type.Asn1Type;
-
 /**
  * Tagging option for tagging an ASN1 type.
  */
@@ -82,12 +80,12 @@ public final class TaggingOption {
      * @param isTaggedConstructed Tagged Constructed or not
      * @return tag flag
      */
-    public int tagFlags(boolean isTaggedConstructed) {
+    public Tag getTag(boolean isTaggedConstructed) {
         boolean isConstructed = isImplicit ? isTaggedConstructed : true;
         TagClass tagClass = isAppSpecific ? TagClass.APPLICATION : TagClass.CONTEXT_SPECIFIC;
-        int flags = tagClass.getValue() | (isConstructed ? Asn1Type.CONSTRUCTED_FLAG : 0x00);
+        int flags = tagClass.getValue() | (isConstructed ? 0x20 : 0x00);
 
-        return flags;
+        return new Tag(flags, tagNo);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/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 4dc2c28..b12c67d 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
@@ -19,7 +19,8 @@
  */
 package org.apache.kerby.asn1.type;
 
-import org.apache.kerby.asn1.TagClass;
+import org.apache.kerby.asn1.Tag;
+import org.apache.kerby.asn1.UniversalTag;
 
 /**
  * The abstract ASN1 type for all the ASN1 types. It provides basic
@@ -32,43 +33,39 @@ public abstract class AbstractAsn1Type<T> extends Asn1Object {
     private T value;
 
     /**
-     * Default constructor, generally for decoding as a value container
-     * @param tagClass The tag class
-     * @param tagNo The tag number
+     * Default constructor.
+     * @param tag the tag
+     * @param value the value
      */
-    public AbstractAsn1Type(TagClass tagClass, int tagNo) {
-        this(tagClass, tagNo, null);
+    public AbstractAsn1Type(Tag tag, T value) {
+        super(tag);
+        this.value = value;
     }
 
     /**
-     * Default constructor, generally for decoding as a value container
-     * @param tagFlags The tag flags
-     * @param tagNo The tag number
+     * Default constructor.
+     * @param tag the tag
      */
-    public AbstractAsn1Type(int tagFlags, int tagNo) {
-        this(tagFlags, tagNo, null);
+    public AbstractAsn1Type(Tag tag) {
+        super(tag);
     }
 
     /**
-     * Constructor with a value, generally for encoding of the value
-     * @param tagFlags The tag flags
-     * @param tagNo The tag number
-     * @param value The value
+     * Default constructor with an universal tag.
+     * @param tag the tag
+     * @param value the value
      */
-    public AbstractAsn1Type(int tagFlags, int tagNo, T value) {
-        this(TagClass.fromTagFlags(tagFlags), tagNo, value);
-        setTagFlags(tagFlags);
+    public AbstractAsn1Type(UniversalTag tag, T value) {
+        super(tag);
+        this.value = value;
     }
 
     /**
-     * Constructor with a value, generally for encoding of the value
-     * @param tagClass The tag class
-     * @param tagNo The tag number
-     * @param value The value
+     * Default constructor with an universal tag.
+     * @param tag the tag
      */
-    public AbstractAsn1Type(TagClass tagClass, int tagNo, T value) {
-        super(tagClass, tagNo);
-        this.value = value;
+    public AbstractAsn1Type(UniversalTag tag) {
+        super(tag);
     }
 
     public T getValue() {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Any.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Any.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Any.java
index 73a82c7..0506edd 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Any.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Any.java
@@ -20,7 +20,6 @@
 package org.apache.kerby.asn1.type;
 
 import org.apache.kerby.asn1.LimitedByteBuffer;
-import org.apache.kerby.asn1.TagClass;
 import org.apache.kerby.asn1.UniversalTag;
 
 import java.io.IOException;
@@ -36,7 +35,7 @@ public class Asn1Any extends AbstractAsn1Type<Asn1Type> {
     private Asn1Type field;
 
     public Asn1Any() {
-        super(TagClass.UNIVERSAL, UniversalTag.ANY.getValue());
+        super(UniversalTag.ANY);
     }
 
     // For encoding phase.

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Choice.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Choice.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Choice.java
index 3641070..a2a9504 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Choice.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Choice.java
@@ -21,7 +21,6 @@ package org.apache.kerby.asn1.type;
 
 import org.apache.kerby.asn1.Asn1FieldInfo;
 import org.apache.kerby.asn1.LimitedByteBuffer;
-import org.apache.kerby.asn1.TagClass;
 import org.apache.kerby.asn1.TaggingOption;
 import org.apache.kerby.asn1.UniversalTag;
 
@@ -34,7 +33,7 @@ public class Asn1Choice extends AbstractAsn1Type<Asn1Type> {
     private Asn1Type[] fields;
 
     public Asn1Choice(Asn1FieldInfo[] fieldInfos) {
-        super(TagClass.UNIVERSAL, UniversalTag.CHOICE.getValue());
+        super(UniversalTag.CHOICE);
         setValue(this);
         this.fieldInfos = fieldInfos.clone();
         this.fields = new Asn1Type[fieldInfos.length];
@@ -84,8 +83,7 @@ public class Asn1Choice extends AbstractAsn1Type<Asn1Type> {
                 }
             } else {
                 initField(i);
-                if (fields[i].tagFlags() == item.tagFlags()
-                        && fields[i].tagNo() == item.tagNo()) {
+                if (fields[i].tag().equals(item.tag())) {
                     foundPos = i;
                     break;
                 } else {
@@ -95,7 +93,7 @@ public class Asn1Choice extends AbstractAsn1Type<Asn1Type> {
         }
         if (foundPos == -1) {
             throw new RuntimeException("Unexpected item with (tagFlags, tagNo): ("
-                    + item.tagFlags() + ", " + item.tagNo() + ")");
+                    + item.tag() + ", " + item.tagNo() + ")");
         }
 
         if (!item.isFullyDecoded()) {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java
index 54a2e9d..52c46c0 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java
@@ -20,7 +20,7 @@
 package org.apache.kerby.asn1.type;
 
 import org.apache.kerby.asn1.LimitedByteBuffer;
-import org.apache.kerby.asn1.TagClass;
+import org.apache.kerby.asn1.Tag;
 import org.apache.kerby.asn1.UniversalTag;
 
 import java.io.IOException;
@@ -32,8 +32,9 @@ import java.util.List;
  * ASN1 complex type, may be better named.
  */
 public class Asn1Collection extends AbstractAsn1Type<List<Asn1Item>> {
-    public Asn1Collection(TagClass tagClass, int tagNo) {
-        super(tagClass, tagNo);
+
+    public Asn1Collection(UniversalTag universalTag) {
+        super(universalTag);
         setValue(new ArrayList<Asn1Item>());
         usePrimitive(false);
     }
@@ -84,8 +85,12 @@ public class Asn1Collection extends AbstractAsn1Type<List<Asn1Item>> {
         }
     }
 
-    public static boolean isCollection(int tagNo) {
-        return isCollection(UniversalTag.fromValue(tagNo));
+    public static boolean isCollection(Tag tag) {
+        return isCollection(tag.universalTag());
+    }
+
+    public static boolean isCollection(int tag) {
+        return isCollection(new Tag(tag));
     }
 
     public static boolean isCollection(UniversalTag tagNo) {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionOf.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionOf.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionOf.java
index 76ed6f5..6d7f36b 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionOf.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionOf.java
@@ -19,7 +19,7 @@
  */
 package org.apache.kerby.asn1.type;
 
-import org.apache.kerby.asn1.TagClass;
+import org.apache.kerby.asn1.UniversalTag;
 
 import java.io.IOException;
 import java.lang.reflect.ParameterizedType;
@@ -28,8 +28,8 @@ import java.util.List;
 
 public abstract class Asn1CollectionOf<T extends Asn1Type> extends Asn1Collection
 {
-    public Asn1CollectionOf(TagClass tagClass, int tagNo) {
-        super(tagClass, tagNo);
+    public Asn1CollectionOf(UniversalTag universalTag) {
+        super(universalTag);
     }
 
     public List<T> getElements() {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java
index 72278d3..fc45949 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java
@@ -21,8 +21,8 @@ package org.apache.kerby.asn1.type;
 
 import org.apache.kerby.asn1.Asn1FieldInfo;
 import org.apache.kerby.asn1.LimitedByteBuffer;
-import org.apache.kerby.asn1.TagClass;
 import org.apache.kerby.asn1.TaggingOption;
+import org.apache.kerby.asn1.UniversalTag;
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
@@ -34,8 +34,9 @@ public abstract class Asn1CollectionType extends AbstractAsn1Type<Asn1Collection
     private Asn1FieldInfo[] fieldInfos;
     private Asn1Type[] fields;
 
-    public Asn1CollectionType(int universalTagNo, Asn1FieldInfo[] fieldInfos) {
-        super(TagClass.UNIVERSAL, universalTagNo);
+    public Asn1CollectionType(UniversalTag universalTag, Asn1FieldInfo[] fieldInfos) {
+        super(universalTag);
+
         setValue(this);
         this.fieldInfos = fieldInfos.clone();
         this.fields = new Asn1Type[fieldInfos.length];
@@ -79,7 +80,7 @@ public abstract class Asn1CollectionType extends AbstractAsn1Type<Asn1Collection
         initFields();
 
         Asn1Collection coll = createCollection();
-        coll.decode(tagFlags(), tagNo(), content);
+        coll.decode(tag(), content);
 
         int lastPos = -1, foundPos = -1;
         for (Asn1Item item : coll.getValue()) {
@@ -90,15 +91,13 @@ public abstract class Asn1CollectionType extends AbstractAsn1Type<Asn1Collection
                         foundPos = i;
                         break;
                     }
-                } else if (fields[i].tagFlags() == item.tagFlags()
-                        && fields[i].tagNo() == item.tagNo()) {
+                } else if (fields[i].tag().equals(item.tag())) {
                     foundPos = i;
                     break;
                 }
             }
             if (foundPos == -1) {
-                throw new RuntimeException("Unexpected item with (tagFlags, tagNo): ("
-                        + item.tagFlags() + ", " + item.tagNo() + ")");
+                throw new RuntimeException("Unexpected item with tag: " + item.tag());
             }
 
             if (!item.isFullyDecoded()) {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Item.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Item.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Item.java
index 74ef10a..a7924d8 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Item.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Item.java
@@ -21,6 +21,7 @@ package org.apache.kerby.asn1.type;
 
 import org.apache.kerby.asn1.Asn1Factory;
 import org.apache.kerby.asn1.LimitedByteBuffer;
+import org.apache.kerby.asn1.Tag;
 import org.apache.kerby.asn1.TaggingOption;
 
 import java.io.IOException;
@@ -42,11 +43,11 @@ public class Asn1Item extends AbstractAsn1Type<Asn1Type> {
     private LimitedByteBuffer bodyContent;
 
     public Asn1Item(Asn1Type value) {
-        super(value.tagFlags(), value.tagNo(), value);
+        super(value.tag(), value);
     }
 
-    public Asn1Item(int tag, int tagNo, LimitedByteBuffer bodyContent) {
-        super(tag, tagNo);
+    public Asn1Item(Tag tag, LimitedByteBuffer bodyContent) {
+        super(tag);
         this.bodyContent = bodyContent;
     }
 
@@ -120,23 +121,7 @@ public class Asn1Item extends AbstractAsn1Type<Asn1Type> {
 
     public void decodeValueWith(Asn1Type value) throws IOException {
         setValue(value);
-        ((AbstractAsn1Type<?>) value).decode(tagFlags(), tagNo(), bodyContent);
-    }
-
-    public void decodeValueAsImplicitTagged(int originalTag, int originalTagNo) throws IOException {
-        if (!isTagged()) {
-            throw new IllegalArgumentException("Attempting to decode non-tagged value using tagging way");
-        }
-        Asn1Item taggedValue = new Asn1Item(originalTag, originalTagNo, getBodyContent());
-        decodeValueWith(taggedValue);
-    }
-
-    public void decodeValueAsExplicitTagged() throws IOException {
-        if (!isTagged()) {
-            throw new IllegalArgumentException("Attempting to decode non-tagged value using tagging way");
-        }
-        Asn1Item taggedValue = decodeOne(getBodyContent());
-        decodeValueWith(taggedValue);
+        ((AbstractAsn1Type<?>) value).decode(tag(), bodyContent);
     }
 
     private void decodeValueWith(Asn1Item taggedValue) throws IOException {
@@ -152,7 +137,7 @@ public class Asn1Item extends AbstractAsn1Type<Asn1Type> {
         if (!isTagged()) {
             throw new IllegalArgumentException("Attempting to decode non-tagged value using tagging way");
         }
-        ((AbstractAsn1Type<?>) value).taggedDecode(tagFlags(), tagNo(), getBodyContent(), taggingOption);
+        ((AbstractAsn1Type<?>) value).taggedDecode(tag(), getBodyContent(), taggingOption);
         setValue(value);
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/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 95da847..aa9ebe2 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
@@ -20,8 +20,9 @@
 package org.apache.kerby.asn1.type;
 
 import org.apache.kerby.asn1.LimitedByteBuffer;
-import org.apache.kerby.asn1.TagClass;
+import org.apache.kerby.asn1.Tag;
 import org.apache.kerby.asn1.TaggingOption;
+import org.apache.kerby.asn1.UniversalTag;
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
@@ -31,10 +32,8 @@ import java.nio.ByteBuffer;
  * encoding and decoding utilities.
  */
 public abstract class Asn1Object implements Asn1Type {
+    private final Tag tag;
 
-    private TagClass tagClass = TagClass.UNKNOWN;
-    private int tagNo = -1;
-    private int tagFlags = 0;
     private int encodingLen = -1;
 
     // encoding options
@@ -43,66 +42,50 @@ public abstract class Asn1Object implements Asn1Type {
     private boolean isDefinitiveLength = false;
 
     /**
-     * Constructor with a value, generally for encoding of the value
-     * @param tagFlags The tag flags
-     * @param tagNo The tag number
+     * Constructor with a tag
+     * @param tag The tag
      */
-    public Asn1Object(int tagFlags, int tagNo) {
-        this(TagClass.fromTagFlags(tagFlags), tagNo);
-        setTagFlags(tagFlags);
+    public Asn1Object(Tag tag) {
+        this.tag = new Tag(tag);
     }
 
     /**
-     * Constructor with a value, generally for encoding of the value
-     * @param tagClass The tag class
-     * @param tagNo The tag number
+     * Default constructor with an universal tag.
+     * @param tag the tag
      */
-    public Asn1Object(TagClass tagClass, int tagNo) {
-        this.tagClass = tagClass;
-        this.tagNo = tagNo;
-
-        this.tagFlags |= tagClass.getValue();
-    }
-
-    @Override
-    public TagClass tagClass() {
-        return tagClass;
-    }
-
-    protected void setTagClass(TagClass tagClass) {
-        this.tagClass = tagClass;
+    public Asn1Object(UniversalTag tag) {
+        this.tag = new Tag(tag);
     }
 
-    @Override
-    public int tagFlags() {
-        return tagFlags;
+    /**
+     * Constructor with a tag
+     * @param tag The tag
+     */
+    public Asn1Object(int tag) {
+        this.tag = new Tag(tag);
     }
 
     @Override
-    public int tagNo() {
-        return tagNo;
+    public Tag tag() {
+        return tag;
     }
 
-    protected void setTagFlags(int tagFlags) {
-        this.tagFlags = tagFlags & 0xe0;
+    protected int tagFlags() {
+        return tag().tagFlags();
     }
 
-    protected void setTagNo(int tagNo) {
-        this.tagNo = tagNo;
+    protected int tagNo() {
+        return tag().tagNo();
     }
 
     @Override
     public void usePrimitive(boolean isPrimitive) {
-        if (isPrimitive) {
-            tagFlags &= ~CONSTRUCTED_FLAG;
-        } else {
-            tagFlags |= CONSTRUCTED_FLAG;
-        }
+        tag.usePrimitive(isPrimitive);
     }
 
     @Override
     public boolean isPrimitive() {
-        return (tagFlags & CONSTRUCTED_FLAG) == 0;
+        return tag.isPrimitive();
     }
 
     @Override
@@ -165,7 +148,7 @@ public abstract class Asn1Object implements Asn1Type {
 
     @Override
     public void encode(ByteBuffer buffer) {
-        encodeTag(buffer, tagFlags(), tagNo());
+        encodeTag(buffer, tag());
         encodeLength(buffer, encodingBodyLength());
         encodeBody(buffer);
     }
@@ -186,61 +169,50 @@ public abstract class Asn1Object implements Asn1Type {
     public int encodingLength() {
         if (encodingLen == -1) {
             int bodyLen = encodingBodyLength();
-            encodingLen = lengthOfTagLength(tagNo()) + lengthOfBodyLength(bodyLen) + bodyLen;
+            encodingLen = lengthOfTagLength(tagNo())
+                + lengthOfBodyLength(bodyLen) + bodyLen;
         }
         return encodingLen;
     }
 
-    public boolean isUniversal() {
-        return tagClass.isUniversal();
+    protected boolean isUniversal() {
+        return tag.isUniversal();
     }
 
-    public boolean isAppSpecific() {
-        return tagClass.isAppSpecific();
+    protected boolean isAppSpecific() {
+        return tag.isAppSpecific();
     }
 
-    public boolean isContextSpecific() {
-        return tagClass.isContextSpecific();
+    protected boolean isContextSpecific() {
+        return tag.isContextSpecific();
     }
 
-    public boolean isTagged() {
-        return tagClass.isTagged();
+    protected boolean isTagged() {
+        return tag.isTagged();
     }
 
     public boolean isSimple() {
-        return isUniversal() && Asn1Simple.isSimple(tagNo);
+        return Asn1Simple.isSimple(tag());
     }
 
     public boolean isCollection() {
-        return isUniversal() && Asn1Collection.isCollection(tagNo);
+        return Asn1Collection.isCollection(tag());
     }
 
     protected abstract int encodingBodyLength();
 
     protected void decode(LimitedByteBuffer content) throws IOException {
-        int tag = readTag(content);
-        int tagNo = readTagNo(content, tag);
-        int tmpTagFlags = tag & 0xe0;
+        Tag tmpTag = readTag(content);
         int length = readLength(content);
 
-        decode(tmpTagFlags, tagNo, new LimitedByteBuffer(content, length));
+        decode(tmpTag, new LimitedByteBuffer(content, length));
     }
 
-    public void decode(int tagFlags, int tagNo,
-                       LimitedByteBuffer content) throws IOException {
-        if (tagClass() != TagClass.UNKNOWN && tagClass()
-                != TagClass.fromTagFlags(tagFlags)) {
-            throw new IOException("Unexpected tagFlags " + tagFlags
-                    + ", expecting " + tagClass());
+    public void decode(Tag tag, LimitedByteBuffer content) throws IOException {
+        if (!tag().equals(tag)) {
+            throw new IOException("Unexpected tag " + tag
+                    + ", expecting " + tag());
         }
-        if (tagNo() != -1 && tagNo() != tagNo) {
-            throw new IOException("Unexpected tagNo " + tagNo + ", "
-                    + "expecting " + tagNo());
-        }
-
-        setTagClass(TagClass.fromTagFlags(tagFlags));
-        setTagFlags(tagFlags);
-        setTagNo(tagNo);
 
         decodeBody(content);
     }
@@ -265,8 +237,8 @@ public abstract class Asn1Object implements Asn1Type {
 
     @Override
     public void taggedEncode(ByteBuffer buffer, TaggingOption taggingOption) {
-        int taggingTagFlags = taggingOption.tagFlags(!isPrimitive());
-        encodeTag(buffer, taggingTagFlags, taggingOption.getTagNo());
+        Tag taggingTag = taggingOption.getTag(!isPrimitive());
+        encodeTag(buffer, taggingTag);
         int taggingBodyLen = taggingOption.isImplicit() ? encodingBodyLength()
                 : encodingLength();
         encodeLength(buffer, taggingBodyLen);
@@ -291,27 +263,20 @@ public abstract class Asn1Object implements Asn1Type {
 
     protected void taggedDecode(LimitedByteBuffer content,
                                 TaggingOption taggingOption) throws IOException {
-        int taggingTag = readTag(content);
-        int taggingTagNo = readTagNo(content, taggingTag);
+        Tag taggingTag = readTag(content);
         int taggingLength = readLength(content);
         LimitedByteBuffer newContent = new LimitedByteBuffer(content, taggingLength);
 
-        int tmpTagFlags = taggingTag & 0xe0;
-        taggedDecode(tmpTagFlags, taggingTagNo, newContent, taggingOption);
+        taggedDecode(taggingTag, newContent, taggingOption);
     }
 
-    protected void taggedDecode(int taggingTagFlags, int taggingTagNo,
-                                LimitedByteBuffer content,
+    protected void taggedDecode(Tag taggingTag, LimitedByteBuffer content,
                                 TaggingOption taggingOption) throws IOException {
-        int expectedTaggingTagFlags = taggingOption.tagFlags(!isPrimitive());
-        if (expectedTaggingTagFlags != taggingTagFlags) {
-            throw new IOException("Unexpected tag flags " + taggingTagFlags
+        Tag expectedTaggingTagFlags = taggingOption.getTag(!isPrimitive());
+        if (!expectedTaggingTagFlags.equals(taggingTag)) {
+            throw new IOException("Unexpected tag " + taggingTag
                     + ", expecting " + expectedTaggingTagFlags);
         }
-        if (taggingOption.getTagNo() != taggingTagNo) {
-            throw new IOException("Unexpected tagNo " + taggingTagNo + ", "
-                    + "expecting " + taggingOption.getTagNo());
-        }
 
         if (taggingOption.isImplicit()) {
             decodeBody(content);
@@ -321,8 +286,7 @@ public abstract class Asn1Object implements Asn1Type {
     }
 
     public static Asn1Item decodeOne(LimitedByteBuffer content) throws IOException {
-        int tag = readTag(content);
-        int tagNo = readTagNo(content, tag);
+        Tag tmpTag = readTag(content);
         int length = readLength(content);
         if (length < 0) {
             throw new IOException("Unexpected length");
@@ -330,7 +294,7 @@ public abstract class Asn1Object implements Asn1Type {
         LimitedByteBuffer valueContent = new LimitedByteBuffer(content, length);
         content.skip(length);
 
-        Asn1Item result = new Asn1Item(tag, tagNo, valueContent);
+        Asn1Item result = new Asn1Item(tmpTag, valueContent);
         if (result.isSimple()) {
             result.decodeValueAsSimple();
         }
@@ -338,8 +302,7 @@ public abstract class Asn1Object implements Asn1Type {
     }
 
     public static void skipOne(LimitedByteBuffer content) throws IOException {
-        int tag = readTag(content);
-        readTagNo(content, tag);
+        readTag(content);
         int length = readLength(content);
         if (length < 0) {
             throw new IOException("Unexpected length");
@@ -380,7 +343,10 @@ public abstract class Asn1Object implements Asn1Type {
         return length;
     }
 
-    public static void encodeTag(ByteBuffer buffer, int flags, int tagNo) {
+    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 {
@@ -423,16 +389,22 @@ public abstract class Asn1Object implements Asn1Type {
         }
     }
 
-    public static int readTag(LimitedByteBuffer buffer) throws IOException {
-        int tag = buffer.readByte() & 0xff;
-        if (tag == 0) {
+    public static Tag readTag(LimitedByteBuffer buffer) throws IOException {
+        int tagFlags = readTagFlags(buffer);
+        int tagNo = readTagNo(buffer, tagFlags);
+        return new Tag(tagFlags, tagNo);
+    }
+
+    private static int readTagFlags(LimitedByteBuffer buffer) throws IOException {
+        int tagFlags = buffer.readByte() & 0xff;
+        if (tagFlags == 0) {
             throw new IOException("Bad tag 0 found");
         }
-        return tag;
+        return tagFlags;
     }
 
-    public static int readTagNo(LimitedByteBuffer buffer, int tag) throws IOException {
-        int tagNo = tag & 0x1f;
+    private static int readTagNo(LimitedByteBuffer buffer, int tagFlags) throws IOException {
+        int tagNo = tagFlags & 0x1f;
 
         if (tagNo == 0x1f) {
             tagNo = 0;

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Sequence.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Sequence.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Sequence.java
index 2e483d2..c5a7dfc 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Sequence.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Sequence.java
@@ -19,11 +19,10 @@
  */
 package org.apache.kerby.asn1.type;
 
-import org.apache.kerby.asn1.TagClass;
 import org.apache.kerby.asn1.UniversalTag;
 
 public class Asn1Sequence extends Asn1Collection {
     public Asn1Sequence() {
-        super(TagClass.UNIVERSAL, UniversalTag.SEQUENCE.getValue());
+        super(UniversalTag.SEQUENCE);
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SequenceOf.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SequenceOf.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SequenceOf.java
index 5dc69e0..074a8b7 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SequenceOf.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SequenceOf.java
@@ -19,12 +19,11 @@
  */
 package org.apache.kerby.asn1.type;
 
-import org.apache.kerby.asn1.TagClass;
 import org.apache.kerby.asn1.UniversalTag;
 
 public class Asn1SequenceOf<T extends Asn1Type> extends Asn1CollectionOf<T> {
     public Asn1SequenceOf() {
-        super(TagClass.UNIVERSAL, UniversalTag.SEQUENCE_OF.getValue());
+        super(UniversalTag.SEQUENCE_OF);
     }
 
     public boolean isEmpty() {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SequenceType.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SequenceType.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SequenceType.java
index 4733838..85d2763 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SequenceType.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SequenceType.java
@@ -28,7 +28,7 @@ import org.apache.kerby.asn1.UniversalTag;
 public class Asn1SequenceType extends Asn1CollectionType {
 
     public Asn1SequenceType(Asn1FieldInfo[] tags) {
-        super(UniversalTag.SEQUENCE.getValue(), tags);
+        super(UniversalTag.SEQUENCE, tags);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Set.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Set.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Set.java
index 500c6f1..c2cded4 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Set.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Set.java
@@ -19,11 +19,10 @@
  */
 package org.apache.kerby.asn1.type;
 
-import org.apache.kerby.asn1.TagClass;
 import org.apache.kerby.asn1.UniversalTag;
 
 public class Asn1Set extends Asn1Collection {
     public Asn1Set() {
-        super(TagClass.UNIVERSAL, UniversalTag.SET.getValue());
+        super(UniversalTag.SET);
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SetOf.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SetOf.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SetOf.java
index 2838538..a351eaf 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SetOf.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SetOf.java
@@ -19,11 +19,10 @@
  */
 package org.apache.kerby.asn1.type;
 
-import org.apache.kerby.asn1.TagClass;
 import org.apache.kerby.asn1.UniversalTag;
 
 public class Asn1SetOf<T extends Asn1Type> extends Asn1CollectionOf<T> {
     public Asn1SetOf() {
-        super(TagClass.UNIVERSAL, UniversalTag.SET_OF.getValue());
+        super(UniversalTag.SET_OF);
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SetType.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SetType.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SetType.java
index b320224..63b64cd 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SetType.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1SetType.java
@@ -28,7 +28,7 @@ import org.apache.kerby.asn1.UniversalTag;
 public class Asn1SetType extends Asn1CollectionType {
 
     public Asn1SetType(Asn1FieldInfo[] tags) {
-        super(UniversalTag.SET.getValue(), tags);
+        super(UniversalTag.SET, tags);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/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 deb2de8..2f15daf 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
@@ -20,7 +20,7 @@
 package org.apache.kerby.asn1.type;
 
 import org.apache.kerby.asn1.LimitedByteBuffer;
-import org.apache.kerby.asn1.TagClass;
+import org.apache.kerby.asn1.Tag;
 import org.apache.kerby.asn1.UniversalTag;
 
 import java.io.IOException;
@@ -42,11 +42,11 @@ public abstract class Asn1Simple<T> extends AbstractAsn1Type<T> {
 
     /**
      * Constructor with a value, generally for encoding of the value
-     * @param tagNo The tag number
+     * @param universalTag The tag number
      * @param value The value
      */
-    public Asn1Simple(UniversalTag tagNo, T value) {
-        super(TagClass.UNIVERSAL, tagNo.getValue(), value);
+    public Asn1Simple(UniversalTag universalTag, T value) {
+        super(universalTag, value);
         usePrimitive(true);
     }
 
@@ -60,7 +60,7 @@ public abstract class Asn1Simple<T> extends AbstractAsn1Type<T> {
 
     @Override
     public void encode(ByteBuffer buffer) {
-        encodeTag(buffer, tagFlags(), tagNo());
+        encodeTag(buffer, tag());
         int bodyLen = encodingBodyLength();
         encodeLength(buffer, bodyLen);
         if (bodyLen > 0) {
@@ -104,8 +104,12 @@ public abstract class Asn1Simple<T> extends AbstractAsn1Type<T> {
 
     protected void toBytes() { }
 
-    public static boolean isSimple(int tagNo) {
-        return isSimple(UniversalTag.fromValue(tagNo));
+    public static boolean isSimple(Tag tag) {
+        return isSimple(tag.universalTag());
+    }
+
+    public static boolean isSimple(int tag) {
+        return isSimple(new Tag(tag));
     }
 
     public static boolean isSimple(UniversalTag tagNo) {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1String.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1String.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1String.java
index 91c6d19..49ceb4a 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1String.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1String.java
@@ -29,8 +29,8 @@ import java.util.ArrayList;
 import java.util.List;
 
 public abstract class Asn1String extends Asn1Simple<String> {
-    public Asn1String(UniversalTag tagNo) {
-        super(tagNo, null);
+    public Asn1String(UniversalTag universalTag) {
+        super(universalTag, null);
     }
 
     public Asn1String(UniversalTag tagNo, String value) {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Tagging.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Tagging.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Tagging.java
index dd7d394..b808b3e 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Tagging.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Tagging.java
@@ -20,7 +20,7 @@
 package org.apache.kerby.asn1.type;
 
 import org.apache.kerby.asn1.LimitedByteBuffer;
-import org.apache.kerby.asn1.TagClass;
+import org.apache.kerby.asn1.Tag;
 
 import java.io.IOException;
 import java.lang.reflect.ParameterizedType;
@@ -33,14 +33,17 @@ public class Asn1Tagging<T extends Asn1Type> extends AbstractAsn1Type<T> {
 
     public Asn1Tagging(int tagNo, T value,
                        boolean isAppSpecific, boolean isImplicit) {
-        super(isAppSpecific ? TagClass.APPLICATION : TagClass.CONTEXT_SPECIFIC,
-            tagNo, value);
+        super(makeTag(isAppSpecific, tagNo), value);
         if (value == null) {
             initValue();
         }
         useImplicit(isImplicit);
     }
 
+    private static Tag makeTag(boolean isAppSpecific, int tagNo) {
+        return isAppSpecific ? Tag.newAppTag(tagNo) : Tag.newCtxTag(tagNo);
+    }
+
     @Override
     public void useImplicit(boolean isImplicit) {
         super.useImplicit(isImplicit);
@@ -48,9 +51,9 @@ public class Asn1Tagging<T extends Asn1Type> extends AbstractAsn1Type<T> {
         if (!isImplicit) {
             //In effect, explicitly tagged types are structured types consisting
             // of one component, the underlying type.
-            super.usePrimitive(false);
+            usePrimitive(false);
         } else {
-            super.usePrimitive(getValue().isPrimitive());
+            usePrimitive(getValue().isPrimitive());
         }
     }
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Type.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Type.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Type.java
index 271e795..4c1c538 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Type.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Type.java
@@ -19,7 +19,7 @@
  */
 package org.apache.kerby.asn1.type;
 
-import org.apache.kerby.asn1.TagClass;
+import org.apache.kerby.asn1.Tag;
 import org.apache.kerby.asn1.TaggingOption;
 
 import java.io.IOException;
@@ -30,12 +30,6 @@ import java.nio.ByteBuffer;
  */
 public interface Asn1Type {
     /**
-     * A mask to determinate if a Tag is CONSTRUCTED. The fifth bit should be
-     * set to 1 if the type is constructed (0010-0000).
-     */
-    int CONSTRUCTED_FLAG = 0x20;
-
-    /**
      * Encoding type
      */
     enum EncodingType {
@@ -46,21 +40,9 @@ public interface Asn1Type {
 
     /**
      *
-     * @return The tag class
-     */
-    TagClass tagClass();
-
-    /**
-     *
-     * @return The tag flags
-     */
-    int tagFlags();
-
-    /**
-     * Get tag number for the type
-     * @return tag number
+     * @return The tag
      */
-    int tagNo();
+    Tag tag();
 
     /**
      * Use primitive or constructed.

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingCollection.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingCollection.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingCollection.java
index bac51b0..90f95c1 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingCollection.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingCollection.java
@@ -21,7 +21,7 @@ package org.apache.kerby.asn1.type;
 
 import org.apache.kerby.asn1.Asn1FieldInfo;
 import org.apache.kerby.asn1.LimitedByteBuffer;
-import org.apache.kerby.asn1.TagClass;
+import org.apache.kerby.asn1.Tag;
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
@@ -36,19 +36,22 @@ public abstract class TaggingCollection extends AbstractAsn1Type<Asn1CollectionT
 
     public TaggingCollection(int taggingTagNo, Asn1FieldInfo[] tags,
                              boolean isAppSpecific, boolean isImplicit) {
-        super(isAppSpecific ? TagClass.APPLICATION : TagClass.CONTEXT_SPECIFIC,
-            taggingTagNo);
+        super(makeTag(isAppSpecific, taggingTagNo));
         this.tagged = createTaggedCollection(tags);
         setValue(tagged);
         this.tagging = new Asn1Tagging<Asn1CollectionType>(taggingTagNo,
             tagged, isAppSpecific, isImplicit);
     }
 
+    private static Tag makeTag(boolean isAppSpecific, int tagNo) {
+        return isAppSpecific ? Tag.newAppTag(tagNo) : Tag.newCtxTag(tagNo);
+    }
+
     protected abstract Asn1CollectionType createTaggedCollection(Asn1FieldInfo[] tags);
 
     @Override
-    public int tagFlags() {
-        return tagging.tagFlags();
+    public Tag tag() {
+        return tagging.tag();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingSet.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingSet.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingSet.java
index 52158cb..e13d5f0 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingSet.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingSet.java
@@ -22,7 +22,8 @@ package org.apache.kerby.asn1.type;
 import org.apache.kerby.asn1.Asn1FieldInfo;
 
 /**
- * For tagging a sequence type with tagNo, either application specific or context specific class
+ * For tagging a sequence type with tagNo, either application specific or
+ * context specific class
  */
 public class TaggingSet extends TaggingCollection {
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/kerby-kerb/kerb-core-test/src/test/java/org/apache/kerby/kerberos/kerb/codec/CodecTest.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-core-test/src/test/java/org/apache/kerby/kerberos/kerb/codec/CodecTest.java b/kerby-kerb/kerb-core-test/src/test/java/org/apache/kerby/kerberos/kerb/codec/CodecTest.java
index 3bd2117..3507bf0 100644
--- a/kerby-kerb/kerb-core-test/src/test/java/org/apache/kerby/kerberos/kerb/codec/CodecTest.java
+++ b/kerby-kerb/kerb-core-test/src/test/java/org/apache/kerby/kerberos/kerb/codec/CodecTest.java
@@ -41,7 +41,6 @@ public class CodecTest {
         assertThat(restored).isNotNull();
         assertThat(restored.getCksumtype()).isEqualTo(mcs.getCksumtype());
         assertThat(mcs.getChecksum()).isEqualTo(restored.getChecksum());
-        assertThat(restored.tagNo()).isEqualTo(mcs.tagNo());
-        assertThat(restored.tagFlags()).isEqualTo(mcs.tagFlags());
+        assertThat(restored.tag()).isEqualTo(mcs.tag());
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/66fce42a/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 e592567..6db67de 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
@@ -20,6 +20,7 @@
 package org.apache.kerby.kerberos.kerb;
 
 import org.apache.kerby.asn1.LimitedByteBuffer;
+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;
@@ -65,14 +66,12 @@ public class KrbCodec {
 
     public static KrbMessage decodeMessage(ByteBuffer byteBuffer) throws IOException {
         LimitedByteBuffer limitedBuffer = new LimitedByteBuffer(byteBuffer);
-        int tag = Asn1Object.readTag(limitedBuffer);
-        int tagNo = Asn1Object.readTagNo(limitedBuffer, tag);
-        int tagFlags = tag & 0xe0;
+        Tag tag = Asn1Object.readTag(limitedBuffer);
         int length = Asn1Object.readLength(limitedBuffer);
         LimitedByteBuffer valueBuffer = new LimitedByteBuffer(limitedBuffer, length);
 
         KrbMessage msg;
-        KrbMessageType msgType = KrbMessageType.fromValue(tagNo);
+        KrbMessageType msgType = KrbMessageType.fromValue(tag.tagNo());
         if (msgType == KrbMessageType.TGS_REQ) {
             msg = new TgsReq();
         } else if (msgType == KrbMessageType.AS_REP) {
@@ -90,7 +89,7 @@ public class KrbCodec {
         } else {
             throw new IOException("To be supported krb message type with tag: " + tag);
         }
-        msg.decode(tagFlags, tagNo, valueBuffer);
+        msg.decode(tag, valueBuffer);
 
         return msg;
     }


Mime
View raw message