Skip to content

Commit

Permalink
Merge pull request #558 from fgonzal/master
Browse files Browse the repository at this point in the history
`SMAdapter`: add new operation to encrypt clear PIN under PEK.
  • Loading branch information
ar committed Jul 28, 2023
1 parent e3a5fae commit f1486b8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
40 changes: 40 additions & 0 deletions jpos/src/main/java/org/jpos/security/BaseSMAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,30 @@ public EncryptedPIN encryptPIN (String pin, String accountNumber) throws SMExcep
return encryptPIN(pin, accountNumber, true);
}

@Override
public EncryptedPIN encryptPIN(String pin, String accountNumber, T pek) throws SMException {
accountNumber = EncryptedPIN.extractAccountNumberPart(accountNumber);
List<Loggeable> cmdParameters = new ArrayList<>();
cmdParameters.add(new SimpleMsg("parameter", "clear pin", pin));
cmdParameters.add(new SimpleMsg("parameter", "account number", accountNumber));
cmdParameters.add(new SimpleMsg("parameter", "pin encryption Key", pek));
LogEvent evt = new LogEvent(this, "s-m-operation");
evt.addMessage(new SimpleMsg("command", "Encrypt clear PIN under PEK", cmdParameters));
EncryptedPIN result = null;
try {
result = encryptPINImpl(pin, accountNumber, pek);
evt.addMessage(new SimpleMsg("result", "PIN under PEK", result));
}
catch (Exception e) {
evt.addMessage(e);
throw e instanceof SMException ? (SMException) e : new SMException(e);
}
finally {
Logger.log(evt);
}
return result;
}

@Override
public String decryptPIN (EncryptedPIN pinUnderLmk) throws SMException {
List<Loggeable> cmdParameters = new ArrayList<>();
Expand Down Expand Up @@ -912,6 +936,9 @@ public boolean verifydCVV(String accountNo, T imkac, String dcvv,
String expDate, String serviceCode, byte[] atc, MKDMethod mkdm)
throws SMException {

if (accountNo == null || accountNo.trim().length() == 0)
throw new IllegalArgumentException("Account number not set.");

List<Loggeable> cmdParameters = new ArrayList<>();
cmdParameters.add(new SimpleMsg("parameter", "account number", accountNo));
cmdParameters.add(new SimpleMsg("parameter", "imk-ac", imkac == null ? "" : imkac));
Expand Down Expand Up @@ -1578,6 +1605,19 @@ protected EncryptedPIN encryptPINImpl (String pin, String accountNumber) throws
throw new SMException("Operation not supported in: " + this.getClass().getName());
}

/**
* Your SMAdapter should override this method if it has this functionality.
*
* @param pin
* @param accountNumber
* @param pek
* @return encrypted PIN under PEK.
* @throws SMException
*/
protected EncryptedPIN encryptPINImpl(String pin, String accountNumber, T pek) throws SMException {
throw new SMException("Operation not supported in: " + this.getClass().getName());
}

/**
* Your SMAdapter should override this method if it has this functionality
* @param pinUnderLmk
Expand Down
14 changes: 13 additions & 1 deletion jpos/src/main/java/org/jpos/security/SMAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ SecureKey exportKey(SecureKey kek, SecureKey key, SecureKeySpec keySpec)
* Encrypts a clear pin under LMK.
*
* <p>CAUTION: The use of clear pin presents a significant security risk
* @param pin clear pin as entered by card holder
* @param pin clear pin as entered by cardholder
* @param accountNumber if <code>extract</code> is false then account number, including BIN and the check digit
* or if parameter <code>extract</code> is true then 12 right-most digits of the account number, excluding the check digit
* @param extract true to extract 12 right-most digits off the account number
Expand All @@ -393,6 +393,18 @@ SecureKey exportKey(SecureKey kek, SecureKey key, SecureKeySpec keySpec)
*/
EncryptedPIN encryptPIN(String pin, String accountNumber, boolean extract) throws SMException;

/**
* Encrypts a clear PIN under PEK.
*
* <p>CAUTION: The use of clear PIN presents a significant security risk.
* @param pin Clear PIN as entered by cardholder.
* @param accountNumber account number, including BIN and the check digit.
* @param pek PIN encryption key.
* @return Return PIN under PEK.
* @throws SMException
*/
EncryptedPIN encryptPIN(String pin, String accountNumber, T pek) throws SMException;

/**
* Decrypts an Encrypted PIN (under LMK).
* <p>CAUTION: The use of clear pin presents a significant security risk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ public EncryptedPIN encryptPINImpl (String pin, String accountNumber) throws SME
return new EncryptedPIN(translatedPINBlock, FORMAT00, accountNumber, false);
}

@Override
protected EncryptedPIN encryptPINImpl(String pin, String accountNumber, SecureDESKey pek) throws SMException {
byte[] clearPINBlock = calculatePINBlock(pin, FORMAT00, accountNumber);
Key clearPEK = decryptFromLMK(pek);
byte[] translatedPINBlock = jceHandler.encryptData(clearPINBlock, clearPEK);
return new EncryptedPIN(translatedPINBlock, FORMAT00, accountNumber, false);
}

@Override
public String decryptPINImpl (EncryptedPIN pinUnderLmk) throws SMException {
byte[] clearPINBlock = jceHandler.decryptData(pinUnderLmk.getPINBlock(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ public void testVerifyDCVVImplException1() throws Throwable {
byte[] atc = ISOUtil.hex2byte("3210");
assertThrows(SMException.class, () -> {
jcesecmod.verifydCVV(accountNo, imkac, dcvv, expDate
,serviceCode, atc, MKDMethod.OPTION_A);
,serviceCode, atc, MKDMethod.OPTION_A);
});
}

Expand Down

0 comments on commit f1486b8

Please sign in to comment.