Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support cards which only have ALG_RSA_CRT #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions src/com/makina/security/OpenFIPS201/PIVKeyObjectPKI.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ of this software and associated documentation files (the "Software"), to deal
*/
public final class PIVKeyObjectPKI extends PIVKeyObject {

private RSAPrivateKey privateKey;
private Key privateKey;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of Key, should this be PrivateKey?

private RSAPublicKey publicKey;
private KeyPair keyPair;
private boolean isCrtKey;

// The list of elements that can be updated for an asymmetric key

Expand Down Expand Up @@ -80,8 +81,11 @@ public void updateElement(byte element, byte[] buffer, short offset, short lengt
case ELEMENT_RSA_N:
if (length != getKeyLength()) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
if (publicKey == null || privateKey == null) allocate();
if (isCrtKey) {
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
}
publicKey.setModulus(buffer, offset, length);
privateKey.setModulus(buffer, offset, length);
((RSAPrivateKey) privateKey).setModulus(buffer, offset, length);
break;

// RSA Public Exponent
Expand All @@ -95,7 +99,10 @@ public void updateElement(byte element, byte[] buffer, short offset, short lengt
case ELEMENT_RSA_D:
if (length != getKeyLength()) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
if (privateKey == null) allocate();
privateKey.setExponent(buffer, offset, length);
if (isCrtKey) {
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
}
((RSAPrivateKey) privateKey).setExponent(buffer, offset, length);
break;

/*
Expand Down Expand Up @@ -146,7 +153,11 @@ public void updateElement(byte element, byte[] buffer, short offset, short lengt
*/
public void setPrivateExponent(byte[] buffer, short offset, short length) {
if (privateKey == null) allocate();
privateKey.setExponent(buffer, offset, length);
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the next few lines, I'm guessing this line was included in error.

if (isCrtKey) {
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
}
((RSAPrivateKey) privateKey).setExponent(buffer, offset, length);
}

/**
Expand All @@ -168,7 +179,11 @@ public void setPublicExponent(byte[] buffer, short offset, short length) {
*/
public void setModulus(byte[] buffer, short offset, short length) {
if (privateKey == null || publicKey == null) allocate();
privateKey.setModulus(buffer, offset, length);
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as that from line 156 above: I'm guessing this line was added in error.

if (isCrtKey) {
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
}
((RSAPrivateKey) privateKey).setModulus(buffer, offset, length);
publicKey.setModulus(buffer, offset, length);
}

Expand All @@ -194,23 +209,39 @@ public short getModulus(byte[] buffer, short offset) {

private void allocate() {

isCrtKey = false;

// Generate the appropriate key(s)
switch (header[HEADER_MECHANISM]) {

case PIV.ID_ALG_RSA_1024:
keyPair = new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_1024);
try {
keyPair = new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_1024);
} catch (CryptoException e) {
if (e.getReason() == CryptoException.NO_SUCH_ALGORITHM) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent whitespace.

keyPair = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_1024);
isCrtKey = true;
}
}
break;

case PIV.ID_ALG_RSA_2048:
keyPair = new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_2048);
try {
keyPair = new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_2048);
} catch (CryptoException e) {
if (e.getReason() == CryptoException.NO_SUCH_ALGORITHM) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent whitespace.

keyPair = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_2048);
isCrtKey = true;
}
}
break;

default:
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
break;
}

privateKey = (RSAPrivateKey)keyPair.getPrivate();
privateKey = keyPair.getPrivate();
publicKey = (RSAPublicKey)keyPair.getPublic();
}

Expand Down