You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by sm...@apache.org on 2006/04/21 10:46:32 UTC
svn commit: r395814 - in
/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1:
ASN1Time.java BerInputStream.java DerInputStream.java
Author: smishura
Date: Fri Apr 21 01:46:31 2006
New Revision: 395814
URL: http://svn.apache.org/viewcvs?rev=395814&view=rev
Log:
Move decoding GeneralizedTime from DER to BER stream + minor improvements
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Time.java
incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java
incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Time.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Time.java?rev=395814&r1=395813&r2=395814&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Time.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Time.java Fri Apr 21 01:46:31 2006
@@ -52,26 +52,6 @@
}
- public static int getTimeValue(String time, int offset, int length)
- throws IOException {
-
- int end = offset + length;
- if (time.length() < end) {
- throw new ASN1Exception("Wrong param"); //FIXME message & type
- }
-
- int c;
- int result = 0;
- for (int i = offset; i < end; i++) {
- c = time.charAt(i) - 48;
- if (c < 0 || c > 9) {
- throw new ASN1Exception("Wrong param"); //FIXME message & type
- }
- result = result * 10 + c;
- }
- return result;
- }
-
public static long getMilliseconds(int[] times) {
// count the number of milliseconds since Jan 1, 1970, 00:00:00 GMT
long res = times[6]; //milliseconds
Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java?rev=395814&r1=395813&r2=395814&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java Fri Apr 21 01:46:31 2006
@@ -285,7 +285,65 @@
* @throws IOException - if error occured
*/
public void readGeneralizedTime() throws IOException {
- throw new ASN1Exception("Not supported");
+
+ if ((tag & ASN1Constants.PC_CONSTRUCTED) != 0) {
+ throw new ASN1Exception(
+ "Decoding constructed ASN.1 GeneralizedTime"
+ + " type is not provided");
+ }
+
+ //FIXME: any other optimizations?
+ readContent();
+ //FIXME store string somewhere to allow a custom time type perform additional checks
+
+ // check syntax: the last char MUST be Z
+ if (buffer[offset - 1] != 'Z') {
+ // FIXME support only format that is acceptable for DER
+ throw new ASN1Exception(
+ "ASN.1 GeneralizedTime: encoded format is not implemented");
+ }
+
+ // check syntax: MUST be YYYYMMDDHHMMSS[(./,)DDD]'Z'
+ if (length != 15 && (length < 17 || length > 19)) // invalid length
+ {
+ throw new ASN1Exception(
+ "ASN.1 GeneralizedTime wrongly encoded at ["
+ + contentOffset + ']');
+ }
+
+ // check content: milliseconds
+ if (length > 16) {
+ byte char14 = buffer[contentOffset + 14];
+ if (char14 != '.' && char14 != ',') {
+ throw new ASN1Exception(
+ "ASN.1 GeneralizedTime wrongly encoded at ["
+ + contentOffset + ']');
+ }
+ }
+
+ if (times == null) {
+ times = new int[7];
+ }
+ times[0] = strToInt(contentOffset, 4); //year
+ times[1] = strToInt(contentOffset + 4, 2); //month
+ times[2] = strToInt(contentOffset + 6, 2); //day
+ times[3] = strToInt(contentOffset + 8, 2); //hour
+ times[4] = strToInt(contentOffset + 10, 2); //minute
+ times[5] = strToInt(contentOffset + 12, 2); //second
+
+
+ if (length > 16) {
+ //FIXME optimize me
+ times[6] = strToInt(contentOffset + 15, length - 16);
+
+ if (length == 17) {
+ times[6] = times[6] * 100;
+ } else if (length == 18) {
+ times[6] = times[6] * 10;
+ }
+ }
+
+ //FIXME check all values for valid numbers!!!
}
/**
@@ -826,4 +884,4 @@
}
return null;
}
-}
\ No newline at end of file
+}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java?rev=395814&r1=395813&r2=395814&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java Fri Apr 21 01:46:31 2006
@@ -168,65 +168,28 @@
if ((tag & ASN1Constants.PC_CONSTRUCTED) != 0) {
// It is a string type and it can be encoded as primitive or constructed.
throw new ASN1Exception(
- "DER: ASN.1 GeneralizedTime type MUST have primitive encoding");
+ "ASN.1 GeneralizedTime: constructed identifier at ["
+ + tagOffset + "]. Not valid for DER.");
}
- //FIXME it should check format and invoke BerInputStream
- //FIXME: any other optimizations?
- readContent();
- String timeStr = new String(buffer, contentOffset, length);
+ super.readGeneralizedTime();
- //FIXME store string somewhere to allow a custom time type perform additional checks
-
- int len = timeStr.length();
- // check syntax: MUST be YYYYMMDDHHMMSS[(./,)DDD]'Z'
- if (timeStr.charAt(len - 1) != 'Z' // the last char MUST be Z
- || len < 15 || len == 16 || len > 19) // invalid length
- {
- throw new IOException("ASN.1 GeneralizedTime wrongly encoded at ["
- + contentOffset + "].");
- }
-
- // check content: milliseconds
- if (len > 16) {
- char char14 = timeStr.charAt(14);
- if (char14 != '.' && char14 != ',') {
- throw new IOException(
- "ASN.1 GeneralizedTime wrongly encoded at ["
- + contentOffset + "].");
- }
- }
-
- if (times == null) {
- times = new int[7];
- }
- times[0] = ASN1Time.getTimeValue(timeStr, 0, 4); //year
- times[1] = ASN1Time.getTimeValue(timeStr, 4, 2); //month
- times[2] = ASN1Time.getTimeValue(timeStr, 6, 2); //day
- times[3] = ASN1Time.getTimeValue(timeStr, 8, 2); //hour
- times[4] = ASN1Time.getTimeValue(timeStr, 10, 2); //minute
- times[5] = ASN1Time.getTimeValue(timeStr, 12, 2); //second
-
- //FIXME check all values for valid numbers!!!
-
- if (len > 16) {
- //FIXME optimize me
- times[6] = ASN1Time.getTimeValue(timeStr, 15, len - 16); //millisecond
-
- // the fractional-seconds elements, if present MUST
- // omit all trailing zeros
- if (times[6] % 10 == 0) {
- throw new IOException(
- "DER ASN.1 GeneralizedTime wrongly encoded at ["
- + contentOffset
- + "]. Trailing zeros MUST be omitted");
- }
-
- if (len == 17) {
- times[6] = times[6] * 100;
- } else if (len == 18) {
- times[6] = times[6] * 10;
- }
- }
+ // FIXME makes sense only if we support all GeneralizedTime formats
+ // late check syntax: the last char MUST be Z
+ //if (buffer[offset - 1] != 'Z') {
+ // throw new ASN1Exception(
+ // "ASN.1 GeneralizedTime wrongly encoded at ["
+ // + contentOffset + ']');
+ //}
+
+ // the fractional-seconds elements, if present MUST
+ // omit all trailing zeros
+ // FIXME implement me
+ // if () {
+ // throw new IOException(
+ // "DER ASN.1 GeneralizedTime wrongly encoded at ["
+ // + contentOffset
+ // + "]. Trailing zeros MUST be omitted");
+ // }
}
-}
\ No newline at end of file
+}