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/19 10:37:48 UTC
svn commit: r395175 - in
/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1:
BerInputStream.java DerInputStream.java
Author: smishura
Date: Wed Apr 19 01:37:46 2006
New Revision: 395175
URL: http://svn.apache.org/viewcvs?rev=395175&view=rev
Log:
Move decoding UTCTime from DER to BER stream + minor improvments
Modified:
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/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=395175&r1=395174&r2=395175&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 Wed Apr 19 01:37:46 2006
@@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
+import java.util.Calendar;
/**
@@ -293,7 +294,75 @@
* @throws IOException - if an I/O error occurs or the end of the stream is reached
*/
public void readUTCTime() throws IOException {
- throw new ASN1Exception("Not supported");
+
+ if ((tag & ASN1Constants.PC_CONSTRUCTED) != 0) {
+ // It is a string type and it can be encoded as primitive or constructed.
+ throw new ASN1Exception("Decoding constructed ASN.1 UTCTime"
+ + " type is not provided");
+ }
+
+ switch (length) {
+ case ASN1UTCTime.UTC_HM:
+ case ASN1UTCTime.UTC_HMS:
+ break;
+ case ASN1UTCTime.UTC_LOCAL_HM:
+ case ASN1UTCTime.UTC_LOCAL_HMS:
+ // FIXME only coordinated universal time formats are supported
+ throw new ASN1Exception(
+ "ASN.1 UTCTime: local time format is not supported.");
+ default:
+ throw new ASN1Exception(
+ "ASN.1 UTCTime: wrong length, identifier at ["
+ + tagOffset + ']');
+ }
+
+ //FIXME: any other optimizations?
+ readContent();
+
+ // FIXME store string somewhare to allow a custom time type perform additional checks
+
+ // check syntax: the last char MUST be Z
+ if (buffer[offset - 1] != 'Z') {
+ throw new ASN1Exception("ASN.1 UTCTime wrongly encoded at ["
+ + contentOffset + ']');
+ }
+
+ if (times == null) {
+ times = new int[7];
+ }
+
+ times[0] = strToInt(contentOffset, 2) + 1900; //year
+ if (Calendar.getInstance().get(Calendar.YEAR) - times[0] > 80) {
+ times[0] += 100;
+ }
+
+ times[1] = strToInt(contentOffset + 2, 2); //month
+ times[2] = strToInt(contentOffset + 4, 2); //day
+ times[3] = strToInt(contentOffset + 6, 2); //hour
+ times[4] = strToInt(contentOffset + 8, 2); //minute
+
+ if (length == ASN1UTCTime.UTC_HMS) {
+ times[5] = strToInt(contentOffset + 10, 2); //second
+ }
+
+ //FIXME check all time values for valid numbers!!!
+ }
+
+ //TODO comment me
+ private int strToInt(int off, int count) throws ASN1Exception {
+
+ //FIXME works only with buffer
+
+ int c;
+ int result = 0;
+ for (int i = off, end = off + count; i < end; i++) {
+ c = buffer[i] - 48;
+ if (c < 0 || c > 9) {
+ throw new ASN1Exception("Time encoding has invalid char");
+ }
+ result = result * 10 + c;
+ }
+ return result;
}
/**
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=395175&r1=395174&r2=395175&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 Wed Apr 19 01:37:46 2006
@@ -23,8 +23,6 @@
import java.io.IOException;
import java.io.InputStream;
-import java.util.Calendar;
-
/**
* Decodes ASN.1 types encoded with DER (X.690)
@@ -148,40 +146,18 @@
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 UTCTime type MUST have primitive encoding");
- }
-
- //FIXME it should check format and invoke BerInputStream
- //FIXME: any other optimizations?
- readContent();
- String timeStr = new String(buffer, contentOffset, length);
-
- // FIXME store string somewhare to allow a custom time type perform additional checks
-
- // check syntax: MUST be YYMMDDHHMMSS'Z'
- if (timeStr.length() != 13 || // invalid length
- timeStr.charAt(12) != 'Z') // the last char MUST be Z
- {
- throw new ASN1Exception("ASN.1 UTCTime wrongly encoded at ["
- + contentOffset + "] ");
+ "ASN.1 UTCTime: constructed identifier at [" + tagOffset
+ + "]. Not valid for DER.");
}
- if (times == null) {
- times = new int[7];
- }
-
- times[0] = ASN1Time.getTimeValue(timeStr, 0, 2) + 1900; //year
- if (Calendar.getInstance().get(Calendar.YEAR) - times[0] > 80) {
- times[0] += 100;
+ // check format: DER uses YYMMDDHHMMSS'Z' only
+ if (length != ASN1UTCTime.UTC_HMS) {
+ throw new ASN1Exception(
+ "ASN.1 UTCTime: wrong format for DER, identifier at ["
+ + tagOffset + ']');
}
- times[1] = ASN1Time.getTimeValue(timeStr, 2, 2); //month
- times[2] = ASN1Time.getTimeValue(timeStr, 4, 2); //day
- times[3] = ASN1Time.getTimeValue(timeStr, 6, 2); //hour
- times[4] = ASN1Time.getTimeValue(timeStr, 8, 2); //minute
- times[5] = ASN1Time.getTimeValue(timeStr, 10, 2); //second
-
- //FIXME check all time values for valid numbers!!!
+ super.readUTCTime();
}
/**