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.net.wifi;
18 
19 import android.annotation.NonNull;
20 import android.os.Parcel;
21 
22 import com.android.modules.utils.build.SdkLevel;
23 
24 import java.io.ByteArrayInputStream;
25 import java.security.KeyFactory;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.PrivateKey;
28 import java.security.cert.CertificateEncodingException;
29 import java.security.cert.CertificateException;
30 import java.security.cert.CertificateFactory;
31 import java.security.cert.X509Certificate;
32 import java.security.spec.InvalidKeySpecException;
33 import java.security.spec.PKCS8EncodedKeySpec;
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 /**
38  * Provides utilities for writing/reading a non-Parcelable objects to/from
39  * a Parcel object.
40  *
41  * @hide
42  */
43 public class ParcelUtil {
44     /**
45      * Write a PrivateKey object |key| to the specified Parcel |dest|.
46      *
47      * Below is the data format:
48      * |algorithm|     -> String of algorithm name
49      * |endcodedKey|  -> byte[] of key data
50      *
51      * For a null PrivateKey object, a null string will be written to |algorithm| and
52      * |encodedKey| will be skipped. Since a PrivateKey can only be constructed with
53      * a valid algorithm String.
54      *
55      * @param dest Parcel object to write to
56      * @param key PrivateKey object to read from.
57      */
writePrivateKey(Parcel dest, PrivateKey key)58     public static void writePrivateKey(Parcel dest, PrivateKey key) {
59         if (key == null) {
60             dest.writeString(null);
61             return;
62         }
63 
64         dest.writeString(key.getAlgorithm());
65         dest.writeByteArray(key.getEncoded());
66     }
67 
68     /**
69      * Read/create a PrivateKey object from a specified Parcel object |in|.
70      *
71      * Refer to the function above for the expected data format.
72      *
73      * @param in Parcel object to read from
74      * @return a PrivateKey object or null
75      */
readPrivateKey(Parcel in)76     public static PrivateKey readPrivateKey(Parcel in) {
77         String algorithm = in.readString();
78         if (algorithm == null) {
79             return null;
80         }
81 
82         byte[] userKeyBytes = in.createByteArray();
83         try {
84             KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
85             return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(userKeyBytes));
86        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
87             return null;
88        }
89     }
90 
91     /**
92      * Write a X509Certificate object |cert| to a Parcel object |dest|.
93      * The data being written to the Parcel is just a byte[] of the encoded certificate data.
94      *
95      * @param dest Parcel object to write to
96      * @param cert X509Certificate object to read from
97      */
writeCertificate(Parcel dest, X509Certificate cert)98     public static void writeCertificate(Parcel dest, X509Certificate cert) {
99         byte[] certBytes = null;
100         if (cert != null) {
101             try {
102                 certBytes = cert.getEncoded();
103             } catch (CertificateEncodingException e) {
104                 /* empty, write null. */
105             }
106         }
107         dest.writeByteArray(certBytes);
108     }
109 
110     /**
111      * Read/create a X509Certificate object from a specified Parcel object |in|.
112      *
113      * @param in Parcel object to read from
114      * @return a X509Certficate object or null
115      */
readCertificate(Parcel in)116     public static X509Certificate readCertificate(Parcel in) {
117         byte[] certBytes = in.createByteArray();
118         if (certBytes == null) {
119             return null;
120         }
121 
122         try {
123             CertificateFactory cFactory = CertificateFactory.getInstance("X.509");
124             return (X509Certificate) cFactory
125                     .generateCertificate(new ByteArrayInputStream(certBytes));
126         } catch (CertificateException e) {
127             return null;
128         }
129     }
130 
131     /**
132      * Write an array of X509Certificate objects |certs| to a Parcel object |dest|.
133      * The data being written to the Parcel are consist of an integer indicating
134      * the size of the array and the certificates data.  Certificates data will be
135      * skipped for a null array or size of 0 array.
136      *
137      * @param dest Parcel object to write to
138      * @param certs array of X509Certificate objects to read from
139      */
writeCertificates(Parcel dest, X509Certificate[] certs)140     public static void writeCertificates(Parcel dest, X509Certificate[] certs) {
141         if (certs == null || certs.length == 0) {
142             dest.writeInt(0);
143             return;
144         }
145 
146         dest.writeInt(certs.length);
147         for (int i = 0; i < certs.length; i++) {
148             writeCertificate(dest, certs[i]);
149         }
150     }
151 
152     /**
153      * Read/create an array of X509Certificate objects from a specified Parcel object |in|.
154      *
155      * @param in Parcel object to read from
156      * @return X509Certficate[] or null
157      */
readCertificates(Parcel in)158     public static X509Certificate[] readCertificates(Parcel in) {
159         int length = in.readInt();
160         if (length == 0) {
161             return null;
162         }
163 
164         X509Certificate[] certs = new X509Certificate[length];
165         for (int i = 0; i < length; i++) {
166             certs[i] = readCertificate(in);
167         }
168         return certs;
169     }
170 
171     /** Read List<OuiKeyedData> from a Parcel. */
172     @NonNull
readOuiKeyedDataList(@onNull Parcel in)173     public static List<OuiKeyedData> readOuiKeyedDataList(@NonNull Parcel in) {
174         List<OuiKeyedData> dataList = new ArrayList<>();
175         if (SdkLevel.isAtLeastT()) {
176             in.readList(dataList, OuiKeyedData.class.getClassLoader(), OuiKeyedData.class);
177         } else {
178             in.readList(dataList, OuiKeyedData.class.getClassLoader());
179         }
180         return dataList;
181     }
182 }
183