Search in sources :

Example 1 with Password

use of org.eclipse.kura.configuration.Password in project kura by eclipse.

the class NetworkConfiguration method addWifiConfigIP4Properties.

private static void addWifiConfigIP4Properties(WifiConfig wifiConfig, String netIfConfigPrefix, Map<String, Object> properties) {
    WifiMode mode = wifiConfig.getMode();
    if (mode == null) {
        s_logger.trace("WifiMode is null - could not add wifiConfig: {}", wifiConfig);
        return;
    }
    StringBuilder prefix = new StringBuilder(netIfConfigPrefix).append("wifi.").append(mode.toString().toLowerCase());
    int[] channels = wifiConfig.getChannels();
    StringBuffer sbChannel = new StringBuffer();
    if (channels != null) {
        for (int i = 0; i < channels.length; i++) {
            sbChannel.append(channels[i]);
            if (i < channels.length - 1) {
                sbChannel.append(' ');
            }
        }
    }
    properties.put(prefix + ".ssid", wifiConfig.getSSID());
    properties.put(prefix + ".driver", wifiConfig.getDriver());
    if (wifiConfig.getMode() != null) {
        properties.put(prefix + ".mode", wifiConfig.getMode().toString());
    } else {
        properties.put(prefix + ".mode", WifiMode.UNKNOWN.toString());
    }
    if (wifiConfig.getSecurity() != null) {
        properties.put(prefix + ".securityType", wifiConfig.getSecurity().toString());
    } else {
        properties.put(prefix + ".securityType", WifiSecurity.NONE.toString());
    }
    properties.put(prefix + ".channel", sbChannel.toString());
    Password psswd = wifiConfig.getPasskey();
    if (wifiConfig != null && psswd != null) {
        properties.put(prefix + ".passphrase", psswd);
    } else {
        properties.put(prefix + ".passphrase", new Password(""));
    }
    if (wifiConfig != null && wifiConfig.getHardwareMode() != null) {
        properties.put(prefix + ".hardwareMode", wifiConfig.getHardwareMode());
    } else {
        properties.put(prefix + ".hardwareMode", "");
    }
    properties.put(prefix + ".broadcast", Boolean.valueOf(wifiConfig.getBroadcast()));
    if (wifiConfig.getRadioMode() != null) {
        properties.put(prefix + ".radioMode", wifiConfig.getRadioMode().toString());
    }
    if (wifiConfig.getBgscan() != null) {
        properties.put(prefix + ".bgscan", wifiConfig.getBgscan().toString());
    } else {
        properties.put(prefix + ".bgscan", "");
    }
    if (wifiConfig.getPairwiseCiphers() != null) {
        properties.put(prefix + ".pairwiseCiphers", wifiConfig.getPairwiseCiphers().name());
    }
    if (wifiConfig.getGroupCiphers() != null) {
        properties.put(prefix + ".groupCiphers", wifiConfig.getGroupCiphers().name());
    }
    properties.put(prefix + ".pingAccessPoint", wifiConfig.pingAccessPoint());
    properties.put(prefix + ".ignoreSSID", wifiConfig.ignoreSSID());
/*
         * Iterator<Entry<String, Object>> it = properties.entrySet().iterator();
         * while(it.hasNext()) {
         * Entry<String, Object> entry = it.next();
         * System.out.println(entry.getKey() + " = " + entry.getValue());
         * }
         */
}
Also used : WifiMode(org.eclipse.kura.net.wifi.WifiMode) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) Password(org.eclipse.kura.configuration.Password)

Example 2 with Password

use of org.eclipse.kura.configuration.Password in project kura by eclipse.

the class CollectionsUtil method dictionaryToMap.

public static Map<String, Object> dictionaryToMap(Dictionary<String, Object> dictionary, OCD ocd) {
    if (dictionary == null) {
        return null;
    }
    Map<String, AD> ads = new HashMap<String, AD>();
    if (ocd != null) {
        for (AD ad : ocd.getAD()) {
            ads.put(ad.getId(), ad);
        }
    }
    Map<String, Object> map = new HashMap<String, Object>();
    Enumeration<String> keys = dictionary.keys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        Object value = dictionary.get(key);
        AD ad = ads.get(key);
        if (ad != null && ad.getType() != null && Scalar.PASSWORD.equals(ad.getType())) {
            if (value instanceof char[]) {
                map.put(key, new Password((char[]) value));
            } else {
                map.put(key, new Password(value.toString()));
            }
        } else {
            map.put(key, value);
        }
    }
    return map;
}
Also used : AD(org.eclipse.kura.configuration.metatype.AD) HashMap(java.util.HashMap) Password(org.eclipse.kura.configuration.Password)

Example 3 with Password

use of org.eclipse.kura.configuration.Password in project kura by eclipse.

the class ComponentUtil method getObjectValue.

private static Object[] getObjectValue(Scalar type, String[] defaultValues, ComponentContext ctx) {
    List<Object> values = new ArrayList<Object>();
    switch(type) {
        case BOOLEAN:
            for (String value : defaultValues) {
                values.add(Boolean.valueOf(value));
            }
            return values.toArray(new Boolean[] {});
        case BYTE:
            for (String value : defaultValues) {
                values.add(Byte.valueOf(value));
            }
            return values.toArray(new Byte[] {});
        case CHAR:
            for (String value : defaultValues) {
                values.add(Character.valueOf(value.charAt(0)));
            }
            return values.toArray(new Character[] {});
        case DOUBLE:
            for (String value : defaultValues) {
                values.add(Double.valueOf(value));
            }
            return values.toArray(new Double[] {});
        case FLOAT:
            for (String value : defaultValues) {
                values.add(Float.valueOf(value));
            }
            return values.toArray(new Float[] {});
        case INTEGER:
            for (String value : defaultValues) {
                values.add(Integer.valueOf(value));
            }
            return values.toArray(new Integer[] {});
        case LONG:
            for (String value : defaultValues) {
                values.add(Long.valueOf(value));
            }
            return values.toArray(new Long[] {});
        case SHORT:
            for (String value : defaultValues) {
                values.add(Short.valueOf(value));
            }
            return values.toArray(new Short[] {});
        case PASSWORD:
            ServiceReference<CryptoService> sr = ctx.getBundleContext().getServiceReference(CryptoService.class);
            CryptoService cryptoService = ctx.getBundleContext().getService(sr);
            for (String value : defaultValues) {
                try {
                    values.add(new Password(cryptoService.encryptAes(value.toCharArray())));
                } catch (Exception e) {
                    values.add(new Password(value));
                }
            }
            return values.toArray(new Password[] {});
        case STRING:
            return defaultValues;
    }
    return null;
}
Also used : CryptoService(org.eclipse.kura.crypto.CryptoService) ArrayList(java.util.ArrayList) KuraException(org.eclipse.kura.KuraException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) Password(org.eclipse.kura.configuration.Password)

Example 4 with Password

use of org.eclipse.kura.configuration.Password in project kura by eclipse.

the class XmlConfigPropertiesAdapter method unmarshal.

public Map<String, Object> unmarshal(XmlConfigPropertiesAdapted adaptedPropsAdapted) throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    XmlConfigPropertyAdapted[] adaptedProps = adaptedPropsAdapted.getProperties();
    if (adaptedProps == null) {
        return properties;
    }
    for (XmlConfigPropertyAdapted adaptedProp : adaptedProps) {
        String propName = adaptedProp.getName();
        ConfigPropertyType type = adaptedProp.getType();
        if (type != null) {
            Object propvalue = null;
            if (adaptedProp.getArray() == false) {
                switch(adaptedProp.getType()) {
                    case STRING_TYPE:
                        propvalue = adaptedProp.getValues()[0];
                        break;
                    case LONG_TYPE:
                        propvalue = Long.parseLong(adaptedProp.getValues()[0]);
                        break;
                    case DOUBLE_TYPE:
                        propvalue = Double.parseDouble(adaptedProp.getValues()[0]);
                        break;
                    case FLOAT_TYPE:
                        propvalue = Float.parseFloat(adaptedProp.getValues()[0]);
                        break;
                    case INTEGER_TYPE:
                        propvalue = Integer.parseInt(adaptedProp.getValues()[0]);
                        break;
                    case BYTE_TYPE:
                        propvalue = Byte.parseByte(adaptedProp.getValues()[0]);
                        break;
                    case CHAR_TYPE:
                        String s = adaptedProp.getValues()[0];
                        propvalue = Character.valueOf(s.charAt(0));
                        break;
                    case BOOLEAN_TYPE:
                        propvalue = Boolean.parseBoolean(adaptedProp.getValues()[0]);
                        break;
                    case SHORT_TYPE:
                        propvalue = Short.parseShort(adaptedProp.getValues()[0]);
                        break;
                    case PASSWORD_TYPE:
                        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
                        ServiceReference<CryptoService> cryptoServiceRef = bundleContext.getServiceReference(CryptoService.class);
                        CryptoService cryptoService = bundleContext.getService(cryptoServiceRef);
                        propvalue = adaptedProp.getValues()[0];
                        if (adaptedProp.isEncrypted()) {
                            try {
                                propvalue = new Password(cryptoService.decryptAes(((String) propvalue).toCharArray()));
                            } catch (KuraException e) {
                                propvalue = new Password(cryptoService.decodeBase64((String) propvalue));
                            }
                        } else {
                            propvalue = new Password((String) propvalue);
                        }
                        break;
                }
            } else {
                // Starting from 1.2.0 an empty array will never be present in a snapshot.
                if (adaptedProp.getValues() == null) {
                    continue;
                }
                switch(adaptedProp.getType()) {
                    case STRING_TYPE:
                        propvalue = adaptedProp.getValues();
                        break;
                    case LONG_TYPE:
                        Long[] longValues = new Long[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                longValues[i] = Long.parseLong(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = longValues;
                        break;
                    case DOUBLE_TYPE:
                        Double[] doubleValues = new Double[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                doubleValues[i] = Double.parseDouble(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = doubleValues;
                        break;
                    case FLOAT_TYPE:
                        Float[] floatValues = new Float[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                floatValues[i] = Float.parseFloat(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = floatValues;
                        break;
                    case INTEGER_TYPE:
                        Integer[] intValues = new Integer[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                intValues[i] = Integer.parseInt(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = intValues;
                        break;
                    case BYTE_TYPE:
                        Byte[] byteValues = new Byte[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                byteValues[i] = Byte.parseByte(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = byteValues;
                        break;
                    case CHAR_TYPE:
                        Character[] charValues = new Character[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                String s = adaptedProp.getValues()[i];
                                charValues[i] = Character.valueOf(s.charAt(0));
                            }
                        }
                        propvalue = charValues;
                        break;
                    case BOOLEAN_TYPE:
                        Boolean[] booleanValues = new Boolean[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                booleanValues[i] = Boolean.parseBoolean(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = booleanValues;
                        break;
                    case SHORT_TYPE:
                        Short[] shortValues = new Short[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                shortValues[i] = Short.parseShort(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = shortValues;
                        break;
                    case PASSWORD_TYPE:
                        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
                        ServiceReference<CryptoService> cryptoServiceRef = bundleContext.getServiceReference(CryptoService.class);
                        CryptoService cryptoService = bundleContext.getService(cryptoServiceRef);
                        Password[] pwdValues = new Password[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                if (adaptedProp.isEncrypted()) {
                                    try {
                                        pwdValues[i] = new Password(cryptoService.decryptAes(adaptedProp.getValues()[i].toCharArray()));
                                    } catch (KuraException e) {
                                        pwdValues[i] = new Password(cryptoService.decodeBase64(adaptedProp.getValues()[i]));
                                    }
                                } else {
                                    pwdValues[i] = new Password(adaptedProp.getValues()[i]);
                                }
                            }
                        }
                        propvalue = pwdValues;
                        break;
                }
            }
            properties.put(propName, propvalue);
        }
    }
    return properties;
}
Also used : HashMap(java.util.HashMap) ConfigPropertyType(org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted.ConfigPropertyType) CryptoService(org.eclipse.kura.crypto.CryptoService) KuraException(org.eclipse.kura.KuraException) Password(org.eclipse.kura.configuration.Password) BundleContext(org.osgi.framework.BundleContext)

Example 5 with Password

use of org.eclipse.kura.configuration.Password in project kura by eclipse.

the class SslManagerServiceImpl method changeDefaultKeystorePassword.

private boolean changeDefaultKeystorePassword() {
    boolean result = false;
    this.m_timer = new Timer(true);
    char[] snapshotPassword = null;
    boolean needsPasswordChange = true;
    try {
        snapshotPassword = this.m_cryptoService.decryptAes(this.m_options.getSslKeystorePassword().toCharArray());
        needsPasswordChange = isDefaultPassword();
    } catch (KuraException e) {
    }
    // The password in the snapshot is the default password (or cannot be
    // decrypted).
    // If the framework is running in secure mode we must change the
    // password.
    // The keystore must be accessible with the old/default password.
    char[] oldPassword = this.m_cryptoService.getKeyStorePassword(this.m_options.getSslKeyStore());
    if (needsPasswordChange && snapshotPassword != null && isKeyStoreAccessible(this.m_options.getSslKeyStore(), snapshotPassword)) {
        oldPassword = snapshotPassword;
    }
    if (this.m_cryptoService.isFrameworkSecure() && needsPasswordChange && oldPassword != null && isKeyStoreAccessible(this.m_options.getSslKeyStore(), oldPassword)) {
        try {
            // generate a new random password
            char[] newPassword = new BigInteger(160, new SecureRandom()).toString(32).toCharArray();
            // change the password to the keystore
            changeKeyStorePassword(this.m_options.getSslKeyStore(), oldPassword, newPassword);
            // change the CryptoService SSL keystore password
            this.m_cryptoService.setKeyStorePassword(this.m_options.getSslKeyStore(), newPassword);
            // update our configuration with the newly generated password
            final String pid = (String) this.m_properties.get("service.pid");
            Map<String, Object> props = new HashMap<String, Object>(this.m_properties);
            props.put(SslManagerServiceOptions.PROP_TRUST_PASSWORD, new Password(newPassword));
            final Map<String, Object> theProperties = props;
            this.m_timer.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {
                    try {
                        if (SslManagerServiceImpl.this.m_ctx.getServiceReference() != null && SslManagerServiceImpl.this.m_configurationService.getComponentConfiguration(pid) != null) {
                            SslManagerServiceImpl.this.m_configurationService.updateConfiguration(pid, theProperties);
                            SslManagerServiceImpl.this.m_timer.cancel();
                        } else {
                            s_logger.info("No service or configuration available yet. Sleeping...");
                        }
                    } catch (KuraException e) {
                        s_logger.warn("Cannot get/update configuration for pid: {}", pid, e);
                    }
                }
            }, 1000, 1000);
            result = true;
        } catch (Exception e) {
            s_logger.warn("Keystore password change failed");
        }
    }
    return result;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SecureRandom(java.security.SecureRandom) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) KuraException(org.eclipse.kura.KuraException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) KeyManagementException(java.security.KeyManagementException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) KuraException(org.eclipse.kura.KuraException) BigInteger(java.math.BigInteger) Password(org.eclipse.kura.configuration.Password)

Aggregations

Password (org.eclipse.kura.configuration.Password)20 KuraException (org.eclipse.kura.KuraException)12 HashMap (java.util.HashMap)9 IOException (java.io.IOException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 KuraConnectException (org.eclipse.kura.KuraConnectException)3 KuraNotConnectedException (org.eclipse.kura.KuraNotConnectedException)3 KuraTimeoutException (org.eclipse.kura.KuraTimeoutException)3 KuraTooManyInflightMessagesException (org.eclipse.kura.KuraTooManyInflightMessagesException)3 CryptoService (org.eclipse.kura.crypto.CryptoService)3 FileNotFoundException (java.io.FileNotFoundException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 ConfigurationService (org.eclipse.kura.configuration.ConfigurationService)2 WifiMode (org.eclipse.kura.net.wifi.WifiMode)2 GwtKuraException (org.eclipse.kura.web.shared.GwtKuraException)2 GwtConfigParameterType (org.eclipse.kura.web.shared.model.GwtConfigParameter.GwtConfigParameterType)2 MqttException (org.eclipse.paho.client.mqttv3.MqttException)2 MqttPersistenceException (org.eclipse.paho.client.mqttv3.MqttPersistenceException)2