use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.
the class RedisSecurityStore method remove.
@Override
public SecurityInfo remove(String endpoint) {
try (Jedis j = pool.getResource()) {
byte[] data = j.get((SEC_EP + endpoint).getBytes());
if (data != null) {
SecurityInfo info = deserialize(data);
if (info.getIdentity() != null) {
j.hdel(PSKID_SEC.getBytes(), info.getIdentity().getBytes());
}
j.del((SEC_EP + endpoint).getBytes());
return info;
}
}
return null;
}
use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.
the class LeshanBootstrapServerBuilderTest method create_server_with_securityStore_and_disable_secured_endpoint.
@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 List<SecurityInfo> getAllByEndpoint(String endpoint) {
return null;
}
});
builder.disableSecuredEndpoint();
server = builder.build();
assertNull(server.getSecuredAddress());
assertNotNull(server.getUnsecuredAddress());
}
use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.
the class LeshanBootstrapServerBuilderTest method create_server_with_securityStore_and_disable_unsecured_endpoint.
@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 List<SecurityInfo> getAllByEndpoint(String endpoint) {
return null;
}
});
builder.disableUnsecuredEndpoint();
server = builder.build();
assertNotNull(server.getSecuredAddress());
assertNull(server.getUnsecuredAddress());
}
use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.
the class SecurityDeserializer method deserialize.
@Override
public SecurityInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
}
SecurityInfo info = null;
if (json.isJsonObject()) {
JsonObject object = (JsonObject) json;
String endpoint;
if (object.has("endpoint")) {
endpoint = object.get("endpoint").getAsString();
} else {
throw new JsonParseException("Missing endpoint");
}
JsonObject psk = (JsonObject) object.get("psk");
JsonObject rpk = (JsonObject) object.get("rpk");
JsonPrimitive x509 = object.getAsJsonPrimitive("x509");
if (psk != null) {
// PSK Deserialization
String identity;
if (psk.has("identity")) {
identity = psk.get("identity").getAsString();
} else {
throw new JsonParseException("Missing PSK identity");
}
byte[] key;
try {
key = Hex.decodeHex(psk.get("key").getAsString().toCharArray());
} catch (IllegalArgumentException e) {
throw new JsonParseException("key parameter must be a valid hex string", e);
}
info = SecurityInfo.newPreSharedKeyInfo(endpoint, identity, key);
} else if (rpk != null) {
PublicKey key;
try {
byte[] x = Hex.decodeHex(rpk.get("x").getAsString().toCharArray());
byte[] y = Hex.decodeHex(rpk.get("y").getAsString().toCharArray());
String params = rpk.get("params").getAsString();
AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
algoParameters.init(new ECGenParameterSpec(params));
ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);
KeySpec keySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(x), new BigInteger(y)), parameterSpec);
key = KeyFactory.getInstance("EC").generatePublic(keySpec);
} catch (IllegalArgumentException | InvalidKeySpecException | NoSuchAlgorithmException | InvalidParameterSpecException e) {
throw new JsonParseException("Invalid security info content", e);
}
info = SecurityInfo.newRawPublicKeyInfo(endpoint, key);
} else if (x509 != null && x509.getAsBoolean()) {
info = SecurityInfo.newX509CertInfo(endpoint);
} else {
throw new JsonParseException("Invalid security info content");
}
}
return info;
}
Aggregations