use of org.eclipse.californium.scandium.dtls.pskstore.AsyncAdvancedPskStore in project californium by eclipse.
the class ExtendedTestServer method addEndpoint.
private void addEndpoint(InetSocketAddress dtlsInterface, BaseConfig cliConfig) {
InterfaceType interfaceType = dtlsInterface.getAddress().isLoopbackAddress() ? InterfaceType.LOCAL : InterfaceType.EXTERNAL;
Configuration configuration = getConfig(Protocol.DTLS, interfaceType);
String tag = "dtls:" + StringUtil.toString(dtlsInterface);
int handshakeResultDelayMillis = configuration.getTimeAsInt(DTLS_HANDSHAKE_RESULT_DELAY, TimeUnit.MILLISECONDS);
long healthStatusIntervalMillis = configuration.get(SystemConfig.HEALTH_STATUS_INTERVAL, TimeUnit.MILLISECONDS);
Integer cidLength = configuration.get(DtlsConfig.DTLS_CONNECTION_ID_LENGTH);
if (cidLength == null || cidLength < 6) {
throw new IllegalArgumentException("cid length must be at least 6 for cluster!");
}
initCredentials();
DtlsConnectorConfig.Builder dtlsConfigBuilder = DtlsConnectorConfig.builder(configuration);
if (cliConfig.clientAuth != null) {
dtlsConfigBuilder.set(DtlsConfig.DTLS_CLIENT_AUTHENTICATION_MODE, cliConfig.clientAuth);
}
AsyncAdvancedPskStore asyncPskStore = new AsyncAdvancedPskStore(new PlugPskStore());
asyncPskStore.setDelay(handshakeResultDelayMillis);
dtlsConfigBuilder.setAdvancedPskStore(asyncPskStore);
dtlsConfigBuilder.setAddress(dtlsInterface);
X509KeyManager keyManager = SslContextUtil.getX509KeyManager(serverCredentials);
AsyncKeyManagerCertificateProvider certificateProvider = new AsyncKeyManagerCertificateProvider(keyManager, CertificateType.RAW_PUBLIC_KEY, CertificateType.X_509);
certificateProvider.setDelay(handshakeResultDelayMillis);
dtlsConfigBuilder.setCertificateIdentityProvider(certificateProvider);
AsyncNewAdvancedCertificateVerifier.Builder verifierBuilder = AsyncNewAdvancedCertificateVerifier.builder();
if (cliConfig.trustall) {
verifierBuilder.setTrustAllCertificates();
} else {
verifierBuilder.setTrustedCertificates(trustedCertificates);
}
verifierBuilder.setTrustAllRPKs();
AsyncNewAdvancedCertificateVerifier verifier = verifierBuilder.build();
verifier.setDelay(handshakeResultDelayMillis);
dtlsConfigBuilder.setAdvancedCertificateVerifier(verifier);
dtlsConfigBuilder.setConnectionListener(new MdcConnectionListener());
dtlsConfigBuilder.setLoggingTag(tag);
if (healthStatusIntervalMillis > 0) {
DtlsHealthLogger health = new DtlsHealthLogger(tag);
dtlsConfigBuilder.setHealthHandler(health);
add(health);
// reset to prevent active logger
dtlsConfigBuilder.set(SystemConfig.HEALTH_STATUS_INTERVAL, 0, TimeUnit.MILLISECONDS);
}
DtlsConnectorConfig dtlsConnectorConfig = dtlsConfigBuilder.build();
CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
builder.setConnector(new DTLSConnector(dtlsConnectorConfig));
builder.setConfiguration(dtlsConnectorConfig.getConfiguration());
CoapEndpoint endpoint = builder.build();
addEndpoint(endpoint);
print(endpoint, interfaceType);
}
use of org.eclipse.californium.scandium.dtls.pskstore.AsyncAdvancedPskStore in project californium by eclipse.
the class SecureTest method testSecureHandshakes.
/**
* Test processing of repeated CLIENT_HELLOs.
*
* Setup the dtls server to emulate a slow psk lookup by adding a delay. Use
* a short dtls retransmission timeout for the clients to generate more dtls
* message retransmission. With both, the probability that multiple
* CLIENT_HELLOs are executed simultaneous is high enough to fail the test.
*
* @param loop number of loop for error message
* @throws Exception if the test fails
*/
public void testSecureHandshakes(int loop) throws Exception {
CoapEndpoint serverEndpoint = createEndpoint("server", "dummy", TEST_EXCHANGE_LIFETIME, TEST_ACK_TIMEOUT, TEST_DTLS_TIMEOUT, TEST_DTLS_PSK_DELAY);
CoapServer server = new CoapServer(serverEndpoint.getConfig());
server.addEndpoint(serverEndpoint);
server.start();
URI uri = serverEndpoint.getUri();
List<CoapEndpoint> clientEndpoints = new ArrayList<>();
int clients = TestScope.enableIntensiveTests() ? TEST_CLIENTS : 10;
for (int i = 0; i < clients; ++i) {
CoapEndpoint clientEndpoint = createEndpoint("client-" + i, "client-" + i, TEST_EXCHANGE_LIFETIME, TEST_ACK_TIMEOUT, TEST_DTLS_FAST_TIMEOUT, 0);
clientEndpoint.start();
clientEndpoints.add(clientEndpoint);
}
List<Request> requests = new ArrayList<>();
for (CoapEndpoint clientEndpoint : clientEndpoints) {
Request request = Request.newGet();
request.setURI(uri);
clientEndpoint.sendRequest(request);
requests.add(request);
}
List<Integer> pending = new ArrayList<>();
List<Integer> errors = new ArrayList<>();
for (int index = 0; index < requests.size(); ++index) {
Request request = requests.get(index);
Response response = request.waitForResponse(TEST_EXCHANGE_LIFETIME);
if (response == null) {
if (request.getSendError() != null) {
errors.add(index);
} else {
pending.add(index);
}
}
}
for (CoapEndpoint clientEndpoint : clientEndpoints) {
try {
clientEndpoint.destroy();
} catch (Exception ex) {
}
}
try {
server.destroy();
} catch (Exception ex) {
}
if (!pending.isEmpty() || !errors.isEmpty()) {
StringBuilder message = new StringBuilder("loop: ");
message.append(loop).append(" - ");
if (!errors.isEmpty()) {
message.append(errors.size()).append(" requests failed, ");
int max = Math.min(5, errors.size());
for (int index = 0; index < max; ++index) {
message.append(errors.get(index)).append(' ');
}
message.append(", ");
}
if (!errors.isEmpty()) {
message.append(pending.size()).append(" requests pending, ");
int max = Math.min(5, pending.size());
for (int index = 0; index < max; ++index) {
message.append(pending.get(index)).append(' ');
}
}
fail(message.toString());
}
for (AsyncAdvancedPskStore pskStore : pskStores) {
pskStore.shutdown();
}
pskStores.clear();
System.gc();
Thread.sleep(200);
}
use of org.eclipse.californium.scandium.dtls.pskstore.AsyncAdvancedPskStore in project californium by eclipse.
the class SecureTest method createEndpoint.
private CoapEndpoint createEndpoint(String tag, String pskIdentity, int exchangeTimeout, int coapTimeout, int dtlsTimeout, int pskDelay) {
// setup CoAP config
Configuration config = network.createTestConfig().set(CoapConfig.ACK_TIMEOUT, coapTimeout, TimeUnit.MILLISECONDS).set(CoapConfig.EXCHANGE_LIFETIME, exchangeTimeout, TimeUnit.MILLISECONDS).set(DtlsConfig.DTLS_RETRANSMISSION_TIMEOUT, dtlsTimeout, TimeUnit.MILLISECONDS).set(DtlsConfig.DTLS_MAX_RETRANSMISSIONS, TEST_DTLS_RETRANSMISSIONS).set(DtlsConfig.DTLS_RECEIVER_THREAD_COUNT, 2).set(DtlsConfig.DTLS_CONNECTOR_THREAD_COUNT, 2);
// setup DTLS Config
TestUtilPskStore singlePskStore = new TestUtilPskStore();
singlePskStore.set(pskIdentity, PSK_KEY.getBytes());
singlePskStore.setCatchAll(true);
AsyncAdvancedPskStore pskStore = new AsyncAdvancedPskStore(singlePskStore);
pskStore.setDelay(-pskDelay);
pskStores.add(pskStore);
Builder builder = new DtlsConnectorConfig.Builder(config).setAddress(TestTools.LOCALHOST_EPHEMERAL).setLoggingTag(tag).setAdvancedPskStore(pskStore);
DtlsConnectorConfig dtlsConfig = builder.build();
// create endpoint for tests
DTLSConnector connector = new DTLSConnector(dtlsConfig);
CoapEndpoint.Builder coapBuilder = new CoapEndpoint.Builder();
coapBuilder.setConnector(connector);
coapBuilder.setConfiguration(config);
CoapEndpoint coapEndpoint = coapBuilder.build();
return coapEndpoint;
}
use of org.eclipse.californium.scandium.dtls.pskstore.AsyncAdvancedPskStore in project californium by eclipse.
the class AbstractTestServer method addEndpoints.
/**
* Add endpoints.
*
* @param selectAddress list of regular expression to filter the endpoints by
* {@link InetAddress#getHostAddress()}. May be
* {@code null} or {@code empty}, if endpoints should not
* be filtered by their host address.
* @param interfaceTypes list of type to filter the endpoints. Maybe
* {@code null} or empty, if endpoints should not be
* filtered by type.
* @param protocols list of protocols to create endpoints for.
* @param cliConfig client cli-config.
*/
public void addEndpoints(List<String> selectAddress, List<InterfaceType> interfaceTypes, List<Protocol> protocols, BaseConfig cliConfig) {
int coapPort = config.get(CoapConfig.COAP_PORT);
int coapsPort = config.get(CoapConfig.COAP_SECURE_PORT);
if (protocols.contains(Protocol.DTLS) || protocols.contains(Protocol.TLS)) {
initCredentials();
serverSslContext = getServerSslContext(cliConfig.trustall, SslContextUtil.DEFAULT_SSL_PROTOCOL);
if (serverSslContext == null && protocols.contains(Protocol.TLS)) {
throw new IllegalArgumentException("TLS not supported, credentials missing!");
}
}
List<InetAddress> used = new ArrayList<>();
for (InetAddress addr : NetworkInterfacesUtil.getNetworkInterfaces()) {
if (used.contains(addr)) {
continue;
}
if (interfaceTypes != null && !interfaceTypes.isEmpty()) {
if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
if (!interfaceTypes.contains(InterfaceType.LOCAL)) {
String scope = "???";
if (addr.isLoopbackAddress()) {
scope = "lo";
} else if (addr.isLinkLocalAddress()) {
scope = "link";
}
LOGGER.info("{}skip local {} ({})", getTag(), addr, scope);
continue;
}
} else {
if (!interfaceTypes.contains(InterfaceType.EXTERNAL)) {
LOGGER.info("{}skip external {}", getTag(), addr);
continue;
}
}
if (addr instanceof Inet4Address) {
if (!interfaceTypes.contains(InterfaceType.IPV4)) {
LOGGER.info("{}skip ipv4 {}", getTag(), addr);
continue;
}
} else if (addr instanceof Inet6Address) {
if (!interfaceTypes.contains(InterfaceType.IPV6)) {
LOGGER.info("{}skip ipv6 {}", getTag(), addr);
continue;
}
}
}
if (selectAddress != null && !selectAddress.isEmpty()) {
boolean found = false;
String name = addr.getHostAddress();
for (String filter : selectAddress) {
if (name.matches(filter)) {
found = true;
break;
}
}
if (!found && addr instanceof Inet6Address) {
Matcher matcher = IPV6_SCOPE.matcher(name);
if (matcher.matches()) {
// apply filter also on interface name
name = matcher.group(1) + "%" + ((Inet6Address) addr).getScopedInterface().getName();
for (String filter : selectAddress) {
if (name.matches(filter)) {
found = true;
break;
}
}
}
}
if (!found) {
continue;
}
}
used.add(addr);
InterfaceType interfaceType = addr.isLoopbackAddress() ? InterfaceType.LOCAL : InterfaceType.EXTERNAL;
if (protocols.contains(Protocol.UDP) || protocols.contains(Protocol.TCP)) {
InetSocketAddress bindToAddress = new InetSocketAddress(addr, coapPort);
if (protocols.contains(Protocol.UDP)) {
Configuration udpConfig = getConfig(Protocol.UDP, interfaceType);
CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
builder.setInetSocketAddress(bindToAddress);
builder.setConfiguration(udpConfig);
CoapEndpoint endpoint = builder.build();
addEndpoint(endpoint);
print(endpoint, interfaceType);
}
if (protocols.contains(Protocol.TCP)) {
Configuration tcpConfig = getConfig(Protocol.TCP, interfaceType);
TcpServerConnector connector = new TcpServerConnector(bindToAddress, tcpConfig);
CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
builder.setConnector(connector);
builder.setConfiguration(tcpConfig);
CoapEndpoint endpoint = builder.build();
addEndpoint(endpoint);
print(endpoint, interfaceType);
}
}
if (protocols.contains(Protocol.DTLS) || protocols.contains(Protocol.TLS)) {
InetSocketAddress bindToAddress = new InetSocketAddress(addr, coapsPort);
if (protocols.contains(Protocol.DTLS)) {
Configuration dtlsConfig = getConfig(Protocol.DTLS, interfaceType);
int handshakeResultDelayMillis = dtlsConfig.getTimeAsInt(DTLS_HANDSHAKE_RESULT_DELAY, TimeUnit.MILLISECONDS);
DtlsConnectorConfig.Builder dtlsConfigBuilder = DtlsConnectorConfig.builder(dtlsConfig);
if (cliConfig.clientAuth != null) {
dtlsConfigBuilder.set(DtlsConfig.DTLS_CLIENT_AUTHENTICATION_MODE, cliConfig.clientAuth);
}
String tag = "dtls:" + StringUtil.toString(bindToAddress);
dtlsConfigBuilder.setLoggingTag(tag);
AsyncAdvancedPskStore asyncPskStore = new AsyncAdvancedPskStore(new PlugPskStore());
asyncPskStore.setDelay(handshakeResultDelayMillis);
dtlsConfigBuilder.setAdvancedPskStore(asyncPskStore);
dtlsConfigBuilder.setAddress(bindToAddress);
X509KeyManager keyManager = SslContextUtil.getX509KeyManager(serverCredentials);
AsyncKeyManagerCertificateProvider certificateProvider = new AsyncKeyManagerCertificateProvider(keyManager, CertificateType.RAW_PUBLIC_KEY, CertificateType.X_509);
certificateProvider.setDelay(handshakeResultDelayMillis);
dtlsConfigBuilder.setCertificateIdentityProvider(certificateProvider);
AsyncNewAdvancedCertificateVerifier.Builder verifierBuilder = AsyncNewAdvancedCertificateVerifier.builder();
if (cliConfig.trustall) {
verifierBuilder.setTrustAllCertificates();
} else {
verifierBuilder.setTrustedCertificates(trustedCertificates);
}
verifierBuilder.setTrustAllRPKs();
AsyncNewAdvancedCertificateVerifier verifier = verifierBuilder.build();
verifier.setDelay(handshakeResultDelayMillis);
dtlsConfigBuilder.setAdvancedCertificateVerifier(verifier);
AsyncResumptionVerifier resumptionVerifier = new AsyncResumptionVerifier();
resumptionVerifier.setDelay(handshakeResultDelayMillis);
dtlsConfigBuilder.setResumptionVerifier(resumptionVerifier);
dtlsConfigBuilder.setConnectionListener(new MdcConnectionListener());
if (dtlsConfig.get(SystemConfig.HEALTH_STATUS_INTERVAL, TimeUnit.MILLISECONDS) > 0) {
DtlsHealthLogger health = new DtlsHealthLogger(tag);
dtlsConfigBuilder.setHealthHandler(health);
add(health);
// reset to prevent active logger
dtlsConfigBuilder.set(SystemConfig.HEALTH_STATUS_INTERVAL, 0, TimeUnit.MILLISECONDS);
}
DTLSConnector connector = new DTLSConnector(dtlsConfigBuilder.build());
CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
builder.setConnector(connector);
if (MatcherMode.PRINCIPAL == dtlsConfig.get(CoapConfig.RESPONSE_MATCHING)) {
builder.setEndpointContextMatcher(new PrincipalEndpointContextMatcher(true));
}
builder.setConfiguration(dtlsConfig);
CoapEndpoint endpoint = builder.build();
addEndpoint(endpoint);
print(endpoint, interfaceType);
}
if (protocols.contains(Protocol.TLS) && serverSslContext != null) {
Configuration tlsConfig = getConfig(Protocol.TLS, interfaceType);
if (cliConfig.clientAuth != null) {
tlsConfig.set(TcpConfig.TLS_CLIENT_AUTHENTICATION_MODE, cliConfig.clientAuth);
}
int maxPeers = tlsConfig.get(CoapConfig.MAX_ACTIVE_PEERS);
int sessionTimeout = tlsConfig.getTimeAsInt(TcpConfig.TLS_SESSION_TIMEOUT, TimeUnit.SECONDS);
SSLSessionContext serverSessionContext = serverSslContext.getServerSessionContext();
if (serverSessionContext != null) {
serverSessionContext.setSessionTimeout(sessionTimeout);
serverSessionContext.setSessionCacheSize(maxPeers);
}
TlsServerConnector connector = new TlsServerConnector(serverSslContext, bindToAddress, tlsConfig);
CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
builder.setConnector(connector);
builder.setConfiguration(tlsConfig);
CoapEndpoint endpoint = builder.build();
addEndpoint(endpoint);
print(endpoint, interfaceType);
}
}
}
}
use of org.eclipse.californium.scandium.dtls.pskstore.AsyncAdvancedPskStore in project californium by eclipse.
the class DTLSConnectorHandshakeTest method builderSetups.
/**
* @return List of DTLS Configuration Builder setup.
*/
@Parameters(name = "setup = server {0} / client {1}")
public static Iterable<BuilderSetup[]> builderSetups() {
List<BuilderSetup> fragmentModes = Arrays.asList(new BuilderSetup() {
@Override
public String toString() {
return "single-record";
}
@Override
public void setup(Builder builder) {
builder.set(DtlsConfig.DTLS_USE_MULTI_RECORD_MESSAGES, false);
}
}, new BuilderSetup() {
@Override
public String toString() {
return "multi-handshake-messages";
}
@Override
public void setup(Builder builder) {
builder.set(DtlsConfig.DTLS_USE_MULTI_HANDSHAKE_MESSAGE_RECORDS, true);
}
}, new BuilderSetup() {
@Override
public String toString() {
return "single-handshake-messages";
}
@Override
public void setup(Builder builder) {
builder.set(DtlsConfig.DTLS_USE_MULTI_HANDSHAKE_MESSAGE_RECORDS, false);
}
});
List<BuilderSetup> sizeModes = Arrays.asList(new BuilderSetup() {
@Override
public String toString() {
return "no record-size-limit";
}
@Override
public void setup(Builder builder) {
builder.set(DtlsConfig.DTLS_RECORD_SIZE_LIMIT, null);
}
}, new BuilderSetup() {
@Override
public String toString() {
return "record-size-limit";
}
@Override
public void setup(Builder builder) {
builder.set(DtlsConfig.DTLS_RECORD_SIZE_LIMIT, 270);
}
});
List<BuilderSetup> syncModes = Arrays.asList(new BuilderSetup() {
@Override
public String toString() {
return "sync";
}
@Override
public void setup(Builder builder) {
AdvancedPskStore pskStore = builder.getIncompleteConfig().getAdvancedPskStore();
if (pskStore instanceof AsyncAdvancedPskStore) {
((AsyncAdvancedPskStore) pskStore).setDelay(0);
}
NewAdvancedCertificateVerifier verifier = builder.getIncompleteConfig().getAdvancedCertificateVerifier();
if (verifier instanceof AsyncNewAdvancedCertificateVerifier) {
((AsyncNewAdvancedCertificateVerifier) verifier).setDelay(0);
}
CertificateProvider provider = builder.getIncompleteConfig().getCertificateIdentityProvider();
if (provider instanceof AsyncCertificateProvider) {
((AsyncCertificateProvider) provider).setDelay(0);
}
}
}, new BuilderSetup() {
@Override
public String toString() {
return "async";
}
@Override
public void setup(Builder builder) {
AdvancedPskStore pskStore = builder.getIncompleteConfig().getAdvancedPskStore();
if (pskStore instanceof AsyncAdvancedPskStore) {
((AsyncAdvancedPskStore) pskStore).setDelay(1);
}
NewAdvancedCertificateVerifier verifier = builder.getIncompleteConfig().getAdvancedCertificateVerifier();
if (verifier instanceof AsyncNewAdvancedCertificateVerifier) {
((AsyncNewAdvancedCertificateVerifier) verifier).setDelay(1);
}
CertificateProvider provider = builder.getIncompleteConfig().getCertificateIdentityProvider();
if (provider instanceof AsyncCertificateProvider) {
((AsyncCertificateProvider) provider).setDelay(1);
}
}
});
List<BuilderSetup[]> combinations = new ArrayList<>();
if (TestScope.enableIntensiveTests()) {
BuilderSetup[] serverSetups = expand(fragmentModes);
BuilderSetup[] clientSetups = expand(fragmentModes, sizeModes);
for (BuilderSetup server : serverSetups) {
for (BuilderSetup client : clientSetups) {
combinations.add(new BuilderSetup[] { server, client });
}
}
for (BuilderSetup setup : syncModes) {
combinations.add(new BuilderSetup[] { setup, setup });
}
} else {
BuilderSetups server = new BuilderSetups();
server.add(fragmentModes.get(2));
server.add(sizeModes.get(1));
server.add(syncModes.get(1));
BuilderSetups client = new BuilderSetups();
client.add(fragmentModes.get(1));
client.add(sizeModes.get(1));
client.add(syncModes.get(0));
combinations.add(new BuilderSetup[] { server, client });
server = new BuilderSetups();
server.add(syncModes.get(0));
client = new BuilderSetups();
client.add(fragmentModes.get(0));
client.add(sizeModes.get(0));
client.add(syncModes.get(1));
combinations.add(new BuilderSetup[] { server, client });
}
return combinations;
}
Aggregations