Search in sources :

Example 66 with SEPASecurityException

use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.

the class SSLManager method getSSLHttpClientTrustAllCa.

public CloseableHttpClient getSSLHttpClientTrustAllCa(String protocol) throws SEPASecurityException {
    // Trust own CA and all self-signed certificates and allow the specified
    // protocols
    LayeredConnectionSocketFactory sslsf = null;
    try {
        SSLContext ctx = SSLContext.getInstance(protocol);
        ctx.init(null, trustAllCerts, new java.security.SecureRandom());
        sslsf = new SSLConnectionSocketFactory(ctx, protocols, null, this);
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        logger.error(e.getMessage());
        if (logger.isTraceEnabled())
            e.printStackTrace();
        throw new SEPASecurityException(e.getMessage());
    }
    HttpClientBuilder clientFactory = HttpClients.custom().setSSLSocketFactory(sslsf);
    return clientFactory.build();
}
Also used : LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException)

Example 67 with SEPASecurityException

use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.

the class SSLManager method getSSLContextTrustAllCa.

public SSLContext getSSLContextTrustAllCa(String protocol) throws SEPASecurityException {
    SSLContext sc = null;
    try {
        sc = SSLContext.getInstance(protocol);
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new SEPASecurityException(e);
    }
    return sc;
}
Also used : SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException)

Example 68 with SEPASecurityException

use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.

the class ITPattern method genericClientSingleSubscribe.

@RepeatedTest(ConfigurationProvider.REPEATED_TEST)
@Timeout(10)
public void genericClientSingleSubscribe() {
    try {
        genericClient = new GenericClient(provider.getJsap(), handler);
        genericClient.subscribe("ALL", null, "first", provider.TIMEOUT, provider.NRETRY);
        handler.waitSubscribes(1);
        Response ret = genericClient.update("RANDOM", null, provider.TIMEOUT, provider.NRETRY);
        assertFalse(ret.isError(), ret.toString());
        handler.waitEvents(2);
        genericClient.unsubscribe(handler.getSpuid("first"), provider.TIMEOUT, provider.NRETRY);
        handler.waitUnsubscribes(1);
    } catch (SEPAProtocolException | SEPASecurityException | SEPAPropertiesException | SEPABindingsException | InterruptedException | IOException e) {
        e.printStackTrace();
        assertFalse(true, e.getMessage());
    }
}
Also used : Response(it.unibo.arces.wot.sepa.commons.response.Response) SEPAProtocolException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException) SEPABindingsException(it.unibo.arces.wot.sepa.commons.exceptions.SEPABindingsException) SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) IOException(java.io.IOException) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Timeout(org.junit.jupiter.api.Timeout)

Example 69 with SEPASecurityException

use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.

the class ACLManager method main.

public static void main(String[] args) {
    Console console = System.console();
    Scanner in = new Scanner(System.in);
    System.out.println("********************");
    System.out.println("* SEPA ACL Manager *");
    System.out.println("********************");
    LdapSecurityManager ldap;
    String line;
    while (true) {
        System.out.print("Host (return for default: localhost): ");
        line = in.nextLine();
        if (!line.equals(""))
            host = line;
        System.out.print("Port (return for default: 10389): ");
        line = in.nextLine();
        if (!line.equals(""))
            port = Integer.parseInt(line);
        System.out.print("Base (return for default: dc=sepatest,dc=com): ");
        line = in.nextLine();
        if (!line.equals(""))
            base = line;
        System.out.print("User (return for default: uid=admin,ou=system): ");
        line = in.nextLine();
        if (!line.equals(""))
            user = line;
        if (console != null)
            pwd = new String(console.readPassword("Password (default: secret):"));
        else {
            System.out.print("Password (default: secret):");
            line = in.nextLine();
            if (!line.equals(""))
                pwd = line;
        }
        try {
            ldap = new LdapSecurityManager(JKSUtil.getSSLContext("sepa.jks", "sepa2020"), JKSUtil.getRSAKey("sepa.jks", "sepa2020", "jwt", "sepa2020"), new LdapProperties(host, port, base, null, user, pwd, false));
        } catch (SEPASecurityException e2) {
            System.out.println(e2.getMessage());
            continue;
        }
        break;
    }
    System.out.println("Connected to LDAP!");
    System.out.println("Set SPARQL endpoint credentials");
    System.out.print("User (return for default: SEPATest):");
    line = in.nextLine();
    String user = "SEPATest";
    if (!line.equals(""))
        user = line;
    if (console != null)
        pwd = new String(console.readPassword("Password (default: SEPATest):"));
    else {
        System.out.print("Password (default: SEPATest):");
        line = in.nextLine();
        pwd = line;
    }
    while (true) {
        System.out.println("Available actions: ");
        System.out.println("1 - Register application");
        System.out.println("2 - Register device");
        System.out.println("3 - Register user");
        System.out.println("4 - Change SPARQL endpoint credentials");
        System.out.println("5 - Show SPARQL endpoint credentials");
        System.out.println("6 - Exit");
        System.out.print("Select: ");
        String action = in.nextLine();
        if (action.equals("6"))
            break;
        DigitalIdentity identity = null;
        String client_secret = null;
        switch(action) {
            case "1":
                System.out.print("UID: ");
                String uid = in.nextLine();
                identity = new ApplicationIdentity(uid, new Credentials(user, pwd));
                break;
            case "2":
                System.out.print("UID: ");
                uid = in.nextLine();
                identity = new DeviceIdentity(uid, new Credentials(user, pwd));
                break;
            case "3":
                System.out.print("Name: ");
                String cn = in.nextLine();
                System.out.print("Surname: ");
                String sn = in.nextLine();
                System.out.print("email: ");
                uid = in.nextLine();
                identity = new UserIdentity(uid, cn, sn, new Credentials(user, pwd));
                if (console != null)
                    client_secret = new String(console.readPassword("Password: "));
                else {
                    System.out.print("Password: ");
                    line = in.nextLine();
                    client_secret = line;
                }
                break;
            case "4":
                System.out.println("Change SPARQL endpoint credentials");
                System.out.print("User: ");
                user = in.nextLine();
                System.out.print("Password: ");
                pwd = in.nextLine();
                continue;
            case "5":
                System.out.println("SPARQL endpoint credentials");
                System.out.println("---------------------------");
                System.out.println("User: <" + user + ">");
                System.out.println("Password: <" + pwd + ">");
                System.out.println("---------------------------");
                continue;
            default:
                System.out.println("Wrong selection: " + action);
                continue;
        }
        try {
            if (action.equals("3")) {
                if (!ldap.storeCredentials(identity, client_secret)) {
                    System.out.print("Entity already exists! Do you want to replace it? (y/n): ");
                    if (in.nextLine().toLowerCase().startsWith("n"))
                        continue;
                    ldap.removeCredentials(identity);
                    ldap.storeCredentials(identity, client_secret);
                }
            } else
                ldap.addAuthorizedIdentity(identity);
        } catch (SEPASecurityException e) {
            try {
                if (!action.equals("4")) {
                    System.out.print("Entity already exists! Do you want to replace it? (y/n): ");
                    if (in.nextLine().toLowerCase().startsWith("n"))
                        continue;
                    ldap.removeAuthorizedIdentity(identity.getUid());
                    ldap.addAuthorizedIdentity(identity);
                } else {
                    System.out.println("Failed to create entity: " + identity);
                    continue;
                }
            } catch (SEPASecurityException e1) {
                System.out.println("Entity creation failed");
                continue;
            }
        }
        System.out.println("Entity created!");
    }
    in.close();
}
Also used : DeviceIdentity(it.unibo.arces.wot.sepa.engine.dependability.authorization.identities.DeviceIdentity) Scanner(java.util.Scanner) ApplicationIdentity(it.unibo.arces.wot.sepa.engine.dependability.authorization.identities.ApplicationIdentity) UserIdentity(it.unibo.arces.wot.sepa.engine.dependability.authorization.identities.UserIdentity) Console(java.io.Console) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) DigitalIdentity(it.unibo.arces.wot.sepa.engine.dependability.authorization.identities.DigitalIdentity) Credentials(it.unibo.arces.wot.sepa.commons.security.Credentials)

Aggregations

SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)69 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)29 IOException (java.io.IOException)20 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)18 ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)15 Response (it.unibo.arces.wot.sepa.commons.response.Response)12 SEPAPropertiesException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException)11 SEPAProtocolException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException)10 JsonObject (com.google.gson.JsonObject)7 JsonParser (com.google.gson.JsonParser)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)7 Modification (org.apache.directory.api.ldap.model.entry.Modification)7 SEPABindingsException (it.unibo.arces.wot.sepa.commons.exceptions.SEPABindingsException)5 Credentials (it.unibo.arces.wot.sepa.commons.security.Credentials)5 HttpEntity (org.apache.http.HttpEntity)5 JOSEException (com.nimbusds.jose.JOSEException)4 SignedJWT (com.nimbusds.jwt.SignedJWT)4 JWTResponse (it.unibo.arces.wot.sepa.commons.response.JWTResponse)4 ParseException (java.text.ParseException)4