VerbatimX509Certificate.java revision 10d19e98081bec0e2ee5274f2c46d02d036ce89f
1/* 2 * Copyright (C) 2018 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 17package android.util.apk; 18 19import java.security.cert.CertificateEncodingException; 20import java.security.cert.X509Certificate; 21import java.util.Arrays; 22 23/** 24 * For legacy reasons we need to return exactly the original encoded certificate bytes, instead 25 * of letting the underlying implementation have a shot at re-encoding the data. 26 */ 27class VerbatimX509Certificate extends WrappedX509Certificate { 28 private final byte[] mEncodedVerbatim; 29 private int mHash = -1; 30 31 VerbatimX509Certificate(X509Certificate wrapped, byte[] encodedVerbatim) { 32 super(wrapped); 33 this.mEncodedVerbatim = encodedVerbatim; 34 } 35 36 @Override 37 public byte[] getEncoded() throws CertificateEncodingException { 38 return mEncodedVerbatim; 39 } 40 41 @Override 42 public boolean equals(Object o) { 43 if (this == o) return true; 44 if (!(o instanceof VerbatimX509Certificate)) return false; 45 46 try { 47 byte[] a = this.getEncoded(); 48 byte[] b = ((VerbatimX509Certificate) o).getEncoded(); 49 return Arrays.equals(a, b); 50 } catch (CertificateEncodingException e) { 51 return false; 52 } 53 } 54 55 @Override 56 public int hashCode() { 57 if (mHash == -1) { 58 try { 59 mHash = Arrays.hashCode(this.getEncoded()); 60 } catch (CertificateEncodingException e) { 61 mHash = 0; 62 } 63 } 64 return mHash; 65 } 66} 67