Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
vvb2060 committed Feb 23, 2024
1 parent 26db9eb commit a17202b
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 292 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Useful links

[RKP documentation](https://cs.android.com/android/platform/superproject/+/main:hardware/interfaces/security/rkp/README.md)

[Legacy keymaster tags](https://cs.android.com/android/platform/superproject/+/main:hardware/interfaces/keymaster/3.0/types.hal)

License
-------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.bouncycastle.asn1.DEROctetString;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.cert.CertificateParsingException;
Expand Down Expand Up @@ -64,7 +63,7 @@ public static Long getLongFromAsn1(ASN1Encodable asn1Value) throws CertificatePa

public static byte[] getByteArrayFromAsn1(ASN1Encodable asn1Encodable)
throws CertificateParsingException {
if (asn1Encodable == null || !(asn1Encodable instanceof DEROctetString)) {
if (!(asn1Encodable instanceof DEROctetString)) {
throw new CertificateParsingException("Expected DEROctetString");
}
ASN1OctetString derOctectString = (ASN1OctetString) asn1Encodable;
Expand Down Expand Up @@ -122,13 +121,12 @@ public static Set<Integer> getIntegersFromAsn1Set(ASN1Encodable set)
}

public static String getStringFromAsn1OctetStreamAssumingUTF8(ASN1Encodable encodable)
throws CertificateParsingException, UnsupportedEncodingException {
if (!(encodable instanceof ASN1OctetString)) {
throws CertificateParsingException {
if (!(encodable instanceof ASN1OctetString octetString)) {
throw new CertificateParsingException(
"Expected octet string, found " + encodable.getClass().getName());
}

ASN1OctetString octetString = (ASN1OctetString) encodable;
return new String(octetString.getOctets(), StandardCharsets.UTF_8);
}

Expand All @@ -147,11 +145,10 @@ public static Date getDateFromAsn1(ASN1Primitive value) throws CertificateParsin

public static boolean getBooleanFromAsn1(ASN1Encodable value)
throws CertificateParsingException {
if (!(value instanceof ASN1Boolean)) {
if (!(value instanceof ASN1Boolean booleanValue)) {
throw new CertificateParsingException(
"Expected boolean, found " + value.getClass().getName());
}
ASN1Boolean booleanValue = (ASN1Boolean) value;
if (booleanValue.equals(ASN1Boolean.TRUE)) {
return true;
} else if (booleanValue.equals((ASN1Boolean.FALSE))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ public AttestationApplicationId(Context context)

public AttestationApplicationId(ASN1Encodable asn1Encodable)
throws CertificateParsingException {
if (!(asn1Encodable instanceof ASN1Sequence)) {
if (!(asn1Encodable instanceof ASN1Sequence sequence)) {
throw new CertificateParsingException(
"Expected sequence for AttestationApplicationId, found "
+ asn1Encodable.getClass().getName());
}

ASN1Sequence sequence = (ASN1Sequence) asn1Encodable;
packageInfos = parseAttestationPackageInfos(sequence.getObjectAt(PACKAGE_INFOS_INDEX));
// The infos must be sorted, the implementation of Comparable relies on it.
packageInfos.sort(null);
Expand Down Expand Up @@ -140,13 +139,12 @@ public boolean equals(Object o) {

private List<AttestationPackageInfo> parseAttestationPackageInfos(ASN1Encodable asn1Encodable)
throws CertificateParsingException {
if (!(asn1Encodable instanceof ASN1Set)) {
if (!(asn1Encodable instanceof ASN1Set set)) {
throw new CertificateParsingException(
"Expected set for AttestationApplicationsInfos, found "
+ asn1Encodable.getClass().getName());
}

ASN1Set set = (ASN1Set) asn1Encodable;
List<AttestationPackageInfo> result = new ArrayList<AttestationPackageInfo>();
for (ASN1Encodable e : set) {
result.add(new AttestationPackageInfo(e));
Expand All @@ -156,12 +154,11 @@ private List<AttestationPackageInfo> parseAttestationPackageInfos(ASN1Encodable

private List<byte[]> parseSignatures(ASN1Encodable asn1Encodable)
throws CertificateParsingException {
if (!(asn1Encodable instanceof ASN1Set)) {
if (!(asn1Encodable instanceof ASN1Set set)) {
throw new CertificateParsingException("Expected set for Signature digests, found "
+ asn1Encodable.getClass().getName());
}

ASN1Set set = (ASN1Set) asn1Encodable;
List<byte[]> result = new ArrayList<byte[]>();

for (ASN1Encodable e : set) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1Sequence;

import java.io.UnsupportedEncodingException;
import java.security.cert.CertificateParsingException;

public class AttestationPackageInfo implements java.lang.Comparable<AttestationPackageInfo> {
Expand All @@ -33,21 +32,14 @@ public AttestationPackageInfo(String packageName, long version) {
}

public AttestationPackageInfo(ASN1Encodable asn1Encodable) throws CertificateParsingException {
if (!(asn1Encodable instanceof ASN1Sequence)) {
if (!(asn1Encodable instanceof ASN1Sequence sequence)) {
throw new CertificateParsingException(
"Expected sequence for AttestationPackageInfo, found "
+ asn1Encodable.getClass().getName());
}

ASN1Sequence sequence = (ASN1Sequence) asn1Encodable;
try {
packageName = Asn1Utils.getStringFromAsn1OctetStreamAssumingUTF8(
sequence.getObjectAt(PACKAGE_NAME_INDEX));
} catch (UnsupportedEncodingException e) {
throw new CertificateParsingException(
"Converting octet stream to String triggered an UnsupportedEncodingException",
e);
}
packageName = Asn1Utils.getStringFromAsn1OctetStreamAssumingUTF8(
sequence.getObjectAt(PACKAGE_NAME_INDEX));
version = Asn1Utils.getLongFromAsn1(sequence.getObjectAt(VERSION_INDEX));
}

Expand Down
Loading

0 comments on commit a17202b

Please sign in to comment.