Search in sources :

Example 1 with EAC2OutputType

use of iso.std.iso_iec._24727.tech.schema.EAC2OutputType in project open-ecard by ecsec.

the class AndroidMarshaller method marshal.

@Override
public synchronized Document marshal(Object o) throws MarshallingTypeException {
    Document document = documentBuilder.newDocument();
    document.setXmlStandalone(true);
    Element rootElement = null;
    if (o instanceof DestroyChannel) {
        DestroyChannel destroyChannel = (DestroyChannel) o;
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        Element emSlotHandle = createElementIso(document, "SlotHandle");
        emSlotHandle.appendChild(document.createTextNode(ByteUtils.toHexString(destroyChannel.getSlotHandle())));
        rootElement.appendChild(emSlotHandle);
    } else if (o instanceof DestroyChannelResponse) {
        DestroyChannelResponse response = (DestroyChannelResponse) o;
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        if (response.getProfile() != null) {
            Element emProfile = createElementIso(document, "Profile");
            emProfile.appendChild(document.createTextNode(response.getProfile()));
            rootElement.appendChild(emProfile);
        }
        if (response.getRequestID() != null) {
            Element emRequest = createElementIso(document, "RequestID");
            emRequest.appendChild(document.createElement(response.getRequestID()));
            rootElement.appendChild(emRequest);
        }
        if (response.getResult() != null) {
            Element emResult = marshalResult(response.getResult(), document);
            rootElement.appendChild(emResult);
        }
    } else if (o instanceof EstablishChannel) {
        EstablishChannel establishChannel = (EstablishChannel) o;
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        Element emSlotHandle = createElementIso(document, "SlotHandle");
        emSlotHandle.appendChild(document.createTextNode(ByteUtils.toHexString(establishChannel.getSlotHandle())));
        rootElement.appendChild(emSlotHandle);
        Element emAuthProtData = createElementIso(document, "AuthenticationProtocolData");
        emAuthProtData.setAttribute("Protocol", establishChannel.getAuthenticationProtocolData().getProtocol());
        for (Element e : establishChannel.getAuthenticationProtocolData().getAny()) {
            Element eClone = createElementIso(document, e.getLocalName());
            eClone.setTextContent(e.getTextContent());
            eClone.setAttribute("xmlns", "urn:iso:std:iso-iec:24727:tech:schema");
            emAuthProtData.appendChild(eClone);
        }
        if (establishChannel.getProfile() != null) {
            Element emProfile = createElementIso(document, "Profile");
            emProfile.appendChild(document.createTextNode(establishChannel.getProfile()));
            rootElement.appendChild(emProfile);
        }
        if (establishChannel.getRequestID() != null) {
            Element emRequest = createElementIso(document, "RequestID");
            emRequest.appendChild(document.createElement(establishChannel.getRequestID()));
            rootElement.appendChild(emRequest);
        }
        rootElement.appendChild(emAuthProtData);
    } else if (o instanceof EstablishChannelResponse) {
        EstablishChannelResponse response = (EstablishChannelResponse) o;
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        if (response.getProfile() != null) {
            Element emProfile = createElementIso(document, "Profile");
            emProfile.appendChild(document.createTextNode(response.getProfile()));
            rootElement.appendChild(emProfile);
        }
        if (response.getRequestID() != null) {
            Element emRequest = createElementIso(document, "RequestID");
            emRequest.appendChild(document.createElement(response.getRequestID()));
            rootElement.appendChild(emRequest);
        }
        if (response.getResult() != null) {
            Element emResult = marshalResult(response.getResult(), document);
            rootElement.appendChild(emResult);
        }
        if (response.getAuthenticationProtocolData() != null) {
            Element emAuthProtData = createElementIso(document, "AuthenticationProtocolData");
            emAuthProtData.setAttribute("Protocol", response.getAuthenticationProtocolData().getProtocol());
            for (Element e : response.getAuthenticationProtocolData().getAny()) {
                Element eClone = createElementIso(document, e.getLocalName());
                eClone.setTextContent(e.getTextContent());
                eClone.setAttribute("xmlns", "urn:iso:std:iso-iec:24727:tech:schema");
                emAuthProtData.appendChild(eClone);
            }
        }
    } else if (o instanceof DIDAuthenticate) {
        DIDAuthenticate auth = (DIDAuthenticate) o;
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        Element em;
        if (auth.getConnectionHandle() != null) {
            em = marshalConnectionHandle(auth.getConnectionHandle(), document);
            rootElement.appendChild(em);
        }
        if (auth.getDIDScope() != null) {
            em = createElementIso(document, "DIDScope");
            em.appendChild(document.createTextNode(auth.getDIDScope().value()));
            rootElement.appendChild(em);
        }
        if (auth.getDIDName() != null) {
            em = createElementIso(document, "DIDName");
            em.appendChild(document.createTextNode(auth.getDIDName()));
            rootElement.appendChild(em);
        }
        if (auth.getAuthenticationProtocolData() != null) {
            DIDAuthenticationDataType d = auth.getAuthenticationProtocolData();
            em = createElementIso(document, "AuthenticationProtocolData");
            em.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            if (d instanceof EAC1OutputType) {
                em.setAttribute("xsi:type", "iso:EAC1OutputType");
            } else if (d instanceof EAC2OutputType) {
                em.setAttribute("xsi:type", "iso:EAC2OutputType");
            } else if (d instanceof EAC1InputType) {
                em.setAttribute("xsi:type", "iso:EAC1InputType");
            } else if (d instanceof EAC2InputType) {
                em.setAttribute("xsi:type", "iso:EAC2InputType");
            } else {
                String msg = "Marshalling a DIDAuthenticationDataType of " + d.getClass().getName() + " in DIDAuthentication is not supported";
                LOG.error(msg);
                throw new MarshallingTypeException(msg);
            }
            for (Element e : d.getAny()) {
                Element elemCopy = createElementIso(document, e.getLocalName());
                elemCopy.setTextContent(e.getTextContent());
                em.appendChild(elemCopy);
            }
            for (Map.Entry<QName, String> entry : d.getOtherAttributes().entrySet()) {
                em.setAttribute(entry.getKey().getNamespaceURI(), entry.getValue());
            }
            if (d.getProtocol() != null) {
                em.setAttribute("Protocol", d.getProtocol());
            }
            rootElement.appendChild(em);
        }
        if (auth.getSAMConnectionHandle() != null) {
            em = marshalConnectionHandle(auth.getSAMConnectionHandle(), document);
            rootElement.appendChild(em);
        }
        if (auth.getProfile() != null) {
            Element emProfile = createElementIso(document, "Profile");
            emProfile.appendChild(document.createTextNode(auth.getProfile()));
            rootElement.appendChild(emProfile);
        }
        if (auth.getRequestID() != null) {
            Element emRequest = createElementIso(document, "RequestID");
            emRequest.appendChild(document.createElement(auth.getRequestID()));
            rootElement.appendChild(emRequest);
        }
    } else if (o instanceof DIDAuthenticateResponse) {
        DIDAuthenticateResponse didAuthenticateResponse = (DIDAuthenticateResponse) o;
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        rootElement.appendChild(marshalResult(didAuthenticateResponse.getResult(), document));
        if (didAuthenticateResponse.getAuthenticationProtocolData() != null) {
            DIDAuthenticationDataType didAuthenticationDataType = didAuthenticateResponse.getAuthenticationProtocolData();
            Element elemEACOutput = createElementIso(document, "AuthenticationProtocolData");
            elemEACOutput.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            if (didAuthenticationDataType instanceof EAC1OutputType) {
                elemEACOutput.setAttribute("xsi:type", "iso:EAC1OutputType");
            } else if (didAuthenticationDataType instanceof EAC2OutputType) {
                elemEACOutput.setAttribute("xsi:type", "iso:EAC2OutputType");
            } else {
                String msg = "Marshalling a DIDAuthenticationDataType of " + didAuthenticationDataType.getClass().getName() + " in DIDAuthentication is not supported";
                throw new MarshallingTypeException(msg);
            }
            for (Element e : didAuthenticationDataType.getAny()) {
                Element elemCopy = createElementIso(document, e.getLocalName());
                elemCopy.setTextContent(e.getTextContent());
                elemEACOutput.appendChild(elemCopy);
            }
            rootElement.appendChild(elemEACOutput);
        }
    // else only the result (with error) is returned
    } else if (o instanceof InitializeFrameworkResponse) {
        InitializeFrameworkResponse initializeFrameworkResponse = (InitializeFrameworkResponse) o;
        rootElement = createElementEcapi(document, o.getClass().getSimpleName());
        rootElement.appendChild(marshalResult(initializeFrameworkResponse.getResult(), document));
        Element emVersion = createElementEcapi(document, "Version");
        Element emMajor = createElementEcapi(document, "Major");
        emMajor.appendChild(document.createTextNode(initializeFrameworkResponse.getVersion().getMajor().toString()));
        emVersion.appendChild(emMajor);
        Element emMinor = createElementEcapi(document, "Minor");
        emMinor.appendChild(document.createTextNode(initializeFrameworkResponse.getVersion().getMinor().toString()));
        emVersion.appendChild(emMinor);
        Element emSubMinor = createElementEcapi(document, "SubMinor");
        emSubMinor.appendChild(document.createTextNode(initializeFrameworkResponse.getVersion().getSubMinor().toString()));
        emVersion.appendChild(emSubMinor);
        rootElement.appendChild(emVersion);
    } else if (o instanceof InternationalStringType) {
        InternationalStringType internationalStringType = (InternationalStringType) o;
        rootElement = marshalInternationStringType(internationalStringType, document, internationalStringType.getClass().getSimpleName());
    } else if (o instanceof Result) {
        Result r = (Result) o;
        rootElement = marshalResult(r, document);
    } else if (o instanceof iso.std.iso_iec._24727.tech.schema.StartPAOS) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        StartPAOS startPAOS = (StartPAOS) o;
        Element em = createElementIso(document, "SessionIdentifier");
        em.appendChild(document.createTextNode(startPAOS.getSessionIdentifier()));
        rootElement.appendChild(em);
        rootElement.appendChild(marshalConnectionHandle(startPAOS.getConnectionHandle().get(0), document));
    } else if (o instanceof TransmitResponse) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        TransmitResponse transmitResponsePOJO = (TransmitResponse) o;
        Element em = marshalResult(transmitResponsePOJO.getResult(), document);
        rootElement.appendChild(em);
        for (int i = 0; i < transmitResponsePOJO.getOutputAPDU().size(); i++) {
            em = createElementIso(document, "OutputAPDU");
            em.appendChild(document.createTextNode(ByteUtils.toHexString(transmitResponsePOJO.getOutputAPDU().get(i))));
            rootElement.appendChild(em);
        }
    } else if (o instanceof EstablishContext) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
    } else if (o instanceof EstablishContextResponse) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        EstablishContextResponse establishContextResponse = (EstablishContextResponse) o;
        Element em = createElementIso(document, "ContextHandle");
        em.appendChild(document.createTextNode(ByteUtils.toHexString(establishContextResponse.getContextHandle())));
        rootElement.appendChild(em);
        em = marshalResult(establishContextResponse.getResult(), document);
        rootElement.appendChild(em);
    } else if (o instanceof GetStatus) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        GetStatus getStatus = (GetStatus) o;
        Element em = createElementIso(document, "ContextHandle");
        em.appendChild(document.createTextNode(ByteUtils.toHexString(getStatus.getContextHandle())));
        rootElement.appendChild(em);
        if (getStatus.getIFDName() != null) {
            em = createElementIso(document, "IFDName");
            em.appendChild(document.createTextNode(getStatus.getIFDName()));
            rootElement.appendChild(em);
        }
    } else if (o instanceof Wait) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        Wait w = (Wait) o;
        Element em = createElementIso(document, "ContextHandle");
        em.appendChild(document.createTextNode(ByteUtils.toHexString(w.getContextHandle())));
        rootElement.appendChild(em);
        if (w.getTimeOut() != null) {
            em = createElementIso(document, "TimeOut");
            em.appendChild(document.createTextNode(w.getTimeOut().toString(16)));
            rootElement.appendChild(em);
        }
        if (w.getCallback() != null) {
            ChannelHandleType callback = w.getCallback();
            em = createElementIso(document, "Callback");
            if (callback.getBinding() != null) {
                Element em2 = createElementIso(document, "Binding");
                em2.appendChild(document.createTextNode(callback.getBinding()));
                em.appendChild(em2);
            }
            if (callback.getSessionIdentifier() != null) {
                Element em2 = createElementIso(document, "SessionIdentifier");
                em2.appendChild(document.createTextNode(callback.getSessionIdentifier()));
                em.appendChild(em2);
            }
            if (callback.getProtocolTerminationPoint() != null) {
                Element em2 = createElementIso(document, "ProtocolTerminationPoint");
                em2.appendChild(document.createTextNode(callback.getProtocolTerminationPoint()));
                em.appendChild(em2);
            }
            if (callback.getPathSecurity() != null) {
                PathSecurityType pathSecurityType = callback.getPathSecurity();
                Element em2 = createElementIso(document, "PathSecurity");
                Element em3 = createElementIso(document, "Protocol");
                em3.appendChild(document.createTextNode(pathSecurityType.getProtocol()));
                em2.appendChild(em3);
                if (pathSecurityType.getParameters() != null) {
                    em3 = createElementIso(document, "Parameters");
                    em3.appendChild(document.createTextNode(pathSecurityType.getParameters().toString()));
                    em2.appendChild(em3);
                }
                em.appendChild(em2);
            }
            rootElement.appendChild(em);
        }
    } else if (o instanceof Connect) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        Connect c = (Connect) o;
        Element em = createElementIso(document, "ContextHandle");
        em.appendChild(document.createTextNode(ByteUtils.toHexString(c.getContextHandle())));
        rootElement.appendChild(em);
        em = createElementIso(document, "IFDName");
        em.appendChild(document.createTextNode(c.getIFDName()));
        rootElement.appendChild(em);
        em = createElementIso(document, "Slot");
        em.appendChild(document.createTextNode(c.getSlot().toString()));
        rootElement.appendChild(em);
        if (c.isExclusive() != null) {
            em = createElementIso(document, "Exclusive");
            em.appendChild(document.createTextNode(c.isExclusive().toString()));
            rootElement.appendChild(em);
        }
    } else if (o instanceof ConnectResponse) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        ConnectResponse cr = (ConnectResponse) o;
        Element em = createElementIso(document, "SlotHandle");
        if (cr.getSlotHandle() != null) {
            em.appendChild(document.createTextNode(ByteUtils.toHexString(cr.getSlotHandle())));
            rootElement.appendChild(em);
        }
        em = marshalResult(cr.getResult(), document);
        rootElement.appendChild(em);
    } else if (o instanceof ListIFDs) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        ListIFDs c = (ListIFDs) o;
        Element em = createElementIso(document, "ContextHandle");
        em.appendChild(document.createTextNode(ByteUtils.toHexString(c.getContextHandle())));
        rootElement.appendChild(em);
    } else if (o instanceof ListIFDsResponse) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        ListIFDsResponse listIFDsResponse = (ListIFDsResponse) o;
        for (String s : listIFDsResponse.getIFDName()) {
            Element em = createElementIso(document, "IFDName");
            em.appendChild(document.createTextNode(s));
            rootElement.appendChild(em);
        }
        Element em = marshalResult(listIFDsResponse.getResult(), document);
        rootElement.appendChild(em);
    } else if (o instanceof Transmit) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        rootElement.setAttribute("xmlns:iso", "urn:iso:std:iso-iec:24727:tech:schema");
        Transmit t = (Transmit) o;
        Element em = createElementIso(document, "SlotHandle");
        em.appendChild(document.createTextNode(ByteUtils.toHexString(t.getSlotHandle())));
        rootElement.appendChild(em);
        for (int i = 0; i < t.getInputAPDUInfo().size(); i++) {
            em = createElementIso(document, "InputAPDUInfo");
            rootElement.appendChild(em);
            Element em2 = createElementIso(document, "InputAPDU");
            em2.appendChild(document.createTextNode(ByteUtils.toHexString(t.getInputAPDUInfo().get(i).getInputAPDU())));
            em.appendChild(em2);
            for (int y = 0; y < t.getInputAPDUInfo().get(i).getAcceptableStatusCode().size(); y++) {
                em2 = createElementIso(document, "AcceptableStatusCode");
                em2.appendChild(document.createTextNode(ByteUtils.toHexString(t.getInputAPDUInfo().get(i).getAcceptableStatusCode().get(y))));
                em.appendChild(em2);
            }
        }
    } else if (o instanceof RecognitionTree) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        RecognitionTree recognitionTree = (RecognitionTree) o;
        for (CardCall c : recognitionTree.getCardCall()) {
            rootElement.appendChild(marshalCardCall(c, document));
        }
    } else if (o instanceof CardCall) {
        CardCall c = (CardCall) o;
        rootElement = (Element) marshalCardCall(c, document);
    } else if (o instanceof Disconnect) {
        Disconnect d = (Disconnect) o;
        rootElement = createElementIso(document, d.getClass().getSimpleName());
        Element em = createElementIso(document, "SlotHandle");
        em.appendChild(document.createTextNode(ByteUtils.toHexString(d.getSlotHandle())));
        rootElement.appendChild(em);
        if (d.getAction() != null) {
            em = createElementIso(document, "Action");
            em.appendChild(document.createTextNode(d.getAction().value()));
        }
    } else if (o instanceof DisconnectResponse) {
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        DisconnectResponse response = (DisconnectResponse) o;
        String profile = response.getProfile();
        if (profile != null) {
            Element emProfile = createElementIso(document, "Profile");
            emProfile.appendChild(document.createTextNode(profile));
            rootElement.appendChild(emProfile);
        }
        String requestID = response.getRequestID();
        if (requestID != null) {
            Element emRequest = createElementIso(document, "RequestID");
            emRequest.appendChild(document.createElement(requestID));
            rootElement.appendChild(emRequest);
        }
        Element emResult = marshalResult(response.getResult(), document);
        rootElement.appendChild(emResult);
    } else if (o instanceof GetIFDCapabilities) {
        GetIFDCapabilities getIFDCapabilities = (GetIFDCapabilities) o;
        rootElement = createElementIso(document, o.getClass().getSimpleName());
        Element emContextHandle = createElementIso(document, "ContextHandle");
        emContextHandle.appendChild(document.createTextNode(ByteUtils.toHexString(getIFDCapabilities.getContextHandle())));
        rootElement.appendChild(emContextHandle);
        Element emIFDName = createElementIso(document, "IFDName");
        emIFDName.appendChild(document.createTextNode(getIFDCapabilities.getIFDName()));
        rootElement.appendChild(emIFDName);
    } else if (o instanceof GetIFDCapabilitiesResponse) {
        try {
            GetIFDCapabilitiesResponse response = (GetIFDCapabilitiesResponse) o;
            rootElement = createElementIso(document, o.getClass().getSimpleName());
            String profile = response.getProfile();
            if (profile != null) {
                Element emProfile = createElementIso(document, "Profile");
                emProfile.appendChild(document.createTextNode(profile));
                rootElement.appendChild(emProfile);
            }
            String requestID = response.getRequestID();
            if (requestID != null) {
                Element emRequest = createElementIso(document, "RequestID");
                emRequest.appendChild(document.createElement(requestID));
                rootElement.appendChild(emRequest);
            }
            Element emResult = marshalResult(response.getResult(), document);
            rootElement.appendChild(emResult);
            if (response.getIFDCapabilities() != null) {
                Element emIFDCaps = marshalIFDCapabilities(response.getIFDCapabilities(), document);
                rootElement.appendChild(emIFDCaps);
            }
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
        }
    } else if (o instanceof TCTokenType) {
        TCTokenType tctoken = (TCTokenType) o;
        rootElement = document.createElement(o.getClass().getSimpleName());
        Element em = document.createElement("ServerAddress");
        em.appendChild(document.createTextNode(tctoken.getServerAddress()));
        rootElement.appendChild(em);
        em = document.createElement("SessionIdentifier");
        em.appendChild(document.createTextNode(tctoken.getSessionIdentifier()));
        rootElement.appendChild(em);
        em = document.createElement("RefreshAddress");
        em.appendChild(document.createTextNode(tctoken.getRefreshAddress()));
        rootElement.appendChild(em);
        // Optional element CommunicationErrorAddress
        String communicationErrorAddress = tctoken.getCommunicationErrorAddress();
        if (communicationErrorAddress != null) {
            em = document.createElement("CommunicationErrorAddress");
            em.appendChild(document.createTextNode(tctoken.getCommunicationErrorAddress()));
            rootElement.appendChild(em);
        }
        em = document.createElement("Binding");
        em.appendChild(document.createTextNode(tctoken.getBinding()));
        rootElement.appendChild(em);
        // Optional element PathSecurity-Protocol
        String pathSecurityProtocol = tctoken.getPathSecurityProtocol();
        if (pathSecurityProtocol != null) {
            em = document.createElement("PathSecurity-Protocol");
            em.appendChild(document.createTextNode(tctoken.getPathSecurityProtocol()));
            rootElement.appendChild(em);
        }
        // Optional element PathSecurity-Parameters
        TCTokenType.PathSecurityParameters pathSecurityParameters = tctoken.getPathSecurityParameters();
        if (pathSecurityParameters != null) {
            em = document.createElement("PSK");
            em.appendChild(document.createTextNode(ByteUtils.toHexString(pathSecurityParameters.getPSK())));
            Element em1 = document.createElement("PathSecurity-Parameters");
            em1.appendChild(em);
            rootElement.appendChild(em1);
        }
    } else if (o instanceof CardApplicationPath) {
        CardApplicationPath p = (CardApplicationPath) o;
        rootElement = createElementIso(document, "CardApplicationPath");
        Element em = createElementIso(document, "CardAppPathRequest");
        String profile = p.getProfile();
        if (profile != null) {
            Element emProfile = createElementIso(document, "Profile");
            emProfile.appendChild(document.createTextNode(profile));
            rootElement.appendChild(emProfile);
        }
        String requestID = p.getRequestID();
        if (requestID != null) {
            Element emRequest = createElementIso(document, "RequestID");
            emRequest.appendChild(document.createElement(requestID));
            rootElement.appendChild(emRequest);
        }
        // ChannelHandle
        ChannelHandleType h = p.getCardAppPathRequest().getChannelHandle();
        Element emChild = createElementIso(document, "ChannelHandle");
        em.appendChild(emChild);
        Element emChildOfCH;
        if (h.getProtocolTerminationPoint() != null) {
            emChildOfCH = createElementIso(document, "ProtocolTerminationPoint");
            emChildOfCH.appendChild(document.createTextNode(h.getProtocolTerminationPoint()));
            emChild.appendChild(emChildOfCH);
        }
        if (h.getSessionIdentifier() != null) {
            emChildOfCH = createElementIso(document, "SessionIdentifier");
            emChildOfCH.appendChild(document.createTextNode(h.getSessionIdentifier()));
            emChild.appendChild(emChildOfCH);
        }
        if (h.getBinding() != null) {
            emChildOfCH = createElementIso(document, "Binding");
            emChildOfCH.appendChild(document.createTextNode(h.getBinding()));
            emChild.appendChild(emChildOfCH);
        }
        PathSecurityType ps = h.getPathSecurity();
        if (ps != null) {
            emChildOfCH = createElementIso(document, "PathSecurity");
            Element emChildOfPS = createElementIso(document, "Protocol");
            emChildOfPS.appendChild(document.createTextNode(ps.getProtocol()));
            emChildOfCH.appendChild(emChildOfPS);
            // TODO here any type parsen
            LOG.error("AnyType of CardApplicationPath: " + ps.getParameters().toString());
            emChild.appendChild(emChildOfCH);
        }
        // context handle
        emChild = createElementIso(document, "ContextHandle");
        emChild.appendChild(document.createTextNode(ByteUtils.toHexString(p.getCardAppPathRequest().getContextHandle())));
        em.appendChild(emChild);
        // IFDName
        emChild = createElementIso(document, "IFDName");
        emChild.appendChild(document.createTextNode(p.getCardAppPathRequest().getIFDName()));
        em.appendChild(emChild);
        // SlotIndex
        emChild = createElementIso(document, "SlotIndex");
        emChild.appendChild(document.createTextNode(p.getCardAppPathRequest().getSlotIndex().toString()));
        em.appendChild(emChild);
        // Card Application
        emChild = createElementIso(document, "CardApplication");
        emChild.appendChild(document.createTextNode(ByteUtils.toHexString(p.getCardAppPathRequest().getCardApplication())));
        em.appendChild(emChild);
        rootElement.appendChild(em);
    } else if (o instanceof CardApplicationPathResponse) {
        CardApplicationPathResponse resp = (CardApplicationPathResponse) o;
        rootElement = createElementIso(document, "CardApplicationPathResponse");
        String profile = resp.getProfile();
        if (profile != null) {
            Element emProfile = createElementIso(document, "Profile");
            emProfile.appendChild(document.createTextNode(profile));
            rootElement.appendChild(emProfile);
        }
        String requestID = resp.getRequestID();
        if (requestID != null) {
            Element emRequest = createElementIso(document, "RequestID");
            emRequest.appendChild(document.createElement(requestID));
            rootElement.appendChild(emRequest);
        }
        Result result = resp.getResult();
        if (result != null) {
            Element emResult = marshalResult(resp.getResult(), document);
            rootElement.appendChild(emResult);
        }
        Element em = createElementIso(document, "CardAppPathResultSet");
        for (CardApplicationPathType path : resp.getCardAppPathResultSet().getCardApplicationPathResult()) {
            em.appendChild(marshalCardApplicationPathResult(path, document, "CardAppPathRequest"));
        }
        rootElement.appendChild(em);
    } else if (o instanceof BeginTransaction) {
        BeginTransaction t = (BeginTransaction) o;
        rootElement = createElementIso(document, "BeginTransaction");
        Element em = createElementIso(document, "SlotHandle");
        em.appendChild(document.createTextNode(ByteUtils.toHexString(t.getSlotHandle())));
        rootElement.appendChild(em);
    } else if (o instanceof BeginTransactionResponse) {
        BeginTransactionResponse response = (BeginTransactionResponse) o;
        rootElement = createElementIso(document, "BeginTransactionResponse");
        String profile = response.getProfile();
        if (profile != null) {
            Element emProfile = createElementIso(document, "Profile");
            emProfile.appendChild(document.createTextNode(profile));
            rootElement.appendChild(emProfile);
        }
        String requestID = response.getRequestID();
        if (requestID != null) {
            Element emRequest = createElementIso(document, "RequestID");
            emRequest.appendChild(document.createElement(requestID));
            rootElement.appendChild(emRequest);
        }
        Element emResult = marshalResult(response.getResult(), document);
        rootElement.appendChild(emResult);
    } else if (o instanceof EndTransaction) {
        EndTransaction end = (EndTransaction) o;
        rootElement = createElementIso(document, "EndTransaction");
        Element em = createElementIso(document, "SlotHandle");
        em.appendChild(document.createTextNode(ByteUtils.toHexString(end.getSlotHandle())));
        rootElement.appendChild(em);
    } else if (o instanceof EndTransactionResponse) {
        EndTransactionResponse response = (EndTransactionResponse) o;
        rootElement = createElementIso(document, "EndTransactionResponse");
        String profile = response.getProfile();
        if (profile != null) {
            Element emProfile = createElementIso(document, "Profile");
            emProfile.appendChild(document.createTextNode(profile));
            rootElement.appendChild(emProfile);
        }
        String requestID = response.getRequestID();
        if (requestID != null) {
            Element emRequest = createElementIso(document, "RequestID");
            emRequest.appendChild(document.createElement(requestID));
            rootElement.appendChild(emRequest);
        }
        Element emResult = marshalResult(response.getResult(), document);
        rootElement.appendChild(emResult);
    } else if (o instanceof CardApplicationConnect) {
        CardApplicationConnect c = (CardApplicationConnect) o;
        rootElement = createElementIso(document, "CardApplicationConnect");
        // Card Application Path
        rootElement.appendChild(marshalCardApplicationPathResult(c.getCardApplicationPath(), document, "CardApplicationPath"));
        Element em;
        if (c.getOutput() != null) {
            em = createElementIso(document, "Output");
            em.appendChild(marshalOutput(c.getOutput(), document));
            rootElement.appendChild(em);
        }
        // Exclusive Use
        if (c.isExclusiveUse() != null) {
            em = createElementIso(document, "ExclusiveUse");
            em.appendChild(document.createTextNode(Boolean.toString(c.isExclusiveUse())));
            rootElement.appendChild(em);
        }
    } else if (o instanceof CardApplicationConnectResponse) {
        CardApplicationConnectResponse resp = (CardApplicationConnectResponse) o;
        rootElement = createElementIso(document, "CardApplicationConnectResponse");
        appendResponseValues(resp.getProfile(), resp.getRequestID(), resp.getResult(), rootElement, document);
        ConnectionHandleType ch = resp.getConnectionHandle();
        if (ch != null) {
            Element em = marshalConnectionHandle(ch, document);
            rootElement.appendChild(em);
        }
    } else if (o instanceof CardApplicationDisconnect) {
        CardApplicationDisconnect c = (CardApplicationDisconnect) o;
        rootElement = createElementIso(document, "CardApplicationDisconnect");
        if (c.getConnectionHandle() != null) {
            rootElement.appendChild(marshalConnectionHandle(c.getConnectionHandle(), document));
        }
        if (c.getAction() != null) {
            Element em = createElementIso(document, "Action");
            em.appendChild(document.createTextNode(c.getAction().value()));
            rootElement.appendChild(em);
        }
        String profile = c.getProfile();
        if (profile != null) {
            Element emProfile = createElementIso(document, "Profile");
            emProfile.appendChild(document.createTextNode(profile));
            rootElement.appendChild(emProfile);
        }
        String requestID = c.getRequestID();
        if (requestID != null) {
            Element emRequest = createElementIso(document, "RequestID");
            emRequest.appendChild(document.createElement(requestID));
            rootElement.appendChild(emRequest);
        }
    } else if (o instanceof CardApplicationDisconnectResponse) {
        CardApplicationDisconnectResponse response = (CardApplicationDisconnectResponse) o;
        rootElement = createElementIso(document, "CardApplicationDisconnectResponse");
        String profile = response.getProfile();
        if (profile != null) {
            Element emProfile = createElementIso(document, "Profile");
            emProfile.appendChild(document.createTextNode(profile));
            rootElement.appendChild(emProfile);
        }
        String requestID = response.getRequestID();
        if (requestID != null) {
            Element emRequest = createElementIso(document, "RequestID");
            emRequest.appendChild(document.createElement(requestID));
            rootElement.appendChild(emRequest);
        }
        if (response.getResult() != null) {
            Element emResult = marshalResult(response.getResult(), document);
            rootElement.appendChild(emResult);
        }
    } else {
        throw new IllegalArgumentException("Cannot marshal " + o.getClass().getSimpleName());
    }
    document.appendChild(rootElement);
    return document;
}
Also used : TCTokenType(generated.TCTokenType) ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) MarshallingTypeException(org.openecard.ws.marshal.MarshallingTypeException) LocalizedString(org.openecard.addon.manifest.LocalizedString) ChannelHandleType(iso.std.iso_iec._24727.tech.schema.ChannelHandleType) Document(org.w3c.dom.Document) StartPAOS(iso.std.iso_iec._24727.tech.schema.StartPAOS) StreamResult(javax.xml.transform.stream.StreamResult) Result(oasis.names.tc.dss._1_0.core.schema.Result) GetIFDCapabilities(iso.std.iso_iec._24727.tech.schema.GetIFDCapabilities) BeginTransaction(iso.std.iso_iec._24727.tech.schema.BeginTransaction) PathSecurityType(iso.std.iso_iec._24727.tech.schema.PathSecurityType) CardApplicationConnect(iso.std.iso_iec._24727.tech.schema.CardApplicationConnect) EstablishContext(iso.std.iso_iec._24727.tech.schema.EstablishContext) GetStatus(iso.std.iso_iec._24727.tech.schema.GetStatus) EndTransaction(iso.std.iso_iec._24727.tech.schema.EndTransaction) CardApplicationPathResponse(iso.std.iso_iec._24727.tech.schema.CardApplicationPathResponse) EstablishChannelResponse(iso.std.iso_iec._24727.tech.schema.EstablishChannelResponse) CardApplicationConnect(iso.std.iso_iec._24727.tech.schema.CardApplicationConnect) Connect(iso.std.iso_iec._24727.tech.schema.Connect) DIDAuthenticationDataType(iso.std.iso_iec._24727.tech.schema.DIDAuthenticationDataType) EstablishContextResponse(iso.std.iso_iec._24727.tech.schema.EstablishContextResponse) DestroyChannelResponse(iso.std.iso_iec._24727.tech.schema.DestroyChannelResponse) BeginTransactionResponse(iso.std.iso_iec._24727.tech.schema.BeginTransactionResponse) EstablishChannel(iso.std.iso_iec._24727.tech.schema.EstablishChannel) EAC2InputType(iso.std.iso_iec._24727.tech.schema.EAC2InputType) GetIFDCapabilitiesResponse(iso.std.iso_iec._24727.tech.schema.GetIFDCapabilitiesResponse) Wait(iso.std.iso_iec._24727.tech.schema.Wait) Map(java.util.Map) EAC2OutputType(iso.std.iso_iec._24727.tech.schema.EAC2OutputType) ListIFDs(iso.std.iso_iec._24727.tech.schema.ListIFDs) CardApplicationDisconnect(iso.std.iso_iec._24727.tech.schema.CardApplicationDisconnect) ConnectResponse(iso.std.iso_iec._24727.tech.schema.ConnectResponse) CardApplicationConnectResponse(iso.std.iso_iec._24727.tech.schema.CardApplicationConnectResponse) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) EndTransactionResponse(iso.std.iso_iec._24727.tech.schema.EndTransactionResponse) EAC1OutputType(iso.std.iso_iec._24727.tech.schema.EAC1OutputType) CardApplicationPathType(iso.std.iso_iec._24727.tech.schema.CardApplicationPathType) StartPAOS(iso.std.iso_iec._24727.tech.schema.StartPAOS) Disconnect(iso.std.iso_iec._24727.tech.schema.Disconnect) CardApplicationDisconnect(iso.std.iso_iec._24727.tech.schema.CardApplicationDisconnect) DisconnectResponse(iso.std.iso_iec._24727.tech.schema.DisconnectResponse) CardApplicationDisconnectResponse(iso.std.iso_iec._24727.tech.schema.CardApplicationDisconnectResponse) DestroyChannel(iso.std.iso_iec._24727.tech.schema.DestroyChannel) EAC1InputType(iso.std.iso_iec._24727.tech.schema.EAC1InputType) RecognitionTree(iso.std.iso_iec._24727.tech.schema.RecognitionTree) DIDAuthenticate(iso.std.iso_iec._24727.tech.schema.DIDAuthenticate) Transmit(iso.std.iso_iec._24727.tech.schema.Transmit) ListIFDsResponse(iso.std.iso_iec._24727.tech.schema.ListIFDsResponse) QName(javax.xml.namespace.QName) CardApplicationDisconnectResponse(iso.std.iso_iec._24727.tech.schema.CardApplicationDisconnectResponse) CardApplicationConnectResponse(iso.std.iso_iec._24727.tech.schema.CardApplicationConnectResponse) InternationalStringType(oasis.names.tc.dss._1_0.core.schema.InternationalStringType) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) MarshallingTypeException(org.openecard.ws.marshal.MarshallingTypeException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) RuntimeCryptoException(org.openecard.bouncycastle.crypto.RuntimeCryptoException) WSMarshallerException(org.openecard.ws.marshal.WSMarshallerException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SOAPException(org.openecard.ws.soap.SOAPException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DIDAuthenticateResponse(iso.std.iso_iec._24727.tech.schema.DIDAuthenticateResponse) CardApplicationPath(iso.std.iso_iec._24727.tech.schema.CardApplicationPath) InitializeFrameworkResponse(de.bund.bsi.ecard.api._1.InitializeFrameworkResponse) TransmitResponse(iso.std.iso_iec._24727.tech.schema.TransmitResponse) CardCall(iso.std.iso_iec._24727.tech.schema.CardCall)

Example 2 with EAC2OutputType

use of iso.std.iso_iec._24727.tech.schema.EAC2OutputType in project open-ecard by ecsec.

the class MarshalTest method testConversionOfDIDAuthenticatResponseAndInitializeFrameworkResponse.

@Test
public void testConversionOfDIDAuthenticatResponseAndInitializeFrameworkResponse() throws MarshallingTypeException, TransformerException, SOAPException, ParserConfigurationException {
    JAXBMarshaller m = new JAXBMarshaller();
    DIDAuthenticateResponse didAuthenticateResponse = new DIDAuthenticateResponse();
    Result r = new Result();
    r.setResultMajor("major");
    r.setResultMinor("minor");
    InternationalStringType internationalStringType = new InternationalStringType();
    internationalStringType.setLang("en");
    internationalStringType.setValue("message");
    r.setResultMessage(internationalStringType);
    didAuthenticateResponse.setResult(r);
    EAC2OutputType didAuthenticationDataType = new EAC2OutputType();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document d = builder.newDocument();
    Element e = d.createElementNS("urn:iso:std:iso-iec:24727:tech:schema", "Signature");
    e.setTextContent("7117D7BF95D8D6BD437A0D43DE48F42528273A98F2605758D6A3A2BFC38141E7577CABB4F8FBC8DF152E3A097D1B3A703597331842425FE4A9D0F1C9067AC4A9");
    didAuthenticationDataType.getAny().add(e);
    didAuthenticateResponse.setAuthenticationProtocolData(didAuthenticationDataType);
    Document doc = m.marshal(didAuthenticateResponse);
    // test ok if this works
    System.out.println(m.doc2str(doc));
    InitializeFrameworkResponse initializeFrameworkResponse = new InitializeFrameworkResponse();
    Version version = new Version();
    version.setMajor(new BigInteger("11"));
    version.setMinor(new BigInteger("22"));
    version.setSubMinor(new BigInteger("33"));
    initializeFrameworkResponse.setVersion(version);
    r.setResultMessage(internationalStringType);
    initializeFrameworkResponse.setResult(r);
    doc = m.marshal(initializeFrameworkResponse);
    // test ok if this works
    System.out.println(m.doc2str(doc));
}
Also used : DIDAuthenticateResponse(iso.std.iso_iec._24727.tech.schema.DIDAuthenticateResponse) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Version(de.bund.bsi.ecard.api._1.InitializeFrameworkResponse.Version) InitializeFrameworkResponse(de.bund.bsi.ecard.api._1.InitializeFrameworkResponse) Element(org.w3c.dom.Element) BigInteger(java.math.BigInteger) Document(org.w3c.dom.Document) InternationalStringType(oasis.names.tc.dss._1_0.core.schema.InternationalStringType) Result(oasis.names.tc.dss._1_0.core.schema.Result) EAC2OutputType(iso.std.iso_iec._24727.tech.schema.EAC2OutputType) Test(org.testng.annotations.Test)

Example 3 with EAC2OutputType

use of iso.std.iso_iec._24727.tech.schema.EAC2OutputType in project open-ecard by ecsec.

the class TerminalAuthenticationStep method perform.

@Override
public DIDAuthenticateResponse perform(DIDAuthenticate didAuthenticate, Map<String, Object> internalData) {
    DIDAuthenticateResponse response = new DIDAuthenticateResponse();
    DynamicContext dynCtx = DynamicContext.getInstance(TR03112Keys.INSTANCE_KEY);
    try {
        ObjectSchemaValidator valid = (ObjectSchemaValidator) dynCtx.getPromise(EACProtocol.SCHEMA_VALIDATOR).deref();
        boolean messageValid = valid.validateObject(didAuthenticate);
        if (!messageValid) {
            String msg = "Validation of the EAC2InputType message failed.";
            logger.error(msg);
            dynCtx.put(EACProtocol.AUTHENTICATION_FAILED, true);
            response.setResult(WSHelper.makeResultError(ECardConstants.Minor.App.INCORRECT_PARM, msg));
            return response;
        }
    } catch (ObjectValidatorException ex) {
        String msg = "Validation of the EAC2InputType message failed due to invalid input data.";
        logger.error(msg, ex);
        dynCtx.put(EACProtocol.AUTHENTICATION_FAILED, true);
        response.setResult(WSHelper.makeResultError(ECardConstants.Minor.App.INT_ERROR, msg));
        return response;
    } catch (InterruptedException ex) {
        String msg = "Thread interrupted while waiting for schema validator instance.";
        logger.error(msg, ex);
        dynCtx.put(EACProtocol.AUTHENTICATION_FAILED, true);
        response.setResult(WSHelper.makeResultError(ECardConstants.Minor.App.INT_ERROR, msg));
        return response;
    }
    byte[] slotHandle = didAuthenticate.getConnectionHandle().getSlotHandle();
    try {
        EAC2InputType eac2Input = new EAC2InputType(didAuthenticate.getAuthenticationProtocolData());
        EAC2OutputType eac2Output = eac2Input.getOutputType();
        TerminalAuthentication ta = new TerminalAuthentication(dispatcher, slotHandle);
        // Build certificate chain
        CardVerifiableCertificateChain certificateChain;
        certificateChain = (CardVerifiableCertificateChain) internalData.get(EACConstants.IDATA_CERTIFICATES);
        certificateChain.addCertificates(eac2Input.getCertificates());
        byte[] currentCAR = (byte[]) internalData.get(EACConstants.IDATA_CURRENT_CAR);
        byte[] previousCAR = (byte[]) internalData.get(EACConstants.IDATA_PREVIOUS_CAR);
        CardVerifiableCertificateChain tmpChain = certificateChain.getCertificateChainFromCAR(currentCAR);
        // try again with previous car if it didn't work
        if (tmpChain.getCertificates().isEmpty() && previousCAR != null) {
            tmpChain = certificateChain.getCertificateChainFromCAR(previousCAR);
        }
        certificateChain = tmpChain;
        if (certificateChain.getCertificates().isEmpty()) {
            String msg = "Failed to create a valid certificate chain from the transmitted certificates.";
            logger.error(msg);
            response.setResult(WSHelper.makeResultError(ECardConstants.Minor.App.INCORRECT_PARM, msg));
            return response;
        }
        // TA: Step 1 - Verify certificates
        ta.verifyCertificates(certificateChain);
        // save values for later use
        CardVerifiableCertificate terminalCertificate = certificateChain.getTerminalCertificate();
        byte[] key = eac2Input.getEphemeralPublicKey();
        byte[] signature = eac2Input.getSignature();
        internalData.put(EACConstants.IDATA_PK_PCD, key);
        internalData.put(EACConstants.IDATA_SIGNATURE, signature);
        internalData.put(EACConstants.IDATA_TERMINAL_CERTIFICATE, terminalCertificate);
        if (signature != null) {
            logger.trace("Signature has been provided in EAC2InputType.");
            // perform TA and CA authentication
            ChipAuthentication ca = new ChipAuthentication(dispatcher, slotHandle);
            AuthenticationHelper auth = new AuthenticationHelper(ta, ca);
            eac2Output = auth.performAuth(eac2Output, internalData);
            // no third step needed, notify GUI
            DynamicContext ctx = DynamicContext.getInstance(TR03112Keys.INSTANCE_KEY);
            ctx.put(EACProtocol.AUTHENTICATION_DONE, true);
        } else {
            logger.trace("Signature has not been provided in EAC2InputType.");
            // send challenge again
            byte[] rPICC = (byte[]) internalData.get(EACConstants.IDATA_CHALLENGE);
            eac2Output.setChallenge(rPICC);
        }
        response.setResult(WSHelper.makeResultOK());
        response.setAuthenticationProtocolData(eac2Output.getAuthDataType());
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        response.setResult(WSHelper.makeResultUnknownError(e.getMessage()));
        dynCtx.put(EACProtocol.AUTHENTICATION_FAILED, true);
    }
    Promise<Object> p = (Promise<Object>) dynCtx.getPromise(TR03112Keys.PROCESSING_CANCELLATION);
    if (p.derefNonblocking() == null) {
        return response;
    } else {
        response = new DIDAuthenticateResponse();
        String msg = "Authentication Canceled by the user.";
        response.setResult(WSHelper.makeResultError(ECardConstants.Minor.SAL.CANCELLATION_BY_USER, msg));
        return response;
    }
}
Also used : ObjectValidatorException(org.openecard.common.interfaces.ObjectValidatorException) Promise(org.openecard.common.util.Promise) DIDAuthenticateResponse(iso.std.iso_iec._24727.tech.schema.DIDAuthenticateResponse) ObjectValidatorException(org.openecard.common.interfaces.ObjectValidatorException) EAC2InputType(org.openecard.sal.protocol.eac.anytype.EAC2InputType) CardVerifiableCertificate(org.openecard.crypto.common.asn1.cvc.CardVerifiableCertificate) ObjectSchemaValidator(org.openecard.common.interfaces.ObjectSchemaValidator) CardVerifiableCertificateChain(org.openecard.crypto.common.asn1.cvc.CardVerifiableCertificateChain) DynamicContext(org.openecard.common.DynamicContext) EAC2OutputType(org.openecard.sal.protocol.eac.anytype.EAC2OutputType)

Example 4 with EAC2OutputType

use of iso.std.iso_iec._24727.tech.schema.EAC2OutputType in project open-ecard by ecsec.

the class ChipAuthenticationStep method perform.

@Override
public DIDAuthenticateResponse perform(DIDAuthenticate didAuthenticate, Map<String, Object> internalData) {
    DIDAuthenticateResponse response = new DIDAuthenticateResponse();
    byte[] slotHandle = didAuthenticate.getConnectionHandle().getSlotHandle();
    DynamicContext dynCtx = DynamicContext.getInstance(TR03112Keys.INSTANCE_KEY);
    try {
        ObjectSchemaValidator valid = (ObjectSchemaValidator) dynCtx.getPromise(EACProtocol.SCHEMA_VALIDATOR).deref();
        boolean messageValid = valid.validateObject(didAuthenticate);
        if (!messageValid) {
            String msg = "Validation of the EACAdditionalInputType message failed.";
            logger.error(msg);
            dynCtx.put(EACProtocol.AUTHENTICATION_FAILED, true);
            response.setResult(WSHelper.makeResultError(ECardConstants.Minor.App.INCORRECT_PARM, msg));
            return response;
        }
    } catch (ObjectValidatorException ex) {
        String msg = "Validation of the EACAdditionalInputType message failed due to invalid input data.";
        logger.error(msg, ex);
        dynCtx.put(EACProtocol.AUTHENTICATION_FAILED, true);
        response.setResult(WSHelper.makeResultError(ECardConstants.Minor.App.INT_ERROR, msg));
        return response;
    } catch (InterruptedException ex) {
        String msg = "Thread interrupted while waiting for schema validator instance.";
        logger.error(msg, ex);
        dynCtx.put(EACProtocol.AUTHENTICATION_FAILED, true);
        response.setResult(WSHelper.makeResultError(ECardConstants.Minor.App.INT_ERROR, msg));
        return response;
    }
    try {
        EACAdditionalInputType eacAdditionalInput = new EACAdditionalInputType(didAuthenticate.getAuthenticationProtocolData());
        EAC2OutputType eac2Output = eacAdditionalInput.getOutputType();
        TerminalAuthentication ta = new TerminalAuthentication(dispatcher, slotHandle);
        ChipAuthentication ca = new ChipAuthentication(dispatcher, slotHandle);
        // save signature, it is needed in the authentication step
        byte[] signature = eacAdditionalInput.getSignature();
        internalData.put(EACConstants.IDATA_SIGNATURE, signature);
        // perform TA and CA authentication
        AuthenticationHelper auth = new AuthenticationHelper(ta, ca);
        eac2Output = auth.performAuth(eac2Output, internalData);
        response.setResult(WSHelper.makeResultOK());
        response.setAuthenticationProtocolData(eac2Output.getAuthDataType());
    } catch (ParserConfigurationException | ProtocolException | TLVException e) {
        logger.error(e.getMessage(), e);
        response.setResult(WSHelper.makeResultUnknownError(e.getMessage()));
        dynCtx.put(EACProtocol.AUTHENTICATION_FAILED, true);
    }
    Promise<Object> p = (Promise<Object>) dynCtx.getPromise(TR03112Keys.PROCESSING_CANCELLATION);
    if (p.derefNonblocking() == null) {
        // authentication finished, notify GUI
        dynCtx.put(EACProtocol.AUTHENTICATION_DONE, true);
        return response;
    } else {
        // authentication finished, notify GUI
        dynCtx.put(EACProtocol.AUTHENTICATION_DONE, false);
        response = new DIDAuthenticateResponse();
        String msg = "Authentication canceled by the user.";
        response.setResult(WSHelper.makeResultError(ECardConstants.Minor.SAL.CANCELLATION_BY_USER, msg));
        return response;
    }
}
Also used : ProtocolException(org.openecard.common.sal.protocol.exception.ProtocolException) TLVException(org.openecard.common.tlv.TLVException) EACAdditionalInputType(org.openecard.sal.protocol.eac.anytype.EACAdditionalInputType) Promise(org.openecard.common.util.Promise) DIDAuthenticateResponse(iso.std.iso_iec._24727.tech.schema.DIDAuthenticateResponse) ObjectValidatorException(org.openecard.common.interfaces.ObjectValidatorException) ObjectSchemaValidator(org.openecard.common.interfaces.ObjectSchemaValidator) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DynamicContext(org.openecard.common.DynamicContext) EAC2OutputType(org.openecard.sal.protocol.eac.anytype.EAC2OutputType)

Example 5 with EAC2OutputType

use of iso.std.iso_iec._24727.tech.schema.EAC2OutputType in project open-ecard by ecsec.

the class AndroidMarshallerTest method testConversionOfDIDAuthenticateResponseTA.

@Test
public void testConversionOfDIDAuthenticateResponseTA() throws Exception {
    WSMarshaller m = new AndroidMarshaller();
    DIDAuthenticateResponse didAuthResponse = new DIDAuthenticateResponse();
    Result r = new Result();
    r.setResultMajor("major");
    r.setResultMinor("minor");
    InternationalStringType internationalStringType = new InternationalStringType();
    internationalStringType.setLang("en");
    internationalStringType.setValue("message");
    r.setResultMessage(internationalStringType);
    didAuthResponse.setResult(r);
    EAC2OutputType didAuthenticationDataType = new EAC2OutputType();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document d = builder.newDocument();
    Element e = d.createElementNS("urn:iso:std:iso-iec:24727:tech:schema", "Challenge");
    e.setTextContent("1331F2B1571E6DC2");
    didAuthenticationDataType.getAny().add(e);
    didAuthResponse.setAuthenticationProtocolData(didAuthenticationDataType);
    marshalLog(didAuthResponse);
    Document doc = m.marshal(didAuthResponse);
    String s = m.doc2str(doc);
    LOG.debug(s);
    StringReader sr = new StringReader(s);
    DIDAuthenticateResponse didaresp = JAXB.unmarshal(sr, DIDAuthenticateResponse.class);
    marshalLog(didaresp);
}
Also used : DIDAuthenticateResponse(iso.std.iso_iec._24727.tech.schema.DIDAuthenticateResponse) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) WSMarshaller(org.openecard.ws.marshal.WSMarshaller) Document(org.w3c.dom.Document) InternationalStringType(oasis.names.tc.dss._1_0.core.schema.InternationalStringType) Result(oasis.names.tc.dss._1_0.core.schema.Result) EAC2OutputType(iso.std.iso_iec._24727.tech.schema.EAC2OutputType) Test(org.testng.annotations.Test)

Aggregations

DIDAuthenticateResponse (iso.std.iso_iec._24727.tech.schema.DIDAuthenticateResponse)6 EAC2OutputType (iso.std.iso_iec._24727.tech.schema.EAC2OutputType)4 InternationalStringType (oasis.names.tc.dss._1_0.core.schema.InternationalStringType)4 Result (oasis.names.tc.dss._1_0.core.schema.Result)4 Document (org.w3c.dom.Document)4 Element (org.w3c.dom.Element)4 InitializeFrameworkResponse (de.bund.bsi.ecard.api._1.InitializeFrameworkResponse)2 StringReader (java.io.StringReader)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 DynamicContext (org.openecard.common.DynamicContext)2 ObjectSchemaValidator (org.openecard.common.interfaces.ObjectSchemaValidator)2 ObjectValidatorException (org.openecard.common.interfaces.ObjectValidatorException)2 Promise (org.openecard.common.util.Promise)2 EAC2OutputType (org.openecard.sal.protocol.eac.anytype.EAC2OutputType)2 WSMarshaller (org.openecard.ws.marshal.WSMarshaller)2 Test (org.testng.annotations.Test)2 Version (de.bund.bsi.ecard.api._1.InitializeFrameworkResponse.Version)1 TCTokenType (generated.TCTokenType)1