diff options
author | Kenny Root <kenny@the-b.org> | 2013-04-12 23:13:55 -0700 |
---|---|---|
committer | Kenny Root <kenny@the-b.org> | 2013-04-13 00:37:18 -0700 |
commit | 3349f11f30ad4cc5ed74e0fc67901838bd2e4e3a (patch) | |
tree | 8fb8f3b7e1ff4f555b01a439777375ae5be9fcdd /src/com/trilead/ssh2/crypto/SimpleDERReader.java | |
parent | fe02c52bd20b12a6c87c22f2066e5ab6ab15fc4f (diff) | |
download | connectbot-3349f11f30ad4cc5ed74e0fc67901838bd2e4e3a.tar.gz connectbot-3349f11f30ad4cc5ed74e0fc67901838bd2e4e3a.tar.bz2 connectbot-3349f11f30ad4cc5ed74e0fc67901838bd2e4e3a.zip |
Partial support for importing EC keys
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(); |