1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package android.keystore.cts;
16 
17 import org.bouncycastle.asn1.ASN1Encodable;
18 import org.bouncycastle.asn1.ASN1Sequence;
19 
20 import java.security.cert.CertificateParsingException;
21 
22 import java.io.UnsupportedEncodingException;
23 
24 public class AttestationPackageInfo implements java.lang.Comparable<AttestationPackageInfo> {
25     private static final int PACKAGE_NAME_INDEX = 0;
26     private static final int VERSION_INDEX = 1;
27 
28     private final String packageName;
29     private final long version;
30 
AttestationPackageInfo(String packageName, long version)31     public AttestationPackageInfo(String packageName, long version) {
32         this.packageName = packageName;
33         this.version = version;
34     }
35 
AttestationPackageInfo(ASN1Encodable asn1Encodable)36     public AttestationPackageInfo(ASN1Encodable asn1Encodable) throws CertificateParsingException {
37         if (!(asn1Encodable instanceof ASN1Sequence)) {
38             throw new CertificateParsingException(
39                     "Expected sequence for AttestationPackageInfo, found "
40                             + asn1Encodable.getClass().getName());
41         }
42 
43         ASN1Sequence sequence = (ASN1Sequence) asn1Encodable;
44         try {
45             packageName = Asn1Utils.getStringFromAsn1OctetStreamAssumingUTF8(
46                     sequence.getObjectAt(PACKAGE_NAME_INDEX));
47         } catch (UnsupportedEncodingException e) {
48             throw new CertificateParsingException(
49                     "Converting octet stream to String triggered an UnsupportedEncodingException",
50                     e);
51         }
52         version = Asn1Utils.getLongFromAsn1(sequence.getObjectAt(VERSION_INDEX));
53     }
54 
getPackageName()55     public String getPackageName() {
56         return packageName;
57     }
58 
getVersion()59     public long getVersion() {
60         return version;
61     }
62 
63     @Override
toString()64     public String toString() {
65         return new StringBuilder().append("Package name: ").append(getPackageName())
66                 .append("\nVersion: " + getVersion()).toString();
67     }
68 
69     @Override
compareTo(AttestationPackageInfo other)70     public int compareTo(AttestationPackageInfo other) {
71         int res = packageName.compareTo(other.packageName);
72         if (res != 0) return res;
73         res = Long.compare(version, other.version);
74         if (res != 0) return res;
75         return res;
76     }
77 
78     @Override
equals(Object o)79     public boolean equals(Object o) {
80         return (o instanceof AttestationPackageInfo)
81                 && (0 == compareTo((AttestationPackageInfo) o));
82     }
83 }
84