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

Javacard km 41 aosp upmerge 0630 #5

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0b6aeb1
CTS Fixes
subrahmanyaman Oct 29, 2021
8003cc6
CTS Fixes
subrahmanyaman Oct 29, 2021
3f78abf
Reduced writes in SEProvider
subrahmanyaman Oct 29, 2021
db91064
Updated Provision json sample files
subrahmanyaman Oct 30, 2021
cdb6526
Fixed CTS issues
subrahmanyaman Oct 30, 2021
08bde9e
Add configurations class
subrahmanyaman Oct 31, 2021
ddec6bc
Added KMConfiguration class
subrahmanyaman Oct 31, 2021
5fd558a
Separated the provision buffer
subrahmanyaman Nov 3, 2021
f09d41a
1. Corrected some mismatches with 4.1 specification.
subrahmanyaman Nov 5, 2021
c4d5f0c
Added cts keystore patch
subrahmanyaman Nov 7, 2021
49f490c
Added implementation for MAX_USES_PER_BOOT
subrahmanyaman Nov 9, 2021
62066b5
No Buffering for Block ciphers except for AES PKCS7 and AES GCM Decry…
subrahmanyaman Nov 14, 2021
9a103de
Store ComputedHmac Key inside a KeyObject
subrahmanyaman Nov 15, 2021
d999433
Added KMComputedHmacKey
subrahmanyaman Nov 16, 2021
ab85ff6
Added trusted confirmation changes
subrahmanyaman Nov 16, 2021
c48ba44
Hmac signer reset
subrahmanyaman Nov 17, 2021
120c3a6
Added VTS and CTS patches
subrahmanyaman Nov 18, 2021
10682f6
Updated the patches
subrahmanyaman Nov 18, 2021
a0f6023
Cache earlybootEnded event in HAL and send in getHmacSharedParameters
subrahmanyaman Nov 19, 2021
5df5d5e
Added support PKCS8 decoder
subrahmanyaman Nov 20, 2021
be8509a
Added support PKCS8 decoder
subrahmanyaman Nov 20, 2021
524c022
Send earlybootEndedEvent if pending from begin, generateKey, importKey
subrahmanyaman Nov 20, 2021
c8655fe
ProvisionAttestaionKey is accepted only as RAW
subrahmanyaman Nov 22, 2021
d318490
Added applet upgrade versioning support
subrahmanyaman Nov 28, 2021
9c05e26
optimized the AUTH_DATA creation for Keyblob
subrahmanyaman Nov 29, 2021
ce7ec01
Merge pull request #4 from subrahmanyaman/applet_version_upgrade_move…
subrahmanyaman Nov 29, 2021
99973a2
Added Indentation
subrahmanyaman Nov 29, 2021
ec606d5
Added package version
subrahmanyaman Nov 29, 2021
780495e
Removed *.iml file
subrahmanyaman Nov 30, 2021
8b2d166
Incorporated review comments
subrahmanyaman Dec 1, 2021
6bbdbc9
1. moved packageversion from byte array to short
subrahmanyaman Dec 2, 2021
45eae4d
Merge pull request #69 from subrahmanyaman/Javacard_KM_41_CTS_Fixes
mdwivedi Dec 2, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import org.globalplatform.upgrade.OnUpgradeListener;
import org.globalplatform.upgrade.UpgradeManager;

import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;

public class KMAndroidSEApplet extends KMKeymasterApplet implements OnUpgradeListener {

KMAndroidSEApplet() {
Expand Down Expand Up @@ -47,10 +51,22 @@ public void onConsolidate() {
@Override
public void onRestore(Element element) {
element.initRead();
provisionStatus = element.readByte();
byte firstByte = element.readByte();
short packageVersion_ = 0;
byte provisionStatus_ = firstByte;
if (firstByte == KMKeymasterApplet.KM_MAGIC_NUMBER) {
packageVersion_ = element.readShort();
provisionStatus_ = element.readByte();
}
if (0 != packageVersion_ && !isUpgradeAllowed(packageVersion_)) {
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
}
packageVersion = packageVersion_;
provisionStatus = provisionStatus_;
keymasterState = element.readByte();
repository.onRestore(element);
seProvider.onRestore(element);
repository.onRestore(element, packageVersion, CURRENT_PACKAGE_VERSION);
seProvider.onRestore(element, packageVersion, CURRENT_PACKAGE_VERSION);
handleDataUpgradeToVersion1_1();
}

@Override
Expand All @@ -68,6 +84,8 @@ public Element onSave() {
// Create element.
Element element = UpgradeManager.createElement(Element.TYPE_SIMPLE,
primitiveCount, objectCount);
element.write(KM_MAGIC_NUMBER);
element.write(packageVersion);
element.write(provisionStatus);
element.write(keymasterState);
repository.onSave(element);
Expand All @@ -76,12 +94,113 @@ public Element onSave() {
}

private short computePrimitveDataSize() {
// provisionStatus + keymasterState
return (short) 2;
// provisionStatus + keymasterState + magic byte + version
return (short) 5;
}

private short computeObjectCount() {
return (short) 0;
}

public boolean isUpgradeAllowed(short version) {
boolean upgradeAllowed = false;
short oldMajorVersion = (short) (version >> 8 & 0x00FF);
short oldMinorVersion = (short) (version & 0x00FF);
short currentMajorVersion = (short) (CURRENT_PACKAGE_VERSION >> 8 & 0x00FF);
short currentMinorVersion = (short) (CURRENT_PACKAGE_VERSION & 0x00FF);
// Downgrade of the Applet is not allowed.
// Upgrade is not allowed to a next version which is not immediate.
if ((short) (currentMajorVersion - oldMajorVersion) == 1) {
if (currentMinorVersion == 0) {
upgradeAllowed = true;
}
} else if ((short) (currentMajorVersion - oldMajorVersion) == 0) {
if ((short) (currentMinorVersion - oldMinorVersion) == 1) {
upgradeAllowed = true;
}
}
return upgradeAllowed;
}

public void handleDataUpgradeToVersion1_1() {

if (packageVersion != 0) {
// No Data upgrade required.
return;
}
byte status = provisionStatus;
// In the current version of the applet set boot parameters is removed from
// provision status so readjust the provision locked flag.
// 0x40 is provision locked flag in the older applet.
// Unset the 5th bit. setboot parameters flag.
status = (byte) (status & 0xDF);
// Readjust the lock provisioned status flag.
if ((status & 0x40) == 0x40) {
// 0x40 to 0x20
// Unset 6th bit
status = (byte) (status & 0xBF);
// set the 5th bit
status = (byte) (status | 0x20);
}
provisionStatus = status;
packageVersion = CURRENT_PACKAGE_VERSION;

short certExpiryLen = 0;
short issuerLen = 0;
short certExpiry = repository.getCertExpiryTime();
if (certExpiry != KMType.INVALID_VALUE) {
certExpiryLen = KMByteBlob.cast(certExpiry).length();
}
short issuer = repository.getIssuer();
if (issuer != KMType.INVALID_VALUE) {
issuerLen = KMByteBlob.cast(issuer).length();
}
short certChainLen = seProvider.getProvisionedDataLength(KMSEProvider.CERTIFICATE_CHAIN);
short offset = repository.allocReclaimableMemory((short) (certExpiryLen + issuerLen + certChainLen));
// Get the start offset of the certificate chain.
short certChaionOff =
decoder.getCborBytesStartOffset(
repository.getHeap(),
offset,
seProvider.readProvisionedData(KMSEProvider.CERTIFICATE_CHAIN, repository.getHeap(), offset));
certChainLen -= (short) (certChaionOff - offset);
Util.arrayCopyNonAtomic(
KMByteBlob.cast(issuer).getBuffer(),
KMByteBlob.cast(issuer).getStartOff(),
repository.getHeap(),
(short) (certChaionOff + certChainLen),
issuerLen);
Util.arrayCopyNonAtomic(
KMByteBlob.cast(certExpiry).getBuffer(),
KMByteBlob.cast(certExpiry).getStartOff(),
repository.getHeap(),
(short) (certChaionOff + certChainLen + issuerLen),
certExpiryLen);

seProvider.persistProvisionData(
repository.getHeap(),
certChaionOff, // cert chain offset
certChainLen,
(short) (certChaionOff + certChainLen), // issuer offset
issuerLen,
(short) (certChaionOff + certChainLen + issuerLen), // cert expiry offset
certExpiryLen);


// Update computed HMAC key.
short blob = repository.getComputedHmacKey();
if (blob != KMType.INVALID_VALUE) {
seProvider.createComputedHmacKey(
KMByteBlob.cast(blob).getBuffer(),
KMByteBlob.cast(blob).getStartOff(),
KMByteBlob.cast(blob).length()
);
} else {
// Initialize the Key object.
Util.arrayFillNonAtomic(repository.getHeap(), offset, (short) 32, (byte) 0);
seProvider.createComputedHmacKey(repository.getHeap(), offset,(short) 32);
}
repository.reclaimMemory((short) (certExpiryLen + issuerLen + certChainLen));
}
}

Loading