Search in sources :

Example 1 with ChipGatewayDataError

use of org.openecard.addons.cg.ex.ChipGatewayDataError in project open-ecard by ecsec.

the class ChipGateway method createUpdateDialog.

private void createUpdateDialog(String dlUrl, boolean updateRequired) throws ChipGatewayDataError, InvalidRedirectUrlException {
    // stop here when hide dialog system property is set and the update is optional
    if (!updateRequired && ChipGatewayProperties.isHideUpdateDialog()) {
        return;
    }
    // only show if we have a download URL
    if (dlUrl != null && !dlUrl.isEmpty()) {
        try {
            URI uri = new URI(dlUrl);
            if (!"https".equalsIgnoreCase(uri.getScheme())) {
                showErrorMessage(LANG.translationForKey("error.server_wrong_config"));
                throw new MalformedURLException("Download URL is not an https URL.");
            }
            String dlHost = uri.getHost();
            if (ChipGatewayProperties.isUseUpdateDomainWhitelist() && !AllowedUpdateDomains.instance().isAllowedDomain(dlHost)) {
                String msg = String.format("Update host name (%s) does not match allowed domain names.", dlHost);
                LOG.error(msg);
                showErrorMessage(LANG.translationForKey("error.server_wrong_config"));
                throw new MalformedURLException(String.format("Download URL host (%s) is not in whitelist.", dlHost));
            }
            final UpdateDialog dialog = new UpdateDialog(gui, dlUrl, updateRequired);
            showDialogThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    dialog.display();
                }
            }, "Update-Dialog-" + TASK_THREAD_NUM.getAndIncrement());
            showDialogThread.setDaemon(true);
        } catch (MalformedURLException | URISyntaxException ex) {
            String msg = "Received malformed download URL from server.";
            LOG.error(msg, ex);
            throw new ChipGatewayDataError(token.finalizeErrorAddress(ResultMinor.SERVER_ERROR), msg, ex);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ChipGatewayDataError(org.openecard.addons.cg.ex.ChipGatewayDataError) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 2 with ChipGatewayDataError

use of org.openecard.addons.cg.ex.ChipGatewayDataError in project open-ecard by ecsec.

the class ChipGateway method sendMessageInterruptable.

private <T> T sendMessageInterruptable(final String resource, final String msg, final Class<T> resClass) throws ConnectionError, InvalidRedirectUrlException, ChipGatewayDataError, ThreadTerminateException {
    FutureTask<T> task = new FutureTask<>(new Callable<T>() {

        @Override
        public T call() throws Exception {
            return sendMessage(resource, msg, resClass);
        }
    });
    new Thread(task, "HTTP-Client-" + HTTP_THREAD_NUM.getAndIncrement()).start();
    try {
        return task.get();
    } catch (ExecutionException ex) {
        Throwable cause = ex.getCause();
        if (cause instanceof ConnectionError) {
            throw (ConnectionError) cause;
        } else if (cause instanceof InvalidRedirectUrlException) {
            throw (InvalidRedirectUrlException) cause;
        } else if (cause instanceof ChipGatewayDataError) {
            throw (ChipGatewayDataError) cause;
        } else if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        } else {
            throw new RuntimeException("Unexpected exception raised by HTTP message sending thread.", cause);
        }
    } catch (InterruptedException ex) {
        LOG.debug("Sending HTTP message interrupted.");
        task.cancel(true);
        // force new connection because this one may be unfinished and thus unusable
        try {
            conn.shutdown();
        } catch (IOException ignore) {
        }
        throw new ThreadTerminateException("Interrupt received while sending HTTP message.");
    }
}
Also used : InvalidRedirectUrlException(org.openecard.addons.cg.ex.InvalidRedirectUrlException) ConnectionError(org.openecard.addons.cg.ex.ConnectionError) ChipGatewayDataError(org.openecard.addons.cg.ex.ChipGatewayDataError) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RemotePinException(org.openecard.addons.cg.ex.RemotePinException) ThreadTerminateException(org.openecard.common.ThreadTerminateException) HttpException(org.openecard.apache.http.HttpException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) InvalidRedirectUrlException(org.openecard.addons.cg.ex.InvalidRedirectUrlException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) JoseException(org.jose4j.lang.JoseException) AuthServerException(org.openecard.addons.cg.ex.AuthServerException) UnsupportedAlgorithmException(org.openecard.crypto.common.UnsupportedAlgorithmException) MalformedURLException(java.net.MalformedURLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) CertificateException(java.security.cert.CertificateException) FutureTask(java.util.concurrent.FutureTask) ExecutionException(java.util.concurrent.ExecutionException) ThreadTerminateException(org.openecard.common.ThreadTerminateException)

Example 3 with ChipGatewayDataError

use of org.openecard.addons.cg.ex.ChipGatewayDataError in project open-ecard by ecsec.

the class ChipGateway method sendHello.

public TerminateType sendHello() throws VersionTooOld, ChipGatewayDataError, ConnectionError, InvalidRedirectUrlException, AuthServerException {
    try {
        byte[] challenge = ValueGenerators.generateRandom(32);
        helloReq = new HelloRequestType();
        helloReq.setSessionIdentifier(sessionId);
        helloReq.setVersion(String.format("%s.%s.%s", AppVersion.getMajor(), AppVersion.getMinor(), AppVersion.getPatch()));
        helloReq.setChallenge(challenge);
        // send Hello
        String helloReqMsg = mapper.writeValueAsString(helloReq);
        HelloResponseType helloResp = sendMessageInterruptable(getResource(helloUrl), helloReqMsg, HelloResponseType.class);
        processHelloResponse(helloResp);
        // send GetCommand
        GetCommandType cmdReq = createGetCommandRequest();
        String cmdReqMsg = mapper.writeValueAsString(cmdReq);
        CommandType cmdResp;
        try {
            cmdResp = sendMessageInterruptable(getResource(getCommandUrl), cmdReqMsg, CommandType.class);
        } catch (ThreadTerminateException ex) {
            performProcessCancelled();
            throw ex;
        }
        // send messages to the server as long as there is no termination response
        while (cmdResp.getTerminate() == null) {
            ListTokensRequestType tokensReq = cmdResp.getListTokensRequest();
            ListCertificatesRequestType certReq = cmdResp.getListCertificatesRequest();
            SignRequestType signReq = cmdResp.getSignRequest();
            if (tokensReq != null) {
                cmdResp = processTokensRequest(tokensReq);
            } else if (certReq != null) {
                cmdResp = processCertificatesRequest(certReq);
            } else if (signReq != null) {
                cmdResp = processSignRequest(signReq);
            } else {
                throw new ChipGatewayDataError(token.finalizeErrorAddress(ResultMinor.SERVER_ERROR), INVALID_CHIPGATEWAY_MSG);
            }
        }
        // return the last message (terminate type)
        return cmdResp.getTerminate();
    } catch (JsonProcessingException ex) {
        throw new ChipGatewayDataError(token.finalizeErrorAddress(ResultMinor.CLIENT_ERROR), INVALID_CHIPGATEWAY_MSG, ex);
    } finally {
        // clear token cache and delete all pins in it
        tokenCache.clearPins();
        // display GUI if needed
        if (showDialogThread != null) {
            showDialogThread.start();
        }
        try {
            // in case we are interrupted, terminate is sent in the background, so don't close just yet
            if (conn != null && !isInterrupted) {
                conn.close();
            }
        } catch (IOException ex) {
            LOG.error("Failed to close connection to server.", ex);
        }
        // disconnect all slots which have been connected in the process
        for (byte[] nextSlot : connectedSlots) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Disconnecting card with slotHandle={}.", ByteUtils.toHexString(nextSlot));
            }
            CardApplicationDisconnect req = new CardApplicationDisconnect();
            // req.setAction(ActionType.RESET);
            ConnectionHandleType handle = HandlerBuilder.create().setSlotHandle(nextSlot).buildConnectionHandle();
            req.setConnectionHandle(handle);
            dispatcher.safeDeliver(req);
        }
    }
}
Also used : ListTokensRequestType(org.openecard.ws.chipgateway.ListTokensRequestType) ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) CardApplicationDisconnect(iso.std.iso_iec._24727.tech.schema.CardApplicationDisconnect) ChipGatewayDataError(org.openecard.addons.cg.ex.ChipGatewayDataError) GetCommandType(org.openecard.ws.chipgateway.GetCommandType) IOException(java.io.IOException) SignRequestType(org.openecard.ws.chipgateway.SignRequestType) HelloResponseType(org.openecard.ws.chipgateway.HelloResponseType) CommandType(org.openecard.ws.chipgateway.CommandType) GetCommandType(org.openecard.ws.chipgateway.GetCommandType) ListCertificatesRequestType(org.openecard.ws.chipgateway.ListCertificatesRequestType) HelloRequestType(org.openecard.ws.chipgateway.HelloRequestType) ThreadTerminateException(org.openecard.common.ThreadTerminateException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

ChipGatewayDataError (org.openecard.addons.cg.ex.ChipGatewayDataError)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 ThreadTerminateException (org.openecard.common.ThreadTerminateException)2 CardApplicationDisconnect (iso.std.iso_iec._24727.tech.schema.CardApplicationDisconnect)1 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)1 URI (java.net.URI)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 ExecutionException (java.util.concurrent.ExecutionException)1 FutureTask (java.util.concurrent.FutureTask)1 TimeoutException (java.util.concurrent.TimeoutException)1 JoseException (org.jose4j.lang.JoseException)1 AuthServerException (org.openecard.addons.cg.ex.AuthServerException)1 ConnectionError (org.openecard.addons.cg.ex.ConnectionError)1 InvalidRedirectUrlException (org.openecard.addons.cg.ex.InvalidRedirectUrlException)1 RemotePinException (org.openecard.addons.cg.ex.RemotePinException)1