Search in sources :

Example 11 with TbLwM2MSecurityInfo

use of org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo in project thingsboard by thingsboard.

the class TbLwM2mRedisSecurityStore method remove.

@Override
public void remove(String endpoint) {
    Lock lock = null;
    try (var connection = connectionFactory.getConnection()) {
        lock = redisLock.obtain(endpoint);
        lock.lock();
        byte[] data = connection.get((SEC_EP + endpoint).getBytes());
        if (data != null && data.length > 0) {
            SecurityInfo info = ((TbLwM2MSecurityInfo) serializer.asObject(data)).getSecurityInfo();
            if (info != null && info.getIdentity() != null) {
                connection.hDel(PSKID_SEC.getBytes(), info.getIdentity().getBytes());
            }
            connection.del((SEC_EP + endpoint).getBytes());
        }
    } finally {
        if (lock != null) {
            lock.unlock();
        }
    }
}
Also used : TbLwM2MSecurityInfo(org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo) SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo) TbLwM2MSecurityInfo(org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo) Lock(java.util.concurrent.locks.Lock)

Example 12 with TbLwM2MSecurityInfo

use of org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo in project thingsboard by thingsboard.

the class TbLwM2mRedisSecurityStore method put.

@Override
public void put(TbLwM2MSecurityInfo tbSecurityInfo) throws NonUniqueSecurityInfoException {
    SecurityInfo info = tbSecurityInfo.getSecurityInfo();
    byte[] tbSecurityInfoSerialized = serializer.asByteArray(tbSecurityInfo);
    Lock lock = null;
    try (var connection = connectionFactory.getConnection()) {
        lock = redisLock.obtain(tbSecurityInfo.getEndpoint());
        lock.lock();
        if (info != null && info.getIdentity() != null) {
            byte[] oldEndpointBytes = connection.hGet(PSKID_SEC.getBytes(), info.getIdentity().getBytes());
            if (oldEndpointBytes != null) {
                String oldEndpoint = new String(oldEndpointBytes);
                if (!oldEndpoint.equals(info.getEndpoint())) {
                    throw new NonUniqueSecurityInfoException("PSK Identity " + info.getIdentity() + " is already used");
                }
                connection.hSet(PSKID_SEC.getBytes(), info.getIdentity().getBytes(), info.getEndpoint().getBytes());
            }
        }
        byte[] previousData = connection.getSet((SEC_EP + tbSecurityInfo.getEndpoint()).getBytes(), tbSecurityInfoSerialized);
        if (previousData != null && info != null) {
            String previousIdentity = ((TbLwM2MSecurityInfo) serializer.asObject(previousData)).getSecurityInfo().getIdentity();
            if (previousIdentity != null && !previousIdentity.equals(info.getIdentity())) {
                connection.hDel(PSKID_SEC.getBytes(), previousIdentity.getBytes());
            }
        }
    } finally {
        if (lock != null) {
            lock.unlock();
        }
    }
}
Also used : NonUniqueSecurityInfoException(org.eclipse.leshan.server.security.NonUniqueSecurityInfoException) SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo) TbLwM2MSecurityInfo(org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo) Lock(java.util.concurrent.locks.Lock)

Example 13 with TbLwM2MSecurityInfo

use of org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo in project thingsboard by thingsboard.

the class TbLwM2mRedisSecurityStore method getByIdentity.

@Override
public SecurityInfo getByIdentity(String identity) {
    Lock lock = null;
    try (var connection = connectionFactory.getConnection()) {
        lock = redisLock.obtain(toLockKey(identity));
        lock.lock();
        byte[] ep = connection.hGet(PSKID_SEC.getBytes(), identity.getBytes());
        if (ep == null) {
            return null;
        } else {
            byte[] data = connection.get((SEC_EP + new String(ep)).getBytes());
            if (data == null || data.length == 0) {
                return null;
            } else {
                return ((TbLwM2MSecurityInfo) serializer.asObject(data)).getSecurityInfo();
            }
        }
    } finally {
        if (lock != null) {
            lock.unlock();
        }
    }
}
Also used : TbLwM2MSecurityInfo(org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo) Lock(java.util.concurrent.locks.Lock)

Aggregations

TbLwM2MSecurityInfo (org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo)13 Lock (java.util.concurrent.locks.Lock)5 SecurityInfo (org.eclipse.leshan.server.security.SecurityInfo)4 LwM2MAuthException (org.thingsboard.server.transport.lwm2m.server.client.LwM2MAuthException)3 BootstrapConfig (org.eclipse.leshan.server.bootstrap.BootstrapConfig)2 InvalidConfigurationException (org.eclipse.leshan.server.bootstrap.InvalidConfigurationException)2 NonUniqueSecurityInfoException (org.eclipse.leshan.server.security.NonUniqueSecurityInfoException)2 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)2 LwM2MBootstrapConfig (org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MBootstrapConfig)2 PublicKey (java.security.PublicKey)1 CertPath (java.security.cert.CertPath)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 CertificateExpiredException (java.security.cert.CertificateExpiredException)1 CertificateNotYetValidException (java.security.cert.CertificateNotYetValidException)1 X509Certificate (java.security.cert.X509Certificate)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1