Search in sources :

Example 1 with CryptoService

use of org.eclipse.kura.crypto.CryptoService 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 2 with CryptoService

use of org.eclipse.kura.crypto.CryptoService 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 3 with CryptoService

use of org.eclipse.kura.crypto.CryptoService in project kura by eclipse.

the class SecureBasicHttpContext method handleSecurity.

/**
 * Provides Basic authentication over HTTPS.
 */
@Override
public synchronized boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setHeader("X-FRAME-OPTIONS", "SAMEORIGIN");
    response.setHeader("X-XSS-protection", "1; mode=block");
    response.setHeader("X-Content-Type-Options", "nosniff");
    response.setHeader("Cache-Control", "no-cache,no-store");
    response.setHeader("Pragma", "no-cache");
    // If a trailing "/" is used when accesssing the app, redirect
    if (request.getRequestURI().equals(this.m_appRoot + "/")) {
        response.sendRedirect(this.m_appRoot);
    }
    // If using root context, redirect
    if (request.getRequestURI().equals("/")) {
        response.sendRedirect(this.m_appRoot);
    }
    HttpSession session = request.getSession(false);
    if (session != null) {
        String logout = (String) session.getAttribute("logout");
        if (logout != null) {
            session.removeAttribute("logout");
            session.invalidate();
            return failAuthorization(response);
        }
    }
    String authHeader = request.getHeader("Authorization");
    if (authHeader == null) {
        s_logger.debug("Missing 'Authorization' HTTP header");
        return failAuthorization(response);
    }
    StringTokenizer tokens = new StringTokenizer(authHeader);
    String authScheme = tokens.nextToken();
    if (!"Basic".equals(authScheme)) {
        s_logger.error("The authentication scheme is not 'Basic'");
        return failAuthorization(response);
    }
    String base64 = tokens.nextToken();
    String credentials = null;
    try {
        CryptoService cryptoService = ServiceLocator.getInstance().getService(CryptoService.class);
        credentials = cryptoService.decodeBase64(base64);
    } catch (GwtKuraException e) {
        throw new IOException(e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        throw new IOException(e.getMessage());
    }
    int colon = credentials.indexOf(':');
    String userid = credentials.substring(0, colon);
    String password = credentials.substring(colon + 1);
    Subject subject = login(request, response, userid, password);
    if (subject == null) {
        return failAuthorization(response);
    }
    request.setAttribute(HttpContext.REMOTE_USER, null);
    request.setAttribute(HttpContext.AUTHENTICATION_TYPE, request.getAuthType());
    request.setAttribute(HttpContext.AUTHORIZATION, null);
    return true;
}
Also used : GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) StringTokenizer(java.util.StringTokenizer) CryptoService(org.eclipse.kura.crypto.CryptoService) HttpSession(javax.servlet.http.HttpSession) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Subject(javax.security.auth.Subject)

Example 4 with CryptoService

use of org.eclipse.kura.crypto.CryptoService in project kura by eclipse.

the class GwtSslServiceImpl method updateSslConfiguration.

@Override
public void updateSslConfiguration(GwtXSRFToken xsrfToken, GwtSslConfig sslConfig) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    try {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(PROP_PROTOCOL, sslConfig.getProtocol());
        properties.put(PROP_HN_VERIFY, sslConfig.isHostnameVerification());
        properties.put(PROP_TRUST_STORE, sslConfig.getKeyStore());
        if (PLACEHOLDER.equals(sslConfig.getKeystorePassword())) {
            CryptoService cryptoService = ServiceLocator.getInstance().getService(CryptoService.class);
            SslManagerServiceOptions options = getSslConfiguration();
            properties.put(PROP_TRUST_PASSWORD, new Password(cryptoService.decryptAes(options.getSslKeystorePassword().toCharArray())));
        } else {
            properties.put(PROP_TRUST_PASSWORD, new Password(sslConfig.getKeystorePassword()));
        }
        properties.put(PROP_CIPHERS, sslConfig.getCiphers());
        ConfigurationService configService = ServiceLocator.getInstance().getService(ConfigurationService.class);
        configService.updateConfiguration(SSL_PID, properties);
    } catch (KuraException e) {
        throw new GwtKuraException(e.getMessage());
    }
}
Also used : SslManagerServiceOptions(org.eclipse.kura.ssl.SslManagerServiceOptions) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) HashMap(java.util.HashMap) CryptoService(org.eclipse.kura.crypto.CryptoService) KuraException(org.eclipse.kura.KuraException) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) ConfigurationService(org.eclipse.kura.configuration.ConfigurationService) Password(org.eclipse.kura.configuration.Password)

Example 5 with CryptoService

use of org.eclipse.kura.crypto.CryptoService in project kura by eclipse.

the class AuthenticationManager method authenticate.

public boolean authenticate(String username, String password) {
    try {
        CryptoService cryptoService = ServiceLocator.getInstance().getService(CryptoService.class);
        String sha1Password = cryptoService.sha1Hash(password);
        boolean isUsernameMatching = username.equals(this.username);
        boolean isPasswordMatching = Arrays.equals(sha1Password.toCharArray(), this.password);
        return isUsernameMatching && isPasswordMatching;
    } catch (Exception e) {
    }
    return false;
}
Also used : CryptoService(org.eclipse.kura.crypto.CryptoService)

Aggregations

CryptoService (org.eclipse.kura.crypto.CryptoService)5 KuraException (org.eclipse.kura.KuraException)3 Password (org.eclipse.kura.configuration.Password)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 GwtKuraException (org.eclipse.kura.web.shared.GwtKuraException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 Subject (javax.security.auth.Subject)1 HttpSession (javax.servlet.http.HttpSession)1 JAXBException (javax.xml.bind.JAXBException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 ConfigurationService (org.eclipse.kura.configuration.ConfigurationService)1 ConfigPropertyType (org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted.ConfigPropertyType)1 SslManagerServiceOptions (org.eclipse.kura.ssl.SslManagerServiceOptions)1 BundleContext (org.osgi.framework.BundleContext)1