Search in sources :

Example 6 with SecurityInfo

use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.

the class LeshanBootstrapServerBuilderTest method create_server_with_securityStore.

@Test
public void create_server_with_securityStore() {
    builder.setSecurityStore(new BootstrapSecurityStore() {

        @Override
        public SecurityInfo getByIdentity(String pskIdentity) {
            return null;
        }

        @Override
        public List<SecurityInfo> getAllByEndpoint(String endpoint) {
            return null;
        }
    });
    server = builder.build();
    assertNotNull(server.getSecuredAddress());
    assertNotNull(server.getUnsecuredAddress());
    assertNotNull(server.getBootstrapSecurityStore());
}
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 7 with SecurityInfo

use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.

the class FileSecurityStore method add.

@Override
public SecurityInfo add(SecurityInfo info) throws NonUniqueSecurityInfoException {
    writeLock.lock();
    try {
        SecurityInfo previous = addToStore(info);
        saveToFile();
        return previous;
    } finally {
        writeLock.unlock();
    }
}
Also used : SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo)

Example 8 with SecurityInfo

use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.

the class FileSecurityStore method saveToFile.

protected void saveToFile() {
    try {
        File file = new File(filename);
        if (!file.exists()) {
            File parent = file.getParentFile();
            if (parent != null) {
                parent.mkdirs();
            }
            file.createNewFile();
        }
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename))) {
            out.writeObject(this.getAll().toArray(new SecurityInfo[0]));
        }
    } catch (IOException e) {
        LOG.error("Could not save security infos to file", e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo)

Example 9 with SecurityInfo

use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.

the class RedisSecurityStore method getAll.

@Override
public Collection<SecurityInfo> getAll() {
    try (Jedis j = pool.getResource()) {
        ScanParams params = new ScanParams().match(SEC_EP + "*").count(100);
        Collection<SecurityInfo> list = new LinkedList<>();
        String cursor = "0";
        do {
            ScanResult<byte[]> res = j.scan(cursor.getBytes(), params);
            for (byte[] key : res.getResult()) {
                byte[] element = j.get(key);
                list.add(deserialize(element));
            }
            cursor = res.getStringCursor();
        } while (!"0".equals(cursor));
        return list;
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) ScanParams(redis.clients.jedis.ScanParams) SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo) LinkedList(java.util.LinkedList)

Example 10 with SecurityInfo

use of org.eclipse.leshan.server.security.SecurityInfo in project leshan by eclipse.

the class RedisSecurityStore method add.

@Override
public SecurityInfo add(SecurityInfo info) throws NonUniqueSecurityInfoException {
    byte[] data = serialize(info);
    try (Jedis j = pool.getResource()) {
        if (info.getIdentity() != null) {
            // populate the secondary index (security info by PSK id)
            String oldEndpoint = j.hget(PSKID_SEC, info.getIdentity());
            if (oldEndpoint != null && !oldEndpoint.equals(info.getEndpoint())) {
                throw new NonUniqueSecurityInfoException("PSK Identity " + info.getIdentity() + " is already used");
            }
            j.hset(PSKID_SEC.getBytes(), info.getIdentity().getBytes(), info.getEndpoint().getBytes());
        }
        byte[] previousData = j.getSet((SEC_EP + info.getEndpoint()).getBytes(), data);
        SecurityInfo previous = previousData == null ? null : deserialize(previousData);
        String previousIdentity = previous == null ? null : previous.getIdentity();
        if (previousIdentity != null && !previousIdentity.equals(info.getIdentity())) {
            j.hdel(PSKID_SEC, previousIdentity);
        }
        return previous;
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) 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