-
Notifications
You must be signed in to change notification settings - Fork 405
/
LeshanBootstrapServerBuilderTest.java
208 lines (178 loc) · 7.6 KB
/
LeshanBootstrapServerBuilderTest.java
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*******************************************************************************
* Copyright (c) 2017 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server.californium.bootstrap;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.math.BigInteger;
import java.security.AlgorithmParameters;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.ECPrivateKeySpec;
import java.security.spec.ECPublicKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.KeySpec;
import java.util.Iterator;
import org.eclipse.californium.elements.config.Configuration;
import org.eclipse.californium.scandium.config.DtlsConfig;
import org.eclipse.californium.scandium.dtls.cipher.CipherSuite;
import org.eclipse.leshan.core.oscore.OscoreIdentity;
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.core.util.Hex;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig;
import org.eclipse.leshan.server.bootstrap.BootstrapConfigStore;
import org.eclipse.leshan.server.bootstrap.BootstrapSession;
import org.eclipse.leshan.server.security.BootstrapSecurityStore;
import org.eclipse.leshan.server.security.SecurityInfo;
import org.junit.Before;
import org.junit.Test;
public class LeshanBootstrapServerBuilderTest {
private LeshanBootstrapServerBuilder builder;
private LeshanBootstrapServer server;
private PublicKey publicKey;
private PrivateKey privateKey;
public LeshanBootstrapServerBuilderTest() {
try {
// Get point values
byte[] publicX = Hex
.decodeHex("89c048261979208666f2bfb188be1968fc9021c416ce12828c06f4e314c167b5".toCharArray());
byte[] publicY = Hex
.decodeHex("cbf1eb7587f08e01688d9ada4be859137ca49f79394bad9179326b3090967b68".toCharArray());
byte[] privateS = Hex
.decodeHex("e67b68d2aaeb6550f19d98cade3ad62b39532e02e6b422e1f7ea189dabaea5d2".toCharArray());
// Get Elliptic Curve Parameter spec for secp256r1
AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
algoParameters.init(new ECGenParameterSpec("secp256r1"));
ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);
// Create key specs
KeySpec publicKeySpec = new ECPublicKeySpec(
new ECPoint(new BigInteger(1, publicX), new BigInteger(1, publicY)), parameterSpec);
KeySpec privateKeySpec = new ECPrivateKeySpec(new BigInteger(1, privateS), parameterSpec);
// Get keys
publicKey = KeyFactory.getInstance("EC").generatePublic(publicKeySpec);
privateKey = KeyFactory.getInstance("EC").generatePrivate(privateKeySpec);
} catch (InvalidKeySpecException | NoSuchAlgorithmException | InvalidParameterSpecException e) {
throw new IllegalStateException("Unable to create private/public keys for tests");
}
}
@Before
public void start() {
builder = new LeshanBootstrapServerBuilder();
builder.setConfigStore(new BootstrapConfigStore() {
@Override
public BootstrapConfig get(String endpoint, Identity deviceIdentity, BootstrapSession session) {
return null;
}
});
}
@Test
public void create_server_minimal_parameters() {
server = builder.build();
assertNull(server.getSecuredAddress());
assertNotNull(server.getUnsecuredAddress());
}
@Test
public void create_server_with_securityStore() {
builder.setSecurityStore(new BootstrapSecurityStore() {
@Override
public SecurityInfo getByIdentity(String pskIdentity) {
return null;
}
@Override
public Iterator<SecurityInfo> getAllByEndpoint(String endpoint) {
return null;
}
@Override
public SecurityInfo getByOscoreIdentity(OscoreIdentity oscoreIdentity) {
return null;
}
});
server = builder.build();
assertNotNull(server.getSecuredAddress());
assertNotNull(server.getUnsecuredAddress());
}
@Test
public void create_server_with_securityStore_and_disable_secured_endpoint() {
builder.setSecurityStore(new BootstrapSecurityStore() {
@Override
public SecurityInfo getByIdentity(String pskIdentity) {
return null;
}
@Override
public Iterator<SecurityInfo> getAllByEndpoint(String endpoint) {
return null;
}
@Override
public SecurityInfo getByOscoreIdentity(OscoreIdentity oscoreIdentity) {
return null;
}
});
builder.disableSecuredEndpoint();
server = builder.build();
assertNull(server.getSecuredAddress());
assertNotNull(server.getUnsecuredAddress());
}
@Test
public void create_server_with_securityStore_and_disable_unsecured_endpoint() {
builder.setSecurityStore(new BootstrapSecurityStore() {
@Override
public SecurityInfo getByIdentity(String pskIdentity) {
return null;
}
@Override
public Iterator<SecurityInfo> getAllByEndpoint(String endpoint) {
return null;
}
@Override
public SecurityInfo getByOscoreIdentity(OscoreIdentity oscoreIdentity) {
return null;
}
});
builder.disableUnsecuredEndpoint();
server = builder.build();
assertNotNull(server.getSecuredAddress());
assertNull(server.getUnsecuredAddress());
}
@Test
public void create_server_without_psk_cipher() {
Configuration coapConfiguration = LeshanBootstrapServerBuilder.createDefaultCoapConfiguration();
coapConfiguration.setAsList(DtlsConfig.DTLS_CIPHER_SUITES, CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
builder.setCoapConfig(coapConfiguration);
builder.setPrivateKey(privateKey);
builder.setPublicKey(publicKey);
builder.setSecurityStore(new BootstrapSecurityStore() {
@Override
public SecurityInfo getByIdentity(String pskIdentity) {
return null;
}
@Override
public Iterator<SecurityInfo> getAllByEndpoint(String endpoint) {
return null;
}
@Override
public SecurityInfo getByOscoreIdentity(OscoreIdentity oscoreIdentity) {
return null;
}
});
server = builder.build();
assertNotNull(server.getSecuredAddress());
}
}