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
17package android.security.keymaster;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.ArrayList;
23import 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 */
31public class KeymasterCertificateChain implements Parcelable {
32
33    private List<byte[]> mCertificates;
34
35    public static final 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
45    public KeymasterCertificateChain() {
46        mCertificates = null;
47    }
48
49    public KeymasterCertificateChain(List<byte[]> mCertificates) {
50        this.mCertificates = mCertificates;
51    }
52
53    private KeymasterCertificateChain(Parcel in) {
54        readFromParcel(in);
55    }
56
57    public List<byte[]> getCertificates() {
58        return mCertificates;
59    }
60
61    @Override
62    public void writeToParcel(Parcel out, int flags) {
63        if (mCertificates == null) {
64            out.writeInt(0);
65        } else {
66            out.writeInt(mCertificates.size());
67            for (byte[] arg : mCertificates) {
68                out.writeByteArray(arg);
69            }
70        }
71    }
72
73    public void readFromParcel(Parcel in) {
74        int length = in.readInt();
75        mCertificates = new ArrayList<byte[]>(length);
76        for (int i = 0; i < length; i++) {
77            mCertificates.add(in.createByteArray());
78        }
79    }
80
81    @Override
82    public int describeContents() {
83        return 0;
84    }
85}
86