@Test public void testDecodeGeneralizedTimeWithoutTimeZone() { Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(Calendar.YEAR, 2010); cal.set(Calendar.MONTH, Calendar.JULY); cal.set(Calendar.DAY_OF_MONTH, 12); cal.set(Calendar.HOUR_OF_DAY, 21); cal.set(Calendar.MINUTE, 45); cal.set(Calendar.SECOND, 27); Date expectedDate = cal.getTime(); byte[] data = new byte[] { 0x18, 0xe, 0x32, 0x30, 0x31, 0x30, 0x30, 0x37, 0x31, 0x32, 0x32, 0x31, 0x34, 0x35, 0x32, 0x37 }; Date actualDate = Asn1Utils.decodeGeneralizedTime(ByteBuffer.wrap(data)); assertEquals(expectedDate, actualDate); }
public static Date decodeGeneralizedTime(ByteBuffer buf) { DerId id = DerId.decode(buf); if (!id.matches(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_GENERALIZED_TIME_TAG_NUM)) { throw new IllegalArgumentException("Expected GeneralizedTime identifier, received " + id); } int len = DerUtils.decodeLength(buf); if (buf.remaining() < len) { throw new IllegalArgumentException("Insufficient content for GeneralizedTime"); } Date date; byte[] dst = new byte[len]; buf.get(dst); String iso8601DateString = new String(dst); Matcher matcher = GENERALIZED_TIME_PATTERN.matcher(iso8601DateString); if (matcher.matches()) { Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(Calendar.YEAR, Integer.parseInt(matcher.group(1))); cal.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1); cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(matcher.group(3))); cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(matcher.group(4))); cal.set(Calendar.MINUTE, Integer.parseInt(matcher.group(5))); cal.set(Calendar.SECOND, Integer.parseInt(matcher.group(6))); String fracSecStr = matcher.group(7); if (fracSecStr != null) { cal.set(Calendar.MILLISECOND, (int) (Float.parseFloat(fracSecStr) * 1000)); } String tzStr = matcher.group(8); if (tzStr != null) { cal.setTimeZone(TimeZone.getTimeZone("Z".equals(tzStr) ? "GMT" : "GMT" + tzStr)); } date = cal.getTime(); } else { throw new IllegalArgumentException("Malformed GeneralizedTime " + iso8601DateString); } return date; }
Asn1Utils { public static Date decodeGeneralizedTime(ByteBuffer buf) { DerId id = DerId.decode(buf); if (!id.matches(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_GENERALIZED_TIME_TAG_NUM)) { throw new IllegalArgumentException("Expected GeneralizedTime identifier, received " + id); } int len = DerUtils.decodeLength(buf); if (buf.remaining() < len) { throw new IllegalArgumentException("Insufficient content for GeneralizedTime"); } Date date; byte[] dst = new byte[len]; buf.get(dst); String iso8601DateString = new String(dst); Matcher matcher = GENERALIZED_TIME_PATTERN.matcher(iso8601DateString); if (matcher.matches()) { Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(Calendar.YEAR, Integer.parseInt(matcher.group(1))); cal.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1); cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(matcher.group(3))); cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(matcher.group(4))); cal.set(Calendar.MINUTE, Integer.parseInt(matcher.group(5))); cal.set(Calendar.SECOND, Integer.parseInt(matcher.group(6))); String fracSecStr = matcher.group(7); if (fracSecStr != null) { cal.set(Calendar.MILLISECOND, (int) (Float.parseFloat(fracSecStr) * 1000)); } String tzStr = matcher.group(8); if (tzStr != null) { cal.setTimeZone(TimeZone.getTimeZone("Z".equals(tzStr) ? "GMT" : "GMT" + tzStr)); } date = cal.getTime(); } else { throw new IllegalArgumentException("Malformed GeneralizedTime " + iso8601DateString); } return date; } }
Asn1Utils { public static Date decodeGeneralizedTime(ByteBuffer buf) { DerId id = DerId.decode(buf); if (!id.matches(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_GENERALIZED_TIME_TAG_NUM)) { throw new IllegalArgumentException("Expected GeneralizedTime identifier, received " + id); } int len = DerUtils.decodeLength(buf); if (buf.remaining() < len) { throw new IllegalArgumentException("Insufficient content for GeneralizedTime"); } Date date; byte[] dst = new byte[len]; buf.get(dst); String iso8601DateString = new String(dst); Matcher matcher = GENERALIZED_TIME_PATTERN.matcher(iso8601DateString); if (matcher.matches()) { Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(Calendar.YEAR, Integer.parseInt(matcher.group(1))); cal.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1); cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(matcher.group(3))); cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(matcher.group(4))); cal.set(Calendar.MINUTE, Integer.parseInt(matcher.group(5))); cal.set(Calendar.SECOND, Integer.parseInt(matcher.group(6))); String fracSecStr = matcher.group(7); if (fracSecStr != null) { cal.set(Calendar.MILLISECOND, (int) (Float.parseFloat(fracSecStr) * 1000)); } String tzStr = matcher.group(8); if (tzStr != null) { cal.setTimeZone(TimeZone.getTimeZone("Z".equals(tzStr) ? "GMT" : "GMT" + tzStr)); } date = cal.getTime(); } else { throw new IllegalArgumentException("Malformed GeneralizedTime " + iso8601DateString); } return date; } private  Asn1Utils(); }
Asn1Utils { public static Date decodeGeneralizedTime(ByteBuffer buf) { DerId id = DerId.decode(buf); if (!id.matches(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_GENERALIZED_TIME_TAG_NUM)) { throw new IllegalArgumentException("Expected GeneralizedTime identifier, received " + id); } int len = DerUtils.decodeLength(buf); if (buf.remaining() < len) { throw new IllegalArgumentException("Insufficient content for GeneralizedTime"); } Date date; byte[] dst = new byte[len]; buf.get(dst); String iso8601DateString = new String(dst); Matcher matcher = GENERALIZED_TIME_PATTERN.matcher(iso8601DateString); if (matcher.matches()) { Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(Calendar.YEAR, Integer.parseInt(matcher.group(1))); cal.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1); cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(matcher.group(3))); cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(matcher.group(4))); cal.set(Calendar.MINUTE, Integer.parseInt(matcher.group(5))); cal.set(Calendar.SECOND, Integer.parseInt(matcher.group(6))); String fracSecStr = matcher.group(7); if (fracSecStr != null) { cal.set(Calendar.MILLISECOND, (int) (Float.parseFloat(fracSecStr) * 1000)); } String tzStr = matcher.group(8); if (tzStr != null) { cal.setTimeZone(TimeZone.getTimeZone("Z".equals(tzStr) ? "GMT" : "GMT" + tzStr)); } date = cal.getTime(); } else { throw new IllegalArgumentException("Malformed GeneralizedTime " + iso8601DateString); } return date; } private  Asn1Utils(); static BitSet decodeBitString(ByteBuffer buf); static Date decodeGeneralizedTime(ByteBuffer buf); static String decodeIA5String(ByteBuffer buf); static int decodeInteger(ByteBuffer buf); static short[] decodeOctetString(ByteBuffer buf); static int decodeSequence(ByteBuffer buf); static int encodeBitString(BitSet value, int nbits, ByteBuffer buf); static int encodeGeneralizedTime(Date date, ByteBuffer buf); static int encodeIA5String(String value, ByteBuffer buf); static int encodeInteger(int value, ByteBuffer buf); static int encodeOctetString(short[] octets, ByteBuffer buf); static int encodeSequence(int contentLength, ByteBuffer buf); static int sizeOfBitString(BitSet value, int nbits); static int sizeOfGeneralizedTime(Date date); static int sizeOfIA5String(String value); static int sizeOfInteger(int value); static int sizeOfOctetString(short[] octets); static int sizeOfSequence(int length); }
Asn1Utils { public static Date decodeGeneralizedTime(ByteBuffer buf) { DerId id = DerId.decode(buf); if (!id.matches(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_GENERALIZED_TIME_TAG_NUM)) { throw new IllegalArgumentException("Expected GeneralizedTime identifier, received " + id); } int len = DerUtils.decodeLength(buf); if (buf.remaining() < len) { throw new IllegalArgumentException("Insufficient content for GeneralizedTime"); } Date date; byte[] dst = new byte[len]; buf.get(dst); String iso8601DateString = new String(dst); Matcher matcher = GENERALIZED_TIME_PATTERN.matcher(iso8601DateString); if (matcher.matches()) { Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(Calendar.YEAR, Integer.parseInt(matcher.group(1))); cal.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1); cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(matcher.group(3))); cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(matcher.group(4))); cal.set(Calendar.MINUTE, Integer.parseInt(matcher.group(5))); cal.set(Calendar.SECOND, Integer.parseInt(matcher.group(6))); String fracSecStr = matcher.group(7); if (fracSecStr != null) { cal.set(Calendar.MILLISECOND, (int) (Float.parseFloat(fracSecStr) * 1000)); } String tzStr = matcher.group(8); if (tzStr != null) { cal.setTimeZone(TimeZone.getTimeZone("Z".equals(tzStr) ? "GMT" : "GMT" + tzStr)); } date = cal.getTime(); } else { throw new IllegalArgumentException("Malformed GeneralizedTime " + iso8601DateString); } return date; } private  Asn1Utils(); static BitSet decodeBitString(ByteBuffer buf); static Date decodeGeneralizedTime(ByteBuffer buf); static String decodeIA5String(ByteBuffer buf); static int decodeInteger(ByteBuffer buf); static short[] decodeOctetString(ByteBuffer buf); static int decodeSequence(ByteBuffer buf); static int encodeBitString(BitSet value, int nbits, ByteBuffer buf); static int encodeGeneralizedTime(Date date, ByteBuffer buf); static int encodeIA5String(String value, ByteBuffer buf); static int encodeInteger(int value, ByteBuffer buf); static int encodeOctetString(short[] octets, ByteBuffer buf); static int encodeSequence(int contentLength, ByteBuffer buf); static int sizeOfBitString(BitSet value, int nbits); static int sizeOfGeneralizedTime(Date date); static int sizeOfIA5String(String value); static int sizeOfInteger(int value); static int sizeOfOctetString(short[] octets); static int sizeOfSequence(int length);  }