001: package ch.ethz.ssh2.crypto;
002:
003: import java.io.IOException;
004:
005: import java.math.BigInteger;
006:
007: /**
008: * SimpleDERReader.
009: *
010: * @author Christian Plattner, plattner@inf.ethz.ch
011: * @version $Id: SimpleDERReader.java,v 1.3 2006/08/11 12:24:00 cplattne Exp $
012: */
013: public class SimpleDERReader {
014: byte[] buffer;
015: int pos;
016: int count;
017:
018: public SimpleDERReader(byte[] b) {
019: resetInput(b);
020: }
021:
022: public SimpleDERReader(byte[] b, int off, int len) {
023: resetInput(b, off, len);
024: }
025:
026: public void resetInput(byte[] b) {
027: resetInput(b, 0, b.length);
028: }
029:
030: public void resetInput(byte[] b, int off, int len) {
031: buffer = b;
032: pos = off;
033: count = len;
034: }
035:
036: private byte readByte() throws IOException {
037: if (count <= 0)
038: throw new IOException("DER byte array: out of data");
039: count--;
040: return buffer[pos++];
041: }
042:
043: private byte[] readBytes(int len) throws IOException {
044: if (len > count)
045: throw new IOException("DER byte array: out of data");
046:
047: byte[] b = new byte[len];
048:
049: System.arraycopy(buffer, pos, b, 0, len);
050:
051: pos += len;
052: count -= len;
053:
054: return b;
055: }
056:
057: public int available() {
058: return count;
059: }
060:
061: private int readLength() throws IOException {
062: int len = readByte() & 0xff;
063:
064: if ((len & 0x80) == 0)
065: return len;
066:
067: int remain = len & 0x7F;
068:
069: if (remain == 0)
070: return -1;
071:
072: len = 0;
073:
074: while (remain > 0) {
075: len = len << 8;
076: len = len | (readByte() & 0xff);
077: remain--;
078: }
079:
080: return len;
081: }
082:
083: public int ignoreNextObject() throws IOException {
084: int type = readByte() & 0xff;
085:
086: int len = readLength();
087:
088: if ((len < 0) || len > available())
089: throw new IOException("Illegal len in DER object (" + len
090: + ")");
091:
092: readBytes(len);
093:
094: return type;
095: }
096:
097: public BigInteger readInt() throws IOException {
098: int type = readByte() & 0xff;
099:
100: if (type != 0x02)
101: throw new IOException(
102: "Expected DER Integer, but found type " + type);
103:
104: int len = readLength();
105:
106: if ((len < 0) || len > available())
107: throw new IOException("Illegal len in DER object (" + len
108: + ")");
109:
110: byte[] b = readBytes(len);
111:
112: BigInteger bi = new BigInteger(b);
113:
114: return bi;
115: }
116:
117: public byte[] readSequenceAsByteArray() throws IOException {
118: int type = readByte() & 0xff;
119:
120: if (type != 0x30)
121: throw new IOException(
122: "Expected DER Sequence, but found type " + type);
123:
124: int len = readLength();
125:
126: if ((len < 0) || len > available())
127: throw new IOException("Illegal len in DER object (" + len
128: + ")");
129:
130: byte[] b = readBytes(len);
131:
132: return b;
133: }
134:
135: public byte[] readOctetString() throws IOException {
136: int type = readByte() & 0xff;
137:
138: if (type != 0x04)
139: throw new IOException(
140: "Expected DER Octetstring, but found type " + type);
141:
142: int len = readLength();
143:
144: if ((len < 0) || len > available())
145: throw new IOException("Illegal len in DER object (" + len
146: + ")");
147:
148: byte[] b = readBytes(len);
149:
150: return b;
151: }
152:
153: }
|