Search in sources :

Example 1 with UserCancellationException

use of org.openecard.binding.tctoken.ex.UserCancellationException in project open-ecard by ecsec.

the class TCTokenRequest method findCard.

/**
 * Finds a card which matches one of the give types.
 *
 * @param types String array containing valid card types.
 * @param disp Dispatcher used to query cards and terminals.
 * @param gui User consent to display messages to the user.
 * @return ConnectionHandleType object of the chosen card.
 */
private static ConnectionHandleType findCard(@Nonnull String[] types, @Nonnull Context ctx) throws MissingActivationParameterException, UserCancellationException {
    CardRecognition rec = ctx.getRecognition();
    Map<String, String> namesAndType = new HashMap<>();
    for (String type : types) {
        namesAndType.put(rec.getTranslatedCardName(type), type);
    }
    InsertCardDialog insCardDiag = new InsertCardDialog(ctx.getUserConsent(), ctx.getCardStates(), namesAndType, ctx.getEventDispatcher());
    List<ConnectionHandleType> usableCards = insCardDiag.show();
    if (usableCards == null) {
        // user aborted the card insertion dialog
        throw new UserCancellationException(null, LANG.translationForKey(CARD_INSERTION_ABORT));
    }
    ConnectionHandleType handle;
    if (usableCards.size() > 1) {
        UserConsentDescription ucd = new UserConsentDescription(LANG.translationForKey("card.selection.heading.uc", AppVersion.getName()));
        String stepTitle = LANG.translationForKey("card.selection.heading.step");
        CardSelectionStep step = new CardSelectionStep(stepTitle, usableCards, ctx.getRecognition());
        ArrayList<String> types2 = new ArrayList<>();
        types2.addAll(namesAndType.values());
        CardMonitorTask task = new CardMonitorTask(types2, step);
        ctx.getEventDispatcher().add(task, EventType.CARD_REMOVED, EventType.CARD_RECOGNIZED);
        step.setBackgroundTask(task);
        CardSelectionAction action = new CardSelectionAction(step, usableCards, types2, ctx);
        step.setAction(action);
        ucd.getSteps().add(step);
        UserConsent uc = ctx.getUserConsent();
        UserConsentNavigator ucNav = uc.obtainNavigator(ucd);
        ExecutionEngine exec = new ExecutionEngine(ucNav);
        ResultStatus resStatus = exec.process();
        if (resStatus != ResultStatus.OK) {
            throw new MissingActivationParameterException(CARD_SELECTION_ABORT);
        }
        handle = action.getResult();
        ctx.getEventDispatcher().del(task);
    } else {
        handle = usableCards.get(0);
    }
    return handle;
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) HashMap(java.util.HashMap) ResultStatus(org.openecard.gui.ResultStatus) ArrayList(java.util.ArrayList) MissingActivationParameterException(org.openecard.binding.tctoken.ex.MissingActivationParameterException) CardMonitorTask(org.openecard.addons.tr03124.gui.CardMonitorTask) CardSelectionStep(org.openecard.addons.tr03124.gui.CardSelectionStep) UserConsent(org.openecard.gui.UserConsent) UserConsentNavigator(org.openecard.gui.UserConsentNavigator) ExecutionEngine(org.openecard.gui.executor.ExecutionEngine) UserCancellationException(org.openecard.binding.tctoken.ex.UserCancellationException) UserConsentDescription(org.openecard.gui.definition.UserConsentDescription) CardRecognition(org.openecard.common.interfaces.CardRecognition) InsertCardDialog(org.openecard.common.sal.util.InsertCardDialog) CardSelectionAction(org.openecard.addons.tr03124.gui.CardSelectionAction)

Example 2 with UserCancellationException

use of org.openecard.binding.tctoken.ex.UserCancellationException in project open-ecard by ecsec.

the class TCTokenRequest method parseTCTokenRequestURI.

private static TCTokenRequest parseTCTokenRequestURI(Map<String, String> queries, Context ctx) throws InvalidTCTokenException, MissingActivationParameterException, AuthServerException, InvalidRedirectUrlException, InvalidTCTokenElement, InvalidTCTokenUrlException, SecurityViolationException, InvalidAddressException, UserCancellationException {
    TCTokenRequest tcTokenRequest = new TCTokenRequest();
    try {
        if (queries.containsKey("cardTypes") || queries.containsKey("cardType")) {
            String[] types;
            if (queries.containsKey("cardType")) {
                types = new String[] { queries.get("cardType") };
            } else {
                types = queries.get("cardTypes").split(",");
            }
            ConnectionHandleType handle = findCard(types, ctx);
            setIfdName(queries, handle.getIFDName());
            setContextHandle(queries, handle.getContextHandle());
            setSlotIndex(queries, handle.getSlotIndex());
            addTokenUrlParameter(queries, handle.getRecognitionInfo());
        } else {
            String[] types = new String[] { tcTokenRequest.cardType };
            ConnectionHandleType handle = findCard(types, ctx);
            setIfdName(queries, handle.getIFDName());
            setContextHandle(queries, handle.getContextHandle());
            setSlotIndex(queries, handle.getSlotIndex());
        }
    } catch (UserCancellationException ex) {
        if (queries.containsKey("cardTypes")) {
            addTokenUrlParameter(queries, queries.get("cardTypes").split(",")[0]);
        }
        LOG.warn("The user aborted the CardInsertion dialog.", ex);
        DynamicContext dynCtx = DynamicContext.getInstance(TR03112Keys.INSTANCE_KEY);
        dynCtx.put(TR03112Keys.CARD_SELECTION_CANCELLATION, ex);
    }
    String activationTokenUrl = null;
    for (Map.Entry<String, String> next : queries.entrySet()) {
        String k = next.getKey();
        k = k == null ? "" : k;
        String v = next.getValue();
        if (v == null || v.isEmpty()) {
            LOG.info("Skipping query parameter '{}' because it does not contain a value.", k);
        } else {
            switch(k) {
                case "tcTokenURL":
                    activationTokenUrl = v;
                    break;
                case "ifdName":
                    tcTokenRequest.ifdName = v;
                    break;
                case "contextHandle":
                    tcTokenRequest.contextHandle = StringUtils.toByteArray(v);
                    break;
                case "slotIndex":
                    tcTokenRequest.slotIndex = new BigInteger(v);
                    break;
                case "cardType":
                    tcTokenRequest.cardType = v;
                    break;
                default:
                    LOG.info("Unknown query element: {}", k);
                    break;
            }
        }
    }
    // cardType determined! set in dynamic context, so the information is available in ResourceContext
    DynamicContext dynCtx = DynamicContext.getInstance(TR03112Keys.INSTANCE_KEY);
    dynCtx.put(TR03112Keys.ACTIVATION_CARD_TYPE, tcTokenRequest.cardType);
    if (activationTokenUrl != null) {
        try {
            URL tokenUrl = new URL(activationTokenUrl);
            TCTokenContext tokenCtx = TCTokenContext.generateTCToken(tokenUrl);
            tcTokenRequest.tokenCtx = tokenCtx;
            tcTokenRequest.token = tokenCtx.getToken();
            tcTokenRequest.certificates = tokenCtx.getCerts();
            tcTokenRequest.tcTokenURL = tokenUrl;
        } catch (MalformedURLException ex) {
            // TODO: check if the error type is correct, was WRONG_PARAMETER before
            throw new InvalidTCTokenUrlException(INVALID_TCTOKEN_URL, ex, activationTokenUrl);
        }
    }
    if (tcTokenRequest.token == null) {
        throw new MissingActivationParameterException(NO_TOKEN);
    }
    return tcTokenRequest;
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) MalformedURLException(java.net.MalformedURLException) MissingActivationParameterException(org.openecard.binding.tctoken.ex.MissingActivationParameterException) URL(java.net.URL) InvalidTCTokenUrlException(org.openecard.binding.tctoken.ex.InvalidTCTokenUrlException) UserCancellationException(org.openecard.binding.tctoken.ex.UserCancellationException) BigInteger(java.math.BigInteger) HashMap(java.util.HashMap) Map(java.util.Map) DynamicContext(org.openecard.common.DynamicContext)

Example 3 with UserCancellationException

use of org.openecard.binding.tctoken.ex.UserCancellationException in project open-ecard by ecsec.

the class TCTokenVerifier method determineRefreshAddress.

/**
 * Determines the refresh URL.
 *
 * @param ex The exception which caused the abort of the TCToken verification.
 * @throws InvalidRedirectUrlException If the CommunicationErrorAddress cant be determined.
 * @throws InvalidTCTokenElement If a determination of a refresh or CommunicationError address was successful.
 * @throws UserCancellationException Thrown in case {@code ex} is an instance of {@link UserCancellationException}.
 */
private void determineRefreshAddress(ActivationError ex) throws InvalidRedirectUrlException, InvalidTCTokenElement, UserCancellationException {
    if (token.getRefreshAddress() != null) {
        try {
            CertificateValidator validator = new RedirectCertificateValidator(true);
            ResourceContext newResCtx = ResourceContext.getStream(new URL(token.getRefreshAddress()), validator);
            newResCtx.closeStream();
            List<Pair<URL, TlsServerCertificate>> resultPoints = newResCtx.getCerts();
            Pair<URL, TlsServerCertificate> last = resultPoints.get(resultPoints.size() - 1);
            URL resAddr = last.p1;
            String refreshUrl = resAddr.toString();
            if (ex instanceof UserCancellationException) {
                UserCancellationException uex = (UserCancellationException) ex;
                URI refreshUrlAsUrl = createUrlWithErrorParams(refreshUrl, ResultMinor.CANCELLATION_BY_USER, ex.getMessage());
                throw new UserCancellationException(refreshUrlAsUrl.toString(), ex);
            }
            URI refreshUrlAsUrl = createUrlWithErrorParams(refreshUrl, ResultMinor.TRUSTED_CHANNEL_ESTABLISCHMENT_FAILED, ex.getMessage());
            throw new InvalidTCTokenElement(refreshUrlAsUrl.toString(), ex);
        } catch (IOException | ResourceException | InvalidAddressException | ValidationError | URISyntaxException ex1) {
            String errorUrl = token.getComErrorAddressWithParams(ResultMinor.COMMUNICATION_ERROR);
            throw new InvalidTCTokenElement(errorUrl, INVALID_REFRESH_ADDRESS, ex1);
        }
    } else {
        String errorUrl = token.getComErrorAddressWithParams(ResultMinor.COMMUNICATION_ERROR);
        throw new InvalidTCTokenElement(errorUrl, NO_REFRESH_ADDRESS);
    }
}
Also used : IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) TlsServerCertificate(org.openecard.bouncycastle.tls.TlsServerCertificate) UserCancellationException(org.openecard.binding.tctoken.ex.UserCancellationException) InvalidTCTokenElement(org.openecard.binding.tctoken.ex.InvalidTCTokenElement) InvalidAddressException(org.openecard.binding.tctoken.ex.InvalidAddressException) Pair(org.openecard.common.util.Pair)

Example 4 with UserCancellationException

use of org.openecard.binding.tctoken.ex.UserCancellationException in project open-ecard by ecsec.

the class TCTokenVerifier method checkUserCancellation.

private void checkUserCancellation() throws InvalidRedirectUrlException, InvalidTCTokenElement, UserCancellationException {
    DynamicContext dynCtx = DynamicContext.getInstance(TR03112Keys.INSTANCE_KEY);
    UserCancellationException ex = (UserCancellationException) dynCtx.get(TR03112Keys.CARD_SELECTION_CANCELLATION);
    if (ex != null) {
        determineRefreshAddress(ex);
    }
}
Also used : UserCancellationException(org.openecard.binding.tctoken.ex.UserCancellationException) DynamicContext(org.openecard.common.DynamicContext)

Aggregations

UserCancellationException (org.openecard.binding.tctoken.ex.UserCancellationException)4 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 MissingActivationParameterException (org.openecard.binding.tctoken.ex.MissingActivationParameterException)2 DynamicContext (org.openecard.common.DynamicContext)2 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 CardMonitorTask (org.openecard.addons.tr03124.gui.CardMonitorTask)1 CardSelectionAction (org.openecard.addons.tr03124.gui.CardSelectionAction)1 CardSelectionStep (org.openecard.addons.tr03124.gui.CardSelectionStep)1 InvalidAddressException (org.openecard.binding.tctoken.ex.InvalidAddressException)1 InvalidTCTokenElement (org.openecard.binding.tctoken.ex.InvalidTCTokenElement)1 InvalidTCTokenUrlException (org.openecard.binding.tctoken.ex.InvalidTCTokenUrlException)1 TlsServerCertificate (org.openecard.bouncycastle.tls.TlsServerCertificate)1