1 /*
2  * Copyright (C) 2016 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.security.keymaster;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 /**
26  * Utility class for the Java side of keystore-generated certificate chains.
27  *
28  * Serialization code for this must be kept in sync with system/security/keystore
29  * @hide
30  */
31 public class KeymasterCertificateChain implements Parcelable {
32 
33     private List<byte[]> mCertificates;
34 
35     public static final @android.annotation.NonNull Parcelable.Creator<KeymasterCertificateChain> CREATOR = new
36             Parcelable.Creator<KeymasterCertificateChain>() {
37                 public KeymasterCertificateChain createFromParcel(Parcel in) {
38                     return new KeymasterCertificateChain(in);
39                 }
40                 public KeymasterCertificateChain[] newArray(int size) {
41                     return new KeymasterCertificateChain[size];
42                 }
43             };
44 
KeymasterCertificateChain()45     public KeymasterCertificateChain() {
46         mCertificates = null;
47     }
48 
KeymasterCertificateChain(List<byte[]> mCertificates)49     public KeymasterCertificateChain(List<byte[]> mCertificates) {
50         this.mCertificates = mCertificates;
51     }
52 
KeymasterCertificateChain(Parcel in)53     private KeymasterCertificateChain(Parcel in) {
54         readFromParcel(in);
55     }
56 
57     /**
58      * Makes a shallow copy of other by copying the reference to the certificate chain list.
59      * @param other
60      */
shallowCopyFrom(KeymasterCertificateChain other)61     public void shallowCopyFrom(KeymasterCertificateChain other) {
62         this.mCertificates = other.mCertificates;
63     }
64 
getCertificates()65     public List<byte[]> getCertificates() {
66         return mCertificates;
67     }
68 
69     @Override
writeToParcel(Parcel out, int flags)70     public void writeToParcel(Parcel out, int flags) {
71         if (mCertificates == null) {
72             out.writeInt(0);
73         } else {
74             out.writeInt(mCertificates.size());
75             for (byte[] arg : mCertificates) {
76                 out.writeByteArray(arg);
77             }
78         }
79     }
80 
readFromParcel(Parcel in)81     public void readFromParcel(Parcel in) {
82         int length = in.readInt();
83         mCertificates = new ArrayList<byte[]>(length);
84         for (int i = 0; i < length; i++) {
85             mCertificates.add(in.createByteArray());
86         }
87     }
88 
89     @Override
describeContents()90     public int describeContents() {
91         return 0;
92     }
93 }
94