comparison src/ch/ethz/ssh2/crypto/SimpleDERReader.java @ 277:e0da43026046 ganymed

start conversion from trilead to ganymed
author Carl Byington <carl@five-ten-sg.com>
date Fri, 18 Jul 2014 11:59:52 -0700
parents 91a31873c42a
children
comparison
equal deleted inserted replaced
276:3a1deb1040f6 277:e0da43026046
1 /*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5 package ch.ethz.ssh2.crypto; 1 package ch.ethz.ssh2.crypto;
6 2
7 import java.io.IOException; 3 import java.io.IOException;
8 4
9 import java.math.BigInteger; 5 import java.math.BigInteger;
10 6
11 /** 7 /**
12 * SimpleDERReader. 8 * SimpleDERReader.
13 * 9 *
14 * @author Christian Plattner 10 * @author Christian Plattner, plattner@trilead.com
15 * @version 2.50, 03/15/10 11 * @version $Id: SimpleDERReader.java,v 1.1 2007/10/15 12:49:56 cplattne Exp $
16 */ 12 */
17 public class SimpleDERReader 13 public class SimpleDERReader {
18 { 14 private static final int CONSTRUCTED = 0x20;
19 byte[] buffer; 15
20 int pos; 16 byte[] buffer;
21 int count; 17 int pos;
22 18 int count;
23 public SimpleDERReader(byte[] b) 19
24 { 20 public SimpleDERReader(byte[] b) {
25 resetInput(b); 21 resetInput(b);
26 } 22 }
27 23
28 public SimpleDERReader(byte[] b, int off, int len) 24 public SimpleDERReader(byte[] b, int off, int len) {
29 { 25 resetInput(b, off, len);
30 resetInput(b, off, len); 26 }
31 } 27
32 28 public void resetInput(byte[] b) {
33 public void resetInput(byte[] b) 29 resetInput(b, 0, b.length);
34 { 30 }
35 resetInput(b, 0, b.length); 31
36 } 32 public void resetInput(byte[] b, int off, int len) {
37 33 buffer = b;
38 public void resetInput(byte[] b, int off, int len) 34 pos = off;
39 { 35 count = len;
40 buffer = b; 36 }
41 pos = off; 37
42 count = len; 38 private byte readByte() throws IOException {
43 } 39 if (count <= 0)
44 40 throw new IOException("DER byte array: out of data");
45 private byte readByte() throws IOException 41
46 { 42 count--;
47 if (count <= 0) 43 return buffer[pos++];
48 throw new IOException("DER byte array: out of data"); 44 }
49 count--; 45
50 return buffer[pos++]; 46 private byte[] readBytes(int len) throws IOException {
51 } 47 if (len > count)
52 48 throw new IOException("DER byte array: out of data");
53 private byte[] readBytes(int len) throws IOException 49
54 { 50 byte[] b = new byte[len];
55 if (len > count) 51 System.arraycopy(buffer, pos, b, 0, len);
56 throw new IOException("DER byte array: out of data"); 52 pos += len;
57 53 count -= len;
58 byte[] b = new byte[len]; 54 return b;
59 55 }
60 System.arraycopy(buffer, pos, b, 0, len); 56
61 57 public int available() {
62 pos += len; 58 return count;
63 count -= len; 59 }
64 60
65 return b; 61 private int readLength() throws IOException {
66 } 62 int len = readByte() & 0xff;
67 63
68 public int available() 64 if ((len & 0x80) == 0)
69 { 65 return len;
70 return count; 66
71 } 67 int remain = len & 0x7F;
72 68
73 private int readLength() throws IOException 69 if (remain == 0)
74 { 70 return -1;
75 int len = readByte() & 0xff; 71
76 72 len = 0;
77 if ((len & 0x80) == 0) 73
78 return len; 74 while (remain > 0) {
79 75 len = len << 8;
80 int remain = len & 0x7F; 76 len = len | (readByte() & 0xff);
81 77 remain--;
82 if (remain == 0) 78 }
83 return -1; 79
84 80 return len;
85 len = 0; 81 }
86 82
87 while (remain > 0) 83 public int ignoreNextObject() throws IOException {
88 { 84 int type = readByte() & 0xff;
89 len = len << 8; 85 int len = readLength();
90 len = len | (readByte() & 0xff); 86
91 remain--; 87 if ((len < 0) || len > available())
92 } 88 throw new IOException("Illegal len in DER object (" + len + ")");
93 89
94 return len; 90 readBytes(len);
95 } 91 return type;
96 92 }
97 public int ignoreNextObject() throws IOException 93
98 { 94 public BigInteger readInt() throws IOException {
99 int type = readByte() & 0xff; 95 int type = readByte() & 0xff;
100 96
101 int len = readLength(); 97 if (type != 0x02)
102 98 throw new IOException("Expected DER Integer, but found type " + type);
103 if ((len < 0) || len > available()) 99
104 throw new IOException("Illegal len in DER object (" + len + ")"); 100 int len = readLength();
105 101
106 readBytes(len); 102 if ((len < 0) || len > available())
107 103 throw new IOException("Illegal len in DER object (" + len + ")");
108 return type; 104
109 } 105 byte[] b = readBytes(len);
110 106 BigInteger bi = new BigInteger(b);
111 public BigInteger readInt() throws IOException 107 return bi;
112 { 108 }
113 int type = readByte() & 0xff; 109
114 110 public int readConstructedType() throws IOException {
115 if (type != 0x02) 111 int type = readByte() & 0xff;
116 throw new IOException("Expected DER Integer, but found type " + type); 112
117 113 if ((type & CONSTRUCTED) != CONSTRUCTED)
118 int len = readLength(); 114 throw new IOException("Expected constructed type, but was " + type);
119 115
120 if ((len < 0) || len > available()) 116 return type & 0x1f;
121 throw new IOException("Illegal len in DER object (" + len + ")"); 117 }
122 118
123 byte[] b = readBytes(len); 119 public SimpleDERReader readConstructed() throws IOException {
124 120 int len = readLength();
125 return new BigInteger(b); 121
126 } 122 if ((len < 0) || len > available())
127 123 throw new IOException("Illegal len in DER object (" + len + ")");
128 public byte[] readSequenceAsByteArray() throws IOException 124
129 { 125 SimpleDERReader cr = new SimpleDERReader(buffer, pos, len);
130 int type = readByte() & 0xff; 126 pos += len;
131 127 count -= len;
132 if (type != 0x30) 128 return cr;
133 throw new IOException("Expected DER Sequence, but found type " + type); 129 }
134 130
135 int len = readLength(); 131 public byte[] readSequenceAsByteArray() throws IOException {
136 132 int type = readByte() & 0xff;
137 if ((len < 0) || len > available()) 133
138 throw new IOException("Illegal len in DER object (" + len + ")"); 134 if (type != 0x30)
139 135 throw new IOException("Expected DER Sequence, but found type " + type);
140 return readBytes(len); 136
141 } 137 int len = readLength();
142 138
143 public byte[] readOctetString() throws IOException 139 if ((len < 0) || len > available())
144 { 140 throw new IOException("Illegal len in DER object (" + len + ")");
145 int type = readByte() & 0xff; 141
146 142 byte[] b = readBytes(len);
147 if (type != 0x04) 143 return b;
148 throw new IOException("Expected DER Octetstring, but found type " + type); 144 }
149 145
150 int len = readLength(); 146 public String readOid() throws IOException {
151 147 int type = readByte() & 0xff;
152 if ((len < 0) || len > available()) 148
153 throw new IOException("Illegal len in DER object (" + len + ")"); 149 if (type != 0x06)
154 150 throw new IOException("Expected DER OID, but found type " + type);
155 return readBytes(len); 151
156 } 152 int len = readLength();
153
154 if ((len < 1) || len > available())
155 throw new IOException("Illegal len in DER object (" + len + ")");
156
157 byte[] b = readBytes(len);
158 long value = 0;
159 StringBuilder sb = new StringBuilder(64);
160
161 switch (b[0] / 40) {
162 case 0:
163 sb.append('0');
164 break;
165
166 case 1:
167 sb.append('1');
168 b[0] -= 40;
169 break;
170
171 default:
172 sb.append('2');
173 b[0] -= 80;
174 break;
175 }
176
177 for (int i = 0; i < len; i++) {
178 value = (value << 7) + (b[i] & 0x7F);
179
180 if ((b[i] & 0x80) == 0) {
181 sb.append('.');
182 sb.append(value);
183 value = 0;
184 }
185 }
186
187 return sb.toString();
188 }
189
190 public byte[] readOctetString() throws IOException {
191 int type = readByte() & 0xff;
192
193 if (type != 0x04 && type != 0x03)
194 throw new IOException("Expected DER Octetstring, but found type " + type);
195
196 int len = readLength();
197
198 if ((len < 0) || len > available())
199 throw new IOException("Illegal len in DER object (" + len + ")");
200
201 byte[] b = readBytes(len);
202 return b;
203 }
157 204
158 } 205 }