1 /* 2 * Copyright (C) 2024 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" BASIS, 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 17 package android.keystore.cts; 18 19 import org.bouncycastle.asn1.ASN1Encodable; 20 import org.bouncycastle.asn1.ASN1InputStream; 21 import org.bouncycastle.asn1.ASN1Set; 22 23 import java.io.IOException; 24 import java.security.cert.CertificateParsingException; 25 import java.util.ArrayList; 26 import java.util.List; 27 28 public class Modules { 29 // The ASN.1 schema for a module matches that of `AttestationPackageInfo`, so re-use the class. 30 private final List<AttestationPackageInfo> mModules; 31 Modules(byte[] bytes)32 public Modules(byte[] bytes) throws CertificateParsingException { 33 try (ASN1InputStream asn1InputStream = new ASN1InputStream(bytes)) { 34 ASN1Encodable asn1Encodable = asn1InputStream.readObject(); 35 if (!(asn1Encodable instanceof ASN1Set)) { 36 throw new CertificateParsingException( 37 "Expected set for Modules, found " + asn1Encodable.getClass().getName()); 38 } 39 ASN1Set set = (ASN1Set) asn1Encodable; 40 mModules = new ArrayList<AttestationPackageInfo>(); 41 for (ASN1Encodable e : set) { 42 mModules.add(new AttestationPackageInfo(e)); 43 } 44 } catch (IOException e) { 45 throw new CertificateParsingException("Failed to parse SET OF Module", e); 46 } 47 } 48 getModules()49 public List<AttestationPackageInfo> getModules() { 50 return mModules; 51 } 52 53 @Override toString()54 public String toString() { 55 StringBuilder sb = new StringBuilder(); 56 sb.append("Modules:"); 57 int noOfModules = mModules.size(); 58 int i = 1; 59 for (AttestationPackageInfo module : mModules) { 60 sb.append("\n### Module " + i + "/" + noOfModules + " ###\n"); 61 sb.append(module); 62 i += 1; 63 } 64 return sb.toString(); 65 } 66 } 67