Search in sources :

Example 36 with LogEvent

use of org.jpos.util.LogEvent in project jPOS by jpos.

the class BaseSMAdapter method importPIN.

@Override
public EncryptedPIN importPIN(EncryptedPIN pinUnderKd1, SecureDESKey kd1) throws SMException {
    SimpleMsg[] cmdParameters = { new SimpleMsg("parameter", "PIN under Data Key 1", pinUnderKd1), new SimpleMsg("parameter", "Data Key 1", kd1) };
    LogEvent evt = new LogEvent(this, "s-m-operation");
    evt.addMessage(new SimpleMsg("command", "Import PIN", cmdParameters));
    EncryptedPIN result = null;
    try {
        result = importPINImpl(pinUnderKd1, kd1);
        evt.addMessage(new SimpleMsg("result", "PIN under LMK", result));
    } catch (Exception e) {
        evt.addMessage(e);
        throw e instanceof SMException ? (SMException) e : new SMException(e);
    } finally {
        Logger.log(evt);
    }
    return result;
}
Also used : LogEvent(org.jpos.util.LogEvent) SimpleMsg(org.jpos.util.SimpleMsg) ConfigurationException(org.jpos.core.ConfigurationException) NotFoundException(org.jpos.util.NameRegistrar.NotFoundException)

Example 37 with LogEvent

use of org.jpos.util.LogEvent in project jPOS by jpos.

the class BaseSMAdapter method generateEDE_MAC.

@Override
public byte[] generateEDE_MAC(byte[] data, SecureDESKey kd) throws SMException {
    SimpleMsg[] cmdParameters = { new SimpleMsg("parameter", "data", data), new SimpleMsg("parameter", "data key", kd) };
    LogEvent evt = new LogEvent(this, "s-m-operation");
    evt.addMessage(new SimpleMsg("command", "Generate EDE-MAC", cmdParameters));
    byte[] result = null;
    try {
        result = generateEDE_MACImpl(data, kd);
        evt.addMessage(new SimpleMsg("result", "EDE-MAC", result));
    } catch (Exception e) {
        evt.addMessage(e);
        throw e instanceof SMException ? (SMException) e : new SMException(e);
    } finally {
        Logger.log(evt);
    }
    return result;
}
Also used : LogEvent(org.jpos.util.LogEvent) SimpleMsg(org.jpos.util.SimpleMsg) ConfigurationException(org.jpos.core.ConfigurationException) NotFoundException(org.jpos.util.NameRegistrar.NotFoundException)

Example 38 with LogEvent

use of org.jpos.util.LogEvent in project jPOS by jpos.

the class SimpleKeyFile method setKey.

public synchronized void setKey(String alias, SecureKey secureKey) throws SecureKeyStoreException {
    LogEvent evt = new LogEvent(this, "set-key");
    evt.addMessage("alias", alias);
    evt.addMessage(secureKey);
    try {
        if (!(secureKey instanceof SecureDESKey))
            throw new SecureKeyStoreException("Unsupported SecureKey class: " + secureKey.getClass().getName());
        // load new changes (possibly made manually on the file)
        load();
        setProperty(alias, "class", secureKey.getClass().getName());
        setProperty(alias, "key", ISOUtil.hexString(secureKey.getKeyBytes()));
        setProperty(alias, "length", Short.toString(secureKey.getKeyLength()));
        setProperty(alias, "type", secureKey.getKeyType());
        String keyCheckValueHexString = ISOUtil.hexString(((SecureDESKey) secureKey).getKeyCheckValue());
        setProperty(alias, "checkvalue", keyCheckValueHexString);
        store();
    } catch (Exception e) {
        evt.addMessage(e);
        throw e instanceof SecureKeyStoreException ? (SecureKeyStoreException) e : new SecureKeyStoreException(e);
    } finally {
        Logger.log(evt);
    }
}
Also used : LogEvent(org.jpos.util.LogEvent) ConfigurationException(org.jpos.core.ConfigurationException)

Example 39 with LogEvent

use of org.jpos.util.LogEvent in project jPOS by jpos.

the class JCESecurityModule method formKEYfromThreeClearComponents.

/**
 * Forms a key from 3 clear components and returns it encrypted under its corresponding LMK
 * The corresponding LMK is determined from the keyType
 * @param keyLength e.g. LENGTH_DES, LENGTH_DES3_2, LENGTH_DES3_3, ..
 * @param keyType possible values are those defined in the SecurityModule inteface. e.g., ZMK, TMK,...
 * @param clearComponent1HexString HexString containing the first component
 * @param clearComponent2HexString HexString containing the second component
 * @param clearComponent3HexString HexString containing the second component
 * @return forms an SecureDESKey from two clear components
 * @throws SMException
 */
SecureDESKey formKEYfromThreeClearComponents(short keyLength, String keyType, String clearComponent1HexString, String clearComponent2HexString, String clearComponent3HexString) throws SMException {
    SecureDESKey secureDESKey;
    LogEvent evt = new LogEvent(this, "s-m-operation");
    try {
        byte[] clearComponent1 = ISOUtil.hex2byte(clearComponent1HexString);
        byte[] clearComponent2 = ISOUtil.hex2byte(clearComponent2HexString);
        byte[] clearComponent3 = ISOUtil.hex2byte(clearComponent3HexString);
        byte[] clearKeyBytes = ISOUtil.xor(ISOUtil.xor(clearComponent1, clearComponent2), clearComponent3);
        Key clearKey = jceHandler.formDESKey(keyLength, clearKeyBytes);
        secureDESKey = encryptToLMK(keyLength, keyType, clearKey);
        SimpleMsg[] cmdParameters = { new SimpleMsg("parameter", "Key Length", keyLength), new SimpleMsg("parameter", "Key Type", keyType), new SimpleMsg("parameter", "Component 1 Check Value", calculateKeyCheckValue(jceHandler.formDESKey(keyLength, clearComponent1))), new SimpleMsg("parameter", "Component 2 Check Value", calculateKeyCheckValue(jceHandler.formDESKey(keyLength, clearComponent2))), new SimpleMsg("parameter", "Component 3 Check Value", calculateKeyCheckValue(jceHandler.formDESKey(keyLength, clearComponent3))) };
        evt.addMessage(new SimpleMsg("command", "Form Key from Three Clear Components", cmdParameters));
        evt.addMessage(new SimpleMsg("result", "Formed Key", secureDESKey));
    } catch (JCEHandlerException e) {
        evt.addMessage(e);
        throw e;
    } finally {
        Logger.log(evt);
    }
    return secureDESKey;
}
Also used : LogEvent(org.jpos.util.LogEvent) SimpleMsg(org.jpos.util.SimpleMsg) SecretKey(javax.crypto.SecretKey)

Example 40 with LogEvent

use of org.jpos.util.LogEvent in project jPOS by jpos.

the class JCESecurityModule method init.

/**
 * Initializes the JCE Security Module
 * @param jceProviderClassName
 * @param lmkFile Local Master Keys File used by JCE Security Module to store the LMKs
 * @param lmkRebuild if set to true, the lmkFile gets overwritten with newly generated keys (WARNING: this would render all your previously stored SecureKeys unusable)
 * @throws SMException
 */
private void init(String jceProviderClassName, String lmkFile, boolean lmkRebuild) throws SMException {
    File lmk = lmkFile != null ? new File(lmkFile) : null;
    if (lmk == null && !lmkRebuild)
        throw new SMException("null lmkFile - needs rebuild");
    try {
        keyTypeToLMKIndex = new TreeMap<>();
        keyTypeToLMKIndex.put(SMAdapter.TYPE_ZMK, 0x000);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_ZPK, 0x001);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_PVK, 0x002);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_TPK, 0x002);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_TMK, 0x002);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_TAK, 0x003);
        // keyTypeToLMKIndex.put(PINLMKIndex,        0x004);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_CVK, 0x402);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_ZAK, 0x008);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_BDK, 0x009);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_MK_AC, 0x109);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_MK_SMI, 0x209);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_MK_SMC, 0x309);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_MK_DAC, 0x409);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_MK_DN, 0x509);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_MK_CVC3, 0x709);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_ZEK, 0x00A);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_DEK, 0x00B);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_RSA_SK, 0x00C);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_HMAC, 0x10C);
        keyTypeToLMKIndex.put(SMAdapter.TYPE_RSA_PK, 0x00D);
        Provider provider;
        LogEvent evt = new LogEvent(this, "jce-provider");
        try {
            if (jceProviderClassName == null || jceProviderClassName.isEmpty()) {
                evt.addMessage("No JCE Provider specified. Attempting to load default provider (SunJCE).");
                jceProviderClassName = "com.sun.crypto.provider.SunJCE";
            }
            provider = (Provider) Class.forName(jceProviderClassName).newInstance();
            Security.addProvider(provider);
            evt.addMessage("name", provider.getName());
        } catch (Exception e) {
            evt.addMessage(e);
            throw new SMException("Unable to load jce provider whose class name is: " + jceProviderClassName);
        } finally {
            Logger.log(evt);
        }
        jceHandler = new JCEHandler(provider);
        if (lmkRebuild) {
            // Creat new LMK file
            evt = new LogEvent(this, "local-master-keys");
            if (lmk != null)
                evt.addMessage("Rebuilding new Local Master Keys in file: \"" + lmk.getCanonicalPath() + "\".");
            Logger.log(evt);
            // Generate New random Local Master Keys
            generateLMK();
            // Write the new Local Master Keys to file
            evt = new LogEvent(this, "local-master-keys");
            if (lmk != null) {
                writeLMK(lmk);
                evt.addMessage("Local Master Keys built successfully in file: \"" + lmk.getCanonicalPath() + "\".");
            } else {
                evt.addMessage("Local Master Keys built successfully");
            }
            Logger.log(evt);
        }
        if (lmk != null) {
            if (!lmk.exists()) {
                // LMK File does not exist
                throw new SMException("Error loading Local Master Keys, file: \"" + lmk.getCanonicalPath() + "\" does not exist." + " Please specify a valid LMK file, or rebuild a new one.");
            } else {
                // Read LMK from file
                readLMK(lmk);
                evt = new LogEvent(this, "local-master-keys");
                evt.addMessage("Loaded successfully from file: \"" + lmk.getCanonicalPath() + "\"");
                Logger.log(evt);
            }
        }
    } catch (Exception e) {
        if (e instanceof SMException) {
            throw (SMException) e;
        } else {
            throw new SMException(e);
        }
    }
}
Also used : LogEvent(org.jpos.util.LogEvent) File(java.io.File) ConfigurationException(org.jpos.core.ConfigurationException) ISOException(org.jpos.iso.ISOException) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Aggregations

LogEvent (org.jpos.util.LogEvent)189 Test (org.junit.Test)78 ConfigurationException (org.jpos.core.ConfigurationException)51 ISOMsg (org.jpos.iso.ISOMsg)41 SimpleMsg (org.jpos.util.SimpleMsg)40 NotFoundException (org.jpos.util.NameRegistrar.NotFoundException)38 ArrayList (java.util.ArrayList)24 IOException (java.io.IOException)18 SimpleConfiguration (org.jpos.core.SimpleConfiguration)14 CSChannel (org.jpos.iso.channel.CSChannel)12 Loggeable (org.jpos.util.Loggeable)11 Map (java.util.Map)9 ISOChannel (org.jpos.iso.ISOChannel)9 PostChannel (org.jpos.iso.channel.PostChannel)9 CTCSubFieldPackager (org.jpos.iso.packager.CTCSubFieldPackager)9 ISOFilter (org.jpos.iso.ISOFilter)8 BASE24TCPChannel (org.jpos.iso.channel.BASE24TCPChannel)8 PADChannel (org.jpos.iso.channel.PADChannel)8 ISOBaseValidatingPackager (org.jpos.iso.packager.ISOBaseValidatingPackager)8 EOFException (java.io.EOFException)7