package org.jmrtd.lds; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import org.bouncycastle.asn1.ASN1EncodableVector; import org.bouncycastle.asn1.ASN1ObjectIdentifier; import org.bouncycastle.asn1.ASN1Primitive; import org.bouncycastle.asn1.ASN1Set; import org.bouncycastle.asn1.DEROctetString; import org.bouncycastle.asn1.DLSet; import org.bouncycastle.asn1.cms.ContentInfo; import org.bouncycastle.asn1.cms.SignedData; /* loaded from: classes6.dex */ public class CardSecurityFile implements Serializable { private static final String CONTENT_TYPE_OID = "0.4.0.127.0.7.3.2.1"; private static final Logger LOGGER = Logger.getLogger("org.jmrtd"); private static final long serialVersionUID = -3535507558193769952L; private X509Certificate certificate; private String digestAlgorithm; private String digestEncryptionAlgorithm; private byte[] encryptedDigest; private Set securityInfos; public CardSecurityFile(String str, String str2, Collection collection, PrivateKey privateKey, X509Certificate x509Certificate) { this(str, str2, collection, privateKey, x509Certificate, null); } public CardSecurityFile(String str, String str2, Collection collection, PrivateKey privateKey, X509Certificate x509Certificate, String str3) { this(str, str2, collection, (byte[]) null, x509Certificate); this.encryptedDigest = SignedDataUtil.signData(str, str2, CONTENT_TYPE_OID, toContentInfo(CONTENT_TYPE_OID, collection), privateKey, str3); } public CardSecurityFile(String str, String str2, Collection collection, byte[] bArr, X509Certificate x509Certificate) { if (collection == null) { throw new IllegalArgumentException("Null securityInfos"); } if (x509Certificate == null) { throw new IllegalArgumentException("Null certificate"); } this.digestAlgorithm = str; this.digestEncryptionAlgorithm = str2; this.securityInfos = new HashSet(collection); this.encryptedDigest = bArr; this.certificate = x509Certificate; } public CardSecurityFile(InputStream inputStream) throws IOException { readContent(inputStream); } protected void readContent(InputStream inputStream) throws IOException { SignedData readSignedData = SignedDataUtil.readSignedData(inputStream); this.digestAlgorithm = SignedDataUtil.getSignerInfoDigestAlgorithm(readSignedData); this.digestEncryptionAlgorithm = SignedDataUtil.getDigestEncryptionAlgorithm(readSignedData); try { this.certificate = SignedDataUtil.getDocSigningCertificate(readSignedData); } catch (CertificateException e) { LOGGER.log(Level.SEVERE, "Exceptiong while extracting document signing certificate", (Throwable) e); } this.securityInfos = getSecurityInfos(readSignedData); this.encryptedDigest = SignedDataUtil.getEncryptedDigest(readSignedData); } protected void writeContent(OutputStream outputStream) throws IOException { try { SignedDataUtil.writeData(SignedDataUtil.createSignedData(this.digestAlgorithm, this.digestEncryptionAlgorithm, CONTENT_TYPE_OID, toContentInfo(CONTENT_TYPE_OID, this.securityInfos), this.encryptedDigest, this.certificate), outputStream); } catch (NoSuchAlgorithmException e) { LOGGER.log(Level.SEVERE, "Unsupported algorithm", (Throwable) e); throw new IOException(e.getMessage()); } catch (CertificateException e2) { LOGGER.log(Level.SEVERE, "Certificate exception during SignedData creation", (Throwable) e2); throw new IOException(e2.getMessage()); } } public byte[] getEncoded() { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); writeContent(byteArrayOutputStream); byteArrayOutputStream.close(); return byteArrayOutputStream.toByteArray(); } catch (IOException unused) { return null; } } public Collection getPACEInfos() { ArrayList arrayList = new ArrayList(this.securityInfos.size()); for (SecurityInfo securityInfo : this.securityInfos) { if (securityInfo instanceof PACEInfo) { arrayList.add((PACEInfo) securityInfo); } } return arrayList; } public Collection getChipAuthenticationInfos() { ArrayList arrayList = new ArrayList(this.securityInfos.size()); for (SecurityInfo securityInfo : this.securityInfos) { if (securityInfo instanceof ChipAuthenticationInfo) { arrayList.add((ChipAuthenticationInfo) securityInfo); } } return arrayList; } public Collection getChipAuthenticationPublicKeyInfos() { ArrayList arrayList = new ArrayList(this.securityInfos.size()); for (SecurityInfo securityInfo : this.securityInfos) { if (securityInfo instanceof ChipAuthenticationPublicKeyInfo) { arrayList.add((ChipAuthenticationPublicKeyInfo) securityInfo); } } return arrayList; } public String toString() { StringBuilder sb = new StringBuilder("CardSecurityFile ["); sb.append(this.securityInfos.toString()); sb.append("]"); return sb.toString(); } public boolean equals(Object obj) { if (obj == null || !obj.getClass().equals(getClass())) { return false; } CardSecurityFile cardSecurityFile = (CardSecurityFile) obj; Set set = this.securityInfos; if (set == null) { return cardSecurityFile.securityInfos == null; } Set set2 = cardSecurityFile.securityInfos; if (set2 == null) { return set == null; } return set.equals(set2); } public int hashCode() { return (this.securityInfos.hashCode() * 3) + 63; } private static ContentInfo toContentInfo(String str, Collection collection) { try { ASN1EncodableVector aSN1EncodableVector = new ASN1EncodableVector(); Iterator it = collection.iterator(); while (it.hasNext()) { aSN1EncodableVector.add(it.next().getDERObject()); } return new ContentInfo(new ASN1ObjectIdentifier(str), new DEROctetString(new DLSet(aSN1EncodableVector))); } catch (IOException e) { Logger logger = LOGGER; Level level = Level.SEVERE; StringBuilder sb = new StringBuilder("Error creating signedData: "); sb.append(e.getMessage()); logger.log(level, sb.toString()); throw new IllegalArgumentException("Error DER encoding the security infos"); } } private static Set getSecurityInfos(SignedData signedData) throws IOException { ASN1Primitive content = SignedDataUtil.getContent(signedData); if (!(content instanceof ASN1Set)) { StringBuilder sb = new StringBuilder("Was expecting an ASN1Set, found "); sb.append(content.getClass()); throw new IOException(sb.toString()); } ASN1Set aSN1Set = (ASN1Set) content; HashSet hashSet = new HashSet(); for (int i = 0; i < aSN1Set.size(); i++) { try { SecurityInfo securityInfo = SecurityInfo.getInstance(aSN1Set.getObjectAt(i).toASN1Primitive()); if (securityInfo == null) { LOGGER.log(Level.WARNING, "Could not parse, skipping security info"); } else { hashSet.add(securityInfo); } } catch (Exception e) { LOGGER.log(Level.WARNING, "Exception while parsing, skipping security info", (Throwable) e); } } return hashSet; } public Collection getSecurityInfos() { return this.securityInfos; } public byte[] getEncryptedDigest() { return this.encryptedDigest; } public String getDigestEncryptionAlgorithm() { return this.digestEncryptionAlgorithm; } public String getDigestAlgorithm() { return this.digestAlgorithm; } }