aboutsummaryrefslogtreecommitdiffstats
path: root/sshlib/src/test/java/com/trilead/ssh2/crypto/SimpleDERReaderTest.java
blob: 6a97218bf390dbea71eeb2ebeeed887155c0787d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.trilead.ssh2.crypto;

import org.junit.Test;

import java.io.IOException;
import java.math.BigInteger;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

/**
 * Created by kenny on 12/25/15.
 */
public class SimpleDERReaderTest {
    @Test
    public void readLength_Extended_OverlyLongLength() throws Exception {
        byte[] vector = new byte[] {
                (byte) 0x85, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        assertEquals(-1, reader.readLength());
    }

    @Test
    public void readLength_Extended_TooLongForInt() throws Exception {
        byte[] vector = new byte[] {
                (byte) 0x84, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        assertEquals(-1, reader.readLength());
    }

    @Test
    public void readLength_Extended_Zero() throws Exception {
        byte[] vector = new byte[] {
                (byte) 0x80, (byte) 0x01
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        assertEquals(-1, reader.readLength());
    }

    @Test
    public void readLength_Extended_Valid() throws Exception {
        byte[] vector = new byte[] {
                (byte) 0x82, (byte) 0x05, (byte) 0xFF
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        assertEquals(0x5FF, reader.readLength());
    }

    @Test
    public void readLength_Short_Zero() throws Exception {
        byte[] vector = new byte[] {
                (byte) 0x00
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        assertEquals(0, reader.readLength());
    }

    @Test
    public void readLength_Short_Regular() throws Exception {
        byte[] vector = new byte[] {
                (byte) 0x09
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        assertEquals(9, reader.readLength());
    }

    @Test
    public void readInt_MaxInt() throws Exception {
        byte[] vector = new byte[] {
                (byte) 0x02, (byte) 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        assertEquals(BigInteger.valueOf(0xFFFFFFFF), reader.readInt());
    }

    @Test
    public void readInt_NotReallyInteger() throws Exception {
        byte[] vector = new byte[] {
                (byte) 0x01, (byte) 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        try {
            reader.readInt();
        } catch (IOException expected) {
            assertThat(expected.getMessage(), containsString("Expected DER Integer"));
        }
    }

    @Test
    public void readInt_InvalidLength() throws Exception {
        byte[] vector = new byte[] {
                (byte) 0x02, (byte) 0x80, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        try {
            reader.readInt();
        } catch (IOException expected) {
            assertThat(expected.getMessage(), containsString("Illegal len"));
        }
    }

    @Test
    public void readInt_ShortArray() throws Exception {
        byte[] vector = new byte[] {
                (byte) 0x02, (byte) 0x02, (byte) 0xFF
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        try {
            reader.readInt();
        } catch (IOException expected) {
        }
    }

    @Test
    public void readOid_InvalidLength() throws Exception {
        byte[] vector = new byte[]{
                (byte) 0x02, (byte) 0x80, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        try {
            reader.readOid();
        } catch (IOException expected) {
        }
    }

    @Test
    public void readOid_TooShort() throws Exception {
        byte[] vector = new byte[]{
                (byte) 0x02, (byte) 0x00
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        try {
            reader.readOid();
        } catch (IOException expected) {
        }
    }

    @Test
    public void readOid_NotOidValue() throws Exception {
        byte[] vector = new byte[]{
                (byte) 0x02, (byte) 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        try {
            reader.readOid();
        } catch (IOException expected) {
        }
    }

    @Test
    public void readOid_Valid1() throws Exception {
        byte[] vector = new byte[]{
                (byte) 0x06, (byte) 0x01, (byte) 0x28
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        assertEquals("1.0", reader.readOid());
    }

    @Test
    public void readOid_Valid1Prefix() throws Exception {
        byte[] vector = new byte[]{
                (byte) 0x06, (byte) 0x09, (byte) 0x2a, (byte) 0x86, (byte) 0x48, (byte) 0x86, (byte) 0xf7, (byte) 0x0d, (byte) 0x01, (byte) 0x01, (byte) 0x0b
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        assertEquals("1.2.840.113549.1.1.11", reader.readOid());
    }

    @Test
    public void readOid_Valid0Prefix() throws Exception {
        byte[] vector = new byte[]{
                (byte) 0x06, (byte) 0x0A, (byte) 0x09, (byte) 0x92, (byte) 0x26, (byte) 0x89, (byte) 0x93, (byte) 0xF2, (byte) 0x2C, (byte) 0x64, (byte) 0x04, (byte) 0x0D
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        assertEquals("0.9.2342.19200300.100.4.13", reader.readOid());
    }

    @Test
    public void readOid_Valid2Prefix() throws Exception {
        byte[] vector = new byte[]{
                (byte) 0x06, (byte) 0x03, (byte) 0x55, (byte) 0x1D, (byte) 0x0E
        };
        SimpleDERReader reader = new SimpleDERReader(vector);
        assertEquals("2.5.29.14", reader.readOid());
    }
}