Search in sources :

Example 1 with SecurityInfo

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;
}
Also used : PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) KeySpec(java.security.spec.KeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) JsonObject(com.eclipsesource.json.JsonObject) BigInteger(java.math.BigInteger) ECPoint(java.security.spec.ECPoint) SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 2 with SecurityInfo

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));
}
Also used : ECParameterSpec(java.security.spec.ECParameterSpec) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) KeySpec(java.security.spec.KeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) BigInteger(java.math.BigInteger) ECPoint(java.security.spec.ECPoint) SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) AlgorithmParameters(java.security.AlgorithmParameters) Test(org.junit.Test)

Example 3 with SecurityInfo

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));
}
Also used : SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo) Test(org.junit.Test)

Example 4 with SecurityInfo

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);
    }
}
Also used : NonUniqueSecurityInfoException(org.eclipse.leshan.server.security.NonUniqueSecurityInfoException) InputStreamReader(java.io.InputStreamReader) JsonParseException(com.google.gson.JsonParseException) SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo)

Example 5 with SecurityInfo

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();
    }
}
Also used : NonUniqueSecurityInfoException(org.eclipse.leshan.server.security.NonUniqueSecurityInfoException) SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo)

Aggregations

SecurityInfo (org.eclipse.leshan.server.security.SecurityInfo)14 Test (org.junit.Test)5 BigInteger (java.math.BigInteger)3 AlgorithmParameters (java.security.AlgorithmParameters)3 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)3 ECParameterSpec (java.security.spec.ECParameterSpec)3 ECPoint (java.security.spec.ECPoint)3 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)3 KeySpec (java.security.spec.KeySpec)3 List (java.util.List)3 BootstrapSecurityStore (org.eclipse.leshan.server.security.BootstrapSecurityStore)3 NonUniqueSecurityInfoException (org.eclipse.leshan.server.security.NonUniqueSecurityInfoException)3 Jedis (redis.clients.jedis.Jedis)3 JsonParseException (com.google.gson.JsonParseException)2 PublicKey (java.security.PublicKey)2 JsonObject (com.eclipsesource.json.JsonObject)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1