Skip to content

Commit b631084

Browse files
committed
DO NOT MERGE Add components for the Android Config Updater to system server.
This adds the necessary bits to verify and install configuration updates using system server. It also includes the cert pinning updater as the first user. Change-Id: I42307f58074157b33b6e01216aab10022340d449
1 parent 4f8da32 commit b631084

File tree

4 files changed

+517
-0
lines changed

4 files changed

+517
-0
lines changed

core/res/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,12 @@
18431843
</intent-filter>
18441844
</receiver>
18451845

1846+
<receiver android:name="com.android.server.updates.CertPinInstallReceiver" >
1847+
<intent-filter>
1848+
<action android:name="android.intent.action.UPDATE_PINS" />
1849+
</intent-filter>
1850+
</receiver>
1851+
18461852
<receiver android:name="com.android.server.MasterClearReceiver"
18471853
android:permission="android.permission.MASTER_CLEAR"
18481854
android:priority="100" >
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2012 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 com.android.server.updates;
18+
19+
public class CertPinInstallReceiver extends ConfigUpdateInstallReceiver {
20+
21+
public CertPinInstallReceiver() {
22+
super("/data/misc/keychain/", "pins", "metadata/", "version");
23+
}
24+
}
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/*
2+
* Copyright (C) 2012 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 com.android.server.updates;
18+
19+
import android.content.BroadcastReceiver;
20+
import android.content.ContentResolver;
21+
import android.content.Context;
22+
import android.content.Intent;
23+
import android.provider.Settings;
24+
import android.os.FileUtils;
25+
import android.util.Base64;
26+
import android.util.Slog;
27+
28+
import java.io.ByteArrayInputStream;
29+
import java.io.File;
30+
import java.io.FileNotFoundException;
31+
import java.io.FileOutputStream;
32+
import java.io.InputStream;
33+
import java.io.IOException;
34+
import java.security.cert.Certificate;
35+
import java.security.cert.CertificateException;
36+
import java.security.cert.CertificateFactory;
37+
import java.security.cert.X509Certificate;
38+
import java.security.MessageDigest;
39+
import java.security.NoSuchAlgorithmException;
40+
import java.security.Signature;
41+
import java.security.SignatureException;
42+
43+
import libcore.io.IoUtils;
44+
45+
public class ConfigUpdateInstallReceiver extends BroadcastReceiver {
46+
47+
private static final String TAG = "ConfigUpdateInstallReceiver";
48+
49+
private static final String EXTRA_CONTENT_PATH = "CONTENT_PATH";
50+
private static final String EXTRA_REQUIRED_HASH = "REQUIRED_HASH";
51+
private static final String EXTRA_SIGNATURE = "SIGNATURE";
52+
private static final String EXTRA_VERSION_NUMBER = "VERSION";
53+
54+
private static final String UPDATE_CERTIFICATE_KEY = "config_update_certificate";
55+
56+
private final File updateDir;
57+
private final File updateContent;
58+
private final File updateVersion;
59+
60+
public ConfigUpdateInstallReceiver(String updateDir, String updateContentPath,
61+
String updateMetadataPath, String updateVersionPath) {
62+
this.updateDir = new File(updateDir);
63+
this.updateContent = new File(updateDir, updateContentPath);
64+
File updateMetadataDir = new File(updateDir, updateMetadataPath);
65+
this.updateVersion = new File(updateMetadataDir, updateVersionPath);
66+
}
67+
68+
@Override
69+
public void onReceive(final Context context, final Intent intent) {
70+
new Thread() {
71+
@Override
72+
public void run() {
73+
try {
74+
// get the certificate from Settings.Secure
75+
X509Certificate cert = getCert(context.getContentResolver());
76+
// get the content path from the extras
77+
String altContent = getAltContent(intent);
78+
// get the version from the extras
79+
int altVersion = getVersionFromIntent(intent);
80+
// get the previous value from the extras
81+
String altRequiredHash = getRequiredHashFromIntent(intent);
82+
// get the signature from the extras
83+
String altSig = getSignatureFromIntent(intent);
84+
// get the version currently being used
85+
int currentVersion = getCurrentVersion();
86+
// get the hash of the currently used value
87+
String currentHash = getCurrentHash(getCurrentContent());
88+
if (!verifyVersion(currentVersion, altVersion)) {
89+
Slog.e(TAG, "New version is not greater than current version, aborting!");
90+
} else if (!verifyPreviousHash(currentHash, altRequiredHash)) {
91+
Slog.e(TAG, "Current hash did not match required value, aborting!");
92+
} else if (!verifySignature(altContent, altVersion, altRequiredHash, altSig,
93+
cert)) {
94+
Slog.e(TAG, "Signature did not verify, aborting!");
95+
} else {
96+
// install the new content
97+
Slog.i(TAG, "Found new update, installing...");
98+
install(altContent, altVersion);
99+
Slog.i(TAG, "Installation successful");
100+
}
101+
} catch (Exception e) {
102+
Slog.e(TAG, "Could not update content!", e);
103+
}
104+
}
105+
}.start();
106+
}
107+
108+
private X509Certificate getCert(ContentResolver cr) {
109+
// get the cert from settings
110+
String cert = Settings.Secure.getString(cr, UPDATE_CERTIFICATE_KEY);
111+
// convert it into a real certificate
112+
try {
113+
byte[] derCert = Base64.decode(cert.getBytes(), Base64.DEFAULT);
114+
InputStream istream = new ByteArrayInputStream(derCert);
115+
CertificateFactory cf = CertificateFactory.getInstance("X.509");
116+
return (X509Certificate) cf.generateCertificate(istream);
117+
} catch (CertificateException e) {
118+
throw new IllegalStateException("Got malformed certificate from settings, ignoring", e);
119+
}
120+
}
121+
122+
private String getContentFromIntent(Intent i) {
123+
String extraValue = i.getStringExtra(EXTRA_CONTENT_PATH);
124+
if (extraValue == null) {
125+
throw new IllegalStateException("Missing required content path, ignoring.");
126+
}
127+
return extraValue;
128+
}
129+
130+
private int getVersionFromIntent(Intent i) throws NumberFormatException {
131+
String extraValue = i.getStringExtra(EXTRA_VERSION_NUMBER);
132+
if (extraValue == null) {
133+
throw new IllegalStateException("Missing required version number, ignoring.");
134+
}
135+
return Integer.parseInt(extraValue.trim());
136+
}
137+
138+
private String getRequiredHashFromIntent(Intent i) {
139+
String extraValue = i.getStringExtra(EXTRA_REQUIRED_HASH);
140+
if (extraValue == null) {
141+
throw new IllegalStateException("Missing required previous hash, ignoring.");
142+
}
143+
return extraValue.trim();
144+
}
145+
146+
private String getSignatureFromIntent(Intent i) {
147+
String extraValue = i.getStringExtra(EXTRA_SIGNATURE);
148+
if (extraValue == null) {
149+
throw new IllegalStateException("Missing required signature, ignoring.");
150+
}
151+
return extraValue.trim();
152+
}
153+
154+
private int getCurrentVersion() throws NumberFormatException {
155+
try {
156+
String strVersion = IoUtils.readFileAsString(updateVersion.getCanonicalPath()).trim();
157+
return Integer.parseInt(strVersion);
158+
} catch (IOException e) {
159+
Slog.i(TAG, "Couldn't find current metadata, assuming first update", e);
160+
return 0;
161+
}
162+
}
163+
164+
private String getAltContent(Intent i) throws IOException {
165+
String contents = IoUtils.readFileAsString(getContentFromIntent(i));
166+
return contents.trim();
167+
}
168+
169+
private String getCurrentContent() {
170+
try {
171+
return IoUtils.readFileAsString(updateContent.getCanonicalPath()).trim();
172+
} catch (IOException e) {
173+
Slog.i(TAG, "Failed to read current content, assuming first update!", e);
174+
return null;
175+
}
176+
}
177+
178+
private static String getCurrentHash(String content) {
179+
if (content == null) {
180+
return "0";
181+
}
182+
try {
183+
MessageDigest dgst = MessageDigest.getInstance("SHA512");
184+
byte[] encoded = content.getBytes();
185+
byte[] fingerprint = dgst.digest(encoded);
186+
return IntegralToString.bytesToHexString(fingerprint, false);
187+
} catch (NoSuchAlgorithmException e) {
188+
throw new AssertionError(e);
189+
}
190+
}
191+
192+
private boolean verifyVersion(int current, int alternative) {
193+
return (current < alternative);
194+
}
195+
196+
private boolean verifyPreviousHash(String current, String required) {
197+
// this is an optional value- if the required field is NONE then we ignore it
198+
if (required.equals("NONE")) {
199+
return true;
200+
}
201+
// otherwise, verify that we match correctly
202+
return current.equals(required);
203+
}
204+
205+
private boolean verifySignature(String content, int version, String requiredPrevious,
206+
String signature, X509Certificate cert) throws Exception {
207+
Signature signer = Signature.getInstance("SHA512withRSA");
208+
signer.initVerify(cert);
209+
signer.update(content.getBytes());
210+
signer.update(Long.toString(version).getBytes());
211+
signer.update(requiredPrevious.getBytes());
212+
return signer.verify(Base64.decode(signature.getBytes(), Base64.DEFAULT));
213+
}
214+
215+
private void writeUpdate(File dir, File file, String content) {
216+
FileOutputStream out = null;
217+
File tmp = null;
218+
try {
219+
// create the temporary file
220+
tmp = File.createTempFile("journal", "", dir);
221+
// mark tmp -rw-r--r--
222+
tmp.setReadable(true, false);
223+
// write to it
224+
out = new FileOutputStream(tmp);
225+
out.write(content.getBytes());
226+
// sync to disk
227+
FileUtils.sync(out);
228+
// atomic rename
229+
tmp.renameTo(file);
230+
} catch (IOException e) {
231+
Slog.e(TAG, "Failed to write update", e);
232+
} finally {
233+
if (tmp != null) {
234+
tmp.delete();
235+
}
236+
IoUtils.closeQuietly(out);
237+
}
238+
}
239+
240+
private void install(String content, int version) {
241+
writeUpdate(updateDir, updateContent, content);
242+
writeUpdate(updateDir, updateVersion, Long.toString(version));
243+
}
244+
}

0 commit comments

Comments
 (0)