Skip to content

Commit 5cc8d6c

Browse files
authored
Merge pull request #63 from subrahmanyaman/Javacard_KeyMint_100_master
Keymint AttestKey and RKP changes.
2 parents 8c0d85e + 09da484 commit 5cc8d6c

File tree

104 files changed

+14808
-560
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+14808
-560
lines changed

Applet/JCardSimProvider/src/com/android/javacard/keymaster/KMAttestationCertImpl.java

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class KMAttestationCertImpl implements KMAttestationCert {
100100
private static final byte keyUsageCertSign = (byte) 0x04; // 5th- bit
101101

102102
private static final byte KEYMASTER_VERSION = 100;
103-
private static final byte ATTESTATION_VERSION = 3;
103+
private static final byte ATTESTATION_VERSION = 100;
104104
private static final byte[] pubExponent = {0x01, 0x00, 0x01};
105105
private static final byte SERIAL_NUM = (byte) 0x01;
106106
private static final byte X509_VERSION = (byte) 0x02;
@@ -138,7 +138,8 @@ public class KMAttestationCertImpl implements KMAttestationCert {
138138
private static short serialNum;
139139

140140
private static byte certMode;
141-
private static short certAttestKey ;
141+
private static short certAttestKeySecret;
142+
private static short certAttestKeyRsaPubModulus;
142143
private static boolean certRsaSign;
143144
private static final byte SERIAL_NUM_MAX_LEN = 20;
144145
private static final byte SUBJECT_NAME_MAX_LEN = 32;
@@ -186,7 +187,7 @@ private static void init() {
186187
deviceLocked = 0;
187188
signPriv = 0;
188189
certMode = KMType.NO_CERT;
189-
certAttestKey = KMType.INVALID_VALUE;
190+
certAttestKeySecret = KMType.INVALID_VALUE;
190191
certRsaSign = true;
191192
issuer = KMType.INVALID_VALUE;
192193
subjectName = KMType.INVALID_VALUE;
@@ -234,7 +235,6 @@ public KMAttestationCert notAfter(short usageExpiryTimeObj, boolean derEncoded,
234235
if (usageExpiryTimeObj != KMType.INVALID_VALUE) {
235236
// compare if the expiry time is greater then 2051 then use generalized
236237
// time format else use utc time format.
237-
usageExpiryTimeObj = KMIntegerTag.cast(usageExpiryTimeObj).getValue();
238238
short tmpVar = KMInteger.uint_64(KMUtils.firstJan2051, (short) 0);
239239
if (KMInteger.compare(usageExpiryTimeObj, tmpVar) >= 0) {
240240
usageExpiryTimeObj = KMUtils.convertToDate(usageExpiryTimeObj, scratchPad,
@@ -514,14 +514,16 @@ private static void pushHWParams() {
514514
// Below are the allowed hardwareEnforced Authorization tags inside the attestation certificate's extension.
515515
short[] tagIds = {
516516
KMType.BOOT_PATCH_LEVEL, KMType.VENDOR_PATCH_LEVEL,
517+
KMType.ATTESTATION_ID_MODEL, KMType.ATTESTATION_ID_MANUFACTURER,
518+
KMType.ATTESTATION_ID_MEID, KMType.ATTESTATION_ID_IMEI,
519+
KMType.ATTESTATION_ID_SERIAL, KMType.ATTESTATION_ID_PRODUCT,
520+
KMType.ATTESTATION_ID_DEVICE, KMType.ATTESTATION_ID_BRAND,
517521
KMType.OS_PATCH_LEVEL, KMType.OS_VERSION, KMType.ROOT_OF_TRUST,
518-
KMType.ORIGIN, KMType.APPLICATION_ID,
519-
KMType.TRUSTED_CONFIRMATION_REQUIRED,
520-
KMType.TRUSTED_USER_PRESENCE_REQUIRED, KMType.ALLOW_WHILE_ON_BODY,
521-
KMType.AUTH_TIMEOUT, KMType.USER_AUTH_TYPE, KMType.NO_AUTH_REQUIRED,
522-
KMType.ROLLBACK_RESISTANCE, KMType.RSA_PUBLIC_EXPONENT,
523-
KMType.ECCURVE, KMType.PADDING, KMType.DIGEST, KMType.KEYSIZE,
524-
KMType.ALGORITHM, KMType.PURPOSE};
522+
KMType.ORIGIN, KMType.AUTH_TIMEOUT, KMType.USER_AUTH_TYPE,
523+
KMType.NO_AUTH_REQUIRED, KMType.USER_SECURE_ID,
524+
KMType.RSA_PUBLIC_EXPONENT, KMType.ECCURVE, KMType.MIN_MAC_LENGTH,
525+
KMType.CALLER_NONCE, KMType.PADDING, KMType.DIGEST, KMType.BLOCK_MODE,
526+
KMType.KEYSIZE, KMType.ALGORITHM, KMType.PURPOSE};
525527

526528
byte index = 0;
527529
do {
@@ -862,8 +864,8 @@ public short getCertLength() {
862864
return certLength;
863865
}
864866

865-
@Override
866-
public void build(byte[] attBuf, short attStart, short attLength, boolean rsaSign, boolean fakeCert) {
867+
868+
public void build(short attSecret, short attMod, boolean rsaSign, boolean fakeCert) {
867869
stackPtr = (short)(bufStart + bufLength);
868870
short last = stackPtr;
869871
short sigLen = 0;
@@ -891,18 +893,18 @@ else if (rsaSign) {
891893
tbsStart = stackPtr;
892894
tbsLength = (short) (tbsLength - tbsStart);
893895
KMJCardSimulator provider = KMJCardSimulator.getInstance();
894-
if(attBuf != null){
896+
if(attSecret != KMType.INVALID_VALUE){
895897
// Sign with the attestation key
896898
// The pubKey is the modulus.
897899
if (rsaSign) {
898900
sigLen = provider
899901
.rsaSign256Pkcs1(
900-
attBuf,
901-
attStart,
902-
attLength,
903-
KMByteBlob.cast(pubKey).getBuffer(),
904-
KMByteBlob.cast(pubKey).getStartOff(),
905-
KMByteBlob.cast(pubKey).length(),
902+
KMByteBlob.cast(attSecret).getBuffer(),
903+
KMByteBlob.cast(attSecret).getStartOff(),
904+
KMByteBlob.cast(attSecret).length(),
905+
KMByteBlob.cast(attMod).getBuffer(),
906+
KMByteBlob.cast(attMod).getStartOff(),
907+
KMByteBlob.cast(attMod).length(),
906908
stack,
907909
tbsStart,
908910
tbsLength,
@@ -912,9 +914,9 @@ else if (rsaSign) {
912914
} else {
913915
sigLen = provider
914916
.ecSign256(
915-
attBuf,
916-
attStart,
917-
attLength,
917+
KMByteBlob.cast(attSecret).getBuffer(),
918+
KMByteBlob.cast(attSecret).getStartOff(),
919+
KMByteBlob.cast(attSecret).length(),
918920
stack,
919921
tbsStart,
920922
tbsLength,
@@ -941,11 +943,9 @@ else if (rsaSign) {
941943
@Override
942944
public void build() {
943945
if(certMode == KMType.FAKE_CERT) {
944-
build(null, (short) 0, (short) 0, true, true);
946+
build(KMType.INVALID_VALUE, KMType.INVALID_VALUE, true, true);
945947
}else {
946-
build(KMByteBlob.cast(certAttestKey).getBuffer(),
947-
KMByteBlob.cast(certAttestKey).getStartOff(),
948-
KMByteBlob.cast(certAttestKey).length(), certRsaSign, false);
948+
build(certAttestKeySecret, certAttestKeyRsaPubModulus, certRsaSign, false);
949949
}
950950
}
951951

@@ -1024,10 +1024,20 @@ public boolean subjectName(short sub){
10241024
}
10251025

10261026
@Override
1027-
public KMAttestationCert attestKey(short attestKey, boolean rsaSign, byte mode){
1027+
public KMAttestationCert ecAttestKey(short attestKey, byte mode){
1028+
certMode = mode;
1029+
certAttestKeySecret = attestKey;
1030+
certAttestKeyRsaPubModulus = KMType.INVALID_VALUE;
1031+
certRsaSign = false;
1032+
return this;
1033+
}
1034+
1035+
@Override
1036+
public KMAttestationCert rsaAttestKey(short attestPrivExp, short attestMod, byte mode){
10281037
certMode = mode;
1029-
certAttestKey = attestKey;
1030-
certRsaSign = rsaSign;
1038+
certAttestKeySecret = attestPrivExp;
1039+
certAttestKeyRsaPubModulus = attestMod;
1040+
certRsaSign = true;
10311041
return this;
10321042
}
10331043

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright(C) 2021 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" (short)0IS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.android.javacard.keymaster;
17+
import javacard.security.ECPrivateKey;
18+
import javacard.security.ECPublicKey;
19+
import javacard.security.KeyPair;
20+
21+
public class KMECDeviceUniqueKey implements KMDeviceUniqueKey {
22+
23+
private KeyPair ecKeyPair;
24+
25+
@Override
26+
public short getPublicKey(byte[] buf, short offset) {
27+
ECPublicKey publicKey = getPublicKey();
28+
return publicKey.getW(buf, offset);
29+
}
30+
31+
public KMECDeviceUniqueKey(KeyPair ecPair) {
32+
ecKeyPair = ecPair;
33+
}
34+
35+
public void setS(byte[] buffer, short offset, short length) {
36+
ECPrivateKey ecPriv = (ECPrivateKey) ecKeyPair.getPrivate();
37+
ecPriv.setS(buffer, offset, length);
38+
}
39+
40+
public void setW(byte[] buffer, short offset, short length) {
41+
ECPublicKey ecPublicKey = (ECPublicKey) ecKeyPair.getPublic();
42+
ecPublicKey.setW(buffer, offset, length);
43+
}
44+
45+
public ECPrivateKey getPrivateKey() {
46+
return (ECPrivateKey) ecKeyPair.getPrivate();
47+
}
48+
49+
public ECPublicKey getPublicKey() {
50+
return (ECPublicKey) ecKeyPair.getPublic();
51+
}
52+
53+
}

0 commit comments

Comments
 (0)