Search in sources :

Example 1 with X509LwM2MBootstrapServerCredential

use of org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.X509LwM2MBootstrapServerCredential in project thingsboard by thingsboard.

the class DeviceProfileDataValidator method validateLwm2mServersCredentialOfBootstrapForClient.

private void validateLwm2mServersCredentialOfBootstrapForClient(LwM2MBootstrapServerCredential bootstrapServerConfig) {
    String server;
    switch(bootstrapServerConfig.getSecurityMode()) {
        case NO_SEC:
        case PSK:
            break;
        case RPK:
            RPKLwM2MBootstrapServerCredential rpkServerCredentials = (RPKLwM2MBootstrapServerCredential) bootstrapServerConfig;
            server = rpkServerCredentials.isBootstrapServerIs() ? "Bootstrap Server" : "LwM2M Server";
            if (StringUtils.isEmpty(rpkServerCredentials.getServerPublicKey())) {
                throw new DeviceCredentialsValidationException(server + " RPK public key must be specified!");
            }
            try {
                String pubkRpkSever = EncryptionUtil.pubkTrimNewLines(rpkServerCredentials.getServerPublicKey());
                rpkServerCredentials.setServerPublicKey(pubkRpkSever);
                SecurityUtil.publicKey.decode(rpkServerCredentials.getDecodedCServerPublicKey());
            } catch (Exception e) {
                throw new DeviceCredentialsValidationException(server + " RPK public key must be in standard [RFC7250] and then encoded to Base64 format!");
            }
            break;
        case X509:
            X509LwM2MBootstrapServerCredential x509ServerCredentials = (X509LwM2MBootstrapServerCredential) bootstrapServerConfig;
            server = x509ServerCredentials.isBootstrapServerIs() ? "Bootstrap Server" : "LwM2M Server";
            if (StringUtils.isEmpty(x509ServerCredentials.getServerPublicKey())) {
                throw new DeviceCredentialsValidationException(server + " X509 certificate must be specified!");
            }
            try {
                String certServer = EncryptionUtil.certTrimNewLines(x509ServerCredentials.getServerPublicKey());
                x509ServerCredentials.setServerPublicKey(certServer);
                SecurityUtil.certificate.decode(x509ServerCredentials.getDecodedCServerPublicKey());
            } catch (Exception e) {
                throw new DeviceCredentialsValidationException(server + " X509 certificate must be in DER-encoded X509v3 format and support only EC algorithm and then encoded to Base64 format!");
            }
            break;
    }
}
Also used : X509LwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.X509LwM2MBootstrapServerCredential) RPKLwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.RPKLwM2MBootstrapServerCredential) DeviceCredentialsValidationException(org.thingsboard.server.dao.exception.DeviceCredentialsValidationException) DeviceCredentialsValidationException(org.thingsboard.server.dao.exception.DeviceCredentialsValidationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException)

Example 2 with X509LwM2MBootstrapServerCredential

use of org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.X509LwM2MBootstrapServerCredential in project thingsboard by thingsboard.

the class AbstractSecurityLwM2MIntegrationTest method getBootstrapServerCredential.

private AbstractLwM2MBootstrapServerCredential getBootstrapServerCredential(LwM2MSecurityMode mode, boolean isBootstrap) {
    AbstractLwM2MBootstrapServerCredential bootstrapServerCredential;
    switch(mode) {
        case PSK:
            bootstrapServerCredential = new PSKLwM2MBootstrapServerCredential();
            bootstrapServerCredential.setServerPublicKey("");
            break;
        case RPK:
            bootstrapServerCredential = new RPKLwM2MBootstrapServerCredential();
            if (isBootstrap) {
                bootstrapServerCredential.setServerPublicKey(Base64.encodeBase64String(serverPublicKeyFromCertBs.getEncoded()));
            } else {
                bootstrapServerCredential.setServerPublicKey(Base64.encodeBase64String(serverPublicKeyFromCert.getEncoded()));
            }
            break;
        case X509:
            bootstrapServerCredential = new X509LwM2MBootstrapServerCredential();
            try {
                if (isBootstrap) {
                    bootstrapServerCredential.setServerPublicKey(Base64.encodeBase64String(serverX509CertBs.getEncoded()));
                } else {
                    bootstrapServerCredential.setServerPublicKey(Base64.encodeBase64String(serverX509Cert.getEncoded()));
                }
            } catch (CertificateEncodingException e) {
                e.printStackTrace();
            }
            break;
        default:
            throw new IllegalStateException("Unexpected value: " + mode);
    }
    bootstrapServerCredential.setShortServerId(isBootstrap ? shortServerIdBs : shortServerId);
    bootstrapServerCredential.setBootstrapServerIs(isBootstrap);
    bootstrapServerCredential.setHost(isBootstrap ? hostBs : host);
    bootstrapServerCredential.setPort(isBootstrap ? securityPortBs : securityPort);
    return bootstrapServerCredential;
}
Also used : PSKLwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.PSKLwM2MBootstrapServerCredential) X509LwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.X509LwM2MBootstrapServerCredential) AbstractLwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.AbstractLwM2MBootstrapServerCredential) RPKLwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.RPKLwM2MBootstrapServerCredential) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Aggregations

RPKLwM2MBootstrapServerCredential (org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.RPKLwM2MBootstrapServerCredential)2 X509LwM2MBootstrapServerCredential (org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.X509LwM2MBootstrapServerCredential)2 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 AbstractLwM2MBootstrapServerCredential (org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.AbstractLwM2MBootstrapServerCredential)1 PSKLwM2MBootstrapServerCredential (org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.PSKLwM2MBootstrapServerCredential)1 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)1 DeviceCredentialsValidationException (org.thingsboard.server.dao.exception.DeviceCredentialsValidationException)1