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 1FEB918022 for ; Sat, 16 Jan 2016 09:38:30 +0000 (UTC) Received: (qmail 81303 invoked by uid 500); 16 Jan 2016 09:38:30 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 81176 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 80362 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 4AD12E0B56; Sat, 16 Jan 2016 09:38:29 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: drankye@apache.org To: commits@directory.apache.org Date: Sat, 16 Jan 2016 09:38:40 -0000 Message-Id: <0cf32654c6a44afba556e521dbcb073e@git.apache.org> In-Reply-To: <8acd522df7754e07bcb4c2ee1596d918@git.apache.org> References: <8acd522df7754e07bcb4c2ee1596d918@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [12/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/parse/Asn1ParseResult.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/parse/Asn1ParseResult.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/parse/Asn1ParseResult.java deleted file mode 100644 index 0152cfc..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/parse/Asn1ParseResult.java +++ /dev/null @@ -1,124 +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.parse; - -import org.apache.kerby.asn1.type.Asn1Object; -import org.apache.kerby.asn1.util.Asn1Util; - -import java.nio.ByteBuffer; - -public abstract class Asn1ParseResult extends Asn1Object { - private Asn1Header header; - private int bodyStart; - private int bodyEnd; - private ByteBuffer buffer; - - public Asn1ParseResult(Asn1Header header, - int bodyStart, ByteBuffer buffer) { - super(header.getTag()); - this.header = header; - this.bodyStart = bodyStart; - this.buffer = buffer; - - this.bodyEnd = isDefinitiveLength() ? bodyStart + header.getLength() : -1; - } - - public Asn1Header getHeader() { - return header; - } - - public int getBodyStart() { - return bodyStart; - } - - public int getBodyEnd() { - return bodyEnd; - } - - public void setBodyEnd(int bodyEnd) { - this.bodyEnd = bodyEnd; - } - - public ByteBuffer getBuffer() { - return buffer; - } - - public ByteBuffer getBodyBuffer() { - ByteBuffer result = buffer.duplicate(); - result.position(bodyStart); - - int end = getBodyEnd(); - if (end >= bodyStart) { - result.limit(end); - } - - return result; - } - - public byte[] readBodyBytes() { - ByteBuffer bodyBuffer = getBodyBuffer(); - byte[] result = new byte[bodyBuffer.remaining()]; - bodyBuffer.get(result); - return result; - } - - public boolean isDefinitiveLength() { - return header.isDefinitiveLength(); - } - - public int getEncodingLength() { - return getHeaderLength() + getBodyLength(); - } - - public int getHeaderLength() { - int bodyLen = getBodyLength(); - int headerLen = Asn1Util.lengthOfTagLength(header.getTag().tagNo()); - headerLen += (header.isDefinitiveLength() - ? Asn1Util.lengthOfBodyLength(bodyLen) : 1); - return headerLen; - } - - public int getOffset() { - return getBodyStart() - getHeaderLength(); - } - - public int getBodyLength() { - if (isDefinitiveLength()) { - return header.getLength(); - } else if (getBodyEnd() != -1) { - return getBodyEnd() - getBodyStart(); - } - return -1; - } - - - public boolean checkBodyFinished(int pos) { - return getBodyEnd() != -1 && pos >= getBodyEnd(); - } - - @Override - public String simpleInfo() { - return tag().typeStr() + " [" - + "tag=" + tag() - + ", off=" + getOffset() - + ", len=" + getHeaderLength() + "+" + getBodyLength() - + "]"; - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/parse/Asn1Parser.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/parse/Asn1Parser.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/parse/Asn1Parser.java deleted file mode 100644 index ff90719..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/parse/Asn1Parser.java +++ /dev/null @@ -1,85 +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.parse; - -import org.apache.kerby.asn1.Tag; - -import java.io.IOException; -import java.nio.ByteBuffer; - -/** - * ASN1 parser. - */ -public class Asn1Parser { - - public static void parse(Asn1Container container) throws IOException { - Asn1Reader reader = new Asn1Reader(container.getBuffer()); - int pos = container.getBodyStart(); - while (true) { - reader.setPosition(pos); - Asn1ParseResult asn1Obj = parse(reader); - if (asn1Obj == null) { - break; - } - - container.addItem(asn1Obj); - - pos += asn1Obj.getEncodingLength(); - if (asn1Obj.isEOC()) { - break; - } - - if (container.checkBodyFinished(pos)) { - break; - } - } - - container.setBodyEnd(pos); - } - - public static Asn1ParseResult parse(ByteBuffer content) throws IOException { - Asn1Reader reader = new Asn1Reader(content); - return parse(reader); - } - - public static Asn1ParseResult parse(Asn1Reader reader) throws IOException { - if (!reader.available()) { - return null; - } - - Asn1Header header = reader.readHeader(); - Tag tmpTag = header.getTag(); - int bodyStart = reader.getPosition(); - Asn1ParseResult parseResult; - - if (tmpTag.isPrimitive()) { - parseResult = new Asn1Item(header, bodyStart, reader.getBuffer()); - } else { - Asn1Container container = new Asn1Container(header, - bodyStart, reader.getBuffer()); - if (header.getLength() != 0) { - parse(container); - } - parseResult = container; - } - - return parseResult; - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/parse/Asn1Reader.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/parse/Asn1Reader.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/parse/Asn1Reader.java deleted file mode 100644 index 89994f8..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/parse/Asn1Reader.java +++ /dev/null @@ -1,126 +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.parse; - -import org.apache.kerby.asn1.Tag; - -import java.io.IOException; -import java.nio.ByteBuffer; - -/** - * ASN1 reader for positional reading. - */ -public final class Asn1Reader { - private ByteBuffer buffer; - private int position; - - public ByteBuffer getBuffer() { - return buffer; - } - - public Asn1Header readHeader() throws IOException { - Tag tag = readTag(); - int valueLength = readLength(); - Asn1Header header = new Asn1Header(tag, valueLength); - return header; - } - - public Asn1Reader(ByteBuffer buffer) { - this.buffer = buffer; - this.position = buffer.position(); - } - - public int getPosition() { - return position; - } - - public void setPosition(int position) { - this.position = position; - } - - public boolean available() { - return position < buffer.limit(); - } - - protected byte readByte() throws IOException { - return buffer.get(position++); - } - - private Tag readTag() throws IOException { - int tagFlags = readTagFlags(); - int tagNo = readTagNo(tagFlags); - return new Tag(tagFlags, tagNo); - } - - private int readTagFlags() throws IOException { - int tagFlags = readByte() & 0xff; - return tagFlags; - } - - private int readTagNo(int tagFlags) throws IOException { - int tagNo = tagFlags & 0x1f; - - if (tagNo == 0x1f) { - tagNo = 0; - - int b = readByte() & 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 = readByte(); - } - - tagNo |= b & 0x7f; - } - - return tagNo; - } - - private int readLength() throws IOException { - int result = readByte() & 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 = readByte() & 0xff; - result = (result << 8) + tmp; - } - } - - if (result < 0) { - throw new IOException("Invalid length " + result); - } - - return result; - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/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 deleted file mode 100644 index 96c68a1..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/AbstractAsn1Type.java +++ /dev/null @@ -1,83 +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.type; - -import org.apache.kerby.asn1.Tag; -import org.apache.kerby.asn1.UniversalTag; - -/** - * The abstract ASN1 type for all the ASN1 types. It provides basic - * encoding and decoding utilities. - * - * @param the type of the value encoded/decoded or wrapped by this - */ -public abstract class AbstractAsn1Type extends Asn1Encodeable { - // The wrapped real value. - private T value; - - /** - * Default constructor. - * @param tag the tag - * @param value the value - */ - public AbstractAsn1Type(Tag tag, T value) { - super(tag); - this.value = value; - } - - /** - * Default constructor. - * @param tag the tag - */ - public AbstractAsn1Type(Tag tag) { - super(tag); - } - - /** - * Default constructor with an universal tag. - * @param tag the tag - * @param value the value - */ - public AbstractAsn1Type(UniversalTag tag, T value) { - super(tag); - this.value = value; - } - - /** - * Default constructor with an universal tag. - * @param tag the tag - */ - public AbstractAsn1Type(UniversalTag tag) { - super(tag); - } - - public T getValue() { - return value; - } - - public void setValue(T value) { - this.value = value; - } - - @Override - public String toString() { - return tag().typeStr(); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/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 deleted file mode 100644 index 3c35173..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Any.java +++ /dev/null @@ -1,212 +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.type; - -import org.apache.kerby.asn1.Asn1Binder; -import org.apache.kerby.asn1.Asn1Converter; -import org.apache.kerby.asn1.Asn1Dumpable; -import org.apache.kerby.asn1.Asn1Dumper; -import org.apache.kerby.asn1.Asn1FieldInfo; -import org.apache.kerby.asn1.Tag; -import org.apache.kerby.asn1.TaggingOption; -import org.apache.kerby.asn1.UniversalTag; -import org.apache.kerby.asn1.parse.Asn1ParseResult; - -import java.io.IOException; -import java.nio.ByteBuffer; - -/** - * Can be any valid ASN-1 ojbect, limited or not limited. - */ -public class Asn1Any - extends AbstractAsn1Type implements Asn1Dumpable { - private Class valueType; - private Asn1FieldInfo decodeInfo; - private Asn1ParseResult parseResult; - private boolean isBlindlyDecoded = true; - - public Asn1Any() { - super(UniversalTag.ANY); - } - - public Asn1Any(Asn1Type anyValue) { - this(); - setValue(anyValue); - } - - @Override - public Tag tag() { - if (getValue() != null) { - return getValue().tag(); - } else if (parseResult != null) { - return parseResult.tag(); - } - return super.tag(); - } - - public void setValueType(Class valueType) { - this.valueType = valueType; - } - - public void setDecodeInfo(Asn1FieldInfo decodeInfo) { - this.decodeInfo = decodeInfo; - } - - public Asn1ParseResult getParseResult() { - return parseResult; - } - - @Override - public void encode(ByteBuffer buffer) throws IOException { - Asn1Encodeable theValue = (Asn1Encodeable) getValue(); - - if (theValue != null) { - if (!isBlindlyDecoded) { - if (decodeInfo.isTagged()) { - TaggingOption taggingOption = - decodeInfo.getTaggingOption(); - theValue.taggedEncode(buffer, taggingOption); - } else { - theValue.encode(buffer); - } - } else { - theValue.encode(buffer); - } - } - } - - @Override - public int encodingLength() { - Asn1Encodeable theValue = (Asn1Encodeable) getValue(); - - if (theValue != null) { - if (!isBlindlyDecoded) { - if (decodeInfo.isTagged()) { - TaggingOption taggingOption = - decodeInfo.getTaggingOption(); - return theValue.taggedEncodingLength(taggingOption); - } else { - return theValue.encodingLength(); - } - } else { - return theValue.encodingLength(); - } - } - - return super.encodingLength(); - } - - @Override - protected int encodingBodyLength() { - Asn1Encodeable theValue = (Asn1Encodeable) getValue(); - - if (theValue == null) { - return 0; - } - - return -1; // Indicate error, shouldn't be here. - } - - @Override - public void decode(ByteBuffer content) throws IOException { - setValue(null); - - super.decode(content); - } - - @Override - public void decode(Asn1ParseResult parseResult) throws IOException { - // Avoid the tag checking here. - decodeBody(parseResult); - } - - @Override - protected void decodeBody(Asn1ParseResult parseResult) throws IOException { - this.parseResult = parseResult; - - if (valueType != null) { - typeAwareDecode(valueType); - } else { - blindlyDecode(); - } - } - - private void blindlyDecode() throws IOException { - Asn1Type anyValue = Asn1Converter.convert(parseResult, false); - if (decodeInfo != null && decodeInfo.isTagged()) { - // Escape the wrapper - Asn1Constructed constructed = (Asn1Constructed) anyValue; - Asn1Type innerValue = constructed.getValue().get(0); - setValue(innerValue); - } else { - setValue(anyValue); - } - - isBlindlyDecoded = true; - } - - protected T getValueAs(Class t) { - Asn1Type value = getValue(); - if (value != null && !isBlindlyDecoded) { - return (T) value; - } - - if (valueType != null && valueType != t) { - throw new RuntimeException("Required value type isn't the same" - + " with the value type set before"); - } - - try { - typeAwareDecode(t); - } catch (IOException e) { - throw new RuntimeException("Type aware decoding of Any type failed"); - } - - return (T) getValue(); - } - - private void typeAwareDecode(Class t) throws IOException { - T result; - try { - result = t.newInstance(); - } catch (Exception e) { - throw new IOException("No default constructor?", e); - } - - if (parseResult.isContextSpecific()) { - Asn1Binder.bindWithTagging(parseResult, result, - decodeInfo.getTaggingOption()); - } else { - Asn1Binder.bind(parseResult, result); - } - - setValue(result); - isBlindlyDecoded = false; - } - - @Override - public void dumpWith(Asn1Dumper dumper, int indents) { - Asn1Type theValue = getValue(); - dumper.indent(indents).append("").newLine(); - //dumper.append(simpleInfo()).newLine(); - dumper.dumpType(indents, theValue); - } -} - http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BitString.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BitString.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BitString.java deleted file mode 100644 index 1b921e1..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BitString.java +++ /dev/null @@ -1,108 +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.type; - -import org.apache.kerby.asn1.UniversalTag; - -import java.io.IOException; - -public class Asn1BitString extends Asn1Simple { - private int padding; - - public Asn1BitString() { - this(null); - } - - public Asn1BitString(byte[] value) { - this(value, 0); - } - - public Asn1BitString(byte[] value, int padding) { - super(UniversalTag.BIT_STRING, value); - this.padding = padding; - } - - public void setPadding(int padding) { - this.padding = padding; - } - - public int getPadding() { - return padding; - } - - @Override - protected int encodingBodyLength() { - byte[] body = getValue(); - if (body != null) { - return body.length + 1; - } - return 0; - } - - @Override - protected void toBytes() { - byte[] bytes = new byte[encodingBodyLength()]; - byte[] body = getValue(); - if (body != null) { - bytes[0] = (byte) padding; - System.arraycopy(body, 0, bytes, 1, bytes.length - 1); - } - setBytes(bytes); - } - - @Override - protected void toValue() throws IOException { - byte[] bytes = getBytes(); - if (bytes.length < 1) { - throw new IOException("Bad stream, zero bytes found for bitstring"); - } - int paddingBits = bytes[0]; - validatePaddingBits(paddingBits); - setPadding(paddingBits); - - byte[] newBytes = new byte[bytes.length - 1]; - if (bytes.length > 1) { - System.arraycopy(bytes, 1, newBytes, 0, bytes.length - 1); - } - setValue(newBytes); - } - - private void validatePaddingBits(int paddingBits) throws IOException { - if (paddingBits < 0 || paddingBits > 7) { - throw new IOException("Bad padding number: " + paddingBits + ", should be in [0, 7]"); - } - } - - @Override - public String toString() { - String typeStr = tag().typeStr() + " [" - + "tag=" + tag() - + ", len=" + getHeaderLength() + "+" + getBodyLength() - + "] "; - - byte[] valueBytes = getValue(); - String valueStr = ""; - if (valueBytes != null) { - valueStr = "<" + valueBytes.length + " bytes>"; - } - - return typeStr + valueStr; - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BmpString.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BmpString.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BmpString.java deleted file mode 100644 index 294649e..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BmpString.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.type; - -import org.apache.kerby.asn1.UniversalTag; -import org.apache.kerby.asn1.parse.Asn1ParseResult; - -import java.io.IOException; - -public class Asn1BmpString extends Asn1Simple { - public Asn1BmpString() { - super(UniversalTag.BMP_STRING); - } - - public Asn1BmpString(String value) { - super(UniversalTag.BMP_STRING, value); - } - - @Override - protected int encodingBodyLength() { - return getValue().length() * 2; - } - - protected void toBytes() { - String strValue = getValue(); - int len = strValue.length(); - byte[] bytes = new byte[len * 2]; - - for (int i = 0; i != len; i++) { - char c = strValue.charAt(i); - bytes[2 * i] = (byte) (c >> 8); - bytes[2 * i + 1] = (byte) c; - } - setBytes(bytes); - } - - protected void toValue() throws IOException { - byte[] bytes = getBytes(); - char[] chars = new char[bytes.length / 2]; - for (int i = 0; i != chars.length; i++) { - chars[i] = (char) ((bytes[2 * i] << 8) | (bytes[2 * i + 1] & 0xff)); - } - setValue(new String(chars)); - } - - @Override - protected void decodeBody(Asn1ParseResult parseResult) throws IOException { - if (parseResult.getBodyLength() % 2 != 0) { - throw new IOException("Bad stream, BMP string expecting multiple of 2 bytes"); - } - super.decodeBody(parseResult); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/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 deleted file mode 100644 index 9679783..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Boolean.java +++ /dev/null @@ -1,85 +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.type; - -import org.apache.kerby.asn1.UniversalTag; -import org.apache.kerby.asn1.parse.Asn1ParseResult; - -import java.io.IOException; - -/** - * ASN1 Boolean type - */ -public class Asn1Boolean extends Asn1Simple { - private static final byte[] TRUE_BYTE = new byte[] {(byte) 0xff}; - private static final byte[] FALSE_BYTE = new byte[] {(byte) 0x00}; - - public static final Asn1Boolean TRUE = new Asn1Boolean(true); - public static final Asn1Boolean FALSE = new Asn1Boolean(false); - - /** - * Default constructor, generally for decoding as a container - */ - public Asn1Boolean() { - this(null); - } - - /** - * Constructor with a value, generally for encoding of the value - * @param value The boolean value - */ - public Asn1Boolean(Boolean value) { - super(UniversalTag.BOOLEAN, value); - } - - @Override - protected int encodingBodyLength() { - return 1; - } - - @Override - protected void decodeBody(Asn1ParseResult parseResult) throws IOException { - if (parseResult.getBodyLength() != 1) { - throw new IOException("More than 1 byte found for Boolean"); - } - super.decodeBody(parseResult); - } - - @Override - protected void toBytes() { - 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) == 0xff) { - // DER only accepts 0xFF as true - setValue(true); - } else if (isBER()) { - // BER accepts any non-zero as true - setValue(true); - } else { - setValue(false); - } - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/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 deleted file mode 100644 index 8835c9d..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Choice.java +++ /dev/null @@ -1,241 +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.type; - -import org.apache.kerby.asn1.Asn1Binder; -import org.apache.kerby.asn1.Asn1Dumpable; -import org.apache.kerby.asn1.Asn1Dumper; -import org.apache.kerby.asn1.Asn1FieldInfo; -import org.apache.kerby.asn1.EnumType; -import org.apache.kerby.asn1.Tag; -import org.apache.kerby.asn1.TaggingOption; -import org.apache.kerby.asn1.UniversalTag; -import org.apache.kerby.asn1.parse.Asn1ParseResult; - -import java.io.IOException; -import java.nio.ByteBuffer; - -public class Asn1Choice - extends AbstractAsn1Type implements Asn1Dumpable { - - private final Asn1FieldInfo[] fieldInfos; - private final Tag[] tags; - - private Asn1FieldInfo chosenField; - - public Asn1Choice(Asn1FieldInfo[] fieldInfos) { - super(UniversalTag.CHOICE); - - this.fieldInfos = fieldInfos; - this.tags = new Tag[fieldInfos.length]; - initTags(); - } - - @Override - public Tag tag() { - if (getValue() != null) { - return getValue().tag(); - } else if (chosenField != null) { - return chosenField.getFieldTag(); - } - return super.tag(); - } - - private void initTags() { - for (int i = 0; i < fieldInfos.length; i++) { - tags[i] = fieldInfos[i].getFieldTag(); - } - } - - public boolean matchAndSetValue(Tag tag) { - int foundPos = -1; - for (int i = 0; i < fieldInfos.length; i++) { - if (tag.isContextSpecific()) { - if (fieldInfos[i].getTagNo() == tag.tagNo()) { - foundPos = i; - break; - } - } else if (tags[i].equals(tag)) { - foundPos = i; - break; - } - } - - if (foundPos != -1) { - this.chosenField = fieldInfos[foundPos]; - setValue(fieldInfos[foundPos].createFieldValue()); - return true; - } - return false; - } - - @Override - public byte[] encode() throws IOException { - Asn1Encodeable theValue = (Asn1Encodeable) getValue(); - - if (theValue != null) { - if (chosenField.isTagged()) { - TaggingOption taggingOption = - chosenField.getTaggingOption(); - return theValue.taggedEncode(taggingOption); - } else { - return theValue.encode(); - } - } - return null; - } - - @Override - public void encode(ByteBuffer buffer) throws IOException { - Asn1Encodeable theValue = (Asn1Encodeable) getValue(); - - if (theValue != null) { - if (chosenField.isTagged()) { - TaggingOption taggingOption = - chosenField.getTaggingOption(); - theValue.taggedEncode(buffer, taggingOption); - } else { - theValue.encode(buffer); - } - } - } - - @Override - public int encodingLength() { - Asn1Encodeable theValue = (Asn1Encodeable) getValue(); - - if (theValue != null) { - if (chosenField.isTagged()) { - TaggingOption taggingOption = - chosenField.getTaggingOption(); - return theValue.taggedEncodingLength(taggingOption); - } else { - return theValue.encodingLength(); - } - } - - return super.encodingLength(); - } - - @Override - protected int encodingBodyLength() { - Asn1Encodeable theValue = (Asn1Encodeable) getValue(); - - if (theValue == null) { - return 0; - } - - return -1; // Indicate error, shouldn't be here. - } - - @Override - protected void encodeBody(ByteBuffer buffer) throws IOException { - Asn1Encodeable theValue = (Asn1Encodeable) getValue(); - - if (theValue != null) { - if (chosenField.isTagged()) { - TaggingOption taggingOption = - chosenField.getTaggingOption(); - theValue.taggedEncode(buffer, taggingOption); - } else { - theValue.encode(buffer); - } - } - } - - @Override - public void decode(ByteBuffer content) throws IOException { - chosenField = null; - setValue(null); - - super.decode(content); - } - - @Override - public void decode(Asn1ParseResult parseResult) throws IOException { - if (chosenField == null) { - matchAndSetValue(parseResult.tag()); - } - - decodeBody(parseResult); - } - - @Override - protected void decodeBody(Asn1ParseResult parseResult) throws IOException { - if (chosenField == null) { - matchAndSetValue(parseResult.tag()); - } - - if (chosenField == null) { - throw new IOException("Unexpected item, not in choices: " - + parseResult.simpleInfo()); - } - - Asn1Type fieldValue = getValue(); - if (parseResult.isContextSpecific()) { - Asn1Binder.bindWithTagging(parseResult, fieldValue, - chosenField.getTaggingOption()); - } else { - Asn1Binder.bind(parseResult, fieldValue); - } - } - - protected T getChoiceValueAs(EnumType index, Class t) { - if (chosenField == null || getValue() == null) { - return null; - } - - if (chosenField != null && index != chosenField.getIndex()) { - throw new IllegalArgumentException("Incorrect chosen value requested"); - } - - return (T) getValue(); - } - - protected void setChoiceValue(EnumType index, Asn1Type value) { - if (fieldInfos[index.getValue()].getIndex() != index) { - throw new IllegalArgumentException("Incorrect choice option to set"); - } - - this.chosenField = fieldInfos[index.getValue()]; - setValue(value); - } - - protected void setChoiceValueAsOctets(EnumType index, byte[] bytes) { - Asn1OctetString value = new Asn1OctetString(bytes); - setChoiceValue(index, value); - } - - protected byte[] getChoiceValueAsOctets(EnumType index) { - Asn1OctetString value = getChoiceValueAs(index, Asn1OctetString.class); - if (value != null) { - return value.getValue(); - } - return null; - } - - @Override - public void dumpWith(Asn1Dumper dumper, int indents) { - Asn1Type theValue = getValue(); - dumper.indent(indents).append("").newLine(); - //dumper.append(simpleInfo()).newLine(); - dumper.dumpType(indents, theValue); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/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 deleted file mode 100644 index 982c29e..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java +++ /dev/null @@ -1,79 +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.type; - -import org.apache.kerby.asn1.Tag; -import org.apache.kerby.asn1.UniversalTag; - -/** - * ASN1 complex type of multiple ASN1 objects. - */ -public class Asn1Collection extends Asn1Constructed { - - public Asn1Collection(UniversalTag universalTag) { - super(new Tag(universalTag)); - } - - 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 universalTag) { - switch (universalTag) { - case SEQUENCE: - case SEQUENCE_OF: - case SET: - case SET_OF: - return true; - default: - return false; - } - } - - public static Asn1Collection createCollection(Tag tag) { - if (!isCollection(tag)) { - throw new IllegalArgumentException("Not collection type, tag: " + tag); - } - return createCollection(tag.universalTag()); - } - - public static Asn1Collection createCollection(UniversalTag universalTag) { - if (!isCollection(universalTag)) { - throw new IllegalArgumentException("Not collection type, tag: " + universalTag); - } - - switch (universalTag) { - case SEQUENCE: - return new Asn1Sequence(); - case SEQUENCE_OF: - return new Asn1Sequence(); - case SET: - return new Asn1Set(); - case SET_OF: - return new Asn1Set(); - default: - throw new IllegalArgumentException("Unexpected tag " + universalTag.getValue()); - } - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/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 deleted file mode 100644 index 5c9d3a2..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionOf.java +++ /dev/null @@ -1,88 +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.type; - -import org.apache.kerby.asn1.Asn1Binder; -import org.apache.kerby.asn1.UniversalTag; -import org.apache.kerby.asn1.parse.Asn1ParseResult; - -import java.io.IOException; -import java.lang.reflect.ParameterizedType; -import java.util.List; - -public abstract class Asn1CollectionOf - extends Asn1Collection { - - public Asn1CollectionOf(UniversalTag universalTag) { - super(universalTag); - } - - @Override - protected void decodeElements() throws IOException { - for (Asn1ParseResult parsingItem : getContainer().getChildren()) { - if (parsingItem.isEOC()) { - continue; - } - - Asn1Type tmpValue = createElement(); - Asn1Binder.bind(parsingItem, tmpValue); - addItem(tmpValue); - } - } - - public List getElements() { - return (List) getValue(); - } - - public void setElements(List elements) { - super.clear(); - - for (T ele : elements) { - addElement(ele); - } - } - - public void addElements(T ... elements) { - for (T ele : elements) { - addElement(ele); - } - } - - public void addElement(T element) { - super.addItem(element); - } - - private Class getElementType() { - Class elementType = (Class) ((ParameterizedType) - getClass().getGenericSuperclass()).getActualTypeArguments()[0]; - return elementType; - } - - protected T createElement() throws IOException { - Class eleType = getElementType(); - try { - T result = (T) eleType.newInstance(); - return result; - } catch (Exception e) { - throw new IOException("Failed to create element type, " - + "no default constructor? " + eleType.getName(), e); - } - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/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 deleted file mode 100644 index 8f546c6..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java +++ /dev/null @@ -1,299 +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.type; - -import org.apache.kerby.asn1.Asn1Binder; -import org.apache.kerby.asn1.Asn1Dumpable; -import org.apache.kerby.asn1.Asn1Dumper; -import org.apache.kerby.asn1.Asn1FieldInfo; -import org.apache.kerby.asn1.EnumType; -import org.apache.kerby.asn1.TaggingOption; -import org.apache.kerby.asn1.UniversalTag; -import org.apache.kerby.asn1.parse.Asn1Container; -import org.apache.kerby.asn1.parse.Asn1ParseResult; - -import java.io.IOException; -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.List; - -/** - * For collection type that may consist of tagged fields - */ -public abstract class Asn1CollectionType - extends AbstractAsn1Type implements Asn1Dumpable { - private final Asn1FieldInfo[] fieldInfos; - private final Asn1Type[] fields; - - public Asn1CollectionType(UniversalTag universalTag, - final Asn1FieldInfo[] fieldInfos) { - super(universalTag); - - setValue(this); - this.fieldInfos = fieldInfos; - this.fields = new Asn1Type[fieldInfos.length]; - usePrimitive(false); - } - - @Override - protected int encodingBodyLength() throws IOException { - int allLen = 0; - int fieldLen; - for (int i = 0; i < fields.length; ++i) { - Asn1Encodeable field = (Asn1Encodeable) fields[i]; - if (field != null) { - if (fieldInfos[i].isTagged()) { - TaggingOption taggingOption = - fieldInfos[i].getTaggingOption(); - fieldLen = field.taggedEncodingLength(taggingOption); - } else { - fieldLen = field.encodingLength(); - } - allLen += fieldLen; - } - } - return allLen; - } - - @Override - protected void encodeBody(ByteBuffer buffer) throws IOException { - for (int i = 0; i < fields.length; ++i) { - Asn1Type field = fields[i]; - if (field != null) { - if (fieldInfos[i].isTagged()) { - TaggingOption taggingOption = - fieldInfos[i].getTaggingOption(); - field.taggedEncode(buffer, taggingOption); - } else { - field.encode(buffer); - } - } - } - } - - @Override - protected void decodeBody(Asn1ParseResult parseResult) throws IOException { - checkAndInitFields(); - useDefinitiveLength(parseResult.isDefinitiveLength()); - - Asn1Container container = (Asn1Container) parseResult; - List parseResults = container.getChildren(); - - int lastPos = -1, foundPos = -1; - - for (Asn1ParseResult parseItem : parseResults) { - if (parseItem.isEOC()) { - continue; - } - - foundPos = match(lastPos, parseItem); - if (foundPos == -1) { - throw new IOException("Unexpected item: " + parseItem.simpleInfo()); - } - lastPos = foundPos; - - attemptBinding(parseItem, foundPos); - } - } - - private void attemptBinding(Asn1ParseResult parseItem, - int foundPos) throws IOException { - Asn1Type fieldValue = fields[foundPos]; - Asn1FieldInfo fieldInfo = fieldInfos[foundPos]; - - if (fieldValue instanceof Asn1Any) { - Asn1Any any = (Asn1Any) fieldValue; - any.setDecodeInfo(fieldInfo); - Asn1Binder.bind(parseItem, any); - } else { - if (parseItem.isContextSpecific()) { - Asn1Binder.bindWithTagging(parseItem, fieldValue, - fieldInfo.getTaggingOption()); - } else { - Asn1Binder.bind(parseItem, fieldValue); - } - } - } - - private int match(int lastPos, Asn1ParseResult parseItem) { - int foundPos = -1; - for (int i = lastPos + 1; i < fieldInfos.length; ++i) { - Asn1Type fieldValue = fields[i]; - Asn1FieldInfo fieldInfo = fieldInfos[i]; - - if (fieldInfo.isTagged()) { - if (!parseItem.isContextSpecific()) { - continue; - } - if (fieldInfo.getTagNo() == parseItem.tagNo()) { - foundPos = i; - break; - } - } else if (fieldValue.tag().equals(parseItem.tag())) { - foundPos = i; - break; - } else if (fieldValue instanceof Asn1Choice) { - Asn1Choice aChoice = (Asn1Choice) fields[i]; - if (aChoice.matchAndSetValue(parseItem.tag())) { - foundPos = i; - break; - } - } else if (fieldValue instanceof Asn1Any) { - foundPos = i; - break; - } - } - - return foundPos; - } - - private void checkAndInitFields() { - for (int i = 0; i < fieldInfos.length; ++i) { - checkAndInitField(i); - } - } - - private void checkAndInitField(int index) { - if (fields[index] == null) { - fields[index] = fieldInfos[index].createFieldValue(); - } - } - - protected abstract Asn1Collection createCollection(); - - protected T getFieldAs(EnumType index, Class t) { - Asn1Type value = fields[index.getValue()]; - if (value == null) { - return null; - } - return (T) value; - } - - protected void setFieldAs(EnumType index, Asn1Type value) { - fields[index.getValue()] = value; - } - - protected String getFieldAsString(EnumType index) { - Asn1Type value = fields[index.getValue()]; - if (value == null) { - return null; - } - - if (value instanceof Asn1String) { - return ((Asn1String) value).getValue(); - } - - throw new RuntimeException("The targeted field type isn't of string"); - } - - protected byte[] getFieldAsOctets(EnumType index) { - Asn1OctetString value = getFieldAs(index, Asn1OctetString.class); - if (value != null) { - return value.getValue(); - } - return null; - } - - protected void setFieldAsOctets(EnumType index, byte[] bytes) { - Asn1OctetString value = new Asn1OctetString(bytes); - setFieldAs(index, value); - } - - protected Integer getFieldAsInteger(EnumType index) { - Asn1Integer value = getFieldAs(index, Asn1Integer.class); - if (value != null && value.getValue() != null) { - return value.getValue().intValue(); - } - return null; - } - - protected void setFieldAsInt(EnumType index, int value) { - setFieldAs(index, new Asn1Integer(value)); - } - - protected void setFieldAsInt(EnumType index, BigInteger value) { - setFieldAs(index, new Asn1Integer(value)); - } - - protected void setFieldAsObjId(EnumType index, String value) { - setFieldAs(index, new Asn1ObjectIdentifier(value)); - } - - protected String getFieldAsObjId(EnumType index) { - Asn1ObjectIdentifier objId = getFieldAs(index, Asn1ObjectIdentifier.class); - if (objId != null) { - return objId.getValue(); - } - return null; - } - - protected T getFieldAsAny(EnumType index, Class t) { - Asn1Type value = fields[index.getValue()]; - if (value != null && value instanceof Asn1Any) { - Asn1Any any = (Asn1Any) value; - return any.getValueAs(t); - } - return null; - } - - protected void setFieldAsAny(EnumType index, Asn1Type value) { - if (value != null) { - Asn1Any any = new Asn1Any(value); - any.setDecodeInfo(fieldInfos[index.getValue()]); - setFieldAs(index, any); - } - } - - protected void setAnyFieldValueType(EnumType index, - Class valueType) { - if (valueType != null) { - checkAndInitField(index.getValue()); - Asn1Type value = fields[index.getValue()]; - if (value != null && value instanceof Asn1Any) { - Asn1Any any = (Asn1Any) value; - any.setValueType(valueType); - } - } - } - - @Override - public void dumpWith(Asn1Dumper dumper, int indents) { - dumper.indent(indents).appendType(getClass()); - dumper.append(simpleInfo()).newLine(); - - String fdName; - for (int i = 0; i < fieldInfos.length; i++) { - fdName = fieldInfos[i].getIndex().getName(); - fdName = fdName.replace("_", "-").toLowerCase(); - - dumper.indent(indents + 4).append(fdName).append(" = "); - - Asn1Type fdValue = fields[i]; - if (fdValue == null || fdValue instanceof Asn1Simple) { - dumper.append((Asn1Simple) fdValue); - } else { - dumper.newLine().dumpType(indents + 8, fdValue); - } - if (i < fieldInfos.length - 1) { - dumper.newLine(); - } - } - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Constructed.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Constructed.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Constructed.java deleted file mode 100644 index fd8a187..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Constructed.java +++ /dev/null @@ -1,132 +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.type; - -import org.apache.kerby.asn1.Asn1Converter; -import org.apache.kerby.asn1.Asn1Dumpable; -import org.apache.kerby.asn1.Asn1Dumper; -import org.apache.kerby.asn1.Tag; -import org.apache.kerby.asn1.parse.Asn1Container; -import org.apache.kerby.asn1.parse.Asn1ParseResult; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; - -/** - * ASN1 constructed types, mainly structured ones, but also some primitive ones. - */ -public class Asn1Constructed - extends AbstractAsn1Type> implements Asn1Dumpable { - - protected Asn1Container container; - - private boolean lazy = false; - - public Asn1Constructed(Tag tag) { - super(tag); - setValue(new ArrayList()); - usePrimitive(false); - } - - - public Asn1Container getContainer() { - return container; - } - - public void setLazy(boolean lazy) { - this.lazy = lazy; - } - - public boolean isLazy() { - return lazy; - } - - public void addItem(Asn1Type value) { - getValue().add(value); - } - - public void clear() { - getValue().clear(); - } - - @Override - protected int encodingBodyLength() throws IOException { - List valueItems = getValue(); - int allLen = 0; - for (Asn1Type item : valueItems) { - if (item != null) { - allLen += item.encodingLength(); - } - } - return allLen; - } - - @Override - protected void encodeBody(ByteBuffer buffer) throws IOException { - List valueItems = getValue(); - for (Asn1Type item : valueItems) { - if (item != null) { - item.encode(buffer); - } - } - } - - @Override - protected void decodeBody(Asn1ParseResult parseResult) throws IOException { - Asn1Container container = (Asn1Container) parseResult; - this.container = container; - useDefinitiveLength(parseResult.isDefinitiveLength()); - - if (!isLazy()) { - decodeElements(); - } - } - - protected void decodeElements() throws IOException { - for (Asn1ParseResult parsingItem : getContainer().getChildren()) { - if (parsingItem.isEOC()) { - continue; - } - - Asn1Type tmpValue = Asn1Converter.convert(parsingItem, lazy); - addItem(tmpValue); - } - } - - @Override - public void dumpWith(Asn1Dumper dumper, int indents) { - String typeStr = tag().typeStr() + " [" - + "tag=" + tag() - + ", len=" + getHeaderLength() + "+" + getBodyLength() - + "] "; - dumper.indent(indents).append(typeStr).newLine(); - - List items = getValue(); - int i = 0; - for (Asn1Type aObj : items) { - dumper.dumpType(indents + 4, aObj); - if (i++ != items.size() - 1) { - dumper.newLine(); - } - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Encodeable.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Encodeable.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Encodeable.java deleted file mode 100644 index 0bd2e81..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Encodeable.java +++ /dev/null @@ -1,292 +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.type; - -import org.apache.kerby.asn1.Tag; -import org.apache.kerby.asn1.TaggingOption; -import org.apache.kerby.asn1.UniversalTag; -import org.apache.kerby.asn1.parse.Asn1Container; -import org.apache.kerby.asn1.parse.Asn1DerivedItem; -import org.apache.kerby.asn1.parse.Asn1ParseResult; -import org.apache.kerby.asn1.parse.Asn1Parser; -import org.apache.kerby.asn1.util.Asn1Util; - -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 abstract class Asn1Encodeable extends Asn1Object implements Asn1Type { - - private int bodyLength = -1; - - // encoding options - private EncodingType encodingType = EncodingType.BER; - private boolean isImplicit = true; - private boolean isDefinitiveLength = true; // by default!! - - /** - * Constructor with a tag - * @param tag The tag - */ - public Asn1Encodeable(Tag tag) { - super(tag); - } - - /** - * Default constructor with an universal tag. - * @param tag the tag - */ - public Asn1Encodeable(UniversalTag tag) { - super(tag); - } - - /** - * Constructor with a tag - * @param tag The tag - */ - public Asn1Encodeable(int tag) { - super(tag); - } - - @Override - public void usePrimitive(boolean isPrimitive) { - tag().usePrimitive(isPrimitive); - } - - @Override - public boolean isPrimitive() { - return tag().isPrimitive(); - } - - @Override - public void useDefinitiveLength(boolean isDefinitiveLength) { - this.isDefinitiveLength = isDefinitiveLength; - } - - @Override - public boolean isDefinitiveLength() { - return isDefinitiveLength; - } - - @Override - public void useImplicit(boolean isImplicit) { - this.isImplicit = isImplicit; - } - - @Override - public boolean isImplicit() { - return isImplicit; - } - - @Override - public void useDER() { - this.encodingType = EncodingType.DER; - } - - @Override - public boolean isDER() { - return encodingType == EncodingType.DER; - } - - @Override - public void useBER() { - this.encodingType = EncodingType.BER; - } - - @Override - public boolean isBER() { - return encodingType == EncodingType.BER; - } - - @Override - public void useCER() { - this.encodingType = EncodingType.CER; - } - - @Override - public boolean isCER() { - return encodingType == EncodingType.CER; - } - - @Override - public byte[] encode() throws IOException { - int len = encodingLength(); - ByteBuffer byteBuffer = ByteBuffer.allocate(len); - encode(byteBuffer); - byteBuffer.flip(); - return byteBuffer.array(); - } - - @Override - public void encode(ByteBuffer buffer) throws IOException { - Asn1Util.encodeTag(buffer, tag()); - int bodyLen = getBodyLength(); - Asn1Util.encodeLength(buffer, bodyLen); - encodeBody(buffer); - } - - protected void encodeBody(ByteBuffer buffer) throws IOException { } - - @Override - public void decode(byte[] content) throws IOException { - decode(ByteBuffer.wrap(content)); - } - - @Override - public int encodingLength() { - return getHeaderLength() + getBodyLength(); - } - - @Override - protected int getHeaderLength() { - try { - return encodingHeaderLength(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - protected int getBodyLength() { - if (bodyLength == -1) { - try { - bodyLength = encodingBodyLength(); - } catch (IOException e) { - throw new RuntimeException(e); - } - if (bodyLength == -1) { - throw new RuntimeException("Unexpected body length: -1"); - } - } - return bodyLength; - } - - protected int encodingHeaderLength() throws IOException { - int headerLen = Asn1Util.lengthOfTagLength(tagNo()); - int bodyLen = getBodyLength(); - headerLen += Asn1Util.lengthOfBodyLength(bodyLen); - - return headerLen; - } - - protected abstract int encodingBodyLength() throws IOException; - - @Override - public void decode(ByteBuffer content) throws IOException { - Asn1ParseResult parseResult = Asn1Parser.parse(content); - decode(parseResult); - } - - public void decode(Asn1ParseResult parseResult) throws IOException { - Asn1ParseResult tmpParseResult = parseResult; - - if (!tag().equals(parseResult.tag())) { - // Primitive but using constructed encoding - if (isPrimitive() && !parseResult.isPrimitive()) { - Asn1Container container = (Asn1Container) parseResult; - tmpParseResult = new Asn1DerivedItem(tag(), container); - } else { - throw new IOException("Unexpected item " + parseResult.simpleInfo() - + ", expecting " + tag()); - } - } - - decodeBody(tmpParseResult); - } - - protected abstract void decodeBody(Asn1ParseResult parseResult) throws IOException; - - protected int taggedEncodingLength(TaggingOption taggingOption) { - int taggingTagNo = taggingOption.getTagNo(); - int taggingBodyLen = taggingOption.isImplicit() ? getBodyLength() - : encodingLength(); - int taggingEncodingLen = Asn1Util.lengthOfTagLength(taggingTagNo) - + Asn1Util.lengthOfBodyLength(taggingBodyLen) + taggingBodyLen; - return taggingEncodingLen; - } - - @Override - public byte[] taggedEncode(TaggingOption taggingOption) throws IOException { - int len = taggedEncodingLength(taggingOption); - ByteBuffer byteBuffer = ByteBuffer.allocate(len); - taggedEncode(byteBuffer, taggingOption); - byteBuffer.flip(); - return byteBuffer.array(); - } - - @Override - public void taggedEncode(ByteBuffer buffer, TaggingOption taggingOption) throws IOException { - Tag taggingTag = taggingOption.getTag(!isPrimitive()); - Asn1Util.encodeTag(buffer, taggingTag); - - int taggingBodyLen = taggingOption.isImplicit() ? encodingBodyLength() - : encodingLength(); - Asn1Util.encodeLength(buffer, taggingBodyLen); - - if (taggingOption.isImplicit()) { - encodeBody(buffer); - } else { - encode(buffer); - } - } - - @Override - public void taggedDecode(byte[] content, - TaggingOption taggingOption) throws IOException { - taggedDecode(ByteBuffer.wrap(content), taggingOption); - } - - @Override - public void taggedDecode(ByteBuffer content, - TaggingOption taggingOption) throws IOException { - Asn1ParseResult parseResult = Asn1Parser.parse(content); - taggedDecode(parseResult, taggingOption); - } - - public void taggedDecode(Asn1ParseResult parseResult, - TaggingOption taggingOption) throws IOException { - Tag expectedTaggingTagFlags = taggingOption.getTag(!isPrimitive()); - - Asn1ParseResult tmpParseResult = parseResult; - if (!expectedTaggingTagFlags.equals(parseResult.tag())) { - // Primitive but using constructed encoding - if (isPrimitive() && !parseResult.isPrimitive()) { - Asn1Container container = (Asn1Container) parseResult; - tmpParseResult = new Asn1DerivedItem(tag(), container); - } else { - throw new IOException("Unexpected tag " + parseResult.tag() - + ", expecting " + expectedTaggingTagFlags); - } - } - - if (taggingOption.isImplicit()) { - decodeBody(tmpParseResult); - } else { - - Asn1Container container = (Asn1Container) parseResult; - tmpParseResult = container.getChildren().get(0); - - decode(tmpParseResult); - } - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Enumerated.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Enumerated.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Enumerated.java deleted file mode 100644 index 223fc97..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Enumerated.java +++ /dev/null @@ -1,65 +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.type; - -import org.apache.kerby.asn1.EnumType; -import org.apache.kerby.asn1.UniversalTag; - -import java.io.IOException; -import java.math.BigInteger; - -/** - * The ASN1 enumerated type - */ -public abstract class Asn1Enumerated extends Asn1Simple { - - /** - * Default constructor, generally for decoding as a container - */ - public Asn1Enumerated() { - this(null); - } - - /** - * Constructor with a value, generally for encoding of the value - * @param value The boolean value - */ - public Asn1Enumerated(T value) { - super(UniversalTag.ENUMERATED, value); - } - - protected void toBytes() { - BigInteger biValue = BigInteger.valueOf(getValue().getValue()); - setBytes(biValue.toByteArray()); - } - - protected void toValue() throws IOException { - BigInteger biVal = new BigInteger(getBytes()); - int iVal = biVal.intValue(); - EnumType[] allValues = getAllEnumValues(); - for (EnumType val : allValues) { - if (val.getValue() == iVal) { - setValue((T) val); - } - } - } - - protected abstract EnumType[] getAllEnumValues(); -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Eoc.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Eoc.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Eoc.java deleted file mode 100644 index 44cdd21..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Eoc.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.type; - -import org.apache.kerby.asn1.UniversalTag; -import org.apache.kerby.asn1.parse.Asn1ParseResult; - -import java.io.IOException; -import java.nio.ByteBuffer; - -/** - * To represent Asn1 End Of Content type - */ -public final class Asn1Eoc extends Asn1Simple { - public static final Asn1Eoc INSTANCE = new Asn1Eoc(); - private static final byte[] EMPTY_BYTES = new byte[0]; - - private Asn1Eoc() { - super(UniversalTag.EOC, null); - } - - @Override - public void encode(ByteBuffer buffer) { - buffer.put((byte) 0); - buffer.put((byte) 0); - } - - @Override - protected byte[] encodeBody() { - return EMPTY_BYTES; - } - - @Override - protected int encodingBodyLength() { - return 0; - } - - @Override - protected void decodeBody(Asn1ParseResult parseResult) throws IOException { - if (parseResult.getBodyLength() != 0) { - throw new IOException("Unexpected bytes found for EOC"); - } - } - - @Override - public String toString() { - String typeStr = tag().typeStr() + " [" - + "tag=" + tag() - + ", len=" + getHeaderLength() + "+" + getBodyLength() - + "] "; - return typeStr + "eoc"; - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Flags.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Flags.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Flags.java deleted file mode 100644 index f11d6df..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Flags.java +++ /dev/null @@ -1,132 +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.type; - -import org.apache.kerby.asn1.EnumType; - -import java.io.IOException; - -/** - KrbFlags ::= BIT STRING (SIZE (32..MAX)) - -- minimum number of bits shall be sent, - -- but no fewer than 32 - */ -public class Asn1Flags extends Asn1BitString { - private static final int MAX_SIZE = 32; - private static final int MASK; - - static { - int maskBuilder = 0; - for (int i = 0; i < MAX_SIZE; i++) { - maskBuilder = maskBuilder << 1; - maskBuilder |= 0x00000001; - } - MASK = maskBuilder; - } - - private int flags; - - public Asn1Flags() { - this(0); - } - - public Asn1Flags(int value) { - super(); - setFlags(value); - } - - public void setFlags(int flags) { - this.flags = flags; - flags2Value(); - } - - @Override - public void setValue(byte[] value) { - super.setValue(value); - value2Flags(); - } - - public int getFlags() { - return flags; - } - - public boolean isFlagSet(int flag) { - return (flags & flag) != 0; - } - - public void setFlag(int flag) { - setFlags(flags | flag); - } - - public void clearFlag(int flag) { - setFlags(flags & (MASK ^ flag)); - } - - public void clear() { - setFlags(0); - } - - public boolean isFlagSet(EnumType flag) { - return isFlagSet(flag.getValue()); - } - - public void setFlag(EnumType flag) { - setFlag(flag.getValue()); - } - - public void setFlag(EnumType flag, boolean isSet) { - if (isSet) { - setFlag(flag.getValue()); - } else { - clearFlag(flag.getValue()); - } - } - - public void clearFlag(EnumType flag) { - clearFlag(flag.getValue()); - } - - private void flags2Value() { - byte[] bytes = new byte[4]; - bytes[0] = (byte) (flags >> 24); - bytes[1] = (byte) ((flags >> 16) & 0xFF); - bytes[2] = (byte) ((flags >> 8) & 0xFF); - bytes[3] = (byte) (flags & 0xFF); - - setValue(bytes); - } - - private void value2Flags() { - byte[] valueBytes = getValue(); - flags = ((valueBytes[0] & 0xFF) << 24) | ((valueBytes[1] & 0xFF) << 16) - | ((valueBytes[2] & 0xFF) << 8) | (0xFF & valueBytes[3]); - } - - @Override - protected void toValue() throws IOException { - super.toValue(); - - if (getPadding() != 0 || getValue().length != 4) { - throw new IOException("Bad bitstring decoded as invalid krb flags"); - } - - value2Flags(); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1GeneralString.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1GeneralString.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1GeneralString.java deleted file mode 100644 index aae244d..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1GeneralString.java +++ /dev/null @@ -1,32 +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.type; - -import org.apache.kerby.asn1.UniversalTag; - -public class Asn1GeneralString extends Asn1String { - public Asn1GeneralString() { - super(UniversalTag.GENERAL_STRING); - } - - public Asn1GeneralString(String value) { - super(UniversalTag.GENERAL_STRING, value); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1GeneralizedTime.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1GeneralizedTime.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1GeneralizedTime.java deleted file mode 100644 index 48d0cef..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1GeneralizedTime.java +++ /dev/null @@ -1,136 +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.type; - -import org.apache.kerby.asn1.UniversalTag; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.SimpleTimeZone; -import java.util.TimeZone; - -public class Asn1GeneralizedTime extends Asn1Simple { - public Asn1GeneralizedTime() { - this(null); - } - - /** - * time in milliseconds - * @param time The long time - */ - public Asn1GeneralizedTime(long time) { - super(UniversalTag.GENERALIZED_TIME, new Date(time)); - } - - public Asn1GeneralizedTime(Date date) { - super(UniversalTag.GENERALIZED_TIME, date); - } - - protected void toValue() throws IOException { - String dateStr = new String(getBytes(), StandardCharsets.US_ASCII); - SimpleDateFormat sdf; - String fixedDateStr = dateStr; - - boolean withZ = dateStr.endsWith("Z"); - String timeZonePart = getTimeZonePart(dateStr); - boolean withZone = timeZonePart != null; - String millSecs = getMillSeconds(dateStr); - - fixedDateStr = dateStr.substring(0, 14) + millSecs; - if (withZ) { - sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); - sdf.setTimeZone(new SimpleTimeZone(0, "Z")); - } else if (withZone) { - fixedDateStr += timeZonePart; - sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSz"); - sdf.setTimeZone(new SimpleTimeZone(0, "Z")); - } else { - sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); - sdf.setTimeZone(new SimpleTimeZone(0, TimeZone.getDefault().getID())); - } - - try { - setValue(sdf.parse(fixedDateStr)); - } catch (ParseException e) { - throw new IOException("Failed to parse as generalized time string " + dateStr); - } - } - - @Override - protected void toBytes() { - Date date = getValue(); - SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'"); - dateF.setTimeZone(new SimpleTimeZone(0, "Z")); - - String str = dateF.format(date); - byte[] bytes = str.getBytes(StandardCharsets.US_ASCII); - setBytes(bytes); - } - - /** - * Extract the fractional part in seconds and convert into integer by (frac * 1000) as milli seconds - */ - private String getMillSeconds(String dateStr) { - char[] millDigits = new char[] {'0', '0', '0'}; - - int iPos = dateStr.indexOf('.'); - if (iPos > 0) { - if (iPos != 14) { - throw new IllegalArgumentException("Bad generalized time string, " - + "with improper milli seconds " + dateStr); - } - - char chr; - int j = 0; - for (int i = 15; i < dateStr.length() && j < millDigits.length; i++) { - chr = dateStr.charAt(i); - if ('0' <= chr && chr <= '9') { - millDigits[j++] = chr; - } else { - break; - } - } - } - - return new String(millDigits); - } - - /** - * Extract the timezone part if any - */ - private String getTimeZonePart(String dateStr) { - int iPos = dateStr.indexOf('+'); - if (iPos == -1) { - iPos = dateStr.indexOf('-'); - } - if (iPos > 0 && iPos != dateStr.length() - 5) { - throw new IllegalArgumentException("Bad generalized time string, " - + "with improper timezone part " + dateStr); - } - - if (iPos > 0) { - return dateStr.substring(iPos); - } - return null; - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9e4dbd6e/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1IA5String.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1IA5String.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1IA5String.java deleted file mode 100644 index b7a60d1..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1IA5String.java +++ /dev/null @@ -1,32 +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.type; - -import org.apache.kerby.asn1.UniversalTag; - -public class Asn1IA5String extends Asn1String { - public Asn1IA5String() { - super(UniversalTag.IA5_STRING); - } - - public Asn1IA5String(String value) { - super(UniversalTag.IA5_STRING, value); - } -}