Search in sources :

Example 11 with SecurityInfo

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

Example 12 with SecurityInfo

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

Example 13 with SecurityInfo

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

Example 14 with SecurityInfo

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

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