Skip to content

Commit 9a4a29d

Browse files
Merge pull request #51 from AvinashHedage/optimization
Optimization
2 parents 1ac564a + 185b6cf commit 9a4a29d

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

Applet/src/com/android/javacard/keymaster/KMAsn1Parser.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ public class KMAsn1Parser {
1212
public static final byte ASN1_A0_TAG = (byte) 0xA0;
1313
public static final byte ASN1_A1_TAG = (byte) 0xA1;
1414
public static final byte ASN1_BIT_STRING = 0x03;
15+
1516
public static final byte ASN1_UTF8_STRING = 0x0C;
17+
public static final byte ASN1_TELETEX_STRING = 0x14;
18+
public static final byte ASN1_PRINTABLE_STRING = 0x13;
19+
public static final byte ASN1_UNIVERSAL_STRING = 0x1C;
20+
public static final byte ASN1_BMP_STRING = 0x1E;
21+
1622
public static final byte[] EC_CURVE = {
1723
0x06,0x08,0x2a,(byte)0x86,0x48,(byte)0xce,0x3d,0x03,
1824
0x01,0x07
@@ -58,7 +64,7 @@ public short decodeSubject(short blob) {
5864
header(ASN1_SET);
5965
header(ASN1_SEQUENCE);
6066
objectIdentifier(COMMON_NAME_OID);
61-
return header(ASN1_UTF8_STRING);
67+
return subjectHeader();
6268
}
6369

6470
public short decodeEcSubjectPublicKeyInfo(short blob) {
@@ -215,6 +221,18 @@ private short header(short tag){
215221
return getLength();
216222
}
217223

224+
private short subjectHeader(){
225+
short t = getByte();
226+
if(t != ASN1_UTF8_STRING &&
227+
t != ASN1_TELETEX_STRING &&
228+
t != ASN1_PRINTABLE_STRING &&
229+
t != ASN1_UNIVERSAL_STRING &&
230+
t != ASN1_BMP_STRING) {
231+
KMException.throwIt(KMError.UNKNOWN_ERROR);
232+
}
233+
return getLength();
234+
}
235+
218236
private byte getByte(){
219237
byte d = data[cur];
220238
incrementCursor((short)1);

Applet/src/com/android/javacard/keymaster/KMDecoder.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,13 @@ private short decodeKeyParam(short exp) {
363363
obj = decode(tagClass);
364364
KMArray.cast(vals).add(arrPos++, obj);
365365
break;
366-
}catch(KMException e){
367-
if(KMException.reason() == KMError.INVALID_TAG &&
368-
!ignoreInvalidTags){
369-
KMException.throwIt(KMError.INVALID_TAG);
366+
} catch(KMException e){
367+
if (KMException.reason() == KMError.INVALID_TAG) {
368+
if(!ignoreInvalidTags){
369+
KMException.throwIt(KMError.INVALID_TAG);
370+
}
371+
}else {
372+
KMException.throwIt(KMException.reason());
370373
}
371374
break;
372375
}

Applet/src/com/android/javacard/keymaster/KMEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private void encode(short obj) {
7777
push(obj);
7878
}
7979

80-
// Use this function, when the max len
80+
// Use this function, when the max len is given
8181
public short encode(short object, byte[] buffer, short startOff, short maxLength) {
8282
scratchBuf[STACK_PTR_OFFSET] = 0;
8383
bufferRef[0] = buffer;

Applet/src/com/android/javacard/keymaster/KMKeymasterApplet.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,6 @@ private KMAttestationCert makeCommonCert(byte[] scratchPad) {
13511351
return cert;
13521352
}
13531353

1354-
13551354
private KMAttestationCert makeAttestationCert(short attKeyBlob, short attKeyParam,
13561355
short attChallenge, short issuer, byte[] scratchPad) {
13571356
KMAttestationCert cert = makeCommonCert(scratchPad);
@@ -1382,7 +1381,12 @@ private KMAttestationCert makeAttestationCert(short attKeyBlob, short attKeyPara
13821381
KMException.throwIt(KMError.INCOMPATIBLE_PURPOSE);
13831382
}
13841383
KMAsn1Parser asn1Decoder = KMAsn1Parser.instance();
1385-
short length = asn1Decoder.decodeSubject(issuer);
1384+
short length = 0;
1385+
try {
1386+
length = asn1Decoder.decodeSubject(issuer);
1387+
} catch (KMException e) {
1388+
KMException.throwIt(KMError.INVALID_ISSUER_SUBJECT_NAME);
1389+
}
13861390
if (length > KMType.MAX_SUBJECT_CN_LEN) {
13871391
KMException.throwIt(KMError.INVALID_ISSUER_SUBJECT_NAME);
13881392
}

0 commit comments

Comments
 (0)