Subversion Repositories Programming Utils

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 rm5248 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
package org.apache.sshd.server.keyprovider;
20
 
21
import java.io.File;
22
import java.security.spec.ECGenParameterSpec;
23
 
24
import org.apache.sshd.common.KeyPairProvider;
25
import org.apache.sshd.common.util.SecurityUtils;
26
import org.apache.sshd.util.BaseTest;
27
import org.junit.Test;
28
 
29
import static org.junit.Assert.assertEquals;
30
import static org.junit.Assert.assertNotNull;
31
 
32
/**
33
 * TODO Add javadoc
34
 *
35
 * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
36
 */
37
public class SimpleGeneratorHostKeyProviderTest extends BaseTest {
38
 
39
    @Test
40
    public void testDSA() {
41
        File path = new File("target/keys");
42
        path.mkdirs();
43
        path = new File(path, "simple.key");
44
        path.delete();
45
 
46
        // Generate
47
        SimpleGeneratorHostKeyProvider provider = new SimpleGeneratorHostKeyProvider();
48
        provider.setAlgorithm("DSA");
49
        provider.setKeySize(512);
50
        provider.setPath(path.getPath());
51
        assertEquals(KeyPairProvider.SSH_DSS, provider.getKeyTypes());
52
        assertNotNull(provider.loadKey(KeyPairProvider.SSH_DSS));
53
 
54
        // Read existing
55
        provider = new SimpleGeneratorHostKeyProvider();
56
        provider.setAlgorithm("DSA");
57
        provider.setKeySize(512);
58
        provider.setPath(path.getPath());
59
        assertEquals(KeyPairProvider.SSH_DSS, provider.getKeyTypes());
60
        assertNotNull(provider.loadKey(KeyPairProvider.SSH_DSS));
61
    }
62
 
63
    @Test
64
    public void testRSA() {
65
        File path = new File("target/keys");
66
        path.mkdirs();
67
        path = new File(path, "simple.key");
68
        path.delete();
69
 
70
        // Generate
71
        SimpleGeneratorHostKeyProvider provider = new SimpleGeneratorHostKeyProvider();
72
        provider.setAlgorithm("RSA");
73
        provider.setKeySize(512);
74
        provider.setPath(path.getPath());
75
        assertEquals(KeyPairProvider.SSH_RSA, provider.getKeyTypes());
76
        assertNotNull(provider.loadKey(KeyPairProvider.SSH_RSA));
77
 
78
        // Read existing
79
        provider = new SimpleGeneratorHostKeyProvider();
80
        provider.setAlgorithm("RSA");
81
        provider.setKeySize(512);
82
        provider.setPath(path.getPath());
83
        assertEquals(KeyPairProvider.SSH_RSA, provider.getKeyTypes());
84
        assertNotNull(provider.loadKey(KeyPairProvider.SSH_RSA));
85
    }
86
 
87
    @Test
88
    public void testEC_NISTP256() {
89
        if (!SecurityUtils.isBouncyCastleRegistered()) {
90
            return;
91
        }
92
 
93
        File path = new File("target/keys");
94
        path.mkdirs();
95
        path = new File(path, "simple.key");
96
        path.delete();
97
 
98
        // Generate
99
        SimpleGeneratorHostKeyProvider provider = new SimpleGeneratorHostKeyProvider();
100
        provider.setAlgorithm("EC");
101
        provider.setKeySpec(new ECGenParameterSpec("prime256v1"));
102
        provider.setPath(path.getPath());
103
        assertEquals(KeyPairProvider.ECDSA_SHA2_NISTP256, provider.getKeyTypes());
104
        assertNotNull(provider.loadKey(KeyPairProvider.ECDSA_SHA2_NISTP256));
105
 
106
        // Read existing
107
        provider = new SimpleGeneratorHostKeyProvider();
108
        provider.setAlgorithm("EC");
109
        provider.setKeySpec(new ECGenParameterSpec("prime256v1"));
110
        provider.setPath(path.getPath());
111
        assertEquals(KeyPairProvider.ECDSA_SHA2_NISTP256, provider.getKeyTypes());
112
        assertNotNull(provider.loadKey(KeyPairProvider.ECDSA_SHA2_NISTP256));
113
    }
114
 
115
    @Test
116
    public void testEC_NISTP384() {
117
        if (!SecurityUtils.isBouncyCastleRegistered()) {
118
            return;
119
        }
120
 
121
        File path = new File("target/keys");
122
        path.mkdirs();
123
        path = new File(path, "simple.key");
124
        path.delete();
125
 
126
        // Generate
127
        SimpleGeneratorHostKeyProvider provider = new SimpleGeneratorHostKeyProvider();
128
        provider.setAlgorithm("EC");
129
        provider.setKeySpec(new ECGenParameterSpec("P-384"));
130
        provider.setPath(path.getPath());
131
        assertEquals(KeyPairProvider.ECDSA_SHA2_NISTP384, provider.getKeyTypes());
132
        assertNotNull(provider.loadKey(KeyPairProvider.ECDSA_SHA2_NISTP384));
133
 
134
        // Read existing
135
        provider = new SimpleGeneratorHostKeyProvider();
136
        provider.setAlgorithm("EC");
137
        provider.setKeySpec(new ECGenParameterSpec("P-384"));
138
        provider.setPath(path.getPath());
139
        assertEquals(KeyPairProvider.ECDSA_SHA2_NISTP384, provider.getKeyTypes());
140
        assertNotNull(provider.loadKey(KeyPairProvider.ECDSA_SHA2_NISTP384));
141
    }
142
 
143
    @Test
144
    public void testEC_NISTP521() {
145
        if (!SecurityUtils.isBouncyCastleRegistered()) {
146
            return;
147
        }
148
 
149
        File path = new File("target/keys");
150
        path.mkdirs();
151
        path = new File(path, "simple.key");
152
        path.delete();
153
 
154
        // Generate
155
        SimpleGeneratorHostKeyProvider provider = new SimpleGeneratorHostKeyProvider();
156
        provider.setAlgorithm("EC");
157
        provider.setKeySpec(new ECGenParameterSpec("P-521"));
158
        provider.setPath(path.getPath());
159
        assertEquals(KeyPairProvider.ECDSA_SHA2_NISTP521, provider.getKeyTypes());
160
        assertNotNull(provider.loadKey(KeyPairProvider.ECDSA_SHA2_NISTP521));
161
 
162
        // Read existing
163
        provider = new SimpleGeneratorHostKeyProvider();
164
        provider.setAlgorithm("EC");
165
        provider.setKeySpec(new ECGenParameterSpec("P-521"));
166
        provider.setPath(path.getPath());
167
        assertEquals(KeyPairProvider.ECDSA_SHA2_NISTP521, provider.getKeyTypes());
168
        assertNotNull(provider.loadKey(KeyPairProvider.ECDSA_SHA2_NISTP521));
169
    }
170
}