diff options
Diffstat (limited to 'src/com/trilead/ssh2/crypto/SimpleDERReader.java')
-rw-r--r-- | src/com/trilead/ssh2/crypto/SimpleDERReader.java | 75 |
1 files changed, 72 insertions, 3 deletions
diff --git a/src/com/trilead/ssh2/crypto/SimpleDERReader.java b/src/com/trilead/ssh2/crypto/SimpleDERReader.java index 9f626ad..ff8112a 100644 --- a/src/com/trilead/ssh2/crypto/SimpleDERReader.java +++ b/src/com/trilead/ssh2/crypto/SimpleDERReader.java @@ -12,6 +12,8 @@ import java.math.BigInteger; */ public class SimpleDERReader { + private static final int CONSTRUCTED = 0x20; + byte[] buffer; int pos; int count; @@ -123,6 +125,30 @@ public class SimpleDERReader return bi; } + public int readConstructedType() throws IOException { + int type = readByte() & 0xff; + + if ((type & CONSTRUCTED) != CONSTRUCTED) + throw new IOException("Expected constructed type, but was " + type); + + return type & 0x1f; + } + + public SimpleDERReader readConstructed() throws IOException + { + int len = readLength(); + + if ((len < 0) || len > available()) + throw new IOException("Illegal len in DER object (" + len + ")"); + + SimpleDERReader cr = new SimpleDERReader(buffer, pos, len); + + pos += len; + count -= len; + + return cr; + } + public byte[] readSequenceAsByteArray() throws IOException { int type = readByte() & 0xff; @@ -139,12 +165,55 @@ public class SimpleDERReader return b; } - + + public String readOid() throws IOException + { + int type = readByte() & 0xff; + + if (type != 0x06) + throw new IOException("Expected DER OID, but found type " + type); + + int len = readLength(); + + if ((len < 1) || len > available()) + throw new IOException("Illegal len in DER object (" + len + ")"); + + byte[] b = readBytes(len); + + long value = 0; + + StringBuilder sb = new StringBuilder(64); + switch(b[0] / 40) { + case 0: + sb.append('0'); + break; + case 1: + sb.append('1'); + b[0] -= 40; + break; + default: + sb.append('2'); + b[0] -= 80; + break; + } + + for (int i = 0; i < len; i++) { + value = (value << 7) + (b[i] & 0x7F); + if ((b[i] & 0x80) == 0) { + sb.append('.'); + sb.append(value); + value = 0; + } + } + + return sb.toString(); + } + public byte[] readOctetString() throws IOException { int type = readByte() & 0xff; - - if (type != 0x04) + + if (type != 0x04 && type != 0x03) throw new IOException("Expected DER Octetstring, but found type " + type); int len = readLength(); |