Search in sources :

Example 71 with ConnectionHandleType

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

the class VerifySignatureStep method perform.

@Override
public VerifySignatureResponse perform(VerifySignature request, Map<String, Object> internalData) {
    VerifySignatureResponse response = WSHelper.makeResponse(VerifySignatureResponse.class, WSHelper.makeResultOK());
    try {
        ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
        CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(internalData, connectionHandle);
        String didName = SALUtils.getDIDName(request);
        DIDStructureType didStructure = SALUtils.getDIDStructure(request, didName, cardStateEntry, connectionHandle);
        // required
        byte[] signature = request.getSignature();
        // optional
        byte[] message = request.getMessage();
        CryptoMarkerType cryptoMarker = new CryptoMarkerType(didStructure.getDIDMarker());
        String dataSetNameCertificate = cryptoMarker.getCertificateRefs().get(0).getDataSetName();
        String algorithmIdentifier = cryptoMarker.getAlgorithmInfo().getAlgorithmIdentifier().getAlgorithm();
        DSIRead dsiRead = new DSIRead();
        dsiRead.setConnectionHandle(connectionHandle);
        dsiRead.setDSIName(dataSetNameCertificate);
        DSIReadResponse dsiReadResponse = (DSIReadResponse) dispatcher.safeDeliver(dsiRead);
        WSHelper.checkResult(dsiReadResponse);
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(dsiReadResponse.getDSIContent()));
        Signature signatureAlgorithm;
        if (algorithmIdentifier.equals(GenericCryptoUris.RSA_ENCRYPTION)) {
            signatureAlgorithm = Signature.getInstance("RSA", new BouncyCastleProvider());
        } else if (algorithmIdentifier.equals(GenericCryptoUris.RSASSA_PSS_SHA256)) {
            signatureAlgorithm = Signature.getInstance("RAWRSASSA-PSS", new BouncyCastleProvider());
            signatureAlgorithm.setParameter(new PSSParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), 32, 1));
        } else if (algorithmIdentifier.equals(GenericCryptoUris.sigS_ISO9796_2)) {
            return WSHelper.makeResponse(VerifySignatureResponse.class, WSHelper.makeResultUnknownError(algorithmIdentifier + " Not supported yet."));
        } else if (algorithmIdentifier.equals(GenericCryptoUris.sigS_ISO9796_2rnd)) {
            return WSHelper.makeResponse(VerifySignatureResponse.class, WSHelper.makeResultUnknownError(algorithmIdentifier + " Not supported yet."));
        } else {
            throw new IncorrectParameterException("Unknown signature algorithm.");
        }
        signatureAlgorithm.initVerify(cert);
        if (message != null) {
            signatureAlgorithm.update(message);
        }
        if (!signatureAlgorithm.verify(signature)) {
            throw new InvalidSignatureException();
        }
    } catch (ECardException e) {
        LOG.error(e.getMessage(), e);
        response.setResult(e.getResult());
    } catch (Exception e) {
        response.setResult(WSHelper.makeResult(e));
    }
    return response;
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) CardStateEntry(org.openecard.common.sal.state.CardStateEntry) InvalidSignatureException(org.openecard.common.sal.exception.InvalidSignatureException) DSIRead(iso.std.iso_iec._24727.tech.schema.DSIRead) CryptoMarkerType(org.openecard.crypto.common.sal.did.CryptoMarkerType) VerifySignatureResponse(iso.std.iso_iec._24727.tech.schema.VerifySignatureResponse) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) ECardException(org.openecard.common.ECardException) IncorrectParameterException(org.openecard.common.sal.exception.IncorrectParameterException) InvalidSignatureException(org.openecard.common.sal.exception.InvalidSignatureException) ECardException(org.openecard.common.ECardException) ByteArrayInputStream(java.io.ByteArrayInputStream) PSSParameterSpec(java.security.spec.PSSParameterSpec) Signature(java.security.Signature) VerifySignature(iso.std.iso_iec._24727.tech.schema.VerifySignature) IncorrectParameterException(org.openecard.common.sal.exception.IncorrectParameterException) DIDStructureType(iso.std.iso_iec._24727.tech.schema.DIDStructureType) DSIReadResponse(iso.std.iso_iec._24727.tech.schema.DSIReadResponse) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) BouncyCastleProvider(org.openecard.bouncycastle.jce.provider.BouncyCastleProvider) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 72 with ConnectionHandleType

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

the class CardStateMap method addEntry.

public synchronized void addEntry(CardStateEntry entry) {
    ConnectionHandleType handle = entry.handleCopy();
    ChannelHandleType channel = handle.getChannelHandle();
    if (channel != null) {
        addMapEntry(channel.getSessionIdentifier(), sessionMap, entry);
    }
    addMapEntry(handle.getContextHandle(), contextMap, entry);
    addMapEntry(handle.getSlotHandle(), slothandleMap, entry);
    allEntries.add(entry);
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) ChannelHandleType(iso.std.iso_iec._24727.tech.schema.ChannelHandleType)

Example 73 with ConnectionHandleType

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

the class CardStateMap method removeEntry.

/**
 * Remove all references to this CardStateEntry.
 * @param entry Entry to delete.
 * @param removeSlotHandles When set remove all occurrences of this entry in the slotHandle index.
 */
private synchronized void removeEntry(CardStateEntry entry, boolean removeSlotHandles) {
    ConnectionHandleType handle = entry.handleCopy();
    ChannelHandleType channel = handle.getChannelHandle();
    if (channel != null) {
        removeMapEntry(channel.getSessionIdentifier(), sessionMap, entry);
    }
    removeMapEntry(handle.getContextHandle(), contextMap, entry);
    // remove all or just the one a key is given for
    if (removeSlotHandles) {
        Iterator<byte[]> it = slothandleMap.keySet().iterator();
        while (it.hasNext()) {
            byte[] key = it.next();
            removeMapEntry(key, slothandleMap, entry);
        }
    } else {
        removeMapEntry(handle.getSlotHandle(), slothandleMap, entry);
    }
    allEntries.remove(entry);
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) ChannelHandleType(iso.std.iso_iec._24727.tech.schema.ChannelHandleType)

Example 74 with ConnectionHandleType

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

the class HandlePrinter method printHandle.

public static void printHandle(Writer w, ConnectionHandleType handle) throws IOException {
    ChannelHandleType channel = handle.getChannelHandle();
    String session = null;
    if (channel != null) {
        session = channel.getSessionIdentifier();
    }
    byte[] ctx = handle.getContextHandle();
    String ifdname = handle.getIFDName();
    BigInteger slotIdx = handle.getSlotIndex();
    byte[] slotHandle = handle.getSlotHandle();
    ConnectionHandleType.RecognitionInfo rec = handle.getRecognitionInfo();
    String cardType = null;
    if (rec != null) {
        cardType = rec.getCardType();
    }
    w.write("ConnectionHandle:");
    if (session != null) {
        w.write("\n  Session: ");
        w.write(session);
    }
    if (ctx != null) {
        w.write("\n  ContextHandle: ");
        w.write(ByteUtils.toHexString(ctx));
    }
    if (ifdname != null) {
        w.write("\n  IFDName: ");
        w.write(ifdname);
        if (slotIdx != null) {
            w.write("  SlotIndex: ");
            w.write(slotIdx.toString());
        }
    }
    if (slotHandle != null) {
        w.write("\n  SlotHandle: ");
        w.write(ByteUtils.toHexString(slotHandle));
    }
    if (cardType != null) {
        w.write("\n  CardType: ");
        w.write(cardType);
    }
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) BigInteger(java.math.BigInteger) ChannelHandleType(iso.std.iso_iec._24727.tech.schema.ChannelHandleType)

Example 75 with ConnectionHandleType

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

the class TestClient method setup.

private void setup() throws Exception {
    // Set up client environment
    ClientEnv env = new ClientEnv();
    // Set up the IFD
    IFD ifd = new IFD();
    env.setIFD(ifd);
    // Set up Management
    TinyManagement management = new TinyManagement(env);
    env.setManagement(management);
    // Set up the Dispatcher
    MessageDispatcher dispatcher = new MessageDispatcher(env);
    env.setDispatcher(dispatcher);
    // Perform an EstablishContext to get a ContextHandle
    EstablishContext establishContext = new EstablishContext();
    EstablishContextResponse establishContextResponse = ifd.establishContext(establishContext);
    byte[] contextHandle = ifd.establishContext(establishContext).getContextHandle();
    final CardRecognitionImpl recognition = new CardRecognitionImpl(env);
    env.setRecognition(recognition);
    env.setCIFProvider(new CIFProvider() {

        @Override
        public CardInfoType getCardInfo(ConnectionHandleType type, String cardType) {
            return recognition.getCardInfo(cardType);
        }

        @Override
        public boolean needsRecognition(byte[] atr) {
            return true;
        }

        @Override
        public CardInfoType getCardInfo(String cardType) throws RuntimeException {
            return recognition.getCardInfo(cardType);
        }

        @Override
        public InputStream getCardImage(String cardType) {
            return recognition.getCardImage(cardType);
        }
    });
    // Set up EventManager
    EventDispatcher ed = new EventDispatcherImpl();
    env.setEventDispatcher(ed);
    // Set up SALStateCallback
    cardStates = new CardStateMap();
    SALStateCallback salCallback = new SALStateCallback(env, cardStates);
    ed.add(salCallback);
    // Set up SAL
    sal = new TinySAL(env, cardStates);
    env.setSAL(sal);
    // Set up GUI
    SwingUserConsent gui = new SwingUserConsent(new SwingDialogWrapper());
    sal.setGUI(gui);
    ifd.setGUI(gui);
    // Initialize the EventManager
    ed.start();
    AddonManager manager = new AddonManager(env, gui, cardStates, null);
    sal.setAddonManager(manager);
    HttpBinding binding = new HttpBinding(24727);
    binding.setAddonManager(manager);
    binding.start();
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) SALStateCallback(org.openecard.common.sal.state.SALStateCallback) TinySAL(org.openecard.sal.TinySAL) EventDispatcherImpl(org.openecard.common.event.EventDispatcherImpl) IFD(org.openecard.ifd.scio.IFD) InputStream(java.io.InputStream) CardRecognitionImpl(org.openecard.recognition.CardRecognitionImpl) EstablishContextResponse(iso.std.iso_iec._24727.tech.schema.EstablishContextResponse) ClientEnv(org.openecard.common.ClientEnv) CIFProvider(org.openecard.common.interfaces.CIFProvider) MessageDispatcher(org.openecard.transport.dispatcher.MessageDispatcher) SwingDialogWrapper(org.openecard.gui.swing.SwingDialogWrapper) CardInfoType(iso.std.iso_iec._24727.tech.schema.CardInfoType) EventDispatcher(org.openecard.common.interfaces.EventDispatcher) SwingUserConsent(org.openecard.gui.swing.SwingUserConsent) TinyManagement(org.openecard.management.TinyManagement) CardStateMap(org.openecard.common.sal.state.CardStateMap) EstablishContext(iso.std.iso_iec._24727.tech.schema.EstablishContext) AddonManager(org.openecard.addon.AddonManager)

Aggregations

ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)110 CardStateEntry (org.openecard.common.sal.state.CardStateEntry)47 ECardException (org.openecard.common.ECardException)43 IncorrectParameterException (org.openecard.common.sal.exception.IncorrectParameterException)37 ThreadTerminateException (org.openecard.common.ThreadTerminateException)36 NamedEntityNotFoundException (org.openecard.common.sal.exception.NamedEntityNotFoundException)34 UnknownProtocolException (org.openecard.common.sal.exception.UnknownProtocolException)34 TLVException (org.openecard.common.tlv.TLVException)29 AddonNotFoundException (org.openecard.addon.AddonNotFoundException)28 InappropriateProtocolForActionException (org.openecard.common.sal.exception.InappropriateProtocolForActionException)28 NameExistsException (org.openecard.common.sal.exception.NameExistsException)28 PrerequisitesNotSatisfiedException (org.openecard.common.sal.exception.PrerequisitesNotSatisfiedException)28 SecurityConditionNotSatisfiedException (org.openecard.common.sal.exception.SecurityConditionNotSatisfiedException)28 UnknownConnectionHandleException (org.openecard.common.sal.exception.UnknownConnectionHandleException)28 DIDStructureType (iso.std.iso_iec._24727.tech.schema.DIDStructureType)22 Publish (org.openecard.common.interfaces.Publish)17 CardApplicationConnectResponse (iso.std.iso_iec._24727.tech.schema.CardApplicationConnectResponse)15 ArrayList (java.util.ArrayList)15 CardApplicationConnect (iso.std.iso_iec._24727.tech.schema.CardApplicationConnect)14 CardApplicationPathType (iso.std.iso_iec._24727.tech.schema.CardApplicationPathType)14