use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.
the class SecurityInfoSerDes method deserialize.
public static SecurityInfo deserialize(byte[] data) {
JsonObject o = (JsonObject) Json.parse(new String(data));
SecurityInfo i;
String ep = o.getString("ep", null);
if (o.get("psk") != null) {
i = SecurityInfo.newPreSharedKeyInfo(ep, o.getString("id", null), Hex.decodeHex(o.getString("psk", null).toCharArray()));
} else if (o.get("x509") != null) {
i = SecurityInfo.newX509CertInfo(ep);
} else {
JsonObject rpk = (JsonObject) o.get("rpk");
PublicKey key;
try {
byte[] x = Hex.decodeHex(rpk.getString("x", null).toCharArray());
byte[] y = Hex.decodeHex(rpk.getString("y", null).toCharArray());
String params = rpk.getString("params", null);
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 IllegalStateException("Invalid security info content", e);
}
i = SecurityInfo.newRawPublicKeyInfo(ep, key);
}
return i;
}
use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.
the class SecurityInfoSerDesTest method security_info_rpk_ser_des_then_equal.
@Test
public void security_info_rpk_ser_des_then_equal() throws Exception {
byte[] publicX = Hex.decodeHex("89c048261979208666f2bfb188be1968fc9021c416ce12828c06f4e314c167b5".toCharArray());
byte[] publicY = Hex.decodeHex("cbf1eb7587f08e01688d9ada4be859137ca49f79394bad9179326b3090967b68".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(publicX), new BigInteger(publicY)), parameterSpec);
SecurityInfo si = SecurityInfo.newRawPublicKeyInfo("myendpoint", KeyFactory.getInstance("EC").generatePublic(publicKeySpec));
byte[] data = SecurityInfoSerDes.serialize(si);
assertEquals("{\"ep\":\"myendpoint\",\"rpk\":{\"x\":\"89c048261979208666f2bfb188be1968fc9021c416ce12828c06f4e314c167b5\",\"y\":\"cbf1eb7587f08e01688d9ada4be859137ca49f79394bad9179326b3090967b68\",\"params\":\"secp256r1\"}}", new String(data));
System.err.println(new String(SecurityInfoSerDes.serialize(SecurityInfoSerDes.deserialize(data))));
assertEquals(si, SecurityInfoSerDes.deserialize(data));
}
use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.
the class SecurityInfoSerDesTest method security_info_psk_ser_des_then_equal.
@Test
public void security_info_psk_ser_des_then_equal() {
SecurityInfo si = SecurityInfo.newPreSharedKeyInfo("myendPoint", "pskIdentity", Hex.decodeHex("deadbeef".toCharArray()));
byte[] data = SecurityInfoSerDes.serialize(si);
assertEquals("{\"ep\":\"myendPoint\",\"id\":\"pskIdentity\",\"psk\":\"deadbeef\"}", new String(data));
assertEquals(si, SecurityInfoSerDes.deserialize(data));
}
use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.
the class SecurityServlet method doPut.
/**
* {@inheritDoc}
*/
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String[] path = StringUtils.split(req.getPathInfo(), '/');
if (path.length != 1 && "clients".equals(path[0])) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
try {
SecurityInfo info = gsonDes.fromJson(new InputStreamReader(req.getInputStream()), SecurityInfo.class);
LOG.debug("New security info for end-point {}: {}", info.getEndpoint(), info);
store.add(info);
resp.setStatus(HttpServletResponse.SC_OK);
} catch (NonUniqueSecurityInfoException e) {
LOG.warn("Non unique security info: " + e.getMessage());
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().append(e.getMessage()).flush();
} catch (JsonParseException e) {
LOG.warn("Could not parse request body", e);
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().append("Invalid request body").flush();
} catch (RuntimeException e) {
LOG.warn("unexpected error for request " + req.getPathInfo(), e);
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.
the class InMemorySecurityStore method add.
@Override
public SecurityInfo add(SecurityInfo info) throws NonUniqueSecurityInfoException {
writeLock.lock();
try {
String identity = info.getIdentity();
if (identity != null) {
SecurityInfo infoByIdentity = securityByIdentity.get(info.getIdentity());
if (infoByIdentity != null && !info.getEndpoint().equals(infoByIdentity.getEndpoint())) {
throw new NonUniqueSecurityInfoException("PSK Identity " + info.getIdentity() + " is already used");
}
securityByIdentity.put(info.getIdentity(), info);
}
SecurityInfo previous = securityByEp.put(info.getEndpoint(), info);
String previousIdentity = previous == null ? null : previous.getIdentity();
if (previousIdentity != null && !previousIdentity.equals(identity)) {
securityByIdentity.remove(previousIdentity);
}
return previous;
} finally {
writeLock.unlock();
}
}
Aggregations